mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.4.1.1
This commit is contained in:
54
400_xowa/src/gplx/dbs/Db_attach_cmd.java
Normal file
54
400_xowa/src/gplx/dbs/Db_attach_cmd.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.sqlite.*;
|
||||
public class Db_attach_cmd {
|
||||
private final boolean diff_db;
|
||||
private final Db_conn conn; private final String attach_name; private final Io_url attach_url;
|
||||
private final ListAdp sql_list = ListAdp_.new_();
|
||||
Db_attach_cmd(Db_conn conn, String attach_name, Io_url attach_url) {
|
||||
this.conn = conn; this.attach_name = attach_name; this.attach_url = attach_url;
|
||||
Sqlite_conn_info conn_info = (Sqlite_conn_info)conn.Conn_info();
|
||||
this.diff_db = !String_.Eq(conn_info.Url().Raw(), attach_url.Raw());
|
||||
}
|
||||
public Db_attach_cmd Add_fmt(String msg, String sql_fmt, Object... sql_args) {
|
||||
String sql = String_.Format(sql_fmt, sql_args);
|
||||
sql = String_.Replace(sql, "<attach_db>", diff_db ? attach_name + "." : ""); // replace <attach> with either "attach_db." or "";
|
||||
sql_list.Add(new Db_exec_sql_by_attach_itm(msg, sql));
|
||||
return this;
|
||||
}
|
||||
public void Exec() {
|
||||
Gfo_usr_dlg usr_dlg = Gfo_usr_dlg_.I;
|
||||
if (diff_db) conn.Env_db_attach(attach_name, attach_url);
|
||||
conn.Txn_bgn(attach_name); // NOTE: BEGIN TRAN must occur after ATTACH else sqlite will throw error
|
||||
int len = sql_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_exec_sql_by_attach_itm itm = (Db_exec_sql_by_attach_itm)sql_list.FetchAt(i);
|
||||
usr_dlg.Plog_many("", "", itm.Msg());
|
||||
conn.Exec_sql(itm.Sql());
|
||||
}
|
||||
conn.Txn_end();
|
||||
if (diff_db) conn.Env_db_detach(attach_name);
|
||||
}
|
||||
public static Db_attach_cmd new_(Db_conn conn, String attach_name, Io_url attach_url) {return new Db_attach_cmd(conn, attach_name, attach_url);}
|
||||
}
|
||||
class Db_exec_sql_by_attach_itm {
|
||||
public Db_exec_sql_by_attach_itm(String msg, String sql) {this.msg = msg; this.sql = sql;}
|
||||
public String Msg() {return msg;} private final String msg;
|
||||
public String Sql() {return sql;} private final String sql;
|
||||
}
|
||||
@@ -16,12 +16,20 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.schemas.*;
|
||||
public class Gfdb_db_base {
|
||||
public Db_conn Conn() {return conn;} private Db_conn conn;
|
||||
public Schema_db_mgr Schema() {return schema;} private Schema_db_mgr schema = new Schema_db_mgr();
|
||||
public void Init(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
schema.Init(conn);
|
||||
import gplx.dbs.engines.sqlite.*;
|
||||
public class Db_attach_rdr {
|
||||
private final boolean diff_db;
|
||||
private final Db_conn conn; private final String attach_name; private final Io_url attach_url;
|
||||
public Db_attach_rdr(Db_conn conn, String attach_name, Io_url attach_url) {
|
||||
this.conn = conn; this.attach_name = attach_name; this.attach_url = attach_url;
|
||||
Sqlite_conn_info conn_info = (Sqlite_conn_info)conn.Conn_info();
|
||||
this.diff_db = !String_.Eq(conn_info.Url().Raw(), attach_url.Raw());
|
||||
}
|
||||
public Db_rdr Exec_as_rdr(String sql) {
|
||||
if (diff_db) conn.Env_db_attach(attach_name, attach_url);
|
||||
return conn.Exec_sql_as_rdr2(sql);
|
||||
}
|
||||
public void Rls() {
|
||||
if (diff_db) conn.Env_db_detach(attach_name);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_cfg_grp {
|
||||
private OrderedHash itms = OrderedHash_.new_();
|
||||
public Db_cfg_grp(String grp) {this.grp = grp;}
|
||||
public String Grp() {return grp;} private String grp;
|
||||
public void Insert(String key, String val) {
|
||||
if (itms.Has(key)) throw Err_.new_fmt_("cfg_grp.Insert failed; key={0}", key);
|
||||
Fsm_cfg_itm itm = new Fsm_cfg_itm(grp, key, val);
|
||||
itms.Add(key, itm);
|
||||
}
|
||||
public void Update(String key, String val) {
|
||||
Fsm_cfg_itm itm = (Fsm_cfg_itm)itms.Fetch(key);
|
||||
if (itm == null) throw Err_.new_fmt_("cfg_grp.Update failed; key={0}", key);
|
||||
itm.Val_(val);
|
||||
}
|
||||
public void Upsert(String key, String val) {
|
||||
Fsm_cfg_itm itm = (Fsm_cfg_itm)itms.Fetch(key);
|
||||
if (itm == null) {
|
||||
itm = new Fsm_cfg_itm(grp, key, val);
|
||||
itms.Add(key, itm);
|
||||
}
|
||||
else
|
||||
itm.Val_(val);
|
||||
}
|
||||
public boolean Get_yn_or_y(String key) {return Get_yn_or(key, Bool_.Y);}
|
||||
public boolean Get_yn_or_n(String key) {return Get_yn_or(key, Bool_.N);}
|
||||
public boolean Get_yn_or(String key, boolean or) {
|
||||
String rv = Get_str_or(key, null);
|
||||
return rv == null ? or : Yn.parse_(rv);
|
||||
}
|
||||
public int Get_int_or(String key, int or) {
|
||||
String rv = Get_str_or(key, null);
|
||||
return rv == null ? or : Int_.parse_(rv);
|
||||
}
|
||||
public String Get_str_or(String key, String or) {
|
||||
Fsm_cfg_itm itm = (Fsm_cfg_itm)itms.Fetch(key);
|
||||
return itm == null ? or : itm.Val();
|
||||
}
|
||||
public static final Db_cfg_grp Null = new Db_cfg_grp(); Db_cfg_grp() {}
|
||||
}
|
||||
class Fsm_cfg_itm {
|
||||
public Fsm_cfg_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private String grp;
|
||||
public String Key() {return key;} private String key;
|
||||
public String Val() {return val;} public Fsm_cfg_itm Val_(String v) {val = v; return this;} private String val;
|
||||
}
|
||||
34
400_xowa/src/gplx/dbs/cfgs/Db_cfg_hash.java
Normal file
34
400_xowa/src/gplx/dbs/cfgs/Db_cfg_hash.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_cfg_hash {
|
||||
private final String grp; private final OrderedHash hash = OrderedHash_.new_();
|
||||
public Db_cfg_hash(String grp) {this.grp = grp;}
|
||||
public int Len() {return hash.Count();}
|
||||
public Db_cfg_itm Get_at(int i) {return (Db_cfg_itm)hash.FetchAt(i);}
|
||||
public Db_cfg_itm Get(String key) {
|
||||
Db_cfg_itm rv = (Db_cfg_itm)hash.Fetch(key);
|
||||
return rv == null ? Db_cfg_itm.Empty : rv;
|
||||
}
|
||||
public void Set(String key, String val) {hash.Del(key); Add(key, val);}
|
||||
public void Add(String key, String val) {
|
||||
if (hash.Has(key)) throw Err_.new_fmt_("itm exists; grp={0} key={01}", grp, key);
|
||||
Db_cfg_itm itm = new Db_cfg_itm(grp, key, val);
|
||||
hash.Add(key, itm);
|
||||
}
|
||||
}
|
||||
59
400_xowa/src/gplx/dbs/cfgs/Db_cfg_itm.java
Normal file
59
400_xowa/src/gplx/dbs/cfgs/Db_cfg_itm.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_cfg_itm {
|
||||
public Db_cfg_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private final String grp;
|
||||
public String Key() {return key;} private final String key;
|
||||
public String Val() {return val;} public Db_cfg_itm Val_(String v) {val = v; return this;} private String val;
|
||||
public String To_str_or(String or) {return val == null ? or : val;}
|
||||
public byte[] To_bry_or(byte[] or) {try {return val == null ? or : Bry_.new_utf8_(val) ;} catch (Exception e) {throw err_parse(e, Bry_.Cls_val_name);}}
|
||||
public int To_int_or(int or) {try {return val == null ? or : Int_.parse_or_(val, or) ;} catch (Exception e) {throw err_parse(e, Int_.Cls_val_name);}}
|
||||
public long To_long_or(long or) {try {return val == null ? or : Long_.parse_or_(val, or) ;} catch (Exception e) {throw err_parse(e, Long_.Cls_val_name);}}
|
||||
public byte To_byte_or(byte or) {try {return val == null ? or : Byte_.parse_or_(val, or) ;} catch (Exception e) {throw err_parse(e, Byte_.Cls_val_name);}}
|
||||
public boolean To_yn_or_n() {return To_yn_or(Bool_.N);}
|
||||
public boolean To_yn_or(boolean or) {try {return val == null ? or : Yn.parse_by_char_or(val, or);} catch (Exception e) {throw err_parse(e, Bool_.Cls_val_name);}}
|
||||
public DateAdp To_date_or(DateAdp or) {try {return val == null ? or : DateAdp_.parse_gplx(val) ;} catch (Exception e) {throw err_parse(e, DateAdp_.Cls_ref_name);}}
|
||||
public Guid_adp To_guid_or(Guid_adp or) {try {return val == null ? or : Guid_adp_.parse_(val) ;} catch (Exception e) {throw err_parse(e, Guid_adp_.Cls_ref_name);}}
|
||||
public boolean To_bool() {Fail_if_null(); try {return Yn.parse_(val) ;} catch (Exception e) {throw err_parse(e, Bool_.Cls_val_name);}}
|
||||
public byte To_byte() {Fail_if_null(); try {return Byte_.parse_(val) ;} catch (Exception e) {throw err_parse(e, Byte_.Cls_val_name);}}
|
||||
public int To_int() {Fail_if_null(); try {return Int_.parse_(val) ;} catch (Exception e) {throw err_parse(e, Int_.Cls_val_name);}}
|
||||
public String To_str() {Fail_if_null(); return val;}
|
||||
private void Fail_if_null() {if (val == null) throw Err_.new_("cfg.val is empty; grp={0} key={1}", grp, key); }
|
||||
private Err err_parse(Exception e, String type) {return Err_.new_("cfg.val is not parseable; grp={0} key={1} val={2} type={3}", grp, key, val, type);}
|
||||
|
||||
private static final String Grp_none = "";
|
||||
public static Db_cfg_itm new_str (String key, String val) {return new Db_cfg_itm(Grp_none , key, val);}
|
||||
public static Db_cfg_itm new_str (String grp, String key, String val) {return new Db_cfg_itm(grp , key, val);}
|
||||
public static Db_cfg_itm new_bry (String key, byte[] val) {return new Db_cfg_itm(Grp_none , key, String_.new_utf8_(val));}
|
||||
public static Db_cfg_itm new_bry (String grp, String key, byte[] val) {return new Db_cfg_itm(grp , key, String_.new_utf8_(val));}
|
||||
public static Db_cfg_itm new_int (String key, int val) {return new Db_cfg_itm(Grp_none , key, Int_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_int (String grp, String key, int val) {return new Db_cfg_itm(grp , key, Int_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_long (String key, long val) {return new Db_cfg_itm(Grp_none , key, Long_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_long (String grp, String key, long val) {return new Db_cfg_itm(grp , key, Long_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_byte (String key, byte val) {return new Db_cfg_itm(Grp_none , key, Byte_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_byte (String grp, String key, byte val) {return new Db_cfg_itm(grp , key, Byte_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_yn (String key, boolean val) {return new Db_cfg_itm(Grp_none , key, Yn.Xto_str(val));}
|
||||
public static Db_cfg_itm new_yn (String grp, String key, boolean val) {return new Db_cfg_itm(grp , key, Yn.Xto_str(val));}
|
||||
public static Db_cfg_itm new_DateAdp (String key, DateAdp val) {return new Db_cfg_itm(Grp_none , key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public static Db_cfg_itm new_DateAdp (String grp, String key, DateAdp val) {return new Db_cfg_itm(grp , key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public static Db_cfg_itm new_guid (String key, Guid_adp val) {return new Db_cfg_itm(Grp_none , key, val.XtoStr());}
|
||||
public static Db_cfg_itm new_guid (String grp, String key, Guid_adp val) {return new Db_cfg_itm(grp , key, val.XtoStr());}
|
||||
|
||||
public static final Db_cfg_itm Empty = new Db_cfg_itm("empty", "empty", null);
|
||||
}
|
||||
@@ -16,72 +16,104 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.*;
|
||||
public class Db_cfg_tbl {
|
||||
private String tbl_name; private final Db_meta_fld_list flds = Db_meta_fld_list.new_();
|
||||
private String fld_grp, fld_key, fld_val;
|
||||
private Db_conn conn; private Db_stmt stmt_insert, stmt_update, stmt_select;
|
||||
public Db_conn Conn() {return conn;}
|
||||
public void Conn_(Db_conn new_conn, boolean created, boolean schema_is_1, String tbl_v1, String tbl_v2) {
|
||||
this.conn = new_conn; flds.Clear();
|
||||
String fld_prefix = "";
|
||||
if (schema_is_1) {
|
||||
tbl_name = tbl_v1;
|
||||
fld_prefix = "cfg_";
|
||||
}
|
||||
else
|
||||
tbl_name = tbl_v2;
|
||||
fld_grp = flds.Add_str(fld_prefix + "grp", 255);
|
||||
fld_key = flds.Add_str(fld_prefix + "key", 255);
|
||||
fld_val = flds.Add_str(fld_prefix + "val", 1024);
|
||||
if (created) {
|
||||
Db_meta_tbl meta = Db_meta_tbl.new_(tbl_name, flds
|
||||
, Db_meta_idx.new_unique_by_tbl(tbl_name, "main", fld_grp, fld_key, fld_val)
|
||||
);
|
||||
conn.Exec_create_tbl_and_idx(meta);
|
||||
}
|
||||
stmt_insert = stmt_update = stmt_select = null;
|
||||
import gplx.core.primitives.*;
|
||||
public class Db_cfg_tbl implements RlsAble {
|
||||
private final String tbl_name; private final Db_meta_fld_list flds = Db_meta_fld_list.new_();
|
||||
private final String fld_grp, fld_key, fld_val;
|
||||
private Db_stmt stmt_insert, stmt_update, stmt_select;
|
||||
public Db_conn Conn() {return conn;} private final Db_conn conn;
|
||||
public Db_cfg_tbl(Db_conn conn, String tbl_name) {
|
||||
this.conn = conn; this.tbl_name = tbl_name;
|
||||
this.fld_grp = flds.Add_str("cfg_grp", 255);
|
||||
this.fld_key = flds.Add_str("cfg_key", 255);
|
||||
this.fld_val = flds.Add_str("cfg_val", 1024);
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public void Insert(String grp, String key, String val) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Rls_reg(conn.Stmt_insert(tbl_name, flds));
|
||||
public void Rls() {
|
||||
stmt_insert = Db_stmt_.Rls(stmt_insert);
|
||||
stmt_update = Db_stmt_.Rls(stmt_update);
|
||||
stmt_select = Db_stmt_.Rls(stmt_select);
|
||||
}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Db_meta_tbl.new_(tbl_name, flds, Db_meta_idx.new_unique_by_tbl(tbl_name, "main", fld_grp, fld_key, fld_val)));}
|
||||
public void Delete_val(String grp, String key) {conn.Stmt_delete(tbl_name, fld_grp, fld_key).Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_delete();}
|
||||
public void Delete_grp(String grp) {conn.Stmt_delete(tbl_name, fld_grp).Crt_str(fld_grp, grp).Exec_delete();}
|
||||
public void Delete_all() {conn.Stmt_delete(tbl_name, Db_meta_fld.Ary_empy).Exec_delete();}
|
||||
public void Insert_yn (String grp, String key, boolean val) {Insert_str(grp, key, val ? "y" : "n");}
|
||||
public void Insert_byte (String grp, String key, byte val) {Insert_str(grp, key, Byte_.Xto_str(val));}
|
||||
public void Insert_int (String grp, String key, int val) {Insert_str(grp, key, Int_.Xto_str(val));}
|
||||
public void Insert_long (String grp, String key, long val) {Insert_str(grp, key, Long_.Xto_str(val));}
|
||||
public void Insert_date (String grp, String key, DateAdp val) {Insert_str(grp, key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public void Insert_guid (String grp, String key, Guid_adp val) {Insert_str(grp, key, val.XtoStr());}
|
||||
public void Insert_bry (String grp, String key, byte[] val) {Insert_str(grp, key, String_.new_utf8_(val));}
|
||||
public void Insert_str (String grp, String key, String val) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear().Val_str(fld_grp, grp).Val_str(fld_key, key).Val_str(fld_val, val).Exec_insert();
|
||||
}
|
||||
public void Update(String grp, String key, String val) {
|
||||
if (stmt_update == null) stmt_update = conn.Rls_reg(conn.Stmt_update_exclude(tbl_name, flds, fld_grp, fld_key));
|
||||
}
|
||||
public void Update_yn (String grp, String key, boolean val) {Update_str(grp, key, val ? "y" : "n");}
|
||||
public void Update_byte (String grp, String key, byte val) {Update_str(grp, key, Byte_.Xto_str(val));}
|
||||
public void Update_int (String grp, String key, int val) {Update_str(grp, key, Int_.Xto_str(val));}
|
||||
public void Update_long (String grp, String key, long val) {Update_str(grp, key, Long_.Xto_str(val));}
|
||||
public void Update_date (String grp, String key, DateAdp val) {Update_str(grp, key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public void Update_guid (String grp, String key, Guid_adp val) {Update_str(grp, key, val.XtoStr());}
|
||||
public void Update_bry (String grp, String key, byte[] val) {Update_str(grp, key, String_.new_utf8_(val));}
|
||||
public void Update_str (String grp, String key, String val) {
|
||||
if (stmt_update == null) stmt_update = conn.Stmt_update_exclude(tbl_name, flds, fld_grp, fld_key);
|
||||
stmt_update.Clear().Val_str(fld_val, val).Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_update();
|
||||
}
|
||||
public int Select_as_int_or_fail(String grp, String key) {
|
||||
int rv = Select_as_int_or(grp, key, Int_.MinValue);
|
||||
if (rv == Int_.MinValue) throw Err_.new_fmt_("dbs.cfg_tbl.Select_as_int_or_fail: tbl={0} grp={1} key={2}", tbl_name, grp, key);
|
||||
public void Upsert_str (String grp, String key, String val) {
|
||||
String cur_val = this.Select_str_or(grp, key, null);
|
||||
if (cur_val == null) this.Insert_str(grp, key, val);
|
||||
else this.Update_str(grp, key, val);
|
||||
}
|
||||
public boolean Select_yn (String grp, String key) {String val = Select_str(grp, key); return Parse_yn (grp, key, val);}
|
||||
public byte Select_byte (String grp, String key) {String val = Select_str(grp, key); return Parse_byte (grp, key, val);}
|
||||
public int Select_int (String grp, String key) {String val = Select_str(grp, key); return Parse_int (grp, key, val);}
|
||||
public long Select_long (String grp, String key) {String val = Select_str(grp, key); return Parse_long (grp, key, val);}
|
||||
public byte[] Select_bry (String grp, String key) {String val = Select_str(grp, key); return Parse_bry (grp, key, val);}
|
||||
public DateAdp Select_date (String grp, String key) {String val = Select_str(grp, key); return Parse_date (grp, key, val);}
|
||||
public Guid_adp Select_guid (String grp, String key) {String val = Select_str(grp, key); return Parse_guid (grp, key, val);}
|
||||
public boolean Select_yn_or (String grp, String key, boolean or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_yn (grp, key, val);}
|
||||
public byte Select_byte_or (String grp, String key, byte or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_byte (grp, key, val);}
|
||||
public int Select_int_or (String grp, String key, int or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_int (grp, key, val);}
|
||||
public long Select_long_or (String grp, String key, long or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_long (grp, key, val);}
|
||||
public byte[] Select_bry_or (String grp, String key, byte[] or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_bry (grp, key, val);}
|
||||
public DateAdp Select_date_or (String grp, String key, DateAdp or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_date (grp, key, val);}
|
||||
public Guid_adp Select_guid_or (String grp, String key, Guid_adp or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_guid (grp, key, val);}
|
||||
public String Select_str (String grp, String key) {
|
||||
String rv = Select_str_or(grp, key, null); if (rv == null) throw Err_.new_("cfg.missing; grp={0} key={1}", grp, key);
|
||||
return rv;
|
||||
}
|
||||
public long Select_as_long_or(String grp, String key, long or) {return Long_.parse_or_(Select_as_str_or(grp, key, null), or);}
|
||||
public byte Select_as_byte_or(String grp, String key, byte or) {return Byte_.parse_or_(Select_as_str_or(grp, key, null), or);}
|
||||
public int Select_as_int_or(String grp, String key, int or) {return Int_.parse_or_(Select_as_str_or(grp, key, null), or);}
|
||||
public String Select_as_str_or(String grp, String key, String or) {
|
||||
if (stmt_select == null) stmt_select = conn.Rls_reg(conn.Stmt_select(tbl_name, String_.Ary(fld_val), fld_grp, fld_key));
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
try {
|
||||
rdr = stmt_select.Clear()
|
||||
.Crt_str(fld_grp, grp)
|
||||
.Crt_str(fld_key, key)
|
||||
.Exec_select_as_rdr();
|
||||
return rdr.Move_next() ? rdr.Read_str(fld_val) : or;
|
||||
} finally {rdr.Rls();}
|
||||
public String Select_str_or (String grp, String key, String or) {
|
||||
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, String_.Ary(fld_val), fld_grp, fld_key);
|
||||
Db_rdr rdr = stmt_select.Clear().Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_select__rls_manual();
|
||||
try {return rdr.Move_next() ? rdr.Read_str(fld_val) : or;} finally {rdr.Rls();}
|
||||
}
|
||||
public Db_cfg_grp Select_as_grp(String grp) {
|
||||
Db_cfg_grp rv = null;
|
||||
Db_stmt stmt = conn.Stmt_select(tbl_name, flds, fld_grp);
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
public Db_cfg_hash Select_as_hash(String grp) {
|
||||
Db_cfg_hash rv = new Db_cfg_hash(grp);
|
||||
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld_grp).Crt_str(fld_grp, grp).Exec_select__rls_auto();
|
||||
try {
|
||||
rdr = stmt.Clear().Crt_str(fld_grp, grp).Exec_select_as_rdr();
|
||||
while (rdr.Move_next()) {
|
||||
if (rv == null) rv = new Db_cfg_grp(grp);
|
||||
rv.Upsert(rdr.Read_str(fld_key), rdr.Read_str(fld_val));
|
||||
rv.Add(rdr.Read_str(fld_key), rdr.Read_str(fld_val));
|
||||
}
|
||||
}
|
||||
finally {rdr.Rls();}
|
||||
return rv == null ? Db_cfg_grp.Null : rv;
|
||||
return rv;
|
||||
}
|
||||
public void Rls() {conn.Conn_term();}
|
||||
// NOTE: Assert guarantees that a value exists in database and returns it (Select + Insert); (1) String val = Assert('grp', 'key', 'val'); (2) Update('grp', 'key', 'val2');
|
||||
public boolean Assert_yn (String grp, String key, boolean or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_yn (grp, key, or); return or;} return Parse_yn (grp, key, val);}
|
||||
public byte Assert_byte (String grp, String key, byte or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_byte (grp, key, or); return or;} return Parse_byte (grp, key, val);}
|
||||
public int Assert_int (String grp, String key, int or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_int (grp, key, or); return or;} return Parse_int (grp, key, val);}
|
||||
public long Assert_long (String grp, String key, long or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_long (grp, key, or); return or;} return Parse_long (grp, key, val);}
|
||||
public byte[] Assert_bry (String grp, String key, byte[] or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_bry (grp, key, or); return or;} return Parse_bry (grp, key, val);}
|
||||
public DateAdp Assert_date (String grp, String key, DateAdp or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_date (grp, key, or); return or;} return Parse_date (grp, key, val);}
|
||||
public Guid_adp Assert_guid (String grp, String key, Guid_adp or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_guid (grp, key, or); return or;} return Parse_guid (grp, key, val);}
|
||||
public String Assert_str (String grp, String key, String or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_str (grp, key, or); return or;} return val;}
|
||||
private boolean Parse_yn (String grp, String key, String val) {try {return Yn.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Bool_.Cls_val_name);}}
|
||||
private byte Parse_byte (String grp, String key, String val) {try {return Byte_.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Byte_.Cls_val_name);}}
|
||||
private int Parse_int (String grp, String key, String val) {try {return Int_.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Int_.Cls_val_name);}}
|
||||
private long Parse_long (String grp, String key, String val) {try {return Long_.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Long_.Cls_val_name);}}
|
||||
private byte[] Parse_bry (String grp, String key, String val) {try {return Bry_.new_utf8_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Bry_.Cls_val_name);}}
|
||||
private DateAdp Parse_date (String grp, String key, String val) {try {return DateAdp_.parse_gplx(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, DateAdp_.Cls_ref_name);}}
|
||||
private Guid_adp Parse_guid (String grp, String key, String val) {try {return Guid_adp_.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Guid_adp_.Cls_ref_name);}}
|
||||
private Err err_parse(Exception e, String grp, String key, String val, String type) {return Err_.new_("cfg.val is not parseable; grp={0} key={1} val={2} type={3}", grp, key, val, type);}
|
||||
}
|
||||
|
||||
@@ -18,31 +18,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.schemas; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Schema_loader_mgr_ {
|
||||
public static final Schema_loader_mgr Null = new Schema_loader_mgr__null();
|
||||
public static final Schema_loader_mgr Sqlite = new Schema_loader_mgr__sqlite();
|
||||
public static final Schema_loader_mgr Null = new Schema_loader_mgr__null();
|
||||
public static final Schema_loader_mgr Sqlite = new Schema_loader_mgr__sqlite();
|
||||
}
|
||||
class Schema_loader_mgr__null implements Schema_loader_mgr {
|
||||
public void Load(Schema_db_mgr db_mgr, Db_conn conn) {}
|
||||
}
|
||||
class Schema_loader_mgr__sqlite implements Schema_loader_mgr {
|
||||
public void Load(Schema_db_mgr db_mgr, Db_conn conn) {
|
||||
Gfo_usr_dlg_._.Log_many("", "", "db.schema.load.bgn: conn=~{0}", conn.Url().Xto_api());
|
||||
Gfo_usr_dlg_.I.Log_many("", "", "db.schema.load.bgn: conn=~{0}", conn.Conn_info().Xto_api());
|
||||
Schema_tbl_mgr tbl_mgr = db_mgr.Tbl_mgr();
|
||||
Db_qry__select_in_tbl qry = Db_qry__select_in_tbl.new_("sqlite_master", String_.Ary_empty, String_.Ary("type", "name", "sql"), Db_qry__select_in_tbl.Order_by_null);
|
||||
Db_stmt stmt = Db_stmt_.new_select_as_rdr(conn, qry);
|
||||
Db_rdr rdr = stmt.Exec_select_as_rdr();
|
||||
while (rdr.Move_next()) {
|
||||
int type = Schema_itm_tid.Xto_int(rdr.Read_str(0));
|
||||
switch (type) {
|
||||
case Schema_itm_tid.Tid_table:
|
||||
Schema_tbl_itm tbl_itm = new Schema_tbl_itm(rdr.Read_str(1), rdr.Read_str(2));
|
||||
tbl_mgr.Add(tbl_itm);
|
||||
break;
|
||||
case Schema_itm_tid.Tid_index: break; // noop for now
|
||||
default: throw Err_.unhandled(type);
|
||||
Db_rdr rdr = conn.Stmt_new(qry).Exec_select__rls_auto();
|
||||
try {
|
||||
while (rdr.Move_next()) {
|
||||
int type = Schema_itm_tid.Xto_int(rdr.Read_str(0));
|
||||
switch (type) {
|
||||
case Schema_itm_tid.Tid_table:
|
||||
Schema_tbl_itm tbl_itm = new Schema_tbl_itm(rdr.Read_str(1), rdr.Read_str(2));
|
||||
tbl_mgr.Add(tbl_itm);
|
||||
break;
|
||||
case Schema_itm_tid.Tid_index: break; // noop for now
|
||||
default: throw Err_.unhandled(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
rdr.Rls();
|
||||
Gfo_usr_dlg_._.Log_many("", "", "db.schema.load.end");
|
||||
} finally {rdr.Rls();}
|
||||
Gfo_usr_dlg_.I.Log_many("", "", "db.schema.load.end");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ class Schema_update_cmd__tbl_create implements Schema_update_cmd {
|
||||
public boolean Exec_is_done() {return exec_is_done;} private boolean exec_is_done;
|
||||
public void Exec(Schema_db_mgr db_mgr, Db_conn conn) {
|
||||
if (db_mgr.Tbl_mgr().Has(tbl_name)) return;
|
||||
Gfo_usr_dlg_._.Log_many("", "", "schema.tbl.create: tbl=~{0}", tbl_name);
|
||||
Gfo_usr_dlg_.I.Log_many("", "", "schema.tbl.create: tbl=~{0}", tbl_name);
|
||||
Sqlite_engine_.Tbl_create(conn, tbl_name, tbl_sql);
|
||||
Sqlite_engine_.Idx_create(conn, tbl_idxs);
|
||||
exec_is_done = true;
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Schema_update_mgr {
|
||||
Schema_update_cmd cmd = (Schema_update_cmd)cmds.FetchAt(i);
|
||||
try {cmd.Exec(schema_mgr, conn);}
|
||||
catch (Exception e) {
|
||||
Gfo_usr_dlg_._.Warn_many("", "", "failed to run update cmd; name=~{0} err=~{1}", cmd.Name(), Err_.Message_gplx_brief(e));
|
||||
Gfo_usr_dlg_.I.Warn_many("", "", "failed to run update cmd; name=~{0} err=~{1}", cmd.Name(), Err_.Message_gplx_brief(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class Schema_update_mgr_fxt {
|
||||
public void Test_exec_n(Schema_update_cmd cmd) {Test_exec(cmd, Bool_.N);}
|
||||
private void Test_exec(Schema_update_cmd cmd, boolean expd) {
|
||||
update_mgr.Add(cmd);
|
||||
update_mgr.Update(db_mgr, Db_conn_.Null);
|
||||
update_mgr.Update(db_mgr, Db_conn_.Empty);
|
||||
Tfds.Eq(expd, cmd.Exec_is_done());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user