mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.2.1.1
This commit is contained in:
@@ -25,10 +25,15 @@ public class Db_conn {
|
||||
}
|
||||
public Db_url Url() {return engine.Url();}
|
||||
public Db_txn_mgr Txn_mgr() {return txn_mgr;} private final Db_txn_mgr txn_mgr;
|
||||
public Db_stmt New_stmt_insert(String tbl, Db_meta_fld_list flds) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, flds.To_str_ary()));}
|
||||
public Db_stmt New_stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt New_stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, cols));}
|
||||
public Db_stmt New_stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt New_stmt_select_all_where(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols));}
|
||||
public Db_stmt New_stmt_select_all_where(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary()));}
|
||||
public Db_stmt New_stmt_update_by_meta(String tbl, Db_meta_fld_list flds, String... where) {
|
||||
return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, flds.To_str_ary_exclude(where)));
|
||||
}
|
||||
public void Itms_add(Db_conn_itm itm) {itm_list.Add(itm);}
|
||||
public void Itms_del(Db_conn_itm itm) {itm_list.Del(itm);}
|
||||
public void Conn_term() {
|
||||
|
||||
@@ -16,6 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.primitives.*;
|
||||
public interface Db_conn_mkr {
|
||||
Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref);
|
||||
}
|
||||
|
||||
@@ -18,24 +18,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.mems.*;
|
||||
public class Db_conn_pool {
|
||||
private final HashAdp dbm_hash = HashAdp_.new_(); private final HashAdp engine_hash = HashAdp_.new_();
|
||||
private final HashAdp conn_hash = HashAdp_.new_(); private final HashAdp engine_hash = HashAdp_.new_();
|
||||
Db_conn_pool() {this.Init();}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_url_.sqlite_(url));}
|
||||
public Db_conn Get_or_new(Db_url url) {
|
||||
Db_conn rv = (Db_conn)dbm_hash.Fetch(url.Xto_api());
|
||||
Db_conn rv = (Db_conn)conn_hash.Fetch(url.Xto_api());
|
||||
if (rv == null) {
|
||||
Db_engine prime = (Db_engine)engine_hash.Fetch(url.Tid()); if (prime == null) throw Err_.new_("db engine prototype not found; tid={0}", url.Tid());
|
||||
Db_engine clone = prime.New_clone(url);
|
||||
rv = new Db_conn(clone);
|
||||
dbm_hash.Add(url.Xto_api(), rv);
|
||||
conn_hash.Add(url.Xto_api(), rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Set_mem(String db, Db_meta_tbl... tbls) {
|
||||
public Db_conn Set_mem(String db, Db_meta_tbl... tbls) {
|
||||
Db_url url = Db_url__mem.new_(db);
|
||||
Db_engine__mem engine = new Db_engine__mem(url, tbls);
|
||||
Db_conn dbm = new Db_conn(engine);
|
||||
dbm_hash.AddReplace(url.Xto_api(), dbm);
|
||||
Db_conn conn = new Db_conn(engine);
|
||||
conn_hash.AddReplace(url.Xto_api(), conn);
|
||||
return conn;
|
||||
}
|
||||
private void Init() {
|
||||
this.Engines__add(Db_engine_null._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
|
||||
@@ -28,4 +28,5 @@ public class Db_meta_fld {
|
||||
public boolean Primary() {return primary;} private final boolean primary;
|
||||
public boolean Autoincrement() {return autoincrement;} private final boolean autoincrement;
|
||||
public static final int Tid_bool = 0, Tid_byte = 1, Tid_short = 2, Tid_int = 3, Tid_long = 4, Tid_float = 5, Tid_double = 6, Tid_str = 7, Tid_text = 8, Tid_bry = 9;
|
||||
public static final String[] Ary_empy = String_.Ary_empty;
|
||||
}
|
||||
|
||||
@@ -20,8 +20,28 @@ public class Db_meta_fld_list {
|
||||
private final OrderedHash flds = OrderedHash_.new_();
|
||||
private final ListAdp keys = ListAdp_.new_();
|
||||
public Db_meta_fld Get_by(String name) {return (Db_meta_fld)flds.Fetch(name);}
|
||||
public String[] Xto_str_ary() {if (str_ary == null) str_ary = (String[])keys.Xto_ary(String.class); return str_ary;} private String[] str_ary;
|
||||
public Db_meta_fld[] Xto_fld_ary() {if (fld_ary == null) fld_ary = (Db_meta_fld[])flds.Xto_ary(Db_meta_fld.class); return fld_ary;} private Db_meta_fld[] fld_ary;
|
||||
public String[] To_str_ary() {if (str_ary == null) str_ary = (String[])keys.Xto_ary(String.class); return str_ary;} private String[] str_ary;
|
||||
public Db_meta_fld[] To_fld_ary() {if (fld_ary == null) fld_ary = (Db_meta_fld[])flds.Xto_ary(Db_meta_fld.class); return fld_ary;} private Db_meta_fld[] fld_ary;
|
||||
public String[] To_str_ary_exclude(String[] ary) {
|
||||
ListAdp rv = ListAdp_.new_();
|
||||
int ary_len = ary.length;
|
||||
int fld_len = flds.Count();
|
||||
for (int i = 0; i < fld_len; ++i) {
|
||||
Db_meta_fld fld = (Db_meta_fld)flds.FetchAt(i);
|
||||
String fld_key = fld.Name();
|
||||
boolean include = true;
|
||||
for (int j = 0; j < ary_len; ++j) {
|
||||
String itm_key = ary[j];
|
||||
if (String_.Eq(fld_key, itm_key)) {
|
||||
include = false;
|
||||
break;
|
||||
}
|
||||
if (include)
|
||||
rv.Add(itm_key);
|
||||
}
|
||||
}
|
||||
return rv.XtoStrAry();
|
||||
}
|
||||
public String Add_bool(String name) {return Add(name, Db_meta_fld.Tid_bool, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_byte(String name) {return Add(name, Db_meta_fld.Tid_byte, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_short(String name) {return Add(name, Db_meta_fld.Tid_short, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
|
||||
@@ -23,6 +23,7 @@ public class Db_meta_tbl {
|
||||
public String Name() {return name;} private final String name;
|
||||
public Db_meta_fld[] Flds() {return flds;} private final Db_meta_fld[] flds;
|
||||
public Db_meta_idx[] Idxs() {return idxs;} private final Db_meta_idx[] idxs;
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld_list flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds.To_fld_ary(), idxs);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld[] flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds, idxs);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld... flds) {return new Db_meta_tbl(name, flds, null);}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ public class Db_sqlbldr_tst {
|
||||
flds.Add_str("fld_str", 123);
|
||||
flds.Add_text("fld_text");
|
||||
flds.Add_bry("fld_bry");
|
||||
fxt.Test_create_tbl(Db_meta_tbl.new_("tbl_name", flds.Xto_fld_ary())
|
||||
fxt.Test_create_tbl(Db_meta_tbl.new_("tbl_name", flds.To_fld_ary())
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "CREATE TABLE tbl_name"
|
||||
, "( fld_int_pkey int NOT NULL PRIMARY KEY"
|
||||
|
||||
@@ -30,9 +30,9 @@ public class Db_sys_regy_mgr_tst {
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt .Exec_set("grp", "key_0", "val_0")
|
||||
.Exec_set("grp", "key_1", "val_0")
|
||||
.Exec_set("grp", "key_1", "val_1")
|
||||
.Exec_del("grp", "key_1")
|
||||
.Test_get("grp", "key_0", null)
|
||||
.Test_get("grp", "key_0", "val_0")
|
||||
.Test_get("grp", "key_1", null)
|
||||
;
|
||||
}
|
||||
|
||||
@@ -28,22 +28,22 @@ class Db_sys_regy_tbl implements Db_conn_itm {
|
||||
}
|
||||
// private Db_meta_tbl meta;
|
||||
public void Insert(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.New_stmt_insert(tbl_name, Flds.Xto_str_ary());
|
||||
Db_stmt stmt = conn.New_stmt_insert(tbl_name, Flds.To_str_ary());
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Val_str(val).Exec_insert();
|
||||
}
|
||||
public void Update(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.New_stmt_update(tbl_name, String_.Ary(Fld_regy_grp, Fld_regy_key), Fld_regy_val);
|
||||
stmt.Clear().Val_str(val).Val_str(grp).Val_str(key).Exec_update();
|
||||
stmt.Clear().Val_str(val).Crt_str(Fld_regy_grp, grp).Crt_str(Fld_regy_key, key).Exec_update();
|
||||
}
|
||||
public void Delete(String grp, String key) {
|
||||
Db_stmt stmt = conn.New_stmt_delete(tbl_name, Fld_regy_grp, Fld_regy_key);
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Exec_delete();
|
||||
stmt.Clear().Crt_str(Fld_regy_grp, grp).Crt_str(Fld_regy_key, key).Exec_delete();
|
||||
}
|
||||
public String Select_val_or(String grp, String key, String or) {
|
||||
Db_stmt stmt = conn.New_stmt_select_all_where(tbl_name, Flds.Xto_str_ary(), Fld_regy_grp, Fld_regy_key);
|
||||
Db_stmt stmt = conn.New_stmt_select_all_where(tbl_name, Flds.To_str_ary(), Fld_regy_grp, Fld_regy_key);
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
try {
|
||||
rdr = stmt.Clear().Val_str(grp).Val_str(key).Exec_select_as_rdr();
|
||||
rdr = stmt.Clear().Crt_str(Fld_regy_grp, grp).Crt_str(Fld_regy_key, key).Exec_select_as_rdr();
|
||||
return rdr.Move_next() ? rdr.Read_str(Fld_regy_val) : or;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
@@ -54,8 +54,8 @@ class Db_sys_regy_tbl implements Db_conn_itm {
|
||||
, Fld_regy_val = Flds.Add_str("regy_val", 4096)
|
||||
;
|
||||
public static Db_meta_tbl new_meta(String tbl) {
|
||||
return Db_meta_tbl.new_(tbl, Flds.Xto_fld_ary()
|
||||
, Db_meta_idx.new_unique(tbl, "key", Flds.Xto_str_ary())
|
||||
return Db_meta_tbl.new_(tbl, Flds.To_fld_ary()
|
||||
, Db_meta_idx.new_unique(tbl, "key", Flds.To_str_ary())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +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; import gplx.*;
|
||||
import gplx.dbs.engines.mems.*;
|
||||
import gplx.dbs.engines.mems.*; import gplx.dbs.engines.sqlite.*;
|
||||
public class Db_url_ {
|
||||
public static final Db_url Null = Db_url__null._;
|
||||
public static final Db_url Test = Db_url__mysql.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static Db_url parse_(String raw) {return Db_url_pool._.Parse(raw);}
|
||||
public static Db_url parse_(String raw) {return Db_url_pool._.Parse(raw);}
|
||||
public static Db_url sqlite_(Io_url url) {return Db_url__sqlite.load_(url);}
|
||||
public static Db_url tdb_(Io_url url) {return Db_url__tdb.new_(url);}
|
||||
public static Db_url mem_(String db) {return Db_url__mem.new_(db);}
|
||||
|
||||
@@ -16,6 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.strings.*;
|
||||
public abstract class Db_url__base implements Db_url {
|
||||
public abstract String Tid();
|
||||
public String Xto_raw() {return raw;} private String raw = "";
|
||||
|
||||
@@ -16,6 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.strings.*;
|
||||
public class Sql_join_itm {
|
||||
public String SrcTbl() {return srcTbl;} public Sql_join_itm SrcTbl_(String v) {srcTbl = v; return this;} private String srcTbl;
|
||||
public String SrcFld() {return srcFld;} public Sql_join_itm SrcFld_(String v) {srcFld = v; return this;} private String srcFld;
|
||||
@@ -35,7 +36,6 @@ public class Sql_join_itm {
|
||||
class Sql_from {
|
||||
public ListAdp Tbls() {return tbls;} ListAdp tbls = ListAdp_.new_();
|
||||
public Sql_tbl_src BaseTable() {return (Sql_tbl_src)tbls.FetchAt(0);}
|
||||
|
||||
public static Sql_from new_(Sql_tbl_src baseTable) {
|
||||
Sql_from rv = new Sql_from();
|
||||
rv.tbls.Add(baseTable);
|
||||
@@ -57,11 +57,11 @@ class Sql_join_itmType {
|
||||
public String Name() {return name;} private String name;
|
||||
Sql_join_itmType(int v, String name) {this.val = v; this.name = name;}
|
||||
public static final Sql_join_itmType
|
||||
From = new Sql_join_itmType(0, "FROM")
|
||||
, Inner = new Sql_join_itmType(1, "INNER JOIN")
|
||||
, Left = new Sql_join_itmType(2, "LEFT JOIN")
|
||||
, Right = new Sql_join_itmType(3, "RIGHT JOIN")
|
||||
, Outer = new Sql_join_itmType(4, "OUTER JOIN")
|
||||
, Cross = new Sql_join_itmType(5, "CROSS JOIN")
|
||||
;
|
||||
From = new Sql_join_itmType(0, "FROM")
|
||||
, Inner = new Sql_join_itmType(1, "INNER JOIN")
|
||||
, Left = new Sql_join_itmType(2, "LEFT JOIN")
|
||||
, Right = new Sql_join_itmType(3, "RIGHT JOIN")
|
||||
, Outer = new Sql_join_itmType(4, "OUTER JOIN")
|
||||
, Cross = new Sql_join_itmType(5, "CROSS JOIN")
|
||||
;
|
||||
}
|
||||
|
||||
@@ -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; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.core.criterias.*;
|
||||
public interface Sql_qry_wtr {
|
||||
String Xto_str(Db_qry qry, boolean prepare);
|
||||
}
|
||||
|
||||
@@ -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; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.core.strings.*; import gplx.core.criterias.*;
|
||||
class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
private final String_bldr sb = String_bldr_.new_();
|
||||
private boolean prepare = false;
|
||||
@@ -34,7 +34,7 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
}
|
||||
private String Bld_qry_delete(Db_qry_delete cmd) {
|
||||
sb.Add_many("DELETE FROM ", cmd.Base_table());
|
||||
Bld_where2(sb, cmd.Where());
|
||||
Bld_where(sb, cmd.Where());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_insert(Db_qry_insert cmd) {
|
||||
@@ -53,14 +53,14 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
this.Xto_sql_col(sb, pair.Key_as_obj());
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
sb.Add(" VALUES (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
Db_arg prm = (Db_arg)pair.Val();
|
||||
this.Bld_val(sb, prm);
|
||||
Db_arg arg = (Db_arg)pair.Val();
|
||||
this.Bld_val(sb, arg);
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
return sb.Xto_str_and_clear();
|
||||
@@ -71,7 +71,7 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
this.Xto_sql_col(sb, pair.Key_as_obj());
|
||||
sb.Add("=");
|
||||
this.Bld_val(sb, (Db_arg)pair.Val());
|
||||
}
|
||||
@@ -86,7 +86,7 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
for (int i = 0; i < flds.Count(); i++) {
|
||||
Sql_select_fld_base fld = (Sql_select_fld_base)flds.FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, fld.XtoSql());
|
||||
this.Xto_sql_col(sb, fld.XtoSql());
|
||||
}
|
||||
Bld_clause_from(sb, cmd.From());
|
||||
Bld_where(sb, cmd.Where());
|
||||
@@ -127,13 +127,11 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
for (int i = 0; i < tbl.JoinLinks().Count(); i++) {
|
||||
Sql_join_itm joinLink = (Sql_join_itm)tbl.JoinLinks().FetchAt(i);
|
||||
String conjunction = i == 0 ? " ON " : " AND ";
|
||||
sb.Add_many
|
||||
( conjunction, joinLink.SrcTbl(), ".", joinLink.SrcFld(), "=", tblAliasForJoin, ".", joinLink.TrgFldOrSrcFld()
|
||||
);
|
||||
sb.Add_many(conjunction, joinLink.SrcTbl(), ".", joinLink.SrcFld(), "=", tblAliasForJoin, ".", joinLink.TrgFldOrSrcFld());
|
||||
}
|
||||
}
|
||||
}
|
||||
private void XtoSqlCol(String_bldr sb, Object obj) {
|
||||
private void Xto_sql_col(String_bldr sb, Object obj) {
|
||||
if (obj == null) throw Err_.null_("ColName");
|
||||
sb.Add_obj(obj); // FIXME: options for bracketing; ex: [name]
|
||||
}
|
||||
@@ -167,43 +165,50 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
Bld_val_str(sb, arg, valString);
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_str(String_bldr sb, Db_arg prm, String s) {
|
||||
@gplx.Virtual public void Bld_val_str(String_bldr sb, Db_arg arg, String s) {
|
||||
sb.Add_many("'", String_.Replace(s, "'", "''"), "'"); // stupid escaping of '
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_date(String_bldr sb, Db_arg prm, DateAdp s) {
|
||||
@gplx.Virtual public void Bld_val_date(String_bldr sb, Db_arg arg, DateAdp s) {
|
||||
sb.Add_many("'", s.XtoStr_gplx_long(), "'");
|
||||
}
|
||||
private void Bld_where(String_bldr sb, Sql_where where) {
|
||||
if (where == null || where.Crt() == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where(sb, where.Crt());
|
||||
}
|
||||
private void Bld_where2(String_bldr sb, Criteria crt) {
|
||||
if (crt == null || crt == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where(sb, crt);
|
||||
}
|
||||
public void Bld_where(String_bldr sb, Criteria crt) {
|
||||
if (crt == null) return;
|
||||
if (crt.Tid() == Criteria_.Tid_wrapper) {
|
||||
Criteria_fld crt_fld = (Criteria_fld)crt;
|
||||
Criteria crt_inner = crt_fld.Crt();
|
||||
switch (crt_inner.Tid()) {
|
||||
case Criteria_.Tid_const:
|
||||
case Criteria_.Tid_not:
|
||||
case Criteria_.Tid_and:
|
||||
case Criteria_.Tid_or: crt = crt_inner; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
if (crt.Tid() == Criteria_.Tid_const) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where_val(sb, crt);
|
||||
}
|
||||
public void Bld_where_val(String_bldr sb, Criteria crt) {
|
||||
Criteria_bool_base crt_bool = Criteria_bool_base.as_(crt);
|
||||
if (crt_bool != null) {
|
||||
sb.Add("(");
|
||||
Bld_where(sb, crt_bool.Lhs());
|
||||
sb.Add_many(" ", crt_bool.OpLiteral(), " ");
|
||||
Bld_where(sb, crt_bool.Rhs());
|
||||
Bld_where_val(sb, crt_bool.Lhs());
|
||||
sb.Add_many(" ", crt_bool.Op_literal(), " ");
|
||||
Bld_where_val(sb, crt_bool.Rhs());
|
||||
sb.Add(")");
|
||||
return;
|
||||
}
|
||||
if (crt.Crt_tid() == Criteria_.Tid_db_obj_ary) {
|
||||
if (crt.Tid() == Criteria_.Tid_db_obj_ary) {
|
||||
Append_db_obj_ary(sb, (Db_obj_ary_crt)crt);
|
||||
}
|
||||
else {
|
||||
Criteria_wrapper leaf = Criteria_wrapper.as_(crt); if (leaf == null) throw Err_.invalid_op_(crt.XtoStr());
|
||||
sb.Add(leaf.Name_of_GfoFldCrt());
|
||||
Bld_where_crt(sb, leaf.Crt_of_GfoFldCrt());
|
||||
Criteria_fld leaf = Criteria_fld.as_(crt); if (leaf == null) throw Err_.invalid_op_(crt.XtoStr());
|
||||
sb.Add(leaf.Key());
|
||||
Bld_where_crt(sb, leaf.Crt());
|
||||
}
|
||||
}
|
||||
private void Bld_where_crt(String_bldr sb, Criteria crt) {
|
||||
switch (crt.Crt_tid()) {
|
||||
switch (crt.Tid()) {
|
||||
case Criteria_.Tid_eq: Bld_where_eq(sb, Criteria_eq.as_(crt)); break;
|
||||
case Criteria_.Tid_comp: Bld_where_comp(sb, Criteria_comp.as_(crt)); break;
|
||||
case Criteria_.Tid_between: Bld_where_between(sb, Criteria_between.as_(crt)); break;
|
||||
@@ -215,11 +220,11 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
}
|
||||
private void Bld_where_eq(String_bldr sb, Criteria_eq crt) {
|
||||
sb.Add(crt.Negated() ? "!=" : "=");
|
||||
this.Bld_val(sb, Wrap(crt.Value()));
|
||||
this.Bld_val(sb, Wrap(crt.Val()));
|
||||
}
|
||||
private void Bld_where_comp(String_bldr sb, Criteria_comp crt) {
|
||||
sb.Add_many(crt.XtoSymbol());
|
||||
this.Bld_val(sb, Wrap(crt.Value()));
|
||||
this.Bld_val(sb, Wrap(crt.Val()));
|
||||
}
|
||||
private void Bld_where_between(String_bldr sb, Criteria_between crt) {
|
||||
sb.Add(crt.Negated() ? " NOT BETWEEN " : " BETWEEN ");
|
||||
@@ -234,7 +239,7 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
}
|
||||
private void Bld_where_in(String_bldr sb, Criteria_in crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IN (" : " IN (");
|
||||
Object[] crt_vals = crt.Values();
|
||||
Object[] crt_vals = crt.Val_as_obj_ary();
|
||||
int len = crt_vals.length;
|
||||
int last = len - 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
@@ -279,8 +284,8 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
}
|
||||
private Db_arg Wrap(Object val) {return new Db_arg("unknown", val);}
|
||||
}
|
||||
class Sql_qry_wtr_ansi_escape_backslash extends Sql_qry_wtr_ansi { @Override public void Bld_val_str(String_bldr sb, Db_arg prm, String s) {
|
||||
class Sql_qry_wtr_ansi_escape_backslash extends Sql_qry_wtr_ansi { @Override public void Bld_val_str(String_bldr sb, Db_arg arg, String s) {
|
||||
if (String_.Has(s, "\\")) s = String_.Replace(s, "\\", "\\\\");
|
||||
super.Bld_val_str(sb, prm, s);
|
||||
super.Bld_val_str(sb, arg, s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
import org.junit.*; import gplx.core.strings.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Sql_qry_wtr_tst {
|
||||
private final Sql_qry_wtr_fxt fxt = new Sql_qry_wtr_fxt();
|
||||
@Test public void Val() {
|
||||
@@ -55,7 +55,7 @@ class Sql_qry_wtr_fxt {
|
||||
}
|
||||
public void Test_where(Criteria crt, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sql_wtr.Bld_where(sb, crt);
|
||||
sql_wtr.Bld_where_val(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.strings.*;
|
||||
class Sql_select {
|
||||
public Sql_select_fld_list Flds() {return flds;} Sql_select_fld_list flds = Sql_select_fld_list.new_();
|
||||
public boolean Distinct() {return distinct;} public void Distinct_set(boolean v) {distinct = v;} private boolean distinct;
|
||||
@@ -130,6 +131,15 @@ class Sql_select_fld_list {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String[] To_str_ary() {
|
||||
int len = this.Count();
|
||||
String[] rv = new String[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
rv[i] = fld.Fld();
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String XtoStr() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
|
||||
@@ -1,32 +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; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
class Sql_where {
|
||||
public Criteria Crt() {return crt;} Criteria crt;
|
||||
public static Sql_where merge_or_new_(Sql_where where, Criteria crt) {
|
||||
return where == null
|
||||
? Sql_where.new_(crt)
|
||||
: Sql_where.new_(Criteria_.And(where.Crt(), crt));
|
||||
}
|
||||
public static Sql_where new_(Criteria crt) {
|
||||
Sql_where rv = new Sql_where();
|
||||
rv.crt = crt;
|
||||
return rv;
|
||||
} Sql_where() {}
|
||||
}
|
||||
@@ -17,15 +17,17 @@ 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_stmt__mem implements Db_stmt {
|
||||
private int val_idx;
|
||||
private final ListAdp val_list = ListAdp_.new_(); private int val_idx;
|
||||
public Db_stmt__mem(Db_engine__mem engine, Db_qry qry) {this.engine = engine; this.qry = qry;} private Db_engine__mem engine;
|
||||
public int Args_len() {return args.Count();}
|
||||
public Object Args_get_at(int i) {return args.FetchAt(i);}
|
||||
public HashAdp Crts() {return crt_hash;} private final HashAdp crt_hash = HashAdp_.new_();
|
||||
public int Args_len() {return val_list.Count();}
|
||||
public Object Args_get_at(int i) {return val_list.FetchAt(i);}
|
||||
public Db_qry Qry() {return qry;} private Db_qry qry;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
args.Clear();
|
||||
val_list.Clear();
|
||||
crt_hash.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
@@ -39,49 +41,49 @@ public class Db_stmt__mem implements Db_stmt {
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
@@ -92,14 +94,14 @@ public class Db_stmt__mem implements Db_stmt {
|
||||
public Db_stmt Val_str(String k, String v) {return Add_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
gplx.ios.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(++val_idx, bfr.Xto_str_and_clear());
|
||||
Add(++val_idx, "", Bool_.N, bfr.Xto_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
@@ -123,5 +125,15 @@ public class Db_stmt__mem implements Db_stmt {
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec select_val: tbl={0}", qry.Base_table());}
|
||||
}
|
||||
private void Add(int idx, Object v) {args.Add(v);} private ListAdp args = ListAdp_.new_();
|
||||
private void Add(int idx, String k, boolean where, Object v) {
|
||||
val_list.Add(v);
|
||||
if (where) {
|
||||
ListAdp list = (ListAdp)crt_hash.Fetch(k);
|
||||
if (list == null) {
|
||||
list = ListAdp_.new_();
|
||||
crt_hash.Add(k, list);
|
||||
}
|
||||
list.Add(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Mem_tbl {
|
||||
private final ListAdp rows = ListAdp_.new_(); private final ListAdp where_rows = ListAdp_.new_();
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
@@ -30,9 +30,10 @@ public class Mem_tbl {
|
||||
}
|
||||
public int Update(Db_stmt__mem stmt) {
|
||||
Db_qry_update qry = (Db_qry_update)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Cols_where());
|
||||
qry.Where().Val_from_args(stmt.Crts());
|
||||
Select_rows_where(where_rows, stmt, qry.Where());
|
||||
int where_rows_len = where_rows.Count();
|
||||
String[] update_cols = qry.Cols_update(); int update_cols_len = update_cols.length;
|
||||
String[] update_cols = qry.Cols_for_update(); int update_cols_len = update_cols.length;
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)where_rows.FetchAt(i);
|
||||
for (int j = 0; j < update_cols_len; ++j)
|
||||
@@ -42,7 +43,8 @@ public class Mem_tbl {
|
||||
}
|
||||
public int Delete(Db_stmt__mem stmt) {
|
||||
Db_qry_delete qry = (Db_qry_delete)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Where_cols());
|
||||
qry.Where().Val_from_args(stmt.Crts());
|
||||
Select_rows_where(where_rows, stmt, qry.Where());
|
||||
int where_rows_len = where_rows.Count();
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)where_rows.FetchAt(i);
|
||||
@@ -51,41 +53,28 @@ public class Mem_tbl {
|
||||
return where_rows_len;
|
||||
}
|
||||
public Db_rdr Select(Db_stmt__mem stmt) {
|
||||
Db_qry__select_in_tbl qry = (Db_qry__select_in_tbl)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Where_flds());
|
||||
return new Db_rdr__mem(qry.Select_flds(), (Mem_itm[])where_rows.Xto_ary_and_clear(Mem_itm.class));
|
||||
String[] select = null; Criteria where = null;
|
||||
Db_qry__select_in_tbl qry = Db_qry__select_in_tbl.as_(stmt.Qry());
|
||||
if (qry == null) {
|
||||
Db_qry_select qry2 = (Db_qry_select)stmt.Qry();
|
||||
select = qry2.Cols_ary();
|
||||
where = qry2.Where();
|
||||
}
|
||||
else {
|
||||
select = qry.Select_flds();
|
||||
where = qry.Where();
|
||||
}
|
||||
where.Val_from_args(stmt.Crts());
|
||||
Select_rows_where(where_rows, stmt, where);
|
||||
return new Db_rdr__mem(select, (Mem_itm[])where_rows.Xto_ary_and_clear(Mem_itm.class));
|
||||
}
|
||||
private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, String[] where_cols) {
|
||||
int where_len = where_cols.length;
|
||||
KeyVal[] where_kvs = Bld_where_kvs(stmt, where_cols, where_len);
|
||||
Bld_where_rows(where_rows, where_kvs, where_len);
|
||||
}
|
||||
private KeyVal[] Bld_where_kvs(Db_stmt__mem stmt, String[] keys, int keys_len) {
|
||||
KeyVal[] rv = new KeyVal[keys_len];
|
||||
for (int i = 0; i < keys_len; ++i)
|
||||
rv[i] = KeyVal_.new_(keys[i], stmt.Args_get_at(i));
|
||||
return rv;
|
||||
}
|
||||
private void Bld_where_rows(ListAdp rv, KeyVal[] where_kvs, int where_len) {
|
||||
private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, Criteria crt) {
|
||||
rv.Clear();
|
||||
int rows_len = rows.Count();
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)rows.FetchAt(i);
|
||||
for (int j = 0; j < where_len; ++j) {
|
||||
KeyVal kv = where_kvs[j];
|
||||
Object itm_val = itm.Get_by(kv.Key());
|
||||
if (!Object_.Eq(itm_val, kv.Val())) break;
|
||||
}
|
||||
rv.Add(itm);
|
||||
if (crt.Matches(itm))
|
||||
rv.Add(itm);
|
||||
}
|
||||
}
|
||||
// private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, Criteria crt) {
|
||||
// rv.Clear();
|
||||
// int rows_len = rows.Count();
|
||||
// for (int i = 0; i < rows_len; ++i) {
|
||||
// Mem_itm itm = (Mem_itm)rows.FetchAt(i);
|
||||
// if (crt.Matches(itm))
|
||||
// rv.Add(itm);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ 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.*;
|
||||
package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_url__sqlite extends Db_url__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;
|
||||
Reference in New Issue
Block a user