mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v1.6.5.1
This commit is contained in:
23
140_dbs/src_110_dbQry/gplx/dbs/Db_qry.java
Normal file
23
140_dbs/src_110_dbQry/gplx/dbs/Db_qry.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; import gplx.*;
|
||||
public interface Db_qry {
|
||||
String KeyOfDb_qry();
|
||||
boolean ExecRdrAble();
|
||||
String XtoSql();
|
||||
}
|
||||
59
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_.java
Normal file
59
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_.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; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_ {
|
||||
public static Db_qry_select select_cols_(String tbl, Criteria crt, String... cols){return select_().From_(tbl).Where_(crt).Cols_(cols);}
|
||||
public static Db_qry_select select_val_(String tbl, String col, Criteria crt) {return select_().From_(tbl).Where_(crt).Cols_(col);}
|
||||
public static Db_qry_select select_tbl_(String tbl) {return select_().From_(tbl);}
|
||||
public static Db_qry_select select_() {return Db_qry_select.new_();}
|
||||
public static Db_qry_delete delete_(String tbl, Criteria crt) {return Db_qry_delete.new_().BaseTable_(tbl).Where_(crt);}
|
||||
public static Db_qry_delete delete_tbl_(String tbl) {return Db_qry_delete.new_().BaseTable_(tbl);}
|
||||
public static Db_qry_insert insert_(String tbl) {return Db_qry_insert.new_().BaseTable_(tbl);}
|
||||
public static Db_qry_insert insert_common_(String tbl, KeyVal... pairs) {
|
||||
Db_qry_insert cmd = Db_qry_insert.new_().BaseTable_(tbl);
|
||||
for (KeyVal pair : pairs)
|
||||
cmd.Arg_obj_(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public static Db_qry_update update_(String tbl, Criteria crt) {
|
||||
Db_qry_update update = Db_qry_update.new_();
|
||||
update.From_(tbl);
|
||||
update.Where_(crt);
|
||||
return update;
|
||||
}
|
||||
public static Db_qry_update update_common_(String tbl, Criteria crt, KeyVal... pairs) {
|
||||
Db_qry_update cmd = Db_qry_update.new_();
|
||||
cmd.From_(tbl); cmd.Where_(crt);
|
||||
for (KeyVal pair : pairs)
|
||||
cmd.Arg_obj_(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
}
|
||||
public static final Object WhereAll = null;
|
||||
public static Db_qry as_(Object obj) {return obj instanceof Db_qry ? (Db_qry)obj : null;}
|
||||
}
|
||||
interface Db_qryWkr {
|
||||
Object Exec(Db_engine engine, Db_qry cmd);
|
||||
}
|
||||
class Db_qryWkr_ {
|
||||
public static final Db_qryWkr Null = new Db_qryWrk_null();
|
||||
}
|
||||
class Db_qryWrk_null implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engine, Db_qry cmd) {return null;}
|
||||
}
|
||||
40
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_arg_owner.java
Normal file
40
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_arg_owner.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public 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);
|
||||
}
|
||||
class Db_arg {
|
||||
public String Key() {return key;} private String key;
|
||||
public Object Val() {return val;} public Db_arg Val_(Object v) {val = v; return this;} 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;
|
||||
@gplx.Internal protected Db_arg(String key, Object val) {this.key = key; this.val = val;}
|
||||
}
|
||||
39
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_delete.java
Normal file
39
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_delete.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_delete implements Db_qry {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "DELETE";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
|
||||
public Db_qry_delete Where_add_(String key, int val) {
|
||||
Criteria crt = Db_crt_.eq_(key, val);
|
||||
where = Sql_where.merge_or_new_(where, crt);
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} public Db_qry_delete BaseTable_(String baseTable_) {baseTable = baseTable_; return this;} private String baseTable;
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_delete Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
|
||||
public static Db_qry_delete new_() {return new Db_qry_delete();} Db_qry_delete() {}
|
||||
public static Db_qry_delete new_all_(String tbl) {
|
||||
Db_qry_delete rv = new Db_qry_delete();
|
||||
rv.baseTable = tbl;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
44
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_dml_tst.java
Normal file
44
140_dbs/src_110_dbQry/gplx/dbs/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; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_dml_tst {
|
||||
@Test public void Delete_basic() {
|
||||
tst_XtoSql(Db_qry_delete.new_().BaseTable_("tbl0").Where_(Db_crt_.eq_("fld0", "val0"))
|
||||
, "DELETE FROM tbl0 WHERE fld0='val0'");
|
||||
}
|
||||
@Test public void Insert_basic() {
|
||||
tst_XtoSql(Db_qry_insert.new_().BaseTable_("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.XtoSql());}
|
||||
}
|
||||
34
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_flush.java
Normal file
34
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_flush.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_qry_flush implements Db_qry {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "FLUSH";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
|
||||
@gplx.Internal protected 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_.type_mismatch_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() {}
|
||||
}
|
||||
54
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_insert.java
Normal file
54
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_insert.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_qry_insert implements Db_qry_arg_owner {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "INSERT";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public Db_qry_arg_owner From_(String tbl) {baseTable = tbl; return this;}
|
||||
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.XtoDecimal(), 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_utf8_(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) {
|
||||
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 Select() {return select;} Db_qry_select select;
|
||||
public Db_qry_insert Select_(Db_qry_select 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_fld.new_(Sql_select_fld_base.Tbl_null, fld, fld));
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} public Db_qry_insert BaseTable_(String val) {baseTable = val; return this;} private String baseTable;
|
||||
@gplx.Internal protected KeyValHash Args() {return args;} KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected Sql_select_fld_list Cols() {return cols;} Sql_select_fld_list cols;
|
||||
|
||||
public static Db_qry_insert new_() {return new Db_qry_insert();} Db_qry_insert() {}
|
||||
}
|
||||
128
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_select.java
Normal file
128
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_select.java
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_select implements Db_qry {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "SELECT";
|
||||
public boolean ExecRdrAble() {return true;}
|
||||
public DataRdr Exec_qry_as_rdr(Db_provider provider) {return provider.Exec_qry_as_rdr(this);}
|
||||
public GfoNde ExecRdr_nde(Db_provider provider) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {return GfoNde_.rdr_(Exec_qry_as_rdr(provider));} finally {rdr.Rls();}
|
||||
}
|
||||
public Object ExecRdr_val(Db_provider provider) {
|
||||
DataRdr rdr = Exec_qry_as_rdr(provider);
|
||||
try {
|
||||
Object rv = null;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
rv = rdr.Read(cols.Flds().FetchAt(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 String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
|
||||
@gplx.Internal protected Sql_from From() {return from;} Sql_from from;
|
||||
public Db_qry_select From_(String tblName) {return From_(tblName, null);}
|
||||
public Db_qry_select From_(String tblName, String alias) {
|
||||
if (from != null) throw Err_.new_("super table already defined").Add("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 Join_(String name, String alias, Sql_join_itm... ary) {
|
||||
if (from == null) throw Err_.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;
|
||||
}
|
||||
|
||||
@gplx.Internal protected Sql_select Cols() {return cols;} Sql_select cols = Sql_select.All;
|
||||
public Db_qry_select Cols_all_() {return this;}
|
||||
public Db_qry_select Cols_alias_(String expr, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(expr, alias);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select 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 Cols_groupBy_max(String fld) {return Cols_groupBy_max(fld, fld);}
|
||||
public Db_qry_select Cols_groupBy_max(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_minMax.max_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_min(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_minMax.min_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_count(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_count.new_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_sum(String fld) {return Cols_groupBy_sum(fld, fld);}
|
||||
public Db_qry_select Cols_groupBy_sum(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_sum.new_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_select Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
|
||||
@gplx.Internal protected Sql_order_by OrderBy() {return orderBy;} Sql_order_by orderBy = null;
|
||||
public Db_qry_select OrderBy_(String fieldName, boolean ascending) {
|
||||
Sql_order_by_itm item = Sql_order_by_itm.new_(fieldName, ascending);
|
||||
orderBy = Sql_order_by.new_(item);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select OrderBy_asc_(String fieldName) {return OrderBy_(fieldName, true);}
|
||||
public Db_qry_select 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;
|
||||
}
|
||||
@gplx.Internal protected Sql_group_by GroupBy() {return groupBy;} Sql_group_by groupBy = null;
|
||||
public Db_qry_select GroupBy_(String... flds) {
|
||||
if (groupBy != null) throw Err_.new_("group by already defined").Add("group", groupBy);
|
||||
groupBy = Sql_group_by.new_(flds);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Distinct_() {cols.Distinct_set(true); return this;}
|
||||
@gplx.Internal protected int Limit() {return limit;} int limit = -1; public static final int Limit_disabled = -1;
|
||||
public Db_qry_select Limit_(int v) {this.limit = v; return this;}
|
||||
|
||||
public static Db_qry_select new_() {return new Db_qry_select();} Db_qry_select() {}
|
||||
}
|
||||
89
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_select_tst.java
Normal file
89
140_dbs/src_110_dbQry/gplx/dbs/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; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_qry_select_tst {
|
||||
@Before public void setup() {
|
||||
cmd = Db_qry_select.new_();
|
||||
} Db_qry_select 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 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.XtoSql());}
|
||||
}
|
||||
39
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_sql.java
Normal file
39
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_sql.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_qry_sql implements Db_qry {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "SQL";
|
||||
public boolean ExecRdrAble() {return isReader;} private boolean isReader;
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return sql;} private String sql;
|
||||
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 Err_.type_mismatch_exc_(exc, Db_qry_sql.class, obj);}}
|
||||
}
|
||||
53
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_update.java
Normal file
53
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_update.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_update implements Db_qry_arg_owner {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "UPDATE";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
|
||||
public Db_qry_arg_owner From_(String tbl) {baseTable = tbl; return this;}
|
||||
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.XtoDecimal(), 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_utf8_(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) {
|
||||
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);}
|
||||
Db_qry_arg_owner Key_arg_obj_(String k, Object v) {
|
||||
Criteria crt = Db_crt_.eq_(k, v);
|
||||
where = Sql_where.merge_or_new_(where, crt);
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} private String baseTable;
|
||||
@gplx.Internal protected KeyValHash Args() {return args;} final KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
|
||||
|
||||
public static Db_qry_update new_() {return new Db_qry_update();} Db_qry_update() {}
|
||||
}
|
||||
41
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt.java
Normal file
41
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_stmt extends RlsAble {
|
||||
Db_provider Provider();
|
||||
Db_stmt Val_bool_(boolean v);
|
||||
Db_stmt Val_byte_(byte v);
|
||||
Db_stmt Val_byte_by_bool_(boolean v);
|
||||
Db_stmt Val_int_(int v);
|
||||
Db_stmt Val_long_(long v);
|
||||
Db_stmt Val_float_(float v);
|
||||
Db_stmt Val_double_(double v);
|
||||
Db_stmt Val_decimal_(DecimalAdp v);
|
||||
Db_stmt Val_bry_(byte[] v);
|
||||
Db_stmt Val_bry_by_str_(String v);
|
||||
Db_stmt Val_str_by_bry_(byte[] v);
|
||||
Db_stmt Val_str_(String v);
|
||||
Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr rdr, long rdr_len);
|
||||
boolean Exec_insert();
|
||||
int Exec_update();
|
||||
int Exec_delete();
|
||||
DataRdr Exec_select();
|
||||
Object Exec_select_val();
|
||||
Db_stmt Clear();
|
||||
Db_stmt New();
|
||||
}
|
||||
50
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_.java
Normal file
50
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_stmt_ {
|
||||
public static final Db_stmt Null = new Db_stmt_sql();
|
||||
public static Db_stmt new_insert_(Db_provider provider, String tbl, String... flds) {
|
||||
Db_qry_insert qry = Db_qry_insert.new_().BaseTable_(tbl);
|
||||
int len = flds.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
qry.Arg_obj_(flds[i], null);
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_update_(Db_provider provider, String tbl, String[] where, String... flds) {
|
||||
Db_qry_update qry = Db_qry_.update_(tbl, Db_crt_.eq_many_(where));
|
||||
int len = flds.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
qry.Arg_obj_(flds[i], null);
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_delete_(Db_provider provider, String tbl, String... where) {
|
||||
Db_qry_delete qry = Db_qry_.delete_(tbl, Db_crt_.eq_many_(where));
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_select_(Db_provider provider, String tbl, String[] where, String... flds) {
|
||||
Db_qry_select qry = Db_qry_.select_cols_(tbl, Db_crt_.eq_many_(where), flds);
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_select_in_(Db_provider provider, String tbl, String in_fld, Object[] in_vals, String... flds) {
|
||||
Db_qry_select qry = Db_qry_.select_cols_(tbl, Db_crt_.in_(in_fld, in_vals), flds).OrderBy_asc_(in_fld);
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_select_all_(Db_provider provider, String tbl) {
|
||||
return provider.Prepare(Db_qry_.select_tbl_(tbl));
|
||||
}
|
||||
}
|
||||
54
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_bldr.java
Normal file
54
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_bldr.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_stmt_bldr implements RlsAble {
|
||||
private Db_provider provider;
|
||||
private Db_stmt create, update, delete;
|
||||
private String tbl_name; private String[] flds_keys, flds_vals, flds_all;
|
||||
public Db_stmt_bldr(String tbl_name, String[] flds_keys, String... flds_vals) {
|
||||
this.tbl_name = tbl_name; this.flds_keys = flds_keys; this.flds_vals = flds_vals;
|
||||
flds_all = String_.Ary_add(flds_keys, flds_vals);
|
||||
}
|
||||
public Db_stmt_bldr Init(Db_provider v) {
|
||||
this.provider = v;
|
||||
provider.Txn_mgr().Txn_bgn_if_none();
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Get(byte cmd_mode) {
|
||||
switch (cmd_mode) {
|
||||
case Db_cmd_mode.Create: if (create == null) create = Db_stmt_.new_insert_(provider, tbl_name, flds_all); return create;
|
||||
case Db_cmd_mode.Update: if (update == null) update = Db_stmt_.new_update_(provider, tbl_name, flds_keys, flds_vals); return update;
|
||||
case Db_cmd_mode.Delete: if (delete == null) delete = Db_stmt_.new_delete_(provider, tbl_name, flds_keys); return delete;
|
||||
case Db_cmd_mode.Ignore: return Db_stmt_.Null;
|
||||
default: throw Err_.unhandled(cmd_mode);
|
||||
}
|
||||
}
|
||||
public void Commit() {
|
||||
provider.Txn_mgr().Txn_end_all();
|
||||
}
|
||||
public void Rls() {
|
||||
provider = null;
|
||||
create = Rls(create);
|
||||
update = Rls(update);
|
||||
delete = Rls(delete);
|
||||
}
|
||||
private Db_stmt Rls(Db_stmt stmt) {
|
||||
if (stmt != null) stmt.Rls();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
104
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_cmd.java
Normal file
104
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_cmd.java
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import java.sql.*;
|
||||
class Db_stmt_cmd implements Db_stmt {
|
||||
Db_engine engine;
|
||||
PreparedStatement stmt = null;
|
||||
String sql;
|
||||
int val_idx = 0;
|
||||
public Db_stmt_cmd(Db_provider provider, Db_qry qry) {
|
||||
this.provider = provider; this.engine = provider.Engine();
|
||||
sql = Sql_cmd_wtr_.Ansi.XtoSqlQry(qry, true);
|
||||
New();
|
||||
}
|
||||
public Db_stmt New() {
|
||||
stmt = (PreparedStatement)engine.New_db_cmd(sql);
|
||||
return this;
|
||||
}
|
||||
public Db_provider Provider() {return provider;} Db_provider provider;
|
||||
public Db_stmt Val_bool_(boolean v) {
|
||||
try {stmt.setBoolean(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_byte_by_bool_(boolean v) {return Val_byte_(v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Val_byte_(byte v) {
|
||||
try {stmt.setByte(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_int_(int v) {
|
||||
try {stmt.setInt(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_long_(long v) {
|
||||
try {stmt.setLong(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_float_(float v) {
|
||||
try {stmt.setFloat(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_double_(double v) {
|
||||
try {stmt.setDouble(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_decimal_(DecimalAdp v) {
|
||||
try {stmt.setBigDecimal(++val_idx, v.XtoDecimal());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_bry_by_str_(String v) {return Val_bry_(Bry_.new_utf8_(v));}
|
||||
public Db_stmt Val_bry_(byte[] v) {
|
||||
try {stmt.setBytes(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_str_by_bry_(byte[] v) {return Val_str_(String_.new_utf8_(v));}
|
||||
public Db_stmt Val_str_(String v) {
|
||||
try {stmt.setString(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {stmt.setBinaryStream(++val_idx, (java.io.InputStream)v.Under(), (int)rdr_len);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = stmt.execute(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = engine.NewDataRdr(stmt.executeQuery(), sql); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(engine.NewDataRdr(stmt.executeQuery(), sql)); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
try {stmt.clearBatch();}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to clear parameters;", sql);}
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
if (stmt == null) return; // Null instance
|
||||
try {stmt.close();}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to close command: {0}", sql);}
|
||||
}
|
||||
}
|
||||
124
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_sql.java
Normal file
124
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_sql.java
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
class Db_stmt_sql implements Db_stmt {
|
||||
Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
int val_idx = 0;
|
||||
public Db_provider Provider() {return provider;} public void Provider_(Db_provider v) {this.provider = v;} Db_provider provider;
|
||||
public Db_stmt New() {return this;}
|
||||
public Db_stmt Val_bool_(boolean v) {
|
||||
try {Add(++val_idx, v ? "true" : "false");} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_byte_by_bool_(boolean v) {return Val_byte_(v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Val_byte_(byte v) {
|
||||
try {Add(++val_idx, Byte_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_int_(int v) {
|
||||
try {Add(++val_idx, Int_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_long_(long v) {
|
||||
try {Add(++val_idx, Long_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_float_(float v) {
|
||||
try {Add(++val_idx, Float_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_double_(double v) {
|
||||
try {Add(++val_idx, Double_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_decimal_(DecimalAdp v) {
|
||||
try {Add(++val_idx, v.XtoStr());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_bry_by_str_(String v) {return Val_bry_(Bry_.new_utf8_(v));}
|
||||
public Db_stmt Val_bry_(byte[] v) { // HACK: convert to String b/c tdb does not support byte[]
|
||||
try {Add(++val_idx, Val_str_wrap(String_.new_utf8_(v)));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_str_by_bry_(byte[] v) {return Val_str_(String_.new_utf8_(v));}
|
||||
public Db_stmt Val_str_(String v) {
|
||||
try {Add(++val_idx, Val_str_wrap(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
gplx.ios.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(++val_idx, bfr.XtoStrAndClear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
String Val_str_wrap(String v) {
|
||||
return "'" + String_.Replace(v, "'", "\\'") + "'";
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())) != 0; return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = provider.Exec_qry_as_rdr(Db_qry_sql.rdr_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
this.Clear();
|
||||
}
|
||||
public void Add(String v) {Add(-1, v);}
|
||||
public void Add(int idx, String v) {args.Add(v);} ListAdp args = ListAdp_.new_();
|
||||
public String Xto_sql() {
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, (Object[])args.XtoAryAndClear(Object.class));
|
||||
return tmp_bfr.XtoStrAndClear();
|
||||
}
|
||||
String sql_orig;
|
||||
public Db_stmt Parse(String sql_str) {
|
||||
this.sql_orig = sql_str;
|
||||
int arg_idx = 0;
|
||||
byte[] src = Bry_.new_utf8_(sql_str);
|
||||
int pos_prv = 0;
|
||||
tmp_bfr.Clear();
|
||||
while (true) {
|
||||
int pos_cur = Bry_finder.Find_fwd(src, Byte_ascii.Question, pos_prv);
|
||||
if (pos_cur == Bry_.NotFound) break;
|
||||
tmp_bfr.Add_mid(src, 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(src, pos_prv, src.length);
|
||||
tmp_fmtr.Fmt_(tmp_bfr.XtoAryAndClear());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
29
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_sql_tst.java
Normal file
29
140_dbs/src_110_dbQry/gplx/dbs/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; import gplx.*;
|
||||
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("UPDATE tbl_0 SET col_0 = ? WHERE col_1 = ?");
|
||||
stmt.Add("1");
|
||||
stmt.Add("2");
|
||||
Tfds.Eq("UPDATE tbl_0 SET col_0 = 1 WHERE col_1 = 2", stmt.Xto_sql());
|
||||
}
|
||||
}
|
||||
48
140_dbs/src_110_dbQry/gplx/dbs/Db_txn_mgr.java
Normal file
48
140_dbs/src_110_dbQry/gplx/dbs/Db_txn_mgr.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; import gplx.*;
|
||||
public interface Db_txn_mgr {
|
||||
int Txn_depth();
|
||||
void Txn_bgn_if_none();
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
void Txn_end_all();
|
||||
void Txn_end_all_bgn_if_none();
|
||||
int Txn_count(); void Txn_count_(int v);
|
||||
}
|
||||
class Db_txn_mgr_base implements Db_txn_mgr {
|
||||
public Db_txn_mgr_base(Db_engine engine) {this.engine = engine;} Db_engine engine;
|
||||
public int Txn_depth() {return txn_depth;} int txn_depth; // NOTE: only support 1 level for now;
|
||||
public void Txn_bgn_if_none() {if (txn_depth == 0) this.Txn_bgn();}
|
||||
public void Txn_bgn() {
|
||||
engine.Txn_bgn();
|
||||
++txn_depth;
|
||||
}
|
||||
public void Txn_end_all() {this.Txn_end();}
|
||||
public void Txn_end() {
|
||||
if (txn_depth == 0) return;
|
||||
engine.Txn_end();
|
||||
--txn_depth;
|
||||
txn_count = 0;
|
||||
}
|
||||
public void Txn_end_all_bgn_if_none() {
|
||||
this.Txn_end_all();
|
||||
this.Txn_bgn_if_none();
|
||||
}
|
||||
public int Txn_count() {return txn_count;} public void Txn_count_(int v) {txn_count = v;} int txn_count;
|
||||
}
|
||||
35
140_dbs/src_110_dbQry/gplx/dbs/Db_val_type.java
Normal file
35
140_dbs/src_110_dbQry/gplx/dbs/Db_val_type.java
Normal 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; import gplx.*;
|
||||
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
|
||||
;
|
||||
}
|
||||
Reference in New Issue
Block a user