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
2016-01-17 23:18:07 -05:00
parent 096045614c
commit 235228976e
314 changed files with 4458 additions and 2694 deletions

View File

@@ -0,0 +1,58 @@
/*
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.diffs; import gplx.*; import gplx.dbs.*;
import gplx.dbs.metas.*; import gplx.dbs.diffs.builds.*;
class Gfdb_diff_cmd {
private final Gfdb_diff_wkr__db diff_bldr_wkr = new Gfdb_diff_wkr__db();
private final Gfdb_diff_bldr diff_bldr = new Gfdb_diff_bldr();
public Gfdb_diff_cmd() {
diff_bldr.Init(diff_bldr_wkr);
}
public Gfdb_diff_job New_job(Gfdb_diff_db db, String guid, String name, String made_by, String desc) {
return new Gfdb_diff_job(db);
}
public void Bld(Gfdb_diff_job job, Gfdb_diff_tbl_mgr lhs_mgr, Gfdb_diff_tbl_mgr rhs_mgr) {
diff_bldr_wkr.Init_conn(job.Db(), 1000);
int rhs_len = rhs_mgr.Len();
for (int i = 0; i < rhs_len; ++i) {
Gfdb_diff_tbl rhs_tbl = rhs_mgr.Get_at(i);
Gfdb_diff_tbl lhs_tbl = lhs_mgr.Get_by(rhs_tbl.Name);
if (lhs_tbl == null) {
// Add_cmd_tbl_create();
// Add_cmd_tbl_insert_all());
}
else {
// Compare_flds();
// Compare_idxs();
// diff_bldr.Compare(lhs_tbl, rhs_tbl);
}
}
int lhs_len = lhs_mgr.Len();
for (int i = 0; i < lhs_len; ++i) {
Gfdb_diff_tbl lhs_tbl = lhs_mgr.Get_at(i);
if (lhs_tbl == null) {
// Add_cmd_dat_delete_all());
// Add_cmd_tbl_delete();
}
}
}
}
class Gfdb_diff_job {
public Gfdb_diff_job(Gfdb_diff_db db) {this.db = db;}
public Gfdb_diff_db Db() {return db;} private Gfdb_diff_db db;
}

View File

@@ -0,0 +1,24 @@
/*
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.diffs; import gplx.*; import gplx.dbs.*;
public class Gfdb_diff_db {
public Gfdb_diff_db(Db_conn conn) {
this.conn = conn;
}
public Db_conn Conn() {return conn;} private final Db_conn conn;
}

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.diffs; import gplx.*; import gplx.dbs.*;
public class Gfdb_diff_db_ {
public static final String
Fld__diff_site = "diff_site" // -1 for single-site merge; 0+ for multiple-site merges where 0+ is defined in a registry
, Fld__diff_time = "diff_time" // -1 for single-time merge; 0+ for multiple-time merges where 0+ is defined in a registry
, Fld__diff_db_trg = "diff_db_trg" // -1 for single-db tables; 0+ for multiple-db tables
, Fld__diff_db_src = "diff_db_src" // -1 for I,U,D; 0+ for M
, Fld__diff_type = "diff_type" // I,U,D,M
, Fld__diff_uid = "diff_uid" // 0+
;
public static final byte
Tid__insert = 0
, Tid__update = 1
, Tid__delete = 2
, Tid__move = 3
;
}

View File

@@ -0,0 +1,86 @@
/*
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.diffs; import gplx.*; import gplx.dbs.*;
import gplx.dbs.metas.*;
public class Gfdb_diff_tbl {
public Gfdb_diff_tbl(String name, Dbmeta_fld_itm[] flds, Dbmeta_fld_itm[] keys, Dbmeta_fld_itm[] vals) {
this.Name = name; this.Flds = flds; this.Keys = keys; this.Vals = vals;
}
public final String Name;
public final Dbmeta_fld_itm[] Flds;
public final Dbmeta_fld_itm[] Keys;
public final Dbmeta_fld_itm[] Vals;
public Db_rdr Make_rdr(Db_conn conn) {
Db_stmt stmt = conn.Stmt_select(Name, Dbmeta_fld_itm.To_str_ary(Flds));
return stmt.Exec_select__rls_auto();
}
public static Gfdb_diff_tbl New(Dbmeta_tbl_itm tbl) {
Dbmeta_fld_mgr flds = tbl.Flds();
Dbmeta_fld_mgr keys = Calc_keys(tbl);
Dbmeta_fld_mgr vals = Calc_vals(tbl.Flds(), keys);
return new Gfdb_diff_tbl(tbl.Name(), flds.To_ary(), keys.To_ary(), vals.To_ary());
}
public static Dbmeta_fld_mgr Calc_keys(Dbmeta_tbl_itm tbl) {
Dbmeta_fld_mgr rv = new Dbmeta_fld_mgr();
// try to find primary
Dbmeta_fld_mgr flds = tbl.Flds();
int flds_len = flds.Len();
for (int i = 0; i < flds_len; ++i) {
Dbmeta_fld_itm fld = flds.Get_at(i);
if (fld.Primary()) {
rv.Add(fld);
return rv;
}
}
// try to find shortest unique index
Dbmeta_idx_itm unique_idx = null; int unique_idx_len = Int_.Max_value;
Dbmeta_idx_mgr idxs = tbl.Idxs();
int idxs_len = idxs.Len();
for (int i = 0; i < idxs_len; ++i) {
Dbmeta_idx_itm idx = idxs.Get_at(i);
if (idx.Unique() && idx.Flds.length < unique_idx_len) { // get first shortest unique index; note that "<" is "get first" whereas "<=" is "get last"
unique_idx = idx;
unique_idx_len = idx.Flds.length;
break;
}
}
if (unique_idx != null) {
Dbmeta_idx_fld[] idx_flds = unique_idx.Flds;
int idx_flds_len = idx_flds.length;
for (int i = 0; i < idx_flds_len; ++i)
rv.Add(flds.Get_by(idx_flds[i].Name));
return rv;
}
// just add all
for (int i = 0; i < flds_len; ++i) {
Dbmeta_fld_itm fld = flds.Get_at(i);
rv.Add(fld);
}
return rv;
}
private static Dbmeta_fld_mgr Calc_vals(Dbmeta_fld_mgr flds, Dbmeta_fld_mgr keys) {
Dbmeta_fld_mgr rv = new Dbmeta_fld_mgr();
int flds_len = flds.Len();
for (int i = 0; i < flds_len; ++i) {
Dbmeta_fld_itm fld = flds.Get_at(i);
if (!keys.Has(fld.Name())) rv.Add(fld);
}
return rv;
}
}

View File

@@ -0,0 +1,35 @@
/*
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.diffs; import gplx.*; import gplx.dbs.*;
class Gfdb_diff_tbl_mgr {
private final Ordered_hash hash = Ordered_hash_.New();
public int Len() {return hash.Count();}
public Gfdb_diff_tbl Get_at(int idx) {return (Gfdb_diff_tbl)hash.Get_at(idx);}
public Gfdb_diff_tbl Get_by(String key) {return (Gfdb_diff_tbl)hash.Get_by(key);}
}
class Gfdb_diff_tbl_mgr__sqlite {
public void Fill(Gfdb_diff_tbl_mgr tbl_mgr, Db_conn conn) {
// String schema_str = ""; // conn.Get_schema();
}
public void Fill(Gfdb_diff_tbl_mgr tbl_mgr, String schema_str) {
/*
Db_conn conn = null;
conn.Meta_get_tbls(Gfdb_diff_tbl_mgr tbl_mgr, "");
*/
}
}

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.diffs; import gplx.*; import gplx.dbs.*;
import gplx.dbs.metas.*;
public class Gfdb_rdr_utl_ {
public static int Compare(Dbmeta_fld_itm[] flds, int len, Db_rdr lhs_rdr, Db_rdr rhs_rdr) {
int comp = CompareAble_.Same;
for (int i = 0; i < len; ++i) {
Dbmeta_fld_itm fld = flds[i];
String fld_name = fld.Name();
int tid = fld.Type().Tid_ansi();
switch (tid) {
case Dbmeta_fld_tid.Tid__bool: comp = Bool_.Compare (lhs_rdr.Read_bool_by_byte(fld_name), rhs_rdr.Read_bool_by_byte(fld_name)); break;
case Dbmeta_fld_tid.Tid__int: comp = Int_.Compare (lhs_rdr.Read_int(fld_name) , rhs_rdr.Read_int(fld_name)); break;
case Dbmeta_fld_tid.Tid__long: comp = Long_.Compare (lhs_rdr.Read_long(fld_name) , rhs_rdr.Read_long(fld_name)); break;
case Dbmeta_fld_tid.Tid__float: comp = Float_.Compare (lhs_rdr.Read_float(fld_name) , rhs_rdr.Read_float(fld_name)); break;
case Dbmeta_fld_tid.Tid__double: comp = Double_.Compare (lhs_rdr.Read_double(fld_name) , rhs_rdr.Read_double(fld_name)); break;
case Dbmeta_fld_tid.Tid__str: comp = String_.Compare (lhs_rdr.Read_str(fld_name) , rhs_rdr.Read_str(fld_name)); break;
case Dbmeta_fld_tid.Tid__bry: comp = Bry_.Compare (lhs_rdr.Read_bry(fld_name) , rhs_rdr.Read_bry(fld_name)); break;
default: throw Err_.new_unhandled(tid);
}
if (comp != CompareAble_.Same) return comp;
}
return CompareAble_.Same;
}
public static void Stmt_args(Db_stmt stmt, Dbmeta_fld_itm[] flds, int len, Db_rdr rdr) {
for (int i = 0; i < len; ++i) {
Dbmeta_fld_itm fld = flds[i];
String fld_name = fld.Name();
int tid = fld.Type().Tid_ansi();
switch (tid) {
case Dbmeta_fld_tid.Tid__bool: stmt.Val_bool_as_byte (fld_name, rdr.Read_bool_by_byte(fld_name)); break;
case Dbmeta_fld_tid.Tid__byte: stmt.Val_byte (fld_name, rdr.Read_byte(fld_name)); break;
case Dbmeta_fld_tid.Tid__int: stmt.Val_int (fld_name, rdr.Read_int(fld_name)); break;
case Dbmeta_fld_tid.Tid__long: stmt.Val_long (fld_name, rdr.Read_long(fld_name)); break;
case Dbmeta_fld_tid.Tid__float: stmt.Val_float (fld_name, rdr.Read_float(fld_name)); break;
case Dbmeta_fld_tid.Tid__double: stmt.Val_double (fld_name, rdr.Read_double(fld_name)); break;
case Dbmeta_fld_tid.Tid__str: stmt.Val_str (fld_name, rdr.Read_str(fld_name)); break;
case Dbmeta_fld_tid.Tid__bry: stmt.Val_bry (fld_name, rdr.Read_bry(fld_name)); break;
default: throw Err_.new_unhandled(tid);
}
}
}
}

