mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Source: Restore broken commit
This commit is contained in:
24
140_dbs/src/gplx/dbs/qrys/Db_arg.java
Normal file
24
140_dbs/src/gplx/dbs/qrys/Db_arg.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_arg {
|
||||
public Db_arg(String key, Object val, byte val_tid) {this.Key = key; this.Val = val; this.Val_tid = val_tid;}
|
||||
public final String Key;
|
||||
public final Object Val;
|
||||
public final byte Val_tid;
|
||||
}
|
||||
33
140_dbs/src/gplx/dbs/qrys/Db_arg_owner.java
Normal file
33
140_dbs/src/gplx/dbs/qrys/Db_arg_owner.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public interface Db_arg_owner extends Db_qry {
|
||||
Db_arg_owner From_(String tbl);
|
||||
Db_arg_owner Crt_int(String k, int v);
|
||||
Db_arg_owner Crt_str(String k, String v);
|
||||
Db_arg_owner Val_byte(String k, byte v);
|
||||
Db_arg_owner Val_int(String k, int v);
|
||||
Db_arg_owner Val_long(String k, long v);
|
||||
Db_arg_owner Val_decimal(String k, Decimal_adp v);
|
||||
Db_arg_owner Val_str(String k, String v);
|
||||
Db_arg_owner Val_date(String k, DateAdp v);
|
||||
Db_arg_owner Val_blob(String k, byte[] v);
|
||||
Db_arg_owner Val_str_by_bry(String k, byte[] v);
|
||||
Db_arg_owner Val_obj(String key, Object val);
|
||||
Db_arg_owner Val_obj_type(String key, Object val, byte val_tid);
|
||||
}
|
||||
113
140_dbs/src/gplx/dbs/qrys/Db_qry__select_cmd.java
Normal file
113
140_dbs/src/gplx/dbs/qrys/Db_qry__select_cmd.java
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*;
|
||||
import gplx.dbs.sqls.*; import gplx.dbs.sqls.itms.*;
|
||||
public class Db_qry__select_cmd implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_select;}
|
||||
public boolean Exec_is_rdr() {return Bool_.Y;}
|
||||
public String Base_table() {return from.Base_tbl.Name;}
|
||||
public Sql_from_clause From() {return from;} private Sql_from_clause from;
|
||||
public Db_qry__select_cmd From_(String tbl) {return From_(tbl, Sql_tbl_itm.Alias__null);}
|
||||
public Db_qry__select_cmd From_(String name, String alias) {return From_(Sql_tbl_itm.Db__null, name, alias);}
|
||||
public Db_qry__select_cmd From_(String db, String tbl, String alias) {
|
||||
if (from != null) throw Err_.new_("sql_qry", "super table already defined", "from", from.Base_tbl.Name);
|
||||
from = new Sql_from_clause(new Sql_tbl_itm(Sql_tbl_itm.Tid__from, db, tbl, alias, Sql_join_fld.Ary__empty));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Join_(String name, String alias, Sql_join_fld... join_flds) {return Join_(Sql_tbl_itm.Tid__inner, Sql_tbl_itm.Db__null , name, alias, join_flds);}
|
||||
public Db_qry__select_cmd Join_(String db, String name, String alias, Sql_join_fld... join_flds) {return Join_(Sql_tbl_itm.Tid__inner, db , name, alias, join_flds);}
|
||||
public Db_qry__select_cmd Join_(int join_tid, String db, String name, String alias, Sql_join_fld... join_flds) {
|
||||
if (from == null) throw Err_.new_("sql_qry", "super table is not defined");
|
||||
from.Tbls.Add(new Sql_tbl_itm(join_tid, db, name, alias, join_flds));
|
||||
return this;
|
||||
}
|
||||
public Sql_select_clause Cols() {return cols;} private Sql_select_clause cols = new Sql_select_clause();
|
||||
public Db_qry__select_cmd Distinct_() {cols.Distinct = true; return this;}
|
||||
public Db_qry__select_cmd Cols_all_() {cols.Flds.Clear().Add(Sql_select_fld.Wildcard); return this;}
|
||||
public Db_qry__select_cmd Cols_(String... ary) {return Cols_w_tbl_(Sql_select_fld.Tbl__null, ary);}
|
||||
public Db_qry__select_cmd Cols_w_tbl_(String tbl, String... ary) {
|
||||
for (String itm : ary)
|
||||
cols.Flds.Add(Sql_select_fld.New_fld(tbl, itm, itm));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_w_alias_(String expr, String alias) {
|
||||
cols.Flds.Add(Sql_select_fld.New_fld(Sql_select_fld.Tbl__null, expr, alias));
|
||||
return this;
|
||||
}
|
||||
public Sql_where_clause Where_itm() {return where_itm;} private Sql_where_clause where_itm = Sql_where_clause.All;
|
||||
public Db_qry__select_cmd Where_(Criteria root) {
|
||||
if (where_itm == Sql_where_clause.All) where_itm = new Sql_where_clause();
|
||||
where_itm.Root = root;
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Where_and(Criteria crt) {
|
||||
if (where_itm == Sql_where_clause.All) throw Err_.new_("sql_qry", "where is not defined");
|
||||
where_itm.Root = Criteria_.And(where_itm.Root, crt);
|
||||
return this;
|
||||
}
|
||||
public Sql_order_clause Order() {return order;} private Sql_order_clause order = null;
|
||||
public Db_qry__select_cmd Order_asc_(String fld) {return Order_(fld, Bool_.Y);}
|
||||
public Db_qry__select_cmd Order_(String fld) {return Order_(Sql_order_fld.Tbl__null, fld, Bool_.Y);}
|
||||
public Db_qry__select_cmd Order_(String fld, boolean asc) {return Order_(Sql_order_fld.Tbl__null, fld, asc);}
|
||||
public Db_qry__select_cmd Order_(String tbl, String fld) {return Order_(tbl, fld, Bool_.Y);}
|
||||
public Db_qry__select_cmd Order_(String tbl, String fld, boolean asc) {
|
||||
if (order == null) order = new Sql_order_clause();
|
||||
Sql_order_fld item = new Sql_order_fld(tbl, fld, asc ? Sql_order_fld.Sort__nil : Sql_order_fld.Sort__dsc);
|
||||
order.Flds__add(item);
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Order_asc_many_(String... flds) {
|
||||
if (order == null) order = new Sql_order_clause();
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i)
|
||||
order.Flds__add(new Sql_order_fld(Sql_order_fld.Tbl__null, flds[i], Sql_order_fld.Sort__nil));
|
||||
return this;
|
||||
}
|
||||
public int Limit() {return limit;} public Db_qry__select_cmd Limit_(int v) {this.limit = v; return this;} private int limit = Limit__disabled; public static final int Limit__disabled = Int_.Min_value;
|
||||
public int Offset() {return offset;} public Db_qry__select_cmd Offset_(int v) {this.offset = v; return this;} private int offset = Offset__disabled; public static final int Offset__disabled = Int_.Min_value;
|
||||
public String Indexed_by() {return indexed_by;} public Db_qry__select_cmd Indexed_by_(String v) {indexed_by = v; return this;} private String indexed_by;
|
||||
|
||||
public Sql_group_clause GroupBy() {return groupBy;} private Sql_group_clause groupBy = null;
|
||||
public Db_qry__select_cmd GroupBy_(String... flds) {
|
||||
if (groupBy != null) throw Err_.new_("sql_qry", "group by already defined", "group", groupBy);
|
||||
groupBy = Sql_group_clause.new_(flds);
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_groupBy_max(String fld) {return Cols_groupBy_max(fld, fld);}
|
||||
public Db_qry__select_cmd Cols_groupBy_max(String fld, String alias) {
|
||||
cols.Flds.Add(Sql_select_fld.New_max(Sql_select_fld.Tbl__null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_groupBy_min(String fld, String alias) {
|
||||
cols.Flds.Add(Sql_select_fld.New_min(Sql_select_fld.Tbl__null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_groupBy_count(String fld, String alias) {
|
||||
cols.Flds.Add(Sql_select_fld.New_count(Sql_select_fld.Tbl__null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry__select_cmd Cols_groupBy_sum(String fld) {return Cols_groupBy_sum(fld, fld);}
|
||||
public Db_qry__select_cmd Cols_groupBy_sum(String fld, String alias) {
|
||||
cols.Flds.Add(Sql_select_fld.New_sum(Sql_select_fld.Tbl__null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
|
||||
public String To_sql__exec(gplx.dbs.sqls.Sql_qry_wtr wtr) {return wtr.To_sql_str(this, Bool_.N);}
|
||||
public String To_sql__prep(gplx.dbs.sqls.Sql_qry_wtr wtr) {return wtr.To_sql_str(this, Bool_.Y);}
|
||||
}
|
||||
87
140_dbs/src/gplx/dbs/qrys/Db_qry__select_in_tbl.java
Normal file
87
140_dbs/src/gplx/dbs/qrys/Db_qry__select_in_tbl.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
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 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 base_table;} private final String base_table;
|
||||
public Criteria Where() {return where;} private Criteria where;
|
||||
public void Where_(Criteria v) {this.where = v;}
|
||||
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;
|
||||
if (where_flds_len == 0) return;
|
||||
for (int i = 0; i < where_flds_len; ++i) {
|
||||
if (i != 0) sb.Add("AND ");
|
||||
sb.Add(where_flds[i]).Add(" = ? ");
|
||||
}
|
||||
}
|
||||
public String Group_by_sql() {return group_by_sql;} private final String group_by_sql;
|
||||
public String Having_sql() {return having_sql;} private final String having_sql;
|
||||
public String Order_by_sql() {return order_by_sql;} public Db_qry__select_in_tbl Order_by_sql_(String v) {order_by_sql = v; return this;} private String order_by_sql;
|
||||
public String Limit_sql() {return limit_sql;} private final String limit_sql;
|
||||
public String To_sql__exec(gplx.dbs.sqls.Sql_qry_wtr wtr) {
|
||||
synchronized (this) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add("SELECT ");
|
||||
int select_flds_len = select_flds.length;
|
||||
for (int i = 0; i < select_flds_len; ++i) {
|
||||
if (i != 0) sb.Add(",");
|
||||
sb.Add(select_flds[i]);
|
||||
}
|
||||
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.To_str();
|
||||
}
|
||||
}
|
||||
public static Db_qry__select_in_tbl new_(String base_table, String[] where_flds, String[] select_flds, String[] order_flds) {
|
||||
String order_by_sql = null;
|
||||
if (order_flds != Order_by_null) {
|
||||
int len = order_flds.length;
|
||||
switch (len) {
|
||||
case 0: break;
|
||||
case 1: order_by_sql = order_flds[0]; break;
|
||||
default:
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
String order_fld = order_flds[i];
|
||||
if (i != 0) bfr.Add_byte_comma();
|
||||
bfr.Add_str_a7(order_fld);
|
||||
}
|
||||
order_by_sql = bfr.To_str_and_clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
Db_qry__select_in_tbl rv = new Db_qry__select_in_tbl(base_table, select_flds, where_flds, null, null, order_by_sql, null);
|
||||
rv.where = where_flds.length == 0 ? Db_crt_.Wildcard : Db_crt_.eq_many_(where_flds);
|
||||
return rv;
|
||||
}
|
||||
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;
|
||||
public static final String[] Order_by_null = null;
|
||||
}
|
||||
32
140_dbs/src/gplx/dbs/qrys/Db_qry_delete.java
Normal file
32
140_dbs/src/gplx/dbs/qrys/Db_qry_delete.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
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;}
|
||||
public boolean Exec_is_rdr() {return Bool_.N;}
|
||||
public String Base_table() {return base_table;} private final String base_table;
|
||||
public String To_sql__exec(Sql_qry_wtr wtr) {return wtr.To_sql_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 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);}
|
||||
public static final Criteria Where__null = null;
|
||||
}
|
||||
44
140_dbs/src/gplx/dbs/qrys/Db_qry_dml_tst.java
Normal file
44
140_dbs/src/gplx/dbs/qrys/Db_qry_dml_tst.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Db_qry_dml_tst {
|
||||
@Test public void Delete_basic() {
|
||||
tst_XtoSql(Db_qry_delete.new_("tbl0", Db_crt_.New_eq("fld0", "val0"))
|
||||
, "DELETE FROM tbl0 WHERE fld0 = 'val0'");
|
||||
}
|
||||
@Test public void Insert_basic() {
|
||||
tst_XtoSql(new Db_qry_insert("tbl0").Val_int("id", 0).Val_str("name", "me").Val_date("time", DateAdp_.parse_gplx("2007-12-23"))
|
||||
, "INSERT INTO tbl0 (id, name, time) VALUES (0, 'me', '2007-12-23 00:00:00.000')");
|
||||
}
|
||||
@Test public void Update_basic() {
|
||||
Db_qry_update qry = new Db_qry_update();
|
||||
qry.From_("tbl0");
|
||||
qry.Where_(Db_crt_.New_eq("id", 0)).Val_str("name", "me");
|
||||
tst_XtoSql(qry, "UPDATE tbl0 SET name='me' WHERE id = 0");
|
||||
}
|
||||
@Test public void Update_all() {
|
||||
Db_qry_update qry = new Db_qry_update();
|
||||
qry.From_("tbl0");
|
||||
qry.Val_int("id", 1).Val_str("name", "me").Val_date("startTime", DateAdp_.parse_gplx("2007-12-23"));
|
||||
qry.Where_(Criteria_.And(Db_crt_.New_eq("id", 0), Db_crt_.New_mt("startTime", DateAdp_.parse_gplx("2005-01-01"))));
|
||||
tst_XtoSql(qry, "UPDATE tbl0 SET id=1, name='me', startTime='2007-12-23 00:00:00.000' WHERE (id = 0 AND startTime > '2005-01-01 00:00:00.000')");
|
||||
}
|
||||
void tst_XtoSql(Db_qry qry, String expd) {Tfds.Eq(expd, qry.To_sql__exec(gplx.dbs.sqls.Sql_qry_wtr_.New__basic()));}
|
||||
}
|
||||
37
140_dbs/src/gplx/dbs/qrys/Db_qry_flush.java
Normal file
37
140_dbs/src/gplx/dbs/qrys/Db_qry_flush.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_flush implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_flush;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return tableNames[0];}
|
||||
public String To_sql__exec(Sql_qry_wtr wtr) {return wtr.To_sql_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
|
||||
public String[] TableNames() {return tableNames;} private String[] tableNames;
|
||||
|
||||
|
||||
public static Db_qry_flush as_(Object obj) {return obj instanceof Db_qry_flush ? (Db_qry_flush)obj : null;}
|
||||
public static Db_qry_flush cast(Object obj) {try {return (Db_qry_flush)obj;} catch(Exception exc) {throw Err_.new_type_mismatch_w_exc(exc, Db_qry_flush.class, obj);}}
|
||||
public static Db_qry_flush new_(String... ary) {
|
||||
Db_qry_flush rv = new Db_qry_flush();
|
||||
rv.tableNames = ary;
|
||||
return rv;
|
||||
} Db_qry_flush() {}
|
||||
}
|
||||
66
140_dbs/src/gplx/dbs/qrys/Db_qry_insert.java
Normal file
66
140_dbs/src/gplx/dbs/qrys/Db_qry_insert.java
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*; import gplx.dbs.sqls.itms.*;
|
||||
public class Db_qry_insert implements Db_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 To_sql__exec(Sql_qry_wtr wtr) {return wtr.To_sql_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_arg_owner From_(String tbl) {base_table = tbl; return this;}
|
||||
public Keyval_hash Args() {return args;} private final Keyval_hash args = new Keyval_hash();
|
||||
public Db_arg_owner Val_byte(String k, byte v) {return Val_obj_type(k, v, Db_val_type.Tid_byte);}
|
||||
public Db_arg_owner Val_int(String k, int v) {return Val_obj_type(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_arg_owner Val_long(String k, long v) {return Val_obj_type(k, v, Db_val_type.Tid_int64);}
|
||||
public Db_arg_owner Val_decimal(String k, Decimal_adp v) {return Val_obj_type(k, v.Under(), Db_val_type.Tid_decimal);}
|
||||
public Db_arg_owner Val_str(String k, String v) {return Val_obj_type(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_arg_owner Val_str_by_bry(String k, byte[] v) {return Val_obj_type(k, String_.new_u8(v), Db_val_type.Tid_varchar);}
|
||||
public Db_arg_owner Val_date(String k, DateAdp v) {return Val_obj_type(k, v, Db_val_type.Tid_date);}
|
||||
public Db_arg_owner Val_blob(String k, byte[] v) {return Val_obj_type(k, v, Db_val_type.Tid_bry);}
|
||||
public Db_arg_owner Val_obj(String k, Object v) {return Val_obj_type(k, v, Db_val_type.Tid_null);}
|
||||
public Db_arg_owner Val_obj_type(String key, Object val, byte val_tid) {
|
||||
if (key == Dbmeta_fld_itm.Key_null) return this;
|
||||
args.Add(key, new Db_arg(key, val, val_tid));
|
||||
return this;
|
||||
}
|
||||
public Db_arg_owner Crt_int(String k, int v) {return Val_obj_type(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_arg_owner Crt_str(String k, String v) {return Val_obj_type(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_qry__select_cmd Select() {return select;} Db_qry__select_cmd select;
|
||||
public Db_qry_insert Select_(Db_qry__select_cmd qry) {this.select = qry; return this;}
|
||||
public Db_qry_insert Cols_(String... ary) {
|
||||
if (cols == null) cols = new Sql_select_fld_list();
|
||||
for (String fld : ary)
|
||||
cols.Add(Sql_select_fld.New_fld(Sql_select_fld.Tbl__null, fld, fld));
|
||||
return this;
|
||||
}
|
||||
public Sql_select_fld_list Cols() {return cols;} private Sql_select_fld_list cols;
|
||||
|
||||
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_();
|
||||
rv.base_table = tbl;
|
||||
rv.cols_for_insert = keys;
|
||||
int len = keys.length;
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv.Val_obj(keys[i], null);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
89
140_dbs/src/gplx/dbs/qrys/Db_qry_select_tst.java
Normal file
89
140_dbs/src/gplx/dbs/qrys/Db_qry_select_tst.java
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_select_tst {
|
||||
@Before public void setup() {
|
||||
cmd = new Db_qry__select_cmd();
|
||||
} Db_qry__select_cmd cmd; String expd;
|
||||
@Test public void Basic() {
|
||||
cmd.Cols_("fld0", "fld1").From_("tbl0");
|
||||
expd = "SELECT fld0, fld1 FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderDoesNotMatter() {
|
||||
cmd.From_("tbl0").Cols_("fld0", "fld1");
|
||||
expd = "SELECT fld0, fld1 FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void DefaultAllFields() {
|
||||
cmd.From_("tbl0");
|
||||
expd = "SELECT * FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Where() {
|
||||
cmd.From_("tbl0").Where_(Db_crt_.New_eq("fld0", 0));
|
||||
expd = "SELECT * FROM tbl0 WHERE fld0 = 0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Join() {
|
||||
cmd.From_("tbl0").Join_("tbl1", "t1", Db_qry_.New_join__join("fld1", "tbl0", "fld0"));
|
||||
expd = "SELECT * FROM tbl0 INNER JOIN tbl1 t1 ON tbl0.fld0 = t1.fld1";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderBy() {
|
||||
cmd.From_("tbl0").Order_("fld0", true);
|
||||
expd = "SELECT * FROM tbl0 ORDER BY fld0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderByMany() {
|
||||
cmd.From_("tbl0").Order_asc_many_("fld0", "fld1");
|
||||
expd = "SELECT * FROM tbl0 ORDER BY fld0, fld1";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Limit() {
|
||||
cmd.From_("tbl0").Limit_(10);
|
||||
expd = "SELECT * FROM tbl0 LIMIT 10";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
// @Test public void GroupBy() {
|
||||
// cmd.From_("tbl0").groupBy_("fld0", "fld1");
|
||||
// expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1";
|
||||
// Tfds.Eq(cmd.To_str(), expd);
|
||||
// }
|
||||
// @Test public void Union() {
|
||||
// cmd.From_("tbl0").select("fld0").union_(qry2.from("tbl1").select("fld0"));
|
||||
// cmd.From_("tbl0").select("fld0").union_().from("tbl1").select("fld0"); // feasible, but will be bad later when trying to access Db_qry__select_cmd props
|
||||
// expd = "SELECT fld0 FROM tbl0 UNION SELECT fld0 FROM tbl1";
|
||||
// Tfds.Eq(cmd.To_str(), expd);
|
||||
// }
|
||||
// @Test public void Having() {
|
||||
// cmd.From_("tbl0").groupBy_("fld0", "fld1");
|
||||
// expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1 HAVING Count(fld0) > 1";
|
||||
// Tfds.Eq(cmd.To_str(), expd);
|
||||
// }
|
||||
void tst_XtoStr(Db_qry qry, String expd) {Tfds.Eq(expd, cmd.To_sql__exec(Sql_qry_wtr_.New__basic()));}
|
||||
}
|
||||
82
140_dbs/src/gplx/dbs/qrys/Db_qry_sql.java
Normal file
82
140_dbs/src/gplx/dbs/qrys/Db_qry_sql.java
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_sql implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_sql;}
|
||||
public boolean Exec_is_rdr() {return isReader;} private boolean isReader;
|
||||
public String Base_table() {throw Err_.new_unimplemented();}
|
||||
public String To_sql__exec(Sql_qry_wtr wtr) {return sql;} private String sql;
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public static Db_qry_sql dml_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql ddl_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql xtn_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql sql_(String sql) {
|
||||
Db_qry_sql rv = new Db_qry_sql();
|
||||
rv.sql = sql; rv.isReader = false;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_sql rdr_(String sql) {
|
||||
Db_qry_sql rv = new Db_qry_sql();
|
||||
rv.sql = sql; rv.isReader = true;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_sql as_(Object obj) {return obj instanceof Db_qry_sql ? (Db_qry_sql)obj : null;}
|
||||
public static Db_qry_sql cast(Object obj) {try {return (Db_qry_sql)obj;} catch(Exception exc) {throw Err_.new_type_mismatch_w_exc(exc, Db_qry_sql.class, obj);}}
|
||||
public static String Gen_sql(Sql_qry_wtr qry_wtr, Db_qry qry, Object... args) {
|
||||
byte[] src = Bry_.new_u8(qry_wtr.To_sql_str(qry, true));
|
||||
int src_len = src.length;
|
||||
int args_idx = 0, args_len = args.length, pos = 0;
|
||||
Bry_bfr bfr = Bry_bfr_.New_w_size(src_len);
|
||||
while (pos < src_len) {
|
||||
int question_pos = Bry_find_.Find_fwd(src, Byte_ascii.Question, pos);
|
||||
if (question_pos == Bry_find_.Not_found)
|
||||
question_pos = src_len;
|
||||
bfr.Add_mid(src, pos, question_pos);
|
||||
if (args_idx < args_len)
|
||||
Gen_sql_arg(bfr, args[args_idx++]);
|
||||
pos = question_pos + 1;
|
||||
}
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
private static void Gen_sql_arg(Bry_bfr bfr, Object val) {
|
||||
if (val == null) {bfr.Add(Bry_null); return;}
|
||||
Class<?> val_type = val.getClass();
|
||||
if (Type_adp_.Eq(val_type, Int_.Cls_ref_type))
|
||||
bfr.Add_int_variable(Int_.cast(val));
|
||||
else if (Type_adp_.Eq(val_type, Bool_.Cls_ref_type))
|
||||
bfr.Add_int_fixed(1, Bool_.To_int(Bool_.Cast(val))); // NOTE: save boolean to 0 or 1, b/c (a) db may not support bit datatype (sqllite) and (b) avoid i18n issues with "true"/"false"
|
||||
else if (Type_adp_.Eq(val_type, Double_.Cls_ref_type))
|
||||
bfr.Add_double(Double_.cast(val));
|
||||
else if (Type_adp_.Eq(val_type, Long_.Cls_ref_type))
|
||||
bfr.Add_long_variable(Long_.cast(val));
|
||||
else if (Type_adp_.Eq(val_type, Float_.Cls_ref_type))
|
||||
bfr.Add_float(Float_.cast(val));
|
||||
else if (Type_adp_.Eq(val_type, Byte_.Cls_ref_type))
|
||||
bfr.Add_byte(Byte_.cast(val));
|
||||
else if (Type_adp_.Eq(val_type, DateAdp_.Cls_ref_type))
|
||||
bfr.Add_byte_apos().Add_str_a7(DateAdp_.cast(val).XtoStr_gplx_long()).Add_byte_apos();
|
||||
else if (Type_adp_.Eq(val_type, Decimal_adp_.Cls_ref_type))
|
||||
bfr.Add_str_a7(Decimal_adp_.cast(val).To_str());
|
||||
else {
|
||||
byte[] val_bry = Bry_.new_u8(Object_.Xto_str_strict_or_null(val));
|
||||
val_bry = Bry_.Replace(val_bry, Byte_ascii.Apos_bry, Bry_escape_apos);
|
||||
bfr.Add_byte_apos().Add(val_bry).Add_byte_apos();
|
||||
}
|
||||
} private static final byte[] Bry_null = Bry_.new_u8("NULL"), Bry_escape_apos = Bry_.new_a7("''");
|
||||
}
|
||||
48
140_dbs/src/gplx/dbs/qrys/Db_qry_sql_tst.java
Normal file
48
140_dbs/src/gplx/dbs/qrys/Db_qry_sql_tst.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_sql_tst {
|
||||
@Before public void init() {fxt.Clear();} private Db_qry_sql_fxt fxt = new Db_qry_sql_fxt();
|
||||
@Test public void Insert() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_insert.new_("tbl", "k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9")
|
||||
, Object_.Ary(123, Bool_.Y, 1.23d, 123L, 123f, Byte_ascii.Num_1, "123", DateAdp_.parse_iso8561("1981-04-05T14:30:30"), Decimal_adp_.parse("1.23"))
|
||||
, "INSERT INTO tbl (k1, k2, k3, k4, k5, k6, k7, k8, k9) VALUES (123, 1, 1.23, 123, 123, 1, '123', '1981-04-05 14:30:30.000', 1.23)"
|
||||
);
|
||||
}
|
||||
@Test public void Update() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_update.New("tbl", String_.Ary("k1", "k2"), "k3", "k4")
|
||||
, Object_.Ary("v3", "v4", "v1", "v2")
|
||||
, "UPDATE tbl SET k3='v3', k4='v4' WHERE (k1 = 'v1' AND k2 = 'v2')"
|
||||
);
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_delete.new_("tbl", String_.Ary("k1", "k2"))
|
||||
, Object_.Ary("v1", "v2")
|
||||
, "DELETE FROM tbl WHERE (k1 = 'v1' AND k2 = 'v2')"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Db_qry_sql_fxt {
|
||||
private final Sql_qry_wtr qry_wtr = Sql_qry_wtr_.New__sqlite();
|
||||
public void Clear() {}
|
||||
public void Test_qry(Db_qry qry, Object[] vals, String expd) {Tfds.Eq(expd, Db_qry_sql.Gen_sql(qry_wtr, qry, vals));}
|
||||
}
|
||||
59
140_dbs/src/gplx/dbs/qrys/Db_qry_update.java
Normal file
59
140_dbs/src/gplx/dbs/qrys/Db_qry_update.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_update implements Db_arg_owner {
|
||||
public int Tid() {return Db_qry_.Tid_update;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String To_sql__exec(Sql_qry_wtr wtr) {return wtr.To_sql_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_update() {return cols_for_update;} private String[] cols_for_update;
|
||||
public Criteria Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = crt; return this;} private Criteria where;
|
||||
public Db_arg_owner From_(String tbl) {base_table = tbl; return this;}
|
||||
public Keyval_hash Args() {return args;} private final Keyval_hash args = new Keyval_hash();
|
||||
public Db_arg_owner Val_byte(String k, byte v) {return Val_obj_type(k, v, Db_val_type.Tid_byte);}
|
||||
public Db_arg_owner Val_int(String k, int v) {return Val_obj_type(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_arg_owner Val_long(String k, long v) {return Val_obj_type(k, v, Db_val_type.Tid_int64);}
|
||||
public Db_arg_owner Val_decimal(String k, Decimal_adp v) {return Val_obj_type(k, v.Under(), Db_val_type.Tid_decimal);}
|
||||
public Db_arg_owner Val_str(String k, String v) {return Val_obj_type(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_arg_owner Val_str_by_bry(String k, byte[] v) {return Val_obj_type(k, String_.new_u8(v), Db_val_type.Tid_varchar);}
|
||||
public Db_arg_owner Val_date(String k, DateAdp v) {return Val_obj_type(k, v, Db_val_type.Tid_date);}
|
||||
public Db_arg_owner Val_blob(String k, byte[] v) {return Val_obj_type(k, v, Db_val_type.Tid_bry);}
|
||||
public Db_arg_owner Val_obj(String k, Object v) {return Val_obj_type(k, v, Db_val_type.Tid_null);}
|
||||
public Db_arg_owner Val_obj_type(String key, Object val, byte val_tid) {
|
||||
if (key == Dbmeta_fld_itm.Key_null) return this;
|
||||
args.Add(key, new Db_arg(key, val, val_tid));
|
||||
return this;
|
||||
}
|
||||
public Db_arg_owner Crt_int(String k, int v) {return Key_obj_(k, v);}
|
||||
public Db_arg_owner Crt_str(String k, String v) {return Key_obj_(k, v);}
|
||||
private Db_arg_owner Key_obj_(String k, Object v) {
|
||||
Criteria crt = Db_crt_.New_eq(k, v);
|
||||
where = where == null ? crt : Criteria_.And(where, crt);
|
||||
return this;
|
||||
}
|
||||
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_for_update = update;
|
||||
int len = update.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
rv.Val_obj(update[i], null);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
178
140_dbs/src/gplx/dbs/qrys/Db_stmt_cmd.java
Normal file
178
140_dbs/src/gplx/dbs/qrys/Db_stmt_cmd.java
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.sqls.*; import gplx.core.stores.*;
|
||||
public class Db_stmt_cmd implements Db_stmt {
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private Db_engine engine;
|
||||
private PreparedStatement stmt = null;
|
||||
private String sql; private int val_idx = 0;
|
||||
public Db_stmt_cmd(Db_engine engine, Db_qry qry) {Ctor_stmt(engine, qry);}
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {
|
||||
this.engine = engine;
|
||||
sql = qry.Tid() == Db_qry_.Tid_select_in_tbl ? ((Db_qry__select_in_tbl)qry).To_sql__exec(engine.Sql_wtr()) : engine.Sql_wtr().To_sql_str(qry, true);
|
||||
Reset_stmt();
|
||||
}
|
||||
public Db_stmt Reset_stmt() {
|
||||
stmt = (PreparedStatement)engine.Stmt_by_sql(sql);
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setByte(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "byte", "val", v, "sql", sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int_by_bool(String k, boolean v) {return Add_int(Bool_.N, k, v ? 1 : 0);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setInt(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "int", "val", v, "sql", sql);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setLong(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "long", "val", v, "sql", sql);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setFloat(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "float", "val", v, "sql", sql);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setDouble(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "double", "val", v, "sql", sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, Decimal_adp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, Decimal_adp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(Decimal_adp v) {return Add_decimal(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, Decimal_adp v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setBigDecimal(++val_idx, v.Under_as_native());} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "decimal", "val", v, "sql", sql);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setBytes(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "byte[]", v.length, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_u8(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
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, Key_na, v);}
|
||||
@gplx.Virtual protected Db_stmt Add_str(boolean where, String k, String v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setString(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "String", "val", v, "sql", sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_date(String k, DateAdp v) {return Add_date(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_date(String k, DateAdp v) {return Add_date(Bool_.N, k, v);}
|
||||
@gplx.Virtual protected Db_stmt Add_date(boolean where, String k, DateAdp v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setTimestamp(++val_idx, new java.sql.Timestamp(v.UnderDateTime().getTime().getTime()));} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "date", "val", v, "sql", sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_text(String k, String v) {return Add_text(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_text(String k, String v) {return Add_text(Bool_.N, k, v);}
|
||||
private Db_stmt Add_text(boolean where, String k, String v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setString(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.new_exc(e, "db", "failed to add value", "type", "text", "val", v, "sql", sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.core.ios.streams.Io_stream_rdr v, long rdr_len) {
|
||||
try {stmt.setBinaryStream(++val_idx, (java.io.InputStream)v.Under(), (int)rdr_len);} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "rdr", "val", v);}
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = stmt.execute(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.new_exc(e, "db_stmt", "insert failed", "url", engine.Conn_info().Db_api(), "sql", sql);
|
||||
}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.new_exc(e, "db_stmt", "update failed", "url", engine.Conn_info().Db_api(), "sql", sql);
|
||||
}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.new_exc(e, "db_stmt", "delete failed", "url", engine.Conn_info().Db_api(), "sql", sql);
|
||||
}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = engine.New_rdr(stmt.executeQuery(), sql); return rv;} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql);}
|
||||
}
|
||||
public Db_rdr Exec_select__rls_auto() {
|
||||
try {return engine.Exec_as_rdr__rls_auto(this, stmt.executeQuery(), sql);} catch (Exception e) {throw Err_.new_exc(e, "db", "select failed", "sql", sql);}
|
||||
}
|
||||
public Db_rdr Exec_select__rls_manual() {
|
||||
try {return engine.Exec_as_rdr__rls_manual(stmt.executeQuery(), sql);} catch (Exception e) {throw Err_.new_exc(e, "db", "select failed", "sql", sql);}
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = DataRdr_.Read_1st_row_and_1st_fld(engine.New_rdr(stmt.executeQuery(), sql)); return rv;} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
try {stmt.clearBatch();}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "failed to clear parameters", "sql", sql);}
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
if (stmt == null) return; // Null instance
|
||||
try {
|
||||
if (stmt.getConnection().isClosed()) return; // do not close stmt if connection is already closed; throws null error; DATE:2015-02-11
|
||||
stmt.close();
|
||||
stmt = null;
|
||||
}
|
||||
catch (Exception e) {throw Err_.new_exc(e, "db", "failed to close command", "sql", sql);}
|
||||
}
|
||||
}
|
||||
191
140_dbs/src/gplx/dbs/qrys/Db_stmt_sql.java
Normal file
191
140_dbs/src/gplx/dbs/qrys/Db_stmt_sql.java
Normal file
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.brys.fmtrs.*; import gplx.core.stores.*;
|
||||
import gplx.dbs.engines.*;
|
||||
public class Db_stmt_sql implements Db_stmt {// used for formatting SQL statements; not used for actual insert into database
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private final List_adp args = List_adp_.New();
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
private final Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {}
|
||||
public Db_conn Conn() {return conn;} public void Conn_(Db_conn v) {this.conn = v;} Db_conn conn;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(k, Byte_.To_str(v));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "byte", "val", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int_by_bool(String k, boolean v) {return Add_int(Bool_.N, k, v ? 1 : 0);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(k, Int_.To_str(v));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "int", "val", 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, Key_na, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(k, Long_.To_str(v));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "long", "val", 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, Key_na, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(k, Float_.To_str(v));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "float", "val", 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, Key_na, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(k, Double_.To_str(v));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "double", "val", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, Decimal_adp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, Decimal_adp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(Decimal_adp v) {return Add_decimal(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, Decimal_adp v) {
|
||||
try {Add(k, v.To_str());} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "decimal", "val", 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, Key_na, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {// HACK: convert to String b/c tdb does not support byte[]
|
||||
try {Add(k, Val_str_wrap(String_.new_u8(v)));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "byte[]", "val", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_u8(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
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, Key_na, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(k, Val_str_wrap(v));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "String", "val", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_date(String k, DateAdp v) {return Add_date(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_date(String k, DateAdp v) {return Add_date(Bool_.N, k, v);}
|
||||
private Db_stmt Add_date(boolean where, String k, DateAdp v) {
|
||||
try {Add(k, Val_str_wrap(v.XtoStr_fmt_iso_8561()));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "date", "val", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_text(String k, String v) {return Add_text(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_text(String k, String v) {return Add_text(Bool_.N, k, v);}
|
||||
private Db_stmt Add_text(boolean where, String k, String v) {
|
||||
try {Add(k, Val_str_wrap(v));} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "text", "val", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.core.ios.streams.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
gplx.core.ios.streams.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(Key_na, bfr.To_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to add value", "type", "rdr", "val", v);}
|
||||
return this;
|
||||
}
|
||||
private String Val_str_wrap(String v) {
|
||||
return "'" + String_.Replace(v, "'", "\\'") + "'";
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())) != 0; return rv;} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql_orig);}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql_orig);}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql_orig);}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = conn.Exec_qry_as_old_rdr(Db_qry_sql.rdr_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql_orig);}
|
||||
}
|
||||
public Db_rdr Exec_select__rls_auto() {return Db_rdr_.Empty;}
|
||||
public Db_rdr Exec_select__rls_manual() {return Db_rdr_.Empty;}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = DataRdr_.Read_1st_row_and_1st_fld(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql_orig);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {this.Clear();}
|
||||
public void Add(String k, String v) {
|
||||
if (k == Dbmeta_fld_itm.Key_null) return; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
args.Add(v);
|
||||
}
|
||||
public String Xto_sql() {
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, (Object[])args.To_ary_and_clear(Object.class));
|
||||
return tmp_bfr.To_str_and_clear();
|
||||
}
|
||||
public int Args_len() {return args.Count();}
|
||||
public String Args_get_at(int i) {return (String)args.Get_at(i);}
|
||||
private String sql_orig;
|
||||
public Db_qry Qry() {return qry;} private Db_qry qry;
|
||||
public Db_stmt Parse(Db_qry qry, String sql_str) {
|
||||
this.qry = qry;
|
||||
this.sql_orig = sql_str;
|
||||
Init_fmtr(tmp_bfr, tmp_fmtr, sql_str);
|
||||
return this;
|
||||
}
|
||||
private static void Init_fmtr(Bry_bfr tmp_bfr, Bry_fmtr tmp_fmtr, String sql_str) {
|
||||
byte[] sql_bry = Bry_.new_u8(sql_str);
|
||||
int arg_idx = 0; int pos_prv = 0;
|
||||
tmp_bfr.Clear();
|
||||
while (true) {
|
||||
int pos_cur = Bry_find_.Find_fwd(sql_bry, Byte_ascii.Question, pos_prv); if (pos_cur == Bry_find_.Not_found) break;
|
||||
tmp_bfr.Add_mid(sql_bry, pos_prv, pos_cur);
|
||||
tmp_bfr.Add_byte(Byte_ascii.Tilde).Add_byte(Byte_ascii.Curly_bgn);
|
||||
tmp_bfr.Add_int_variable(arg_idx++);
|
||||
tmp_bfr.Add_byte(Byte_ascii.Curly_end);
|
||||
pos_prv = pos_cur + 1;
|
||||
}
|
||||
tmp_bfr.Add_mid(sql_bry, pos_prv, sql_bry.length);
|
||||
tmp_fmtr.Fmt_(tmp_bfr.To_bry_and_clear());
|
||||
}
|
||||
public static String Xto_str(Bry_bfr tmp_bfr, Bry_fmtr tmp_fmtr, String sql_str, List_adp args) {
|
||||
Init_fmtr(tmp_bfr, tmp_fmtr, sql_str);
|
||||
Object[] ary = args.To_obj_ary();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Object obj = ary[i];
|
||||
String str = "";
|
||||
if (obj == null)
|
||||
str = "NULL";
|
||||
else {
|
||||
str = Object_.Xto_str_strict_or_null(obj);
|
||||
if (Type_adp_.Eq(obj.getClass(), String_.Cls_ref_type))
|
||||
str = "'" + String_.Replace(str, "'", "''") + "'";
|
||||
}
|
||||
ary[i] = str;
|
||||
}
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, ary);
|
||||
return tmp_bfr.To_str_and_clear();
|
||||
}
|
||||
}
|
||||
29
140_dbs/src/gplx/dbs/qrys/Db_stmt_sql_tst.java
Normal file
29
140_dbs/src/gplx/dbs/qrys/Db_stmt_sql_tst.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Db_stmt_sql_tst {
|
||||
@Before public void init() {}
|
||||
@Test public void Basic() {
|
||||
Db_stmt_sql stmt = new Db_stmt_sql();
|
||||
stmt.Parse(null, "UPDATE tbl_0 SET col_0 = ? WHERE col_1 = ?");
|
||||
stmt.Add("col_0", "1");
|
||||
stmt.Add("col_1", "2");
|
||||
Tfds.Eq("UPDATE tbl_0 SET col_0 = 1 WHERE col_1 = 2", stmt.Xto_sql());
|
||||
}
|
||||
}
|
||||
36
140_dbs/src/gplx/dbs/qrys/Db_val_type.java
Normal file
36
140_dbs/src/gplx/dbs/qrys/Db_val_type.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_val_type {
|
||||
public static final byte // not serialized
|
||||
Tid_null = 0
|
||||
, Tid_bool = 1
|
||||
, Tid_byte = 2
|
||||
, Tid_int32 = 3
|
||||
, Tid_int64 = 4
|
||||
, Tid_date = 5
|
||||
, Tid_decimal = 6
|
||||
, Tid_float = 7
|
||||
, Tid_double = 8
|
||||
, Tid_bry = 9
|
||||
, Tid_varchar = 10
|
||||
, Tid_nvarchar = 11
|
||||
, Tid_rdr = 12
|
||||
, Tid_text = 13
|
||||
;
|
||||
}
|
||||
48
140_dbs/src/gplx/dbs/qrys/bats/Db_batch__journal_off.java
Normal file
48
140_dbs/src/gplx/dbs/qrys/bats/Db_batch__journal_off.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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.qrys.bats; import gplx.*; import gplx.dbs.*; import gplx.dbs.qrys.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*;
|
||||
public class Db_batch__journal_off {
|
||||
public static void Batch__init(Db_batch_mgr batch_mgr) {
|
||||
batch_mgr.Conn_bgn().Add(Db_batch__journal_off__conn_bgn.Instance);
|
||||
batch_mgr.Conn_end().Add(Db_batch__journal_off__conn_end.Instance);
|
||||
}
|
||||
public static void Batch__term(Db_batch_mgr batch_mgr) {
|
||||
batch_mgr.Conn_bgn().Del(Db_batch__journal_off__conn_bgn.Instance.Key());
|
||||
batch_mgr.Conn_end().Del(Db_batch__journal_off__conn_end.Instance.Key());
|
||||
}
|
||||
}
|
||||
class Db_batch__journal_off__conn_bgn implements Db_batch_itm {
|
||||
public String Key() {return KEY;} public static final String KEY = "journal_off.conn_bgn";
|
||||
public void Qry_bat__run(Db_engine engine) {
|
||||
engine.Props().Add(Sqlite_pragma.Const__journal_mode, Sqlite_pragma.Const__journal_mode__off);
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__journal__off()); // off b/c failure and corruption doesn't matter to import
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__synchronous__off()); // off b/c failure and corruption doesn't matter to import
|
||||
}
|
||||
public static final Db_batch__journal_off__conn_bgn Instance = new Db_batch__journal_off__conn_bgn(); Db_batch__journal_off__conn_bgn() {}
|
||||
}
|
||||
class Db_batch__journal_off__conn_end implements Db_batch_itm {
|
||||
public String Key() {return KEY;} public static final String KEY = "journal_off.conn_end";
|
||||
public void Qry_bat__run(Db_engine engine) {
|
||||
if (!engine.Props().Match(Sqlite_pragma.Const__journal_mode, Sqlite_pragma.Const__journal_mode__off)) return;
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__journal__delete());
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__synchronous__full());
|
||||
engine.Props().Del(Sqlite_pragma.Const__journal_mode);
|
||||
}
|
||||
public static final Db_batch__journal_off__conn_end Instance = new Db_batch__journal_off__conn_end(); Db_batch__journal_off__conn_end() {}
|
||||
}
|
||||
62
140_dbs/src/gplx/dbs/qrys/bats/Db_batch__journal_wal.java
Normal file
62
140_dbs/src/gplx/dbs/qrys/bats/Db_batch__journal_wal.java
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
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.qrys.bats; import gplx.*; import gplx.dbs.*; import gplx.dbs.qrys.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*;
|
||||
public class Db_batch__journal_wal {
|
||||
public static void Batch__init(Db_batch_mgr batch_mgr) {
|
||||
batch_mgr.Conn_bgn().Add(Db_batch__journal_wal__conn_bgn.Instance);
|
||||
batch_mgr.Conn_end().Add(Db_batch__journal_wal__conn_end.Instance);
|
||||
batch_mgr.Conn_bgn().Add(Db_batch__journal_wal__txn_end.Instance);
|
||||
}
|
||||
public static void Batch__term(Db_batch_mgr batch_mgr) {
|
||||
batch_mgr.Conn_bgn().Del(Db_batch__journal_wal__conn_bgn.Instance.Key());
|
||||
batch_mgr.Conn_end().Del(Db_batch__journal_wal__conn_end.Instance.Key());
|
||||
batch_mgr.Txn_end().Del(Db_batch__journal_wal__txn_end.Instance.Key());
|
||||
}
|
||||
}
|
||||
class Db_batch__journal_wal__conn_bgn implements Db_batch_itm {
|
||||
public String Key() {return KEY;} public static final String KEY = "journal_wal.conn_bgn";
|
||||
public void Qry_bat__run(Db_engine engine) {
|
||||
if (engine.Props().Match(Sqlite_pragma.Const__journal_mode, Sqlite_pragma.Const__journal_mode__off)) return; // if off, don't enable wal
|
||||
if (engine.Props().Match(Sqlite_pragma.Const__journal_mode, Sqlite_pragma.Const__journal_mode__wal)) return; // if wal, don't enable again
|
||||
engine.Props().Add(Sqlite_pragma.Const__journal_mode, Sqlite_pragma.Const__journal_mode__wal);
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__journal__wal());
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__synchronous__normal());
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__wal_autocheckpoint(0));
|
||||
}
|
||||
public static final Db_batch__journal_wal__conn_bgn Instance = new Db_batch__journal_wal__conn_bgn(); Db_batch__journal_wal__conn_bgn() {}
|
||||
}
|
||||
class Db_batch__journal_wal__conn_end implements Db_batch_itm {
|
||||
public String Key() {return KEY;} public static final String KEY = "journal_wal.conn_end";
|
||||
public void Qry_bat__run(Db_engine engine) {
|
||||
if (!engine.Props().Match(Sqlite_pragma.Const__journal_mode, Sqlite_pragma.Const__journal_mode__wal)) return; // if in off mode, don't enable wal
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__wal_checkpoint__truncate());
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__journal__delete());
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__synchronous__full());
|
||||
engine.Props().Del(Sqlite_pragma.Const__journal_mode);
|
||||
}
|
||||
public static final Db_batch__journal_wal__conn_end Instance = new Db_batch__journal_wal__conn_end(); Db_batch__journal_wal__conn_end() {}
|
||||
}
|
||||
class Db_batch__journal_wal__txn_end implements Db_batch_itm {
|
||||
public String Key() {return KEY;} public static final String KEY = "journal_wal.txn_end";
|
||||
public void Qry_bat__run(Db_engine engine) {
|
||||
if (!engine.Props().Match(Sqlite_pragma.Const__journal_mode, Sqlite_pragma.Const__journal_mode__wal)) return; // if in off mode, don't enable wal
|
||||
engine.Exec_as_obj(Sqlite_pragma.New__wal_checkpoint__truncate());
|
||||
}
|
||||
public static final Db_batch__journal_wal__txn_end Instance = new Db_batch__journal_wal__txn_end(); Db_batch__journal_wal__txn_end() {}
|
||||
}
|
||||
43
140_dbs/src/gplx/dbs/qrys/bats/Db_batch_grp.java
Normal file
43
140_dbs/src/gplx/dbs/qrys/bats/Db_batch_grp.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
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.qrys.bats; import gplx.*; import gplx.dbs.*; import gplx.dbs.qrys.*;
|
||||
import gplx.dbs.engines.*;
|
||||
public class Db_batch_grp {
|
||||
public final Ordered_hash hash = Ordered_hash_.New();
|
||||
public Db_batch_grp(byte tid) {this.tid = tid;}
|
||||
public byte Tid() {return tid;} private final byte tid;
|
||||
public int Len() {return hash.Len();}
|
||||
public Db_batch_itm Get_at(int idx) {return (Db_batch_itm)hash.Get_at(idx); }
|
||||
public void Add(Db_batch_itm itm) {hash.Add(itm.Key(), itm);}
|
||||
public void Del(String key) {hash.Del(key);}
|
||||
public void Run(Db_engine engine) {
|
||||
int len = hash.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_batch_itm itm = (Db_batch_itm)hash.Get_at(i);
|
||||
itm.Qry_bat__run(engine);
|
||||
}
|
||||
}
|
||||
public void Copy(Db_batch_grp src) {
|
||||
int len = src.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_batch_itm itm = src.Get_at(i);
|
||||
this.Add(itm);
|
||||
}
|
||||
}
|
||||
public static final byte Tid__conn_bgn = 0, Tid__conn_end = 1, Tid__txn_bgn = 2, Tid__txn_end = 3;
|
||||
}
|
||||
23
140_dbs/src/gplx/dbs/qrys/bats/Db_batch_itm.java
Normal file
23
140_dbs/src/gplx/dbs/qrys/bats/Db_batch_itm.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys.bats; import gplx.*; import gplx.dbs.*; import gplx.dbs.qrys.*;
|
||||
import gplx.dbs.engines.*;
|
||||
public interface Db_batch_itm {
|
||||
String Key();
|
||||
void Qry_bat__run(Db_engine engine);
|
||||
}
|
||||
28
140_dbs/src/gplx/dbs/qrys/bats/Db_batch_mgr.java
Normal file
28
140_dbs/src/gplx/dbs/qrys/bats/Db_batch_mgr.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys.bats; import gplx.*; import gplx.dbs.*; import gplx.dbs.qrys.*;
|
||||
public class Db_batch_mgr {
|
||||
public Db_batch_grp Conn_bgn() {return conn_bgn;} private final Db_batch_grp conn_bgn = new Db_batch_grp(Db_batch_grp.Tid__conn_bgn);
|
||||
public Db_batch_grp Conn_end() {return conn_end;} private final Db_batch_grp conn_end = new Db_batch_grp(Db_batch_grp.Tid__conn_end);
|
||||
public Db_batch_grp Txn_end () {return txn_end;} private final Db_batch_grp txn_end = new Db_batch_grp(Db_batch_grp.Tid__txn_end);
|
||||
public void Copy(String src_tid, Db_batch_mgr src) {
|
||||
conn_bgn.Copy(src.conn_bgn);
|
||||
conn_end.Copy(src.conn_end);
|
||||
txn_end.Copy(src.txn_end);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user