mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.2.4.1
This commit is contained in:
63
400_xowa/src/gplx/dbs/cfgs/Db_cfg_grp.java
Normal file
63
400_xowa/src/gplx/dbs/cfgs/Db_cfg_grp.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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;
|
||||
}
|
||||
84
400_xowa/src/gplx/dbs/cfgs/Db_cfg_tbl.java
Normal file
84
400_xowa/src/gplx/dbs/cfgs/Db_cfg_tbl.java
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
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.*;
|
||||
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 void Conn_(Db_conn new_conn, boolean created, boolean version_is_1, String tbl_v1, String tbl_v2) {
|
||||
this.conn = new_conn; flds.Clear();
|
||||
String fld_prefix = "";
|
||||
if (version_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;
|
||||
}
|
||||
public void Insert(String grp, String key, String val) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Rls_reg(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));
|
||||
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);
|
||||
return rv;
|
||||
}
|
||||
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 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;
|
||||
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));
|
||||
}
|
||||
}
|
||||
finally {rdr.Rls();}
|
||||
return rv == null ? Db_cfg_grp.Null : rv;
|
||||
}
|
||||
public void Rls() {conn.Conn_term();}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ 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.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();
|
||||
|
||||
@@ -16,6 +16,7 @@ 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.schemas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.schemas.*;
|
||||
import gplx.dbs.engines.sqlite.*;
|
||||
public class Schema_update_cmd_ {
|
||||
public static Schema_update_cmd Make_tbl_create(String tbl_name, String tbl_sql, Db_idx_itm... tbl_idxs) {return new Schema_update_cmd__tbl_create(tbl_name, tbl_sql, tbl_idxs);}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user