1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-07-12 21:10:02 -04:00
commit 794b5a232f
3099 changed files with 238212 additions and 0 deletions

View 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 List_adp sql_list = List_adp_.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.Get_at(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;
}

View File

@@ -0,0 +1,40 @@
/*
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_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 void Attach() {
try {
if (diff_db) conn.Env_db_attach(attach_name, attach_url);
} catch (Exception e) {Exc_.Noop(e); Gfo_usr_dlg_.I.Warn_many("", "", "db:failed to attach db; name=~{0} url=~{1}", attach_name, attach_url.Raw());}
}
public Db_rdr Exec_as_rdr(String sql) {
sql = String_.Replace(sql, "<attach_db>", diff_db ? attach_name + "." : ""); // replace <attach> with either "attach_db." or "";
return conn.Exec_sql_as_rdr_v2(sql);
}
public void Detach() {
if (diff_db) conn.Env_db_detach(attach_name);
}
}

View 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 Ordered_hash hash = Ordered_hash_.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.Get_at(i);}
public Db_cfg_itm Get(String key) {
Db_cfg_itm rv = (Db_cfg_itm)hash.Get_by(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 Exc_.new_("itm exists", "grp", grp, "key", key);
Db_cfg_itm itm = new Db_cfg_itm(grp, key, val);
hash.Add(key, itm);
}
}

View 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_u8(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 Exc_.new_("cfg.val is empty", "grp", grp, "key", key); }
private Exc err_parse(Exception e, String type) {return Exc_.new_("cfg.val is not parseable", "grp", grp, "key", key, "val", val, "type", type).Stack_erase_1_();}
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_u8(val));}
public static Db_cfg_itm new_bry (String grp, String key, byte[] val) {return new Db_cfg_itm(grp , key, String_.new_u8(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);
}

View File

@@ -0,0 +1,123 @@
/*
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.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 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_empty).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_u8(val));}
public void Insert_str (String grp, String key, String val) {
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
try {
stmt_insert.Clear().Val_str(fld_grp, grp).Val_str(fld_key, key).Val_str(fld_val, val).Exec_insert();
} catch (Exception e) {throw Exc_.new_exc(e, "db", "db_cfg.insert failed", "grp", grp, "key", key, "val", val, "db", conn.Conn_info().Xto_api());}
}
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_u8(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 void Upsert_yn (String grp, String key, boolean val) {Upsert_str(grp, key, val ? "y" : "n");}
public void Upsert_int (String grp, String key, int val) {Upsert_str(grp, key, Int_.Xto_str(val));}
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 Exc_.new_("cfg.missing", "grp", grp, "key", key);
return rv;
}
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_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 {
while (rdr.Move_next()) {
rv.Add(rdr.Read_str(fld_key), rdr.Read_str(fld_val));
}
}
finally {rdr.Rls();}
return rv;
}
// 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_u8(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 Exc err_parse(Exception e, String grp, String key, String val, String type) {return Exc_.new_exc(e, "db", "cfg.val is not parseable", "grp", grp, "key", key, "val", val, "type", type);}
}

View File

@@ -0,0 +1,28 @@
/*
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.metas; import gplx.*; import gplx.dbs.*;
import gplx.dbs.metas.updates.*;
public class Schema_db_mgr {
public Schema_loader_mgr Loader() {return loader;} public void Loader_(Schema_loader_mgr v) {loader = v;} private Schema_loader_mgr loader;
public Schema_update_mgr Updater() {return updater;} private final Schema_update_mgr updater = new Schema_update_mgr();
public Meta_tbl_mgr Tbl_mgr() {return tbl_mgr;} private final Meta_tbl_mgr tbl_mgr = new Meta_tbl_mgr();
public void Init(Db_conn conn) {
loader.Load(this, conn);
updater.Update(this, conn);
}
}

View File

@@ -0,0 +1,21 @@
/*
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.metas; import gplx.*; import gplx.dbs.*;
public interface Schema_loader_mgr {
void Load(Schema_db_mgr db_mgr, Db_conn conn);
}

View File

@@ -0,0 +1,51 @@
/*
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.metas; 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();
}
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_.I.Log_many("", "", "db.schema.load.bgn: conn=~{0}", conn.Conn_info().Xto_api());
Meta_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_rdr rdr = conn.Stmt_new(qry).Exec_select__rls_auto();
try {
while (rdr.Move_next()) {
String type_str = rdr.Read_str("type");
String name = rdr.Read_str("name");
String sql = rdr.Read_str("sql");
int type_int = Meta_itm_tid.Xto_int(type_str);
switch (type_int) {
case Meta_itm_tid.Tid_table:
Meta_tbl_itm tbl_itm = new Meta_tbl_itm(name, sql);
tbl_mgr.Add(tbl_itm);
break;
case Meta_itm_tid.Tid_index: break; // noop for now
default: throw Exc_.new_unhandled(type_str);
}
}
} finally {rdr.Rls();}
Gfo_usr_dlg_.I.Log_many("", "", "db.schema.load.end");
}
}

View File

@@ -0,0 +1,23 @@
/*
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.metas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
public interface Schema_update_cmd {
String Name();
boolean Exec_is_done();
void Exec(Schema_db_mgr mgr, Db_conn conn);
}

View File

@@ -0,0 +1,37 @@
/*
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.metas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
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);}
}
class Schema_update_cmd__tbl_create implements Schema_update_cmd {
private final String tbl_sql; private final Db_idx_itm[] tbl_idxs;
public Schema_update_cmd__tbl_create(String tbl_name, String tbl_sql, Db_idx_itm... tbl_idxs) {
this.tbl_name = tbl_name; this.tbl_sql = tbl_sql; this.tbl_idxs = tbl_idxs;
}
public String Name() {return "schema.tbl.create." + tbl_name;} private final String tbl_name;
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_.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;
}
}

View File

@@ -0,0 +1,32 @@
/*
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.metas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
public class Schema_update_mgr {
private List_adp cmds = List_adp_.new_();
public void Add(Schema_update_cmd cmd) {cmds.Add(cmd);}
public void Update(Schema_db_mgr schema_mgr, Db_conn conn) {
int cmds_len = cmds.Count();
for (int i = 0; i < cmds_len; ++i) {
Schema_update_cmd cmd = (Schema_update_cmd)cmds.Get_at(i);
try {cmd.Exec(schema_mgr, conn);}
catch (Exception e) {
Gfo_usr_dlg_.I.Warn_many("", "", "failed to run update cmd; name=~{0} err=~{1}", cmd.Name(), Err_.Message_gplx_brief(e));
}
}
}
}

View File

@@ -0,0 +1,55 @@
/*
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.metas.updates; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import org.junit.*; import gplx.dbs.*;
public class Schema_update_mgr_tst {
@Before public void init() {fxt.Clear();} private Schema_update_mgr_fxt fxt = new Schema_update_mgr_fxt();
@Test public void Create() {
fxt.Test_exec_y(new Schema_update_cmd__mock());
}
@Test public void Delete() {
fxt.Init_itm(Meta_itm_tid.Tid_table, Schema_update_cmd__mock.Tbl_name);
fxt.Test_exec_n(new Schema_update_cmd__mock());
}
}
class Schema_update_mgr_fxt {
private Schema_update_mgr update_mgr; private Schema_db_mgr db_mgr;
public void Clear() {
update_mgr = new Schema_update_mgr();
db_mgr = new Schema_db_mgr();
}
public void Init_itm(int tid, String name) {
db_mgr.Tbl_mgr().Add(new Meta_tbl_itm(name, "sql"));
}
public void Test_exec_y(Schema_update_cmd cmd) {Test_exec(cmd, Bool_.Y);}
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_.Noop);
Tfds.Eq(expd, cmd.Exec_is_done());
}
}
class Schema_update_cmd__mock implements Schema_update_cmd {
public String Name() {return "xowa.user.cfg_regy.create";}
public boolean Exec_is_done() {return exec_is_done;} private boolean exec_is_done;
public void Exec(Schema_db_mgr mgr, Db_conn conn) {
if (mgr.Tbl_mgr().Has(Tbl_name)) return;
exec_is_done = true;
}
public static final String Tbl_name = "tbl_mock";
}