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-07-12 21:10:02 -04:00
commit 794b5a232f
3099 changed files with 238212 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
public class Db_arg {
public Db_arg(String key, Object val) {this.key = key; this.val = val;}
public String Key() {return key;} private String key;
public Object Val() {return val;} public void Val_(Object v) {this.val = v;} private Object val;
public byte Val_tid() {return val_tid;} public Db_arg Val_tid_(byte v) {val_tid = v; return this;} private byte val_tid = Db_val_type.Tid_null;
}

View File

@@ -0,0 +1,131 @@
/*
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__select_cmd 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 String Xto_sql_prepare() {return Sql_qry_wtr_.I.Xto_str(this, true);}
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();}
}
public Object ExecRdr_val(Db_conn conn) {
DataRdr rdr = Exec_qry_as_rdr(conn);
try {
Object rv = null;
if (rdr.MoveNextPeer()) {
rv = rdr.Read(cols.Flds().Get_at(0).Fld()); // NOTE: need to access from flds for tdb
}
return rv;
} finally {rdr.Rls();}
}
public static Object Rdr_to_val(DataRdr rdr) {
try {
Object rv = null;
if (rdr.MoveNextPeer()) {
rv = rdr.ReadAt(0);
}
return rv;
} finally {rdr.Rls();}
}
public Sql_from From() {return from;} Sql_from from;
public Db_qry__select_cmd From_(String tblName) {return From_(tblName, null);}
public Db_qry__select_cmd From_(String tblName, String alias) {
if (from != null) throw Exc_.new_("super table already defined", "from", from.Tbls().Count());
from = Sql_from.new_(Sql_tbl_src.new_().JoinType_(Sql_join_itmType.From).TblName_(tblName).Alias_(alias));
return this;
}
public Db_qry__select_cmd Join_(String name, String alias, Sql_join_itm... ary) {
if (from == null) throw Exc_.new_("super table is not defined");
Sql_tbl_src tbl = Sql_tbl_src.new_().JoinType_(Sql_join_itmType.Inner).TblName_(name).Alias_(alias);
for (Sql_join_itm itm : ary)
tbl.JoinLinks().Add(itm);
from.Tbls().Add(tbl);
return this;
}
public 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_cmd Cols_all_() {return this;}
public Db_qry__select_cmd Cols_alias_(String expr, String alias) {
if (cols == Sql_select.All) cols = Sql_select.new_();
cols.Add(expr, alias);
return this;
}
public Db_qry__select_cmd Cols_(String... ary) {
if (cols == Sql_select.All) cols = Sql_select.new_();
for (String itm : ary)
cols.Add(itm);
return this;
}
public Db_qry__select_cmd Cols_groupBy_max(String fld) {return Cols_groupBy_max(fld, fld);}
public Db_qry__select_cmd Cols_groupBy_max(String fld, String alias) {
if (cols == Sql_select.All) cols = Sql_select.new_();
cols.Add(Sql_select_fld_.new_max(Sql_select_fld_base.Tbl_null, fld, alias));
return this;
}
public Db_qry__select_cmd Cols_groupBy_min(String fld, String alias) {
if (cols == Sql_select.All) cols = Sql_select.new_();
cols.Add(Sql_select_fld_.new_min(Sql_select_fld_base.Tbl_null, fld, alias));
return this;
}
public Db_qry__select_cmd Cols_groupBy_count(String fld, String alias) {
if (cols == Sql_select.All) cols = Sql_select.new_();
cols.Add(Sql_select_fld_.new_count(Sql_select_fld_base.Tbl_null, fld, alias));
return this;
}
public Db_qry__select_cmd Cols_groupBy_sum(String fld) {return Cols_groupBy_sum(fld, fld);}
public Db_qry__select_cmd Cols_groupBy_sum(String fld, String alias) {
if (cols == Sql_select.All) cols = Sql_select.new_();
cols.Add(Sql_select_fld_.new_sum(Sql_select_fld_base.Tbl_null, fld, alias));
return this;
}
public Criteria Where() {return where;} public Db_qry__select_cmd Where_(Criteria crt) {where = crt; return this;} Criteria where;
public Sql_order_by OrderBy() {return orderBy;} Sql_order_by orderBy = null;
public Db_qry__select_cmd OrderBy_(String fieldName, boolean ascending) {
Sql_order_by_itm item = Sql_order_by_itm.new_(fieldName, ascending);
orderBy = Sql_order_by.new_(item);
return this;
}
public Db_qry__select_cmd OrderBy_asc_(String fieldName) {return OrderBy_(fieldName, true);}
public Db_qry__select_cmd OrderBy_many_(String... fldNames) {
Sql_order_by_itm[] ary = new Sql_order_by_itm[fldNames.length];
for (int i = 0; i < fldNames.length; i++)
ary[i] = Sql_order_by_itm.new_(fldNames[i], true);
orderBy = Sql_order_by.new_(ary);
return this;
}
public Sql_group_by GroupBy() {return groupBy;} Sql_group_by groupBy = null;
public Db_qry__select_cmd GroupBy_(String... flds) {
if (groupBy != null) throw Exc_.new_("group by already defined", "group", groupBy);
groupBy = Sql_group_by.new_(flds);
return this;
}
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 Db_qry__select_cmd Distinct_() {cols.Distinct_set(true); return this;}
public int Limit() {return limit;} int limit = -1; public static final int Limit_disabled = -1;
public Db_qry__select_cmd Limit_(int v) {this.limit = v; return this;}
public static Db_qry__select_cmd new_() {return new Db_qry__select_cmd();} Db_qry__select_cmd() {}
}

View 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 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 XtoSql() {return Xto_sql();}
public String Xto_sql() {
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.XtoStr();
}
}
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.Xto_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;
}

View 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_qry_arg_owner extends Db_qry {
Db_qry_arg_owner From_(String tbl);
Db_qry_arg_owner Key_arg_(String k, int v);
Db_qry_arg_owner Key_arg_(String k, String v);
Db_qry_arg_owner Arg_(String k, int v);
Db_qry_arg_owner Arg_(String k, long v);
Db_qry_arg_owner Arg_(String k, String v);
Db_qry_arg_owner Arg_(String k, byte[] v);
Db_qry_arg_owner Arg_(String k, DateAdp v);
Db_qry_arg_owner Arg_(String k, DecimalAdp v);
Db_qry_arg_owner Arg_byte_(String k, byte v);
Db_qry_arg_owner Arg_bry_(String k, byte[] v);
Db_qry_arg_owner Arg_obj_(String key, Object val);
Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid);
}

View File

@@ -0,0 +1,31 @@
/*
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 false;}
public String Base_table() {return base_table;} private final String base_table;
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 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

@@ -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_.eq_("fld0", "val0"))
, "DELETE FROM tbl0 WHERE fld0='val0'");
}
@Test public void Insert_basic() {
tst_XtoSql(new Db_qry_insert("tbl0").Arg_("id", 0).Arg_("name", "me").Arg_("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 = Db_qry_update.new_();
qry.From_("tbl0");
qry.Where_(Db_crt_.eq_("id", 0)).Arg_("name", "me");
tst_XtoSql(qry, "UPDATE tbl0 SET name='me' WHERE id=0");
}
@Test public void Update_all() {
Db_qry_update qry = Db_qry_update.new_();
qry.From_("tbl0");
qry.Arg_("id", 1).Arg_("name", "me").Arg_("startTime", DateAdp_.parse_gplx("2007-12-23"));
qry.Where_(Criteria_.And(Db_crt_.eq_("id", 0), Db_crt_.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.Xto_sql());}
}

View 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 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[] 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 Exc_.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() {}
}

View File

@@ -0,0 +1,67 @@
/*
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_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 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);}
public Db_qry_arg_owner Arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
public Db_qry_arg_owner Arg_(String k, long v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int64);}
public Db_qry_arg_owner Arg_(String k, String v) {return Arg_obj_type_(k, v, Db_val_type.Tid_varchar);}
public Db_qry_arg_owner Arg_bry_(String k, byte[] v) {return Arg_obj_type_(k, v, Db_val_type.Tid_bry);}
public Db_qry_arg_owner Arg_(String k, byte[] v) {return Arg_obj_type_(k, String_.new_u8(v), Db_val_type.Tid_varchar);}
public Db_qry_arg_owner Arg_obj_(String k, Object v) {return Arg_obj_type_(k, v, Db_val_type.Tid_null);}
public Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid) {
if (key == Db_meta_fld.Key_null) return this;
Db_arg arg = new Db_arg(key, val).Val_tid_(val_tid);
args.Add(arg.Key(), arg);
return this;
}
public Db_qry_arg_owner Key_arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
public Db_qry_arg_owner Key_arg_(String k, String v) {return Arg_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 = Sql_select_fld_list.new_();
for (String fld : ary)
cols.Add(Sql_select_fld_.new_fld(Sql_select_fld_base.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.Arg_obj_(keys[i], null);
return rv;
}
}

View 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 = Db_qry__select_cmd.new_();
} 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_.eq_("fld0", 0));
expd = "SELECT * FROM tbl0 WHERE fld0=0";
tst_XtoStr(cmd, expd);
}
@Test public void Join() {
cmd.From_("tbl0").Join_("tbl1", "t1", Sql_join_itm.new_("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").OrderBy_("fld0", true);
expd = "SELECT * FROM tbl0 ORDER BY fld0";
tst_XtoStr(cmd, expd);
}
@Test public void OrderByMany() {
cmd.From_("tbl0").OrderBy_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.XtoStr(), 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.XtoStr(), 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.XtoStr(), expd);
// }
void tst_XtoStr(Db_qry qry, String expd) {Tfds.Eq(expd, cmd.Xto_sql());}
}

View 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 Exc_.new_unimplemented();}
public String Xto_sql() {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);}
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 Exc_.new_type_mismatch_w_exc(exc, Db_qry_sql.class, obj);}}
public static String Gen_sql(Db_qry qry, Object... args) {
byte[] src = Bry_.new_u8(Sql_qry_wtr_.Gen_placeholder_parameters(qry));
int src_len = src.length;
int args_idx = 0, args_len = args.length, pos = 0;
Bry_bfr bfr = Bry_bfr.new_(src_len);
while (pos < src_len) {
int question_pos = Bry_finder.Find_fwd(src, Byte_ascii.Question, pos);
if (question_pos == Bry_.NotFound)
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.Xto_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 (ClassAdp_.Eq(val_type, Int_.Cls_ref_type))
bfr.Add_int_variable(Int_.cast_(val));
else if (ClassAdp_.Eq(val_type, Bool_.Cls_ref_type))
bfr.Add_int_fixed(1, Bool_.Xto_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 (ClassAdp_.Eq(val_type, Double_.Cls_ref_type))
bfr.Add_double(Double_.cast_(val));
else if (ClassAdp_.Eq(val_type, Long_.Cls_ref_type))
bfr.Add_long_variable(Long_.cast_(val));
else if (ClassAdp_.Eq(val_type, Float_.Cls_ref_type))
bfr.Add_float(Float_.cast_(val));
else if (ClassAdp_.Eq(val_type, Byte_.Cls_ref_type))
bfr.Add_byte(Byte_.cast_(val));
else if (ClassAdp_.Eq(val_type, DateAdp_.Cls_ref_type))
bfr.Add_byte_apos().Add_str(DateAdp_.cast_(val).XtoStr_gplx_long()).Add_byte_apos();
else if (ClassAdp_.Eq(val_type, DecimalAdp_.Cls_ref_type))
bfr.Add_str(DecimalAdp_.cast_(val).Xto_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("''");
}

View File

@@ -0,0 +1,47 @@
/*
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_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"), DecimalAdp_.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 {
public void Clear() {}
public void Test_qry(Db_qry qry, Object[] vals, String expd) {Tfds.Eq(expd, Db_qry_sql.Gen_sql(qry, vals));}
}

View File

@@ -0,0 +1,61 @@
/*
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_qry_arg_owner {
public int Tid() {return Db_qry_.Tid_update;}
public boolean Exec_is_rdr() {return false;}
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_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);}
public Db_qry_arg_owner Arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
public Db_qry_arg_owner Arg_(String k, long v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int64);}
public Db_qry_arg_owner Arg_(String k, String v) {return Arg_obj_type_(k, v, Db_val_type.Tid_varchar);}
public Db_qry_arg_owner Arg_bry_(String k, byte[] v) {return Arg_obj_type_(k, v, Db_val_type.Tid_bry);}
public Db_qry_arg_owner Arg_(String k, byte[] v) {return Arg_obj_type_(k, String_.new_u8(v), Db_val_type.Tid_varchar);}
public Db_qry_arg_owner Arg_obj_(String k, Object v) {return Arg_obj_type_(k, v, Db_val_type.Tid_null);}
public Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid) {
if (key == Db_meta_fld.Key_null) return this;
Db_arg arg = new Db_arg(key, val).Val_tid_(val_tid);
args.Add(arg.Key(), arg);
return this;
}
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);}
private Db_qry_arg_owner Key_arg_obj_(String k, Object v) {
Criteria crt = Db_crt_.eq_(k, v);
where = where == null ? crt : Criteria_.And(where, crt);
return this;
}
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_for_update = update;
int len = update.length;
for (int i = 0; i < len; i++)
rv.Arg_obj_(update[i], null);
return rv;
}
}

View File

@@ -0,0 +1,163 @@
/*
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.*;
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).Xto_sql() : Sql_qry_wtr_.I.Xto_str(qry, true);
Reset_stmt();
}
public Db_stmt Reset_stmt() {
stmt = (PreparedStatement)engine.New_stmt_prep_as_obj(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 == Db_meta_fld.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 Exc_.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(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 == Db_meta_fld.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 Exc_.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 == Db_meta_fld.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 Exc_.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 == Db_meta_fld.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 Exc_.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 == Db_meta_fld.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 Exc_.new_exc(e, "db", "failed to add value", "type", "double", "val", v, "sql", sql);}
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, Key_na, v);}
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
try {stmt.setBigDecimal(++val_idx, v.Xto_decimal());} catch (Exception e) {this.Rls(); throw Exc_.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 == Db_meta_fld.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 Exc_.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);}
private Db_stmt Add_str(boolean where, String k, String v) {
if (k == Db_meta_fld.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 Exc_.new_exc(e, "db", "failed to add value", "type", "String", "val", v, "sql", sql);}
return this;
}
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
try {stmt.setBinaryStream(++val_idx, (java.io.InputStream)v.Under(), (int)rdr_len);} catch (Exception e) {throw Exc_.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 Exc_.new_exc(e, "db_stmt", "insert failed", "url", engine.Conn_info().Xto_api(), "sql", sql);
}
}
public int Exec_update() {
try {int rv = stmt.executeUpdate(); return rv;}
catch (Exception e) {
this.Rls();
Reset_stmt();
throw Exc_.new_exc(e, "db_stmt", "update failed", "url", engine.Conn_info().Xto_api(), "sql", sql);
}
}
public int Exec_delete() {
try {int rv = stmt.executeUpdate(); return rv;}
catch (Exception e) {
this.Rls();
Reset_stmt();
throw Exc_.new_exc(e, "db_stmt", "delete failed", "url", engine.Conn_info().Xto_api(), "sql", sql);
}
}
public DataRdr Exec_select() {
try {DataRdr rv = engine.New_rdr(stmt.executeQuery(), sql); return rv;} catch (Exception e) {throw Exc_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql);}
}
public Db_rdr Exec_select__rls_auto() {
try {return engine.New_rdr__rls_auto(this, stmt.executeQuery(), sql);} catch (Exception e) {throw Exc_.new_exc(e, "db", "select failed", "sql", sql);}
}
public Db_rdr Exec_select__rls_manual() {
try {return engine.New_rdr__rls_manual(stmt.executeQuery(), sql);} catch (Exception e) {throw Exc_.new_exc(e, "db", "select failed", "sql", sql);}
}
public Object Exec_select_val() {
try {Object rv = Db_qry__select_cmd.Rdr_to_val(engine.New_rdr(stmt.executeQuery(), sql)); return rv;} catch (Exception e) {throw Exc_.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 Exc_.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 Exc_.new_exc(e, "db", "failed to close command", "sql", sql);}
}
}

View File

@@ -0,0 +1,177 @@
/*
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.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_.Xto_str(v));} catch (Exception e) {throw Exc_.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(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_.Xto_str(v));} catch (Exception e) {throw Exc_.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_.Xto_str(v));} catch (Exception e) {throw Exc_.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_.Xto_str(v));} catch (Exception e) {throw Exc_.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_.Xto_str(v));} catch (Exception e) {throw Exc_.new_exc(e, "db", "failed to add value", "type", "double", "val", 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, Key_na, v);}
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
try {Add(k, v.Xto_str());} catch (Exception e) {throw Exc_.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 Exc_.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 Exc_.new_exc(e, "db", "failed to add value", "type", "String", "val", 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(Key_na, bfr.Xto_str_and_clear());
} catch (Exception e) {throw Exc_.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 Exc_.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 Exc_.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 Exc_.new_exc(e, "db", "failed to exec prepared statement", "sql", sql_orig);}
}
public DataRdr Exec_select() {
try {DataRdr rv = conn.Exec_qry_as_rdr(Db_qry_sql.rdr_(this.Xto_sql())); return rv;} catch (Exception e) {throw Exc_.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 = Db_qry__select_cmd.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Exc_.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 == Db_meta_fld.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.Xto_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_finder.Find_fwd(sql_bry, Byte_ascii.Question, pos_prv); if (pos_cur == Bry_.NotFound) 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.Xto_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 (ClassAdp_.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.Xto_str_and_clear();
}
}

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

View File

@@ -0,0 +1,35 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.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
;
}