mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v1.9.2.1
This commit is contained in:
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_qry {
|
||||
int Tid();
|
||||
String KeyOfDb_qry();
|
||||
boolean ExecRdrAble();
|
||||
String XtoSql();
|
||||
|
||||
@@ -47,6 +47,7 @@ public class Db_qry_ {
|
||||
}
|
||||
public static final Object WhereAll = null;
|
||||
public static Db_qry as_(Object obj) {return obj instanceof Db_qry ? (Db_qry)obj : null;}
|
||||
public static final int Tid_basic = 0, Tid_select_in_tbl = 1;
|
||||
}
|
||||
interface Db_qryWkr {
|
||||
Object Exec(Db_engine engine, Db_qry cmd);
|
||||
|
||||
60
140_dbs/src_110_dbQry/gplx/dbs/Db_qry__select_in_tbl.java
Normal file
60
140_dbs/src_110_dbQry/gplx/dbs/Db_qry__select_in_tbl.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
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__select_in_tbl implements Db_qry {
|
||||
public Db_qry__select_in_tbl(String tbl_name, String[] select_flds, String[] where_flds, String group_by_sql, String having_sql, String order_by_sql, String limit_sql) {
|
||||
this.tbl_name = tbl_name; this.select_flds = select_flds; this.where_flds = where_flds; this.group_by_sql = group_by_sql; this.having_sql = having_sql; this.order_by_sql = order_by_sql; this.limit_sql = limit_sql;
|
||||
}
|
||||
public int Tid() {return Db_qry_.Tid_select_in_tbl;}
|
||||
public String Tbl_name() {return tbl_name;} private final String tbl_name;
|
||||
public String[] Select_flds() {return select_flds;} private final String[] select_flds;
|
||||
public String[] Where_flds() {return where_flds;} private final String[] where_flds;
|
||||
public 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;} private final String order_by_sql;
|
||||
public String Limit_sql() {return limit_sql;} private final String limit_sql;
|
||||
public static Db_qry__select_in_tbl new_(String tbl_name, String[] where_flds, String[] select_flds) {return new Db_qry__select_in_tbl(tbl_name, select_flds, where_flds, null, null, null, null);}
|
||||
public String KeyOfDb_qry() {return "select_in_tbl";}
|
||||
public boolean ExecRdrAble() {return true;}
|
||||
public String XtoSql() {return Xto_sql();}
|
||||
public String Xto_sql() {
|
||||
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(tbl_name).Add(" ");
|
||||
if (where_flds != null) {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_sql);
|
||||
if (limit_sql != null) sb.Add(limit_sql);
|
||||
return sb.XtoStr();
|
||||
}
|
||||
}
|
||||
@@ -18,11 +18,11 @@ 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 int Tid() {return Db_qry_.Tid_basic;}
|
||||
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);
|
||||
|
||||
@@ -17,6 +17,7 @@ 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 int Tid() {return Db_qry_.Tid_basic;}
|
||||
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);}
|
||||
|
||||
@@ -17,12 +17,13 @@ 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 int Tid() {return Db_qry_.Tid_basic;}
|
||||
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, 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);}
|
||||
|
||||
@@ -18,6 +18,7 @@ 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 int Tid() {return Db_qry_.Tid_basic;}
|
||||
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);}
|
||||
|
||||
@@ -17,6 +17,7 @@ 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 int Tid() {return Db_qry_.Tid_basic;}
|
||||
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);}
|
||||
|
||||
@@ -18,13 +18,14 @@ 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 int Tid() {return Db_qry_.Tid_basic;}
|
||||
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, 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);}
|
||||
|
||||
@@ -35,6 +35,7 @@ public interface Db_stmt extends RlsAble {
|
||||
int Exec_update();
|
||||
int Exec_delete();
|
||||
DataRdr Exec_select();
|
||||
Db_rdr Exec_select_as_rdr();
|
||||
Object Exec_select_val();
|
||||
Db_stmt Clear();
|
||||
Db_stmt New();
|
||||
|
||||
@@ -47,4 +47,7 @@ public class Db_stmt_ {
|
||||
public static Db_stmt new_select_all_(Db_provider provider, String tbl) {
|
||||
return provider.Prepare(Db_qry_.select_tbl_(tbl));
|
||||
}
|
||||
public static Db_stmt new_select_as_rdr(Db_provider provider, Db_qry__select_in_tbl qry) {
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,13 @@ 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;
|
||||
private Db_engine engine;
|
||||
private PreparedStatement stmt = null;
|
||||
private String sql;
|
||||
private 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);
|
||||
sql = qry.Tid() == Db_qry_.Tid_select_in_tbl ? ((Db_qry__select_in_tbl)qry).Xto_sql() : Sql_cmd_wtr_.Ansi.XtoSqlQry(qry, true);
|
||||
New();
|
||||
}
|
||||
public Db_stmt New() {
|
||||
@@ -58,7 +58,7 @@ class Db_stmt_cmd implements Db_stmt {
|
||||
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);}
|
||||
try {stmt.setBigDecimal(++val_idx, v.Xto_decimal());} 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));}
|
||||
@@ -87,6 +87,9 @@ class Db_stmt_cmd implements Db_stmt {
|
||||
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 Db_rdr Exec_select_as_rdr() {
|
||||
try {return engine.New_db_rdr(stmt.executeQuery(), sql);} catch (Exception e) {throw Err_.err_(e, "select failed: 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);}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ 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;
|
||||
private Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
private Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
private 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) {
|
||||
@@ -28,27 +28,27 @@ class Db_stmt_sql implements Db_stmt {
|
||||
}
|
||||
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);}
|
||||
try {Add(++val_idx, Byte_.Xto_str(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);}
|
||||
try {Add(++val_idx, Int_.Xto_str(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);}
|
||||
try {Add(++val_idx, Long_.Xto_str(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);}
|
||||
try {Add(++val_idx, Float_.Xto_str(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);}
|
||||
try {Add(++val_idx, Double_.Xto_str(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);}
|
||||
try {Add(++val_idx, v.Xto_str());} 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));}
|
||||
@@ -84,6 +84,7 @@ class Db_stmt_sql implements Db_stmt {
|
||||
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 Db_rdr Exec_select_as_rdr() {throw Err_.not_implemented_();}
|
||||
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);}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user