1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-02-01 15:11:16 -05:00
parent 5efed51da9
commit 3df6db4b7b
410 changed files with 1577 additions and 1150 deletions

View File

@@ -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() {

View File

@@ -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);
}

View File

@@ -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._);

View File

@@ -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;
}

View File

@@ -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);}

View File

@@ -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);}
}

View File

@@ -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"

View File

@@ -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)
;
}

View File

@@ -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())
);
}
}

View File

@@ -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);}

View File

@@ -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 = "";

View File

@@ -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")
;
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}

View File

@@ -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++) {

View File

@@ -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() {}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
// }
// }
}

View File

@@ -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;

View File

@@ -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 class Db_qry_ {
public static Db_qry_select select_cols_(String tbl, Criteria crt, String... cols){return select_().From_(tbl).Where_(crt).Cols_(cols);}
public static Db_qry_select select_val_(String tbl, String col, Criteria crt) {return select_().From_(tbl).Where_(crt).Cols_(col);}

View File

@@ -16,16 +16,17 @@ 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.*; import gplx.core.criterias.*;
public class Db_qry__select_in_tbl implements Db_qry {
public Db_qry__select_in_tbl(String tbl_name, String[] select_flds, String[] where_flds, String group_by_sql, String having_sql, String order_by_sql, String limit_sql) {
this.tbl_name = tbl_name; this.select_flds = select_flds; this.where_flds = where_flds; this.group_by_sql = group_by_sql; this.having_sql = having_sql; this.order_by_sql = order_by_sql; this.limit_sql = limit_sql;
public Db_qry__select_in_tbl(String base_table, String[] select_flds, String[] where_flds, String group_by_sql, String having_sql, String order_by_sql, String limit_sql) {
this.base_table = base_table; this.select_flds = select_flds; this.where_flds = where_flds; this.group_by_sql = group_by_sql; this.having_sql = having_sql; this.order_by_sql = order_by_sql; this.limit_sql = limit_sql;
}
public int Tid() {return Db_qry_.Tid_select_in_tbl;}
public boolean Exec_is_rdr() {return true;}
public String Base_table() {return tbl_name;} private final String tbl_name;
public String From() {return tbl_name;}
public String[] Select_flds() {return select_flds;} private final String[] select_flds;
public String[] Where_flds() {return where_flds;} private final String[] where_flds;
public String Base_table() {return base_table;} private final String base_table;
public Criteria Where() {return where;} private Criteria where;
public String[] Select_flds() {return select_flds;} private final String[] select_flds;
private final String[] where_flds;
public void Where_sql(String_bldr sb) {
if (where_flds == null) return;
int where_flds_len = where_flds.length;
@@ -48,17 +49,19 @@ public class Db_qry__select_in_tbl implements Db_qry {
if (i != 0) sb.Add(",");
sb.Add(select_flds[i]);
}
sb.Add(" FROM ").Add(tbl_name);
if (where_flds != null) {sb.Add(" WHERE "); Where_sql(sb);}
sb.Add(" FROM ").Add(base_table);
if (where_flds != null && where_flds.length != 0) {sb.Add(" WHERE "); Where_sql(sb);}
if (group_by_sql != null) sb.Add(group_by_sql);
if (having_sql != null) sb.Add(having_sql);
if (order_by_sql != null) {sb.Add(" ORDER BY "); sb.Add(order_by_sql);}
if (limit_sql != null) sb.Add(limit_sql);
return sb.XtoStr();
}
public static Db_qry__select_in_tbl new_(String tbl_name, String[] where_flds, String[] select_flds) {
Db_qry__select_in_tbl rv = new Db_qry__select_in_tbl(tbl_name, select_flds, where_flds, null, null, null, null);
public static Db_qry__select_in_tbl new_(String base_table, String[] where_flds, String[] select_flds) {
Db_qry__select_in_tbl rv = new Db_qry__select_in_tbl(base_table, select_flds, where_flds, null, null, null, null);
rv.where = Db_crt_.eq_many_(where_flds);
return rv;
}
public static final String[] Where_flds__all = null;
public static Db_qry__select_in_tbl as_(Object obj) {return obj instanceof Db_qry__select_in_tbl ? (Db_qry__select_in_tbl)obj : null;}
public static final String[] Where_flds__all = String_.Ary_empty;
}

View File

@@ -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.dbs.sqls.*;
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
public class Db_qry_delete implements Db_qry {
Db_qry_delete(String base_table, Criteria where) {this.base_table = base_table; this.where = where;}
public int Tid() {return Db_qry_.Tid_delete;}
@@ -25,17 +25,7 @@ public class Db_qry_delete implements Db_qry {
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
public Criteria Where() {return where;} private final Criteria where;
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
public String[] Where_cols() {return where_cols;} private String[] where_cols;
public static Db_qry_delete new_all_(String tbl) {
Db_qry_delete rv = new_(tbl, Criteria_.All);
rv.where_cols = String_.Ary_empty;
return rv;
}
public static Db_qry_delete new_(String tbl, String... where) {
Db_qry_delete rv = Db_qry_delete.new_(tbl, Db_crt_.eq_many_(where));
rv.where_cols = where;
return rv;
}
public static Db_qry_delete new_(String tbl, Criteria where) {return new Db_qry_delete(tbl, where);}
public static Db_qry_delete new_all_(String tbl) {return new Db_qry_delete(tbl, Criteria_.All);}
public static Db_qry_delete new_(String tbl, String... where) {return new Db_qry_delete(tbl, Db_crt_.eq_many_(where));}
public static Db_qry_delete new_(String tbl, Criteria where) {return new Db_qry_delete(tbl, where);}
}

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs; import gplx.*;
import org.junit.*;
import gplx.criterias.*;
import gplx.core.criterias.*;
public class Db_qry_dml_tst {
@Test public void Delete_basic() {
tst_XtoSql(Db_qry_delete.new_("tbl0", Db_crt_.eq_("fld0", "val0"))

View File

@@ -21,10 +21,12 @@ public class Db_qry_insert implements Db_qry_arg_owner {
public Db_qry_insert(String base_table) {this.base_table = base_table;}
public int Tid() {return Db_qry_.Tid_insert;}
public boolean Exec_is_rdr() {return false;}
public String Base_table() {return base_table;} private String base_table;
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
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_qry_arg_owner From_(String tbl) {base_table = tbl; return this;}
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.Xto_decimal(), Db_val_type.Tid_decimal);}
public Db_qry_arg_owner Arg_(String k, DateAdp v) {return Arg_obj_type_(k, v, Db_val_type.Tid_date);}
public Db_qry_arg_owner Arg_byte_(String k, byte v) {return Arg_obj_type_(k, v, Db_val_type.Tid_byte);}
@@ -49,10 +51,8 @@ public class Db_qry_insert implements Db_qry_arg_owner {
cols.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fld, fld));
return this;
}
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
@gplx.Internal protected Sql_select_fld_list Cols() {return cols;} private Sql_select_fld_list cols;
public String[] Cols_for_insert() {return cols_for_insert;} private String[] cols_for_insert;
public static Db_qry_insert new_() {return new Db_qry_insert();} Db_qry_insert() {}
public static Db_qry_insert new_(String tbl, String... keys) {
Db_qry_insert rv = Db_qry_insert.new_();

View File

@@ -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; import gplx.*;
import gplx.criterias.*; import gplx.dbs.sqls.*;
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
public class Db_qry_select implements Db_qry {
public int Tid() {return Db_qry_.Tid_select;}
public boolean Exec_is_rdr() {return true;}
public String Base_table() {return from.BaseTable().TblName();}
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
public DataRdr Exec_qry_as_rdr(Db_conn conn) {return conn.Exec_qry_as_rdr(this);}
public DataRdr Exec_qry_as_rdr(Db_conn conn) {return conn.Exec_qry_as_rdr(this);}
public GfoNde ExecRdr_nde(Db_conn conn) {
DataRdr rdr = DataRdr_.Null;
try {return GfoNde_.rdr_(Exec_qry_as_rdr(conn));} finally {rdr.Rls();}
@@ -65,6 +65,7 @@ public class Db_qry_select implements Db_qry {
}
@gplx.Internal protected Sql_select Cols() {return cols;} Sql_select cols = Sql_select.All;
public String[] Cols_ary() {return cols.Flds().To_str_ary();}
public Db_qry_select Cols_all_() {return this;}
public Db_qry_select Cols_alias_(String expr, String alias) {
if (cols == Sql_select.All) cols = Sql_select.new_();
@@ -100,7 +101,7 @@ public class Db_qry_select implements Db_qry {
return this;
}
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_select Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
public Criteria Where() {return where;} public Db_qry_select Where_(Criteria crt) {where = crt; return this;} Criteria where;
@gplx.Internal protected Sql_order_by OrderBy() {return orderBy;} Sql_order_by orderBy = null;
public Db_qry_select OrderBy_(String fieldName, boolean ascending) {
Sql_order_by_itm item = Sql_order_by_itm.new_(fieldName, ascending);

View File

@@ -16,14 +16,17 @@ 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.dbs.sqls.*;
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
public class Db_qry_update implements Db_qry_arg_owner {
public int Tid() {return Db_qry_.Tid_update;}
public boolean Exec_is_rdr() {return false;}
public String Base_table() {return baseTable;}
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
public Db_qry_arg_owner From_(String tbl) {baseTable = tbl; return this;}
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
public String Base_table() {return base_table;} private String base_table;
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_qry_arg_owner From_(String tbl) {base_table = tbl; return this;}
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.Xto_decimal(), Db_val_type.Tid_decimal);}
public Db_qry_arg_owner Arg_(String k, DateAdp v) {return Arg_obj_type_(k, v, Db_val_type.Tid_date);}
public Db_qry_arg_owner Arg_byte_(String k, byte v) {return Arg_obj_type_(k, v, Db_val_type.Tid_byte);}
@@ -40,21 +43,15 @@ public class Db_qry_update implements Db_qry_arg_owner {
}
public Db_qry_arg_owner Key_arg_(String k, int v) {return Key_arg_obj_(k, v);}
public Db_qry_arg_owner Key_arg_(String k, String v) {return Key_arg_obj_(k, v);}
Db_qry_arg_owner Key_arg_obj_(String k, Object v) {
private Db_qry_arg_owner Key_arg_obj_(String k, Object v) {
Criteria crt = Db_crt_.eq_(k, v);
where = Sql_where.merge_or_new_(where, crt);
where = where == null ? crt : Criteria_.And(where, crt);
return this;
}
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
@gplx.Internal protected String BaseTable() {return baseTable;} private String baseTable;
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} private Sql_where where;
public String[] Cols_where() {return cols_where;} private String[] cols_where;
public String[] Cols_update() {return cols_update;} private String[] cols_update;
public static Db_qry_update new_() {return new Db_qry_update();} Db_qry_update() {}
public static Db_qry_update new_(String tbl, String[] where, String... update) {
Db_qry_update rv = Db_qry_.update_(tbl, Db_crt_.eq_many_(where));
rv.cols_update = update;
rv.cols_where = where;
rv.cols_for_update = update;
int len = update.length;
for (int i = 0; i < len; i++)
rv.Arg_obj_(update[i], null);

View File

@@ -16,13 +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.criterias.*;
public class Db_obj_ary_crt implements gplx.criterias.Criteria {
public byte Crt_tid() {return Criteria_.Tid_db_obj_ary;}
import gplx.core.criterias.*;
public class Db_obj_ary_crt implements gplx.core.criterias.Criteria {
public byte Tid() {return Criteria_.Tid_db_obj_ary;}
public Db_fld[] Flds() {return flds;} public Db_obj_ary_crt Flds_(Db_fld[] v) {this.flds = v; return this;} private Db_fld[] flds;
public Object[][] Vals() {return vals;} public void Vals_(Object[][] v) {this.vals = v;} private Object[][] vals;
public boolean Matches(Object obj) {return false;}
public String XtoStr() {return "";}
public Object[][] Vals() {return vals;} public void Vals_(Object[][] v) {this.vals = v;} private Object[][] vals;
public void Val_from_args(HashAdp args) {throw Err_.not_implemented_();}
public void Val_as_obj_(Object v) {throw Err_.not_implemented_();}
public boolean Matches(Object obj) {return false;}
public String XtoStr() {return "";}
public static Db_obj_ary_crt new_(Db_fld... flds) {return new Db_obj_ary_crt().Flds_(flds);}
public static Db_obj_ary_crt new_by_type(byte type_tid, String... names) {
int len = names.length;

View File

@@ -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 org.junit.*; import gplx.dbs.sqls.*;
import org.junit.*; import gplx.core.strings.*; import gplx.dbs.sqls.*;
public class Db_obj_ary_tst {
@Before public void init() {} private Db_obj_ary_fxt fxt = new Db_obj_ary_fxt();
@Test public void Int() {

View File

@@ -16,20 +16,33 @@ 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 class Db_crt_ {
public static final Criteria Wildcard = Criteria_.All;
public static Criteria eq_(String name, Object val) {return Criteria_wrapper.new_(name, Criteria_.eq_(val));}
public static Criteria eqn_(String name, Object val) {return Criteria_wrapper.new_(name, Criteria_.eqn_(val));}
public static Criteria lt_(String name, Comparable val) {return Criteria_wrapper.new_(name, Criteria_.lt_(val));}
public static Criteria lte_(String name, Comparable val) {return Criteria_wrapper.new_(name, Criteria_.lte_(val));}
public static Criteria mt_(String name, Comparable val) {return Criteria_wrapper.new_(name, Criteria_.mt_(val));}
public static Criteria mte_(String name, Comparable val) {return Criteria_wrapper.new_(name, Criteria_.mte_(val));}
public static Criteria between_(String name, Comparable lhs, Comparable rhs) {return Criteria_wrapper.new_(name, Criteria_.between_(lhs, rhs));}
public static Criteria in_(String name, Object... vals) {return Criteria_wrapper.new_(name, Criteria_.in_(vals));}
public static Criteria like_(String name, String pattern) {return Criteria_wrapper.new_(name, Criteria_.like_(pattern));}
public static Criteria liken_(String name, String pattern) {return Criteria_wrapper.new_(name, Criteria_.liken_(pattern));}
public static Criteria eqMany_(KeyVal... array) {
public static Criteria_fld eq_(String key, Object val) {return Criteria_fld.new_(key, Criteria_.eq_(val));}
public static Criteria_fld eqn_(String key, Object val) {return Criteria_fld.new_(key, Criteria_.eqn_(val));}
public static Criteria_fld lt_(String key, Comparable val) {return Criteria_fld.new_(key, Criteria_.lt_(val));}
public static Criteria_fld lte_(String key, Comparable val) {return Criteria_fld.new_(key, Criteria_.lte_(val));}
public static Criteria_fld mt_(String key, Comparable val) {return Criteria_fld.new_(key, Criteria_.mt_(val));}
public static Criteria_fld mte_(String key, Comparable val) {return Criteria_fld.new_(key, Criteria_.mte_(val));}
public static Criteria_fld between_(String key, Comparable lhs, Comparable rhs) {return Criteria_fld.new_(key, Criteria_.between_(lhs, rhs));}
public static Criteria_fld in_(String key, Object... vals) {return Criteria_fld.new_(key, Criteria_.in_(vals));}
public static Criteria_fld like_(String key, String pattern) {return Criteria_fld.new_(key, Criteria_.like_(pattern));}
public static Criteria_fld liken_(String key, String pattern) {return Criteria_fld.new_(key, Criteria_.liken_(pattern));}
public static Criteria_fld eq_(String key) {return Criteria_fld.new_(key, Criteria_.eq_(null));}
public static Criteria eq_many_(String... ary) {
Criteria rv = null;
int len = ary.length;
for (int i = 0; i < len; i++) {
Criteria crt = Db_crt_.eq_(ary[i], null);
rv = (i == 0)? crt : Criteria_.And(rv, crt);
}
// if (rv == null) return Wildcard;
// return rv.Tid() == Criteria_.Tid_wrapper ? (Criteria_fld)rv : Criteria_fld.new_(Criteria_fld.Key_null, rv);
return rv;
}
// public static Criteria_fld and(Criteria and) {return Criteria_fld.new_(Criteria_fld.Key_null, and);}
public static Criteria eq_many_(KeyVal... array) {
Criteria rv = null;
for (int i = 0; i < array.length; i++) {
KeyVal pair = array[i];
@@ -38,15 +51,5 @@ public class Db_crt_ {
}
return rv;
}
public static Criteria eq_(String name) {return Criteria_wrapper.new_(name, Criteria_.eq_(null));}
public static Criteria eq_many_(String... ary) {
Criteria rv = null;
int len = ary.length;
for (int i = 0; i < len; i++) {
Criteria crt = Db_crt_.eq_(ary[i], null);
rv = (i == 0)? crt : Criteria_.And(rv, crt);
}
return rv;
}
public static Criteria wrap_(String name, Criteria crt) {return Criteria_wrapper.new_(name, crt);}
public static Criteria_fld wrap_(String key, Criteria crt) {return Criteria_fld.new_(key, crt);}
}

View File

@@ -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 gplx.stores.*;
import java.sql.*;
import gplx.stores.*; import gplx.dbs.engines.sqlite.*;
class Sqlite_engine extends Db_engine_sql_base {
@Override public String Tid() {return Db_url__sqlite.Tid_const;}
@Override public Db_engine New_clone(Db_url connectInfo) {

View File

@@ -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.*; import gplx.dbs.engines.sqlite.*;
public class Sqlite_engine_ {
public static void Db_attach(Db_conn p, String alias, String url) {
String s = String_.Format("ATTACH '{0}' AS {1};", url, alias);

View File

@@ -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.*;
import gplx.lists.*; /*GfoNde*/
class TdbDeleteWkr implements Db_qryWkr {
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {

View File

@@ -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.dbs.sqls.*;
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
import gplx.lists.*; /*ComparerAble*/ import gplx.stores.*; /*GfoNdeRdr*/
class TdbSelectWkr implements Db_qryWkr {
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
@@ -24,7 +24,7 @@ class TdbSelectWkr implements Db_qryWkr {
if (cmd.From().Tbls().Count() > 1) throw Err_.new_key_("gplx.tdbs", "joins not supported for tdbs").Add("sql", cmd.Xto_sql());
TdbTable tbl = engine.FetchTbl(cmd.From().BaseTable().TblName());
GfoNdeList rv = (cmd.Where() == Db_qry_.WhereAll && cmd.Limit() == Db_qry_select.Limit_disabled) ? rv = tbl.Rows() : FilterRecords(tbl, cmd.Where().Crt(), cmd.Limit());
GfoNdeList rv = (cmd.Where() == Db_qry_.WhereAll && cmd.Limit() == Db_qry_select.Limit_disabled) ? rv = tbl.Rows() : FilterRecords(tbl, cmd.Where(), cmd.Limit());
if (cmd.GroupBy() != null)
rv = TdbGroupByWkr.GroupByExec(cmd, rv, tbl);
if (cmd.OrderBy() != null) { // don't use null pattern here; if null ORDER BY, then don't call .Sort on GfoNdeList

View File

@@ -16,15 +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.criterias.*;
import gplx.core.criterias.*;
import gplx.lists.*; /*GfoNde*/
class TdbUpdateWkr implements Db_qryWkr {
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_update cmd = (Db_qry_update)cmdObj;
int rv = 0;
TdbTable tbl = engine.FetchTbl(cmd.BaseTable());
Criteria crt = cmd.Where().Crt();
TdbTable tbl = engine.FetchTbl(cmd.Base_table());
Criteria crt = cmd.Where();
for (int i = 0; i < tbl.Rows().Count(); i++) {
GfoNde row = (GfoNde)tbl.Rows().FetchAt_asGfoNde(i);
if (crt.Matches(row)) {

View File

@@ -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.stores; import gplx.*;
import gplx.criterias.*; import gplx.dbs.*;
import gplx.core.criterias.*; import gplx.dbs.*;
public class DbMaprRdr extends DataRdr_base implements SrlMgr {
@Override public String NameOfNode() {return "DbMaprRdr";}
@Override public Object StoreRoot(SrlObj subProto, String key) {

View File

@@ -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.stores; import gplx.*;
import gplx.criterias.*; import gplx.dbs.*;
import gplx.core.criterias.*; import gplx.dbs.*;
public class DbMaprWtr extends DataWtr_base implements DataWtr {
public void InitWtr(String key, Object val) {}
@Override public Object StoreRoot(SrlObj root, String key) {

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs; import gplx.*;
import org.junit.*;
import gplx.criterias.*; import gplx.dbs.sqls.*;
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
public class AnsiSqlWtr_tst {
Sql_qry_wtr sqlWtr = Sql_qry_wtr_.new_ansi();
@Test public void Insert() {

View File

@@ -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.dbs.engines.sqlite.*;
public class Db_conn_fxt implements RlsAble {
public Db_conn Conn() {return conn;} public Db_conn_fxt Conn_(Db_conn v) {conn = v; return this;} Db_conn conn;
public void DmlAffectedAvailable_(boolean v) {dmlAffectedAvailable = v;} private boolean dmlAffectedAvailable = true;

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs; import gplx.*;
import org.junit.*;
import gplx.criterias.*;
import gplx.core.criterias.*;
public class Db_crt_tst {
@Before public void setup() {
row = GfoNde_.vals_(GfoFldList_.new_().Add("id", IntClassXtn._).Add("name", StringClassXtn._), Object_.Ary(1, "me"));

View File

@@ -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.*; /*Criteria_base*/
import org.junit.*; import gplx.core.strings.*;
import gplx.core.criterias.*; /*Criteria_base*/
import gplx.ios.*; import gplx.dbs.sqls.*;
public class IoSqlCriteriaWriter_tst {
@Test public void Type() {
@@ -49,12 +49,12 @@ public class IoSqlCriteriaWriter_tst {
( ioCrt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmDir.Type_Dir)), ioCrt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmFil.Type_Fil))
));
}
Criteria ioCrt_(String fld, Criteria crt) {return Criteria_wrapper.new_(fld, crt);}
Criteria ioCrt_(String fld, Criteria crt) {return Criteria_fld.new_(fld, crt);}
String fld;
void tst_Write(String expd, Criteria crt) {
String_bldr sb = String_bldr_.new_();
Sql_qry_wtr_ansi whereWtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.new_ansi();
whereWtr.Bld_where(sb, crt);
whereWtr.Bld_where_val(sb, crt);
Tfds.Eq(expd, sb.XtoStr());
}
}