mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
'v3.3.4.1'
This commit is contained in:
@@ -16,7 +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.core.srls; import gplx.*; import gplx.core.*;
|
||||
import gplx.dbs.metas.*;
|
||||
import gplx.dbs.*; import gplx.dbs.metas.*;
|
||||
public class Dbmeta_dat_mgr {
|
||||
private final Ordered_hash hash = Ordered_hash_.New();
|
||||
public Dbmeta_dat_mgr Clear() {hash.Clear(); return this;}
|
||||
|
||||
@@ -29,7 +29,7 @@ public class DbMaprItm {
|
||||
contextFlds.Add(arg.ObjProp(), arg);
|
||||
return this;
|
||||
}
|
||||
public DbMaprItm ConstantFlds_add(String dbFld, Object dbVal) {constantFlds.Add(dbFld, KeyVal_.new_(dbFld, dbVal)); return this;}
|
||||
public DbMaprItm ConstantFlds_add(String dbFld, Object dbVal) {constantFlds.Add(dbFld, Keyval_.new_(dbFld, dbVal)); return this;}
|
||||
public DbMaprItm Subs_add(DbMaprItm... ary) {
|
||||
for (DbMaprItm itm : ary)
|
||||
subs.Add(itm);
|
||||
|
||||
@@ -44,16 +44,16 @@ public class DbMaprMgr_tst {
|
||||
} DbMaprMgr mgr; DbMaprWtr wtr; Db_conn conn; MockDisc disc; MockTitle title; MockChapter chapter; MockStream audio, subtitle; SrlMgr rdr;
|
||||
@Test public void PurgeObjTree() {
|
||||
disc = MockDisc.new_().Id_(1);
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1));
|
||||
DbMaprWtrUtl.PurgeObjTree(disc, mgr, conn);
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(conn, "mock_discs"));
|
||||
}
|
||||
@Test public void PurgeObjTree_deep() {
|
||||
disc = MockDisc.new_().Id_(1);
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", KeyValList.args_("disc_id", 1).Add("title_id", 2).Add("chapter_id", 3));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", KeyValList.args_("disc_id", 2).Add("title_id", 2).Add("chapter_id", 3));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 2).Add("chapter_id", 3));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", Keyval_list.New_with_one("disc_id", 2).Add("title_id", 2).Add("chapter_id", 3));
|
||||
DbMaprWtrUtl.PurgeObjTree(disc, mgr, conn);
|
||||
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(conn, "mock_discs"));
|
||||
@@ -92,7 +92,7 @@ public class DbMaprMgr_tst {
|
||||
}
|
||||
@Test public void Load_root() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1).Add("disc_name", "name"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc.Instance, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
@@ -101,9 +101,9 @@ public class DbMaprMgr_tst {
|
||||
}
|
||||
@Test public void Load_subs() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 2).Add("title_name", "title2"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 2).Add("title_name", "title2"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc.Instance, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
@@ -114,11 +114,11 @@ public class DbMaprMgr_tst {
|
||||
}
|
||||
@Test public void Load_deep() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("chapter_id", 3).Add("chapter_name", "chapter1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_streams", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("stream_id", 4).Add("stream_type", 0).Add("stream_name", "audio1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_streams", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("stream_id", 5).Add("stream_type", 1).Add("stream_name", "subtitle1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("chapter_id", 3).Add("chapter_name", "chapter1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_streams", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("stream_id", 4).Add("stream_type", 0).Add("stream_name", "audio1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_streams", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("stream_id", 5).Add("stream_type", 1).Add("stream_name", "subtitle1"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc.Instance, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
|
||||
@@ -50,7 +50,7 @@ public class DbMaprRdr extends DataRdr_base implements SrlMgr {
|
||||
Criteria rv = null, cur = null;
|
||||
List_adp list = GetIdxFlds(mgr, mapr);
|
||||
for (Object kvObj : list) {
|
||||
KeyVal kv = (KeyVal)kvObj;
|
||||
Keyval kv = (Keyval)kvObj;
|
||||
cur = Db_crt_.New_eq(kv.Key(), kv.Val());
|
||||
rv = (rv == null) ? cur : Criteria_.And(rv, cur);
|
||||
}
|
||||
@@ -65,11 +65,11 @@ public class DbMaprRdr extends DataRdr_base implements SrlMgr {
|
||||
for (Object argObj : mapr.ContextFlds()) {
|
||||
DbMaprArg arg = (DbMaprArg)argObj;
|
||||
Object propVal = GfoInvkAble_.InvkCmd((GfoInvkAble)gobj, arg.ObjProp());
|
||||
rv.Add(KeyVal_.new_(arg.DbFld(), propVal));
|
||||
rv.Add(Keyval_.new_(arg.DbFld(), propVal));
|
||||
}
|
||||
}
|
||||
for (Object argObj : curMapr.ConstantFlds()) {
|
||||
KeyVal arg = (KeyVal)argObj;
|
||||
Keyval arg = (Keyval)argObj;
|
||||
rv.Add(arg);
|
||||
}
|
||||
return rv;
|
||||
@@ -109,7 +109,7 @@ public class DbMaprRdr extends DataRdr_base implements SrlMgr {
|
||||
@Override public int FieldCount() {throw Err_.new_unimplemented();}
|
||||
@Override public String KeyAt(int i) {throw Err_.new_unimplemented();}
|
||||
@Override public Object ReadAt(int i) {throw Err_.new_unimplemented();}
|
||||
@Override public KeyVal KeyValAt(int i) {throw Err_.new_unimplemented();}
|
||||
@Override public Keyval KeyValAt(int i) {throw Err_.new_unimplemented();}
|
||||
@Override public SrlMgr SrlMgr_new(Object o) {return new DbMaprRdr();}
|
||||
Hash_adp tables = Hash_adp_.new_();
|
||||
Db_conn conn; Criteria rootCrt;
|
||||
|
||||
28
140_dbs/src/gplx/dbs/Db_attach_itm.java
Normal file
28
140_dbs/src/gplx/dbs/Db_attach_itm.java
Normal 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; import gplx.*;
|
||||
public class Db_attach_itm {
|
||||
public Db_attach_itm(String key, Io_url url) {
|
||||
this.Key = key; this.Url = url;
|
||||
}
|
||||
public Db_attach_itm(String key, Db_conn conn) {
|
||||
this.Key = key; this.Url = gplx.dbs.engines.sqlite.Sqlite_conn_info.To_url(conn);
|
||||
}
|
||||
public final String Key;
|
||||
public final Io_url Url;
|
||||
}
|
||||
120
140_dbs/src/gplx/dbs/Db_attach_mgr.java
Normal file
120
140_dbs/src/gplx/dbs/Db_attach_mgr.java
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
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.sqls.*; import gplx.dbs.sqls.itms.*;
|
||||
public class Db_attach_mgr {
|
||||
private final Ordered_hash hash = Ordered_hash_.New();
|
||||
public final List_adp attached_dbs_list = List_adp_.new_();
|
||||
public String Attached_sql() {return attached_sql;} private String attached_sql; // TEST
|
||||
private Db_conn main_conn; private Io_url main_conn_url;
|
||||
public Db_attach_mgr() {}
|
||||
public Db_attach_mgr(Db_conn main_conn, Db_attach_itm... itms_ary) {
|
||||
this.Main_conn_(main_conn);
|
||||
this.Init(itms_ary);
|
||||
}
|
||||
public Db_attach_mgr Init(Db_attach_itm... itms_ary) {
|
||||
hash.Clear();
|
||||
int itms_len = itms_ary.length;
|
||||
for (int i = 0; i < itms_len; ++i) {
|
||||
Db_attach_itm itm = itms_ary[i];
|
||||
hash.Add(itm.Key, itm);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public Db_attach_mgr Main_conn_(Db_conn conn) {
|
||||
this.main_conn = conn; this.main_conn_url = Db_conn_info_.To_url(conn.Conn_info());
|
||||
return this;
|
||||
}
|
||||
public void Attach() {
|
||||
int len = attached_dbs_list.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_attach_itm itm = (Db_attach_itm)attached_dbs_list.Get_at(i);
|
||||
main_conn.Env_db_attach(itm.Key, itm.Url);
|
||||
}
|
||||
}
|
||||
public void Detach() {
|
||||
int len = attached_dbs_list.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_attach_itm itm = (Db_attach_itm)attached_dbs_list.Get_at(i);
|
||||
main_conn.Env_db_detach(itm.Key);
|
||||
}
|
||||
attached_dbs_list.Clear(); // clear list so multiple detachs don't fail
|
||||
}
|
||||
public String List__to_str() {
|
||||
String rv = "";
|
||||
int len = attached_dbs_list.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_attach_itm itm = (Db_attach_itm)attached_dbs_list.Get_at(i);
|
||||
rv += itm.Key + ";";
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String Resolve_sql(String sql) {
|
||||
attached_dbs_list.Clear();
|
||||
int hash_len = hash.Count();
|
||||
for (int i = 0; i < hash_len; ++i) {
|
||||
Db_attach_itm attach_itm = (Db_attach_itm)hash.Get_at(i);
|
||||
String tkn = "<" + attach_itm.Key + ">";
|
||||
if (String_.Has(sql, tkn)) {
|
||||
Io_url attach_url = attach_itm.Url;
|
||||
String repl = "";
|
||||
if (!attach_url.Eq(main_conn_url)) {
|
||||
repl = attach_itm.Key + ".";
|
||||
attached_dbs_list.Add(attach_itm);
|
||||
}
|
||||
sql = String_.Replace(sql, tkn, repl);
|
||||
}
|
||||
}
|
||||
attached_sql = sql;
|
||||
return sql;
|
||||
}
|
||||
public Db_stmt Make_stmt_and_attach(Db_qry qry, gplx.dbs.sqls.itms.Sql_from_clause from_itm) {
|
||||
attached_dbs_list.Clear();
|
||||
Sql_qry_wtr sql_wtr = main_conn.Engine().Sql_wtr();
|
||||
List_adp from_tbls = from_itm.Tbls;
|
||||
int from_tbls_len = from_tbls.Count();
|
||||
for (int i = 0; i < from_tbls_len; ++i) {
|
||||
Sql_tbl_itm from_tbl = (Sql_tbl_itm)from_tbls.Get_at(i);
|
||||
String from_tbl_db = from_tbl.Db;
|
||||
if (String_.Eq(Sql_tbl_itm.Db__null, from_tbl_db)) continue; // tbl does not have db defined; only "tbl" not "db.tbl"; skip
|
||||
Db_attach_itm attach_itm = (Db_attach_itm)hash.Get_by(from_tbl_db); if (attach_itm == null) throw Err_.new_("dbs", "qry defines an unknown database for attach_wkr", "from_tbl_db", from_tbl_db, "sql", qry.To_sql__exec(sql_wtr));
|
||||
if (attach_itm.Url.Eq(main_conn_url)) // attach_db same as conn; blank db, so "tbl", not "db.tbl"
|
||||
from_tbl.Db_enabled = false;
|
||||
else
|
||||
attached_dbs_list.Add(attach_itm);
|
||||
}
|
||||
attached_sql = sql_wtr.To_sql_str(qry, true);
|
||||
this.Attach();
|
||||
for (int i = 0; i < from_tbls_len; ++i) { // reverse blanking from above
|
||||
Sql_tbl_itm from_tbl = (Sql_tbl_itm)from_tbls.Get_at(i);
|
||||
from_tbl.Db_enabled = true;
|
||||
}
|
||||
return main_conn.Stmt_sql(attached_sql);
|
||||
}
|
||||
public Db_attach_mgr Exec_sql_w_msg(String msg, String sql, Object... args) {
|
||||
Gfo_usr_dlg_.Instance.Plog_many("", "", msg);
|
||||
Exec_sql(sql, args);
|
||||
return this;
|
||||
}
|
||||
public void Exec_sql(String sql, Object... args) {
|
||||
String attach_sql = String_.Format(Resolve_sql(sql), args);
|
||||
this.Attach();
|
||||
try {main_conn.Exec_sql(attach_sql);}
|
||||
finally {this.Detach();}
|
||||
}
|
||||
}
|
||||
65
140_dbs/src/gplx/dbs/Db_attach_mgr__tst.java
Normal file
65
140_dbs/src/gplx/dbs/Db_attach_mgr__tst.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
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 org.junit.*; import gplx.dbs.qrys.*;
|
||||
public class Db_attach_mgr__tst {
|
||||
private final Db_attach_mgr__fxt fxt = new Db_attach_mgr__fxt();
|
||||
@Test public void Basic() {
|
||||
Db_qry__select_cmd qry = Db_qry_.select_()
|
||||
.Cols_w_tbl_("t1", "fld_1")
|
||||
.Cols_w_tbl_("t2", "fld_2")
|
||||
.Cols_w_tbl_("t3", "fld_3")
|
||||
.From_("db_1", "tbl_1", "t1")
|
||||
.Join_("db_2", "tbl_2", "t2", Db_qry_.New_join__same("t1", "fld_2"))
|
||||
.Join_("db_3", "tbl_3", "t3", Db_qry_.New_join__same("t1", "fld_3"))
|
||||
.Join_( "tbl_4", "t4", Db_qry_.New_join__same("t1", "fld_4"))
|
||||
;
|
||||
fxt.Init("db_2", fxt.Make__itm("db_1"), fxt.Make__itm("db_2"), fxt.Make__itm("db_3"));
|
||||
fxt.Test__make_stmt_and_attach(qry
|
||||
, "SELECT t1.fld_1, t2.fld_2, t3.fld_3 "
|
||||
+ "FROM db_1.tbl_1 t1 "
|
||||
+ "INNER JOIN tbl_2 t2 ON t1.fld_2 = t2.fld_2 " // NOTE: curr is db_2 so do not prefix tbl_2 with db_2; fails if "db_2.tbl_2"
|
||||
+ "INNER JOIN db_3.tbl_3 t3 ON t1.fld_3 = t3.fld_3 "
|
||||
+ "INNER JOIN tbl_4 t4 ON t1.fld_4 = t4.fld_4"
|
||||
, String_.Ary("db_1", "db_3") // NOTE: no "db_2"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Db_attach_mgr__fxt {
|
||||
private Db_attach_mgr mgr;
|
||||
public Db_attach_itm Make__itm(String key) {return new Db_attach_itm(key, Io_url_.mem_fil_("mem/" + key));}
|
||||
public Db_conn Make__conn(String key) {return Db_conn_pool.Instance.Get_or_new__mem(key);}
|
||||
public void Init(String conn_key, Db_attach_itm... ary) {
|
||||
Db_conn conn = Make__conn(conn_key);
|
||||
mgr = new Db_attach_mgr(conn, ary);
|
||||
}
|
||||
public void Test__make_stmt_and_attach(Db_qry__select_cmd qry, String expd_sql, String[] expd_dbs) {
|
||||
mgr.Make_stmt_and_attach(qry, qry.From());
|
||||
Tfds.Eq_str(expd_sql, mgr.Attached_sql());
|
||||
Tfds.Eq_ary_str(expd_dbs, To_key_ary(mgr.attached_dbs_list));
|
||||
}
|
||||
private static String[] To_key_ary(List_adp attach_dbs_list) {
|
||||
int rv_len = attach_dbs_list.Count();
|
||||
String[] rv = new String[rv_len];
|
||||
for (int i = 0; i < rv_len; ++i) {
|
||||
Db_attach_itm itm = (Db_attach_itm)attach_dbs_list.Get_at(i);
|
||||
rv[i] = itm.Key;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
@@ -16,59 +16,54 @@ 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.core.brys.fmtrs.*;
|
||||
import gplx.core.stores.*; import gplx.dbs.metas.*; import gplx.dbs.engines.*; import gplx.dbs.qrys.*; import gplx.dbs.sys.*;
|
||||
public class Db_conn {
|
||||
private final List_adp rls_list = List_adp_.new_(); private final Db_engine engine;
|
||||
private final Db_engine engine; private final List_adp rls_list = List_adp_.new_();
|
||||
private final Bry_fmt exec_sql_fmt = Bry_fmt.New(""); private final Bry_bfr exec_sql_bfr = Bry_bfr.new_();
|
||||
public Db_conn(Db_engine engine) {
|
||||
this.engine = engine;
|
||||
sys_mgr = new Db_sys_mgr(this);
|
||||
}
|
||||
public Db_engine Engine() {return engine;}
|
||||
public Db_conn_info Conn_info() {return engine.Conn_info();}
|
||||
public boolean Eq(Db_conn comp) {return String_.Eq(engine.Conn_info().Xto_api(), comp.Conn_info().Xto_api());}
|
||||
public Db_sys_mgr Sys_mgr() {return sys_mgr;} private final Db_sys_mgr sys_mgr;
|
||||
public boolean Eq(Db_conn comp) {return String_.Eq(engine.Conn_info().Db_api(), comp.Conn_info().Db_api());}
|
||||
public void Txn_bgn(String name) {engine.Txn_bgn(name);}
|
||||
public void Txn_end() {engine.Txn_end();}
|
||||
public void Txn_cxl() {engine.Txn_cxl();}
|
||||
public void Txn_sav() {engine.Txn_sav();}
|
||||
public Db_stmt Stmt_insert(String tbl, Dbmeta_fld_list flds) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, flds.To_str_ary_wo_autonum()));}
|
||||
public Db_stmt Stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt Stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.New(tbl, where, cols));}
|
||||
public Db_stmt Stmt_update_exclude(String tbl, Dbmeta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry_update.New(tbl, where, flds.To_str_ary_exclude(where)));}
|
||||
public Db_stmt Stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt Stmt_select(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols, null));}
|
||||
public Db_stmt Stmt_select(String tbl, Dbmeta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary(), null));}
|
||||
public Db_stmt Stmt_select_max(String tbl, String col, String... where) {
|
||||
Db_qry__select_in_tbl qry = new Db_qry__select_in_tbl(tbl, String_.Ary(String_.Format("Max({0}) AS {0}", col)), where, null, null, null, null);
|
||||
return engine.New_stmt_prep(qry);
|
||||
}
|
||||
public void Env_db_attach(String alias, Db_conn conn) {engine.Env_db_attach(alias, conn);}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {engine.Env_db_attach(alias, db_url);}
|
||||
public void Env_db_detach(String alias) {engine.Env_db_detach(alias);}
|
||||
public void Env_vacuum() {Exec_sql_plog_ntx("vacuuming: url=" + this.Conn_info().Db_api(), "VACUUM;");}
|
||||
public boolean Meta_tbl_exists(String tbl) {return engine.Meta_tbl_exists(tbl);}
|
||||
public void Meta_tbl_create(Dbmeta_tbl_itm meta) {engine.Ddl_create_tbl(meta); engine.Ddl_create_idx(Gfo_usr_dlg_.Noop, meta.Idxs().To_ary());}
|
||||
public void Meta_tbl_drop(String tbl) {engine.Ddl_delete_tbl(tbl);}
|
||||
public void Meta_idx_create(Dbmeta_idx_itm... idxs) {engine.Ddl_create_idx(Gfo_usr_dlg_.Instance, idxs);}
|
||||
public void Meta_idx_create(Gfo_usr_dlg usr_dlg, Dbmeta_idx_itm... idxs) {engine.Ddl_create_idx(usr_dlg, idxs);}
|
||||
public boolean Meta_fld_exists(String tbl, String fld) {return engine.Meta_fld_exists(tbl, fld);}
|
||||
public void Meta_fld_append(String tbl, Dbmeta_fld_itm fld) {engine.Ddl_append_fld(tbl, fld);}
|
||||
public void Meta_fld_assert(String tbl, String fld, Dbmeta_fld_tid tid, Object dflt) {if (!Meta_fld_exists(tbl, fld)) this.Meta_fld_append(tbl, new Dbmeta_fld_itm(fld, tid).Default_(dflt));}
|
||||
public Dbmeta_tbl_mgr Meta_load_all() {return engine.Meta_tbl_load_all();}
|
||||
public Db_stmt Stmt_insert(String tbl, Dbmeta_fld_list flds) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, flds.To_str_ary_wo_autonum()));}
|
||||
public Db_stmt Stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt Stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.New(tbl, where, cols));}
|
||||
public Db_stmt Stmt_update_exclude(String tbl, Dbmeta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry_update.New(tbl, where, flds.To_str_ary_exclude(where)));}
|
||||
public Db_stmt Stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt Stmt_select(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols, null));}
|
||||
public Db_stmt Stmt_select(String tbl, Dbmeta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary(), null));}
|
||||
public Db_stmt Stmt_select_order(String tbl, Dbmeta_fld_list flds, String[] where, String... orderbys) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary(), orderbys));}
|
||||
public Db_stmt Stmt_select_order(String tbl, String[] flds, String[] where, String... orderbys) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds, orderbys));}
|
||||
public Db_stmt Stmt_new(Db_qry qry) {return engine.New_stmt_prep(qry);}
|
||||
public Db_sys_mgr Sys_mgr() {return sys_mgr;} private final Db_sys_mgr sys_mgr;
|
||||
public void Env_db_attach(String alias, Io_url db_url) {engine.Env_db_attach(alias, db_url);}
|
||||
public void Env_db_detach(String alias) {engine.Env_db_detach(alias);}
|
||||
public void Env_vacuum() {Exec_sql_plog_ntx("vacuuming: url=" + this.Conn_info().Xto_api(), "VACUUM;");}
|
||||
public void Ddl_create_tbl(Dbmeta_tbl_itm meta) {engine.Ddl_create_tbl(meta); engine.Ddl_create_idx(Gfo_usr_dlg_.Noop, meta.Idxs().To_ary());}
|
||||
public void Ddl_create_idx(Dbmeta_idx_itm... idxs) {engine.Ddl_create_idx(Gfo_usr_dlg_.Instance, idxs);}
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Dbmeta_idx_itm... idxs) {engine.Ddl_create_idx(usr_dlg, idxs);}
|
||||
public void Ddl_append_fld(String tbl, Dbmeta_fld_itm fld) {engine.Ddl_append_fld(tbl, fld);}
|
||||
public void Ddl_delete_tbl(String tbl) {engine.Ddl_delete_tbl(tbl);}
|
||||
public boolean Meta_tbl_exists(String tbl) {return engine.Meta_tbl_exists(tbl);}
|
||||
public boolean Meta_fld_exists(String tbl, String fld) {return engine.Meta_fld_exists(tbl, fld);}
|
||||
public Dbmeta_tbl_mgr Meta_tbl_load_all() {return engine.Meta_tbl_load_all();}
|
||||
public void Rls_reg(Rls_able rls) {rls_list.Add(rls);}
|
||||
public void Rls_conn() {
|
||||
int len = rls_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Rls_able itm = (Rls_able)rls_list.Get_at(i);
|
||||
itm.Rls();
|
||||
}
|
||||
engine.Conn_term();
|
||||
Db_conn_pool.Instance.Del(engine.Conn_info());
|
||||
}
|
||||
public void Exec_delete_all(String tbl) {Stmt_delete(tbl).Exec_delete();}
|
||||
public int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
|
||||
public Db_rdr Exec_sql_as_rdr_v2(String sql) {return this.Stmt_new(Db_qry_sql.dml_(sql)).Exec_select__rls_auto();}
|
||||
public Db_stmt Stmt_sql(String sql) {return engine.New_stmt_prep(Db_qry_sql.sql_(sql));}
|
||||
public int Exec_qry(Db_qry qry) {return Int_.cast(engine.Exec_as_obj(qry));}
|
||||
public int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
|
||||
public int Exec_sql(String msg, String sql) {Gfo_usr_dlg_.Instance.Plog_many("", "", msg); return this.Exec_sql(sql);}
|
||||
public Db_rdr Exec_rdr(String sql) {return this.Stmt_sql(sql).Exec_select__rls_auto();}
|
||||
public void Exec_delete_all(String tbl) {Stmt_delete(tbl).Exec_delete();}
|
||||
public int Exec_sql_args(String sql, Object... args) {return this.Exec_qry(Db_qry_sql.dml_(String_.Format(sql, args)));}
|
||||
public int Exec_sql_fmt_by_keys(String sql, String[] keys, Object... args) {return Exec_sql(exec_sql_fmt.Fmt_(sql).Keys_(Bry_.Ary(keys)).Bld_many_to_str(exec_sql_bfr, args));}
|
||||
public int Exec_sql_plog_ntx(String msg, String sql) {return Exec_sql_plog(Bool_.N, msg, sql);}
|
||||
public int Exec_sql_plog_txn(String msg, String sql) {return Exec_sql_plog(Bool_.Y, msg, sql);}
|
||||
public int Exec_sql_plog(boolean txn, String msg, String sql) {
|
||||
@@ -79,8 +74,29 @@ public class Db_conn {
|
||||
Gfo_usr_dlg_.Instance.Plog_many("", "", "done:" + msg);
|
||||
return rv;
|
||||
}
|
||||
public int Exec_qry(Db_qry qry) {return Int_.cast(engine.Exec_as_obj(qry));}
|
||||
public int Exec_sql_args(String sql, Object... args) {return this.Exec_qry(Db_qry_sql.dml_(String_.Format(sql, args)));}
|
||||
public int Exec_select_as_int (String sql, int or) {Object rv = Exec_select_as_obj(sql); return rv == null ? or : Int_.cast(rv);}
|
||||
public double Exec_select_as_double (String sql, double or) {Object rv = Exec_select_as_obj(sql); return rv == null ? or : Double_.cast(rv);}
|
||||
private Object Exec_select_as_obj(String sql) {
|
||||
Db_rdr rdr = Exec_rdr(sql);
|
||||
try {return rdr.Move_next() ? rdr.Read_at(0) : null;}
|
||||
finally {rdr.Rls();}
|
||||
}
|
||||
public void Rls_reg(Rls_able rls) {rls_list.Add(rls);}
|
||||
public void Rls_conn() {
|
||||
int len = rls_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Rls_able itm = (Rls_able)rls_list.Get_at(i);
|
||||
itm.Rls();
|
||||
}
|
||||
engine.Conn_term();
|
||||
Db_conn_pool.Instance.Del(engine.Conn_info());
|
||||
}
|
||||
|
||||
public Db_stmt Stmt_select_max(String tbl, String col, String... where) {
|
||||
Db_qry__select_in_tbl qry = new Db_qry__select_in_tbl(tbl, String_.Ary(String_.Format("Max({0}) AS {0}", col)), where, null, null, null, null);
|
||||
return engine.New_stmt_prep(qry);
|
||||
}
|
||||
|
||||
public DataRdr Exec_sql_as_old_rdr(String sql) {return DataRdr_.cast(engine.Exec_as_obj(Db_qry_sql.rdr_(sql)));}
|
||||
public DataRdr Exec_qry_as_old_rdr(Db_qry qry) {return DataRdr_.cast(engine.Exec_as_obj(qry));}
|
||||
}
|
||||
|
||||
@@ -16,24 +16,6 @@ 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.core.stores.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Db_conn_ {
|
||||
public static final Db_conn Noop = Db_conn_pool.Instance.Get_or_new(Db_conn_info_.Null);
|
||||
public static int Select_fld0_as_int_or(Db_conn conn, String sql, int or) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = conn.Exec_qry_as_old_rdr(Db_qry_sql.rdr_(sql));
|
||||
int rv = or;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
Object rv_obj = rdr.ReadAt(0);
|
||||
if (rv_obj != null) // Max(fil_id) will be NULL if tbl is empty
|
||||
rv = Int_.cast_or(rv_obj, or);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
finally {
|
||||
rdr.Rls();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_conn_info {
|
||||
String Tid();
|
||||
String Database();
|
||||
String Xto_raw();
|
||||
String Xto_api();
|
||||
Db_conn_info New_self(String raw, GfoMsg m);
|
||||
String Key(); // EX: "sqlite"
|
||||
String Raw(); // EX: "gplx_key=sqlite;data source=/db.sqlite3;version=3"
|
||||
String Db_api(); // EX: "data source=/db.sqlite3;version=3"
|
||||
String Database(); // EX: /db.sqlite3 -> "db" ; xowa -> "xowa"
|
||||
Db_conn_info New_self(String raw, Keyval_hash hash);
|
||||
}
|
||||
|
||||
@@ -24,19 +24,24 @@ public class Db_conn_info_ {
|
||||
public static Db_conn_info parse(String raw) {return Db_conn_info_pool.Instance.Parse(raw);}
|
||||
public static Db_conn_info sqlite_(Io_url url) {return Sqlite_conn_info.load_(url);}
|
||||
public static Db_conn_info tdb_(Io_url url) {return Tdb_conn_info.new_(url);}
|
||||
public static Db_conn_info mem_(String db) {return Db_conn_info__mem.new_(db);}
|
||||
public static Db_conn_info mem_(String db) {return Mem_conn_info.new_(db);}
|
||||
public static final String Key_tdb = Tdb_conn_info.Tid_const;
|
||||
public static Io_url To_url(Db_conn_info cs) {
|
||||
if (String_.Eq(cs.Key(), Sqlite_conn_info.Key_const)) return ((Sqlite_conn_info)cs).Url();
|
||||
else if (String_.Eq(cs.Key(), Mem_conn_info.Instance.Key())) return Io_url_.mem_fil_("mem/" + ((Mem_conn_info)cs).Database());
|
||||
else throw Err_.new_unhandled_default(cs.Key());
|
||||
}
|
||||
}
|
||||
class Db_conn_info_pool {
|
||||
private Ordered_hash regy = Ordered_hash_.New();
|
||||
private final Ordered_hash regy = Ordered_hash_.New();
|
||||
public Db_conn_info_pool() {
|
||||
this.Add(Noop_conn_info.Instance).Add(Tdb_conn_info.Instance).Add(Mysql_conn_info.Instance).Add(Postgres_conn_info.Instance).Add(Sqlite_conn_info.Instance);
|
||||
this.Add(Db_conn_info__mem.Instance);
|
||||
this.Add(Mem_conn_info.Instance);
|
||||
}
|
||||
public Db_conn_info_pool Add(Db_conn_info itm) {regy.Add_if_dupe_use_nth(itm.Tid(), itm); return this;}
|
||||
public Db_conn_info_pool Add(Db_conn_info itm) {regy.Add_if_dupe_use_nth(itm.Key(), itm); return this;}
|
||||
public Db_conn_info Parse(String raw) {// assume each pair has format of: name=val;
|
||||
try {
|
||||
GfoMsg m = GfoMsg_.new_parse_("db_url");
|
||||
Keyval_hash hash = new Keyval_hash();
|
||||
String[] terms = String_.Split(raw, ";");
|
||||
String url_tid = "";
|
||||
for (String term : terms) {
|
||||
@@ -45,15 +50,15 @@ class Db_conn_info_pool {
|
||||
if (String_.Eq(kv[0], "gplx_key"))
|
||||
url_tid = kv[1]; // NOTE: do not add to GfoMsg; will not be part of ApiStr
|
||||
else
|
||||
m.Add(kv[0], kv[1]);
|
||||
hash.Add(kv[0], kv[1]);
|
||||
}
|
||||
Db_conn_info prototype = (Db_conn_info)regy.Get_by(url_tid);
|
||||
return prototype.New_self(raw, m);
|
||||
return prototype.New_self(raw, hash);
|
||||
}
|
||||
catch(Exception exc) {throw Err_.new_parse_exc(exc, Db_conn_info.class, raw);}
|
||||
}
|
||||
public Db_conn_info Parse_or_sqlite_or_fail(String raw) {// assume each pair has format of: name=val;
|
||||
GfoMsg msg = GfoMsg_.new_parse_("db_url");
|
||||
Keyval_hash hash = new Keyval_hash();
|
||||
String[] kvps = String_.Split(raw, ";");
|
||||
String cs_tid = null;
|
||||
int kvps_len = kvps.length;
|
||||
@@ -65,19 +70,19 @@ class Db_conn_info_pool {
|
||||
if (String_.Eq(key, "gplx_key"))
|
||||
cs_tid = val; // NOTE: do not add to GfoMsg; will not be part of ApiStr
|
||||
else
|
||||
msg.Add(key, val);
|
||||
hash.Add(key, val);
|
||||
}
|
||||
if (cs_tid == null) { // gplx_key not found; try url as sqlite; EX: "/db.sqlite"
|
||||
Io_url sqlite_url = null;
|
||||
try {sqlite_url = Io_url_.new_any_(raw);}
|
||||
catch (Exception exc) {throw Err_.new_exc(exc, "dbs", "invalid connection String", "raw", raw);}
|
||||
msg.Clear();
|
||||
cs_tid = Sqlite_conn_info.Tid_const;
|
||||
msg.Add(Sqlite_conn_info.Cs__data_source, sqlite_url.Raw());
|
||||
msg.Add(Sqlite_conn_info.Cs__version , Sqlite_conn_info.Cs__version__3);
|
||||
hash.Clear();
|
||||
cs_tid = Sqlite_conn_info.Key_const;
|
||||
hash.Add(Sqlite_conn_info.Cs__data_source, sqlite_url.Raw());
|
||||
hash.Add(Sqlite_conn_info.Cs__version , Sqlite_conn_info.Cs__version__3);
|
||||
}
|
||||
Db_conn_info prototype = (Db_conn_info)regy.Get_by(cs_tid);
|
||||
return prototype.New_self(raw, msg);
|
||||
return prototype.New_self(raw, hash);
|
||||
}
|
||||
public static final Db_conn_info_pool Instance = new Db_conn_info_pool();
|
||||
}
|
||||
|
||||
@@ -16,30 +16,15 @@ 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.core.strings.*;
|
||||
public abstract class Db_conn_info__base implements Db_conn_info {
|
||||
public abstract String Tid();
|
||||
public String Xto_raw() {return raw;} private String raw = "";
|
||||
public String Xto_api() {return api;} private String api = "";
|
||||
public String Database() {return database;} protected String database = "";
|
||||
public String Server() {return server;} private String server = "";
|
||||
public abstract Db_conn_info New_self(String raw, GfoMsg m);
|
||||
protected void Ctor(String server, String database, String raw, String api) {this.server = server; this.database = database; this.raw = raw; this.api = api;}
|
||||
protected static String BldApi(GfoMsg m, KeyVal... xtnAry) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Hash_adp hash = Hash_adp_.new_();
|
||||
for (int i = 0; i < m.Args_count(); i++) {
|
||||
KeyVal kv = m.Args_getAt(i);
|
||||
sb.Add_fmt("{0}={1};", kv.Key(), kv.Val_to_str_or_empty());
|
||||
hash.Add_as_key_and_val(kv.Key());
|
||||
}
|
||||
for (KeyVal xtn : xtnAry) {
|
||||
if (hash.Has(xtn.Key())) continue;
|
||||
sb.Add_fmt("{0}={1};", xtn.Key(), xtn.Val_to_str_or_empty());
|
||||
}
|
||||
return sb.To_str();
|
||||
}
|
||||
protected static String Bld_raw(String... ary) {
|
||||
public Db_conn_info__base(String raw, String db_api, String database) {this.raw = raw; this.db_api = db_api; this.database = database;}
|
||||
public abstract String Key();
|
||||
public String Raw() {return raw;} private final String raw;
|
||||
public String Db_api() {return db_api;} private final String db_api;
|
||||
public String Database() {return database;} protected final String database;
|
||||
public abstract Db_conn_info New_self(String raw, Keyval_hash hash);
|
||||
|
||||
protected static String Bld_raw(String... ary) {// "a", "b" -> "a=b;"
|
||||
Bry_bfr bfr = Bry_bfr.reset_(255);
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
@@ -47,6 +32,19 @@ public abstract class Db_conn_info__base implements Db_conn_info {
|
||||
bfr.Add_str_u8(itm);
|
||||
bfr.Add_byte(i % 2 == 0 ? Byte_ascii.Eq : Byte_ascii.Semic);
|
||||
}
|
||||
return bfr.To_str();
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
protected static String Bld_api(Keyval_hash hash, Keyval... xtn_ary) {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
int len = hash.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Keyval kv = hash.Get_at(i);
|
||||
bfr.Add_str_u8_fmt("{0}={1};", kv.Key(), kv.Val_to_str_or_empty());
|
||||
}
|
||||
for (Keyval xtn : xtn_ary) {
|
||||
if (hash.Has(xtn.Key())) continue;
|
||||
bfr.Add_str_u8_fmt("{0}={1};", xtn.Key(), xtn.Val_to_str_or_empty());
|
||||
}
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,21 +26,23 @@ public class Db_conn_info_tst {
|
||||
tst_Parse("gplx_key=mock;id=1;name=me;", kv_("id", "1"), kv_("name", "me")); // many
|
||||
tst_Parse("gplx_key=mock;id=1;name=me" , kv_("id", "1"), kv_("name", "me")); // no semi-colon at end
|
||||
}
|
||||
private KeyVal kv_(String key, Object val) {return KeyVal_.new_(key, val);}
|
||||
private void tst_Parse(String raw, KeyVal... expd) {
|
||||
private Keyval kv_(String key, Object val) {return Keyval_.new_(key, val);}
|
||||
private void tst_Parse(String raw, Keyval... expd) {
|
||||
Db_conn_info_mock mock = (Db_conn_info_mock)regy.Parse(raw);
|
||||
Tfds.Eq_ary_str(expd, mock.Kvs());
|
||||
}
|
||||
}
|
||||
class Db_conn_info_mock extends Db_conn_info__base {
|
||||
public KeyVal[] Kvs() {return kvs;} KeyVal[] kvs;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mock";
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Db_conn_info_mock rv = new Db_conn_info_mock();
|
||||
rv.kvs = new KeyVal[m.Args_count()];
|
||||
for (int i = 0; i < m.Args_count(); i++)
|
||||
rv.kvs[i] = m.Args_getAt(i);
|
||||
public Db_conn_info_mock(String raw, String db_api, String database) {super(raw, db_api, database);}
|
||||
public Keyval[] Kvs() {return kvs;} Keyval[] kvs;
|
||||
@Override public String Key() {return Tid_const;} public static final String Tid_const = "mock";
|
||||
@Override public Db_conn_info New_self(String raw, Keyval_hash hash) {
|
||||
Db_conn_info_mock rv = new Db_conn_info_mock("", "", "");
|
||||
int len = hash.Count();
|
||||
rv.kvs = new Keyval[len];
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv.kvs[i] = hash.Get_at(i);
|
||||
return rv;
|
||||
}
|
||||
public static final Db_conn_info_mock Instance = new Db_conn_info_mock(); Db_conn_info_mock() {}
|
||||
public static final Db_conn_info_mock Instance = new Db_conn_info_mock("", "", "");
|
||||
}
|
||||
|
||||
@@ -21,17 +21,17 @@ import gplx.dbs.engines.sqlite.*; import gplx.dbs.engines.mysql.*; import gplx.d
|
||||
public class Db_conn_pool {
|
||||
private final Hash_adp conn_hash = Hash_adp_.new_(); private final Hash_adp engine_hash = Hash_adp_.new_();
|
||||
public void Clear() {conn_hash.Clear();}
|
||||
public void Del(Db_conn_info url) {conn_hash.Del(url.Xto_api());}
|
||||
public Db_conn Get_or_new__mem(String db) {return Get_or_new(Db_conn_info__mem.new_(db));}
|
||||
public void Del(Db_conn_info url) {conn_hash.Del(url.Db_api());}
|
||||
public Db_conn Get_or_new__mem(String db) {return Get_or_new(Mem_conn_info.new_(db));}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_conn_info_.sqlite_(url));}
|
||||
public Db_conn Get_or_new(String s) {return Get_or_new(Db_conn_info_.parse(s));}
|
||||
public Db_conn Get_or_new(Db_conn_info url) {
|
||||
Db_conn rv = (Db_conn)conn_hash.Get_by(url.Xto_api());
|
||||
Db_conn rv = (Db_conn)conn_hash.Get_by(url.Db_api());
|
||||
if (rv == null) {
|
||||
Db_engine prime = (Db_engine)engine_hash.Get_by(url.Tid()); if (prime == null) Err_.new_wo_type("db engine prototype not found", "tid", url.Tid());
|
||||
Db_engine prime = (Db_engine)engine_hash.Get_by(url.Key()); if (prime == null) Err_.new_wo_type("db engine prototype not found", "key", url.Key());
|
||||
Db_engine clone = prime.New_clone(url);
|
||||
rv = new Db_conn(clone);
|
||||
conn_hash.Add(url.Xto_api(), rv);
|
||||
conn_hash.Add(url.Db_api(), rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
@@ -41,6 +41,6 @@ public class Db_conn_pool {
|
||||
}
|
||||
public static final Db_conn_pool Instance = new Db_conn_pool(); Db_conn_pool() {this.Init();}
|
||||
private void Init() {
|
||||
this.Engines__add(Noop_engine.Instance, TdbEngine.Instance, Mysql_engine.Instance, Postgres_engine.Instance, Sqlite_engine.Instance, Db_engine__mem.Instance);
|
||||
this.Engines__add(Noop_engine.Instance, TdbEngine.Instance, Mysql_engine.Instance, Postgres_engine.Instance, Sqlite_engine.Instance, Mem_engine.Instance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,11 +22,8 @@ public class Db_conn_utl {
|
||||
Db_conn_bldr.Instance.Reg_default_mem();
|
||||
return Db_conn_bldr.Instance.Get_or_new(Io_url_.mem_fil_("mem/" + url_rel)).Conn();
|
||||
}
|
||||
public static void Tbl__delete(Db_conn conn, String tbl) {
|
||||
conn.Ddl_delete_tbl(tbl);
|
||||
}
|
||||
public static void Tbl__new(Db_conn conn, String tbl, Dbmeta_fld_itm[] flds, Object[]... rows) {
|
||||
conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl, flds));
|
||||
conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl, flds));
|
||||
int rows_len = rows.length;
|
||||
Db_stmt stmt = conn.Stmt_insert(tbl, Dbmeta_fld_itm.To_str_ary(flds));
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
@@ -51,4 +48,30 @@ public class Db_conn_utl {
|
||||
stmt.Exec_insert();
|
||||
}
|
||||
}
|
||||
public static void Insert(Db_conn conn, String tbl_name, String[] flds, Object[]... rows) {
|
||||
Db_stmt stmt = Db_stmt_.new_insert_(conn, tbl_name, flds);
|
||||
int flds_len = flds.length;
|
||||
int rows_len = rows.length;
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
Object[] row = rows[i];
|
||||
stmt.Clear();
|
||||
for (int j = 0; j < flds_len; ++j)
|
||||
Db_stmt_.Val_by_obj(stmt, flds[j], row[j]);
|
||||
stmt.Exec_insert();
|
||||
}
|
||||
}
|
||||
public static Object[][] Select(Db_conn conn, Db_qry qry) {
|
||||
List_adp rv = List_adp_.new_();
|
||||
Db_rdr rdr = conn.Stmt_new(qry).Exec_select__rls_auto();
|
||||
try {
|
||||
while (rdr.Move_next()) {
|
||||
int fld_len = rdr.Fld_len();
|
||||
Object[] row = new Object[fld_len];
|
||||
for (int i = 0; i < fld_len; ++i)
|
||||
row[i] = rdr.Read_at(i);
|
||||
rv.Add(row);
|
||||
}
|
||||
} finally {rdr.Rls();}
|
||||
return (Object[][])rv.To_ary_and_clear(Object[].class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package gplx.dbs; import gplx.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Db_crt_ {
|
||||
public static final Criteria Wildcard = Criteria_.All;
|
||||
public static Criteria New_and (Criteria lhs, Criteria rhs) {return Criteria_.And(lhs, rhs);}
|
||||
public static Criteria_fld New_eq (String key, Object val) {return Criteria_fld.new_(key, Criteria_.eq_(val));}
|
||||
public static Criteria_fld New_eq (String pre, String key, Object val) {return Criteria_fld.new_(pre, key, Criteria_.eq_(val));}
|
||||
public static Criteria_fld New_eq_not (String key, Object val) {return Criteria_fld.new_(key, Criteria_.eqn_(val));}
|
||||
@@ -51,10 +52,10 @@ public class Db_crt_ {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Criteria eq_many_(KeyVal... array) {
|
||||
public static Criteria eq_many_(Keyval... array) {
|
||||
Criteria rv = null;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
KeyVal pair = array[i];
|
||||
Keyval pair = array[i];
|
||||
Criteria crt = Db_crt_.New_eq(pair.Key(), pair.Val());
|
||||
rv = (i == 0)? crt : Criteria_.And(rv, crt);
|
||||
}
|
||||
|
||||
24
140_dbs/src/gplx/dbs/Db_null.java
Normal file
24
140_dbs/src/gplx/dbs/Db_null.java
Normal 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; import gplx.*;
|
||||
public class Db_null implements gplx.core.brys.Bfr_arg {
|
||||
public void Bfr_arg__add(Bry_bfr bfr) {bfr.Add_str_a7(Null_str);}
|
||||
@Override public String toString() {return Null_str;}
|
||||
public static final String Null_str = "NULL";
|
||||
public static final Db_null Instance = new Db_null(); Db_null() {}
|
||||
}
|
||||
@@ -20,21 +20,21 @@ import gplx.core.criterias.*; import gplx.dbs.qrys.*; import gplx.dbs.sqls.*; im
|
||||
public class Db_qry_ {
|
||||
public static Db_qry__select_cmd select_cols_(String tbl, Criteria crt, String... cols){return select_().From_(tbl).Where_(crt).Cols_(cols);}
|
||||
public static Db_qry__select_cmd select_val_(String tbl, String col, Criteria crt) {return select_().From_(tbl).Where_(crt).Cols_(col);}
|
||||
public static Db_qry__select_cmd select_tbl_(String tbl) {return select_().From_(tbl);}
|
||||
public static Db_qry__select_cmd select_tbl_(String tbl) {return select_().From_(tbl).Cols_all_();}
|
||||
public static Db_qry__select_cmd select_() {return new Db_qry__select_cmd();}
|
||||
public static Db_qry__select_cmd select_(String tbl, String... cols) {return new Db_qry__select_cmd().From_(tbl).Cols_(cols);}
|
||||
public static Db_qry_delete delete_(String tbl, Criteria crt) {return Db_qry_delete.new_(tbl, crt);}
|
||||
public static Db_qry_delete delete_tbl_(String tbl) {return Db_qry_delete.new_(tbl);}
|
||||
public static Db_qry_insert insert_(String tbl) {return new Db_qry_insert(tbl);}
|
||||
public static Db_qry_insert insert_common_(String tbl, KeyVal... pairs) {
|
||||
public static Db_qry_insert insert_common_(String tbl, Keyval... pairs) {
|
||||
Db_qry_insert cmd = new Db_qry_insert(tbl);
|
||||
for (KeyVal pair : pairs)
|
||||
for (Keyval pair : pairs)
|
||||
cmd.Val_obj(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public static Sql_join_itm New_join__join(String trg_fld, String src_tbl, String src_fld) {return new Sql_join_itm(trg_fld, src_tbl, src_fld);}
|
||||
public static Sql_join_itm New_join__same(String tbl, String fld) {return new Sql_join_itm(fld, tbl, fld);}
|
||||
public static Sql_join_fld New_join__join(String trg_fld, String src_tbl, String src_fld) {return new Sql_join_fld(trg_fld, src_tbl, src_fld);}
|
||||
public static Sql_join_fld New_join__same(String tbl, String fld) {return new Sql_join_fld(fld, tbl, fld);}
|
||||
|
||||
public static Db_qry_update update_(String tbl, Criteria crt) {
|
||||
Db_qry_update update = new Db_qry_update();
|
||||
@@ -42,10 +42,10 @@ public class Db_qry_ {
|
||||
update.Where_(crt);
|
||||
return update;
|
||||
}
|
||||
public static Db_qry_update update_common_(String tbl, Criteria crt, KeyVal... pairs) {
|
||||
public static Db_qry_update update_common_(String tbl, Criteria crt, Keyval... pairs) {
|
||||
Db_qry_update cmd = new Db_qry_update();
|
||||
cmd.From_(tbl); cmd.Where_(crt);
|
||||
for (KeyVal pair : pairs)
|
||||
for (Keyval pair : pairs)
|
||||
cmd.Val_obj(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.qrys.*; import gplx.core.gfo_ndes.*;
|
||||
public class Db_qry_fxt {
|
||||
public static void Insert_kvo(Db_conn conn, String tblName, KeyValList kvList) {
|
||||
public static void Insert_kvo(Db_conn conn, String tblName, Keyval_list kvList) {
|
||||
Db_qry_insert qry = Db_qry_.insert_(tblName);
|
||||
for (int i = 0; i < kvList.Count(); i++) {
|
||||
KeyVal kv = kvList.GetAt(i);
|
||||
Keyval kv = kvList.Get_at(i);
|
||||
qry.Val_obj(kv.Key(), kv.Val());
|
||||
}
|
||||
qry.Exec_qry(conn);
|
||||
@@ -52,4 +52,16 @@ public class Db_qry_fxt {
|
||||
}
|
||||
}
|
||||
}
|
||||
public static String Db__print_tbl_as_str(Bry_bfr bfr, Db_conn conn, String tbl, String... cols) {
|
||||
int cols_len = cols.length;
|
||||
Db_rdr rdr = conn.Stmt_select(tbl, cols).Exec_select__rls_auto();
|
||||
while (rdr.Move_next()) {
|
||||
for (int i = 0; i < cols_len; ++i) {
|
||||
bfr.Add_obj(rdr.Read_at(i));
|
||||
bfr.Add_byte(i == cols_len - 1 ? Byte_ascii.Nl : Byte_ascii.Pipe);
|
||||
}
|
||||
}
|
||||
rdr.Rls();
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public interface Db_rdr {
|
||||
double Read_double(String k);
|
||||
DateAdp Read_date_by_str(String k);
|
||||
boolean Read_bool_by_byte(String k);
|
||||
int Fld_len();
|
||||
Object Read_obj(String k);
|
||||
Object Read_at(int i);
|
||||
void Rls();
|
||||
|
||||
@@ -32,6 +32,7 @@ class Db_rdr__empty implements Db_rdr {
|
||||
public float Read_float(String k) {return Float_.NaN;}
|
||||
public double Read_double(String k) {return Double_.NaN;}
|
||||
public boolean Read_bool_by_byte(String k) {return false;}
|
||||
public int Fld_len() {return 0;}
|
||||
public Object Read_obj(String k) {return null;}
|
||||
public Object Read_at(int i) {return null;}
|
||||
public void Rls() {}
|
||||
|
||||
@@ -37,8 +37,9 @@ public class Db_rdr__basic implements Db_rdr {
|
||||
@gplx.Virtual public double Read_double(String k) {try {return Double_.cast(rdr.getObject(k));} catch (Exception e) {throw Err_.new_exc(e, "db", "read failed", "key", k, "type", Double_.Cls_val_name);}}
|
||||
@gplx.Virtual public byte Read_byte(String k) {try {return Byte_.cast(rdr.getObject(k));} catch (Exception e) {throw Err_.new_exc(e, "db", "read failed", "key", k, "type", Byte_.Cls_val_name);}}
|
||||
@gplx.Virtual public boolean Read_bool_by_byte(String k) {try {return Byte_.cast(rdr.getObject(k)) == 1;} catch (Exception e) {throw Err_.new_exc(e, "db", "read failed", "key", k, "type", Bool_.Cls_val_name);}}
|
||||
@gplx.Virtual public int Fld_len() {try {return rdr.getMetaData().getColumnCount();} catch (Exception e) {throw Err_.new_exc(e, "db", "field count failed", "sql", sql);}}
|
||||
@gplx.Virtual public Object Read_obj(String k) {try {return rdr.getObject(k);} catch (Exception e) {throw Err_.new_exc(e, "db", "read failed", "key", k, "type", Object_.Cls_val_name);}}
|
||||
@gplx.Virtual public Object Read_at(int i) {try {return rdr.getObject(i);} catch (Exception e) {throw Err_.new_exc(e, "db", "read failed", "idx", i, "type", Object_.Cls_val_name);}}
|
||||
@gplx.Virtual public Object Read_at(int i) {try {return rdr.getObject(i + 1);} catch (Exception e) {throw Err_.new_exc(e, "db", "read failed", "idx", i, "type", Object_.Cls_val_name);}}
|
||||
@gplx.Virtual public void Rls() {
|
||||
try {rdr.close();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "close failed");}
|
||||
|
||||
@@ -55,4 +55,18 @@ public class Db_stmt_ {
|
||||
throw Err_.new_exc(e, "core", "db call failed", "tbl", tbl, "proc", proc);
|
||||
}
|
||||
public static Db_stmt Rls(Db_stmt v) {if (v != null) v.Rls(); return null;}
|
||||
public static void Val_by_obj(Db_stmt stmt, String key, Object val) {
|
||||
int tid = Type_adp_.To_tid_obj(val);
|
||||
switch (tid) {
|
||||
case Type_adp_.Tid__bool: stmt.Val_bool_as_byte (key, Bool_.cast(val)); break;
|
||||
case Type_adp_.Tid__byte: stmt.Val_byte (key, Byte_.cast(val)); break;
|
||||
case Type_adp_.Tid__int: stmt.Val_int (key, Int_.cast(val)); break;
|
||||
case Type_adp_.Tid__long: stmt.Val_long (key, Long_.cast(val)); break;
|
||||
case Type_adp_.Tid__float: stmt.Val_float (key, Float_.cast(val)); break;
|
||||
case Type_adp_.Tid__double: stmt.Val_double (key, Double_.cast(val)); break;
|
||||
case Type_adp_.Tid__str: stmt.Val_str (key, String_.cast(val)); break;
|
||||
case Type_adp_.Tid__bry: stmt.Val_bry (key, Bry_.cast(val)); break;
|
||||
default: throw Err_.new_unhandled_default(tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,4 +61,21 @@ public class Dbmeta_fld_itm {
|
||||
rv[i] = ary[i].name;
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static String Make_or_null(Db_conn conn, Dbmeta_fld_list flds, String tbl_name, int fld_type, Object fld_dflt, String fld_name) {
|
||||
boolean tbl_exists = conn.Meta_tbl_exists(tbl_name);
|
||||
boolean fld_exists = true;
|
||||
if (tbl_exists) {
|
||||
fld_exists = conn.Meta_fld_exists(tbl_name, fld_name);
|
||||
if (!fld_exists) return Dbmeta_fld_itm.Key_null;
|
||||
}
|
||||
Dbmeta_fld_itm fld = null;
|
||||
switch (fld_type) {
|
||||
case Dbmeta_fld_tid.Tid__int: fld = Dbmeta_fld_itm.new_int(fld_name); break;
|
||||
}
|
||||
if (fld_dflt != null) fld.Default_(fld_dflt);
|
||||
flds.Add(fld);
|
||||
return fld.name;
|
||||
}
|
||||
public static String To_double_str_by_int(int v) {return Int_.To_str(v) + ".0";}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,10 @@ public class Dbmeta_fld_list {
|
||||
private final Ordered_hash flds = Ordered_hash_.New();
|
||||
private final List_adp keys = List_adp_.new_();
|
||||
public void Clear() {flds.Clear(); keys.Clear(); str_ary = null; fld_ary = null;}
|
||||
public int Len() {return flds.Len();}
|
||||
public Dbmeta_fld_itm Get_by(String name) {return (Dbmeta_fld_itm)flds.Get_by(name);}
|
||||
public Dbmeta_fld_itm Get_at(int idx) {return (Dbmeta_fld_itm)flds.Get_at(idx);}
|
||||
public String[] To_str_ary() {if (str_ary == null) str_ary = (String[])keys.To_ary(String.class); return str_ary;} private String[] str_ary;
|
||||
public String[] To_str_ary() {if (str_ary == null) str_ary = (String[])keys.To_ary(String.class); return str_ary;} private String[] str_ary;
|
||||
public Dbmeta_fld_itm[] To_fld_ary() {if (fld_ary == null) fld_ary = (Dbmeta_fld_itm[])flds.To_ary(Dbmeta_fld_itm.class); return fld_ary;} private Dbmeta_fld_itm[] fld_ary;
|
||||
public String[] To_str_ary_wo_autonum() {
|
||||
int len = flds.Count();
|
||||
@@ -72,6 +73,7 @@ public class Dbmeta_fld_list {
|
||||
public String Add_str_dflt(String name, int len, String dflt)
|
||||
{return Add(Dbmeta_fld_itm.new_str(name, len).Default_(dflt));}
|
||||
public String Add(Dbmeta_fld_itm fld) {
|
||||
fld_ary = null; str_ary = null;
|
||||
String name = fld.Name();
|
||||
flds.Add(name, fld);
|
||||
keys.Add(name);
|
||||
|
||||
@@ -15,8 +15,8 @@ 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.engines.sqlite.*;
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.sqlite.*; // for Tid_sqlite; note that Tid_sqlite is not used, and only exists for doc purposes
|
||||
public class Dbmeta_fld_tid {
|
||||
public Dbmeta_fld_tid(int tid_ansi, int tid_sqlite, byte[] name, int len_1, int len_2) {
|
||||
this.tid_ansi = tid_ansi; this.tid_sqlite = tid_sqlite; this.name = name; this.len_1 = len_1; this.len_2 = len_2;
|
||||
@@ -72,8 +72,8 @@ class Gfdb_diff_bldr_fxt {
|
||||
}
|
||||
public void Clear() {
|
||||
ctx.Clear();
|
||||
Db_conn_utl.Tbl__delete(old_conn, "tbl");
|
||||
Db_conn_utl.Tbl__delete(new_conn, "tbl");
|
||||
old_conn.Meta_tbl_drop("tbl");
|
||||
new_conn.Meta_tbl_drop("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);}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class Gfdb_diff_wkr__db implements Gfdb_diff_wkr {
|
||||
this.uid = 0; this.prog_count = 0;
|
||||
|
||||
String dif_tbl = tbl.Name; Dbmeta_fld_itm[] dif_flds = Gfdb_diff_wkr__db_.New_dif_flds(tbl.Flds);
|
||||
if (!dif_conn.Meta_tbl_exists(dif_tbl)) dif_conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(dif_tbl, dif_flds));
|
||||
if (!dif_conn.Meta_tbl_exists(dif_tbl)) dif_conn.Meta_tbl_create(Dbmeta_tbl_itm.New(dif_tbl, dif_flds));
|
||||
this.stmt = dif_conn.Stmt_insert(dif_tbl, Gfdb_diff_wkr__db_.To_str_ary(dif_flds));
|
||||
dif_conn.Txn_bgn("dif_db_tbl_" + dif_tbl);
|
||||
cmd_create = true;
|
||||
|
||||
@@ -45,7 +45,7 @@ class Gfdb_diff_cmd__idx__create implements Gfo_srl_itm {
|
||||
rdr.Itm_end();
|
||||
}
|
||||
public void Exec(Db_conn conn) {
|
||||
conn.Ddl_create_idx(cur);
|
||||
conn.Meta_idx_create(cur);
|
||||
}
|
||||
}
|
||||
class Gfdb_diff_cmd__idx__fld implements Gfo_srl_itm {
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Gdif_cmd_tbl implements Rls_able {
|
||||
fld_grp_id = flds.Add_int("grp_id"); fld_cmd_id = flds.Add_int("cmd_id"); fld_tid = flds.Add_int("tid"); fld_data = flds.Add_text("data");
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_tbl(tbl_name, "main", fld_grp_id, fld_cmd_id)));}
|
||||
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_tbl(tbl_name, "main", fld_grp_id, fld_cmd_id)));}
|
||||
public Gdif_cmd_itm Insert(int grp_id, int cmd_id, int tid, String data) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear()
|
||||
|
||||
@@ -28,7 +28,7 @@ public class Gdif_job_tbl implements Rls_able {
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;}
|
||||
public String Fld_job_id() {return fld_job_id;}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds));}
|
||||
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));}
|
||||
public Gdif_job_itm Insert(int id, String name, String made_by, DateAdp made_on, String data) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear()
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Gdif_txn_tbl implements Rls_able {
|
||||
fld_job_id = flds.Add_int("job_id"); fld_txn_id = flds.Add_int("txn_id"); fld_cmd_id = flds.Add_int("cmd_id"); fld_owner_txn = flds.Add_int("owner_txn");
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_tbl(tbl_name, "main", fld_job_id, fld_txn_id)));}
|
||||
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_tbl(tbl_name, "main", fld_job_id, fld_txn_id)));}
|
||||
public Gdif_txn_itm Insert(int job_id, int txn_id, int cmd_id, int owner_txn) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear()
|
||||
|
||||
@@ -38,8 +38,10 @@ public interface Db_engine {
|
||||
void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Dbmeta_idx_itm... ary);
|
||||
void Ddl_append_fld(String tbl, Dbmeta_fld_itm fld);
|
||||
void Ddl_delete_tbl(String tbl);
|
||||
void Env_db_attach(String alias, Db_conn conn);
|
||||
void Env_db_attach(String alias, Io_url db_url);
|
||||
void Env_db_detach(String alias);
|
||||
void Meta_reload();
|
||||
boolean Meta_tbl_exists(String tbl);
|
||||
boolean Meta_fld_exists(String tbl, String fld);
|
||||
Dbmeta_tbl_mgr Meta_tbl_load_all();
|
||||
|
||||
@@ -46,7 +46,7 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
Statement cmd = New_stmt_exec(sql);
|
||||
return cmd.executeUpdate(sql);
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "db.engine:exec failed", "url", conn_info.Xto_api(), "sql", sql);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "db.engine:exec failed", "url", conn_info.Db_api(), "sql", sql);}
|
||||
}
|
||||
private DataRdr Exec_as_rdr(String sql) {
|
||||
try {
|
||||
@@ -55,16 +55,17 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
ResultSet rdr = cmd.getResultSet();
|
||||
return New_rdr(rdr, sql);
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "db.engine:rdr failed", "url", conn_info.Xto_api(), "sql", sql);}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "db.engine:rdr failed", "url", conn_info.Db_api(), "sql", sql);}
|
||||
}
|
||||
public void Ddl_create_tbl(Dbmeta_tbl_itm tbl) {Exec_as_int(tbl.To_sql_create(this.Sql_wtr()));}
|
||||
public void Ddl_create_tbl(Dbmeta_tbl_itm tbl) {Exec_as_int(tbl.To_sql_create(this.Sql_wtr())); this.Meta_reload();}
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Dbmeta_idx_itm... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Dbmeta_idx_itm idx = ary[i];
|
||||
usr_dlg.Plog_many("", "", "creating database index (please wait); db=~{0} idx=~{1}", conn_info.Database(), idx.Name());
|
||||
usr_dlg.Plog_many("", "", "creating db index (please wait); db=~{0} idx=~{1}", conn_info.Database(), idx.Name());
|
||||
Exec_as_int(idx.To_sql_create(Sql_wtr()));
|
||||
}
|
||||
this.Meta_reload();
|
||||
}
|
||||
public void Ddl_append_fld(String tbl, Dbmeta_fld_itm fld) {
|
||||
Gfo_usr_dlg_.Instance.Plog_many("", "", "adding column to table: db=~{0} tbl=~{1} fld=~{2}", conn_info.Database(), tbl, fld.Name());
|
||||
@@ -75,10 +76,13 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
catch (Exception e) { // catch error if column already added to table
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "column not added to table: db=~{0} tbl=~{1} fld=~{2} err=~{3}", conn_info.Database(), tbl, fld.Name(), Err_.Message_gplx_full(e));
|
||||
}
|
||||
this.Meta_reload();
|
||||
}
|
||||
public void Ddl_delete_tbl(String tbl) {Exec_as_int(this.Sql_wtr().Schema_wtr().Bld_drop_tbl(tbl));}
|
||||
public void Ddl_delete_tbl(String tbl) {Exec_as_int(this.Sql_wtr().Schema_wtr().Bld_drop_tbl(tbl)); this.Meta_reload();}
|
||||
@gplx.Virtual public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
@gplx.Virtual public void Env_db_attach(String alias, Db_conn db_url) {}
|
||||
@gplx.Virtual public void Env_db_detach(String alias) {}
|
||||
@gplx.Virtual public void Meta_reload() {}
|
||||
@gplx.Virtual public boolean Meta_tbl_exists(String tbl) {return false;}
|
||||
@gplx.Virtual public boolean Meta_fld_exists(String tbl, String fld) {return false;}
|
||||
public abstract Dbmeta_tbl_mgr Meta_tbl_load_all();
|
||||
@@ -94,7 +98,7 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
public void Conn_term() {
|
||||
if (connection == null) return; // connection never opened; just exit
|
||||
try {connection.close();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "Conn_term failed", "url", conn_info.Xto_raw());}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "Conn_term failed", "url", conn_info.Raw());}
|
||||
connection = null;
|
||||
}
|
||||
public Object New_stmt_prep_as_obj(String sql) {
|
||||
@@ -109,6 +113,6 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
}
|
||||
protected Connection Conn_make_by_url(String url, String uid, String pwd) {
|
||||
try {return DriverManager.getConnection(url, uid, pwd);}
|
||||
catch (SQLException e) {throw Err_.new_exc(e, "db", "connection open failed", "info", Conn_info().Xto_raw());}
|
||||
catch (SQLException e) {throw Err_.new_exc(e, "db", "connection open failed", "info", Conn_info().Raw());}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,11 @@ 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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_conn_info__mem extends Db_conn_info__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mem";
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Db_conn_info__mem rv = new Db_conn_info__mem();
|
||||
rv.Ctor("", m.ReadStr("database"), raw, raw);
|
||||
return rv;
|
||||
public class Mem_conn_info extends Db_conn_info__base {
|
||||
public Mem_conn_info(String raw, String db_api, String database) {super(raw, db_api, database);}
|
||||
@Override public String Key() {return Tid_const;} public static final String Tid_const = "mem";
|
||||
@Override public Db_conn_info New_self(String raw, Keyval_hash hash) {
|
||||
return new Mem_conn_info(raw, raw, hash.Get_val_as_str_or_fail("database"));
|
||||
}
|
||||
public static Db_conn_info new_(String database) {
|
||||
return Db_conn_info_.parse(Bld_raw
|
||||
@@ -29,5 +28,5 @@ public class Db_conn_info__mem extends Db_conn_info__base {
|
||||
, "database", database
|
||||
));
|
||||
}
|
||||
public static final Db_conn_info__mem Instance = new Db_conn_info__mem(); Db_conn_info__mem() {}
|
||||
public static final Mem_conn_info Instance = new Mem_conn_info("", "", "");
|
||||
}
|
||||
74
140_dbs/src/gplx/dbs/engines/mems/Mem_db_fxt.java
Normal file
74
140_dbs/src/gplx/dbs/engines/mems/Mem_db_fxt.java
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
class Mem_db_fxt {
|
||||
public Mem_db_fxt() {
|
||||
Io_mgr.Instance.InitEngine_mem();
|
||||
Db_conn_bldr.Instance.Reg_default_mem();
|
||||
}
|
||||
public Db_conn Make_conn(String url) {return Db_conn_bldr.Instance.Get_or_autocreate(Bool_.Y, Io_url_.mem_fil_(url));}
|
||||
public Dbmeta_tbl_itm Exec__create_tbl(Db_conn conn, String tbl, String... fld_names) {
|
||||
Dbmeta_fld_list flds = Dbmeta_fld_list.new_();
|
||||
int len = fld_names.length;
|
||||
for (int i = 0; i < len; ++i)
|
||||
flds.Add_str(fld_names[i], 255);
|
||||
Dbmeta_tbl_itm rv = Dbmeta_tbl_itm.New(tbl, flds);
|
||||
conn.Meta_tbl_create(rv);
|
||||
return rv;
|
||||
}
|
||||
public void Exec__insert(Db_conn conn, String tbl_name, String[]... rows) {
|
||||
Mem_engine engine = (Mem_engine)conn.Engine();
|
||||
int rows_len = rows.length;
|
||||
Mem_tbl tbl = engine.Tbls__get(tbl_name);
|
||||
Dbmeta_fld_list flds_list = tbl.Meta().Flds().To_fld_list();
|
||||
int flds_len = flds_list.Len();
|
||||
Db_stmt stmt = conn.Stmt_insert(tbl_name, flds_list);
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
stmt.Clear();
|
||||
String[] row = rows[i];
|
||||
for (int j = 0; j < flds_len; ++j)
|
||||
stmt.Val_str(flds_list.Get_at(j).Name(), row[j]);
|
||||
stmt.Exec_insert();
|
||||
}
|
||||
}
|
||||
public void Test__select(Db_conn conn, Db_qry qry, String[]... expd) {
|
||||
Db_stmt stmt = conn.Stmt_new(qry);
|
||||
Db_rdr rdr = new Mem_exec_select((Mem_engine)conn.Engine()).Select((Mem_stmt)stmt);
|
||||
List_adp actl_list = List_adp_.new_();
|
||||
Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
int expd_len = expd.length;
|
||||
String[] expd_rows = new String[expd_len];
|
||||
for (int i = 0; i < expd_len; ++i) {
|
||||
String[] expd_row = expd[i];
|
||||
for (int j = 0; j < expd_row.length; ++j) {
|
||||
if (j != 0) tmp_bfr.Add_byte_pipe();
|
||||
tmp_bfr.Add_str_u8(expd_row[j]);
|
||||
}
|
||||
expd_rows[i] = tmp_bfr.To_str_and_clear();
|
||||
}
|
||||
while (rdr.Move_next()) {
|
||||
int fld_len = rdr.Fld_len();
|
||||
for (int i = 0; i < fld_len; ++i) {
|
||||
if (i != 0) tmp_bfr.Add_byte_pipe();
|
||||
tmp_bfr.Add_obj_strict(rdr.Read_at(i));
|
||||
}
|
||||
actl_list.Add(tmp_bfr.To_str_and_clear());
|
||||
}
|
||||
Tfds.Eq_ary(expd_rows, (String[])actl_list.To_ary_and_clear(String.class));
|
||||
}
|
||||
}
|
||||
@@ -17,15 +17,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.stores.*; import gplx.dbs.metas.*; import gplx.dbs.sqls.*;
|
||||
public class Db_engine__mem implements Db_engine {
|
||||
public class Mem_engine implements Db_engine {
|
||||
private final Hash_adp tbl_hash = Hash_adp_.new_();
|
||||
Db_engine__mem(Db_conn_info conn_info) {this.conn_info = conn_info;}
|
||||
public String Tid() {return Db_conn_info__mem.Tid_const;}
|
||||
Mem_engine(Db_conn_info conn_info) {
|
||||
this.conn_info = conn_info;
|
||||
this.qry_runner = new Mem_exec_select(this);
|
||||
}
|
||||
public String Tid() {return Mem_conn_info.Tid_const;}
|
||||
public Db_conn_info Conn_info() {return conn_info;} private Db_conn_info conn_info;
|
||||
public Mem_exec_select Qry_runner() {return qry_runner;} private Mem_exec_select qry_runner;
|
||||
public Sql_qry_wtr Sql_wtr() {return sql_wtr;} private final Sql_qry_wtr sql_wtr = Sql_qry_wtr_.Basic;
|
||||
public Db_engine New_clone(Db_conn_info conn_info) {return new Db_engine__mem(conn_info);}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt__mem(this, qry);}
|
||||
public Mem_tbl Tbls_get(String name) {return (Mem_tbl)tbl_hash.Get_by(name);}
|
||||
public Db_engine New_clone(Db_conn_info conn_info) {return new Mem_engine(conn_info);}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Mem_stmt(this, qry);}
|
||||
public Mem_tbl Tbls__get(String name) {return (Mem_tbl)tbl_hash.Get_by(name);}
|
||||
public void Tbls__del(String name) {tbl_hash.Del(name);}
|
||||
public void Txn_bgn(String name) {++txn_count;} private int txn_count = 0;
|
||||
public String Txn_end() {--txn_count; return "";}
|
||||
public void Txn_cxl() {--txn_count;}
|
||||
@@ -46,14 +51,19 @@ public class Db_engine__mem implements Db_engine {
|
||||
}
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Dbmeta_idx_itm... ary) {} // TODO: implement unique index
|
||||
public void Ddl_append_fld(String tbl, Dbmeta_fld_itm fld) {}
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Ddl_delete_tbl(String tbl_key) {
|
||||
Mem_tbl tbl = (Mem_tbl)tbl_hash.Get_by(tbl_key);
|
||||
if (tbl != null) tbl.rows.Clear();
|
||||
}
|
||||
public void Env_db_attach(String alias, Db_conn conn) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
public void Meta_reload() {}
|
||||
public boolean Meta_tbl_exists(String tbl) {return tbl_hash.Has(tbl);}
|
||||
public boolean Meta_fld_exists(String tbl, String fld) {
|
||||
Mem_tbl mem_tbl = (Mem_tbl)tbl_hash.Get_by(tbl); if (mem_tbl == null) return false;
|
||||
return mem_tbl.Meta().Flds().Has(fld);
|
||||
}
|
||||
public Dbmeta_tbl_mgr Meta_tbl_load_all() {return meta_tbl_mgr;} private final Dbmeta_tbl_mgr meta_tbl_mgr = new Dbmeta_tbl_mgr();
|
||||
public static final Db_engine__mem Instance = new Db_engine__mem(); Db_engine__mem() {}
|
||||
public static final Mem_engine Instance = new Mem_engine(); Mem_engine() {}
|
||||
}
|
||||
211
140_dbs/src/gplx/dbs/engines/mems/Mem_exec_select.java
Normal file
211
140_dbs/src/gplx/dbs/engines/mems/Mem_exec_select.java
Normal file
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*;
|
||||
import gplx.dbs.qrys.*; import gplx.dbs.sqls.itms.*;
|
||||
public class Mem_exec_select {
|
||||
private final Mem_engine engine;
|
||||
private final List_adp tmp_where_rows = List_adp_.new_();
|
||||
public Mem_exec_select(Mem_engine engine) {this.engine = engine;}
|
||||
public Db_rdr Select(Mem_stmt stmt) {
|
||||
Db_qry stmt_qry = stmt.Qry();
|
||||
Mem_tbl tbl = engine.Tbls__get(stmt_qry.Base_table());
|
||||
String[] select = null; Criteria where = null;
|
||||
Db_qry__select_in_tbl qry = Db_qry__select_in_tbl.as_(stmt_qry);
|
||||
Db_qry__select_cmd qry2 = null;
|
||||
if (qry == null) {
|
||||
qry2 = (Db_qry__select_cmd)stmt_qry;
|
||||
select = Mem_exec_.Flds__to_str_ary(qry2.Cols().Flds);
|
||||
where = qry2.Where_itm().Root;
|
||||
}
|
||||
else {
|
||||
select = qry.Select_flds();
|
||||
where = qry.Where();
|
||||
}
|
||||
Mem_stmt_args_.Fill(stmt.Stmt_args(), where);
|
||||
Mem_row[] tbl_rows = (Mem_row[])tbl.rows.To_ary(Mem_row.class);
|
||||
if (qry2 != null) {
|
||||
tbl_rows = Mem_exec_.Rows__alias(tbl_rows, qry2.From().Base_tbl.Alias);
|
||||
List_adp join_tbls = qry2.From().Tbls;
|
||||
int join_tbls_len = join_tbls.Len();
|
||||
for (int i = 1; i < join_tbls_len; ++i) {
|
||||
Sql_tbl_itm join_tbl = (Sql_tbl_itm)join_tbls.Get_at(i);
|
||||
Mem_row[] join_rows = (Mem_row[])engine.Tbls__get(join_tbl.Name).rows.To_ary(Mem_row.class);
|
||||
join_rows = Mem_exec_.Rows__alias(join_rows, join_tbl.Alias);
|
||||
tbl_rows = Mem_exec_.Rows__join(join_tbl.Join_tid, tbl_rows, join_rows, join_tbl.Alias, join_tbl.Join_flds);
|
||||
}
|
||||
}
|
||||
Mem_exec_.Where__filter(tmp_where_rows, tbl_rows, stmt, where);
|
||||
Mem_row[] rslt_rows = (Mem_row[])tmp_where_rows.To_ary_and_clear(Mem_row.class);
|
||||
if (qry2 != null) {
|
||||
if (qry2.Order() != null && qry2.Order().Flds().length > 0)
|
||||
Array_.Sort(rslt_rows, new Mem_sorter(qry2.Order().Flds()));
|
||||
int offset = qry2.Offset();
|
||||
if (offset != Db_qry__select_cmd.Offset__disabled) {
|
||||
Mem_row[] trg_rows = new Mem_row[rslt_rows.length - offset];
|
||||
Array_.Copy_to(rslt_rows, offset, trg_rows, 0, trg_rows.length);
|
||||
rslt_rows = trg_rows;
|
||||
}
|
||||
int limit = qry2.Limit();
|
||||
if (limit != Db_qry__select_cmd.Limit__disabled) {
|
||||
Mem_row[] trg_rows = new Mem_row[limit];
|
||||
Array_.Copy_to(rslt_rows, 0, trg_rows, 0, limit);
|
||||
rslt_rows = trg_rows;
|
||||
}
|
||||
rslt_rows = Mem_exec_.Rows__select_flds(rslt_rows, qry2.Cols());
|
||||
}
|
||||
return new Mem_rdr(select, rslt_rows);
|
||||
}
|
||||
}
|
||||
class Mem_sorter implements gplx.core.lists.ComparerAble {
|
||||
private final Sql_order_fld[] flds;
|
||||
public Mem_sorter(Sql_order_fld[] flds) {
|
||||
this.flds = flds;
|
||||
}
|
||||
public int compare(Object lhsObj, Object rhsObj) {
|
||||
Mem_row lhs = (Mem_row)lhsObj;
|
||||
Mem_row rhs = (Mem_row)rhsObj;
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
Sql_order_fld fld = flds[i];
|
||||
Object lhs_val = lhs.Get_by(fld.Name);
|
||||
Object rhs_val = rhs.Get_by(fld.Name);
|
||||
int comp = CompareAble_.Compare_obj(lhs_val, rhs_val);
|
||||
if (comp != CompareAble_.Same) return comp * (fld.Sort == Sql_order_fld.Sort__dsc ? -1 : 1);
|
||||
}
|
||||
return CompareAble_.Same;
|
||||
}
|
||||
}
|
||||
class Mem_exec_ {
|
||||
public static Mem_row[] Rows__join(int join_tid, Mem_row[] lhs_rows, Mem_row[] rhs_rows, String tbl_alias, Sql_join_fld[] join_flds) {
|
||||
int join_flds_len = join_flds.length;
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
Hash_adp_bry rhs_hash = Hash_adp_bry.cs();
|
||||
int rhs_rows_len = rhs_rows.length;
|
||||
for (int i = 0; i < rhs_rows_len; ++i) {
|
||||
Mem_row rhs_row = rhs_rows[i];
|
||||
byte[] rhs_key = Rows__bld_key(bfr, Bool_.N, tbl_alias, rhs_row, join_flds, join_flds_len);
|
||||
List_adp rhs_list = (List_adp)rhs_hash.Get_by_bry(rhs_key);
|
||||
if (rhs_list == null) {
|
||||
rhs_list = List_adp_.new_();
|
||||
rhs_hash.Add(rhs_key, rhs_list);
|
||||
}
|
||||
rhs_list.Add(rhs_row);
|
||||
}
|
||||
List_adp rv = List_adp_.new_();
|
||||
int lhs_len = lhs_rows.length;
|
||||
for (int i = 0; i < lhs_len; ++i) {
|
||||
Mem_row lhs_row = lhs_rows[i];
|
||||
byte[] lhs_key = Rows__bld_key(bfr, Bool_.Y, tbl_alias, lhs_row, join_flds, join_flds_len);
|
||||
List_adp rhs_list = (List_adp)rhs_hash.Get_by_bry(lhs_key);
|
||||
if (rhs_list == null) {
|
||||
switch (join_tid) {
|
||||
case Sql_tbl_itm.Tid__inner: continue;
|
||||
case Sql_tbl_itm.Tid__left: break;
|
||||
default: throw Err_.new_unhandled_default(join_tid);
|
||||
}
|
||||
}
|
||||
int rhs_list_len = rhs_list == null ? 1 : rhs_list.Len();
|
||||
for (int j = 0; j < rhs_list_len; ++j) {
|
||||
Mem_row rhs_row = rhs_list == null ? Mem_row.Null_row : (Mem_row)rhs_list.Get_at(j);
|
||||
Mem_row merged = Rows__merge(lhs_row, rhs_row);
|
||||
rv.Add(merged);
|
||||
}
|
||||
}
|
||||
return (Mem_row[])rv.To_ary_and_clear(Mem_row.class);
|
||||
}
|
||||
public static Mem_row[] Rows__alias(Mem_row[] src_rows, String tbl_alias) {
|
||||
int src_rows_len = src_rows.length;
|
||||
Mem_row[] rv = new Mem_row[src_rows_len];
|
||||
for (int i = 0; i < src_rows_len; ++i) {
|
||||
Mem_row src_row = src_rows[i];
|
||||
Mem_row trg_row = new Mem_row(); rv[i] = trg_row;
|
||||
int src_flds_len = src_row.Len();
|
||||
for (int j = 0; j < src_flds_len; ++j) {
|
||||
String fld = src_row.Fld_at(j);
|
||||
Object val = src_row.Get_at(j);
|
||||
trg_row.Add(Sql_select_fld.Bld_tbl_w_fld(tbl_alias, fld), val);
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private static byte[] Rows__bld_key(Bry_bfr bfr, boolean join_is_src, String trg_tbl, Mem_row row, Sql_join_fld[] join_flds, int join_flds_len) {
|
||||
for (int i = 0; i < join_flds_len; ++i) {
|
||||
if (i != 0) bfr.Add_byte_pipe();
|
||||
Sql_join_fld join_fld = join_flds[i];
|
||||
Object val = row.Get_by(join_fld.To_fld_sql(join_is_src, trg_tbl));
|
||||
bfr.Add_obj(val);
|
||||
}
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
private static Mem_row Rows__merge(Mem_row lhs, Mem_row rhs) {
|
||||
Mem_row rv = new Mem_row();
|
||||
Rows__copy_to(lhs, rv);
|
||||
Rows__copy_to(rhs, rv);
|
||||
return rv;
|
||||
}
|
||||
private static void Rows__copy_to(Mem_row src, Mem_row trg) {
|
||||
int src_len = src.Len();
|
||||
for (int i = 0; i < src_len; ++i) {
|
||||
String fld = src.Fld_at(i);
|
||||
Object val = src.Get_by_or_dbnull(fld);
|
||||
trg.Add(fld, val);
|
||||
}
|
||||
}
|
||||
public static Mem_row[] Rows__select_flds(Mem_row[] src_rows, Sql_select_clause cols) {
|
||||
Sql_select_fld_list select_flds = cols.Flds; int select_flds_len = select_flds.Len();
|
||||
int src_rows_len = src_rows.length;
|
||||
Mem_row[] rv = new Mem_row[src_rows_len];
|
||||
for (int i = 0; i < src_rows_len; ++i) { // loop over each row
|
||||
Mem_row src_row = src_rows[i];
|
||||
Mem_row trg_row = new Mem_row(); rv[i] = trg_row;
|
||||
for (int j = 0; j < select_flds_len; ++j) { // loop over each fld
|
||||
Sql_select_fld fld = select_flds.Get_at(j);
|
||||
if (String_.Eq(fld.Alias, Sql_select_fld.Fld__wildcard)) { // wildcard; add all cols
|
||||
for (int k = 0; k < src_row.Len(); ++k) {
|
||||
String key = src_row.Fld_at(k);
|
||||
Object val = src_row.Get_by_or_dbnull(key);
|
||||
trg_row.Add(key, val);
|
||||
}
|
||||
}
|
||||
else { // regular field; add it only
|
||||
String key = fld.To_fld_key();
|
||||
Object val = src_row.Get_by_or_dbnull(key);
|
||||
trg_row.Add(fld.To_fld_alias(), val);
|
||||
}
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static String[] Flds__to_str_ary(Sql_select_fld_list flds) {
|
||||
int len = flds.Len();
|
||||
String[] rv = new String[len];
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv[i] = flds.Get_at(i).Fld;
|
||||
return rv;
|
||||
}
|
||||
public static void Where__filter(List_adp rv, Mem_row[] rows, Mem_stmt stmt, Criteria crt) {
|
||||
rv.Clear();
|
||||
int rows_len = rows.length;
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
Mem_row itm = rows[i];
|
||||
if (crt.Matches(itm))
|
||||
rv.Add(itm);
|
||||
}
|
||||
}
|
||||
}
|
||||
130
140_dbs/src/gplx/dbs/engines/mems/Mem_exec_select_tst.java
Normal file
130
140_dbs/src/gplx/dbs/engines/mems/Mem_exec_select_tst.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.itms.*;
|
||||
public class Mem_exec_select_tst {
|
||||
private final Mem_db_fxt__single fxt = new Mem_db_fxt__single();
|
||||
@Test public void Basic() {
|
||||
fxt.Exec__create_tbl("tbl_1", "fld_1");
|
||||
fxt.Exec__insert("tbl_1"
|
||||
, String_.Ary("a_1")
|
||||
, String_.Ary("a_2")
|
||||
, String_.Ary("a_0")
|
||||
);
|
||||
|
||||
// select all
|
||||
fxt.Test__select(Db_qry_.select_().From_("tbl_1").Cols_all_()
|
||||
, String_.Ary("a_1")
|
||||
, String_.Ary("a_2")
|
||||
, String_.Ary("a_0")
|
||||
);
|
||||
|
||||
// order by
|
||||
fxt.Test__select(Db_qry_.select_().From_("tbl_1").Cols_all_().Order_("fld_1", Bool_.N)
|
||||
, String_.Ary("a_2")
|
||||
, String_.Ary("a_1")
|
||||
, String_.Ary("a_0")
|
||||
);
|
||||
|
||||
// offset
|
||||
fxt.Test__select(Db_qry_.select_().From_("tbl_1").Cols_all_().Offset_(1)
|
||||
, String_.Ary("a_2")
|
||||
, String_.Ary("a_0")
|
||||
);
|
||||
|
||||
// limit
|
||||
fxt.Test__select(Db_qry_.select_().From_("tbl_1").Cols_all_().Limit_(2)
|
||||
, String_.Ary("a_1")
|
||||
, String_.Ary("a_2")
|
||||
);
|
||||
}
|
||||
@Test public void Join__single() {
|
||||
fxt.Exec__create_tbl("tbl_1", "fld_a", "fld_1a");
|
||||
fxt.Exec__create_tbl("tbl_2", "fld_a", "fld_2a");
|
||||
fxt.Exec__insert("tbl_1"
|
||||
, String_.Ary("a_0", "1a_0")
|
||||
, String_.Ary("a_1", "1a_1")
|
||||
, String_.Ary("a_2", "1a_2")
|
||||
);
|
||||
fxt.Exec__insert("tbl_2"
|
||||
, String_.Ary("a_0", "2a_0")
|
||||
, String_.Ary("a_2", "2a_2")
|
||||
);
|
||||
|
||||
// inner join
|
||||
fxt.Test__select(Db_qry_.select_()
|
||||
.Cols_w_tbl_("t1", "fld_a", "fld_1a").Cols_w_tbl_("t2", "fld_2a")
|
||||
.From_("tbl_1", "t1")
|
||||
. Join_("tbl_2", "t2", Db_qry_.New_join__join("fld_a", "t1", "fld_a"))
|
||||
, String_.Ary("a_0", "1a_0", "2a_0")
|
||||
, String_.Ary("a_2", "1a_2", "2a_2")
|
||||
);
|
||||
|
||||
// left join
|
||||
fxt.Test__select(Db_qry_.select_()
|
||||
.Cols_w_tbl_("t1", "fld_a", "fld_1a").Cols_w_tbl_("t2", "fld_2a")
|
||||
.From_("tbl_1", "t1")
|
||||
. Join_(Sql_tbl_itm.Tid__left, Sql_tbl_itm.Db__null, "tbl_2", "t2", Db_qry_.New_join__join("fld_a", "t1", "fld_a"))
|
||||
, String_.Ary("a_0", "1a_0", "2a_0")
|
||||
, String_.Ary("a_1", "1a_1", Db_null.Null_str)
|
||||
, String_.Ary("a_2", "1a_2", "2a_2")
|
||||
);
|
||||
}
|
||||
@Test public void Join__many() {
|
||||
fxt.Exec__create_tbl("tbl_1", "fld_a", "fld_1a");
|
||||
fxt.Exec__create_tbl("tbl_2", "fld_a", "fld_2a");
|
||||
fxt.Exec__insert("tbl_1"
|
||||
, String_.Ary("a_0", "1a_0")
|
||||
, String_.Ary("a_1", "1a_1")
|
||||
);
|
||||
fxt.Exec__insert("tbl_2"
|
||||
, String_.Ary("a_0", "2a_0")
|
||||
, String_.Ary("a_0", "2a_1")
|
||||
);
|
||||
|
||||
// inner join
|
||||
fxt.Test__select(Db_qry_.select_()
|
||||
.Cols_w_tbl_("t1", "fld_a", "fld_1a").Cols_w_tbl_("t2", "fld_2a")
|
||||
.From_("tbl_1", "t1")
|
||||
. Join_("tbl_2", "t2", Db_qry_.New_join__join("fld_a", "t1", "fld_a"))
|
||||
, String_.Ary("a_0", "1a_0", "2a_0")
|
||||
, String_.Ary("a_0", "1a_0", "2a_1")
|
||||
);
|
||||
|
||||
// left join
|
||||
fxt.Test__select(Db_qry_.select_()
|
||||
.Cols_w_tbl_("t1", "fld_a", "fld_1a").Cols_w_tbl_("t2", "fld_2a")
|
||||
.From_("tbl_1", "t1")
|
||||
. Join_(Sql_tbl_itm.Tid__left, Sql_tbl_itm.Db__null, "tbl_2", "t2", Db_qry_.New_join__join("fld_a", "t1", "fld_a"))
|
||||
, String_.Ary("a_0", "1a_0", "2a_0")
|
||||
, String_.Ary("a_0", "1a_0", "2a_1")
|
||||
, String_.Ary("a_1", "1a_1", Db_null.Null_str)
|
||||
);
|
||||
}
|
||||
}
|
||||
class Mem_db_fxt__single {
|
||||
private final Mem_db_fxt mem_fxt;
|
||||
private final Db_conn conn;
|
||||
public Mem_db_fxt__single() {
|
||||
this.mem_fxt = new Mem_db_fxt();
|
||||
this.conn = mem_fxt.Make_conn("mem/test.db");
|
||||
}
|
||||
public void Exec__create_tbl (String tbl, String... fld_names) {mem_fxt.Exec__create_tbl(conn, tbl, fld_names);}
|
||||
public void Exec__insert (String tbl, String[]... rows) {mem_fxt.Exec__insert(conn, tbl, rows);}
|
||||
public void Test__select (Db_qry qry, String[]... expd) {mem_fxt.Test__select(conn, qry, expd);}
|
||||
}
|
||||
@@ -16,10 +16,10 @@ 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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_rdr__mem implements Db_rdr {
|
||||
public class Mem_rdr implements Db_rdr {
|
||||
private final Mem_row[] rows; private int row_idx = -1; private final int rows_len;
|
||||
private Mem_row row;
|
||||
public Db_rdr__mem(String[] cols, Mem_row[] rows) {
|
||||
public Mem_rdr(String[] cols, Mem_row[] rows) {
|
||||
this.rows = rows; this.rows_len = rows.length;
|
||||
}
|
||||
public boolean Move_next() {
|
||||
@@ -39,6 +39,7 @@ public class Db_rdr__mem implements Db_rdr {
|
||||
public float Read_float(String k) {return Float_.cast(row.Get_by(k));}
|
||||
public double Read_double(String k) {return Double_.cast(row.Get_by(k));}
|
||||
public boolean Read_bool_by_byte(String k) {return Byte_.cast(row.Get_by(k)) == 1;}
|
||||
public int Fld_len() {return row.Len();}
|
||||
public Object Read_obj(String k) {return row.Get_by(k);}
|
||||
public Object Read_at(int i) {return row.Get_at(i);}
|
||||
public void Rls() {}
|
||||
@@ -18,13 +18,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Mem_row implements GfoInvkAble {
|
||||
private final Ordered_hash hash = Ordered_hash_.New();
|
||||
public Object Get_by(String key) {return hash.Get_by(key);}
|
||||
private final Ordered_hash flds = Ordered_hash_.New();
|
||||
public int Len() {return hash.Len();}
|
||||
public String Fld_at(int i) {return (String)flds.Get_at(i);}
|
||||
public Object Get_at(int i) {return hash.Get_at(i);}
|
||||
public void Set_by(String key, Object val) {hash.Add_if_dupe_use_nth(key, val);}
|
||||
public void Add(String key, Object val) {hash.Add(key, val);}
|
||||
public Object Get_by(String key) {return hash.Get_by(key);}
|
||||
public Object Get_by_or_dbnull(String key) {
|
||||
Object rv = hash.Get_by(key);
|
||||
return rv == null ? Db_null.Instance : rv;
|
||||
}
|
||||
public void Set_by(String key, Object val) {hash.Add_if_dupe_use_nth(key, val); flds.Add_if_dupe_use_1st(key, key);}
|
||||
public void Add(String key, Object val) {hash.Add(key, val); flds.Add_if_dupe_use_1st(key, key);}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
Object rv = Get_by(k);
|
||||
if (rv == null) return GfoInvkAble_.Rv_unhandled;
|
||||
return rv;
|
||||
}
|
||||
public static final Mem_row[] Ary_empty = new Mem_row[0];
|
||||
public static final Mem_row Null_row = new Mem_row();
|
||||
}
|
||||
|
||||
@@ -17,12 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.stores.*;
|
||||
public class Db_stmt__mem implements Db_stmt {
|
||||
public class Mem_stmt implements Db_stmt {
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private final Ordered_hash val_list = Ordered_hash_.New();
|
||||
public Db_stmt__mem(Db_engine__mem engine, Db_qry qry) {Ctor_stmt(engine, qry);} private Db_engine__mem engine;
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {this.engine = (Db_engine__mem)engine; this.qry = qry;}
|
||||
public Hash_adp Crts() {return crt_hash;} private final Hash_adp crt_hash = Hash_adp_.new_();
|
||||
public Mem_stmt(Mem_engine engine, Db_qry qry) {Ctor_stmt(engine, qry);} private Mem_engine engine;
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {this.engine = (Mem_engine)engine; this.qry = qry;}
|
||||
public Mem_stmt_args Stmt_args() {return stmt_args;} private final Mem_stmt_args stmt_args = new Mem_stmt_args();
|
||||
public int Args_len() {return val_list.Count();}
|
||||
public Object Args_get_at(int i) {return val_list.Get_at(i);}
|
||||
public Object Args_get_by(String k) {return val_list.Get_by(k);}
|
||||
@@ -30,7 +30,7 @@ public class Db_stmt__mem implements Db_stmt {
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Clear() {
|
||||
val_list.Clear();
|
||||
crt_hash.Clear();
|
||||
stmt_args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {this.Clear();}
|
||||
@@ -120,35 +120,29 @@ public class Db_stmt__mem implements Db_stmt {
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
Mem_tbl tbl = engine.Tbls__get(qry.Base_table());
|
||||
if (tbl == null) throw Err_.new_wo_type("must call Create_tbl", "tbl", qry.Base_table());
|
||||
return tbl.Insert(this) == 1;
|
||||
}
|
||||
public int Exec_update() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
Mem_tbl tbl = engine.Tbls__get(qry.Base_table());
|
||||
return tbl.Update(this);
|
||||
}
|
||||
public int Exec_delete() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
Mem_tbl tbl = engine.Tbls__get(qry.Base_table());
|
||||
return tbl.Delete(this);
|
||||
}
|
||||
public DataRdr Exec_select() {throw Err_.new_unimplemented();}
|
||||
public Db_rdr Exec_select__rls_auto() {return this.Exec_select__rls_manual();}
|
||||
public Db_rdr Exec_select__rls_manual() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Select(this);
|
||||
// Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
// return tbl.Select(this);
|
||||
return engine.Qry_runner().Select(this);
|
||||
}
|
||||
public Object Exec_select_val() {throw Err_.new_unimplemented();}
|
||||
private void Add(String k, boolean where, Object v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return; // key is explicitly null; ignore; allows schema_2+ type definitions
|
||||
if (k == Dbmeta_fld_itm.Key_null) return; // key is explicitly null; ignore; allows schema_2+ type definitions
|
||||
val_list.Add_if_dupe_use_1st(k, v); // NOTE: only add if new; WHERE with IN will call Add many times; fld_ttl IN ('A.png', 'B.png');
|
||||
if (where) {
|
||||
List_adp list = (List_adp)crt_hash.Get_by(k);
|
||||
if (list == null) {
|
||||
list = List_adp_.new_();
|
||||
crt_hash.Add(k, list);
|
||||
}
|
||||
list.Add(v);
|
||||
}
|
||||
if (where) stmt_args.Add(k, v);
|
||||
}
|
||||
}
|
||||
77
140_dbs/src/gplx/dbs/engines/mems/Mem_stmt_args.java
Normal file
77
140_dbs/src/gplx/dbs/engines/mems/Mem_stmt_args.java
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Mem_stmt_args {
|
||||
private final List_adp list = List_adp_.new_();
|
||||
private int cur_idx = -1;
|
||||
public void Clear() {list.Clear(); cur_idx = -1;}
|
||||
public void Add(String k, Object v) {list.Add(Keyval_.new_(k, v));}
|
||||
public Keyval Get_next() {
|
||||
int idx = ++cur_idx;
|
||||
return idx == list.Count() ? null: (Keyval)list.Get_at(idx);
|
||||
}
|
||||
}
|
||||
class Mem_stmt_args_ {
|
||||
public static void Fill(Mem_stmt_args args, Criteria crt) {
|
||||
int tid = crt.Tid();
|
||||
if (tid == Criteria_.Tid_wrapper) {
|
||||
Criteria_fld fld = (Criteria_fld)crt;
|
||||
Criteria sub = fld.Crt();
|
||||
String fld_key = fld.Key();
|
||||
switch (sub.Tid()) {
|
||||
case Criteria_.Tid_eq:
|
||||
case Criteria_.Tid_comp:
|
||||
case Criteria_.Tid_like:
|
||||
case Criteria_.Tid_iomatch:
|
||||
Keyval kvp = args.Get_next();
|
||||
if (!String_.Eq(kvp.Key(), fld.Key())) throw Err_.new_("db", "fld_crt.key mismatch", "fld.key", fld_key, "kvp.key", kvp.Key());
|
||||
sub.Val_as_obj_(kvp.Val());
|
||||
break;
|
||||
case Criteria_.Tid_in:
|
||||
Criteria_in crt_in = (Criteria_in)sub;
|
||||
Object[] ary = crt_in.Ary(); int ary_len = crt_in.Ary_len();
|
||||
for (int i = 0; i < ary_len; ++i)
|
||||
ary[i] = args.Get_next().Val();
|
||||
break;
|
||||
case Criteria_.Tid_between:
|
||||
Criteria_between crt_between = (Criteria_between)sub;
|
||||
crt_between.Lo_((Comparable)(args.Get_next()).Val());
|
||||
crt_between.Hi_((Comparable)(args.Get_next()).Val());
|
||||
break;
|
||||
default: throw Err_.new_unhandled(sub.Tid());
|
||||
}
|
||||
}
|
||||
else {
|
||||
switch (tid) {
|
||||
case Criteria_.Tid_const: break; // true / false; nothing to fill
|
||||
case Criteria_.Tid_and:
|
||||
case Criteria_.Tid_or:
|
||||
Criteria_bool_base crt_dual = (Criteria_bool_base)crt;
|
||||
Fill(args, crt_dual.Lhs());
|
||||
Fill(args, crt_dual.Rhs());
|
||||
break;
|
||||
case Criteria_.Tid_not:
|
||||
Criteria_not crt_not = (Criteria_not)crt;
|
||||
Fill(args, crt_not.Crt());
|
||||
break;
|
||||
default: throw Err_.new_unhandled(tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,11 +19,12 @@ package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs
|
||||
import gplx.core.primitives.*; import gplx.core.criterias.*; import gplx.dbs.qrys.*; import gplx.dbs.sqls.itms.*;
|
||||
import gplx.dbs.metas.*;
|
||||
public class Mem_tbl {
|
||||
private final List_adp rows = List_adp_.new_(); private final List_adp where_rows = List_adp_.new_();
|
||||
private final List_adp where_rows = List_adp_.new_();
|
||||
private final Hash_adp autonum_hash = Hash_adp_.new_();
|
||||
public Mem_tbl(Dbmeta_tbl_itm meta) {this.meta = meta;}
|
||||
public Dbmeta_tbl_itm Meta() {return meta;} private final Dbmeta_tbl_itm meta;
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
public final List_adp rows = List_adp_.new_();
|
||||
public int Insert(Mem_stmt stmt) {
|
||||
Mem_row itm = new Mem_row();
|
||||
Dbmeta_fld_mgr flds = meta.Flds();
|
||||
int len = flds.Len();
|
||||
@@ -31,6 +32,7 @@ public class Mem_tbl {
|
||||
Dbmeta_fld_itm fld = flds.Get_at(i);
|
||||
String fld_name = fld.Name();
|
||||
Object val = fld.Autonum() ? Autonum_calc(fld_name) : stmt.Args_get_by(fld_name);
|
||||
if (val == null) continue; // NOTE: allow Bulk_insert from test to skip filds
|
||||
itm.Set_by(fld_name, val);
|
||||
}
|
||||
rows.Add(itm);
|
||||
@@ -44,10 +46,11 @@ public class Mem_tbl {
|
||||
}
|
||||
return autonum_itm.Val_add();
|
||||
}
|
||||
public int Update(Db_stmt__mem stmt) {
|
||||
public int Update(Mem_stmt stmt) {
|
||||
Db_qry_update qry = (Db_qry_update)stmt.Qry();
|
||||
qry.Where().Val_from_args(stmt.Crts());
|
||||
Select_rows_where(where_rows, stmt, qry.Where());
|
||||
Criteria where_crt = qry.Where(); if (where_crt == null) where_crt = Criteria_.All;
|
||||
Mem_stmt_args_.Fill(stmt.Stmt_args(), where_crt);
|
||||
Select_rows_where(where_rows, stmt, where_crt);
|
||||
int where_rows_len = where_rows.Count();
|
||||
String[] update_cols = qry.Cols_for_update(); int update_cols_len = update_cols.length;
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
@@ -57,9 +60,9 @@ public class Mem_tbl {
|
||||
}
|
||||
return where_rows_len;
|
||||
}
|
||||
public int Delete(Db_stmt__mem stmt) {
|
||||
public int Delete(Mem_stmt stmt) {
|
||||
Db_qry_delete qry = (Db_qry_delete)stmt.Qry();
|
||||
qry.Where().Val_from_args(stmt.Crts());
|
||||
Mem_stmt_args_.Fill(stmt.Stmt_args(), qry.Where());
|
||||
Select_rows_where(where_rows, stmt, qry.Where());
|
||||
int where_rows_len = where_rows.Count();
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
@@ -68,7 +71,7 @@ public class Mem_tbl {
|
||||
}
|
||||
return where_rows_len;
|
||||
}
|
||||
public Db_rdr Select(Db_stmt__mem stmt) {
|
||||
public Db_rdr Select(Mem_stmt stmt) {
|
||||
String[] select = null; Criteria where = null;
|
||||
Db_qry__select_in_tbl qry = Db_qry__select_in_tbl.as_(stmt.Qry());
|
||||
if (qry == null) {
|
||||
@@ -80,9 +83,9 @@ public class Mem_tbl {
|
||||
select = qry.Select_flds();
|
||||
where = qry.Where();
|
||||
}
|
||||
where.Val_from_args(stmt.Crts());
|
||||
Mem_stmt_args_.Fill(stmt.Stmt_args(), where);
|
||||
Select_rows_where(where_rows, stmt, where);
|
||||
return new Db_rdr__mem(select, (Mem_row[])where_rows.To_ary_and_clear(Mem_row.class));
|
||||
return new Mem_rdr(select, (Mem_row[])where_rows.To_ary_and_clear(Mem_row.class));
|
||||
}
|
||||
private String[] To_str_ary(Sql_select_fld_list flds) {
|
||||
int len = flds.Len();
|
||||
@@ -91,7 +94,7 @@ public class Mem_tbl {
|
||||
rv[i] = flds.Get_at(i).Fld;
|
||||
return rv;
|
||||
}
|
||||
private void Select_rows_where(List_adp rv, Db_stmt__mem stmt, Criteria crt) {
|
||||
private void Select_rows_where(List_adp rv, Mem_stmt stmt, Criteria crt) {
|
||||
rv.Clear();
|
||||
int rows_len = rows.Count();
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
|
||||
@@ -17,9 +17,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mysql; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Mysql_conn_info extends Db_conn_info__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mysql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public Mysql_conn_info(String raw, String db_api, String database, String server, String uid, String pwd) {super(raw, db_api, database);
|
||||
this.server = server;
|
||||
this.uid = uid;
|
||||
this.pwd = pwd;
|
||||
}
|
||||
@Override public String Key() {return Tid_const;} public static final String Tid_const = "mysql";
|
||||
public String Server() {return server;} private final String server;
|
||||
public String Uid() {return uid;} private final String uid;
|
||||
public String Pwd() {return pwd;} private final String pwd;
|
||||
public static Db_conn_info new_(String server, String database, String uid, String pwd) {
|
||||
return Db_conn_info_.parse(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
@@ -30,12 +36,10 @@ public class Mysql_conn_info extends Db_conn_info__base {
|
||||
, "charset", "utf8"
|
||||
));
|
||||
}
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Mysql_conn_info rv = new Mysql_conn_info();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("charset", "utf8")));
|
||||
rv.uid = m.ReadStr("uid");
|
||||
rv.pwd = m.ReadStr("pwd");
|
||||
return rv;
|
||||
@Override public Db_conn_info New_self(String raw, Keyval_hash hash) {
|
||||
return new Mysql_conn_info
|
||||
( raw, Bld_api(hash, Keyval_.new_("charset", "utf8")), hash.Get_val_as_str_or_fail("database")
|
||||
, hash.Get_val_as_str_or_fail("server"), hash.Get_val_as_str_or_fail("uid"), hash.Get_val_as_str_or_fail("pwd"));
|
||||
}
|
||||
public static final Mysql_conn_info Instance = new Mysql_conn_info(); Mysql_conn_info() {}
|
||||
public static final Mysql_conn_info Instance = new Mysql_conn_info("", "", "", "", "", "");
|
||||
}
|
||||
|
||||
@@ -30,7 +30,8 @@ public class Mysql_engine extends Db_engine_sql_base {
|
||||
@Override public Dbmeta_tbl_mgr Meta_tbl_load_all() {throw Err_.new_unimplemented();}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Mysql_conn_info conn_info_as_mysql = (Mysql_conn_info)conn_info;
|
||||
return Conn_make_by_url("jdbc:mysql://localhost/" + conn_info_as_mysql.Database() + "?characterEncoding=UTF8", conn_info_as_mysql.Uid(), conn_info_as_mysql.Pwd());
|
||||
Connection rv = Conn_make_by_url("jdbc:mysql://localhost/" + conn_info_as_mysql.Database() + "?characterEncoding=UTF8&useSSL=false", conn_info_as_mysql.Uid(), conn_info_as_mysql.Pwd());
|
||||
return rv;
|
||||
}
|
||||
public static final Mysql_engine Instance = new Mysql_engine(); Mysql_engine() {}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.nulls; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Noop_conn_info extends Db_conn_info__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "null_db";
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {return this;}
|
||||
public static final Noop_conn_info Instance = new Noop_conn_info(); Noop_conn_info() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
public Noop_conn_info(String raw, String db_api, String database) {super(raw, db_api, database);}
|
||||
@Override public String Key() {return Tid_const;} public static final String Tid_const = "null_db";
|
||||
@Override public Db_conn_info New_self(String raw, Keyval_hash hash) {return this;}
|
||||
public static final Noop_conn_info Instance = new Noop_conn_info("gplx_key=null_db", "", "");
|
||||
}
|
||||
|
||||
@@ -38,8 +38,10 @@ public class Noop_engine implements Db_engine {
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Dbmeta_idx_itm... ary) {}
|
||||
public void Ddl_append_fld(String tbl, Dbmeta_fld_itm fld) {}
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Env_db_attach(String alias, Db_conn conn) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
public void Meta_reload() {}
|
||||
public boolean Meta_tbl_exists(String tbl) {return false;}
|
||||
public boolean Meta_fld_exists(String tbl, String fld) {return false;}
|
||||
public Dbmeta_tbl_mgr Meta_tbl_load_all() {return meta_tbl_mgr;} private final Dbmeta_tbl_mgr meta_tbl_mgr = new Dbmeta_tbl_mgr();
|
||||
|
||||
@@ -17,9 +17,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.postgres; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Postgres_conn_info extends Db_conn_info__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "postgresql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public Postgres_conn_info(String raw, String db_api, String database, String server, String uid, String pwd) {super(raw, db_api, database);
|
||||
this.server = server;
|
||||
this.uid = uid;
|
||||
this.pwd = pwd;
|
||||
}
|
||||
@Override public String Key() {return Tid_const;} public static final String Tid_const = "postgresql";
|
||||
public String Server() {return server;} private final String server;
|
||||
public String Uid() {return uid;} private final String uid;
|
||||
public String Pwd() {return pwd;} private final String pwd;
|
||||
public static Db_conn_info new_(String server, String database, String uid, String pwd) {
|
||||
return Db_conn_info_.parse(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
@@ -31,12 +37,10 @@ public class Postgres_conn_info extends Db_conn_info__base {
|
||||
, "encoding", "unicode" // needed for 1.1 conn; otherwise, ascii
|
||||
));
|
||||
}
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Postgres_conn_info rv = new Postgres_conn_info();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("encoding", "unicode")));
|
||||
rv.uid = m.ReadStr("user id");
|
||||
rv.pwd = m.ReadStr("password");
|
||||
return rv;
|
||||
@Override public Db_conn_info New_self(String raw, Keyval_hash hash) {
|
||||
return new Postgres_conn_info
|
||||
( raw, Bld_api(hash, Keyval_.new_("encoding", "unicode")), hash.Get_val_as_str_or_fail("database")
|
||||
, hash.Get_val_as_str_or_fail("server"), hash.Get_val_as_str_or_fail("user id"), hash.Get_val_as_str_or_fail("password"));
|
||||
}
|
||||
public static final Postgres_conn_info Instance = new Postgres_conn_info(); Postgres_conn_info() {}
|
||||
public static final Postgres_conn_info Instance = new Postgres_conn_info("", "", "", "", "", "");
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ public class Postgres_engine extends Db_engine_sql_base {
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Db_data_rdr_.new_(rdr, commandText);}
|
||||
@Override public Dbmeta_tbl_mgr Meta_tbl_load_all() {throw Err_.new_unimplemented();}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Postgres_conn_info conn_info_as_postgres = (Postgres_conn_info)conn_info;
|
||||
return Conn_make_by_url("jdbc:" + conn_info_as_postgres.Tid() + "://localhost/" + conn_info_as_postgres.Database(), conn_info_as_postgres.Uid(), conn_info_as_postgres.Pwd());
|
||||
Postgres_conn_info conn_info_as_postgres = (Postgres_conn_info)conn_info;
|
||||
return Conn_make_by_url("jdbc:" + conn_info_as_postgres.Key() + "://localhost/" + conn_info_as_postgres.Database(), conn_info_as_postgres.Uid(), conn_info_as_postgres.Pwd());
|
||||
}
|
||||
public static final Postgres_engine Instance = new Postgres_engine(); Postgres_engine() {}
|
||||
}
|
||||
|
||||
@@ -17,19 +17,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Sqlite_conn_info extends Db_conn_info__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "sqlite";
|
||||
public Io_url Url() {return url;} private Io_url url;
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Sqlite_conn_info rv = new Sqlite_conn_info();
|
||||
String url = m.ReadStr("data source");
|
||||
rv.url = Io_url_.new_any_(url);
|
||||
rv.Ctor("", url, raw, BldApi(m, KeyVal_.new_("version", "3")));
|
||||
rv.database = rv.url.NameOnly();
|
||||
return rv;
|
||||
public Sqlite_conn_info(String raw, String db_api, String database, Io_url url) {super(raw, db_api, database);this.url = url;}
|
||||
@Override public String Key() {return Key_const;} public static final String Key_const = "sqlite";
|
||||
public Io_url Url() {return url;} private final Io_url url;
|
||||
@Override public Db_conn_info New_self(String raw, Keyval_hash hash) {
|
||||
Io_url url = Io_url_.new_any_(hash.Get_val_as_str_or_fail("data source"));
|
||||
String db = url.NameOnly();
|
||||
String api = Bld_api(hash, Keyval_.new_("version", "3"));
|
||||
return new Sqlite_conn_info(raw, api, db, url);
|
||||
}
|
||||
|
||||
public static final Sqlite_conn_info Instance = new Sqlite_conn_info("", "", "", Io_url_.Empty);
|
||||
public static Db_conn_info load_(Io_url url) {
|
||||
return Db_conn_info_.parse(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
( "gplx_key" , Key_const
|
||||
, Cs__data_source , url.Xto_api()
|
||||
, Cs__version , Cs__version__3
|
||||
));
|
||||
@@ -37,12 +38,12 @@ public class Sqlite_conn_info extends Db_conn_info__base {
|
||||
public static Db_conn_info make_(Io_url url) {
|
||||
Io_mgr.Instance.CreateDirIfAbsent(url.OwnerDir());
|
||||
return Db_conn_info_.parse(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
( "gplx_key" , Key_const
|
||||
, Cs__data_source , url.Xto_api()
|
||||
, Cs__version , Cs__version__3
|
||||
|
||||
));
|
||||
}
|
||||
public static final Sqlite_conn_info Instance = new Sqlite_conn_info(); Sqlite_conn_info() {}
|
||||
public static final String Cs__data_source = "data source", Cs__version = "version", Cs__version__3 = "3";
|
||||
public static Io_url To_url(Db_conn conn) {return ((Sqlite_conn_info)conn.Conn_info()).url;}
|
||||
}
|
||||
@@ -19,13 +19,14 @@ package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.d
|
||||
import java.sql.*;
|
||||
import gplx.core.stores.*; import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*; import gplx.dbs.metas.*; import gplx.dbs.sqls.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
import org.sqlite.SQLiteConnection;
|
||||
public class Sqlite_engine extends Db_engine_sql_base {
|
||||
private final Sqlite_txn_mgr txn_mgr; private final Sqlite_schema_mgr schema_mgr;
|
||||
private final Sqlite_txn_mgr txn_mgr; private final Sqlite_schema_mgr schema_mgr;
|
||||
Sqlite_engine() {
|
||||
this.txn_mgr = new Sqlite_txn_mgr(this);
|
||||
this.schema_mgr = new Sqlite_schema_mgr(this);
|
||||
}
|
||||
@Override public String Tid() {return Sqlite_conn_info.Tid_const;}
|
||||
@Override public String Tid() {return Sqlite_conn_info.Key_const;}
|
||||
@Override public gplx.dbs.sqls.Sql_qry_wtr Sql_wtr() {return Sql_qry_wtr_.Sqlite;}
|
||||
@Override public Db_engine New_clone(Db_conn_info connectInfo) {
|
||||
Sqlite_engine rv = new Sqlite_engine();
|
||||
@@ -34,12 +35,18 @@ public class Sqlite_engine extends Db_engine_sql_base {
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Sqlite_rdr.new_(rdr, commandText);}
|
||||
@Override public Db_rdr New_rdr_clone() {return new Db_rdr__sqlite();}
|
||||
@Override public void Env_db_attach(String alias, Db_conn conn) {
|
||||
Db_conn_info cs_obj = conn.Conn_info(); if (!String_.Eq(cs_obj.Key(), Sqlite_conn_info.Key_const)) throw Err_.new_("dbs", "must attach to sqlite databases", "conn", cs_obj.Raw());
|
||||
Sqlite_conn_info cs = (Sqlite_conn_info)cs_obj;
|
||||
Env_db_attach(alias, cs.Url());
|
||||
}
|
||||
@Override public void Env_db_attach(String alias, Io_url db_url) {Exec_as_int(String_.Format("ATTACH '{0}' AS {1};", db_url.Raw(), alias));}
|
||||
@Override public void Env_db_detach(String alias) {Exec_as_int(String_.Format("DETACH {0};", alias));}
|
||||
@Override public void Txn_bgn(String name) {txn_mgr.Txn_bgn(name);}
|
||||
@Override public String Txn_end() {return txn_mgr.Txn_end();}
|
||||
@Override public void Txn_cxl() {txn_mgr.Txn_cxl();}
|
||||
@Override public void Txn_sav() {txn_mgr.Txn_sav();}
|
||||
@Override public void Meta_reload() {schema_mgr.Tbl_load_all();}
|
||||
@Override public boolean Meta_tbl_exists(String tbl) {return schema_mgr.Tbl_exists(tbl);}
|
||||
@Override public boolean Meta_fld_exists(String tbl, String fld) {return schema_mgr.Fld_exists(tbl, fld);}
|
||||
@Override public Dbmeta_tbl_mgr Meta_tbl_load_all() {return schema_mgr.Tbl_load_all();}
|
||||
@@ -56,9 +63,12 @@ public class Sqlite_engine extends Db_engine_sql_base {
|
||||
}
|
||||
Sqlite_conn_info conn_info_as_sqlite = (Sqlite_conn_info)conn_info;
|
||||
Connection rv = Conn_make_by_url("jdbc:sqlite://" + String_.Replace(conn_info_as_sqlite.Url().Raw(), "\\", "/"), "", "");
|
||||
SQLiteConnection rv_as_sqlite = (org.sqlite.SQLiteConnection)rv;
|
||||
try {rv_as_sqlite.setBusyTimeout(10000);}
|
||||
catch (SQLException e) {Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to set busy timeout; err=~{0}", Err_.Message_gplx_log(e));}
|
||||
return rv;
|
||||
}
|
||||
public static final Sqlite_engine Instance = new Sqlite_engine();
|
||||
public static final Sqlite_engine Instance = new Sqlite_engine();
|
||||
}
|
||||
class Db_rdr__sqlite extends Db_rdr__basic { @Override public byte Read_byte(String k) {try {return (byte)Int_.cast(rdr.getObject(k));} catch (Exception e) {throw Err_.new_exc(e, "db", "read failed", "k", k, "type", Byte_.Cls_val_name);}}
|
||||
@Override public boolean Read_bool_by_byte(String k) {
|
||||
|
||||
@@ -74,15 +74,6 @@ public class Sqlite_engine_ {
|
||||
usr_dlg.Log_many("", "", "index created: ~{0} ~{1}", file_id, index);
|
||||
}
|
||||
}
|
||||
public static Db_conn Conn_load_or_make_(Io_url url, Bool_obj_ref created) {
|
||||
boolean exists = Io_mgr.Instance.ExistsFil(url);
|
||||
created.Val_(!exists);
|
||||
Db_conn_info connect = exists ? Sqlite_conn_info.load_(url) : Sqlite_conn_info.make_(url);
|
||||
Db_conn p = Db_conn_pool.Instance.Get_or_new(connect);
|
||||
if (!exists)
|
||||
Pragma_page_size(p, 4096);
|
||||
return p;
|
||||
}
|
||||
public static final int Stmt_arg_max = 999; // 999 is max number of variables allowed by sqlite
|
||||
public static final boolean Supports_read_binary_stream = false;
|
||||
public static final boolean Supports_indexed_by = true;
|
||||
|
||||
@@ -38,7 +38,7 @@ public class Sqlite_schema_mgr {
|
||||
}
|
||||
private void Init(Db_engine engine) {
|
||||
init = false;
|
||||
Gfo_usr_dlg_.Instance.Log_many("", "", "db.schema.load.bgn: conn=~{0}", engine.Conn_info().Xto_api());
|
||||
Gfo_usr_dlg_.Instance.Log_many("", "", "db.schema.load.bgn: conn=~{0}", engine.Conn_info().Db_api());
|
||||
tbl_mgr.Clear(); idx_mgr.Clear();
|
||||
Dbmeta_parser__tbl tbl_parser = new Dbmeta_parser__tbl();
|
||||
Dbmeta_parser__idx idx_parser = new Dbmeta_parser__idx();
|
||||
@@ -52,14 +52,15 @@ public class Sqlite_schema_mgr {
|
||||
int type_int = Dbmeta_itm_tid.Xto_int(type_str);
|
||||
switch (type_int) {
|
||||
case Dbmeta_itm_tid.Tid_table:
|
||||
if (String_.Eq(name, "sqlite_sequence")) continue; // ignore b/c of non-orthodox syntax; EX: "CREATE TABLE sqlite_sequence(name, seq)";
|
||||
if (String_.Has_at_bgn(name, "sqlite_")) continue; // ignore b/c of non-orthodox syntax; EX: "CREATE TABLE sqlite_sequence(name, seq)"; also "CREATE TABLE sqlite_stat(tbl,idx,stat)";
|
||||
tbl_mgr.Add(tbl_parser.Parse(Bry_.new_u8(sql)));
|
||||
break;
|
||||
case Dbmeta_itm_tid.Tid_index:
|
||||
if (sql == null) continue; // ignore "autoindex"; EX: sqlite_autoindex_temp_page_len_avg_1
|
||||
idx_mgr.Add(idx_parser.Parse(Bry_.new_u8(sql)));
|
||||
break;
|
||||
default:
|
||||
Gfo_usr_dlg_.Instance.Log_many("", "", "db.schema.unknown type: conn=~{0} type=~{1} name=~{2} sql=~{3}", engine.Conn_info().Xto_api(), type_str, name, sql);
|
||||
Gfo_usr_dlg_.Instance.Log_many("", "", "db.schema.unknown type: conn=~{0} type=~{1} name=~{2} sql=~{3}", engine.Conn_info().Db_api(), type_str, name, sql);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,8 +63,10 @@ public class TdbEngine implements Db_engine {
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Dbmeta_idx_itm... ary) {throw Err_.new_unimplemented();}
|
||||
public void Ddl_append_fld(String tbl, Dbmeta_fld_itm fld) {throw Err_.new_unimplemented();}
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Env_db_attach(String alias, Db_conn conn) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
public void Meta_reload() {}
|
||||
public boolean Meta_tbl_exists(String name) {return false;}
|
||||
public boolean Meta_fld_exists(String tbl, String fld) {return false;}
|
||||
public Dbmeta_tbl_mgr Meta_tbl_load_all() {return meta_tbl_mgr;} private final Dbmeta_tbl_mgr meta_tbl_mgr = new Dbmeta_tbl_mgr();
|
||||
|
||||
@@ -49,7 +49,7 @@ class TdbInsertWkr implements Db_qryWkr {
|
||||
int InsertRowsByVals(TdbEngine engine, TdbTable tbl, Db_qry_insert insert) {
|
||||
GfoNde row = GfoNde_.vals_(tbl.Flds(), new Object[tbl.Flds().Count()]);
|
||||
for (int i = 0; i < insert.Args().Count(); i++) {
|
||||
KeyVal kv = insert.Args().Get_at(i);
|
||||
Keyval kv = insert.Args().Get_at(i);
|
||||
Db_arg arg = (Db_arg)kv.Val();
|
||||
row.Write(kv.Key(), arg.Val);
|
||||
}
|
||||
|
||||
@@ -24,11 +24,11 @@ class TdbSelectWkr implements Db_qryWkr {
|
||||
if (cmd.From().Tbls.Count() > 1) throw Err_.new_("gplx.tdbs", "joins not supported for tdbs", "sql", cmd.To_sql__exec(engineObj.Sql_wtr()));
|
||||
|
||||
TdbTable tbl = engine.FetchTbl(cmd.From().Base_tbl.Name);
|
||||
GfoNdeList rv = (cmd.Where_itm() == Sql_where_itm.Where__null && cmd.Limit() == Db_qry__select_cmd.Limit__disabled) ? rv = tbl.Rows() : FilterRecords(tbl, cmd.Where_itm().Root, cmd.Limit());
|
||||
GfoNdeList rv = (cmd.Where_itm() == Sql_where_clause.Where__null && cmd.Limit() == Db_qry__select_cmd.Limit__disabled) ? rv = tbl.Rows() : FilterRecords(tbl, cmd.Where_itm().Root, cmd.Limit());
|
||||
if (cmd.GroupBy() != null)
|
||||
rv = TdbGroupByWkr.GroupByExec(cmd, rv, tbl);
|
||||
if (cmd.Order() != null) { // don't use null pattern here; if null ORDER BY, then don't call .Sort on GfoNdeList
|
||||
ComparerAble comparer = Sql_order_fld_sorter.new_(cmd.Order().Flds);
|
||||
ComparerAble comparer = Sql_order_fld_sorter.new_(cmd.Order().Flds());
|
||||
rv.Sort_by(comparer);
|
||||
}
|
||||
return GfoNdeRdr_.peers_(rv, false);
|
||||
|
||||
@@ -37,7 +37,7 @@ class TdbUpdateWkr implements Db_qryWkr {
|
||||
}
|
||||
void UpdateRow(Db_qry_update cmd, GfoNde row) {
|
||||
for (int i = 0; i < cmd.Args().Count(); i++) {
|
||||
KeyVal p = (KeyVal)cmd.Args().Get_at(i);
|
||||
Keyval p = (Keyval)cmd.Args().Get_at(i);
|
||||
Db_arg prm = (Db_arg)p.Val();
|
||||
row.Write(p.Key(), prm.Val);
|
||||
}
|
||||
|
||||
@@ -17,21 +17,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Tdb_conn_info extends Db_conn_info__base {
|
||||
public Io_url Url() {return url;} Io_url url;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "tdb";
|
||||
public static Db_conn_info new_(Io_url url) {
|
||||
public Tdb_conn_info(String raw, String db_api, String database, Io_url url) {super(raw, db_api, database);this.url = url; this.server = url.Raw(); }
|
||||
@Override public String Key() {return Tid_const;} public static final String Tid_const = "tdb";
|
||||
public Io_url Url() {return url;} private final Io_url url;
|
||||
public String Server() {return server;} private final String server;
|
||||
@Override public Db_conn_info New_self(String raw, Keyval_hash hash) {
|
||||
Io_url url = Io_url_.new_any_(hash.Get_val_as_str_or_fail("url"));
|
||||
String db = url.NameOnly();
|
||||
String api = Bld_api(hash, Keyval_.new_("version", "3"));
|
||||
return new Tdb_conn_info(raw, api, db, url);
|
||||
}
|
||||
public static Db_conn_info new_(Io_url url) {
|
||||
return Db_conn_info_.parse(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "url", url.Raw()
|
||||
));
|
||||
} Tdb_conn_info() {}
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Tdb_conn_info rv = new Tdb_conn_info();
|
||||
String urlStr = m.ReadStr("url");
|
||||
Io_url url = Io_url_.new_any_(urlStr);
|
||||
rv.Ctor(urlStr, url.NameOnly(), raw, BldApi(m));
|
||||
rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final Tdb_conn_info Instance = new Tdb_conn_info();
|
||||
public static final Tdb_conn_info Instance = new Tdb_conn_info("", "", "", Io_url_.Empty);
|
||||
}
|
||||
|
||||
@@ -25,4 +25,11 @@ public class Dbmeta_fld_mgr {
|
||||
public Dbmeta_fld_itm Get_at(int idx) {return (Dbmeta_fld_itm)hash.Get_at(idx);}
|
||||
public Dbmeta_fld_itm Get_by(String name) {return (Dbmeta_fld_itm)hash.Get_by(name);}
|
||||
public Dbmeta_fld_itm[] To_ary() {return hash.Count() == 0 ? Dbmeta_fld_itm.Ary_empty : (Dbmeta_fld_itm[])hash.To_ary(Dbmeta_fld_itm.class);}
|
||||
public Dbmeta_fld_list To_fld_list() {
|
||||
Dbmeta_fld_list rv = Dbmeta_fld_list.new_();
|
||||
int len = hash.Count();
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv.Add(Get_at(i));
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,25 +94,54 @@ class Dbmeta_fld_wkr__default extends Dbmeta_fld_wkr__base {
|
||||
Object default_val = null;
|
||||
rdr.Skip_ws();
|
||||
byte[] src = rdr.Src();
|
||||
byte b = src[rdr.Pos()];
|
||||
switch (b) {
|
||||
case Byte_ascii.Quote:
|
||||
case Byte_ascii.Apos:
|
||||
switch (fld.Type().Tid_ansi()) {
|
||||
case Dbmeta_fld_tid.Tid__bool:
|
||||
case Dbmeta_fld_tid.Tid__byte:
|
||||
case Dbmeta_fld_tid.Tid__short:
|
||||
case Dbmeta_fld_tid.Tid__int: default_val = Int_.parse(Read_str_to_end_of_num(rdr)); break;
|
||||
case Dbmeta_fld_tid.Tid__long: default_val = Long_.parse(Read_str_to_end_of_num(rdr)); break;
|
||||
case Dbmeta_fld_tid.Tid__float: default_val = Float_.parse(Read_str_to_end_of_num(rdr)); break;
|
||||
case Dbmeta_fld_tid.Tid__double: default_val = Double_.parse(Read_str_to_end_of_num(rdr)); break;
|
||||
case Dbmeta_fld_tid.Tid__decimal: default_val = Decimal_adp_.parse(Read_str_to_end_of_num(rdr)); break;
|
||||
case Dbmeta_fld_tid.Tid__str:
|
||||
case Dbmeta_fld_tid.Tid__text:
|
||||
case Dbmeta_fld_tid.Tid__bry:
|
||||
byte b = src[rdr.Pos()];
|
||||
int bgn_pos = rdr.Pos() + 1;
|
||||
int end_pos = Bry_find_.Find_fwd(src, b, bgn_pos); if (end_pos == Bry_find_.Not_found) rdr.Err_wkr().Fail("unclosed quote");
|
||||
default_val = Bry_.Mid(src, bgn_pos, end_pos);
|
||||
rdr.Move_to(end_pos + 1);
|
||||
break;
|
||||
case Byte_ascii.Dash:
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
default_val = rdr.Read_int_to_non_num();
|
||||
break;
|
||||
default:
|
||||
rdr.Err_wkr().Fail("invalid field_default"); break;
|
||||
case Dbmeta_fld_tid.Tid__date: throw Err_.new_unhandled_default(fld.Type().Tid_ansi());
|
||||
}
|
||||
fld.Default_(default_val);
|
||||
}
|
||||
public String Read_str_to_end_of_num(Bry_rdr rdr) {
|
||||
int bgn = rdr.Pos();
|
||||
int pos = bgn, end = bgn;
|
||||
int src_end = rdr.Src_end();
|
||||
byte[] src = rdr.Src();
|
||||
boolean loop = true;
|
||||
while (loop) {
|
||||
if (pos >= src_end) {
|
||||
end = src_end;
|
||||
break;
|
||||
}
|
||||
byte b = src[pos]; ++pos;
|
||||
switch (b) {
|
||||
case Byte_ascii.Space: case Byte_ascii.Tab: case Byte_ascii.Nl: case Byte_ascii.Cr:
|
||||
case Byte_ascii.Paren_end:
|
||||
case Byte_ascii.Comma:
|
||||
end = pos - 1;
|
||||
loop = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
rdr.Move_to(end);
|
||||
return String_.new_a7(src, bgn, end);
|
||||
}
|
||||
@Override protected void When_match(Dbmeta_fld_itm fld) {}
|
||||
private static final byte[] Hook = Bry_.new_a7("default");
|
||||
public static final Dbmeta_fld_wkr__default Instance = new Dbmeta_fld_wkr__default();
|
||||
|
||||
@@ -27,13 +27,13 @@ public class Dbmeta_parser__fld_tst {
|
||||
fxt.Test_parse_type(" decimal ( 12 , 10 )" , fxt.Make_type(Dbmeta_fld_tid.Tid__decimal, 12, 10));
|
||||
}
|
||||
@Test public void Parse_fld() {
|
||||
fxt.Test_parse_fld("name_1 int" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_unknown));
|
||||
fxt.Test_parse_fld("name_1 int null" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_null));
|
||||
fxt.Test_parse_fld("name_1 int not null" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null));
|
||||
fxt.Test_parse_fld("name_1 int not null autoincrement" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.N, Bool_.Y));
|
||||
fxt.Test_parse_fld("name_1 int not null primary key" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N));
|
||||
fxt.Test_parse_fld("name_1 int not null default -1" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N, -1));
|
||||
fxt.Test_parse_fld("name_1 int not null default 'abc'" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N, "abc"));
|
||||
fxt.Test_parse_fld("name_1 int" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_unknown));
|
||||
fxt.Test_parse_fld("name_1 int null" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_null));
|
||||
fxt.Test_parse_fld("name_1 int not null" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null));
|
||||
fxt.Test_parse_fld("name_1 int not null autoincrement" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.N, Bool_.Y));
|
||||
fxt.Test_parse_fld("name_1 int not null primary key" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N));
|
||||
fxt.Test_parse_fld("name_1 int not null default -1" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N, -1));
|
||||
fxt.Test_parse_fld("name_1 varchar(3) not null default 'abc'" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__str, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N, "abc"));
|
||||
}
|
||||
}
|
||||
class Dbmeta_parser__fld_fxt {
|
||||
|
||||
@@ -22,92 +22,89 @@ public class Db_qry__select_cmd implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_select;}
|
||||
public boolean Exec_is_rdr() {return Bool_.Y;}
|
||||
public String Base_table() {return from.Base_tbl.Name;}
|
||||
public Sql_from_itm From() {return from;} private Sql_from_itm from;
|
||||
public Sql_from_clause From() {return from;} private Sql_from_clause from;
|
||||
public Db_qry__select_cmd From_(String tbl) {return From_(tbl, Sql_tbl_itm.Alias__null);}
|
||||
public Db_qry__select_cmd From_(String name, String alias) {return From_(Sql_tbl_itm.Db__null, name, alias);}
|
||||
public Db_qry__select_cmd From_(String db, String tbl, String alias) {
|
||||
if (from != null) throw Err_.new_("sql_qry", "super table already defined", "from", from.Base_tbl.Name);
|
||||
from = new Sql_from_itm(new Sql_tbl_itm(Sql_tbl_itm.Tid__from, db, tbl, alias, Sql_join_itm.Ary__empty));
|
||||
from = new Sql_from_clause(new Sql_tbl_itm(Sql_tbl_itm.Tid__from, db, tbl, alias, Sql_join_fld.Ary__empty));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Join_(String name, String alias, Sql_join_itm... join_flds) {return Join_(Sql_tbl_itm.Db__null, name, alias, join_flds);}
|
||||
public Db_qry__select_cmd Join_(String db, String name, String alias, Sql_join_itm... join_flds) {
|
||||
public Db_qry__select_cmd Join_(String name, String alias, Sql_join_fld... join_flds) {return Join_(Sql_tbl_itm.Tid__inner, Sql_tbl_itm.Db__null , name, alias, join_flds);}
|
||||
public Db_qry__select_cmd Join_(String db, String name, String alias, Sql_join_fld... join_flds) {return Join_(Sql_tbl_itm.Tid__inner, db , name, alias, join_flds);}
|
||||
public Db_qry__select_cmd Join_(int join_tid, String db, String name, String alias, Sql_join_fld... join_flds) {
|
||||
if (from == null) throw Err_.new_("sql_qry", "super table is not defined");
|
||||
from.Tbls.Add(new Sql_tbl_itm(Sql_tbl_itm.Tid__inner, db, name, alias, join_flds));
|
||||
from.Tbls.Add(new Sql_tbl_itm(join_tid, db, name, alias, join_flds));
|
||||
return this;
|
||||
}
|
||||
public Sql_select_itm Cols() {return cols;} private Sql_select_itm cols = Sql_select_itm.All;
|
||||
public Sql_select_clause Cols() {return cols;} private Sql_select_clause cols = new Sql_select_clause();
|
||||
public Db_qry__select_cmd Distinct_() {cols.Distinct = true; return this;}
|
||||
public Db_qry__select_cmd Cols_all_() {cols = Sql_select_itm.All; return this;}
|
||||
public Db_qry__select_cmd Cols_(String... ary) {return Cols_w_tbl_(Sql_select_fld.Tbl_null, ary);}
|
||||
public Db_qry__select_cmd Cols_all_() {cols.Flds.Clear().Add(Sql_select_fld.Wildcard); return this;}
|
||||
public Db_qry__select_cmd Cols_(String... ary) {return Cols_w_tbl_(Sql_select_fld.Tbl__null, ary);}
|
||||
public Db_qry__select_cmd Cols_w_tbl_(String tbl, String... ary) {
|
||||
if (cols == Sql_select_itm.All) cols = new Sql_select_itm();
|
||||
for (String itm : ary)
|
||||
cols.Flds.Add(Sql_select_fld.New_fld(tbl, itm, itm));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_w_alias_(String expr, String alias) {
|
||||
if (cols == Sql_select_itm.All) cols = new Sql_select_itm();
|
||||
cols.Flds.Add(Sql_select_fld.New_fld(Sql_select_fld.Tbl_null, expr, alias));
|
||||
cols.Flds.Add(Sql_select_fld.New_fld(Sql_select_fld.Tbl__null, expr, alias));
|
||||
return this;
|
||||
}
|
||||
public Sql_where_itm Where_itm() {return where_itm;} private Sql_where_itm where_itm = Sql_where_itm.All;
|
||||
public Sql_where_clause Where_itm() {return where_itm;} private Sql_where_clause where_itm = Sql_where_clause.All;
|
||||
public Db_qry__select_cmd Where_(Criteria root) {
|
||||
if (where_itm == Sql_where_itm.All) where_itm = new Sql_where_itm();
|
||||
if (where_itm == Sql_where_clause.All) where_itm = new Sql_where_clause();
|
||||
where_itm.Root = root;
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Where_and(Criteria crt) {
|
||||
if (where_itm == Sql_where_itm.All) throw Err_.new_("sql_qry", "where is not defined");
|
||||
if (where_itm == Sql_where_clause.All) throw Err_.new_("sql_qry", "where is not defined");
|
||||
where_itm.Root = Criteria_.And(where_itm.Root, crt);
|
||||
return this;
|
||||
}
|
||||
public Sql_order_itm Order() {return order;} private Sql_order_itm order = null;
|
||||
public Sql_order_clause Order() {return order;} private Sql_order_clause order = null;
|
||||
public Db_qry__select_cmd Order_asc_(String fld) {return Order_(fld, Bool_.Y);}
|
||||
public Db_qry__select_cmd Order_(String fld, boolean asc) {return Order_(Sql_order_fld.Tbl__null, fld, asc);}
|
||||
public Db_qry__select_cmd Order_(String fld) {return Order_(Sql_order_fld.Tbl__null, fld, Bool_.Y);}
|
||||
public Db_qry__select_cmd Order_(String fld, boolean asc) {return Order_(Sql_order_fld.Tbl__null, fld, asc);}
|
||||
public Db_qry__select_cmd Order_(String tbl, String fld) {return Order_(tbl, fld, Bool_.Y);}
|
||||
public Db_qry__select_cmd Order_(String tbl, String fld, boolean asc) {
|
||||
if (order == null) order = new Sql_order_clause();
|
||||
Sql_order_fld item = new Sql_order_fld(tbl, fld, asc ? Sql_order_fld.Sort__nil : Sql_order_fld.Sort__dsc);
|
||||
order = new Sql_order_itm(new Sql_order_fld[] {item});
|
||||
order.Flds__add(item);
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Order_asc_many_(String... flds) {
|
||||
if (order == null) order = new Sql_order_clause();
|
||||
int flds_len = flds.length;
|
||||
Sql_order_fld[] ary = new Sql_order_fld[flds_len];
|
||||
for (int i = 0; i < flds_len; ++i)
|
||||
ary[i] = new Sql_order_fld(Sql_order_fld.Tbl__null, flds[i], Sql_order_fld.Sort__nil);
|
||||
order = new Sql_order_itm(ary);
|
||||
order.Flds__add(new Sql_order_fld(Sql_order_fld.Tbl__null, flds[i], Sql_order_fld.Sort__nil));
|
||||
return this;
|
||||
}
|
||||
public int Limit() {return limit;} public Db_qry__select_cmd Limit_(int v) {this.limit = v; return this;} private int limit = Limit__disabled; public static final int Limit__disabled = Int_.Min_value;
|
||||
public int Offset() {return offset;} public Db_qry__select_cmd Offset_(int v) {this.offset = v; return this;} private int offset = Offset__disabled; public static final int Offset__disabled = Int_.Min_value;
|
||||
public String Indexed_by() {return indexed_by;} public Db_qry__select_cmd Indexed_by_(String v) {indexed_by = v; return this;} private String indexed_by;
|
||||
|
||||
public Sql_group_itm GroupBy() {return groupBy;} private Sql_group_itm groupBy = null;
|
||||
public Sql_group_clause GroupBy() {return groupBy;} private Sql_group_clause groupBy = null;
|
||||
public Db_qry__select_cmd GroupBy_(String... flds) {
|
||||
if (groupBy != null) throw Err_.new_("sql_qry", "group by already defined", "group", groupBy);
|
||||
groupBy = Sql_group_itm.new_(flds);
|
||||
groupBy = Sql_group_clause.new_(flds);
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_groupBy_max(String fld) {return Cols_groupBy_max(fld, fld);}
|
||||
public Db_qry__select_cmd Cols_groupBy_max(String fld, String alias) {
|
||||
if (cols == Sql_select_itm.All) cols = new Sql_select_itm();
|
||||
cols.Flds.Add(Sql_select_fld.New_max(Sql_select_fld.Tbl_null, fld, alias));
|
||||
cols.Flds.Add(Sql_select_fld.New_max(Sql_select_fld.Tbl__null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_groupBy_min(String fld, String alias) {
|
||||
if (cols == Sql_select_itm.All) cols = new Sql_select_itm();
|
||||
cols.Flds.Add(Sql_select_fld.New_min(Sql_select_fld.Tbl_null, fld, alias));
|
||||
cols.Flds.Add(Sql_select_fld.New_min(Sql_select_fld.Tbl__null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_groupBy_count(String fld, String alias) {
|
||||
if (cols == Sql_select_itm.All) cols = new Sql_select_itm();
|
||||
cols.Flds.Add(Sql_select_fld.New_count(Sql_select_fld.Tbl_null, fld, alias));
|
||||
cols.Flds.Add(Sql_select_fld.New_count(Sql_select_fld.Tbl__null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_groupBy_sum(String fld) {return Cols_groupBy_sum(fld, fld);}
|
||||
public Db_qry__select_cmd Cols_groupBy_sum(String fld, String alias) {
|
||||
if (cols == Sql_select_itm.All) cols = new Sql_select_itm();
|
||||
cols.Flds.Add(Sql_select_fld.New_sum(Sql_select_fld.Tbl_null, fld, alias));
|
||||
cols.Flds.Add(Sql_select_fld.New_sum(Sql_select_fld.Tbl__null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Db_qry_insert implements Db_arg_owner {
|
||||
public String Base_table() {return base_table;} private String base_table;
|
||||
public String[] Cols_for_insert() {return cols_for_insert;} private String[] cols_for_insert;
|
||||
public Db_arg_owner From_(String tbl) {base_table = tbl; return this;}
|
||||
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
|
||||
public Keyval_hash Args() {return args;} private final Keyval_hash args = new Keyval_hash();
|
||||
public Db_arg_owner Val_byte(String k, byte v) {return Val_obj_type(k, v, Db_val_type.Tid_byte);}
|
||||
public Db_arg_owner Val_int(String k, int v) {return Val_obj_type(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_arg_owner Val_long(String k, long v) {return Val_obj_type(k, v, Db_val_type.Tid_int64);}
|
||||
@@ -48,7 +48,7 @@ public class Db_qry_insert implements Db_arg_owner {
|
||||
public Db_qry_insert Cols_(String... ary) {
|
||||
if (cols == null) cols = new Sql_select_fld_list();
|
||||
for (String fld : ary)
|
||||
cols.Add(Sql_select_fld.New_fld(Sql_select_fld.Tbl_null, fld, fld));
|
||||
cols.Add(Sql_select_fld.New_fld(Sql_select_fld.Tbl__null, fld, fld));
|
||||
return this;
|
||||
}
|
||||
public Sql_select_fld_list Cols() {return cols;} private Sql_select_fld_list cols;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Db_qry_sql implements Db_qry {
|
||||
public static Db_qry_sql dml_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql ddl_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql xtn_(String sql) {return sql_(sql);}
|
||||
static Db_qry_sql sql_(String sql) {
|
||||
public static Db_qry_sql sql_(String sql) {
|
||||
Db_qry_sql rv = new Db_qry_sql();
|
||||
rv.sql = sql; rv.isReader = false;
|
||||
return rv;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Db_qry_update implements Db_arg_owner {
|
||||
public String[] Cols_for_update() {return cols_for_update;} private String[] cols_for_update;
|
||||
public Criteria Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = crt; return this;} private Criteria where;
|
||||
public Db_arg_owner From_(String tbl) {base_table = tbl; return this;}
|
||||
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
|
||||
public Keyval_hash Args() {return args;} private final Keyval_hash args = new Keyval_hash();
|
||||
public Db_arg_owner Val_byte(String k, byte v) {return Val_obj_type(k, v, Db_val_type.Tid_byte);}
|
||||
public Db_arg_owner Val_int(String k, int v) {return Val_obj_type(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_arg_owner Val_long(String k, long v) {return Val_obj_type(k, v, Db_val_type.Tid_int64);}
|
||||
|
||||
@@ -129,7 +129,7 @@ public class Db_stmt_cmd implements Db_stmt {
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.new_exc(e, "db_stmt", "insert failed", "url", engine.Conn_info().Xto_api(), "sql", sql);
|
||||
throw Err_.new_exc(e, "db_stmt", "insert failed", "url", engine.Conn_info().Db_api(), "sql", sql);
|
||||
}
|
||||
}
|
||||
public int Exec_update() {
|
||||
@@ -137,7 +137,7 @@ public class Db_stmt_cmd implements Db_stmt {
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.new_exc(e, "db_stmt", "update failed", "url", engine.Conn_info().Xto_api(), "sql", sql);
|
||||
throw Err_.new_exc(e, "db_stmt", "update failed", "url", engine.Conn_info().Db_api(), "sql", sql);
|
||||
}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
@@ -145,7 +145,7 @@ public class Db_stmt_cmd implements Db_stmt {
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.new_exc(e, "db_stmt", "delete failed", "url", engine.Conn_info().Xto_api(), "sql", sql);
|
||||
throw Err_.new_exc(e, "db_stmt", "delete failed", "url", engine.Conn_info().Db_api(), "sql", sql);
|
||||
}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
|
||||
@@ -18,10 +18,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.wtrs.*;
|
||||
public class Sql_qry_wtr_ {
|
||||
public static final Sql_qry_wtr
|
||||
public static final Sql_qry_wtr
|
||||
Basic = new Sql_core_wtr()
|
||||
, Mysql = new Sql_core_wtr__mysql()
|
||||
, Sqlite = new Sql_core_wtr__sqlite()
|
||||
;
|
||||
|
||||
public static final byte Like_wildcard = Byte_ascii.Percent;
|
||||
public static String Quote_arg(String s) { // only for constructing DEBUG SQL strings
|
||||
return "'" + String_.Replace(s, "'", "''") + "'";
|
||||
}
|
||||
|
||||
public static String Gen_placeholder_parameters(Db_qry qry) {return Sql_qry_wtr_.Sqlite.To_sql_str(qry, true);} // replace arguments with ?; EX: UPDATE a SET b = ? WHERE c = ?;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ 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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_from_itm {
|
||||
public Sql_from_itm(Sql_tbl_itm base_tbl) {
|
||||
public class Sql_from_clause {
|
||||
public Sql_from_clause(Sql_tbl_itm base_tbl) {
|
||||
this.Base_tbl = base_tbl;
|
||||
Tbls.Add(base_tbl);
|
||||
}
|
||||
@@ -16,13 +16,13 @@ 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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_group_itm {
|
||||
public class Sql_group_clause {
|
||||
public List_adp Flds() {return flds;} List_adp flds = List_adp_.new_();
|
||||
|
||||
public static Sql_group_itm new_(String... ary) {
|
||||
Sql_group_itm rv = new Sql_group_itm();
|
||||
public static Sql_group_clause new_(String... ary) {
|
||||
Sql_group_clause rv = new Sql_group_clause();
|
||||
for (String itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_group_itm() {}
|
||||
} Sql_group_clause() {}
|
||||
}
|
||||
@@ -16,8 +16,8 @@ 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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_join_itm {
|
||||
public Sql_join_itm(String trg_fld, String src_tbl, String src_fld) {
|
||||
public class Sql_join_fld {
|
||||
public Sql_join_fld(String trg_fld, String src_tbl, String src_fld) {
|
||||
this.Trg_fld = trg_fld;
|
||||
this.Src_tbl = src_tbl;
|
||||
this.Src_fld = src_fld;
|
||||
@@ -26,5 +26,9 @@ public class Sql_join_itm {
|
||||
public final String Src_fld;
|
||||
public final String Trg_fld;
|
||||
|
||||
public static final Sql_join_itm[] Ary__empty = new Sql_join_itm[0];
|
||||
public String To_fld_sql(boolean fld_is_src, String trg_tbl) {
|
||||
return fld_is_src ? Src_tbl + "." + Src_fld : trg_tbl + "." + Trg_fld;
|
||||
}
|
||||
|
||||
public static final Sql_join_fld[] Ary__empty = new Sql_join_fld[0];
|
||||
}
|
||||
27
140_dbs/src/gplx/dbs/sqls/itms/Sql_order_clause.java
Normal file
27
140_dbs/src/gplx/dbs/sqls/itms/Sql_order_clause.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_order_clause {
|
||||
private final List_adp list = List_adp_.new_();
|
||||
private Sql_order_fld[] ary;
|
||||
public void Flds__add(Sql_order_fld fld) {list.Add(fld);}
|
||||
public Sql_order_fld[] Flds() {
|
||||
if (ary == null) ary = (Sql_order_fld[])list.To_ary_and_clear(Sql_order_fld.class);
|
||||
return ary;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_order_itm {
|
||||
public Sql_order_itm(Sql_order_fld[] flds) {this.Flds = flds;}
|
||||
public final Sql_order_fld[] Flds;
|
||||
public class Sql_select_clause {
|
||||
public boolean Distinct = false;
|
||||
public final Sql_select_fld_list Flds = new Sql_select_fld_list();
|
||||
}
|
||||
@@ -21,37 +21,40 @@ public abstract class Sql_select_fld {
|
||||
public Sql_select_fld(String tbl, String fld, String alias) {
|
||||
this.Tbl = tbl; this.Fld = fld; this.Alias = alias;
|
||||
}
|
||||
public final String Tbl;
|
||||
public final String Fld;
|
||||
public final String Alias;
|
||||
public abstract String To_sql();
|
||||
public final String Tbl; // tbl_alias; EX: "t1."
|
||||
public final String Fld; // fld_key; EX: "fld"
|
||||
public final String Alias; // alias; EX: " AS [Field Name]"; NOTE: must be fld name if no alias defined; EX: "SELECT fld1" should have tbl='', fld='fld1' and alias='fld1'
|
||||
public abstract String To_fld_sql(); // EX: "t1.fld AS [Field Name]"
|
||||
public String To_fld_alias() {return Alias;} // EX: "t1.fld AS [Field Name]" -> "Field Name"; "t1.fld1" -> "fld1"
|
||||
public String To_fld_key() {return Tbl == Tbl__null ? Fld : Tbl + "." + Fld;}
|
||||
|
||||
public static final String Tbl_null = null;
|
||||
public static final String Tbl__null = null, Fld__wildcard = "*";
|
||||
public static Sql_select_fld New_fld (String tbl, String fld, String alias) {return new Sql_select_fld_col(tbl, fld, alias);}
|
||||
public static Sql_select_fld Wildcard = Sql_select_fld_wild.Instance;
|
||||
public static Sql_select_fld New_count (String tbl, String fld, String alias) {return new Sql_select_fld_count(tbl, fld, alias);}
|
||||
public static Sql_select_fld New_sum (String tbl, String fld, String alias) {return new Sql_select_fld_sum(tbl, fld, alias);}
|
||||
public static Sql_select_fld New_min (String tbl, String fld, String alias) {return new Sql_select_fld_minMax(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld New_max (String tbl, String fld, String alias) {return new Sql_select_fld_minMax(CompareAble_.More, tbl, fld, alias);}
|
||||
public static String Bld_tbl_w_fld(String tbl, String fld) {return tbl == null ? fld : tbl + "." + fld;}
|
||||
|
||||
// tdb related functions
|
||||
public ClassXtn Val_type() {return val_type;} public void Val_type_(ClassXtn val) {val_type = val;} private ClassXtn val_type = ObjectClassXtn.Instance;
|
||||
public abstract Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type);
|
||||
@gplx.Virtual public void GroupBy_type(ClassXtn type) {this.Val_type_(type);}
|
||||
}
|
||||
class Sql_select_fld_wild extends Sql_select_fld { Sql_select_fld_wild() {super(Tbl_null, Fld_wildcard, Fld_wildcard);}
|
||||
@Override public String To_sql() {return Fld_wildcard;}
|
||||
class Sql_select_fld_wild extends Sql_select_fld { Sql_select_fld_wild() {super(Sql_select_fld.Tbl__null, Fld__wildcard, Fld__wildcard);}
|
||||
@Override public String To_fld_sql() {return Fld__wildcard;}
|
||||
|
||||
public static final Sql_select_fld_wild Instance = new Sql_select_fld_wild();
|
||||
public static final String Fld_wildcard = "*";
|
||||
|
||||
// tdb-related functions
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {throw Err_.new_wo_type("group by eval not allowed on *");}
|
||||
@Override public void GroupBy_type(ClassXtn type) {throw Err_.new_wo_type("group by type not allowed on *");}
|
||||
}
|
||||
class Sql_select_fld_col extends Sql_select_fld { public Sql_select_fld_col(String tbl, String fld, String alias) {super(tbl, fld, alias);}
|
||||
@Override public String To_sql() {
|
||||
@Override public String To_fld_sql() {
|
||||
String rv = Fld;
|
||||
if (this.Tbl != Tbl_null)
|
||||
if (this.Tbl != Tbl__null)
|
||||
rv = this.Tbl + "." + Fld;
|
||||
if (!String_.Eq(Alias, Fld))
|
||||
rv = rv + " AS " + Alias;
|
||||
|
||||
@@ -19,7 +19,7 @@ package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sq
|
||||
import gplx.core.type_xtns.*;
|
||||
abstract class Sql_select_fld_func extends Sql_select_fld { public Sql_select_fld_func(String tbl, String fld, String alias) {super(tbl, fld, alias);}
|
||||
public abstract String XtoSql_functionName();
|
||||
@Override public String To_sql() {
|
||||
@Override public String To_fld_sql() {
|
||||
return String_.Format("{0}({1}) AS {2}", XtoSql_functionName(), Fld, Alias);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_select_fld_list {
|
||||
private final Ordered_hash hash = Ordered_hash_.New();
|
||||
public int Len() {return hash.Count();}
|
||||
public Sql_select_fld Get_at(int i) {return (Sql_select_fld)hash.Get_at(i);}
|
||||
public void Add(Sql_select_fld fld) {hash.Add(fld.Alias, fld);}
|
||||
public int Len() {return hash.Count();}
|
||||
public Sql_select_fld_list Clear() {hash.Clear();return this;}
|
||||
public Sql_select_fld Get_at(int i) {return (Sql_select_fld)hash.Get_at(i);}
|
||||
public void Add(Sql_select_fld fld) {hash.Add(fld.Alias, fld);}
|
||||
}
|
||||
|
||||
@@ -1,25 +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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_select_itm {
|
||||
public boolean Distinct = false;
|
||||
public final Sql_select_fld_list Flds = new Sql_select_fld_list();
|
||||
|
||||
public static final Sql_select_itm All = all_();
|
||||
private static Sql_select_itm all_() {Sql_select_itm rv = new Sql_select_itm(); rv.Flds.Add(Sql_select_fld_wild.Instance); return rv;}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_tbl_itm {
|
||||
public Sql_tbl_itm(int join_tid, String db, String name, String alias, Sql_join_itm[] join_flds) {
|
||||
public Sql_tbl_itm(int join_tid, String db, String name, String alias, Sql_join_fld[] join_flds) {
|
||||
this.Join_tid = join_tid;
|
||||
this.Db = db;
|
||||
this.Name = name;
|
||||
@@ -28,7 +28,8 @@ public class Sql_tbl_itm {
|
||||
public final String Db;
|
||||
public final String Name;
|
||||
public final String Alias;
|
||||
public final Sql_join_itm[] Join_flds;
|
||||
public boolean Db_enabled = true;
|
||||
public final Sql_join_fld[] Join_flds;
|
||||
|
||||
public static final String Alias__null = String_.Null;
|
||||
public static final String Db__null = String_.Null;
|
||||
|
||||
@@ -17,9 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Sql_where_itm {
|
||||
public class Sql_where_clause {
|
||||
public Criteria Root = Criteria_.All;
|
||||
public static final Object Where__null = null;
|
||||
|
||||
public static final Sql_where_itm All = new Sql_where_itm();
|
||||
public static final Sql_where_clause All = new Sql_where_clause();
|
||||
}
|
||||
@@ -68,13 +68,13 @@ public class Sql_core_wtr implements Sql_qry_wtr {
|
||||
int last = arg_count - 1;
|
||||
bfr.Add_str_u8_many("INSERT INTO ", qry.Base_table(), " (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = qry.Args().Get_at(i);
|
||||
Keyval pair = qry.Args().Get_at(i);
|
||||
this.Bld_col_name(bfr, pair.Key());
|
||||
bfr.Add_str_a7(i == last ? ")" : ", ");
|
||||
}
|
||||
bfr.Add_str_a7(" VALUES (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = qry.Args().Get_at(i);
|
||||
Keyval pair = qry.Args().Get_at(i);
|
||||
Db_arg arg = (Db_arg)pair.Val();
|
||||
val_wtr.Bld_val(bfr, ctx, arg.Val);
|
||||
bfr.Add_str_a7(i == last ? ")" : ", ");
|
||||
@@ -85,7 +85,7 @@ public class Sql_core_wtr implements Sql_qry_wtr {
|
||||
int arg_count = qry.Args().Count(); if (arg_count == 0) throw Err_.new_wo_type("Db_qry_update has no columns", "base_table", qry.Base_table());
|
||||
bfr.Add_str_u8_many("UPDATE ", qry.Base_table(), " SET ");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = qry.Args().Get_at(i);
|
||||
Keyval pair = qry.Args().Get_at(i);
|
||||
if (i > 0) bfr.Add_str_a7(", ");
|
||||
this.Bld_col_name(bfr, pair.Key());
|
||||
bfr.Add_str_a7("=");
|
||||
|
||||
@@ -18,20 +18,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.dbs.sqls.itms.*;
|
||||
public class Sql_from_wtr {
|
||||
public void Bld_clause_from(Bry_bfr bfr, Sql_from_itm from) {
|
||||
public void Bld_clause_from(Bry_bfr bfr, Sql_from_clause from) {
|
||||
List_adp tbls = from.Tbls; int tbls_len = tbls.Count();
|
||||
for (int i = 0; i < tbls_len; ++i) {
|
||||
Sql_tbl_itm tbl = (Sql_tbl_itm)tbls.Get_at(i);
|
||||
bfr.Add_byte_space().Add_str_a7(Bld_join(tbl.Join_tid)).Add_byte_space();
|
||||
if (tbl.Db != Sql_tbl_itm.Db__null)
|
||||
if (tbl.Db != Sql_tbl_itm.Db__null && tbl.Db_enabled)
|
||||
bfr.Add_str_u8(tbl.Db).Add_byte_dot();
|
||||
bfr.Add_str_u8(tbl.Name);
|
||||
if (tbl.Alias != Sql_tbl_itm.Alias__null)
|
||||
bfr.Add_byte_space().Add_str_u8(tbl.Alias);
|
||||
String tbl_alias = tbl.Alias == null ? tbl.Name : tbl.Alias;
|
||||
Sql_join_itm[] flds = tbl.Join_flds; int flds_len = flds.length;
|
||||
Sql_join_fld[] flds = tbl.Join_flds; int flds_len = flds.length;
|
||||
for (int j = 0; j < flds_len; ++j) {
|
||||
Sql_join_itm join_fld = flds[j];
|
||||
Sql_join_fld join_fld = flds[j];
|
||||
bfr.Add_str_a7(j == 0 ? " ON " : " AND ");
|
||||
bfr.Add_str_u8(join_fld.Src_tbl).Add_byte_dot().Add_str_u8(join_fld.Src_fld);
|
||||
bfr.Add(Bry__join_eq);
|
||||
|
||||
@@ -34,6 +34,7 @@ public class Sql_schema_wtr {
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
Dbmeta_idx_fld fld = flds[i];
|
||||
if (fld == null) continue; // fld will be null when tbl has Dbmetafld.Key_null (to support obsoleted schemas)
|
||||
if (i != 0) tmp_bfr.Add_str_a7(", ");
|
||||
tmp_bfr.Add_str_a7(fld.Name);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public class Sql_select_wtr {
|
||||
for (int i = 0; i < flds_len; i++) {
|
||||
Sql_select_fld fld = (Sql_select_fld)flds.Get_at(i);
|
||||
if (i > 0) bfr.Add_str_a7(", ");
|
||||
qry_wtr.Bld_col_name(bfr, fld.To_sql());
|
||||
qry_wtr.Bld_col_name(bfr, fld.To_fld_sql());
|
||||
}
|
||||
qry_wtr.From_wtr().Bld_clause_from(bfr, qry.From());
|
||||
Bld_indexed_by(bfr, ctx, qry, qry.Indexed_by());
|
||||
@@ -40,7 +40,7 @@ public class Sql_select_wtr {
|
||||
if (qry.Offset() != Db_qry__select_cmd.Offset__disabled)
|
||||
Bld_offset(bfr, ctx, qry, qry.Offset());
|
||||
}
|
||||
private void Bld_select_group_by(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, Sql_group_itm groupBy) {
|
||||
private void Bld_select_group_by(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, Sql_group_clause groupBy) {
|
||||
if (groupBy == null) return;
|
||||
bfr.Add_str_a7(" GROUP BY ");
|
||||
for (int i = 0; i < groupBy.Flds().Count(); i++) {
|
||||
@@ -49,12 +49,12 @@ public class Sql_select_wtr {
|
||||
bfr.Add_str_a7(item);
|
||||
}
|
||||
}
|
||||
private void Bld_select_order_by(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, Sql_order_itm orderBy) {
|
||||
private void Bld_select_order_by(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, Sql_order_clause orderBy) {
|
||||
if (orderBy == null) return;
|
||||
bfr.Add_str_a7(" ORDER BY ");
|
||||
int len = orderBy.Flds.length;
|
||||
int len = orderBy.Flds().length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Sql_order_fld item = orderBy.Flds[i];
|
||||
Sql_order_fld item = orderBy.Flds()[i];
|
||||
if (i > 0) bfr.Add_str_a7(", ");
|
||||
bfr.Add_str_a7(item.To_sql());
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ public class Sql_where_wtr {
|
||||
private final Sql_core_wtr qry_wtr;
|
||||
private final Sql_val_wtr val_wtr;
|
||||
public Sql_where_wtr(Sql_core_wtr qry_wtr, Sql_val_wtr val_wtr) {this.qry_wtr = qry_wtr; this.val_wtr = val_wtr;}
|
||||
public void Bld_where(Bry_bfr bfr, Sql_wtr_ctx ctx, Sql_where_itm where_itm) {
|
||||
if (where_itm == Sql_where_itm.Where__null) return;
|
||||
public void Bld_where(Bry_bfr bfr, Sql_wtr_ctx ctx, Sql_where_clause where_itm) {
|
||||
if (where_itm == Sql_where_clause.Where__null) return;
|
||||
Bld_where(bfr, ctx, where_itm.Root);
|
||||
}
|
||||
public void Bld_where(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria crt) {
|
||||
|
||||
25
140_dbs/src/gplx/dbs/stmts/Db_stmt_arg.java
Normal file
25
140_dbs/src/gplx/dbs/stmts/Db_stmt_arg.java
Normal 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.stmts; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_stmt_arg {
|
||||
public Db_stmt_arg(boolean crt, int tid, String key, Object val) {this.Crt = crt; this.Tid = tid; this.Key = key; this.Val = val;}
|
||||
public final boolean Crt;
|
||||
public final int Tid;
|
||||
public final String Key;
|
||||
public Object Val;
|
||||
}
|
||||
65
140_dbs/src/gplx/dbs/stmts/Db_stmt_arg_list.java
Normal file
65
140_dbs/src/gplx/dbs/stmts/Db_stmt_arg_list.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
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.stmts; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_stmt_arg_list {
|
||||
private final List_adp list = List_adp_.new_();
|
||||
public void Clear() {list.Clear();}
|
||||
public int Len() {return list.Len();}
|
||||
public Db_stmt_arg Get_at(int i) {return (Db_stmt_arg)list.Get_at(i);}
|
||||
public Db_stmt_arg_list Crt_int (String key, int val) {return Add(Bool_.Y, Dbmeta_fld_tid.Tid__int, key, val);}
|
||||
public Db_stmt_arg_list Crt_str_by_bry (String key, byte[] val) {return Add(Bool_.Y, Dbmeta_fld_tid.Tid__str, key, String_.new_u8(val));}
|
||||
public Db_stmt_arg_list Crt_str (String key, String val) {return Add(Bool_.Y, Dbmeta_fld_tid.Tid__str, key, val);}
|
||||
public Db_stmt_arg_list Add(boolean crt, int tid, String key, Object val) {list.Add(new Db_stmt_arg(crt, tid, key, val)); return this;}
|
||||
public void Fill(Db_stmt stmt) {
|
||||
int len = list.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_stmt_arg arg = (Db_stmt_arg)list.Get_at(i);
|
||||
if (arg.Crt)
|
||||
Fill_crt(stmt, arg.Tid, arg.Key, arg.Val);
|
||||
else
|
||||
Fill_val(stmt, arg.Tid, arg.Key, arg.Val);
|
||||
}
|
||||
list.Clear();
|
||||
}
|
||||
private static void Fill_crt(Db_stmt stmt, int tid, String key, Object val) {
|
||||
switch (tid) {
|
||||
case Dbmeta_fld_tid.Tid__bool: stmt.Crt_bool_as_byte (key, Bool_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__byte: stmt.Crt_byte (key, Byte_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__int: stmt.Crt_int (key, Int_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__long: stmt.Crt_long (key, Long_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__float: stmt.Crt_float (key, Float_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__double: stmt.Crt_double (key, Double_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__str: stmt.Crt_str (key, String_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__bry: stmt.Crt_bry (key, Bry_.cast(val)); break;
|
||||
default: throw Err_.new_unhandled_default(tid);
|
||||
}
|
||||
}
|
||||
private static void Fill_val(Db_stmt stmt, int tid, String key, Object val) {
|
||||
switch (tid) {
|
||||
case Dbmeta_fld_tid.Tid__bool: stmt.Val_bool_as_byte (key, Bool_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__byte: stmt.Val_byte (key, Byte_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__int: stmt.Val_int (key, Int_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__long: stmt.Val_long (key, Long_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__float: stmt.Val_float (key, Float_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__double: stmt.Val_double (key, Double_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__str: stmt.Val_str (key, String_.cast(val)); break;
|
||||
case Dbmeta_fld_tid.Tid__bry: stmt.Val_bry (key, Bry_.cast(val)); break;
|
||||
default: throw Err_.new_unhandled_default(tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
140_dbs/src/gplx/dbs/stmts/Db_stmt_mgr.java
Normal file
53
140_dbs/src/gplx/dbs/stmts/Db_stmt_mgr.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
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.stmts; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_stmt_mgr {
|
||||
private final List_adp fmt_list = List_adp_.new_();
|
||||
private final Db_stmt_arg_list arg_list = new Db_stmt_arg_list();
|
||||
public boolean Mode_is_stmt() {return mode_is_stmt;} public Db_stmt_mgr Mode_is_stmt_(boolean v) {mode_is_stmt = v; return this;} private boolean mode_is_stmt = true;
|
||||
public void Clear() {arg_list.Clear(); fmt_list.Clear(); bfr.Clear();}
|
||||
public Bry_bfr Bfr() {return bfr;} private final Bry_bfr bfr = Bry_bfr.new_();
|
||||
public void Add_var_many(Object... ary) {
|
||||
for (Object o : ary)
|
||||
fmt_list.Add(o);
|
||||
}
|
||||
public void Add_crt_str(String key, String val) {
|
||||
fmt_list.Add(mode_is_stmt ? stmt_arg_placeholder : gplx.dbs.sqls.Sql_qry_wtr_.Quote_arg(val));
|
||||
arg_list.Add(Bool_.Y, Dbmeta_fld_tid.Tid__str, key, val);
|
||||
}
|
||||
public void Add_crt_int(String key, int val) {
|
||||
fmt_list.Add(mode_is_stmt ? stmt_arg_placeholder : Int_.To_str(val));
|
||||
arg_list.Add(Bool_.Y, Dbmeta_fld_tid.Tid__int, key, val);
|
||||
}
|
||||
public void Write_fmt(Bry_fmt fmt) {
|
||||
fmt.Bld_many(bfr, (Object[])fmt_list.To_ary_and_clear(Object.class));
|
||||
}
|
||||
public String Make_sql(Bry_fmt fmt) { // should only be called publicly for debugging purposes
|
||||
Write_fmt(fmt);
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
public Db_stmt Make_stmt(Db_conn conn, Bry_fmt fmt) {
|
||||
return conn.Stmt_sql(Make_sql(fmt));
|
||||
}
|
||||
public void Fill_stmt_and_clear(Db_stmt stmt) {
|
||||
stmt.Clear();
|
||||
arg_list.Fill(stmt);
|
||||
fmt_list.Clear(); // NOTE: also clear fmt_list, since Fill_stmt can be called without calling Make_sql / Make_stmt
|
||||
}
|
||||
private static final String stmt_arg_placeholder = "?";
|
||||
}
|
||||
@@ -27,7 +27,7 @@ class Db_sys_tbl implements Rls_able {
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds));}
|
||||
public void Create_tbl() {conn.Meta_tbl_create(Dbmeta_tbl_itm.New(tbl_name, flds));}
|
||||
public int Assert_int_or(String key, int or) {
|
||||
String rv = Assert_str_or(key, Int_.To_str(or));
|
||||
try {return Int_.parse(rv);}
|
||||
|
||||
Reference in New Issue
Block a user