mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.1.4.1
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src_100_core"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="src_110_dbQry"/>
|
||||
<classpathentry kind="src" path="src_120_sql"/>
|
||||
<classpathentry kind="src" path="src_130_misc"/>
|
||||
|
||||
48
140_dbs/src/gplx/dbs/Db_conn.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn.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 class Db_conn {
|
||||
private final Db_engine engine;
|
||||
private final ListAdp itm_list = ListAdp_.new_();
|
||||
public Db_conn(Db_engine engine) {
|
||||
this.engine = engine;
|
||||
this.txn_mgr = new Db_txn_mgr_base(engine);
|
||||
}
|
||||
public Db_url Url() {return engine.Url();}
|
||||
public Db_txn_mgr Txn_mgr() {return txn_mgr;} private final Db_txn_mgr txn_mgr;
|
||||
public Db_stmt New_stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt New_stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, cols));}
|
||||
public Db_stmt New_stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt New_stmt_select_all_where(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols));}
|
||||
public void Itms_add(Db_conn_itm itm) {itm_list.Add(itm);}
|
||||
public void Itms_del(Db_conn_itm itm) {itm_list.Del(itm);}
|
||||
public void Conn_term() {
|
||||
int len = itm_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_conn_itm itm = (Db_conn_itm)itm_list.FetchAt(i);
|
||||
itm.Conn_term();
|
||||
}
|
||||
engine.Conn_term();
|
||||
Db_conn_pool_old._.Del(this.Url()); // remove from pool, else rls'd instance will be cached and fail upon next use
|
||||
}
|
||||
public Db_stmt New_stmt(Db_qry qry) {return engine.New_stmt_prep(qry);}
|
||||
public int Exec_qry(Db_qry qry) {txn_mgr.Txn_count_(txn_mgr.Txn_count() + 1); return Int_.cast_(engine.Exec_as_obj(qry));}
|
||||
public DataRdr Exec_qry_as_rdr(Db_qry qry) {return DataRdr_.cast_(engine.Exec_as_obj(qry));}
|
||||
public int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
|
||||
public DataRdr Exec_sql_as_rdr(String sql) {return this.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));}
|
||||
}
|
||||
48
140_dbs/src/gplx/dbs/Db_conn_.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn_.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 class Db_conn_ {
|
||||
public static final Db_conn Null = new_and_open_(Db_url_.Null);
|
||||
public static Db_conn new_and_open_(Db_url url) {
|
||||
Db_engine prototype = Db_engine_regy._.Get(url.Tid());
|
||||
Db_engine engine = prototype.New_clone(url);
|
||||
engine.Conn_open(); // auto-open
|
||||
return new Db_conn(engine);
|
||||
}
|
||||
public static Db_conn Reg_itm(Db_conn_itm itm, Db_conn old_conn, Db_conn new_conn) {
|
||||
if (old_conn != null) old_conn.Itms_del(itm);
|
||||
new_conn.Itms_add(itm);
|
||||
return new_conn;
|
||||
}
|
||||
public static int Select_fld0_as_int_or(Db_conn p, String sql, int or) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = p.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));
|
||||
int rv = or;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
Object rv_obj = rdr.ReadAt(0);
|
||||
if (rv_obj != null) // Max(fil_id) will be NULL if tbl is empty
|
||||
rv = Int_.cast_or_(rv_obj, or);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
finally {
|
||||
rdr.Rls();
|
||||
}
|
||||
}
|
||||
}
|
||||
21
140_dbs/src/gplx/dbs/Db_conn_itm.java
Normal file
21
140_dbs/src/gplx/dbs/Db_conn_itm.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
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_conn_itm {
|
||||
void Conn_term();
|
||||
}
|
||||
34
140_dbs/src/gplx/dbs/Db_conn_mkr.java
Normal file
34
140_dbs/src/gplx/dbs/Db_conn_mkr.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 interface Db_conn_mkr {
|
||||
Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref);
|
||||
}
|
||||
class Db_conn_mkr_sqlite implements Db_conn_mkr {
|
||||
public Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref) {return Sqlite_engine_.Conn_load_or_make_(db_url, created_ref);}
|
||||
public static final Db_conn_mkr_sqlite _ = new Db_conn_mkr_sqlite(); Db_conn_mkr_sqlite() {}
|
||||
}
|
||||
class Db_conn_mkr_mem implements Db_conn_mkr {
|
||||
private boolean create;
|
||||
public Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref create_ref) {create_ref.Val_(create); return null;}
|
||||
public static Db_conn_mkr_mem create_(boolean create) {
|
||||
Db_conn_mkr_mem rv = new Db_conn_mkr_mem();
|
||||
rv.create = create;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
25
140_dbs/src/gplx/dbs/Db_conn_mkr_.java
Normal file
25
140_dbs/src/gplx/dbs/Db_conn_mkr_.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
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_conn_mkr_ {
|
||||
public static final Db_conn_mkr
|
||||
Sqlite = Db_conn_mkr_sqlite._
|
||||
, Mem_create_y = Db_conn_mkr_mem.create_(Bool_.Y)
|
||||
, Mem_create_n = Db_conn_mkr_mem.create_(Bool_.N)
|
||||
;
|
||||
}
|
||||
48
140_dbs/src/gplx/dbs/Db_conn_pool.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn_pool.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.*;
|
||||
import gplx.dbs.engines.mems.*;
|
||||
public class Db_conn_pool {
|
||||
private final HashAdp dbm_hash = HashAdp_.new_(); private final HashAdp engine_hash = HashAdp_.new_();
|
||||
Db_conn_pool() {this.Init();}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_url_.sqlite_(url));}
|
||||
public Db_conn Get_or_new(Db_url url) {
|
||||
Db_conn rv = (Db_conn)dbm_hash.Fetch(url.Xto_api());
|
||||
if (rv == null) {
|
||||
Db_engine prime = (Db_engine)engine_hash.Fetch(url.Tid()); if (prime == null) throw Err_.new_("db engine prototype not found; tid={0}", url.Tid());
|
||||
Db_engine clone = prime.New_clone(url);
|
||||
rv = new Db_conn(clone);
|
||||
dbm_hash.Add(url.Xto_api(), rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Set_mem(String db, Db_meta_tbl... tbls) {
|
||||
Db_url url = Db_url__mem.new_(db);
|
||||
Db_engine__mem engine = new Db_engine__mem(url, tbls);
|
||||
Db_conn dbm = new Db_conn(engine);
|
||||
dbm_hash.AddReplace(url.Xto_api(), dbm);
|
||||
}
|
||||
private void Init() {
|
||||
this.Engines__add(Db_engine_null._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
}
|
||||
public void Engines__add(Db_engine... ary) {
|
||||
for (Db_engine itm : ary)
|
||||
engine_hash.Add(itm.Tid(), itm);
|
||||
}
|
||||
public static final Db_conn_pool I = new Db_conn_pool();
|
||||
}
|
||||
40
140_dbs/src/gplx/dbs/Db_conn_pool_old.java
Normal file
40
140_dbs/src/gplx/dbs/Db_conn_pool_old.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 class Db_conn_pool_old {
|
||||
private final OrderedHash hash = OrderedHash_.new_();
|
||||
public Db_conn Get_or_new(Db_url url) {return Get_or_new(url.Xto_raw());}
|
||||
public Db_conn Get_or_new(String raw) {
|
||||
Db_conn rv = (Db_conn)hash.Fetch(raw);
|
||||
if (rv == null) {
|
||||
rv = Db_conn_.new_and_open_(Db_url_.parse_(raw));
|
||||
hash.Add(raw, rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Del(Db_url url) {hash.Del(url.Xto_raw());}
|
||||
public void Clear() {
|
||||
int len = hash.Count();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Db_conn conn = (Db_conn)hash.FetchAt(0);
|
||||
conn.Conn_term();
|
||||
}
|
||||
hash.Clear();
|
||||
}
|
||||
public static final Db_conn_pool_old _ = new Db_conn_pool_old(); Db_conn_pool_old() {}
|
||||
}
|
||||
31
140_dbs/src/gplx/dbs/Db_meta_fld.java
Normal file
31
140_dbs/src/gplx/dbs/Db_meta_fld.java
Normal 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; import gplx.*;
|
||||
public class Db_meta_fld {
|
||||
public Db_meta_fld(String name, int tid, int len, boolean nullable, boolean primary, boolean autoincrement) {
|
||||
this.name = name; this.tid = tid; this.len = len;
|
||||
this.nullable = nullable; this.primary = primary; this.autoincrement = autoincrement;
|
||||
}
|
||||
public int Tid() {return tid;} private final int tid;
|
||||
public String Name() {return name;} private final String name;
|
||||
public int Len() {return len;} private final int len;
|
||||
public boolean Nullable() {return nullable;} private final boolean nullable;
|
||||
public boolean Primary() {return primary;} private final boolean primary;
|
||||
public boolean Autoincrement() {return autoincrement;} private final boolean autoincrement;
|
||||
public static final int Tid_bool = 0, Tid_byte = 1, Tid_short = 2, Tid_int = 3, Tid_long = 4, Tid_float = 5, Tid_double = 6, Tid_str = 7, Tid_text = 8, Tid_bry = 9;
|
||||
}
|
||||
45
140_dbs/src/gplx/dbs/Db_meta_fld_list.java
Normal file
45
140_dbs/src/gplx/dbs/Db_meta_fld_list.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
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_meta_fld_list {
|
||||
private final OrderedHash flds = OrderedHash_.new_();
|
||||
private final ListAdp keys = ListAdp_.new_();
|
||||
public Db_meta_fld Get_by(String name) {return (Db_meta_fld)flds.Fetch(name);}
|
||||
public String[] Xto_str_ary() {if (str_ary == null) str_ary = (String[])keys.Xto_ary(String.class); return str_ary;} private String[] str_ary;
|
||||
public Db_meta_fld[] Xto_fld_ary() {if (fld_ary == null) fld_ary = (Db_meta_fld[])flds.Xto_ary(Db_meta_fld.class); return fld_ary;} private Db_meta_fld[] fld_ary;
|
||||
public String Add_bool(String name) {return Add(name, Db_meta_fld.Tid_bool, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_byte(String name) {return Add(name, Db_meta_fld.Tid_byte, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_short(String name) {return Add(name, Db_meta_fld.Tid_short, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_int(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_int_pkey(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.N);}
|
||||
public String Add_int_pkey_autonum(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.Y);}
|
||||
public String Add_long(String name) {return Add(name, Db_meta_fld.Tid_long, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_float(String name) {return Add(name, Db_meta_fld.Tid_float, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_double(String name) {return Add(name, Db_meta_fld.Tid_double, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_str(String name, int len) {return Add(name, Db_meta_fld.Tid_str, len, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_text(String name) {return Add(name, Db_meta_fld.Tid_text, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_bry(String name) {return Add(name, Db_meta_fld.Tid_bry, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add(String name, int tid, int len, boolean nullable, boolean primary, boolean autoincrement) {
|
||||
Db_meta_fld fld = new Db_meta_fld(name, tid, len, nullable, primary, autoincrement);
|
||||
flds.Add(name, fld);
|
||||
keys.Add(name);
|
||||
return name;
|
||||
}
|
||||
private static final int Len_null = -1;
|
||||
public static Db_meta_fld_list new_() {return new Db_meta_fld_list();}
|
||||
}
|
||||
27
140_dbs/src/gplx/dbs/Db_meta_idx.java
Normal file
27
140_dbs/src/gplx/dbs/Db_meta_idx.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
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_meta_idx {
|
||||
public Db_meta_idx(String tbl, String name, boolean unique, String[] flds) {this.tbl = tbl; this.name = name; this.unique = unique; this.flds = flds;}
|
||||
public String Name() {return name;} private final String name;
|
||||
public boolean Unique() {return unique;} private final boolean unique;
|
||||
public String[] Flds() {return flds;} private final String[] flds;
|
||||
public String Tbl() {return tbl;} private final String tbl;
|
||||
public static Db_meta_idx new_unique(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, name, Bool_.Y, flds);}
|
||||
public static Db_meta_idx new_normal(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, name, Bool_.N, flds);}
|
||||
}
|
||||
28
140_dbs/src/gplx/dbs/Db_meta_tbl.java
Normal file
28
140_dbs/src/gplx/dbs/Db_meta_tbl.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_tbl {
|
||||
public Db_meta_tbl(String name, Db_meta_fld[] flds, Db_meta_idx[] idxs) {
|
||||
this.name = name; this.flds = flds; this.idxs = idxs;
|
||||
}
|
||||
public String Name() {return name;} private final String name;
|
||||
public Db_meta_fld[] Flds() {return flds;} private final Db_meta_fld[] flds;
|
||||
public Db_meta_idx[] Idxs() {return idxs;} private final Db_meta_idx[] idxs;
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld[] flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds, idxs);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld... flds) {return new Db_meta_tbl(name, flds, null);}
|
||||
}
|
||||
42
140_dbs/src/gplx/dbs/Db_rdr.java
Normal file
42
140_dbs/src/gplx/dbs/Db_rdr.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
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_rdr {
|
||||
boolean Move_next();
|
||||
byte[] Read_bry(int i);
|
||||
byte[] Read_bry(String k);
|
||||
byte[] Read_bry_by_str(int i);
|
||||
byte[] Read_bry_by_str(String k);
|
||||
String Read_str(int i);
|
||||
String Read_str(String k);
|
||||
byte Read_byte(int i);
|
||||
byte Read_byte(String k);
|
||||
int Read_int(int i);
|
||||
int Read_int(String k);
|
||||
long Read_long(int i);
|
||||
long Read_long(String k);
|
||||
float Read_float(int i);
|
||||
float Read_float(String k);
|
||||
double Read_double(int i);
|
||||
double Read_double(String k);
|
||||
DateAdp Read_date_by_str(int i);
|
||||
DateAdp Read_date_by_str(String k);
|
||||
boolean Read_bool_by_byte(int i);
|
||||
boolean Read_bool_by_byte(String k);
|
||||
void Rls();
|
||||
}
|
||||
45
140_dbs/src/gplx/dbs/Db_rdr_.java
Normal file
45
140_dbs/src/gplx/dbs/Db_rdr_.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
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_rdr_ {
|
||||
public static final Db_rdr Null = new Db_rdr__null();
|
||||
}
|
||||
class Db_rdr__null implements Db_rdr {
|
||||
public boolean Move_next() {return false;}
|
||||
public byte[] Read_bry(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry(String k) {return Bry_.Empty;}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.Empty;}
|
||||
public byte Read_byte(int i) {return Byte_.Max_value_127;}
|
||||
public byte Read_byte(String k) {return Byte_.Max_value_127;}
|
||||
public String Read_str(int i) {return String_.Empty;}
|
||||
public String Read_str(String k) {return String_.Empty;}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.MinValue;}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.MinValue;}
|
||||
public int Read_int(int i) {return Int_.MinValue;}
|
||||
public int Read_int(String k) {return Int_.MinValue;}
|
||||
public long Read_long(int i) {return Long_.MinValue;}
|
||||
public long Read_long(String k) {return Long_.MinValue;}
|
||||
public float Read_float(int i) {return Float_.NaN;}
|
||||
public float Read_float(String k) {return Float_.NaN;}
|
||||
public double Read_double(int i) {return Double_.NaN;}
|
||||
public double Read_double(String k) {return Double_.NaN;}
|
||||
public boolean Read_bool_by_byte(int i) {return false;}
|
||||
public boolean Read_bool_by_byte(String k) {return false;}
|
||||
public void Rls() {}
|
||||
}
|
||||
49
140_dbs/src/gplx/dbs/Db_rdr__basic.java
Normal file
49
140_dbs/src/gplx/dbs/Db_rdr__basic.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
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.ResultSet;
|
||||
class Db_rdr__basic implements Db_rdr {
|
||||
protected ResultSet rdr;
|
||||
public void Ctor(ResultSet rdr, String sql) {this.rdr = rdr; this.sql = sql;}
|
||||
public String Sql() {return sql;} private String sql;
|
||||
public boolean Move_next() {
|
||||
try {return rdr.next();}
|
||||
catch (Exception e) {throw Err_.new_fmt_("move_next failed; check column casting error in SQL: err={0} sql={1}", Err_.Message_lang(e), sql);}
|
||||
}
|
||||
public byte[] Read_bry(int i) {try {return rdr.getBytes(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry(String k) {try {return (byte[])rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry_by_str(int i) {try {return Bry_.new_utf8_(rdr.getString(i + 1));} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry_by_str(String k) {try {return Bry_.new_utf8_((String)rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public String Read_str(int i) {try {return rdr.getString(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public String Read_str(String k) {try {return (String)rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561(Read_str(i));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561(Read_str(k));}
|
||||
public int Read_int(int i) {try {return rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public int Read_int(String k) {try {return Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public long Read_long(int i) {try {return rdr.getLong(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public long Read_long(String k) {try {return Long_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public float Read_float(int i) {try {return rdr.getFloat(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public float Read_float(String k) {try {return Float_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public double Read_double(int i) {try {return rdr.getDouble(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public double Read_double(String k) {try {return Double_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(int i) {try {return rdr.getByte(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(String k) {try {return Byte_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public boolean Read_bool_by_byte(int i) {try {return rdr.getByte(i + 1) == 1;} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public boolean Read_bool_by_byte(String k) {try {return Byte_.cast_(rdr.getObject(k)) == 1;} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public void Rls() {try {rdr.close();} catch (Exception e) {throw Err_.new_("close failed: err={0}", Err_.Message_lang(e));}}
|
||||
}
|
||||
73
140_dbs/src/gplx/dbs/Db_sqlbldr__sqlite.java
Normal file
73
140_dbs/src/gplx/dbs/Db_sqlbldr__sqlite.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
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.*;
|
||||
interface Db_sqlbldr {}
|
||||
class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.reset_(1024);
|
||||
public String Bld_create_idx(Db_meta_idx idx) {
|
||||
tmp_bfr.Add_str_ascii("CREATE ");
|
||||
if (idx.Unique())
|
||||
tmp_bfr.Add_str_ascii("UNIQUE ");
|
||||
tmp_bfr.Add_str_ascii("INDEX ");
|
||||
tmp_bfr.Add_str_ascii("IF NOT EXISTS ");
|
||||
tmp_bfr.Add_str_ascii(idx.Tbl()).Add_str_ascii("__").Add_str_ascii(idx.Name());
|
||||
tmp_bfr.Add_str_ascii(" (");
|
||||
String[] flds = idx.Flds();
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
String fld = flds[i];
|
||||
if (i != 0) tmp_bfr.Add_str_ascii(", ");
|
||||
tmp_bfr.Add_str_ascii(fld);
|
||||
}
|
||||
tmp_bfr.Add_str_ascii(");");
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
public String Bld_create_tbl(Db_meta_tbl tbl) {
|
||||
tmp_bfr.Add_str_ascii("CREATE TABLE ").Add_str_ascii(tbl.Name()).Add_byte_nl();
|
||||
Db_meta_fld[] flds = tbl.Flds();
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
Db_meta_fld fld = flds[i];
|
||||
tmp_bfr.Add_byte(i == 0 ? Byte_ascii.Paren_bgn : Byte_ascii.Comma).Add_byte_space();
|
||||
tmp_bfr.Add_str_ascii(fld.Name()).Add_byte_space();
|
||||
Tid_to_sql(tmp_bfr, fld.Tid(), fld.Len()); tmp_bfr.Add_byte_space();
|
||||
tmp_bfr.Add_str_ascii(fld.Nullable() ? "NULL " : "NOT NULL ");
|
||||
if (fld.Primary()) tmp_bfr.Add_str_ascii("PRIMARY KEY ");
|
||||
if (fld.Autoincrement()) tmp_bfr.Add_str_ascii("AUTOINCREMENT ");
|
||||
tmp_bfr.Del_by_1(); // remove trailing space
|
||||
tmp_bfr.Add_byte_nl();
|
||||
}
|
||||
tmp_bfr.Add_str_ascii(");");
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
public void Tid_to_sql(Bry_bfr tmp_bfr, int tid, int len) {// REF: https://www.sqlite.org/datatype3.html
|
||||
switch (tid) {
|
||||
case Db_meta_fld.Tid_bool: tmp_bfr.Add_str_ascii("boolean"); break;
|
||||
case Db_meta_fld.Tid_byte: tmp_bfr.Add_str_ascii("tinyint"); break;
|
||||
case Db_meta_fld.Tid_short: tmp_bfr.Add_str_ascii("smallint"); break;
|
||||
case Db_meta_fld.Tid_int: tmp_bfr.Add_str_ascii("int"); break;
|
||||
case Db_meta_fld.Tid_long: tmp_bfr.Add_str_ascii("bigint"); break;
|
||||
case Db_meta_fld.Tid_float: tmp_bfr.Add_str_ascii("float"); break;
|
||||
case Db_meta_fld.Tid_double: tmp_bfr.Add_str_ascii("double"); break;
|
||||
case Db_meta_fld.Tid_str: tmp_bfr.Add_str_ascii("varchar(").Add_int_variable(len).Add_byte(Byte_ascii.Paren_end); break;
|
||||
case Db_meta_fld.Tid_text: tmp_bfr.Add_str_ascii("text"); break;
|
||||
case Db_meta_fld.Tid_bry: tmp_bfr.Add_str_ascii("blob"); break;
|
||||
default: throw Err_.unhandled(tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
64
140_dbs/src/gplx/dbs/Db_sqlbldr_tst.java
Normal file
64
140_dbs/src/gplx/dbs/Db_sqlbldr_tst.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
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_sqlbldr_tst {
|
||||
@Before public void setup() {} private final Db_sqlbldr_fxt fxt = new Db_sqlbldr_fxt();
|
||||
@Test public void Idx_unique() {
|
||||
fxt.Test_create_idx(Db_meta_idx.new_unique("tbl_name", "idx_name", "fld_1", "fld_2")
|
||||
, "CREATE UNIQUE INDEX IF NOT EXISTS tbl_name__idx_name (fld_1, fld_2);"
|
||||
);
|
||||
}
|
||||
@Test public void Tbl_basic() {
|
||||
Db_meta_fld_list flds = Db_meta_fld_list.new_();
|
||||
flds.Add_int_pkey("fld_int_pkey");
|
||||
flds.Add_bool("fld_bool");
|
||||
flds.Add_short("fld_short");
|
||||
flds.Add_int("fld_int");
|
||||
flds.Add_long("fld_long");
|
||||
flds.Add_float("fld_float");
|
||||
flds.Add_double("fld_double");
|
||||
flds.Add_str("fld_str", 123);
|
||||
flds.Add_text("fld_text");
|
||||
flds.Add_bry("fld_bry");
|
||||
fxt.Test_create_tbl(Db_meta_tbl.new_("tbl_name", flds.Xto_fld_ary())
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "CREATE TABLE tbl_name"
|
||||
, "( fld_int_pkey int NOT NULL PRIMARY KEY"
|
||||
, ", fld_bool boolean NOT NULL"
|
||||
, ", fld_short smallint NOT NULL"
|
||||
, ", fld_int int NOT NULL"
|
||||
, ", fld_long bigint NOT NULL"
|
||||
, ", fld_float float NOT NULL"
|
||||
, ", fld_double double NOT NULL"
|
||||
, ", fld_str varchar(123) NOT NULL"
|
||||
, ", fld_text text NOT NULL"
|
||||
, ", fld_bry blob NOT NULL"
|
||||
, ");"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Db_sqlbldr_fxt {
|
||||
private Db_sqlbldr__sqlite sqlbldr = new Db_sqlbldr__sqlite();
|
||||
public void Test_create_idx(Db_meta_idx idx, String expd) {
|
||||
Tfds.Eq(expd, sqlbldr.Bld_create_idx(idx));
|
||||
}
|
||||
public void Test_create_tbl(Db_meta_tbl tbl, String expd) {
|
||||
Tfds.Eq_str_lines(expd, sqlbldr.Bld_create_tbl(tbl));
|
||||
}
|
||||
}
|
||||
40
140_dbs/src/gplx/dbs/Db_sys_regy_mgr.java
Normal file
40
140_dbs/src/gplx/dbs/Db_sys_regy_mgr.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.*;
|
||||
class Db_sys_regy_mgr {
|
||||
private Db_sys_regy_tbl tbl;
|
||||
public Db_sys_regy_mgr(Db_url url, String name) {tbl = new Db_sys_regy_tbl(url, name);}
|
||||
public void Set(String grp, String key, String val) {
|
||||
if (tbl.Select_val_or(grp, key, null) == null)
|
||||
tbl.Insert(grp, key, val);
|
||||
else
|
||||
tbl.Update(grp, key, val);
|
||||
}
|
||||
public void Del(String grp, String key) {
|
||||
tbl.Delete(grp, key);
|
||||
}
|
||||
public String Get_val_as_str_or(String grp, String key, String or) {
|
||||
return tbl.Select_val_or(grp, key, or);
|
||||
}
|
||||
}
|
||||
class Db_sys_regy_itm {
|
||||
public Db_sys_regy_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private final String grp;
|
||||
public String Key() {return key;} private final String key;
|
||||
public String Val() {return val;} private final String val;
|
||||
}
|
||||
54
140_dbs/src/gplx/dbs/Db_sys_regy_mgr_tst.java
Normal file
54
140_dbs/src/gplx/dbs/Db_sys_regy_mgr_tst.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.*;
|
||||
import org.junit.*;
|
||||
public class Db_sys_regy_mgr_tst {
|
||||
@Before public void init() {fxt.Init();} private Db_sys_regy_mgr_fxt fxt = new Db_sys_regy_mgr_fxt();
|
||||
@Test public void Insert() {
|
||||
fxt .Exec_set("grp", "key", "val_0")
|
||||
.Test_get("grp", "key", "val_0");
|
||||
}
|
||||
@Test public void Update() {
|
||||
fxt .Exec_set("grp", "key", "val_0")
|
||||
.Exec_set("grp", "key", "val_1")
|
||||
.Test_get("grp", "key", "val_1");
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt .Exec_set("grp", "key_0", "val_0")
|
||||
.Exec_set("grp", "key_1", "val_0")
|
||||
.Exec_del("grp", "key_1")
|
||||
.Test_get("grp", "key_0", null)
|
||||
.Test_get("grp", "key_1", null)
|
||||
;
|
||||
}
|
||||
}
|
||||
class Db_sys_regy_mgr_fxt {
|
||||
private Db_sys_regy_mgr sys_regy_mgr;
|
||||
public void Init() {
|
||||
if (sys_regy_mgr == null) {
|
||||
Db_conn_pool.I.Set_mem("test_db", Db_sys_regy_tbl.new_meta("test_regy"));
|
||||
sys_regy_mgr = new Db_sys_regy_mgr(Db_url_.mem_("test_db"), "test_regy");
|
||||
}
|
||||
}
|
||||
public Db_sys_regy_mgr_fxt Exec_set(String grp, String key, String val) {sys_regy_mgr.Set(grp, key, val); return this;}
|
||||
public Db_sys_regy_mgr_fxt Exec_del(String grp, String key) {sys_regy_mgr.Del(grp, key); return this;}
|
||||
public Db_sys_regy_mgr_fxt Test_get(String grp, String key, String expd_val) {
|
||||
Tfds.Eq(expd_val, sys_regy_mgr.Get_val_as_str_or(grp, key, null));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
61
140_dbs/src/gplx/dbs/Db_sys_regy_tbl.java
Normal file
61
140_dbs/src/gplx/dbs/Db_sys_regy_tbl.java
Normal 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; import gplx.*;
|
||||
class Db_sys_regy_tbl implements Db_conn_itm {
|
||||
private final String tbl_name;
|
||||
private Db_conn conn;
|
||||
public void Conn_term() {}
|
||||
public Db_sys_regy_tbl(Db_url url, String tbl_name) {
|
||||
this.tbl_name = tbl_name;
|
||||
// this.meta = Db_sys_regy_tbl.new_meta(tbl_name);
|
||||
this.conn = Db_conn_pool.I.Get_or_new(url);
|
||||
conn.Itms_add(this);
|
||||
}
|
||||
// private Db_meta_tbl meta;
|
||||
public void Insert(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.New_stmt_insert(tbl_name, Flds.Xto_str_ary());
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Val_str(val).Exec_insert();
|
||||
}
|
||||
public void Update(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.New_stmt_update(tbl_name, String_.Ary(Fld_regy_grp, Fld_regy_key), Fld_regy_val);
|
||||
stmt.Clear().Val_str(val).Val_str(grp).Val_str(key).Exec_update();
|
||||
}
|
||||
public void Delete(String grp, String key) {
|
||||
Db_stmt stmt = conn.New_stmt_delete(tbl_name, Fld_regy_grp, Fld_regy_key);
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Exec_delete();
|
||||
}
|
||||
public String Select_val_or(String grp, String key, String or) {
|
||||
Db_stmt stmt = conn.New_stmt_select_all_where(tbl_name, Flds.Xto_str_ary(), Fld_regy_grp, Fld_regy_key);
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
try {
|
||||
rdr = stmt.Clear().Val_str(grp).Val_str(key).Exec_select_as_rdr();
|
||||
return rdr.Move_next() ? rdr.Read_str(Fld_regy_val) : or;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
private static final Db_meta_fld_list Flds = Db_meta_fld_list.new_();
|
||||
private static final String
|
||||
Fld_regy_grp = Flds.Add_str("regy_grp", 1024)
|
||||
, Fld_regy_key = Flds.Add_str("regy_key", 1024)
|
||||
, Fld_regy_val = Flds.Add_str("regy_val", 4096)
|
||||
;
|
||||
public static Db_meta_tbl new_meta(String tbl) {
|
||||
return Db_meta_tbl.new_(tbl, Flds.Xto_fld_ary()
|
||||
, Db_meta_idx.new_unique(tbl, "key", Flds.Xto_str_ary())
|
||||
);
|
||||
}
|
||||
}
|
||||
95
140_dbs/src/gplx/dbs/Db_url.java
Normal file
95
140_dbs/src/gplx/dbs/Db_url.java
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
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_url {
|
||||
String Tid();
|
||||
String Xto_raw();
|
||||
String Xto_api();
|
||||
Db_url New_self(String raw, GfoMsg m);
|
||||
}
|
||||
class Db_url__null extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "null_db";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {return this;}
|
||||
public static final Db_url__null _ = new Db_url__null(); Db_url__null() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
}
|
||||
class Db_url__mysql extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mysql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "uid", uid
|
||||
, "pwd", pwd
|
||||
, "charset", "utf8"
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__mysql rv = new Db_url__mysql();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("charset", "utf8")));
|
||||
rv.uid = m.ReadStr("uid");
|
||||
rv.pwd = m.ReadStr("pwd");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__mysql _ = new Db_url__mysql(); Db_url__mysql() {}
|
||||
}
|
||||
class Db_url__postgres extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "postgresql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "port", "5432"
|
||||
, "user id", uid
|
||||
, "password", pwd
|
||||
, "encoding", "unicode" // needed for 1.1 conn; otherwise, ascii
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__postgres rv = new Db_url__postgres();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("encoding", "unicode")));
|
||||
rv.uid = m.ReadStr("user id");
|
||||
rv.pwd = m.ReadStr("password");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__postgres _ = new Db_url__postgres(); Db_url__postgres() {}
|
||||
}
|
||||
class Db_url__tdb extends Db_url__base {
|
||||
public Io_url Url() {return url;} Io_url url;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "tdb";
|
||||
public static Db_url new_(Io_url url) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "url", url.Raw()
|
||||
));
|
||||
} Db_url__tdb() {}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__tdb rv = new Db_url__tdb();
|
||||
String urlStr = m.ReadStr("url");
|
||||
Io_url url = Io_url_.new_any_(urlStr);
|
||||
rv.Ctor(urlStr, url.NameOnly(), raw, BldApi(m));
|
||||
rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__tdb _ = new Db_url__tdb();
|
||||
}
|
||||
55
140_dbs/src/gplx/dbs/Db_url_.java
Normal file
55
140_dbs/src/gplx/dbs/Db_url_.java
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
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.dbs.engines.mems.*;
|
||||
public class Db_url_ {
|
||||
public static final Db_url Null = Db_url__null._;
|
||||
public static final Db_url Test = Db_url__mysql.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static Db_url parse_(String raw) {return Db_url_pool._.Parse(raw);}
|
||||
public static Db_url sqlite_(Io_url url) {return Db_url__sqlite.load_(url);}
|
||||
public static Db_url tdb_(Io_url url) {return Db_url__tdb.new_(url);}
|
||||
public static Db_url mem_(String db) {return Db_url__mem.new_(db);}
|
||||
public static final String Key_tdb = Db_url__tdb.Tid_const;
|
||||
}
|
||||
class Db_url_pool {
|
||||
private OrderedHash regy = OrderedHash_.new_();
|
||||
public Db_url_pool() {
|
||||
this.Add(Db_url__null._).Add(Db_url__tdb._).Add(Db_url__mysql._).Add(Db_url__postgres._).Add(Db_url__sqlite._);
|
||||
this.Add(Db_url__mem.I);
|
||||
}
|
||||
public Db_url_pool Add(Db_url itm) {regy.AddReplace(itm.Tid(), itm); return this;}
|
||||
public Db_url Parse(String raw) {// assume each pair has format of: name=val;
|
||||
try {
|
||||
GfoMsg m = GfoMsg_.new_parse_("db_url");
|
||||
String[] terms = String_.Split(raw, ";");
|
||||
String url_tid = "";
|
||||
for (String term : terms) {
|
||||
if (String_.Len(term) == 0) continue;
|
||||
String[] kv = String_.Split(term, "=");
|
||||
if (String_.Eq(kv[0], "gplx_key"))
|
||||
url_tid = kv[1]; // NOTE: do not add to GfoMsg; will not be part of ApiStr
|
||||
else
|
||||
m.Add(kv[0], kv[1]);
|
||||
}
|
||||
Db_url prototype = (Db_url)regy.Fetch(url_tid);
|
||||
return prototype.New_self(raw, m);
|
||||
}
|
||||
catch(Exception exc) {throw Err_.parse_type_exc_(exc, Db_url.class, raw);}
|
||||
}
|
||||
public static final Db_url_pool _ = new Db_url_pool();
|
||||
}
|
||||
51
140_dbs/src/gplx/dbs/Db_url__base.java
Normal file
51
140_dbs/src/gplx/dbs/Db_url__base.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
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 abstract class Db_url__base implements Db_url {
|
||||
public abstract String Tid();
|
||||
public String Xto_raw() {return raw;} private String raw = "";
|
||||
public String Xto_api() {return api;} private String api = "";
|
||||
public String Database() {return database;} private String database = "";
|
||||
public String Server() {return server;} private String server = "";
|
||||
public abstract Db_url New_self(String raw, GfoMsg m);
|
||||
protected void Ctor(String server, String database, String raw, String api) {this.server = server; this.database = database; this.raw = raw; this.api = api;}
|
||||
protected static String BldApi(GfoMsg m, KeyVal... xtnAry) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
HashAdp hash = HashAdp_.new_();
|
||||
for (int i = 0; i < m.Args_count(); i++) {
|
||||
KeyVal kv = m.Args_getAt(i);
|
||||
sb.Add_fmt("{0}={1};", kv.Key(), kv.Val_to_str_or_empty());
|
||||
hash.AddKeyVal(kv.Key());
|
||||
}
|
||||
for (KeyVal xtn : xtnAry) {
|
||||
if (hash.Has(xtn.Key())) continue;
|
||||
sb.Add_fmt("{0}={1};", xtn.Key(), xtn.Val_to_str_or_empty());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
protected static String Bld_raw(String... ary) {
|
||||
Bry_bfr bfr = Bry_bfr.reset_(255);
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
String itm = ary[i];
|
||||
bfr.Add_str_utf8(itm);
|
||||
bfr.Add_byte(i % 2 == 0 ? Byte_ascii.Eq : Byte_ascii.Semic);
|
||||
}
|
||||
return bfr.Xto_str();
|
||||
}
|
||||
}
|
||||
46
140_dbs/src/gplx/dbs/Db_url__sqlite.java
Normal file
46
140_dbs/src/gplx/dbs/Db_url__sqlite.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
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_url__sqlite extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "sqlite";
|
||||
public Io_url Url() {return url;} private Io_url url;
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__sqlite rv = new Db_url__sqlite();
|
||||
String url = m.ReadStr("data source");
|
||||
rv.url = Io_url_.new_any_(url);
|
||||
rv.Ctor("", url, raw, BldApi(m, KeyVal_.new_("version", "3")));
|
||||
return rv;
|
||||
}
|
||||
public static Db_url load_(Io_url url) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
, "data source" , url.Xto_api()
|
||||
, "version" , "3"
|
||||
));
|
||||
}
|
||||
public static Db_url make_(Io_url url) {
|
||||
Io_mgr._.CreateDirIfAbsent(url.OwnerDir());
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
, "data source" , url.Xto_api()
|
||||
, "version" , "3"
|
||||
|
||||
));
|
||||
}
|
||||
public static final Db_url__sqlite _ = new Db_url__sqlite(); Db_url__sqlite() {}
|
||||
}
|
||||
46
140_dbs/src/gplx/dbs/Db_url_tst.java
Normal file
46
140_dbs/src/gplx/dbs/Db_url_tst.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
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_url_tst {
|
||||
@Before public void setup() {
|
||||
regy.Add(Db_url_mock._);
|
||||
} private final Db_url_pool regy = new Db_url_pool();
|
||||
@Test public void Parse() {
|
||||
tst_Parse("gplx_key=mock;id=1;", kv_("id", "1")); // one; gplx_key removed
|
||||
tst_Parse("gplx_key=mock;id=1;name=me;", kv_("id", "1"), kv_("name", "me")); // many
|
||||
tst_Parse("gplx_key=mock;id=1;name=me" , kv_("id", "1"), kv_("name", "me")); // no semi-colon at end
|
||||
}
|
||||
private KeyVal kv_(String key, Object val) {return KeyVal_.new_(key, val);}
|
||||
private void tst_Parse(String raw, KeyVal... expd) {
|
||||
Db_url_mock mock = (Db_url_mock)regy.Parse(raw);
|
||||
Tfds.Eq_ary_str(expd, mock.Kvs());
|
||||
}
|
||||
}
|
||||
class Db_url_mock extends Db_url__base {
|
||||
public KeyVal[] Kvs() {return kvs;} KeyVal[] kvs;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mock";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url_mock rv = new Db_url_mock();
|
||||
rv.kvs = new KeyVal[m.Args_count()];
|
||||
for (int i = 0; i < m.Args_count(); i++)
|
||||
rv.kvs[i] = m.Args_getAt(i);
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url_mock _ = new Db_url_mock(); Db_url_mock() {}
|
||||
}
|
||||
67
140_dbs/src/gplx/dbs/Sql_join_itm.java
Normal file
67
140_dbs/src/gplx/dbs/Sql_join_itm.java
Normal 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; import gplx.*;
|
||||
public class Sql_join_itm {
|
||||
public String SrcTbl() {return srcTbl;} public Sql_join_itm SrcTbl_(String v) {srcTbl = v; return this;} private String srcTbl;
|
||||
public String SrcFld() {return srcFld;} public Sql_join_itm SrcFld_(String v) {srcFld = v; return this;} private String srcFld;
|
||||
public String TrgFld() {return trgFld;} public Sql_join_itm TrgFld_(String v) {trgFld = v; return this;} private String trgFld;
|
||||
public String TrgFldOrSrcFld() {return trgFld == null ? srcFld : trgFld;}
|
||||
public static Sql_join_itm new_(String trgFld, String srcTbl, String srcFld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = trgFld; rv.srcTbl = srcTbl; rv.srcFld = srcFld;
|
||||
return rv;
|
||||
} Sql_join_itm() {}
|
||||
public static Sql_join_itm same_(String tbl, String fld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = fld; rv.srcTbl = tbl; rv.srcFld = fld;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
class Sql_from {
|
||||
public ListAdp Tbls() {return tbls;} ListAdp tbls = ListAdp_.new_();
|
||||
public Sql_tbl_src BaseTable() {return (Sql_tbl_src)tbls.FetchAt(0);}
|
||||
|
||||
public static Sql_from new_(Sql_tbl_src baseTable) {
|
||||
Sql_from rv = new Sql_from();
|
||||
rv.tbls.Add(baseTable);
|
||||
return rv;
|
||||
} Sql_from() {}
|
||||
}
|
||||
class Sql_tbl_src {
|
||||
public Sql_join_itmType JoinType() {return type;} public Sql_tbl_src JoinType_(Sql_join_itmType v) {this.type = v; return this;} Sql_join_itmType type = Sql_join_itmType.Inner;
|
||||
public ListAdp JoinLinks() {return joinLinks;} ListAdp joinLinks = ListAdp_.new_();
|
||||
public String TblName() {return tblName;} public Sql_tbl_src TblName_(String s) {tblName = s; return this;} private String tblName;
|
||||
public String Alias() {return alias;} public Sql_tbl_src Alias_(String s) {alias = s; return this;} private String alias;
|
||||
public void XtoSql(String_bldr sb) {
|
||||
sb.Add_many(tblName, alias == null ? "" : " " + alias);
|
||||
}
|
||||
public static Sql_tbl_src new_() {return new Sql_tbl_src();} Sql_tbl_src() {}
|
||||
}
|
||||
class Sql_join_itmType {
|
||||
public int Val() {return val;} int val;
|
||||
public String Name() {return name;} private String name;
|
||||
Sql_join_itmType(int v, String name) {this.val = v; this.name = name;}
|
||||
public static final Sql_join_itmType
|
||||
From = new Sql_join_itmType(0, "FROM")
|
||||
, Inner = new Sql_join_itmType(1, "INNER JOIN")
|
||||
, Left = new Sql_join_itmType(2, "LEFT JOIN")
|
||||
, Right = new Sql_join_itmType(3, "RIGHT JOIN")
|
||||
, Outer = new Sql_join_itmType(4, "OUTER JOIN")
|
||||
, Cross = new Sql_join_itmType(5, "CROSS JOIN")
|
||||
;
|
||||
}
|
||||
73
140_dbs/src/gplx/dbs/Sql_order_by.java
Normal file
73
140_dbs/src/gplx/dbs/Sql_order_by.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
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.lists.*; /*ComparerAble*/
|
||||
class Sql_order_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_order_by new_(Sql_order_by_itm... ary) {
|
||||
Sql_order_by rv = new Sql_order_by();
|
||||
for (Sql_order_by_itm itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_order_by() {}
|
||||
}
|
||||
class Sql_group_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_group_by new_(String... ary) {
|
||||
Sql_group_by rv = new Sql_group_by();
|
||||
for (String itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_group_by() {}
|
||||
}
|
||||
class Sql_order_by_itm {
|
||||
public String Name() {return name;} private String name;
|
||||
public boolean Ascending() {return ascending;} private boolean ascending;
|
||||
public String XtoSql() {
|
||||
String ascString = ascending ? "" : " DESC";
|
||||
return name + ascString;
|
||||
}
|
||||
public static Sql_order_by_itm new_(String name, boolean ascending) {
|
||||
Sql_order_by_itm rv = new Sql_order_by_itm();
|
||||
rv.name = name; rv.ascending = ascending;
|
||||
return rv;
|
||||
} Sql_order_by_itm() {}
|
||||
}
|
||||
class Sql_order_by_sorter implements ComparerAble {
|
||||
public int compare(Object lhsObj, Object rhsObj) {
|
||||
GfoNde lhs = (GfoNde)lhsObj; GfoNde rhs = (GfoNde)rhsObj;
|
||||
Sql_order_by_itm item = null; Object lhsData = null, rhsData = null;
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
item = items[i];
|
||||
lhsData = lhs.Read(item.Name()); rhsData = rhs.Read(item.Name());
|
||||
int compare = CompareAble_.Compare_obj(lhsData, rhsData);
|
||||
if (compare == CompareAble_.Same) continue;
|
||||
int ascendingVal = item.Ascending() ? 1 : -1;
|
||||
return compare * ascendingVal;
|
||||
}
|
||||
return CompareAble_.Same;
|
||||
}
|
||||
Sql_order_by_itm[] items;
|
||||
public static ComparerAble new_(Sql_order_by_itm[] items) {
|
||||
Sql_order_by_sorter rv = new Sql_order_by_sorter();
|
||||
rv.items = items;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
22
140_dbs/src/gplx/dbs/Sql_qry_wtr.java
Normal file
22
140_dbs/src/gplx/dbs/Sql_qry_wtr.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
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 interface Sql_qry_wtr {
|
||||
String Xto_str(Db_qry qry, boolean prepare);
|
||||
}
|
||||
24
140_dbs/src/gplx/dbs/Sql_qry_wtr_.java
Normal file
24
140_dbs/src/gplx/dbs/Sql_qry_wtr_.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Sql_qry_wtr_ {
|
||||
public static Sql_qry_wtr new_ansi() {return new Sql_qry_wtr_ansi();}
|
||||
public static Sql_qry_wtr new_escape_backslash() {return new Sql_qry_wtr_ansi_escape_backslash();}
|
||||
public static final Sql_qry_wtr I = new Sql_qry_wtr_ansi();
|
||||
public static String Gen_placeholder_parameters(Db_qry qry) {return Sql_qry_wtr_.I.Xto_str(qry, true);} // replace arguments with ?; EX: UPDATE a SET b = ? WHERE c = ?;
|
||||
}
|
||||
286
140_dbs/src/gplx/dbs/Sql_qry_wtr_ansi.java
Normal file
286
140_dbs/src/gplx/dbs/Sql_qry_wtr_ansi.java
Normal file
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
private final String_bldr sb = String_bldr_.new_();
|
||||
private boolean prepare = false;
|
||||
public String Xto_str(Db_qry cmd, boolean prepare) {
|
||||
this.prepare = prepare;
|
||||
switch (cmd.Tid()) {
|
||||
case Db_qry_.Tid_insert: return Bld_qry_insert((Db_qry_insert)cmd);
|
||||
case Db_qry_.Tid_delete: return Bld_qry_delete((Db_qry_delete)cmd);
|
||||
case Db_qry_.Tid_update: return Bld_qry_update((Db_qry_update)cmd);
|
||||
case Db_qry_.Tid_select_in_tbl:
|
||||
case Db_qry_.Tid_select: return Bld_qry_select((Db_qry_select)cmd);
|
||||
case Db_qry_.Tid_sql: return ((Db_qry_sql)cmd).Xto_sql();
|
||||
default: throw Err_.unhandled(cmd.Tid());
|
||||
}
|
||||
}
|
||||
private String Bld_qry_delete(Db_qry_delete cmd) {
|
||||
sb.Add_many("DELETE FROM ", cmd.Base_table());
|
||||
Bld_where2(sb, cmd.Where());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_insert(Db_qry_insert cmd) {
|
||||
if (cmd.Select() != null) {
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < cmd.Cols().Count(); i++) {
|
||||
Sql_select_fld_base fld = cmd.Cols().FetchAt(i);
|
||||
sb.Add(fld.Alias());
|
||||
sb.Add(i == cmd.Cols().Count() - 1 ? ") " : ", ");
|
||||
}
|
||||
sb.Add(Bld_qry_select(cmd.Select()));
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
int arg_count = cmd.Args().Count(); if (arg_count == 0) throw Err_.new_("Db_qry_insert has no columns").Add("base_table", cmd.Base_table());
|
||||
int last = arg_count - 1;
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
sb.Add(" VALUES (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
Db_arg prm = (Db_arg)pair.Val();
|
||||
this.Bld_val(sb, prm);
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_update(Db_qry_update cmd) {
|
||||
int arg_count = cmd.Args().Count(); if (arg_count == 0) throw Err_.new_("Db_qry_update has no columns").Add("base_table", cmd.Base_table());
|
||||
sb.Add_many("UPDATE ", cmd.Base_table(), " SET ");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add("=");
|
||||
this.Bld_val(sb, (Db_arg)pair.Val());
|
||||
}
|
||||
Bld_where(sb, cmd.Where());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_select(Db_qry_select cmd) {
|
||||
sb.Add("SELECT ");
|
||||
if (cmd.Cols().Distinct()) sb.Add("DISTINCT ");
|
||||
Sql_select_fld_list flds = cmd.Cols().Flds();
|
||||
if (flds.Count() == 0) sb.Add("*");
|
||||
for (int i = 0; i < flds.Count(); i++) {
|
||||
Sql_select_fld_base fld = (Sql_select_fld_base)flds.FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, fld.XtoSql());
|
||||
}
|
||||
Bld_clause_from(sb, cmd.From());
|
||||
Bld_where(sb, cmd.Where());
|
||||
Bld_select_group_by(sb, cmd.GroupBy());
|
||||
Bld_select_order_by(sb, cmd.OrderBy());
|
||||
Bld_select_limit(sb, cmd.Limit());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private void Bld_select_group_by(String_bldr sb, Sql_group_by groupBy) {
|
||||
if (groupBy == null) return;
|
||||
sb.Add(" GROUP BY ");
|
||||
for (int i = 0; i < groupBy.Flds().Count(); i++) {
|
||||
String item = (String)groupBy.Flds().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item);
|
||||
}
|
||||
}
|
||||
private void Bld_select_order_by(String_bldr sb, Sql_order_by orderBy) {
|
||||
if (orderBy == null) return;
|
||||
sb.Add(" ORDER BY ");
|
||||
for (int i = 0; i < orderBy.Flds().Count(); i++) {
|
||||
Sql_order_by_itm item = (Sql_order_by_itm)orderBy.Flds().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item.XtoSql());
|
||||
}
|
||||
}
|
||||
private void Bld_select_limit(String_bldr sb, int limit) {
|
||||
if (limit == Db_qry_select.Limit_disabled) return;
|
||||
sb.Add(" LIMIT ").Add(limit);
|
||||
}
|
||||
private void Bld_clause_from(String_bldr sb, Sql_from from) {
|
||||
for (Object tblObj : from.Tbls()) {
|
||||
Sql_tbl_src tbl = (Sql_tbl_src)tblObj;
|
||||
sb.Add_many
|
||||
( " ", String_.Upper(tbl.JoinType().Name()), " ", tbl.TblName(), String_.FormatOrEmptyStrIfNull(" {0}", tbl.Alias())
|
||||
);
|
||||
String tblAliasForJoin = tbl.Alias() == null ? tbl.TblName() : tbl.Alias();
|
||||
for (int i = 0; i < tbl.JoinLinks().Count(); i++) {
|
||||
Sql_join_itm joinLink = (Sql_join_itm)tbl.JoinLinks().FetchAt(i);
|
||||
String conjunction = i == 0 ? " ON " : " AND ";
|
||||
sb.Add_many
|
||||
( conjunction, joinLink.SrcTbl(), ".", joinLink.SrcFld(), "=", tblAliasForJoin, ".", joinLink.TrgFldOrSrcFld()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void XtoSqlCol(String_bldr sb, Object obj) {
|
||||
if (obj == null) throw Err_.null_("ColName");
|
||||
sb.Add_obj(obj); // FIXME: options for bracketing; ex: [name]
|
||||
}
|
||||
public void Bld_val(String_bldr sb, Db_arg arg) {
|
||||
if (prepare) {
|
||||
sb.Add("?");
|
||||
return;
|
||||
}
|
||||
Object val = arg.Val();
|
||||
if (val == null) {
|
||||
sb.Add("NULL");
|
||||
return;
|
||||
}
|
||||
Class<?> val_type = val.getClass();
|
||||
if (val_type == Bool_.Cls_ref_type)
|
||||
sb.Add_obj(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
|
||||
( val_type == Byte_.Cls_ref_type || val_type == Short_.Cls_ref_type
|
||||
|| val_type == Int_.Cls_ref_type || val_type == Long_.Cls_ref_type
|
||||
|| val_type == Float_.Cls_ref_type || val_type == Double_.Cls_ref_type
|
||||
)
|
||||
sb.Add(Object_.Xto_str_strict_or_null(val));
|
||||
else if (val_type == DateAdp.class)
|
||||
Bld_val_date(sb, arg, (DateAdp)val);
|
||||
else if (val_type == DecimalAdp.class) {
|
||||
DecimalAdp valDecimal = (DecimalAdp)val;
|
||||
sb.Add(valDecimal.Xto_str());
|
||||
}
|
||||
else {
|
||||
String valString = Object_.Xto_str_strict_or_null(val);
|
||||
Bld_val_str(sb, arg, valString);
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_str(String_bldr sb, Db_arg prm, String s) {
|
||||
sb.Add_many("'", String_.Replace(s, "'", "''"), "'"); // stupid escaping of '
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_date(String_bldr sb, Db_arg prm, DateAdp s) {
|
||||
sb.Add_many("'", s.XtoStr_gplx_long(), "'");
|
||||
}
|
||||
private void Bld_where(String_bldr sb, Sql_where where) {
|
||||
if (where == null || where.Crt() == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where(sb, where.Crt());
|
||||
}
|
||||
private void Bld_where2(String_bldr sb, Criteria crt) {
|
||||
if (crt == null || crt == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where(sb, crt);
|
||||
}
|
||||
public void Bld_where(String_bldr sb, Criteria crt) {
|
||||
Criteria_bool_base crt_bool = Criteria_bool_base.as_(crt);
|
||||
if (crt_bool != null) {
|
||||
sb.Add("(");
|
||||
Bld_where(sb, crt_bool.Lhs());
|
||||
sb.Add_many(" ", crt_bool.OpLiteral(), " ");
|
||||
Bld_where(sb, crt_bool.Rhs());
|
||||
sb.Add(")");
|
||||
return;
|
||||
}
|
||||
if (crt.Crt_tid() == Criteria_.Tid_db_obj_ary) {
|
||||
Append_db_obj_ary(sb, (Db_obj_ary_crt)crt);
|
||||
}
|
||||
else {
|
||||
Criteria_wrapper leaf = Criteria_wrapper.as_(crt); if (leaf == null) throw Err_.invalid_op_(crt.XtoStr());
|
||||
sb.Add(leaf.Name_of_GfoFldCrt());
|
||||
Bld_where_crt(sb, leaf.Crt_of_GfoFldCrt());
|
||||
}
|
||||
}
|
||||
private void Bld_where_crt(String_bldr sb, Criteria crt) {
|
||||
switch (crt.Crt_tid()) {
|
||||
case Criteria_.Tid_eq: Bld_where_eq(sb, Criteria_eq.as_(crt)); break;
|
||||
case Criteria_.Tid_comp: Bld_where_comp(sb, Criteria_comp.as_(crt)); break;
|
||||
case Criteria_.Tid_between: Bld_where_between(sb, Criteria_between.as_(crt)); break;
|
||||
case Criteria_.Tid_in: Bld_where_in(sb, Criteria_in.as_(crt)); break;
|
||||
case Criteria_.Tid_like: Bld_where_like(sb, Criteria_like.as_(crt)); break;
|
||||
case Criteria_.Tid_iomatch: Bld_where_iomatch(sb, Criteria_ioMatch.as_(crt)); break;
|
||||
default: throw Err_.unhandled(crt);
|
||||
}
|
||||
}
|
||||
private void Bld_where_eq(String_bldr sb, Criteria_eq crt) {
|
||||
sb.Add(crt.Negated() ? "!=" : "=");
|
||||
this.Bld_val(sb, Wrap(crt.Value()));
|
||||
}
|
||||
private void Bld_where_comp(String_bldr sb, Criteria_comp crt) {
|
||||
sb.Add_many(crt.XtoSymbol());
|
||||
this.Bld_val(sb, Wrap(crt.Value()));
|
||||
}
|
||||
private void Bld_where_between(String_bldr sb, Criteria_between crt) {
|
||||
sb.Add(crt.Negated() ? " NOT BETWEEN " : " BETWEEN ");
|
||||
this.Bld_val(sb, Wrap(crt.Lhs()));
|
||||
sb.Add(" AND ");
|
||||
this.Bld_val(sb, Wrap(crt.Rhs()));
|
||||
}
|
||||
private void Bld_where_like(String_bldr sb, Criteria_like crt) {
|
||||
sb.Add(crt.Negated() ? " NOT LIKE " : " LIKE ");
|
||||
this.Bld_val(sb, Wrap(crt.Pattern().Raw()));
|
||||
sb.Add_fmt(" ESCAPE '{0}'", crt.Pattern().Escape());
|
||||
}
|
||||
private void Bld_where_in(String_bldr sb, Criteria_in crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IN (" : " IN (");
|
||||
Object[] crt_vals = crt.Values();
|
||||
int len = crt_vals.length;
|
||||
int last = len - 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Object val = crt_vals[i];
|
||||
this.Bld_val(sb, Wrap(val));
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
}
|
||||
private void Bld_where_iomatch(String_bldr sb, Criteria_ioMatch crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IOMATCH " : " IOMATCH ");
|
||||
this.Bld_val(sb, Wrap(crt.Pattern().Raw()));
|
||||
}
|
||||
public void Append_db_obj_ary(String_bldr sb, Db_obj_ary_crt crt) {
|
||||
Object[][] ary = crt.Vals();
|
||||
int ary_len = ary.length;
|
||||
Db_fld[] flds = crt.Flds();
|
||||
for (int i = 0; i < ary_len; i++) {
|
||||
Object[] itm = (Object[])ary[i];
|
||||
int itm_len = itm.length;
|
||||
if (i != 0) sb.Add(" OR ");
|
||||
sb.Add("(");
|
||||
for (int j = 0; j < itm_len; j++) {
|
||||
if (j != 0) sb.Add(" AND ");
|
||||
Db_fld fld = flds[j];
|
||||
Object val = itm[j];
|
||||
boolean quote = false;
|
||||
switch (fld.Type_tid()) {
|
||||
case ClassAdp_.Tid_str:
|
||||
case ClassAdp_.Tid_char:
|
||||
case ClassAdp_.Tid_date:
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
sb.Add(fld.Name());
|
||||
sb.Add("=");
|
||||
if (quote) sb.Add("'");
|
||||
sb.Add(Object_.Xto_str_strict_or_empty(val));
|
||||
if (quote) sb.Add("'");
|
||||
}
|
||||
sb.Add(")");
|
||||
}
|
||||
}
|
||||
private Db_arg Wrap(Object val) {return new Db_arg("unknown", val);}
|
||||
}
|
||||
class Sql_qry_wtr_ansi_escape_backslash extends Sql_qry_wtr_ansi { @Override public void Bld_val_str(String_bldr sb, Db_arg prm, String s) {
|
||||
if (String_.Has(s, "\\")) s = String_.Replace(s, "\\", "\\\\");
|
||||
super.Bld_val_str(sb, prm, s);
|
||||
}
|
||||
}
|
||||
61
140_dbs/src/gplx/dbs/Sql_qry_wtr_tst.java
Normal file
61
140_dbs/src/gplx/dbs/Sql_qry_wtr_tst.java
Normal 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; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class Sql_qry_wtr_tst {
|
||||
private final Sql_qry_wtr_fxt fxt = new Sql_qry_wtr_fxt();
|
||||
@Test public void Val() {
|
||||
fxt.Test_val(null , "NULL");
|
||||
fxt.Test_val(true , "1");
|
||||
fxt.Test_val(false , "0");
|
||||
fxt.Test_val(1 , "1");
|
||||
fxt.Test_val(1.1 , "1.1");
|
||||
fxt.Test_val("a" , "'a'");
|
||||
fxt.Test_val("a'b" , "'a''b'");
|
||||
}
|
||||
@Test public void Where_basic() {
|
||||
fxt.Test_where(Db_crt_.eq_("id", 1), "id=1");
|
||||
fxt.Test_where(Db_crt_.eqn_("id", 1), "id!=1");
|
||||
fxt.Test_where(Db_crt_.mt_("id", 1), "id>1");
|
||||
fxt.Test_where(Db_crt_.mte_("id", 1), "id>=1");
|
||||
fxt.Test_where(Db_crt_.lt_("id", 1), "id<1");
|
||||
fxt.Test_where(Db_crt_.lte_("id", 1), "id<=1");
|
||||
fxt.Test_where(Db_crt_.between_("id", 1, 2), "id BETWEEN 1 AND 2");
|
||||
fxt.Test_where(Db_crt_.in_("id", 1, 2, 3), "id IN (1, 2, 3)");
|
||||
}
|
||||
@Test public void Where_and() {
|
||||
fxt.Test_where(Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 AND name='me')");
|
||||
fxt.Test_where(Criteria_.Or(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 OR name='me')");
|
||||
fxt.Test_where(Criteria_.Or(Db_crt_.eq_("id", 1), Criteria_.And(Db_crt_.eq_("name", "me"), Db_crt_.eq_("id", 1))), "(id=1 OR (name='me' AND id=1))");
|
||||
}
|
||||
}
|
||||
class Sql_qry_wtr_fxt {
|
||||
private final Sql_qry_wtr_ansi sql_wtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.new_ansi();
|
||||
public void Test_val(Object val, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Db_arg arg = new Db_arg("not needed", val);
|
||||
sql_wtr.Bld_val(sb, arg);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
public void Test_where(Criteria crt, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sql_wtr.Bld_where(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
}
|
||||
143
140_dbs/src/gplx/dbs/Sql_select.java
Normal file
143
140_dbs/src/gplx/dbs/Sql_select.java
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
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 Sql_select {
|
||||
public Sql_select_fld_list Flds() {return flds;} Sql_select_fld_list flds = Sql_select_fld_list.new_();
|
||||
public boolean Distinct() {return distinct;} public void Distinct_set(boolean v) {distinct = v;} private boolean distinct;
|
||||
public void Add(String fldName) {flds.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fldName, fldName));}
|
||||
public void Add(String fldName, String alias) {flds.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fldName, alias));}
|
||||
public void Add(Sql_select_fld_base fld) {flds.Add(fld);}
|
||||
|
||||
public static final Sql_select All = all_(); static Sql_select all_() {Sql_select rv = new_(); rv.Add(Sql_select_fld_wild._); return rv;}
|
||||
public static Sql_select new_() {return new Sql_select();} Sql_select() {}
|
||||
}
|
||||
abstract class Sql_select_fld_base {
|
||||
public String Tbl() {return tbl;} public void Tbl_set(String val) {tbl = val;} private String tbl;
|
||||
public String Fld() {return fld;} public void Fld_set(String val) {fld = val;} private String fld;
|
||||
public String Alias() {return alias;} public void Alias_set(String val) {alias = val;} private String alias;
|
||||
public ClassXtn ValType() {return valType;} public void ValType_set(ClassXtn val) {valType = val;} ClassXtn valType = ObjectClassXtn._;
|
||||
public abstract Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type);
|
||||
@gplx.Virtual public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@gplx.Virtual public boolean Type_fld() {return true;}
|
||||
public abstract String XtoSql();
|
||||
public static final String Tbl_null = null;
|
||||
@gplx.Internal protected void ctor_(String tbl, String fld, String alias) {
|
||||
Tbl_set(tbl); Fld_set(fld); Alias_set(alias);
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_wild extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {throw Err_.new_("group by eval not allowed on *");}
|
||||
@Override public void GroupBy_type(GfoFld fld) {throw Err_.new_("group by type not allowed on *");}
|
||||
@Override public String XtoSql() {return "*";}
|
||||
public static final Sql_select_fld_wild _ = new Sql_select_fld_wild(); Sql_select_fld_wild() {this.ctor_(Tbl_null, "*", "*");}
|
||||
}
|
||||
class Sql_select_fld_fld extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {return curVal;}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@Override public String XtoSql() {
|
||||
String rv = Fld();
|
||||
if (Tbl() != Tbl_null)
|
||||
rv = Tbl() + "." + Fld();
|
||||
if (!String_.Eq(Alias(), Fld()))
|
||||
rv = rv + " AS " + Alias();
|
||||
return rv;
|
||||
}
|
||||
public static Sql_select_fld_fld new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_fld rv = new Sql_select_fld_fld();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_fld() {}
|
||||
}
|
||||
abstract class Sql_select_fld_func_base extends Sql_select_fld_base {
|
||||
public abstract String XtoSql_functionName();
|
||||
@Override public boolean Type_fld() {return false;}
|
||||
@Override public String XtoSql() {
|
||||
return String_.Format("{0}({1}) AS {2}", XtoSql_functionName(), Fld(), Alias());
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_count extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "COUNT";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return 1;
|
||||
return Int_.cast_(groupByVal) + 1;
|
||||
}
|
||||
public static Sql_select_fld_count new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_count rv = new Sql_select_fld_count();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_count() {}
|
||||
}
|
||||
class Sql_select_fld_sum extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "SUM";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return Int_.cast_(curVal);
|
||||
return Int_.cast_(groupByVal) + Int_.cast_(curVal);
|
||||
}
|
||||
public static Sql_select_fld_sum new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_sum rv = new Sql_select_fld_sum();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_sum() {}
|
||||
}
|
||||
class Sql_select_fld_minMax extends Sql_select_fld_func_base {
|
||||
int compareType = CompareAble_.Less;
|
||||
@Override public String XtoSql_functionName() {return compareType == CompareAble_.Less ? "MIN" : "MAX";}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return curVal;
|
||||
int compareVal = CompareAble_.Compare_obj(curVal, groupByVal);
|
||||
return compareVal * compareType > 0 ? curVal : groupByVal;
|
||||
}
|
||||
public static Sql_select_fld_minMax min_(String tbl, String fld, String alias) {return new_(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld_minMax max_(String tbl, String fld, String alias) {return new_(CompareAble_.More, tbl, fld, alias);}
|
||||
static Sql_select_fld_minMax new_(int compareType, String tbl, String fld, String alias) {
|
||||
Sql_select_fld_minMax rv = new Sql_select_fld_minMax();
|
||||
rv.compareType = compareType;
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_minMax() {}
|
||||
}
|
||||
class Sql_select_fld_list {
|
||||
public int Count() {return hash.Count();}
|
||||
public void Add(Sql_select_fld_base fld) {hash.Add(fld.Alias(), fld);}
|
||||
public Sql_select_fld_base FetchAt(int i) {return (Sql_select_fld_base)hash.FetchAt(i);}
|
||||
public Sql_select_fld_base FetchOrNull(String k) {return (Sql_select_fld_base)hash.Fetch(k);}
|
||||
public GfoFldList XtoGfoFldLst(TdbTable tbl) {
|
||||
GfoFldList rv = GfoFldList_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base selectFld = this.FetchAt(i);
|
||||
GfoFld fld = tbl.Flds().FetchOrNull(selectFld.Fld());
|
||||
if (fld == null) throw Err_.new_("fld not found in tbl").Add("fldName", selectFld.Fld()).Add("tblName", tbl.Name()).Add("tblFlds", tbl.Flds().XtoStr());
|
||||
if (rv.Has(selectFld.Alias())) throw Err_.new_("alias is not unique").Add("fldName", selectFld.Fld()).Add("flds", rv.XtoStr());
|
||||
selectFld.GroupBy_type(fld);
|
||||
rv.Add(selectFld.Alias(), selectFld.ValType());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String XtoStr() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
sb.Add_fmt("{0},{1}|", fld.Fld(), fld.Alias());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
OrderedHash hash = OrderedHash_.new_();
|
||||
public static Sql_select_fld_list new_() {return new Sql_select_fld_list();} Sql_select_fld_list() {}
|
||||
}
|
||||
32
140_dbs/src/gplx/dbs/Sql_where.java
Normal file
32
140_dbs/src/gplx/dbs/Sql_where.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
class Sql_where {
|
||||
public Criteria Crt() {return crt;} Criteria crt;
|
||||
public static Sql_where merge_or_new_(Sql_where where, Criteria crt) {
|
||||
return where == null
|
||||
? Sql_where.new_(crt)
|
||||
: Sql_where.new_(Criteria_.And(where.Crt(), crt));
|
||||
}
|
||||
public static Sql_where new_(Criteria crt) {
|
||||
Sql_where rv = new Sql_where();
|
||||
rv.crt = crt;
|
||||
return rv;
|
||||
} Sql_where() {}
|
||||
}
|
||||
46
140_dbs/src/gplx/dbs/engines/mems/Db_engine__mem.java
Normal file
46
140_dbs/src/gplx/dbs/engines/mems/Db_engine__mem.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_engine__mem implements Db_engine {
|
||||
private final HashAdp tbl_hash = HashAdp_.new_();
|
||||
public Db_engine__mem(Db_url url, Db_meta_tbl[] meta_tbls) {
|
||||
this.url = url;
|
||||
int tbls_len = meta_tbls.length;
|
||||
for (int i = 0; i < tbls_len; ++i) {
|
||||
Db_meta_tbl meta_tbl = meta_tbls[i];
|
||||
Mem_tbl tbl = new Mem_tbl();
|
||||
tbl_hash.Add(meta_tbl.Name(), tbl);
|
||||
}
|
||||
}
|
||||
public String Tid() {return Db_url__mem.Tid_const;}
|
||||
public Db_url Url() {return url;} private Db_url url;
|
||||
public Db_engine New_clone(Db_url url) {return this;}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt__mem(this, qry);}
|
||||
public Mem_tbl Tbls_get(String name) {return (Mem_tbl)tbl_hash.Fetch(name);}
|
||||
public void Txn_bgn() {++txn_count;} private int txn_count = 0;
|
||||
public void Txn_end() {--txn_count;}
|
||||
public Object Exec_as_obj(Db_qry qry) {throw Err_.not_implemented_();}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {
|
||||
if (txn_count != 0) throw Err_.new_("Conn_term.txns still open; txn_count={0}", txn_count);
|
||||
}
|
||||
public Db_rdr New_rdr_by_obj(Object rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public static final Db_engine__mem _ = new Db_engine__mem(); Db_engine__mem() {}
|
||||
}
|
||||
57
140_dbs/src/gplx/dbs/engines/mems/Db_rdr__mem.java
Normal file
57
140_dbs/src/gplx/dbs/engines/mems/Db_rdr__mem.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_rdr__mem implements Db_rdr {
|
||||
private final Mem_itm[] rows; private int row_idx = -1; private final int rows_len;
|
||||
private final HashAdp ords = HashAdp_.new_();
|
||||
private Mem_itm row;
|
||||
public Db_rdr__mem(String[] cols, Mem_itm[] rows) {
|
||||
this.rows = rows; this.rows_len = rows.length;
|
||||
int cols_len = cols.length;
|
||||
for (int i = 0; i < cols_len; ++i)
|
||||
ords.Add(cols[i], i);
|
||||
}
|
||||
public boolean Move_next() {
|
||||
boolean rv = ++row_idx < rows_len;
|
||||
if (rv)
|
||||
row = rows[row_idx];
|
||||
return rv;
|
||||
}
|
||||
public byte[] Read_bry(int i) {return (byte[])row.Get_at(i);}
|
||||
public byte[] Read_bry(String k) {return (byte[])row.Get_at(Ord_by_key(k));}
|
||||
public String Read_str(int i) {return (String)row.Get_at(i);}
|
||||
public String Read_str(String k) {return (String)row.Get_at(Ord_by_key(k));}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.new_utf8_((String)row.Get_at(i));}
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.new_utf8_((String)row.Get_at(Ord_by_key(k)));}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561((String)row.Get_at(i));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561((String)row.Get_at(Ord_by_key(k)));}
|
||||
public byte Read_byte(int i) {return Byte_.cast_(row.Get_at(i));}
|
||||
public byte Read_byte(String k) {return Byte_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public int Read_int(int i) {return Int_.cast_(row.Get_at(i));}
|
||||
public int Read_int(String k) {return Int_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public long Read_long(int i) {return Long_.cast_(row.Get_at(i));}
|
||||
public long Read_long(String k) {return Long_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public float Read_float(int i) {return Float_.cast_(row.Get_at(i));}
|
||||
public float Read_float(String k) {return Float_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public double Read_double(int i) {return Double_.cast_(row.Get_at(i));}
|
||||
public double Read_double(String k) {return Double_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public boolean Read_bool_by_byte(int i) {return Byte_.cast_(row.Get_at(i)) == 1;}
|
||||
public boolean Read_bool_by_byte(String k) {return Byte_.cast_(row.Get_at(Ord_by_key(k))) == 1;}
|
||||
public void Rls() {}
|
||||
private int Ord_by_key(String k) {return Int_.cast_(ords.Fetch(k));}
|
||||
}
|
||||
127
140_dbs/src/gplx/dbs/engines/mems/Db_stmt__mem.java
Normal file
127
140_dbs/src/gplx/dbs/engines/mems/Db_stmt__mem.java
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_stmt__mem implements Db_stmt {
|
||||
private int val_idx;
|
||||
public Db_stmt__mem(Db_engine__mem engine, Db_qry qry) {this.engine = engine; this.qry = qry;} private Db_engine__mem engine;
|
||||
public int Args_len() {return args.Count();}
|
||||
public Object Args_get_at(int i) {return args.FetchAt(i);}
|
||||
public Db_qry Qry() {return qry;} private Db_qry qry;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
this.Clear();
|
||||
}
|
||||
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, null, 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, null, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
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, null, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(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, null, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
gplx.ios.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(++val_idx, bfr.Xto_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Insert(this) == 1;
|
||||
}
|
||||
public int Exec_update() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Update(this);
|
||||
}
|
||||
public int Exec_delete() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Delete(this);
|
||||
}
|
||||
public DataRdr Exec_select() {throw Err_.not_implemented_();}
|
||||
public Db_rdr Exec_select_as_rdr() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Select(this);
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec select_val: tbl={0}", qry.Base_table());}
|
||||
}
|
||||
private void Add(int idx, Object v) {args.Add(v);} private ListAdp args = ListAdp_.new_();
|
||||
}
|
||||
33
140_dbs/src/gplx/dbs/engines/mems/Db_url__mem.java
Normal file
33
140_dbs/src/gplx/dbs/engines/mems/Db_url__mem.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_url__mem extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mem";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__mem rv = new Db_url__mem();
|
||||
rv.Ctor("", m.ReadStr("database"), raw, raw);
|
||||
return rv;
|
||||
}
|
||||
public static Db_url new_(String database) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "database", database
|
||||
));
|
||||
}
|
||||
public static final Db_url__mem I = new Db_url__mem(); Db_url__mem() {}
|
||||
}
|
||||
29
140_dbs/src/gplx/dbs/engines/mems/Mem_itm.java
Normal file
29
140_dbs/src/gplx/dbs/engines/mems/Mem_itm.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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Mem_itm implements GfoInvkAble {
|
||||
private final OrderedHash hash = OrderedHash_.new_();
|
||||
public Object Get_by(String key) {return hash.Fetch(key);}
|
||||
public Object Get_at(int i) {return hash.FetchAt(i);}
|
||||
public void Set_by(String key, Object val) {hash.AddReplace(key, val);}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
Object rv = Get_by(k);
|
||||
if (rv == null) return GfoInvkAble_.Rv_unhandled;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
91
140_dbs/src/gplx/dbs/engines/mems/Mem_tbl.java
Normal file
91
140_dbs/src/gplx/dbs/engines/mems/Mem_tbl.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.criterias.*;
|
||||
public class Mem_tbl {
|
||||
private final ListAdp rows = ListAdp_.new_(); private final ListAdp where_rows = ListAdp_.new_();
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
Db_qry_insert qry = (Db_qry_insert)stmt.Qry();
|
||||
String[] cols = qry.Cols_for_insert(); int len = cols.length;
|
||||
Mem_itm itm = new Mem_itm();
|
||||
for (int i = 0; i < len; ++i)
|
||||
itm.Set_by(cols[i], stmt.Args_get_at(i));
|
||||
rows.Add(itm);
|
||||
return 1;
|
||||
}
|
||||
public int Update(Db_stmt__mem stmt) {
|
||||
Db_qry_update qry = (Db_qry_update)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Cols_where());
|
||||
int where_rows_len = where_rows.Count();
|
||||
String[] update_cols = qry.Cols_update(); int update_cols_len = update_cols.length;
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)where_rows.FetchAt(i);
|
||||
for (int j = 0; j < update_cols_len; ++j)
|
||||
itm.Set_by(update_cols[j], stmt.Args_get_at(j));
|
||||
}
|
||||
return where_rows_len;
|
||||
}
|
||||
public int Delete(Db_stmt__mem stmt) {
|
||||
Db_qry_delete qry = (Db_qry_delete)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Where_cols());
|
||||
int where_rows_len = where_rows.Count();
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)where_rows.FetchAt(i);
|
||||
rows.Del(itm);
|
||||
}
|
||||
return where_rows_len;
|
||||
}
|
||||
public Db_rdr Select(Db_stmt__mem stmt) {
|
||||
Db_qry__select_in_tbl qry = (Db_qry__select_in_tbl)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Where_flds());
|
||||
return new Db_rdr__mem(qry.Select_flds(), (Mem_itm[])where_rows.Xto_ary_and_clear(Mem_itm.class));
|
||||
}
|
||||
private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, String[] where_cols) {
|
||||
int where_len = where_cols.length;
|
||||
KeyVal[] where_kvs = Bld_where_kvs(stmt, where_cols, where_len);
|
||||
Bld_where_rows(where_rows, where_kvs, where_len);
|
||||
}
|
||||
private KeyVal[] Bld_where_kvs(Db_stmt__mem stmt, String[] keys, int keys_len) {
|
||||
KeyVal[] rv = new KeyVal[keys_len];
|
||||
for (int i = 0; i < keys_len; ++i)
|
||||
rv[i] = KeyVal_.new_(keys[i], stmt.Args_get_at(i));
|
||||
return rv;
|
||||
}
|
||||
private void Bld_where_rows(ListAdp rv, KeyVal[] where_kvs, int where_len) {
|
||||
rv.Clear();
|
||||
int rows_len = rows.Count();
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)rows.FetchAt(i);
|
||||
for (int j = 0; j < where_len; ++j) {
|
||||
KeyVal kv = where_kvs[j];
|
||||
Object itm_val = itm.Get_by(kv.Key());
|
||||
if (!Object_.Eq(itm_val, kv.Val())) break;
|
||||
}
|
||||
rv.Add(itm);
|
||||
}
|
||||
}
|
||||
// private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, Criteria crt) {
|
||||
// rv.Clear();
|
||||
// int rows_len = rows.Count();
|
||||
// for (int i = 0; i < rows_len; ++i) {
|
||||
// Mem_itm itm = (Mem_itm)rows.FetchAt(i);
|
||||
// if (crt.Matches(itm))
|
||||
// rv.Add(itm);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
19
140_dbs/src/gplx/dbs/sqls/Sql_dummy_class.java
Normal file
19
140_dbs/src/gplx/dbs/sqls/Sql_dummy_class.java
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Sql_dummy_class {}
|
||||
24
140_dbs/src_110_dbQry/gplx/dbs/Db_arg.java
Normal file
24
140_dbs/src_110_dbQry/gplx/dbs/Db_arg.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_arg {
|
||||
@gplx.Internal protected 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;
|
||||
}
|
||||
@@ -17,8 +17,8 @@ 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();
|
||||
int Tid();
|
||||
boolean Exec_is_rdr();
|
||||
String Base_table();
|
||||
String Xto_sql();
|
||||
}
|
||||
|
||||
@@ -22,11 +22,11 @@ public class Db_qry_ {
|
||||
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_delete delete_(String tbl, Criteria crt) {return Db_qry_delete.new_(tbl, crt);}
|
||||
public static Db_qry_delete delete_tbl_(String tbl) {return Db_qry_delete.new_(tbl);}
|
||||
public static Db_qry_insert insert_(String tbl) {return new Db_qry_insert(tbl);}
|
||||
public static Db_qry_insert insert_common_(String tbl, KeyVal... pairs) {
|
||||
Db_qry_insert cmd = Db_qry_insert.new_().BaseTable_(tbl);
|
||||
Db_qry_insert cmd = new Db_qry_insert(tbl);
|
||||
for (KeyVal pair : pairs)
|
||||
cmd.Arg_obj_(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
@@ -47,14 +47,5 @@ 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);
|
||||
}
|
||||
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;}
|
||||
public static final int Tid_insert = 0, Tid_delete = 1, Tid_update = 2, Tid_select = 3, Tid_sql = 4, Tid_select_in_tbl = 5, Tid_flush = 6;
|
||||
}
|
||||
|
||||
@@ -20,8 +20,10 @@ 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 int Tid() {return Db_qry_.Tid_select_in_tbl;}
|
||||
public boolean Exec_is_rdr() {return true;}
|
||||
public String Base_table() {return tbl_name;} private final String tbl_name;
|
||||
public String From() {return tbl_name;}
|
||||
public String[] Select_flds() {return select_flds;} private final String[] select_flds;
|
||||
public String[] Where_flds() {return where_flds;} private final String[] where_flds;
|
||||
public void Where_sql(String_bldr sb) {
|
||||
@@ -37,9 +39,6 @@ public class Db_qry__select_in_tbl implements Db_qry {
|
||||
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 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_();
|
||||
@@ -57,5 +56,9 @@ public class Db_qry__select_in_tbl implements Db_qry {
|
||||
if (limit_sql != null) sb.Add(limit_sql);
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public static Db_qry__select_in_tbl new_(String tbl_name, String[] where_flds, String[] select_flds) {
|
||||
Db_qry__select_in_tbl rv = new Db_qry__select_in_tbl(tbl_name, select_flds, where_flds, null, null, null, null);
|
||||
return rv;
|
||||
}
|
||||
public static final String[] Where_flds__all = null;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ 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);
|
||||
@@ -32,9 +31,3 @@ public interface Db_qry_arg_owner extends Db_qry {
|
||||
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;}
|
||||
}
|
||||
|
||||
@@ -16,24 +16,26 @@ 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 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);
|
||||
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() {}
|
||||
import gplx.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 String[] Where_cols() {return where_cols;} private String[] where_cols;
|
||||
public static Db_qry_delete new_all_(String tbl) {
|
||||
Db_qry_delete rv = new Db_qry_delete();
|
||||
rv.baseTable = tbl;
|
||||
Db_qry_delete rv = new_(tbl, Criteria_.All);
|
||||
rv.where_cols = String_.Ary_empty;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_delete new_(String tbl, String... where) {
|
||||
Db_qry_delete rv = Db_qry_delete.new_(tbl, Db_crt_.eq_many_(where));
|
||||
rv.where_cols = where;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_delete new_(String tbl, Criteria where) {return new Db_qry_delete(tbl, where);}
|
||||
}
|
||||
|
||||
@@ -20,11 +20,11 @@ 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"))
|
||||
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(Db_qry_insert.new_().BaseTable_("tbl0").Arg_("id", 0).Arg_("name", "me").Arg_("time", DateAdp_.parse_gplx("2007-12-23"))
|
||||
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() {
|
||||
@@ -40,5 +40,5 @@ public class Db_qry_dml_tst {
|
||||
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());}
|
||||
void tst_XtoSql(Db_qry qry, String expd) {Tfds.Eq(expd, qry.Xto_sql());}
|
||||
}
|
||||
|
||||
@@ -16,14 +16,16 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
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);}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
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);}
|
||||
|
||||
@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);}}
|
||||
|
||||
@@ -16,13 +16,15 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
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_insert(String base_table) {this.base_table = base_table;}
|
||||
public int Tid() {return Db_qry_.Tid_insert;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return base_table;} private String base_table;
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public Db_qry_arg_owner From_(String tbl) {base_table = tbl; return this;}
|
||||
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);}
|
||||
@@ -47,9 +49,18 @@ public class Db_qry_insert implements Db_qry_arg_owner {
|
||||
cols.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fld, fld));
|
||||
return this;
|
||||
}
|
||||
@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 KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected Sql_select_fld_list Cols() {return cols;} private Sql_select_fld_list cols;
|
||||
|
||||
public String[] Cols_for_insert() {return cols_for_insert;} private String[] cols_for_insert;
|
||||
public static Db_qry_insert new_() {return new Db_qry_insert();} Db_qry_insert() {}
|
||||
public static Db_qry_insert new_(String tbl, String... keys) {
|
||||
Db_qry_insert rv = Db_qry_insert.new_();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,18 +16,19 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.criterias.*; import gplx.dbs.sqls.*;
|
||||
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);}
|
||||
public GfoNde ExecRdr_nde(Db_provider provider) {
|
||||
public int Tid() {return Db_qry_.Tid_select;}
|
||||
public boolean Exec_is_rdr() {return true;}
|
||||
public String Base_table() {return from.BaseTable().TblName();}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public DataRdr Exec_qry_as_rdr(Db_conn conn) {return conn.Exec_qry_as_rdr(this);}
|
||||
public GfoNde ExecRdr_nde(Db_conn conn) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {return GfoNde_.rdr_(Exec_qry_as_rdr(provider));} finally {rdr.Rls();}
|
||||
try {return GfoNde_.rdr_(Exec_qry_as_rdr(conn));} finally {rdr.Rls();}
|
||||
}
|
||||
public Object ExecRdr_val(Db_provider provider) {
|
||||
DataRdr rdr = Exec_qry_as_rdr(provider);
|
||||
public Object ExecRdr_val(Db_conn conn) {
|
||||
DataRdr rdr = Exec_qry_as_rdr(conn);
|
||||
try {
|
||||
Object rv = null;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
@@ -46,7 +47,6 @@ public class Db_qry_select implements Db_qry {
|
||||
} 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);}
|
||||
|
||||
@@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_select_tst {
|
||||
@Before public void setup() {
|
||||
cmd = Db_qry_select.new_();
|
||||
@@ -85,5 +85,5 @@ public class Db_qry_select_tst {
|
||||
// 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());}
|
||||
void tst_XtoStr(Db_qry qry, String expd) {Tfds.Eq(expd, cmd.Xto_sql());}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,13 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
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);}
|
||||
public String XtoSql() {return sql;} private String sql;
|
||||
public int Tid() {return Db_qry_.Tid_sql;}
|
||||
public boolean Exec_is_rdr() {return isReader;} private boolean isReader;
|
||||
public String Base_table() {throw Err_.not_implemented_();}
|
||||
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);}
|
||||
@@ -37,4 +38,45 @@ public class Db_qry_sql implements Db_qry {
|
||||
}
|
||||
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);}}
|
||||
public static String Gen_sql(Db_qry qry, Object... args) {
|
||||
byte[] src = Bry_.new_utf8_(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_utf8_(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_utf8_("NULL"), Bry_escape_apos = Bry_.new_ascii_("''");
|
||||
}
|
||||
|
||||
47
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_sql_tst.java
Normal file
47
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_sql_tst.java
Normal 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; import gplx.*;
|
||||
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));}
|
||||
}
|
||||
@@ -16,14 +16,13 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.criterias.*; import gplx.dbs.sqls.*;
|
||||
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 int Tid() {return Db_qry_.Tid_update;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return baseTable;}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public Db_qry_arg_owner From_(String tbl) {baseTable = tbl; return this;}
|
||||
public 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);}
|
||||
@@ -46,9 +45,19 @@ public class Db_qry_update implements Db_qry_arg_owner {
|
||||
where = Sql_where.merge_or_new_(where, crt);
|
||||
return this;
|
||||
}
|
||||
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} private String baseTable;
|
||||
@gplx.Internal protected 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;
|
||||
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} private Sql_where where;
|
||||
public String[] Cols_where() {return cols_where;} private String[] cols_where;
|
||||
public String[] Cols_update() {return cols_update;} private String[] cols_update;
|
||||
public static Db_qry_update new_() {return new Db_qry_update();} Db_qry_update() {}
|
||||
public static Db_qry_update new_(String tbl, String[] where, String... update) {
|
||||
Db_qry_update rv = Db_qry_.update_(tbl, Db_crt_.eq_many_(where));
|
||||
rv.cols_update = update;
|
||||
rv.cols_where = where;
|
||||
int len = update.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
rv.Arg_obj_(update[i], null);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,19 +17,36 @@ 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 Crt_bool_as_byte(String k, boolean v);
|
||||
Db_stmt Val_bool_as_byte(String k, boolean v);
|
||||
Db_stmt Val_bool_as_byte(boolean v);
|
||||
Db_stmt Crt_byte(String k, byte v);
|
||||
Db_stmt Val_byte(String k, byte v);
|
||||
Db_stmt Val_byte(byte v);
|
||||
Db_stmt Crt_int(String k, int v);
|
||||
Db_stmt Val_int(String k, int v);
|
||||
Db_stmt Val_int(int v);
|
||||
Db_stmt Crt_long(String k, long v);
|
||||
Db_stmt Val_long(String k, long v);
|
||||
Db_stmt Val_long(long v);
|
||||
Db_stmt Crt_float(String k, float v);
|
||||
Db_stmt Val_float(String k, float v);
|
||||
Db_stmt Val_float(float v);
|
||||
Db_stmt Crt_double(String k, double v);
|
||||
Db_stmt Val_double(String k, double v);
|
||||
Db_stmt Val_double(double v);
|
||||
Db_stmt Crt_decimal(String k, DecimalAdp v);
|
||||
Db_stmt Val_decimal(String k, DecimalAdp v);
|
||||
Db_stmt Val_decimal(DecimalAdp v);
|
||||
Db_stmt Crt_bry(String k, byte[] v);
|
||||
Db_stmt Val_bry(String k, byte[] v);
|
||||
Db_stmt Val_bry(byte[] v);
|
||||
Db_stmt Crt_str(String k, String v);
|
||||
Db_stmt Val_str(String k, String v);
|
||||
Db_stmt Val_str(String v);
|
||||
Db_stmt Crt_bry_as_str(String k, byte[] v);
|
||||
Db_stmt Val_bry_as_str(String k, byte[] v);
|
||||
Db_stmt Val_bry_as_str(byte[] v);
|
||||
Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr rdr, long rdr_len);
|
||||
boolean Exec_insert();
|
||||
int Exec_update();
|
||||
@@ -38,5 +55,5 @@ public interface Db_stmt extends RlsAble {
|
||||
Db_rdr Exec_select_as_rdr();
|
||||
Object Exec_select_val();
|
||||
Db_stmt Clear();
|
||||
Db_stmt New();
|
||||
Db_stmt Reset_stmt();
|
||||
}
|
||||
|
||||
@@ -18,39 +18,40 @@ 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_insert_(Db_conn conn, String tbl, String... flds) {
|
||||
Db_qry qry = Db_qry_insert.new_(tbl, flds);
|
||||
return conn.New_stmt(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_update_(Db_conn conn, String tbl, String[] where, String... flds) {
|
||||
Db_qry qry = Db_qry_update.new_(tbl, where, flds);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_delete_(Db_provider provider, String tbl, String... where) {
|
||||
public static Db_stmt new_delete_(Db_conn conn, String tbl, String... where) {
|
||||
Db_qry_delete qry = Db_qry_.delete_(tbl, Db_crt_.eq_many_(where));
|
||||
return provider.Prepare(qry);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_select_(Db_provider provider, String tbl, String[] where, String... flds) {
|
||||
public static Db_stmt new_select_(Db_conn conn, 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);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_select_in_(Db_provider provider, String tbl, String in_fld, Object[] in_vals, String... flds) {
|
||||
public static Db_stmt new_select_in_(Db_conn conn, 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);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
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_all_(Db_conn conn, String tbl) {
|
||||
return conn.New_stmt(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);
|
||||
public static Db_stmt new_select_as_rdr(Db_conn conn, Db_qry__select_in_tbl qry) {
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_select_as_rdr(Db_provider provider, String sql) {
|
||||
return provider.Prepare(Db_qry_sql.rdr_(sql));
|
||||
public static Db_stmt new_select_as_rdr(Db_conn conn, String sql) {
|
||||
return conn.New_stmt(Db_qry_sql.rdr_(sql));
|
||||
}
|
||||
public static Err err_(Exception e, Db_stmt stmt, String proc) {
|
||||
throw Err_.err_(e, String_.Format("db stmt failed: proc=~{0} err=~{1}", proc, Err_.Message_gplx_brief(e)));
|
||||
}
|
||||
public static Err err_(Exception e, String tbl, String proc) {
|
||||
throw Err_.err_(e, String_.Format("db call failed: tbl=~{0} proc=~{1} err=~{2}", tbl, proc, Err_.Message_gplx_brief(e)));
|
||||
}
|
||||
public static Db_stmt Rls(Db_stmt v) {if (v != null) v.Rls(); return null;}
|
||||
}
|
||||
|
||||
@@ -17,32 +17,32 @@ 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_conn conn;
|
||||
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();
|
||||
public Db_stmt_bldr Init(Db_conn v) {
|
||||
this.conn = v;
|
||||
conn.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.Create: if (create == null) create = Db_stmt_.new_insert_(conn, tbl_name, flds_all); return create;
|
||||
case Db_cmd_mode.Update: if (update == null) update = Db_stmt_.new_update_(conn, tbl_name, flds_keys, flds_vals); return update;
|
||||
case Db_cmd_mode.Delete: if (delete == null) delete = Db_stmt_.new_delete_(conn, 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();
|
||||
conn.Txn_mgr().Txn_end_all();
|
||||
}
|
||||
public void Rls() {
|
||||
provider = null;
|
||||
conn = null;
|
||||
create = Rls(create);
|
||||
update = Rls(update);
|
||||
delete = Rls(delete);
|
||||
|
||||
@@ -17,57 +17,81 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
class Db_stmt_cmd implements Db_stmt {
|
||||
private Db_engine engine;
|
||||
private final 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 = qry.Tid() == Db_qry_.Tid_select_in_tbl ? ((Db_qry__select_in_tbl)qry).Xto_sql() : Sql_cmd_wtr_.Ansi.XtoSqlQry(qry, true);
|
||||
New();
|
||||
private String sql; private int val_idx = 0;
|
||||
public Db_stmt_cmd(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 New() {
|
||||
stmt = (PreparedStatement)engine.New_db_cmd(sql);
|
||||
public Db_stmt Reset_stmt() {
|
||||
stmt = (PreparedStatement)engine.New_stmt_prep_as_obj(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) {
|
||||
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, null, 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, null, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, 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) {
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {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) {
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {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) {
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {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) {
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {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) {
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {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));}
|
||||
public Db_stmt Val_bry_(byte[] v) {
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {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) {
|
||||
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, null, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(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, null, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, 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;
|
||||
}
|
||||
@@ -85,13 +109,13 @@ class Db_stmt_cmd implements Db_stmt {
|
||||
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);}
|
||||
try {DataRdr rv = engine.New_rdr(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);}
|
||||
try {return engine.New_rdr_by_obj(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);}
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(engine.New_rdr(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;
|
||||
|
||||
@@ -16,48 +16,73 @@ 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 {
|
||||
public class Db_stmt_sql implements Db_stmt {// used for formatting SQL statements; not used for actual insert into database
|
||||
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) {
|
||||
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) {
|
||||
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, null, 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, null, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, 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) {
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, 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) {
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, 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) {
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, 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) {
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, 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) {
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, v.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));}
|
||||
public Db_stmt Val_bry_(byte[] v) { // HACK: convert to String b/c tdb does not support byte[]
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {// 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) {
|
||||
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, null, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(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, null, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, 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;
|
||||
}
|
||||
@@ -69,20 +94,20 @@ class Db_stmt_sql implements Db_stmt {
|
||||
} 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) {
|
||||
private 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);}
|
||||
try {boolean rv = conn.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);}
|
||||
try {int rv = conn.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);}
|
||||
try {int rv = conn.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);}
|
||||
try {DataRdr rv = conn.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() {
|
||||
@@ -97,13 +122,17 @@ class Db_stmt_sql implements Db_stmt {
|
||||
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 void Add(int idx, String v) {args.Add(v);} private ListAdp args = ListAdp_.new_();
|
||||
public String Xto_sql() {
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, (Object[])args.Xto_ary_and_clear(Object.class));
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
String sql_orig;
|
||||
public Db_stmt Parse(String sql_str) {
|
||||
public int Args_len() {return args.Count();}
|
||||
public String Args_get_at(int i) {return (String)args.FetchAt(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;
|
||||
int arg_idx = 0;
|
||||
byte[] src = Bry_.new_utf8_(sql_str);
|
||||
|
||||
@@ -21,7 +21,7 @@ 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.Parse(null, "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());
|
||||
|
||||
@@ -26,7 +26,7 @@ public interface Db_txn_mgr {
|
||||
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;} private Db_engine engine;
|
||||
public Db_txn_mgr_base(Db_engine engine) {this.engine = engine;} private final 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() {
|
||||
|
||||
@@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public class Db_obj_ary_tst {
|
||||
@Before public void init() {} private Db_obj_ary_fxt fxt = new Db_obj_ary_fxt();
|
||||
@Test public void Int() {
|
||||
@@ -31,7 +31,7 @@ class Db_obj_ary_fxt {
|
||||
public Db_obj_ary_fxt Init_fld(String name, byte tid) {flds_list.Add(new Db_fld(name, tid)); return this;} private ListAdp flds_list = ListAdp_.new_();
|
||||
public Db_obj_ary_fxt Init_vals(Object... ary) {vals_list.Add(ary); return this;} private ListAdp vals_list = ListAdp_.new_();
|
||||
public Db_obj_ary_fxt Test_sql(String expd) {
|
||||
Sql_cmd_wtr_ansi cmd_wtr = (Sql_cmd_wtr_ansi)Sql_cmd_wtr_ansi_.default_();
|
||||
Sql_qry_wtr_ansi cmd_wtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.I;
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
crt.Flds_((Db_fld[])flds_list.Xto_ary_and_clear(Db_fld.class));
|
||||
crt.Vals_((Object[][])vals_list.Xto_ary_and_clear(Object[].class));
|
||||
|
||||
@@ -17,12 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class PoolIds {
|
||||
public int FetchNext(Db_provider provider, String url) {
|
||||
public int FetchNext(Db_conn conn, String url) {
|
||||
Db_qry_select cmd = Db_qry_.select_().From_(Tbl_Name).Where_(Db_crt_.eq_(Fld_id_path, url));
|
||||
int rv = 0;//boolean isNew = true;
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = cmd.Exec_qry_as_rdr(provider);
|
||||
rdr = cmd.Exec_qry_as_rdr(conn);
|
||||
if (rdr.MoveNextPeer()) {
|
||||
rv = rdr.ReadInt(Fld_id_next_id);
|
||||
}
|
||||
@@ -31,17 +31,17 @@ public class PoolIds {
|
||||
return rv;
|
||||
}
|
||||
public int FetchNextAndCommit(String dbInfo, String url) {
|
||||
Db_provider provider = Db_provider_pool._.Get_or_new(dbInfo);
|
||||
int rv = PoolIds._.FetchNext(provider, url);
|
||||
PoolIds._.Commit(provider, url, rv + 1);
|
||||
Db_conn conn = Db_conn_pool_old._.Get_or_new(dbInfo);
|
||||
int rv = PoolIds._.FetchNext(conn, url);
|
||||
PoolIds._.Commit(conn, url, rv + 1);
|
||||
return rv;
|
||||
}
|
||||
public void Commit(Db_provider provider, String url, int val) {
|
||||
int rv = provider.Exec_qry(Db_qry_.update_(Tbl_Name, Db_crt_.eq_(Fld_id_path, url)).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
public void Commit(Db_conn conn, String url, int val) {
|
||||
int rv = conn.Exec_qry(Db_qry_.update_(Tbl_Name, Db_crt_.eq_(Fld_id_path, url)).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
if (rv == 0) {
|
||||
rv = provider.Exec_qry(Db_qry_.insert_(Tbl_Name).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
rv = conn.Exec_qry(Db_qry_.insert_(Tbl_Name).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
}
|
||||
if (rv != 1) throw Err_.new_("failed to update nextId").Add("provider", provider.Conn_info().Str_raw()).Add("url", url).Add("nextId", val);
|
||||
if (rv != 1) throw Err_.new_("failed to update nextId").Add("url", url).Add("nextId", val);
|
||||
}
|
||||
public static final String Tbl_Name = "pool_ids";
|
||||
@gplx.Internal protected static final String Fld_id_path = "id_path";
|
||||
|
||||
@@ -17,42 +17,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_engine {
|
||||
String Key();
|
||||
String Conn_info_tid();
|
||||
Db_conn_info Conn_info();
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
String Tid();
|
||||
Db_url Url();
|
||||
Db_engine New_clone(Db_url url);
|
||||
Db_rdr New_rdr_by_obj(Object o, String sql); // Object o:ResultSet if desktop; Cursor if android
|
||||
Db_stmt New_stmt_prep(Db_qry qry);
|
||||
Object New_stmt_prep_as_obj(String sql);
|
||||
DataRdr New_rdr(java.sql.ResultSet rdr, String sql);
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
Db_engine Make_new(Db_conn_info conn_info);
|
||||
|
||||
Db_rdr New_db_rdr(Object o, String sql); // ResultSet if desktop; Cursor if android
|
||||
Db_stmt New_db_stmt(Db_provider provider, Db_qry sql);
|
||||
Object Execute(Db_qry cmd);
|
||||
DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql);
|
||||
Object New_db_cmd(String sql);
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
Object Exec_as_obj(Db_qry qry);
|
||||
}
|
||||
class Db_engine_null implements Db_engine {
|
||||
public String Key() {return Db_conn_info_.Null.Key();}
|
||||
public String Conn_info_tid() {return Db_conn_info_.Null.Key();}
|
||||
public Db_conn_info Conn_info() {return Db_conn_info_.Null;}
|
||||
public String Tid() {return Db_url__null.Tid_const;}
|
||||
public Db_url Url() {return Db_url_.Null;}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {}
|
||||
public Db_engine New_clone(Db_url url) {return this;}
|
||||
public Db_rdr New_rdr_by_obj(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return Db_stmt_.Null;}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public Db_engine Make_new(Db_conn_info conn_info) {return this;}
|
||||
|
||||
public Object Execute(Db_qry cmd) {return cmd.ExecRdrAble() ? (Object)DataRdr_.Null : -1;}
|
||||
public Object New_db_cmd(String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public Db_rdr New_db_rdr(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public Db_stmt New_db_stmt(Db_provider provider, Db_qry qry) {return Db_stmt_.Null;}
|
||||
public Object Exec_as_obj(Db_qry cmd) {return cmd.Exec_is_rdr() ? (Object)DataRdr_.Null : -1;}
|
||||
public static final Db_engine_null _ = new Db_engine_null(); Db_engine_null() {}
|
||||
}
|
||||
class ExecSqlWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmd) {
|
||||
Db_engine_sql_base engine = (Db_engine_sql_base)engineObj;
|
||||
String sql = engine.SqlWtr().XtoSqlQry(cmd, false); // Tfds.Write(sql);
|
||||
return cmd.ExecRdrAble() ? (Object)engine.ExecuteReader(sql) : engine.ExecuteNonQuery(sql);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class Db_engine_regy {
|
||||
.Add(Sqlite_engine._)
|
||||
;
|
||||
}
|
||||
public Db_engine_regy Add(Db_engine engine) {hash.Add(engine.Key(), engine); return this;}
|
||||
public Db_engine_regy Add(Db_engine engine) {hash.Add(engine.Tid(), engine); return this;}
|
||||
public Db_engine Get(String key) {return (Db_engine)hash.FetchOrFail(key);}
|
||||
public static final Db_engine_regy _ = new Db_engine_regy(1);
|
||||
}
|
||||
|
||||
@@ -17,68 +17,65 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
abstract class Db_engine_sql_base implements Db_engine {
|
||||
public abstract String Key();
|
||||
public abstract String Conn_info_tid();
|
||||
public Db_conn_info Conn_info() {return conn_info;} protected Db_conn_info conn_info;
|
||||
@gplx.Virtual public void Txn_bgn() {Execute(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));}
|
||||
@gplx.Virtual public void Txn_end() {Execute(Db_qry_sql.xtn_("COMMIT TRANSACTION;"));}
|
||||
public abstract Db_engine Make_new(Db_conn_info conn_info);
|
||||
@gplx.Virtual public Sql_cmd_wtr SqlWtr() {return Sql_cmd_wtr_.Ansi;}
|
||||
public Object Execute(Db_qry cmd) {
|
||||
Db_qryWkr wkr = (Db_qryWkr)wkrs.FetchOrFail(cmd.KeyOfDb_qry());
|
||||
return wkr.Exec(this, cmd);
|
||||
@gplx.Internal protected void Ctor(Db_url url) {this.url = url;}
|
||||
public abstract String Tid();
|
||||
public Db_url Url() {return url;} protected Db_url url;
|
||||
public abstract Db_engine New_clone(Db_url url);
|
||||
public Db_rdr New_rdr_by_obj(Object rdr, String sql) {
|
||||
Db_rdr__basic rv = New_rdr_clone();
|
||||
rv.Ctor((ResultSet)rdr, sql);
|
||||
return rv;
|
||||
}
|
||||
@gplx.Internal protected void ctor_SqlEngineBase(Db_conn_info conn_info) {
|
||||
this.conn_info = conn_info;
|
||||
wkrs.Add(Db_qry_select.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_insert.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_update.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_delete.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_sql.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_flush.KeyConst, Db_qryWkr_.Null);
|
||||
} HashAdp wkrs = HashAdp_.new_();
|
||||
@gplx.Internal @gplx.Virtual protected int ExecuteNonQuery(String sql) {
|
||||
@gplx.Virtual public Db_rdr__basic New_rdr_clone() {return new Db_rdr__basic();}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt_cmd(this, qry);}
|
||||
public void Txn_bgn() {Exec_as_obj(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));}
|
||||
public void Txn_end() {Exec_as_obj(Db_qry_sql.xtn_("COMMIT TRANSACTION;"));}
|
||||
public Object Exec_as_obj(Db_qry qry) {
|
||||
if (qry.Tid() == Db_qry_.Tid_flush) return null; // ignore flush (delete-db) statements
|
||||
String sql = this.SqlWtr().Xto_str(qry, false); // DBG: Tfds.Write(sql);
|
||||
return qry.Exec_is_rdr() ? (Object)this.Exec_as_rdr(sql) : this.Exec_as_int(sql);
|
||||
}
|
||||
private int Exec_as_int(String sql) {
|
||||
try {
|
||||
Statement cmd = NewDbCmd(sql);
|
||||
return cmd.executeUpdate(sql);
|
||||
Statement cmd = New_stmt_exec(sql);
|
||||
return cmd.executeUpdate(sql);
|
||||
}
|
||||
catch (Exception exc) {throw Err_.err_(exc, "exec nonQuery failed").Add("sql", sql).Add("err", Err_.Message_gplx_brief(exc));}
|
||||
}
|
||||
@gplx.Internal @gplx.Virtual protected DataRdr ExecuteReader(String sql) {
|
||||
private DataRdr Exec_as_rdr(String sql) {
|
||||
try {
|
||||
Statement cmd = NewDbCmd(sql);
|
||||
Statement cmd = New_stmt_exec(sql);
|
||||
cmd.execute(sql);
|
||||
ResultSet rdr = cmd.getResultSet();
|
||||
return NewDataRdr(rdr, sql);
|
||||
return New_rdr(rdr, sql);
|
||||
}
|
||||
catch (Exception exc) {throw Err_.err_(exc, "exec reader failed").Add("sql", sql).Add("err", Err_.Message_gplx_brief(exc));}
|
||||
}
|
||||
@gplx.Virtual public DataRdr New_rdr(ResultSet rdr, String sql) {return gplx.stores.Db_data_rdr_.new_(rdr, sql);}
|
||||
@gplx.Virtual public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_ansi();}
|
||||
@gplx.Internal protected abstract Connection Conn_new();
|
||||
@gplx.Virtual public DataRdr NewDataRdr(ResultSet rdr, String sql) {return gplx.stores.Db_data_rdr_.new_(rdr, sql);}
|
||||
public Db_rdr New_db_rdr(Object rdr, String sql) {return new Db_rdr__basic((ResultSet)rdr, sql);}
|
||||
public Db_stmt New_db_stmt(Db_provider provider, Db_qry qry) {return new Db_stmt_cmd(provider, qry);}
|
||||
public Object New_db_cmd(String sql) {
|
||||
try {return connection.prepareStatement(sql);}
|
||||
catch (Exception e) {
|
||||
throw Err_.err_(e, "failed to prepare sql; sql={0}", sql);}
|
||||
}
|
||||
public void Conn_open() {
|
||||
connection = Conn_new();
|
||||
} private Connection connection;
|
||||
private Connection connection;
|
||||
public void Conn_open() {connection = Conn_new();}
|
||||
public void Conn_term() {
|
||||
// if (Env_.Mode_testing()) return; // WORKAROUND:MYSQL:else errors randomly when running all tests. possible connection pooling issue (?); // commented out 2013-08-22
|
||||
if (connection == null) return; // connection never opened; just exit
|
||||
try {connection.close();}
|
||||
catch (SQLException e) {throw Err_.err_(e, "close connection failed").Add("ConnectInfo", conn_info.Str_raw());}
|
||||
catch (Exception e) {throw Err_.err_(e, "Conn_term.fail; url={0} err={1}", url.Xto_raw(), Err_.Message_lang(e));}
|
||||
connection = null;
|
||||
}
|
||||
Statement NewDbCmd(String commandText) {
|
||||
Statement cmd = null;
|
||||
try {cmd = connection.createStatement();}
|
||||
catch (SQLException e) {throw Err_.err_(e, "could not create statement").Add("commandText", commandText).Add("e", Err_.Message_lang(e));}
|
||||
return cmd;
|
||||
public Object New_stmt_prep_as_obj(String sql) {
|
||||
if (connection == null) connection = Conn_new(); // auto-open connection
|
||||
try {return connection.prepareStatement(sql);}
|
||||
catch (Exception e) {throw Err_.err_(e, "New_stmt_prep.fail; sql={0} err={1}", sql, Err_.Message_lang(e));}
|
||||
}
|
||||
protected Connection NewDbCon(String url, String uid, String pwd) {
|
||||
private Statement New_stmt_exec(String sql) {
|
||||
if (connection == null) connection = Conn_new(); // auto-open connection
|
||||
try {return connection.createStatement();}
|
||||
catch (Exception e) {throw Err_.err_(e, "New_stmt_exec.fail; sql={0} err={1}", sql, Err_.Message_lang(e));}
|
||||
}
|
||||
protected Connection Conn_make_by_url(String url, String uid, String pwd) {
|
||||
try {return DriverManager.getConnection(url, uid, pwd);}
|
||||
catch (SQLException e) {throw Err_.err_(e, "connection open failed").Add("ConnectInfo", Conn_info().Str_raw());}
|
||||
catch (SQLException e) {throw Err_.err_(e, "connection open failed").Add("ConnectInfo", Url().Xto_raw());}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,21 +16,20 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.stores.*;
|
||||
import gplx.stores.*; import gplx.dbs.sqls.*;
|
||||
import java.sql.*;
|
||||
class Mysql_engine extends Db_engine_sql_base {
|
||||
@Override public String Key() {return Db_conn_info__mysql.Key_const;}
|
||||
@Override public String Conn_info_tid() {return this.Key();}
|
||||
@Override public Sql_cmd_wtr SqlWtr() {return Sql_cmd_wtr_.BackslashSensitive;}
|
||||
@Override public Db_engine Make_new(Db_conn_info connectInfo) {
|
||||
@Override public String Tid() {return Db_url__mysql.Tid_const;}
|
||||
@Override public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_escape_backslash();}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
Mysql_engine rv = new Mysql_engine();
|
||||
rv.ctor_SqlEngineBase(connectInfo);
|
||||
rv.Ctor(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr NewDataRdr(ResultSet rdr, String commandText) {return Mysql_rdr.new_(rdr, commandText);}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Mysql_rdr.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Db_conn_info__mysql connUrl = (Db_conn_info__mysql)conn_info;
|
||||
return NewDbCon("jdbc:mysql://localhost/" + connUrl.Database() + "?characterEncoding=UTF8", connUrl.Uid(), connUrl.Pwd());
|
||||
Db_url__mysql url_as_mysql = (Db_url__mysql)url;
|
||||
return Conn_make_by_url("jdbc:mysql://localhost/" + url_as_mysql.Database() + "?characterEncoding=UTF8", url_as_mysql.Uid(), url_as_mysql.Pwd());
|
||||
}
|
||||
@gplx.Internal protected static final Mysql_engine _ = new Mysql_engine(); Mysql_engine() {}
|
||||
}
|
||||
|
||||
@@ -16,21 +16,20 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.stores.*;
|
||||
import gplx.stores.*; import gplx.dbs.sqls.*;
|
||||
import java.sql.*;
|
||||
class Postgres_engine extends Db_engine_sql_base {
|
||||
@Override public String Key() {return Db_conn_info__postgres.Key_const;}
|
||||
@Override public String Conn_info_tid() {return this.Key();}
|
||||
@Override public Sql_cmd_wtr SqlWtr() {return Sql_cmd_wtr_.BackslashSensitive;}
|
||||
@Override public Db_engine Make_new(Db_conn_info connectInfo) {
|
||||
@Override public String Tid() {return Db_url__postgres.Tid_const;}
|
||||
@Override public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_escape_backslash();}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
Postgres_engine rv = new Postgres_engine();
|
||||
rv.ctor_SqlEngineBase(connectInfo);
|
||||
rv.Ctor(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr NewDataRdr(ResultSet rdr, String commandText) {return Db_data_rdr_.new_(rdr, commandText);}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Db_data_rdr_.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Db_conn_info__postgres connUrl = (Db_conn_info__postgres)conn_info;
|
||||
return NewDbCon("jdbc:" + connUrl.Key() + "://localhost/" + connUrl.Database(), connUrl.Uid(), connUrl.Pwd());
|
||||
Db_url__postgres url_as_postgres = (Db_url__postgres)url;
|
||||
return Conn_make_by_url("jdbc:" + url_as_postgres.Tid() + "://localhost/" + url_as_postgres.Database(), url_as_postgres.Uid(), url_as_postgres.Pwd());
|
||||
}
|
||||
@gplx.Internal protected static final Postgres_engine _ = new Postgres_engine(); Postgres_engine() {}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ package gplx.dbs; import gplx.*;
|
||||
import gplx.stores.*;
|
||||
import java.sql.*;
|
||||
class Sqlite_engine extends Db_engine_sql_base {
|
||||
@Override public String Key() {return Db_conn_info__sqlite.Key_const;}
|
||||
@Override public String Conn_info_tid() {return this.Key();}
|
||||
@Override public Db_engine Make_new(Db_conn_info connectInfo) {
|
||||
@Override public String Tid() {return Db_url__sqlite.Tid_const;}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
Sqlite_engine rv = new Sqlite_engine();
|
||||
rv.ctor_SqlEngineBase(connectInfo);
|
||||
rv.Ctor(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr NewDataRdr(ResultSet rdr, String commandText) {return Sqlite_rdr.new_(rdr, commandText);}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Sqlite_rdr.new_(rdr, commandText);}
|
||||
@Override public Db_rdr__basic New_rdr_clone() {return new Db_rdr__sqlite();}
|
||||
static boolean loaded = false;
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
if (!loaded) {
|
||||
@@ -36,8 +36,8 @@ class Sqlite_engine extends Db_engine_sql_base {
|
||||
catch (ClassNotFoundException e) {throw Err_.new_("could not load sqlite jdbc driver");}
|
||||
loaded = true;
|
||||
}
|
||||
Db_conn_info__sqlite connUrl = (Db_conn_info__sqlite)conn_info;
|
||||
return NewDbCon("jdbc:sqlite://" + String_.Replace(connUrl.Database(), "\\", "/"), "", "");
|
||||
Db_url__sqlite url_as_sqlite = (Db_url__sqlite)url;
|
||||
return Conn_make_by_url("jdbc:sqlite://" + String_.Replace(url_as_sqlite.Database(), "\\", "/"), "", "");
|
||||
}
|
||||
private boolean pragma_needed = true;
|
||||
@Override public void Txn_bgn() {
|
||||
@@ -45,16 +45,19 @@ class Sqlite_engine extends Db_engine_sql_base {
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA journal_mode = OFF;")); // will cause out of memory
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA journal_mode = MEMORY;"));
|
||||
if (pragma_needed) {
|
||||
Execute(Db_qry_sql.xtn_("PRAGMA synchronous = OFF;"));
|
||||
Exec_as_obj(Db_qry_sql.xtn_("PRAGMA synchronous = OFF;"));
|
||||
pragma_needed = false;
|
||||
}
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA temp_store = MEMORY;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA locking_mode = EXCLUSIVE;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA cache_size=4000;")); // too many will also cause out of memory
|
||||
Execute(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));
|
||||
Exec_as_obj(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));
|
||||
}
|
||||
@gplx.Internal protected static final Sqlite_engine _ = new Sqlite_engine(); Sqlite_engine() {}
|
||||
}
|
||||
class Db_rdr__sqlite extends Db_rdr__basic { @Override public byte Read_byte(int i) {try {return (byte)rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@Override public byte Read_byte(String k) {try {return (byte)Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
}
|
||||
class Sqlite_rdr extends Db_data_rdr { @Override public float ReadFloat(String key) {return ReadFloatOr(key, Float.NaN);}
|
||||
@Override public float ReadFloatOr(String key, float or) {
|
||||
Object val = Read(key);
|
||||
|
||||
@@ -17,45 +17,45 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Sqlite_engine_ {
|
||||
public static void Db_attach(Db_provider p, String alias, String url) {
|
||||
public static void Db_attach(Db_conn p, String alias, String url) {
|
||||
String s = String_.Format("ATTACH '{0}' AS {1};", url, alias);
|
||||
Db_qry qry = Db_qry_sql.xtn_(s);
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Db_detach(Db_provider p, String alias) {
|
||||
public static void Db_detach(Db_conn p, String alias) {
|
||||
String s = String_.Format("DETACH '{0}';", alias);
|
||||
Db_qry qry = Db_qry_sql.xtn_(s);
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Tbl_create_and_delete(Db_provider p, String tbl_name, String tbl_sql) {
|
||||
public static void Tbl_create_and_delete(Db_conn p, String tbl_name, String tbl_sql) {
|
||||
Tbl_delete(p, tbl_name);
|
||||
Db_qry qry = Db_qry_sql.ddl_(tbl_sql);
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Tbl_create(Db_provider p, String tbl_name, String tbl_sql) {
|
||||
public static void Tbl_create(Db_conn p, String tbl_name, String tbl_sql) {
|
||||
Db_qry qry = Db_qry_sql.ddl_(tbl_sql);
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Tbl_delete_many(Db_provider p, String... tbls) {
|
||||
public static void Tbl_delete_many(Db_conn p, String... tbls) {
|
||||
int len = tbls.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
Tbl_delete(p, tbls[i]);
|
||||
}
|
||||
public static void Tbl_delete(Db_provider p, String tbl) {
|
||||
public static void Tbl_delete(Db_conn p, String tbl) {
|
||||
Db_qry qry = Db_qry_sql.ddl_("DROP TABLE IF EXISTS " + tbl + ";");
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Tbl_rename(Db_provider p, String src, String trg) {
|
||||
public static void Tbl_rename(Db_conn p, String src, String trg) {
|
||||
Db_qry qry = Db_qry_sql.ddl_(String_.Format("ALTER TABLE {0} RENAME TO {1};", src, trg));
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Pragma_page_size_4096(Db_provider p) {Pragma_page_size(p, 4096);}
|
||||
public static void Pragma_page_size(Db_provider p, int val) {
|
||||
public static void Pragma_page_size_4096(Db_conn p) {Pragma_page_size(p, 4096);}
|
||||
public static void Pragma_page_size(Db_conn p, int val) {
|
||||
Db_qry qry = Db_qry_sql.ddl_("PRAGMA page_size = " + Int_.Xto_str(val) + ";");
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Idx_create(Db_provider p, Db_idx_itm... idxs) {Idx_create(Gfo_usr_dlg_.Null, p, "", idxs);}
|
||||
public static void Idx_create(Gfo_usr_dlg usr_dlg, Db_provider p, String file_id, Db_idx_itm... idxs) {
|
||||
public static void Idx_create(Db_conn p, Db_idx_itm... idxs) {Idx_create(Gfo_usr_dlg_.Null, p, "", idxs);}
|
||||
public static void Idx_create(Gfo_usr_dlg usr_dlg, Db_conn p, String file_id, Db_idx_itm... idxs) {
|
||||
int len = idxs.length;
|
||||
p.Txn_mgr().Txn_end_all(); // commit any pending transactions
|
||||
for (int i = 0; i < len; i++) {
|
||||
@@ -65,18 +65,18 @@ public class Sqlite_engine_ {
|
||||
usr_dlg.Log_many("", "", "index created: ~{0} ~{1}", file_id, index);
|
||||
}
|
||||
}
|
||||
public static Db_provider Provider_load_or_fail_(Io_url url) {
|
||||
public static Db_conn Conn_load_or_fail_(Io_url url) {
|
||||
boolean exists = Io_mgr._.ExistsFil(url);
|
||||
if (!exists) throw Err_.new_fmt_("db does not exist; url=~{0}", url.Raw());
|
||||
Db_conn_info connect = Db_conn_info__sqlite.load_(url);
|
||||
return Db_provider_.new_and_open_(connect);
|
||||
Db_url connect = Db_url__sqlite.load_(url);
|
||||
return Db_conn_.new_and_open_(connect);
|
||||
}
|
||||
public static Db_provider Provider_load_or_make_(Io_url url) {return Provider_load_or_make_(url, Bool_obj_ref.n_());}
|
||||
public static Db_provider Provider_load_or_make_(Io_url url, Bool_obj_ref created) {
|
||||
public static Db_conn Conn_load_or_make_(Io_url url) {return Conn_load_or_make_(url, Bool_obj_ref.n_());}
|
||||
public static Db_conn Conn_load_or_make_(Io_url url, Bool_obj_ref created) {
|
||||
boolean exists = Io_mgr._.ExistsFil(url);
|
||||
created.Val_(!exists);
|
||||
Db_conn_info connect = exists ? Db_conn_info__sqlite.load_(url) : Db_conn_info__sqlite.make_(url);
|
||||
Db_provider p = Db_provider_.new_and_open_(connect);
|
||||
Db_url connect = exists ? Db_url__sqlite.load_(url) : Db_url__sqlite.make_(url);
|
||||
Db_conn p = Db_conn_.new_and_open_(connect);
|
||||
if (!exists)
|
||||
Pragma_page_size(p, 4096);
|
||||
return p;
|
||||
|
||||
@@ -20,24 +20,24 @@ import org.junit.*;
|
||||
public class db_CrudOps_tst {
|
||||
CrudOpsFxt fx = new CrudOpsFxt();
|
||||
@Test public void Mysql() {if (Tfds.SkipDb) return;
|
||||
fx.RunAll(Db_provider_fxt.Mysql());
|
||||
fx.RunAll(Db_conn_fxt.Mysql());
|
||||
}
|
||||
@Test public void Tdb() {if (Tfds.SkipDb) return;
|
||||
fx.RunAll(Db_provider_fxt.Tdb("100_dbs_crud_ops.dsv"));
|
||||
fx.RunAll(Db_conn_fxt.Tdb("100_dbs_crud_ops.dsv"));
|
||||
}
|
||||
@Test public void Postgres() {if (Db_provider_fxt.SkipPostgres) return;
|
||||
fx.RunAll(Db_provider_fxt.Postgres());
|
||||
@Test public void Postgres() {if (Db_conn_fxt.SkipPostgres) return;
|
||||
fx.RunAll(Db_conn_fxt.Postgres());
|
||||
}
|
||||
@Test public void Sqlite() {if (Tfds.SkipDb) return;
|
||||
fx.Fx().DmlAffectedAvailable_(false);
|
||||
fx.RunAll(Db_provider_fxt.Sqlite());
|
||||
fx.RunAll(Db_conn_fxt.Sqlite());
|
||||
}
|
||||
}
|
||||
class CrudOpsFxt {
|
||||
public Db_provider_fxt Fx() {return fx;} Db_provider_fxt fx = new Db_provider_fxt();
|
||||
public Db_conn_fxt Fx() {return fx;} Db_conn_fxt fx = new Db_conn_fxt();
|
||||
void Init() {fx.ini_DeleteAll("dbs_crud_ops");}
|
||||
public void RunAll(Db_provider provider) {
|
||||
fx.Provider_(provider);
|
||||
public void RunAll(Db_conn conn) {
|
||||
fx.Conn_(conn);
|
||||
Insert_hook();
|
||||
UpdateOne_hook();
|
||||
UpdateMany_hook();
|
||||
@@ -125,5 +125,5 @@ class CrudOpsFxt {
|
||||
Tfds.Eq(val, ExecRdr_val(Db_qry_.select_val_("dbs_crud_ops", "name", Db_crt_.eq_("id", 3))));
|
||||
Tfds.Eq(val, ExecRdr_val(Db_qry_.select_val_("dbs_crud_ops", "name", Db_crt_.eq_("name", "\\"))));
|
||||
}
|
||||
String ExecRdr_val(Db_qry_select select) {return (String)select.ExecRdr_val(fx.Provider());}
|
||||
String ExecRdr_val(Db_qry_select select) {return (String)select.ExecRdr_val(fx.Conn());}
|
||||
}
|
||||
|
||||
@@ -21,19 +21,19 @@ public class db_DataTypes_tst {
|
||||
DataTypes_base_fxt fx = new DataTypes_base_fxt();
|
||||
@Test public void Mysql() {if (Tfds.SkipDb) return;
|
||||
fx.Select_FloatStr_("0.333333");
|
||||
fx.RunAll(Db_provider_fxt.Mysql());
|
||||
fx.RunAll(Db_conn_fxt.Mysql());
|
||||
}
|
||||
@Test public void Tdb() {if (Tfds.SkipDb) return;
|
||||
fx.Select_FloatStr_(Float_.Xto_str(Float_.Div(1, 3)));
|
||||
fx.RunAll(Db_provider_fxt.Tdb("110_dbs_multiple_data_types.dsv"));
|
||||
fx.RunAll(Db_conn_fxt.Tdb("110_dbs_multiple_data_types.dsv"));
|
||||
}
|
||||
@Test public void Postgres() {if (Db_provider_fxt.SkipPostgres) return;
|
||||
@Test public void Postgres() {if (Db_conn_fxt.SkipPostgres) return;
|
||||
fx.Select_FloatStr_("0.33333334");
|
||||
fx.RunAll(Db_provider_fxt.Postgres());
|
||||
fx.RunAll(Db_conn_fxt.Postgres());
|
||||
}
|
||||
@Test public void Sqlite() {if (Tfds.SkipDb) return;
|
||||
fx.Select_FloatStr_("0.33333334");
|
||||
fx.RunAll(Db_provider_fxt.Sqlite());
|
||||
fx.RunAll(Db_conn_fxt.Sqlite());
|
||||
}
|
||||
/*
|
||||
DROP TABLE dbs_multiple_data_types;
|
||||
@@ -49,17 +49,17 @@ INSERT INTO dbs_multiple_data_types VALUES (1, 'John Doe', B'1', '3/30/2006 10:2
|
||||
*/
|
||||
}
|
||||
class DataTypes_base_fxt {
|
||||
public Db_provider Provider() {return provider;} Db_provider provider;
|
||||
public Db_conn Conn() {return conn;} Db_conn conn;
|
||||
public DataTypes_base_fxt() {}
|
||||
public void Rls() {provider.Conn_term();}
|
||||
public void Rls() {conn.Conn_term();}
|
||||
public String Select_FloatStr() {return select_FloatStr;} public DataTypes_base_fxt Select_FloatStr_(String v) {select_FloatStr = v; return this;} private String select_FloatStr;
|
||||
public void RunAll(Db_provider provider) {
|
||||
this.provider = provider;
|
||||
public void RunAll(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
this.Select_hook(select_FloatStr);
|
||||
provider.Conn_term();
|
||||
conn.Conn_term();
|
||||
}
|
||||
public void Select_hook(String floatStr) {
|
||||
DataRdr rdr = Db_qry_.select_tbl_("dbs_multiple_data_types").Exec_qry_as_rdr(provider);
|
||||
DataRdr rdr = Db_qry_.select_tbl_("dbs_multiple_data_types").Exec_qry_as_rdr(conn);
|
||||
|
||||
rdr.MoveNextPeer();
|
||||
Tfds.Eq(rdr.ReadInt("unique_id"), 1);
|
||||
@@ -70,9 +70,9 @@ class DataTypes_base_fxt {
|
||||
Tfds.Eq_decimal(rdr.ReadDecimal("amount"), DecimalAdp_.parts_(12, 345));
|
||||
}
|
||||
public void UpdateDate_hook() {
|
||||
provider.Exec_qry(Db_qry_.update_("dbs_multiple_data_types", Db_crt_.eq_("unique_id", 1)).Arg_obj_("last_update", DateAdpClassXtn._.XtoDb(DateAdp_.parse_gplx("20091115 220000.000"))));
|
||||
conn.Exec_qry(Db_qry_.update_("dbs_multiple_data_types", Db_crt_.eq_("unique_id", 1)).Arg_obj_("last_update", DateAdpClassXtn._.XtoDb(DateAdp_.parse_gplx("20091115 220000.000"))));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_tbl_("dbs_multiple_data_types").Exec_qry_as_rdr(provider);
|
||||
DataRdr rdr = Db_qry_.select_tbl_("dbs_multiple_data_types").Exec_qry_as_rdr(conn);
|
||||
rdr.MoveNextPeer();
|
||||
Tfds.Eq_date(rdr.ReadDate("last_update"), DateAdp_.parse_gplx("20091115 220000.000"));
|
||||
}
|
||||
|
||||
@@ -19,14 +19,14 @@ package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class TdbConnectInfo_tst {
|
||||
@Test public void Full() {
|
||||
Db_conn_info connectInfo = Db_conn_info_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb;format=dsv;");
|
||||
Db_url connectInfo = Db_url_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb;format=dsv;");
|
||||
tst_Parse(connectInfo, Io_url_.new_any_("C:\\dir\\xmpl.tdb"), "dsv");
|
||||
}
|
||||
@Test public void DefaultFormat() {
|
||||
Db_conn_info connectInfo = Db_conn_info_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb"); // dsv Format inferred
|
||||
Db_url connectInfo = Db_url_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb"); // dsv Format inferred
|
||||
tst_Parse(connectInfo, Io_url_.new_any_("C:\\dir\\xmpl.tdb"), "dsv");
|
||||
}
|
||||
void tst_Parse(Db_conn_info connectInfo, Io_url url, String format) {
|
||||
Tfds.Eq(((Db_conn_info__tdb)connectInfo).Url(), url);
|
||||
void tst_Parse(Db_url connectInfo, Io_url url, String format) {
|
||||
Tfds.Eq(((Db_url__tdb)connectInfo).Url(), url);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,5 +43,5 @@ public class TdbDatabase {
|
||||
}
|
||||
int FileId_next = TdbFile.MainFileId + 1;
|
||||
int TableId_next = 1;
|
||||
// public static Io_url UrlOf(Db_conn_info url) {return Io_url_.new_any_(url.ServerName());}
|
||||
// public static Io_url UrlOf(Db_url url) {return Io_url_.new_any_(url.ServerName());}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import gplx.lists.*; /*GfoNde*/
|
||||
class TdbDeleteWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_delete cmd = (Db_qry_delete)cmdObj;
|
||||
TdbTable tbl = engine.FetchTbl(cmd.BaseTable());
|
||||
TdbTable tbl = engine.FetchTbl(cmd.Base_table());
|
||||
ListAdp deleted = ListAdp_.new_();
|
||||
int rv = 0;
|
||||
if (cmd.Where() == Db_qry_.WhereAll) {
|
||||
@@ -29,7 +29,7 @@ class TdbDeleteWkr implements Db_qryWkr {
|
||||
tbl.Rows().Clear();
|
||||
}
|
||||
else {
|
||||
Criteria crt = cmd.Where().Crt();
|
||||
Criteria crt = cmd.Where();
|
||||
for (int i = 0; i < tbl.Rows().Count(); i++) {
|
||||
GfoNde row = tbl.Rows().FetchAt_asGfoNde(i);
|
||||
if (crt.Matches(row))
|
||||
|
||||
@@ -16,31 +16,32 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
class TdbEngine implements Db_engine {
|
||||
public String Key() {return KeyDef;} public static final String KeyDef = "tdb";
|
||||
public String Conn_info_tid() {return this.Key();}
|
||||
public Db_conn_info Conn_info() {return conn_info;} Db_conn_info conn_info;
|
||||
public String Tid() {return Db_url__tdb.Tid_const;}
|
||||
public Db_url Url() {return url;} private Db_url url;
|
||||
public TdbDatabase Db() {return db;} TdbDatabase db;
|
||||
public void Conn_open() {
|
||||
String urlStr = (String)conn_info.Server();
|
||||
db = loadMgr.LoadTbls(Io_url_.new_any_(urlStr));
|
||||
Db_url__tdb tdb_url = (Db_url__tdb)url;
|
||||
String url_str = tdb_url.Server();
|
||||
db = loadMgr.LoadTbls(Io_url_.new_any_(url_str));
|
||||
}
|
||||
public void Conn_term() {}
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public Db_engine Make_new(Db_conn_info conn_info) {
|
||||
public Db_engine New_clone(Db_url url) {
|
||||
TdbEngine rv = new TdbEngine();
|
||||
rv.CtorTdbEngine(conn_info);
|
||||
rv.CtorTdbEngine(url);
|
||||
return rv;
|
||||
}
|
||||
public Object Execute(Db_qry cmd) {
|
||||
Db_qryWkr wkr = (Db_qryWkr)wkrs.FetchOrFail(cmd.KeyOfDb_qry());
|
||||
return wkr.Exec(this, cmd);
|
||||
public Object Exec_as_obj(Db_qry qry) {
|
||||
Db_qryWkr wkr = (Db_qryWkr)wkrs.FetchOrFail(qry.Tid());
|
||||
return wkr.Exec(this, qry);
|
||||
}
|
||||
public DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public Db_stmt New_db_stmt(Db_provider provider, Db_qry qry) {return new Db_stmt_sql().Parse(Sql_cmd_wtr_.Ansi.XtoSqlQry(qry, true));}
|
||||
public Object New_db_cmd(String sql) {throw Err_.not_implemented_();}
|
||||
public Db_rdr New_db_rdr(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt_sql().Parse(qry, Sql_qry_wtr_.I.Xto_str(qry, true));}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public Db_rdr New_rdr_by_obj(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public TdbTable FetchTbl(String name) {
|
||||
TdbTable tbl = db.Tables().FetchOrFail(name);
|
||||
if (!tbl.IsLoaded()) loadMgr.LoadTbl(db, tbl);
|
||||
@@ -55,15 +56,23 @@ class TdbEngine implements Db_engine {
|
||||
|
||||
HashAdp wkrs = HashAdp_.new_(); TdbDbLoadMgr loadMgr = TdbDbLoadMgr.new_(); TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_();
|
||||
@gplx.Internal protected static final TdbEngine _ = new TdbEngine();
|
||||
//TdbEngine() {this.conn_info = TdbConnectUrl._;}
|
||||
void CtorTdbEngine(Db_conn_info conn_info) {
|
||||
this.conn_info = conn_info;
|
||||
wkrs.Add(Db_qry_select.KeyConst, TdbSelectWkr._);
|
||||
wkrs.Add(Db_qry_insert.KeyConst, TdbInsertWkr.new_());
|
||||
wkrs.Add(Db_qry_update.KeyConst, TdbUpdateWkr.new_());
|
||||
wkrs.Add(Db_qry_delete.KeyConst, TdbDeleteWkr.new_());
|
||||
wkrs.Add(Db_qry_flush.KeyConst, TdbFlushWkr.new_());
|
||||
void CtorTdbEngine(Db_url url) {
|
||||
this.url = url;
|
||||
wkrs.Add(Db_qry_.Tid_select, TdbSelectWkr._);
|
||||
wkrs.Add(Db_qry_.Tid_insert, TdbInsertWkr.new_());
|
||||
wkrs.Add(Db_qry_.Tid_update, TdbUpdateWkr.new_());
|
||||
wkrs.Add(Db_qry_.Tid_delete, TdbDeleteWkr.new_());
|
||||
wkrs.Add(Db_qry_.Tid_flush, TdbFlushWkr.new_());
|
||||
}
|
||||
public static TdbEngine as_(Object obj) {return obj instanceof TdbEngine ? (TdbEngine)obj : null;}
|
||||
public static TdbEngine cast_(Object obj) {try {return (TdbEngine)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, TdbEngine.class, obj);}}
|
||||
}
|
||||
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;}
|
||||
}
|
||||
|
||||
@@ -81,8 +81,8 @@ public class TdbFlush_tst {
|
||||
}
|
||||
class TdbEngineFxt {
|
||||
public TdbEngine run_MakeEngine(Io_url url) {
|
||||
Db_conn_info connectInfo = Db_conn_info_.tdb_(url);
|
||||
TdbEngine engine = (TdbEngine)TdbEngine._.Make_new(connectInfo);
|
||||
Db_url connectInfo = Db_url_.tdb_(url);
|
||||
TdbEngine engine = (TdbEngine)TdbEngine._.New_clone(connectInfo);
|
||||
engine.Conn_open();
|
||||
return engine;
|
||||
}
|
||||
@@ -93,9 +93,9 @@ class TdbEngineFxt {
|
||||
return rv;
|
||||
}
|
||||
public void run_InsertRow(TdbEngine engine, String tblName, int idVal) {
|
||||
Db_qry_insert cmd = Db_qry_insert.new_().BaseTable_(tblName);
|
||||
Db_qry_insert cmd = new Db_qry_insert(tblName);
|
||||
cmd.Arg_("id", idVal);
|
||||
engine.Execute(cmd);
|
||||
engine.Exec_as_obj(cmd);
|
||||
}
|
||||
|
||||
public void tst_FilesCount(TdbEngine engine, int count) {Tfds.Eq(engine.Db().Files().Count(), count);}
|
||||
|
||||
@@ -16,12 +16,12 @@ 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.lists.*; /*GfoNde*/
|
||||
import gplx.lists.*; import gplx.dbs.sqls.*;
|
||||
class TdbInsertWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_insert cmd = (Db_qry_insert)cmdObj;
|
||||
|
||||
TdbTable tbl = engine.FetchTbl(cmd.BaseTable());
|
||||
TdbTable tbl = engine.FetchTbl(cmd.Base_table());
|
||||
tbl.IsDirty_set(true);
|
||||
return cmd.Select() == null
|
||||
? InsertRowsByVals(engine, tbl, cmd)
|
||||
|
||||
@@ -16,12 +16,12 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.criterias.*; import gplx.dbs.sqls.*;
|
||||
import gplx.lists.*; /*ComparerAble*/ import gplx.stores.*; /*GfoNdeRdr*/
|
||||
class TdbSelectWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_select cmd = (Db_qry_select)cmdObj;
|
||||
if (cmd.From().Tbls().Count() > 1) throw Err_.new_key_("gplx.tdbs", "joins not supported for tdbs").Add("sql", cmd.XtoSql());
|
||||
if (cmd.From().Tbls().Count() > 1) throw Err_.new_key_("gplx.tdbs", "joins not supported for tdbs").Add("sql", cmd.Xto_sql());
|
||||
|
||||
TdbTable tbl = engine.FetchTbl(cmd.From().BaseTable().TblName());
|
||||
GfoNdeList rv = (cmd.Where() == Db_qry_.WhereAll && cmd.Limit() == Db_qry_select.Limit_disabled) ? rv = tbl.Rows() : FilterRecords(tbl, cmd.Where().Crt(), cmd.Limit());
|
||||
|
||||
@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.stores; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.dbs.*; /*Db_conn_info*/
|
||||
import gplx.dbs.*; /*Db_url*/
|
||||
public class DbMaprMgr_tst {
|
||||
@Before public void setup() {
|
||||
mgr = DbMaprMgr.new_().RootIndexFlds_(DbMaprArg.new_("id", "disc_id"))
|
||||
@@ -37,42 +37,42 @@ public class DbMaprMgr_tst {
|
||||
. Flds_add(MockStream.id_idk, "stream_id").Flds_add(MockStream.name_idk, "stream_name")
|
||||
. ConstantFlds_add("stream_type", 1)
|
||||
)));
|
||||
wtr = DbMaprWtr.new_by_url_(Db_conn_info_.Test);
|
||||
wtr = DbMaprWtr.new_by_url_(Db_url_.Test);
|
||||
wtr.EnvVars().Add(DbMaprWtr.Key_Mgr, mgr);
|
||||
provider = Db_provider_pool._.Get_or_new(Db_conn_info_.Test);
|
||||
Db_qry_fxt.DeleteAll(provider, "mock_discs", "mock_titles", "mock_chapters", "mock_streams");
|
||||
} DbMaprMgr mgr; DbMaprWtr wtr; Db_provider provider; MockDisc disc; MockTitle title; MockChapter chapter; MockStream audio, subtitle; SrlMgr rdr;
|
||||
conn = Db_conn_pool_old._.Get_or_new(Db_url_.Test);
|
||||
Db_qry_fxt.DeleteAll(conn, "mock_discs", "mock_titles", "mock_chapters", "mock_streams");
|
||||
} DbMaprMgr mgr; DbMaprWtr wtr; Db_conn conn; MockDisc disc; MockTitle title; MockChapter chapter; MockStream audio, subtitle; SrlMgr rdr;
|
||||
@Test public void PurgeObjTree() {
|
||||
disc = MockDisc.new_().Id_(1);
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1));
|
||||
DbMaprWtrUtl.PurgeObjTree(disc, mgr, provider);
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(provider, "mock_discs"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1));
|
||||
DbMaprWtrUtl.PurgeObjTree(disc, mgr, conn);
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(conn, "mock_discs"));
|
||||
}
|
||||
@Test public void PurgeObjTree_deep() {
|
||||
disc = MockDisc.new_().Id_(1);
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_chapters", KeyValList.args_("disc_id", 1).Add("title_id", 2).Add("chapter_id", 3));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_chapters", KeyValList.args_("disc_id", 2).Add("title_id", 2).Add("chapter_id", 3));
|
||||
DbMaprWtrUtl.PurgeObjTree(disc, mgr, provider);
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", KeyValList.args_("disc_id", 1).Add("title_id", 2).Add("chapter_id", 3));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", KeyValList.args_("disc_id", 2).Add("title_id", 2).Add("chapter_id", 3));
|
||||
DbMaprWtrUtl.PurgeObjTree(disc, mgr, conn);
|
||||
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(provider, "mock_discs"));
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(provider, "mock_titles"));
|
||||
Tfds.Eq(1, Db_qry_fxt.SelectAll_count(provider, "mock_chapters")); // ignore chapter with disc_id=2
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(conn, "mock_discs"));
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(conn, "mock_titles"));
|
||||
Tfds.Eq(1, Db_qry_fxt.SelectAll_count(conn, "mock_chapters")); // ignore chapter with disc_id=2
|
||||
}
|
||||
@Test public void Save_root() {
|
||||
disc = MockDisc.new_().Id_(1).Name_("disc");
|
||||
|
||||
wtr.StoreRoot(disc, "mock_discs");
|
||||
Db_qry_fxt.tst_Select(provider, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
Db_qry_fxt.tst_Select(conn, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
}
|
||||
@Test public void Save_subs() {
|
||||
disc = MockDisc.new_().Id_(1).Name_("disc");
|
||||
title = MockTitle.new_().Id_(2).Name_("title").Disc_(disc);
|
||||
|
||||
wtr.StoreRoot(disc, "mock_discs");
|
||||
Db_qry_fxt.tst_Select(provider, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
Db_qry_fxt.tst_Select(provider, "mock_titles", DbTstRow.vals_only_(1, 2, "title"));
|
||||
Db_qry_fxt.tst_Select(conn, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
Db_qry_fxt.tst_Select(conn, "mock_titles", DbTstRow.vals_only_(1, 2, "title"));
|
||||
}
|
||||
@Test public void Save_deep() {
|
||||
disc = MockDisc.new_().Id_(1).Name_("disc");
|
||||
@@ -82,17 +82,17 @@ public class DbMaprMgr_tst {
|
||||
subtitle = MockStream.new_().Id_(5).Name_("subtitle").Title_(title.Subtitles());
|
||||
|
||||
wtr.StoreRoot(disc, "mock_discs");
|
||||
Db_qry_fxt.tst_Select(provider, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
Db_qry_fxt.tst_Select(provider, "mock_titles", DbTstRow.vals_only_(1, 2, "title"));
|
||||
Db_qry_fxt.tst_Select(provider, "mock_chapters", DbTstRow.vals_only_(1, 2, 3, "chap"));
|
||||
Db_qry_fxt.tst_Select(provider, "mock_streams"
|
||||
Db_qry_fxt.tst_Select(conn, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
Db_qry_fxt.tst_Select(conn, "mock_titles", DbTstRow.vals_only_(1, 2, "title"));
|
||||
Db_qry_fxt.tst_Select(conn, "mock_chapters", DbTstRow.vals_only_(1, 2, 3, "chap"));
|
||||
Db_qry_fxt.tst_Select(conn, "mock_streams"
|
||||
, DbTstRow.vals_only_(1, 2, null, 4, "audio")
|
||||
, DbTstRow.vals_only_(1, 2, null, 5, "subtitle")
|
||||
);
|
||||
}
|
||||
@Test public void Load_root() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc._, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
@@ -101,9 +101,9 @@ public class DbMaprMgr_tst {
|
||||
}
|
||||
@Test public void Load_subs() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 2).Add("title_name", "title2"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 2).Add("title_name", "title2"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc._, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
@@ -114,11 +114,11 @@ public class DbMaprMgr_tst {
|
||||
}
|
||||
@Test public void Load_deep() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_chapters", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("chapter_id", 3).Add("chapter_name", "chapter1"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_streams", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("stream_id", 4).Add("stream_type", 0).Add("stream_name", "audio1"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_streams", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("stream_id", 5).Add("stream_type", 1).Add("stream_name", "subtitle1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("chapter_id", 3).Add("chapter_name", "chapter1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_streams", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("stream_id", 4).Add("stream_type", 0).Add("stream_name", "audio1"));
|
||||
Db_qry_fxt.Insert_kvo(conn, "mock_streams", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("stream_id", 5).Add("stream_type", 1).Add("stream_name", "subtitle1"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc._, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
@@ -133,7 +133,7 @@ public class DbMaprMgr_tst {
|
||||
Tfds.Eq("subtitle1", ((MockStream)t.Subtitles().FetchAt(0)).Name());
|
||||
}
|
||||
DbMaprRdr rdr_() {
|
||||
DbMaprRdr rv = DbMaprRdr.new_(Db_conn_info_.Test, Db_crt_.eq_("disc_id", 1));
|
||||
DbMaprRdr rv = DbMaprRdr.new_(Db_url_.Test, Db_crt_.eq_("disc_id", 1));
|
||||
rv.EnvVars().Add(DbMaprWtr.Key_Mgr, mgr);
|
||||
return rv;
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ public class DbMaprRdr extends DataRdr_base implements SrlMgr {
|
||||
if (tblByRootCrt == null) {
|
||||
DataRdr dbRdr = null;
|
||||
try {
|
||||
dbRdr = Db_qry_.select_().From_(mapr.TableName()).Where_(rootCrt).Exec_qry_as_rdr(provider);
|
||||
dbRdr = Db_qry_.select_().From_(mapr.TableName()).Where_(rootCrt).Exec_qry_as_rdr(conn);
|
||||
tblByRootCrt = GfoNde_.rdr_(dbRdr);
|
||||
}
|
||||
finally {dbRdr.Rls();}
|
||||
@@ -112,11 +112,11 @@ public class DbMaprRdr extends DataRdr_base implements SrlMgr {
|
||||
@Override public KeyVal KeyValAt(int i) {throw Err_.not_implemented_();}
|
||||
@Override public SrlMgr SrlMgr_new(Object o) {return new DbMaprRdr();}
|
||||
HashAdp tables = HashAdp_.new_();
|
||||
Db_provider provider; Criteria rootCrt;
|
||||
Db_conn conn; Criteria rootCrt;
|
||||
DbMaprMgr mgr; ListAdp rowStack = ListAdp_.new_();
|
||||
public static DbMaprRdr new_(Db_conn_info dbInfo, Criteria rootCrt) {
|
||||
public static DbMaprRdr new_(Db_url dbInfo, Criteria rootCrt) {
|
||||
DbMaprRdr rv = new DbMaprRdr();
|
||||
rv.provider = Db_provider_pool._.Get_or_new(dbInfo); rv.rootCrt = rootCrt;
|
||||
rv.conn = Db_conn_pool_old._.Get_or_new(dbInfo); rv.rootCrt = rootCrt;
|
||||
return rv;
|
||||
} DbMaprRdr() {}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class DbMaprWtr extends DataWtr_base implements DataWtr {
|
||||
public void InitWtr(String key, Object val) {}
|
||||
@Override public Object StoreRoot(SrlObj root, String key) {
|
||||
mgr = (DbMaprMgr)this.EnvVars().FetchOrFail(DbMaprWtr.Key_Mgr);
|
||||
DbMaprWtrUtl.PurgeObjTree(root, mgr, provider);
|
||||
DbMaprWtrUtl.PurgeObjTree(root, mgr, conn);
|
||||
WriteGfoObj(root, mgr.Root());
|
||||
mgr.Clear();
|
||||
return null;
|
||||
@@ -56,7 +56,7 @@ public class DbMaprWtr extends DataWtr_base implements DataWtr {
|
||||
}
|
||||
}
|
||||
@Override public void WriteNodeBgn(String v) {
|
||||
if (insertCmd != null) insertCmd.Exec_qry(provider); // occurs for nodes; ex: new title starts; commit changes for own disc
|
||||
if (insertCmd != null) insertCmd.Exec_qry(conn); // occurs for nodes; ex: new title starts; commit changes for own disc
|
||||
curTableName = v;
|
||||
insertCmd = null;
|
||||
}
|
||||
@@ -73,7 +73,7 @@ public class DbMaprWtr extends DataWtr_base implements DataWtr {
|
||||
insertCmd.Arg_obj_(fld, val);
|
||||
}
|
||||
@Override public void WriteNodeEnd() {
|
||||
if (insertCmd != null) insertCmd.Exec_qry(provider); // occurs for nodes and leaves; for nodes, insertCmd will be null (committed by last leaf)
|
||||
if (insertCmd != null) insertCmd.Exec_qry(conn); // occurs for nodes and leaves; for nodes, insertCmd will be null (committed by last leaf)
|
||||
insertCmd = null;
|
||||
}
|
||||
public void WriteTableBgn(String name, GfoFldList fields) {}
|
||||
@@ -82,18 +82,18 @@ public class DbMaprWtr extends DataWtr_base implements DataWtr {
|
||||
public void Clear() {}
|
||||
public String XtoStr() {return "";}
|
||||
@Override public SrlMgr SrlMgr_new(Object o) {return new DbMaprWtr();}
|
||||
DbMaprMgr mgr; Db_provider provider; String curTableName; Db_qry_insert insertCmd;
|
||||
public static DbMaprWtr new_by_url_(Db_conn_info url) {
|
||||
DbMaprMgr mgr; Db_conn conn; String curTableName; Db_qry_insert insertCmd;
|
||||
public static DbMaprWtr new_by_url_(Db_url url) {
|
||||
DbMaprWtr rv = new DbMaprWtr();
|
||||
rv.provider = Db_provider_pool._.Get_or_new(url);
|
||||
rv.conn = Db_conn_pool_old._.Get_or_new(url);
|
||||
return rv;
|
||||
} DbMaprWtr() {}
|
||||
public static final String Key_Mgr = "DbMapr.mgr";
|
||||
}
|
||||
class DbMaprWtrUtl {
|
||||
public static void PurgeObjTree(SrlObj root, DbMaprMgr mgr, Db_provider provider) {
|
||||
public static void PurgeObjTree(SrlObj root, DbMaprMgr mgr, Db_conn conn) {
|
||||
Criteria crt = MakeCriteria(root, mgr.RootIndexFlds());
|
||||
PurgeObj(mgr.Root(), crt, provider);
|
||||
PurgeObj(mgr.Root(), crt, conn);
|
||||
}
|
||||
static Criteria MakeCriteria(SrlObj root, DbMaprArg[] objRootIdxFlds) {
|
||||
Criteria rv = null;
|
||||
@@ -104,11 +104,11 @@ class DbMaprWtrUtl {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
static void PurgeObj(DbMaprItm mapr, Criteria crt, Db_provider provider) {
|
||||
Db_qry_.delete_(mapr.TableName(), crt).Exec_qry(provider);
|
||||
static void PurgeObj(DbMaprItm mapr, Criteria crt, Db_conn conn) {
|
||||
Db_qry_.delete_(mapr.TableName(), crt).Exec_qry(conn);
|
||||
for (Object subObj : mapr.Subs()) {
|
||||
DbMaprItm sub = (DbMaprItm)subObj;
|
||||
PurgeObj(sub, crt, provider);
|
||||
PurgeObj(sub, crt, conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class AnsiSqlWtr_tst {
|
||||
Sql_cmd_wtr sqlWtr = Sql_cmd_wtr_ansi_.default_();
|
||||
Sql_qry_wtr sqlWtr = Sql_qry_wtr_.new_ansi();
|
||||
@Test public void Insert() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.insert_("people").Arg_("id", 1).Arg_("name", "me")
|
||||
@@ -95,5 +95,5 @@ public class AnsiSqlWtr_tst {
|
||||
);
|
||||
}
|
||||
void tst_XtoSql(Db_qry cmd, String expd) {tst_XtoSql(cmd, expd, false);}
|
||||
void tst_XtoSql(Db_qry cmd, String expd, boolean prepare) {Tfds.Eq(expd, sqlWtr.XtoSqlQry(cmd, prepare));}
|
||||
void tst_XtoSql(Db_qry cmd, String expd, boolean prepare) {Tfds.Eq(expd, sqlWtr.Xto_str(cmd, prepare));}
|
||||
}
|
||||
|
||||
54
140_dbs/tst/gplx/dbs/Db_conn_fxt.java
Normal file
54
140_dbs/tst/gplx/dbs/Db_conn_fxt.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_conn_fxt implements RlsAble {
|
||||
public Db_conn Conn() {return conn;} public Db_conn_fxt Conn_(Db_conn v) {conn = v; return this;} Db_conn conn;
|
||||
public void DmlAffectedAvailable_(boolean v) {dmlAffectedAvailable = v;} private boolean dmlAffectedAvailable = true;
|
||||
public void ini_DeleteAll(String tbl) {conn.Exec_qry(Db_qry_.delete_tbl_(tbl));}
|
||||
public void tst_ExecDml(int expd, Db_qry qry) {
|
||||
int actl = conn.Exec_qry(qry);
|
||||
if (dmlAffectedAvailable)
|
||||
Tfds.Eq(expd, actl, "Exec_qry failed: sql={0}", qry.Xto_sql());
|
||||
}
|
||||
public void tst_ExecRdrTbl(int expd, String tblName) {tst_ExecRdr(expd, Db_qry_.select_tbl_(tblName));}
|
||||
public void tst_ExecRdr(int expd, Db_qry qry) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = conn.Exec_qry_as_rdr(qry);
|
||||
tbl = GfoNde_.rdr_(rdr);
|
||||
}
|
||||
catch (Exception e) {Err_.Noop(e); rdr.Rls();}
|
||||
Tfds.Eq(expd, tbl.Subs().Count(), "Exec_qry_as_rdr failed: sql={0}", qry.Xto_sql());
|
||||
} GfoNde tbl;
|
||||
public GfoNde tst_RowAry(int index, Object... expdValAry) {
|
||||
GfoNde record = tbl.Subs().FetchAt_asGfoNde(index);
|
||||
Object[] actlValAry = new Object[expdValAry.length];
|
||||
for (int i = 0; i < actlValAry.length; i++)
|
||||
actlValAry[i] = record.ReadAt(i);
|
||||
Tfds.Eq_ary(actlValAry, expdValAry);
|
||||
return record;
|
||||
}
|
||||
public void Rls() {conn.Conn_term();}
|
||||
|
||||
public static Db_conn Mysql() {return Db_conn_.new_and_open_(Db_url__mysql.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
|
||||
public static Db_conn Tdb(String fileName) {return Db_conn_.new_and_open_(Db_url_.tdb_(Tfds.RscDir.GenSubDir_nest("140_dbs", "tdbs").GenSubFil(fileName)));}
|
||||
public static Db_conn Postgres() {return Db_conn_.new_and_open_(Db_url__postgres.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
|
||||
public static Db_conn Sqlite() {return Db_conn_.new_and_open_(Db_url__sqlite.load_(Tfds.RscDir.GenSubFil_nest("140_dbs", "sqlite", "unit_tests.db")));}
|
||||
// public static Db_conn Mssql() {return MssqlConnectUrl.WindowsAuth(".", "unit_tests");
|
||||
public static final boolean SkipPostgres = Tfds.SkipDb || true;
|
||||
}
|
||||
@@ -17,27 +17,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_qry_fxt {
|
||||
public static void Insert_kvo(Db_provider provider, String tblName, KeyValList kvList) {
|
||||
public static void Insert_kvo(Db_conn conn, String tblName, KeyValList kvList) {
|
||||
Db_qry_insert qry = Db_qry_.insert_(tblName);
|
||||
for (int i = 0; i < kvList.Count(); i++) {
|
||||
KeyVal kv = kvList.GetAt(i);
|
||||
qry.Arg_obj_(kv.Key(), kv.Val());
|
||||
}
|
||||
qry.Exec_qry(provider);
|
||||
qry.Exec_qry(conn);
|
||||
}
|
||||
public static GfoNde SelectAll(Db_provider provider, String tblName) {
|
||||
return Db_qry_.select_tbl_(tblName).ExecRdr_nde(provider);
|
||||
public static GfoNde SelectAll(Db_conn conn, String tblName) {
|
||||
return Db_qry_.select_tbl_(tblName).ExecRdr_nde(conn);
|
||||
}
|
||||
public static int SelectAll_count(Db_provider provider, String tblName) {
|
||||
GfoNde nde = Db_qry_fxt.SelectAll(provider, tblName);
|
||||
public static int SelectAll_count(Db_conn conn, String tblName) {
|
||||
GfoNde nde = Db_qry_fxt.SelectAll(conn, tblName);
|
||||
return nde.Subs().Count();
|
||||
}
|
||||
public static void DeleteAll(Db_provider provider, String... ary) {
|
||||
public static void DeleteAll(Db_conn conn, String... ary) {
|
||||
for (String s : ary)
|
||||
Db_qry_.delete_tbl_(s).Exec_qry(provider);
|
||||
Db_qry_.delete_tbl_(s).Exec_qry(conn);
|
||||
}
|
||||
public static void tst_Select(Db_provider provider, String tblName, DbTstRow... expdAry) {
|
||||
GfoNde nde = Db_qry_fxt.SelectAll(provider, tblName);
|
||||
public static void tst_Select(Db_conn conn, String tblName, DbTstRow... expdAry) {
|
||||
GfoNde nde = Db_qry_fxt.SelectAll(conn, tblName);
|
||||
int len = Array_.Len(expdAry);
|
||||
for (int i = 0; i < len; i++) {
|
||||
DbTstRow expdRow = expdAry[i];
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*; /*Criteria_base*/
|
||||
import gplx.ios.*;
|
||||
import gplx.ios.*; import gplx.dbs.sqls.*;
|
||||
public class IoSqlCriteriaWriter_tst {
|
||||
@Test public void Type() {
|
||||
fld = IoItm_base_.Prop_Type;
|
||||
@@ -53,8 +53,8 @@ public class IoSqlCriteriaWriter_tst {
|
||||
String fld;
|
||||
void tst_Write(String expd, Criteria crt) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Sql_cmd_wtr whereWtr = Sql_cmd_wtr_ansi_.default_();
|
||||
whereWtr.BldWhere(sb, crt);
|
||||
Sql_qry_wtr_ansi whereWtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.new_ansi();
|
||||
whereWtr.Bld_where(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class PoolIds_tst {
|
||||
@Before public void setup() {
|
||||
provider = Db_provider_pool._.Get_or_new(Db_conn_info_.Test);
|
||||
Db_qry_fxt.DeleteAll(provider, PoolIds.Tbl_Name);
|
||||
conn = Db_conn_pool_old._.Get_or_new(Db_url_.Test);
|
||||
Db_qry_fxt.DeleteAll(conn, PoolIds.Tbl_Name);
|
||||
mgr = PoolIds._;
|
||||
}
|
||||
@Test public void FetchNextId() {
|
||||
@@ -45,12 +45,12 @@ public class PoolIds_tst {
|
||||
tst_Fetch("/test1", 1);
|
||||
}
|
||||
void run_Change(String url, int expd) {
|
||||
mgr.Commit(provider, url, expd);
|
||||
mgr.Commit(conn, url, expd);
|
||||
}
|
||||
void tst_Fetch(String url, int expd) {
|
||||
int actl = mgr.FetchNext(provider, url);
|
||||
int actl = mgr.FetchNext(conn, url);
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
Db_provider provider;
|
||||
Db_conn conn;
|
||||
PoolIds mgr;
|
||||
}
|
||||
|
||||
@@ -18,41 +18,41 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.groupBys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public abstract class GroupBys_base_tst {
|
||||
protected abstract Db_provider provider_();
|
||||
protected Db_provider provider;
|
||||
protected abstract Db_conn provider_();
|
||||
protected Db_conn conn;
|
||||
@Before public void setup() {
|
||||
provider = provider_();
|
||||
Db_qry_.delete_tbl_("dbs_group_bys").Exec_qry(provider);
|
||||
conn = provider_();
|
||||
Db_qry_.delete_tbl_("dbs_group_bys").Exec_qry(conn);
|
||||
}
|
||||
@After public void teardown() {
|
||||
provider.Conn_term();
|
||||
conn.Conn_term();
|
||||
}
|
||||
protected void GroupBy_1fld_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1")
|
||||
.GroupBy_("key1")
|
||||
.Exec_qry_as_rdr(provider);
|
||||
.Exec_qry_as_rdr(conn);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
}
|
||||
protected void GroupBy_2fld_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("key2", "b").Arg_("val_int", 1));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("key2", "b").Arg_("val_int", 2));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("key2", "b").Arg_("val_int", 1));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("key2", "b").Arg_("val_int", 2));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1", "key2")
|
||||
.GroupBy_("key1", "key2")
|
||||
.Exec_qry_as_rdr(provider);
|
||||
.Exec_qry_as_rdr(conn);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key2", "b");
|
||||
}
|
||||
protected void MinMax_hook(boolean min) {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
|
||||
Db_qry_select qry = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1")
|
||||
@@ -63,31 +63,31 @@ public abstract class GroupBys_base_tst {
|
||||
else
|
||||
qry.Cols_groupBy_max("val_int", "val_int_func");
|
||||
|
||||
DataRdr rdr = qry.Exec_qry_as_rdr(provider);
|
||||
DataRdr rdr = qry.Exec_qry_as_rdr(conn);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "val_int_func", expd);
|
||||
}
|
||||
protected void Count_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 10));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 20));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 10));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 20));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1").Cols_groupBy_count("val_int", "val_int_func")
|
||||
.GroupBy_("key1")
|
||||
.Exec_qry_as_rdr(provider);
|
||||
.Exec_qry_as_rdr(conn);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "val_int_func", 2);
|
||||
}
|
||||
protected void Sum_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 10));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 20));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 10));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 20));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1").Cols_groupBy_sum("val_int", "val_int_func")
|
||||
.GroupBy_("key1")
|
||||
.Exec_qry_as_rdr(provider);
|
||||
.Exec_qry_as_rdr(conn);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "val_int_func", 30);
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.groupBys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class GroupBys_mysql_tst extends GroupBys_base_tst {
|
||||
@Override protected Db_provider provider_() {return Db_provider_fxt.Mysql();}
|
||||
@Override protected Db_conn provider_() {return Db_conn_fxt.Mysql();}
|
||||
@Test public void GroupBy_1fld() {super.GroupBy_1fld_hook();}
|
||||
@Test public void GroupBy_2fld() {super.GroupBy_2fld_hook();}
|
||||
@Test public void Min() {super.MinMax_hook(true);}
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.groupBys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class GroupBys_tdb_tst extends GroupBys_base_tst {
|
||||
@Override protected Db_provider provider_() {return Db_provider_fxt.Tdb("130_dbs_group_bys.dsv");}
|
||||
@Override protected Db_conn provider_() {return Db_conn_fxt.Tdb("130_dbs_group_bys.dsv");}
|
||||
@Test public void GroupBy_1fld() {super.GroupBy_1fld_hook();}
|
||||
@Test public void GroupBy_2fld() {super.GroupBy_2fld_hook();}
|
||||
@Test public void Min() {super.MinMax_hook(true);}
|
||||
|
||||
@@ -18,42 +18,42 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.insertIntos; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public abstract class InsertIntos_base_tst {
|
||||
protected abstract Db_provider provider_();
|
||||
protected Db_provider provider;
|
||||
protected abstract Db_conn provider_();
|
||||
protected Db_conn conn;
|
||||
@Before public void setup() {
|
||||
provider = provider_();
|
||||
provider.Exec_qry(Db_qry_delete.new_().BaseTable_("dbs_group_bys"));
|
||||
provider.Exec_qry(Db_qry_delete.new_().BaseTable_("dbs_insert_intos"));
|
||||
conn = provider_();
|
||||
conn.Exec_qry(Db_qry_delete.new_("dbs_group_bys"));
|
||||
conn.Exec_qry(Db_qry_delete.new_("dbs_insert_intos"));
|
||||
}
|
||||
@After public void teardown() {
|
||||
provider.Conn_term();
|
||||
conn.Conn_term();
|
||||
}
|
||||
protected void Select_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
|
||||
provider.Exec_qry
|
||||
conn.Exec_qry
|
||||
(Db_qry_.insert_("dbs_insert_intos")
|
||||
.Cols_("key1", "val_int")
|
||||
.Select_
|
||||
( Db_qry_select.new_().Cols_("key1", "val_int").From_("dbs_group_bys")
|
||||
)
|
||||
);
|
||||
DataRdr rdr = provider.Exec_qry_as_rdr(Db_qry_select.new_().Cols_("key1", "val_int").From_("dbs_insert_intos"));
|
||||
DataRdr rdr = conn.Exec_qry_as_rdr(Db_qry_select.new_().Cols_("key1", "val_int").From_("dbs_insert_intos"));
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
}
|
||||
protected void GroupBy_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
|
||||
provider.Exec_qry
|
||||
conn.Exec_qry
|
||||
(Db_qry_.insert_("dbs_insert_intos")
|
||||
.Cols_("key1", "val_int")
|
||||
.Select_
|
||||
( Db_qry_select.new_().Cols_("key1").Cols_groupBy_sum("val_int", "val_int_func")
|
||||
.From_("dbs_group_bys").GroupBy_("key1")
|
||||
));
|
||||
DataRdr rdr = provider.Exec_qry_as_rdr(Db_qry_select.new_().Cols_("key1", "val_int").From_("dbs_insert_intos"));
|
||||
DataRdr rdr = conn.Exec_qry_as_rdr(Db_qry_select.new_().Cols_("key1", "val_int").From_("dbs_insert_intos"));
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "val_int", 3);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.insertIntos; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class InsertIntos_mysql_tst extends InsertIntos_base_tst {
|
||||
@Override protected Db_provider provider_() {return Db_provider_fxt.Mysql();}
|
||||
@Override protected Db_conn provider_() {return Db_conn_fxt.Mysql();}
|
||||
@Test public void Select() {super.Select_hook();}
|
||||
@Test public void GroupBy() {super.GroupBy_hook();}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.insertIntos; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class InsertIntos_tdb_tst extends InsertIntos_base_tst {
|
||||
@Override protected Db_provider provider_() {return Db_provider_fxt.Tdb("140_dbs_insert_intos.dsv");}
|
||||
@Override protected Db_conn provider_() {return Db_conn_fxt.Tdb("140_dbs_insert_intos.dsv");}
|
||||
@Test public void Select() {super.Select_hook();}
|
||||
@Test public void GroupBy() {super.GroupBy_hook();}
|
||||
}
|
||||
|
||||
@@ -16,29 +16,29 @@ 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.joins; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public abstract class Joins_base_tst {
|
||||
protected Db_provider provider;
|
||||
protected Db_conn conn;
|
||||
@Before public void setup() {
|
||||
provider = provider_();
|
||||
Db_qry_delete.new_().BaseTable_("dbs_crud_ops").Exec_qry(provider);
|
||||
Db_qry_delete.new_().BaseTable_("dbs_join1").Exec_qry(provider);
|
||||
conn = provider_();
|
||||
Db_qry_delete.new_("dbs_crud_ops").Exec_qry(conn);
|
||||
Db_qry_delete.new_("dbs_join1").Exec_qry(conn);
|
||||
}
|
||||
@After public void teardown() {
|
||||
provider.Conn_term();
|
||||
conn.Conn_term();
|
||||
}
|
||||
protected void InnerJoin_hook() {
|
||||
provider.Exec_qry(Db_qry_insert.new_().BaseTable_("dbs_crud_ops").Arg_("id", 0).Arg_("name", "me"));
|
||||
provider.Exec_qry(Db_qry_insert.new_().BaseTable_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "you"));
|
||||
provider.Exec_qry(Db_qry_insert.new_().BaseTable_("dbs_join1").Arg_("join_id", 0).Arg_("join_data", "data0"));
|
||||
provider.Exec_qry(Db_qry_insert.new_().BaseTable_("dbs_join1").Arg_("join_id", 1).Arg_("join_data", "data1"));
|
||||
conn.Exec_qry(new Db_qry_insert("dbs_crud_ops").Arg_("id", 0).Arg_("name", "me"));
|
||||
conn.Exec_qry(new Db_qry_insert("dbs_crud_ops").Arg_("id", 1).Arg_("name", "you"));
|
||||
conn.Exec_qry(new Db_qry_insert("dbs_join1").Arg_("join_id", 0).Arg_("join_data", "data0"));
|
||||
conn.Exec_qry(new Db_qry_insert("dbs_join1").Arg_("join_id", 1).Arg_("join_data", "data1"));
|
||||
Db_qry_select select = Db_qry_select.new_().From_("dbs_crud_ops").Join_("dbs_join1", "j1", Sql_join_itm.new_("join_id", "dbs_crud_ops", "id")).Cols_("id", "name", "join_data");
|
||||
|
||||
DataRdr rdr = provider.Exec_qry_as_rdr(select);
|
||||
DataRdr rdr = conn.Exec_qry_as_rdr(select);
|
||||
GfoNde table = GfoNde_.rdr_(rdr);
|
||||
Tfds.Eq(table.Subs().Count(), 2);
|
||||
Tfds.Eq(table.Subs().FetchAt_asGfoNde(0).Read("join_data"), "data0");
|
||||
Tfds.Eq(table.Subs().FetchAt_asGfoNde(1).Read("join_data"), "data1");
|
||||
}
|
||||
protected abstract Db_provider provider_();
|
||||
protected abstract Db_conn provider_();
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.joins; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Joins_tdb_tst extends Joins_base_tst {
|
||||
@Override protected Db_provider provider_() {return Db_provider_fxt.Tdb("120_dbs_joins.dsv");}
|
||||
@Override protected Db_conn provider_() {return Db_conn_fxt.Tdb("120_dbs_joins.dsv");}
|
||||
@Test public void InnerJoin() {
|
||||
try {
|
||||
InnerJoin_hook();
|
||||
|
||||
@@ -19,15 +19,15 @@ package gplx.dbs.orderBys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public abstract class OrderBys_base_tst {
|
||||
@Before public void setup() {
|
||||
provider = provider_();
|
||||
fx.Provider_(provider);
|
||||
Db_qry_delete.new_().BaseTable_("dbs_crud_ops").Exec_qry(provider);
|
||||
} protected Db_provider_fxt fx = new Db_provider_fxt();
|
||||
@After public void teardown() {provider.Conn_term();}
|
||||
protected abstract Db_provider provider_(); protected Db_provider provider;
|
||||
conn = provider_();
|
||||
fx.Conn_(conn);
|
||||
Db_qry_delete.new_("dbs_crud_ops").Exec_qry(conn);
|
||||
} protected Db_conn_fxt fx = new Db_conn_fxt();
|
||||
@After public void teardown() {conn.Conn_term();}
|
||||
protected abstract Db_conn provider_(); protected Db_conn conn;
|
||||
protected void Basic_hook() {
|
||||
fx.tst_ExecDml(1, Db_qry_insert.new_().BaseTable_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "you"));
|
||||
fx.tst_ExecDml(1, Db_qry_insert.new_().BaseTable_("dbs_crud_ops").Arg_("id", 0).Arg_("name", "me"));
|
||||
fx.tst_ExecDml(1, new Db_qry_insert("dbs_crud_ops").Arg_("id", 1).Arg_("name", "you"));
|
||||
fx.tst_ExecDml(1, new Db_qry_insert("dbs_crud_ops").Arg_("id", 0).Arg_("name", "me"));
|
||||
|
||||
fx.tst_ExecRdr(2, Db_qry_select.new_().From_("dbs_crud_ops").OrderBy_("id", true));
|
||||
fx.tst_RowAry(0, 0, "me");
|
||||
@@ -38,8 +38,8 @@ public abstract class OrderBys_base_tst {
|
||||
fx.tst_RowAry(1, 0, "me");
|
||||
}
|
||||
protected void SameVals_hook() {
|
||||
fx.tst_ExecDml(1, Db_qry_insert.new_().BaseTable_("dbs_crud_ops").Arg_("id", 0).Arg_("name", "me"));
|
||||
fx.tst_ExecDml(1, Db_qry_insert.new_().BaseTable_("dbs_crud_ops").Arg_("id", 0).Arg_("name", "you"));
|
||||
fx.tst_ExecDml(1, new Db_qry_insert("dbs_crud_ops").Arg_("id", 0).Arg_("name", "me"));
|
||||
fx.tst_ExecDml(1, new Db_qry_insert("dbs_crud_ops").Arg_("id", 0).Arg_("name", "you"));
|
||||
|
||||
fx.tst_ExecRdr(2, Db_qry_select.new_().From_("dbs_crud_ops").OrderBy_("id", true));
|
||||
fx.tst_RowAry(0, 0, "me");
|
||||
|
||||
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.orderBys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class OrderBys_tdb_tst extends OrderBys_base_tst {
|
||||
@Override protected Db_provider provider_() {return Db_provider_fxt.Tdb("120_dbs_joins.dsv");}
|
||||
@Override protected Db_conn provider_() {return Db_conn_fxt.Tdb("120_dbs_joins.dsv");}
|
||||
@Test public void Basic() {
|
||||
Basic_hook();
|
||||
}
|
||||
@@ -29,10 +29,10 @@ public class OrderBys_tdb_tst extends OrderBys_base_tst {
|
||||
//namespace gplx.dbs.crudOps {
|
||||
// import org.junit.*;
|
||||
// public class CrudOps_tdb_tst {
|
||||
// @Before public void setup() {fx = new CrudOpsFxt(Db_provider_fxt.Tdb("100_dbs_crud_ops.dsv"));} CrudOpsFxt fx;
|
||||
// @Before public void setup() {fx = new CrudOpsFxt(Db_conn_fxt.Tdb("100_dbs_crud_ops.dsv"));} CrudOpsFxt fx;
|
||||
// @Test public void FlushToDisk() {
|
||||
// fx.Fx().tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 2).Arg_("name", "you"));
|
||||
// Db_qry_flush.new_("dbs_crud_ops").Exec_qry(fx.Fx().Provider());
|
||||
// Db_qry_flush.new_("dbs_crud_ops").Exec_qry(fx.Fx().Conn());
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -50,7 +50,7 @@ public class SqliteDbMain {
|
||||
// }
|
||||
private void CreateMany(int number, int base_val) {
|
||||
long time_bgn = Env_.TickCount();
|
||||
Db_provider provider = Sqlite_engine_.Provider_load_or_make_(Io_url_.new_fil_("E:\\test.sqlite3"));
|
||||
Db_conn provider = Sqlite_engine_.Conn_load_or_make_(Io_url_.new_fil_("E:\\test.sqlite3"));
|
||||
String tbl_sql = String_.Concat_lines_nl
|
||||
( "CREATE TABLE fsdb_xtn_thm"
|
||||
, "( thm_id integer NOT NULL PRIMARY KEY"
|
||||
@@ -69,15 +69,15 @@ public class SqliteDbMain {
|
||||
Db_stmt stmt = Db_stmt_.new_insert_(provider, "fsdb_xtn_thm", "thm_id", "thm_owner_id", "thm_w", "thm_h", "thm_thumbtime", "thm_bin_db_id", "thm_size", "thm_modified", "thm_hash");
|
||||
for (int i = 0; i < number; i++) {
|
||||
stmt.Clear()
|
||||
.Val_int_(base_val + i)
|
||||
.Val_int_(base_val + i)
|
||||
.Val_int_(220)
|
||||
.Val_int_(200)
|
||||
.Val_int_(-1)
|
||||
.Val_int_(15)
|
||||
.Val_long_(23456)
|
||||
.Val_str_("")
|
||||
.Val_str_("")
|
||||
.Val_int(base_val + i)
|
||||
.Val_int(base_val + i)
|
||||
.Val_int(220)
|
||||
.Val_int(200)
|
||||
.Val_int(-1)
|
||||
.Val_int(15)
|
||||
.Val_long(23456)
|
||||
.Val_str("")
|
||||
.Val_str("")
|
||||
.Exec_insert();
|
||||
}
|
||||
long time_elapsed = (Env_.TickCount() - time_bgn);
|
||||
|
||||
Reference in New Issue
Block a user