View File

@@ -0,0 +1,39 @@
/*
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import gplx.dbs.*;
public class Gfdb_diff_bldr {
private Gfdb_diff_rdr_comparer rdr_comparer = new Gfdb_diff_rdr_comparer();
private Gfdb_diff_wkr diff_wkr;
public void Init(Gfdb_diff_wkr diff_wkr) {this.diff_wkr = diff_wkr;}
public void Compare(Gfdb_diff_tbl tbl, Db_conn old_conn, Db_conn new_conn) {
Db_rdr old_rdr = tbl.Make_rdr(old_conn), new_rdr = tbl.Make_rdr(new_conn);
rdr_comparer.Init_rdrs(tbl, old_rdr, new_rdr);
diff_wkr.Init_rdrs(tbl, old_rdr, new_rdr);
boolean loop = true;
while (loop) {
int rslt = rdr_comparer.Compare();
switch (rslt) {
case Gfdb_diff_rdr_comparer.Rslt__same: diff_wkr.Handle_same(); break;
case Gfdb_diff_rdr_comparer.Rslt__old_missing: diff_wkr.Handle_old_missing(); break;
case Gfdb_diff_rdr_comparer.Rslt__new_missing: diff_wkr.Handle_new_missing(); break;
case Gfdb_diff_rdr_comparer.Rslt__done: loop = false; break;
}
}
}
}

View File

@@ -0,0 +1,105 @@
/*
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import org.junit.*;
import gplx.dbs.*; import gplx.dbs.metas.*; import gplx.dbs.engines.mems.*;
public class Gfdb_diff_bldr_tst {
private final Gfdb_diff_bldr_fxt fxt = new Gfdb_diff_bldr_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Same() {
fxt.Init__tbl__old(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Init__tbl__cur(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Test__bld();
}
@Test public void Update() {
fxt.Init__tbl__old(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Init__tbl__cur(Object_.Ary(1, "A1") , Object_.Ary(2, "B1"));
fxt.Test__bld("U|1|A1", "U|2|B1");
}
@Test public void Insert() {
fxt.Init__tbl__old(Object_.Ary(1, "A"));
fxt.Init__tbl__cur(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Test__bld("I|2|B");
}
@Test public void Delete() {
fxt.Init__tbl__old(Object_.Ary(1, "A") , Object_.Ary(2, "B"));
fxt.Init__tbl__cur(Object_.Ary(1, "A"));
fxt.Test__bld("D|2");
}
@Test public void Basic() {
fxt.Init__tbl__old
( Object_.Ary(1, "A")
, Object_.Ary(2, "B")
, Object_.Ary(3, "C")
);
fxt.Init__tbl__cur
( Object_.Ary(1, "A")
, Object_.Ary(2, "B1")
, Object_.Ary(4, "D")
);
fxt.Test__bld("U|2|B1", "D|3", "I|4|D");
}
}
class Gfdb_diff_bldr_fxt {
private final Gfdb_diff_bldr bldr = new Gfdb_diff_bldr();
private final Db_conn old_conn, new_conn;
private final Gfdb_diff_tbl tbl;
private final Gfdb_diff_wkr__test wkr = new Gfdb_diff_wkr__test();
private final Dbmeta_fld_itm[] flds_ary;
private final String tbl_name = "tbl";
public Gfdb_diff_bldr_fxt() {
old_conn = Db_conn_utl.Conn__new("old_db");
new_conn = Db_conn_utl.Conn__new("new_db");
this.flds_ary = new Dbmeta_fld_itm[] {Dbmeta_fld_itm.new_int("id").Primary_y_(), Dbmeta_fld_itm.new_str("val", 255)};
tbl = Gfdb_diff_tbl.New(Dbmeta_tbl_itm.New(tbl_name, flds_ary));
bldr.Init(wkr);
}
public void Clear() {
Db_conn_utl.Tbl__delete(old_conn, "tbl");
Db_conn_utl.Tbl__delete(new_conn, "tbl");
}
public void Init__tbl__old(Object[]... rows) {Db_conn_utl.Tbl__new(old_conn, "tbl", flds_ary, rows);}
public void Init__tbl__cur(Object[]... rows) {Db_conn_utl.Tbl__new(new_conn, "tbl", flds_ary, rows);}
public void Test__bld(String... expd) {
bldr.Compare(tbl, old_conn, new_conn);
Tfds.Eq_ary_str(expd, wkr.To_str_ary());
}
}
class Gfdb_diff_wkr__test implements Gfdb_diff_wkr {
private final List_adp list = List_adp_.new_();
private final Bry_bfr bfr = Bry_bfr.new_();
private Db_rdr old_rdr, new_rdr;
public void Init_rdrs(Gfdb_diff_tbl tbl, Db_rdr old_rdr, Db_rdr new_rdr) {
this.old_rdr = old_rdr; this.new_rdr = new_rdr;
}
public void Term_tbls() {}
public void Handle_same() {
String old_val = old_rdr.Read_str("val");
String new_val = new_rdr.Read_str("val");
if (!String_.Eq(old_val, new_val))
list.Add(bfr.Add_str_a7("U").Add_byte_pipe().Add_obj(old_rdr.Read_obj("id")).Add_byte_pipe().Add_str_a7(new_val).To_str_and_clear());
}
public void Handle_old_missing() {
String new_val = new_rdr.Read_str("val");
list.Add(bfr.Add_str_a7("I").Add_byte_pipe().Add_obj(new_rdr.Read_obj("id")).Add_byte_pipe().Add_str_a7(new_val).To_str_and_clear());
}
public void Handle_new_missing() {
list.Add(bfr.Add_str_a7("D").Add_byte_pipe().Add_obj(old_rdr.Read_obj("id")).To_str_and_clear());
}
public String[] To_str_ary() {return list.To_str_ary_and_clear();}
}

View File

@@ -0,0 +1,66 @@
/*
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
class Gfdb_diff_rdr_comparer {
private Db_rdr old_rdr, new_rdr;
private boolean old_rdr_move, new_rdr_move;
private boolean old_rdr_done, new_rdr_done;
private Dbmeta_fld_itm[] key_flds; private int key_flds_len;
public void Init_rdrs(Gfdb_diff_tbl tbl, Db_rdr old_rdr, Db_rdr new_rdr) {
this.old_rdr = old_rdr; this.new_rdr = new_rdr;
this.old_rdr_move = new_rdr_move = Bool_.Y;
this.old_rdr_done = new_rdr_done = Bool_.N;
this.key_flds = tbl.Keys; key_flds_len = key_flds.length;
}
public int Compare() {
if (old_rdr_move) {
old_rdr_move = old_rdr.Move_next();
if (!old_rdr_move) old_rdr_done = true;
}
if (new_rdr_move) {
new_rdr_move = new_rdr.Move_next();
if (!new_rdr_move) new_rdr_done = true;
}
if (old_rdr_done && new_rdr_done) return Gfdb_diff_rdr_comparer.Rslt__done;
else if (old_rdr_done) {new_rdr_move = true; return Gfdb_diff_rdr_comparer.Rslt__old_missing;}
else if (new_rdr_done) {old_rdr_move = true; return Gfdb_diff_rdr_comparer.Rslt__new_missing;}
else {
int comp = Gfdb_rdr_utl_.Compare(key_flds, key_flds_len, old_rdr, new_rdr);
switch (comp) {
case CompareAble_.Same: // old == cur; move both
old_rdr_move = new_rdr_move = true;
return Gfdb_diff_rdr_comparer.Rslt__same;
case CompareAble_.Less: // old < cur; EX: old == 2; cur == 3
old_rdr_move = true;
new_rdr_move = false;
return Gfdb_diff_rdr_comparer.Rslt__new_missing;
case CompareAble_.More: // old > cur; EX: old == 4; cur == 3
old_rdr_move = false;
new_rdr_move = true;
return Gfdb_diff_rdr_comparer.Rslt__old_missing;
default: throw Err_.new_unhandled(comp);
}
}
}
public static final int
Rslt__same = 0
, Rslt__old_missing = 1
, Rslt__new_missing = 2
, Rslt__done = 3
;
}

View File

@@ -0,0 +1,25 @@
/*
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
public interface Gfdb_diff_wkr {
void Init_rdrs(Gfdb_diff_tbl tbl, Db_rdr old_rdr, Db_rdr new_rdr);
void Term_tbls();
void Handle_same();
void Handle_old_missing();
void Handle_new_missing();
}

View 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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import gplx.dbs.metas.*;
public class Gfdb_diff_wkr__db implements Gfdb_diff_wkr {
private Dbmeta_fld_itm[] val_flds; private int val_flds_len;
private Gfdb_diff_tbl tbl;
private Db_conn diff_conn; private Db_stmt stmt;
private Db_rdr old_rdr, new_rdr;
private int uid__upsert, uid__delete; private int prog_interval, prog_count;
public void Init_conn(Gfdb_diff_db diff_db, int prog_interval) {this.diff_conn = diff_db.Conn(); this.prog_interval = prog_interval;}
public void Init_rdrs(Gfdb_diff_tbl tbl, Db_rdr old_rdr, Db_rdr new_rdr) {
this.tbl = tbl; this.old_rdr = old_rdr; this.new_rdr = new_rdr;
this.val_flds = tbl.Vals; val_flds_len = val_flds.length;
this.uid__upsert = 0; uid__delete = 0; this.prog_count = 0;
String tbl_name = tbl.Name;
Dbmeta_fld_itm[] diff_flds = Gfdb_diff_wkr__db_.New_diff_flds(tbl.Flds);
if (!diff_conn.Meta_tbl_exists(tbl_name)) diff_conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, diff_flds));
this.stmt = diff_conn.Stmt_insert(tbl_name, Gfdb_diff_wkr__db_.To_str_ary(diff_flds));
diff_conn.Txn_bgn("diff_db");
}
public void Term_tbls() {
diff_conn.Txn_end();
}
public void Handle_same() {
if (Gfdb_rdr_utl_.Compare(val_flds, val_flds_len, old_rdr, new_rdr) != CompareAble_.Same)
Insert(Gfdb_diff_db_.Tid__update, uid__upsert++, tbl.Flds, new_rdr);
}
public void Handle_old_missing() {Insert(Gfdb_diff_db_.Tid__insert, uid__upsert++, tbl.Flds, new_rdr);}
public void Handle_new_missing() {Insert(Gfdb_diff_db_.Tid__delete, uid__delete++, tbl.Keys, old_rdr);}
private void Insert(byte diff_type, int uid, Dbmeta_fld_itm[] flds, Db_rdr rdr) {
stmt.Val_int (Gfdb_diff_db_.Fld__diff_site , -1)
.Val_int (Gfdb_diff_db_.Fld__diff_time , -1)
.Val_int (Gfdb_diff_db_.Fld__diff_db_trg , -1)
.Val_int (Gfdb_diff_db_.Fld__diff_db_src , -1)
.Val_byte (Gfdb_diff_db_.Fld__diff_type , diff_type)
.Val_int (Gfdb_diff_db_.Fld__diff_type , uid)
;
Gfdb_rdr_utl_.Stmt_args(stmt, flds, flds.length, rdr);
stmt.Exec_insert();
if ((++prog_count % prog_interval) == 0) diff_conn.Txn_sav();
}
}
class Gfdb_diff_wkr__db_ {
public static Dbmeta_fld_itm[] New_diff_flds(Dbmeta_fld_itm[] all_flds) {
int len = all_flds.length;
int system_flds = 6;
Dbmeta_fld_itm[] rv = new Dbmeta_fld_itm[len + system_flds];
rv[0] = Dbmeta_fld_itm.new_int (Gfdb_diff_db_.Fld__diff_site);
rv[1] = Dbmeta_fld_itm.new_int (Gfdb_diff_db_.Fld__diff_time);
rv[2] = Dbmeta_fld_itm.new_int (Gfdb_diff_db_.Fld__diff_db_trg);
rv[3] = Dbmeta_fld_itm.new_int (Gfdb_diff_db_.Fld__diff_db_src);
rv[4] = Dbmeta_fld_itm.new_byte(Gfdb_diff_db_.Fld__diff_type);
rv[5] = Dbmeta_fld_itm.new_int (Gfdb_diff_db_.Fld__diff_uid);
for (int i = 0; i < len; ++i) {
Dbmeta_fld_itm orig_fld = all_flds[i];
Dbmeta_fld_itm diff_fld = new Dbmeta_fld_itm(orig_fld.Name(), orig_fld.Type()).Nullable_y_(); // keep same name and type, but make nullable
all_flds[i + system_flds] = diff_fld;
}
return rv;
}
public static String[] To_str_ary(Dbmeta_fld_itm[] ary) {
int len = ary.length;
String[] rv = new String[len];
for (int i = 0; i < len; ++i)
rv[i] = ary[i].Name();
return rv;
}
}

View File

@@ -0,0 +1,305 @@
/*
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.diffs.cmds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import gplx.core.srls.*; import gplx.core.brys.fmtrs.*;
import gplx.dbs.metas.*;
interface Gfdb_diff_cmd {
void Merge_undo();
void Merge_exec();
}
class Gfdb_diff_ctx implements Gfo_srl_ctx {
public Gfo_srl_mgr_wtr Wtr_bgn(String key) {return null;}
public Gfo_srl_mgr_rdr Rdr_bgn(String key) {return null;}
public Dbmeta_dat_mgr Rdr_subs(String key) {return null;}
public Dbmeta_tbl_itm Tbls__get(String key) {return null;}
}
class Gfo_srl_mgr_rdr__defn {
public String Tbl = null;
public String[] Select_crt_cols = null;
public String[] Delete_crt_cols = null;
}
class Gfo_srl_mgr_rdr__db {
public Object Get_subs (Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_itm proto, String defn_key, Dbmeta_dat_mgr crt_mgr) {
List_adp list = List_adp_.new_();
Gfo_srl_mgr_rdr__defn defn = new Gfo_srl_mgr_rdr__defn(); // Get(key)
Db_conn conn = Db_conn_.Noop;
Db_stmt select = conn.Stmt_select(defn.Tbl, Dbmeta_fld_itm.Str_ary_empty, defn.Select_crt_cols);
int crt_len = crt_mgr.Len();
for (int i = 0; i < crt_len; ++i) {
Dbmeta_dat_itm crt = crt_mgr.Get_at(i);
switch (crt.Tid) {
case Dbmeta_fld_tid.Tid__int: select.Crt_int(crt.Key, Int_.cast(crt.Val)); break;
}
}
Db_rdr rdr = select.Exec_select__rls_manual();
while (rdr.Move_next()) {
Gfo_srl_itm sub = proto.Make_new(ctx);
list.Add(sub);
sub.Load(ctx, owner, null);
}
rdr.Rls();
return list.To_ary_and_clear(proto.getClass());
}
public void Set_subs (Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_itm proto, Gfo_srl_itm[] subs_ary, String defn_key, Dbmeta_dat_mgr crt_mgr) {
Gfo_srl_mgr_rdr__defn defn = new Gfo_srl_mgr_rdr__defn(); // Get(key)
Db_conn conn = Db_conn_.Noop;
Db_stmt delete = conn.Stmt_delete(defn.Tbl, defn.Delete_crt_cols);
int crt_len = crt_mgr.Len();
for (int i = 0; i < crt_len; ++i) {
Dbmeta_dat_itm crt = crt_mgr.Get_at(i);
switch (crt.Tid) {
case Dbmeta_fld_tid.Tid__int: delete.Crt_int(crt.Key, Int_.cast(crt.Val)); break;
}
}
delete.Exec_delete();
int subs_len = subs_ary.length;
for (int i = 0; i < subs_len; ++i) {
Gfo_srl_itm itm = subs_ary[i];
itm.Save(ctx, owner, null);
}
}
}
class Gfdb_diff_cmd__idx__delete {
public Gfdb_diff_cmd__idx__delete(Dbmeta_idx_itm old) {this.Old = old;}
public final Dbmeta_idx_itm Old;
}
class Gfdb_diff_cmd__idx__modify {
public Gfdb_diff_cmd__idx__modify(Dbmeta_idx_itm old, Dbmeta_idx_itm cur) {this.Old = old; this.Cur = cur;}
public final Dbmeta_idx_itm Old;
public final Dbmeta_idx_itm Cur;
}
class Gfdb_diff_txn {
public int Id = 0;
}
class Gfdb_diff_cmd__fld__create {
public Gfdb_diff_cmd__fld__create(Dbmeta_fld_itm cur) {this.cur = cur;}
private Dbmeta_fld_itm cur;
public void Save(Gfo_srl_ctx ctx, Gfo_srl_itm owner) {
Gfo_srl_mgr_wtr wtr = ctx.Wtr_bgn("cmd.fld");
wtr.Set_int ("txn_id" , ((Gfdb_diff_txn)owner).Id);
wtr.Set_str ("name" , cur.Name());
wtr.Set_int ("type_tid" , cur.Type().Tid_ansi());
wtr.Set_int ("type_len_1" , cur.Type().Len_1());
wtr.Set_int ("type_len_2" , cur.Type().Len_2());
wtr.Set_bool ("primary" , cur.Primary());
wtr.Set_int ("nullable" , cur.Nullable_tid());
wtr.Set_str ("dflt" , Object_.Xto_str_or(cur.Default(), null));
wtr.Itm_end();
}
public void Load(Gfo_srl_ctx ctx, Gfo_srl_itm owner) {
Gfo_srl_mgr_rdr rdr = ctx.Rdr_bgn("cmd.fld");
String name = rdr.Get_str("name");
int type_tid = rdr.Get_int("type_tid");
int type_len_1 = rdr.Get_int("type_len_1");
// int type_len_2 = rdr.Get_int("type_len_2");
boolean primary = rdr.Get_bool("primary");
int nullable_tid = rdr.Get_int("nullable");
String dflt_str = rdr.Get_str("dflt");
cur = new Dbmeta_fld_itm(name, Dbmeta_fld_tid.New(type_tid, type_len_1));
cur.Nullable_tid_(nullable_tid);
if (primary) cur.Primary_y_();
if (dflt_str != null) cur.Default_(dflt_str);
rdr.Itm_end();
// Gfdb_diff_cmd__idx__create idx = ((Gfdb_diff_cmd__idx__create)owner);
// Dbmeta_tbl_itm tbl = ((Gfdb_diff_ctx)ctx).Tbls__get(idx.cur.Tbl());
// Dbmeta_fld_tid tid = tbl.Flds().Get_by(name).Type();
}
}
class Gfdb_diff_cmd__fld__delete {
public Gfdb_diff_cmd__fld__delete(Dbmeta_fld_itm old) {this.Old = old;}
public final Dbmeta_fld_itm Old;
}
class Gfdb_diff_cmd__fld__modify {
public Gfdb_diff_cmd__fld__modify(Dbmeta_fld_itm old, Dbmeta_fld_itm cur) {this.Old = old; this.Cur = cur;}
public final Dbmeta_fld_itm Old;
public final Dbmeta_fld_itm Cur;
}
class Gfdb_diff_cmd__tbl__create {
public Gfdb_diff_cmd__tbl__create(Dbmeta_tbl_itm cur) {this.Cur = cur;}
public final Dbmeta_tbl_itm Cur;
}
class Gfdb_diff_cmd__tbl__delete {
public Gfdb_diff_cmd__tbl__delete(Dbmeta_tbl_itm old) {this.Old = old;}
public final Dbmeta_tbl_itm Old;
}
class Gfdb_diff_cmd_bldr {
public void Chk_tbls(List_adp rv, Dbmeta_tbl_mgr old_tbls, Dbmeta_tbl_mgr cur_tbls) {
int cur_tbls_len = cur_tbls.Len();
for (int i = 0; i < cur_tbls_len; ++i) {
Dbmeta_tbl_itm cur_tbl = cur_tbls.Get_at(i);
Dbmeta_tbl_itm old_tbl = old_tbls.Get_by(cur_tbl.Name());
if (old_tbl == null)
rv.Add(new Gfdb_diff_cmd__tbl__create(cur_tbl));
else {
Chk_idxs(rv, old_tbl, cur_tbl);
Chk_flds(rv, old_tbl, cur_tbl);
// Chk_data?
}
}
int old_tbls_len = old_tbls.Len();
for (int i = 0; i < old_tbls_len; ++i) {
Dbmeta_tbl_itm old_tbl = old_tbls.Get_at(i);
Dbmeta_tbl_itm cur_tbl = cur_tbls.Get_by(old_tbl.Name());
if (cur_tbl == null)
rv.Add(new Gfdb_diff_cmd__tbl__delete(old_tbl));
}
}
public void Chk_idxs(List_adp rv, Dbmeta_tbl_itm old_tbl, Dbmeta_tbl_itm cur_tbl) {
Dbmeta_idx_mgr old_idxs = old_tbl.Idxs(), cur_idxs = cur_tbl.Idxs();
int cur_idxs_len = cur_idxs.Len();
for (int i = 0; i < cur_idxs_len; ++i) {
Dbmeta_idx_itm cur_idx = cur_idxs.Get_at(i);
Dbmeta_idx_itm old_idx = old_idxs.Get_by(cur_idx.Name());
if (old_idx == null)
rv.Add(new Gfdb_diff_cmd__idx__create(cur_idx));
else
if (!cur_idx.Eq(old_idx))
rv.Add(new Gfdb_diff_cmd__idx__modify(old_idx, cur_idx));
}
int old_idxs_len = old_idxs.Len();
for (int i = 0; i < old_idxs_len; ++i) {
Dbmeta_idx_itm old_idx = old_idxs.Get_at(i);
Dbmeta_idx_itm cur_idx = cur_idxs.Get_by(old_idx.Name());
if (cur_idx == null)
rv.Add(new Gfdb_diff_cmd__idx__delete(old_idx));
}
}
public void Chk_flds(List_adp rv, Dbmeta_tbl_itm old_tbl, Dbmeta_tbl_itm cur_tbl) {
Dbmeta_fld_mgr old_flds = old_tbl.Flds(), cur_flds = cur_tbl.Flds();
int cur_flds_len = cur_flds.Len();
for (int i = 0; i < cur_flds_len; ++i) {
Dbmeta_fld_itm cur_fld = cur_flds.Get_at(i);
Dbmeta_fld_itm old_fld = old_flds.Get_by(cur_fld.Name());
if (old_fld == null)
rv.Add(new Gfdb_diff_cmd__fld__create(cur_fld));
else
if (!cur_fld.Eq(old_fld))
rv.Add(new Gfdb_diff_cmd__fld__modify(old_fld, cur_fld));
}
int old_flds_len = old_flds.Len();
for (int i = 0; i < old_flds_len; ++i) {
Dbmeta_fld_itm old_fld = old_flds.Get_at(i);
Dbmeta_fld_itm cur_fld = cur_flds.Get_by(old_fld.Name());
if (cur_fld == null)
rv.Add(new Gfdb_diff_cmd__fld__delete(old_fld));
}
}
}
class Gfdb_diff_cmd__insert {
// else if I
// // txn_bgn
// // audit
// INSERT INTO db_temp.page (diff_type, diff, *)
// SELECT 'D', d.page_id, *
// FROM db_diff.page d
// JOIN db_main.page m ON d.page_id = m.page_id
// WHERE d.diff_type = 0
// AND d.diff_idx BETWEEN lo and hi
//
// // update
// INSERT INTO db_main.page
// SELECT d.page_title
// FROM db_diff.page d
// JOIN db_main.page m
// // txn_end
private Db_conn conn;
private String exec_sql;
public void Init(Db_conn main_conn, Db_conn diff_conn, Db_conn temp_conn, Gfdb_diff_tbl tbl) {
this.conn = temp_conn;
this.exec_sql = "";
// this.exec_sql = String_.Format(String_.Concat_lines_nl_skip_last
// ( "INSERT INTO db_curr.{tbl}"
// ), Gfdb_diff_cmd_ctx.Alias__curr);
}
public void Merge_exec() {
conn.Exec_sql(exec_sql);
}
}
class Gfdb_diff_cmd_sql_bldr {
private final Bry_fmtr fmtr = Bry_fmtr.new_();
private final Bry_bfr tmp_bfr = Bry_bfr.new_();
public void Bld_insert(Bry_bfr bfr, String tbl_name, String[] keys, String[] vals, int rng_bgn, int rng_end) {
fmtr.Fmt_(Insert__fmt).Keys_(Insert__keys);
fmtr.Bld_bfr_many(bfr, tbl_name, Bld_flds(tmp_bfr, ", ", "d.", keys, vals), Bld_join(keys), rng_bgn, rng_end);
}
public void Bld_update(Bry_bfr bfr, String tbl_name, String[] keys, String[] vals, int rng_bgn, int rng_end) {
fmtr.Fmt_(Update__fmt).Keys_(Update__keys);
fmtr.Bld_bfr_many(bfr, tbl_name, Bld_flds(tmp_bfr, ", ", "d.", keys, vals), Bld_join(keys), rng_bgn, rng_end);
}
public void Bld_delete(Bry_bfr bfr, String tbl_name, String[] keys, int rng_bgn, int rng_end) {
fmtr.Fmt_(Delete__fmt).Keys_(Delete__keys);
fmtr.Bld_bfr_many(bfr, tbl_name, Bld_flds(tmp_bfr, " || '|' || ", "", keys, String_.Ary_empty), Bld_flds(tmp_bfr, " || '|' || ", "k.", keys, String_.Ary_empty), Bld_join(keys), rng_bgn, rng_end);
}
private static String Bld_flds(Bry_bfr tmp_bfr, String dlm, String alias, String[] keys, String[] vals) {
int keys_len = keys.length;
for (int i = 0; i < keys_len; ++i) {
String key = keys[i];
if (i != 0) tmp_bfr.Add_str_a7(dlm);
tmp_bfr.Add_str_a7(alias).Add_str_a7(key);
}
int flds_len = vals.length;
for (int i = 0; i < flds_len; ++i) {
String val = vals[i];
tmp_bfr.Add_str_a7(dlm);
tmp_bfr.Add_str_a7(alias).Add_str_a7(val);
}
return tmp_bfr.To_str_and_clear();
}
private String Bld_join(String[] keys) {
int len = keys.length;
for (int i = 0; i < len; ++i) {
String key = keys[i];
tmp_bfr.Add_str_a7(i == 0 ? " ON " : " AND ");
tmp_bfr.Add_str_a7("k.").Add_str_a7(key).Add_str_a7(" = ");
tmp_bfr.Add_str_a7("d.").Add_str_a7(key);
}
return tmp_bfr.To_str_and_clear();
}
private static final String[] Insert__keys = String_.Ary("tbl", "flds", "join", "rng_bgn", "rng_end");
private static final String Insert__fmt = String_.Concat_lines_nl_skip_last
( "INSERT INTO db_curr.~{tbl}"
, "SELECT ~{flds}"
, "FROM db_temp.~{tbl}_pkey k"
, " JOIN db_diff.~{tbl} d~{join}"
, "WHERE k.diff_type = 1"
, "AND k.diff_uid BETWEEN ~{rng_bgn} AND ~{rng_end};"
);
private static final String[] Update__keys = String_.Ary("tbl", "flds", "join", "rng_bgn", "rng_end");
private static final String Update__fmt = String_.Concat_lines_nl_skip_last
( "REPLACE INTO db_curr.~{tbl}"
, "SELECT ~{flds}"
, "FROM db_temp.~{tbl}_pkey k"
, " JOIN db_diff.~{tbl} d~{join}"
, "WHERE k.diff_type = 2"
, "AND k.diff_uid BETWEEN ~{rng_bgn} AND ~{rng_end};"
);
private static final String[] Delete__keys = String_.Ary("tbl", "pkey_where", "pkey_select", "join", "rng_bgn", "rng_end");
private static final String Delete__fmt = String_.Concat_lines_nl_skip_last
( "DELETE db_curr.~{tbl}"
, "WHERE ~{pkey_where} IN"
, "( SELECT ~{pkey_select}"
, " FROM db_temp.~{tbl}_pkey k"
, " JOIN db_diff.~{tbl} d~{join}"
, " WHERE k.diff_type = 0"
, " AND k.diff_uid BETWEEN ~{rng_bgn} AND ~{rng_end}"
, ");"
);
}

View File

@@ -0,0 +1,119 @@
/*
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.diffs.cmds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import gplx.core.srls.*; import gplx.dbs.metas.*;
class Gfdb_diff_cmd__idx__create implements Gfo_srl_itm {
public Gfdb_diff_cmd__idx__create(Dbmeta_idx_itm cur) {this.cur = cur;}
public Dbmeta_idx_itm cur;
private Gfdb_diff_cmd__idx__fld[] flds = new Gfdb_diff_cmd__idx__fld[0];
public Gfo_srl_itm Make_new(Gfo_srl_ctx ctx) {return new Gfdb_diff_cmd__idx__create();} Gfdb_diff_cmd__idx__create() {}
private int idx_uid;
// *_sdif_ddl_idx : txn_uid,idx_uid,idx_tbl,idx_name,idx_unique
public void Save(Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_mgr_wtr wtr) {
wtr.Itm_bgn("idx");
wtr.Set_int ("idx_uid" , idx_uid);
wtr.Set_str ("idx_tbl" , cur.Tbl());
wtr.Set_str ("idx_name" , cur.Name());
wtr.Set_bool ("idx_unique" , cur.Unique());
wtr.Set_subs (ctx, this, Gfdb_diff_cmd__idx__fld.Instance, flds, ctx.Rdr_subs("idx_fld").Add_int("idx_uid", idx_uid));
wtr.Itm_end();
}
public void Load(Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_mgr_rdr rdr) {
rdr.Itm_bgn("idx");
this.idx_uid = rdr.Get_int ("idx_uid");
String tbl = rdr.Get_str ("idx_tbl");
String name = rdr.Get_str ("idx_name");
boolean unique = rdr.Get_bool ("idx_unique");
this.flds = (Gfdb_diff_cmd__idx__fld[])rdr.Get_subs(ctx, this, Gfdb_diff_cmd__idx__fld.Instance, ctx.Rdr_subs("idx_fld").Add_int("idx_uid", idx_uid));
cur = new Dbmeta_idx_itm(unique, tbl, name, Dbmeta_idx_fld.Ary_empty);
rdr.Itm_end();
}
public void Exec(Db_conn conn) {
conn.Ddl_create_idx(cur);
}
}
class Gfdb_diff_cmd__idx__fld implements Gfo_srl_itm {
public Gfdb_diff_cmd__idx__fld(Dbmeta_idx_fld cur) {this.cur = cur;}
private Dbmeta_idx_fld cur;
public Gfo_srl_itm Make_new(Gfo_srl_ctx ctx) {return new Gfdb_diff_cmd__idx__fld();} Gfdb_diff_cmd__idx__fld() {}
// *_sdif_ddl_idx_fld : idx_uid,fld_order,fld_name,fld_asc
public void Save(Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_mgr_wtr wtr) {
wtr.Itm_bgn("idx_fld");
wtr.Set_int ("fld_order" , cur.Order);
wtr.Set_str ("fld_name" , cur.Name);
wtr.Set_int ("fld_asc" , cur.Sort_tid);
wtr.Itm_end();
}
public void Load(Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_mgr_rdr rdr) {
rdr.Itm_bgn("idx_fld");
int order = rdr.Get_int ("fld_order");
String name = rdr.Get_str ("fld_name");
int sort_tid = rdr.Get_int ("fld_sort");
cur = new Dbmeta_idx_fld(order, name, sort_tid);
rdr.Itm_end();
}
public static final Gfdb_diff_cmd__idx__fld Instance = new Gfdb_diff_cmd__idx__fld();
}
// class Gfdb_diff_cmd__tbl__fld : Gfo_srl_itm {
// public Gfdb_diff_cmd__tbl__fld(Dbmeta_fld_itm cur) {this.cur = cur;}
// private Dbmeta_fld_itm cur;
// public String Name;
// public int Type_tid;
// public int Len1;
// public int Len2;
// public int Nullable;
// public boolean Primary;
// public boolean Autonum;
//
// public Gfo_srl_itm Make_new(Gfo_srl_ctx ctx) {return new Gfdb_diff_cmd__tbl__fld();} Gfdb_diff_cmd__tbl__fld() {}
// // *_sdif_ddl_tbl_fld : tbl_uid,fld_idx,fld_name,fld_type,fld_len1, fld_len2, fld_nullable, fld_primary, fld_autonumber
// public void Save(Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_mgr_wtr wtr) {
// wtr.Itm_bgn("tbl_fld");
// wtr.Set_str ("fld_name" , cur.Name());
// wtr.Set_int ("fld_type" , cur.Type().Tid_ansi());
// wtr.Set_int ("fld_len1" , cur.Type().Len_1());
// wtr.Set_int ("fld_len2" , cur.Type().Len_2());
// wtr.Set_int ("fld_nullable" , cur.Nullable_tid());
// wtr.Set_bool ("fld_primary" , cur.Primary());
// wtr.Set_bool ("fld_autonum" , cur.Autonum());
// wtr.Set_str ("fld_dflt" , Object_.Xto_str_or(cur.Default(), null));
//// wtr.Set_int ("fld_asc" , cur.Sort_tid);
// wtr.Itm_end();
// }
// public void Load(Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_mgr_rdr rdr) {
// rdr.Itm_bgn("tbl_fld");
// String name = rdr.Get_str("name");
// int type_tid = rdr.Get_int("type_tid");
// int type_len_1 = rdr.Get_int("type_len_1");
// boolean primary = rdr.Get_bool("primary");
// int nullable_tid = rdr.Get_int("nullable");
// String dflt_str = rdr.Get_str("dflt");
// cur = new Dbmeta_fld_itm(name, Dbmeta_fld_tid.New(type_tid, type_len_1));
// cur.Nullable_tid_(nullable_tid);
// if (primary) cur.Primary_y_();
// if (dflt_str != null) cur.Default_(dflt_str);
// rdr.Itm_end();
//// Gfdb_diff_cmd__idx__create idx = ((Gfdb_diff_cmd__idx__create)owner);
//// Dbmeta_tbl_itm tbl = ((Gfdb_diff_ctx)ctx).Tbls__get(idx.cur.Tbl());
//// Dbmeta_fld_tid tid = tbl.Flds().Get_by(name).Type();
// }
//
// public static final Gfdb_diff_cmd__tbl__fld Instance = new Gfdb_diff_cmd__tbl__fld();
// }

View File

@@ -0,0 +1,71 @@
/*
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.diffs.cmds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
import org.junit.*;
import gplx.dbs.*; import gplx.dbs.engines.mems.*;
public class Gfdb_diff_cmd_sql_bldr_tst {
private final Gfdb_diff_cmd_sql_bldr_fxt fxt = new Gfdb_diff_cmd_sql_bldr_fxt();
@Test public void Insert() {
fxt.Test__insert("tbl1", String_.Ary("key1", "key2"), String_.Ary("fld1", "fld2"), 0, 99, String_.Concat_lines_nl_skip_last
( "INSERT INTO db_curr.tbl1"
, "SELECT d.key1, d.key2, d.fld1, d.fld2"
, "FROM db_temp.tbl1_pkey k"
, " JOIN db_diff.tbl1 d ON k.key1 = d.key1 AND k.key2 = d.key2"
, "WHERE k.diff_type = 1"
, "AND k.diff_uid BETWEEN 0 AND 99;"
));
}
@Test public void Update() {
fxt.Test__update("tbl1", String_.Ary("key1", "key2"), String_.Ary("fld1", "fld2"), 0, 99, String_.Concat_lines_nl_skip_last
( "REPLACE INTO db_curr.tbl1"
, "SELECT d.key1, d.key2, d.fld1, d.fld2"
, "FROM db_temp.tbl1_pkey k"
, " JOIN db_diff.tbl1 d ON k.key1 = d.key1 AND k.key2 = d.key2"
, "WHERE k.diff_type = 2"
, "AND k.diff_uid BETWEEN 0 AND 99;"
));
}
@Test public void Delete() {
fxt.Test__delete("tbl1", String_.Ary("key1", "key2"), 0, 99, String_.Concat_lines_nl_skip_last
( "DELETE db_curr.tbl1"
, "WHERE key1 || '|' || key2 IN"
, "( SELECT k.key1 || '|' || k.key2"
, " FROM db_temp.tbl1_pkey k"
, " JOIN db_diff.tbl1 d ON k.key1 = d.key1 AND k.key2 = d.key2"
, " WHERE k.diff_type = 0"
, " AND k.diff_uid BETWEEN 0 AND 99"
, ");"
));
}
}
class Gfdb_diff_cmd_sql_bldr_fxt {
private Gfdb_diff_cmd_sql_bldr bldr = new Gfdb_diff_cmd_sql_bldr();
private final Bry_bfr bfr = Bry_bfr.new_();
public void Test__insert(String tbl_name, String[] keys, String[] flds, int rng_bgn, int rng_end, String expd) {
bldr.Bld_insert(bfr, tbl_name, keys, flds, 0, 99);
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear());
}
public void Test__update(String tbl_name, String[] keys, String[] flds, int rng_bgn, int rng_end, String expd) {
bldr.Bld_update(bfr, tbl_name, keys, flds, 0, 99);
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear());
}
public void Test__delete(String tbl_name, String[] keys, int rng_bgn, int rng_end, String expd) {
bldr.Bld_delete(bfr, tbl_name, keys, 0, 99);
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear());
}
}