mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.2.4.1
This commit is contained in:
42
140_dbs/src/gplx/dbs/Db_cmd_mode.java
Normal file
42
140_dbs/src/gplx/dbs/Db_cmd_mode.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 class Db_cmd_mode {
|
||||
Db_cmd_mode(int val) {this.val = val;}
|
||||
public int Val() {return val;} int val;
|
||||
public Db_cmd_mode MarkUpdated() {return this == Retrieved ? Updated : this;} // Created/Deleted noops
|
||||
public boolean Modified() {return this == Created || this == Updated;}
|
||||
public static final byte Tid_create = 1, Tid_update = 2, Tid_delete = 3, Tid_ignore = 4;
|
||||
public static final Db_cmd_mode
|
||||
Created = new Db_cmd_mode(Tid_create)
|
||||
, Updated = new Db_cmd_mode(Tid_update)
|
||||
, Deleted = new Db_cmd_mode(Tid_delete)
|
||||
, Retrieved = new Db_cmd_mode(Tid_ignore)
|
||||
;
|
||||
public static byte To_update(byte cur) {
|
||||
switch (cur) {
|
||||
case Tid_create: // ignore update if item is already marked for create
|
||||
case Tid_delete: // ignore update if item is already marked for delete (might want to throw error)
|
||||
return cur;
|
||||
case Tid_ignore: // must mark for update
|
||||
case Tid_update: // return self
|
||||
return Tid_update;
|
||||
default: throw Err_.unhandled(cur);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,36 +16,39 @@ 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.*; import gplx.dbs.qrys.*;
|
||||
public class Db_conn {
|
||||
private final Db_engine engine;
|
||||
private final ListAdp itm_list = ListAdp_.new_();
|
||||
private final ListAdp rls_list = ListAdp_.new_();
|
||||
public Db_conn(Db_engine engine) {
|
||||
this.engine = engine;
|
||||
this.txn_mgr = new Db_txn_mgr_base(engine);
|
||||
this.txn_mgr = new Db_txn_mgr(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, Db_meta_fld_list flds) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, flds.To_str_ary()));}
|
||||
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 Db_stmt New_stmt_select_all_where(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary()));}
|
||||
public Db_stmt New_stmt_update_by_meta(String tbl, Db_meta_fld_list flds, String... where) {
|
||||
return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, flds.To_str_ary_exclude(where)));
|
||||
public Db_stmt Stmt_insert(String tbl, Db_meta_fld_list flds) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, flds.To_str_ary()));}
|
||||
public Db_stmt Stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt Stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, cols));}
|
||||
public Db_stmt Stmt_update_exclude(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, flds.To_str_ary_exclude(where)));}
|
||||
public Db_stmt Stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt Stmt_select(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols));}
|
||||
public Db_stmt Stmt_select(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary()));}
|
||||
public void Exec_create_tbl_and_idx(Db_meta_tbl meta) {
|
||||
engine.Exec_create_tbl(meta);
|
||||
engine.Exec_create_idx(Gfo_usr_dlg_.Null, meta.Idxs());
|
||||
}
|
||||
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 Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... idxs) {engine.Exec_create_idx(usr_dlg, idxs);}
|
||||
public Db_stmt Rls_reg(Db_stmt stmt) {rls_list.Add(stmt); return stmt;}
|
||||
public void Conn_term() {
|
||||
int len = itm_list.Count();
|
||||
int len = rls_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_conn_itm itm = (Db_conn_itm)itm_list.FetchAt(i);
|
||||
itm.Conn_term();
|
||||
RlsAble itm = (RlsAble)rls_list.FetchAt(i);
|
||||
itm.Rls();
|
||||
}
|
||||
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
|
||||
// Db_conn_pool.I.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 Db_stmt Stmt_new(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));}
|
||||
|
||||
@@ -16,19 +16,9 @@ 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.qrys.*;
|
||||
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 final Db_conn Null = Db_conn_pool.I.Get_or_new(Db_url_.Null);
|
||||
public static int Select_fld0_as_int_or(Db_conn p, String sql, int or) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
|
||||
31
140_dbs/src/gplx/dbs/Db_conn_bldr.java
Normal file
31
140_dbs/src/gplx/dbs/Db_conn_bldr.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_conn_bldr {
|
||||
private Db_conn_bldr_wkr wkr;
|
||||
public void Reg_default_sqlite() {wkr = Db_conn_bldr_wkr__sqlite.I; wkr.Clear_for_tests();}
|
||||
public void Reg_default_mem() {wkr = Db_conn_bldr_wkr__mem.I; wkr.Clear_for_tests();}
|
||||
public Db_conn Get(String type, Object url_obj) {return wkr.Get(type, url_obj);}
|
||||
public Db_conn New(String type, Object url_obj) {return wkr.New(type, url_obj);}
|
||||
public Db_conn_bldr_data Get_or_new(String type, Object url_obj) {
|
||||
boolean exists = wkr.Exists(type, url_obj);
|
||||
Db_conn conn = exists ? Get(type, url_obj) : New(type, url_obj);
|
||||
return new Db_conn_bldr_data(conn, exists);
|
||||
}
|
||||
public static final Db_conn_bldr I = new Db_conn_bldr(); Db_conn_bldr() {}
|
||||
}
|
||||
@@ -16,10 +16,9 @@ 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)
|
||||
;
|
||||
public class Db_conn_bldr_data {
|
||||
public Db_conn_bldr_data(Db_conn conn, boolean exists) {this.conn = conn; this.exists = exists;}
|
||||
public Db_conn Conn() {return conn;} private final Db_conn conn;
|
||||
public boolean Exists() {return exists;} private final boolean exists;
|
||||
public boolean Created() {return !exists;}
|
||||
}
|
||||
68
140_dbs/src/gplx/dbs/Db_conn_bldr_wkr.java
Normal file
68
140_dbs/src/gplx/dbs/Db_conn_bldr_wkr.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
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.sqlite.*;
|
||||
public interface Db_conn_bldr_wkr {
|
||||
void Clear_for_tests();
|
||||
boolean Exists(String type, Object url_obj);
|
||||
Db_conn Get(String type, Object url_obj);
|
||||
Db_conn New(String type, Object url_obj);
|
||||
}
|
||||
class Db_conn_bldr_wkr__sqlite implements Db_conn_bldr_wkr {
|
||||
public void Clear_for_tests() {}
|
||||
public boolean Exists(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj; return Io_mgr._.ExistsFil(io_url);
|
||||
}
|
||||
public Db_conn Get(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj; if (!Io_mgr._.ExistsFil(io_url)) return null;
|
||||
Db_url db_url = Db_url_.sqlite_(io_url);
|
||||
return Db_conn_pool.I.Get_or_new(db_url);
|
||||
}
|
||||
public Db_conn New(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj;
|
||||
Io_mgr._.CreateDirIfAbsent(io_url.OwnerDir()); // must assert that dir exists
|
||||
Db_url db_url = Sqlite_url.make_(io_url);
|
||||
Db_conn conn = Db_conn_pool.I.Get_or_new(db_url);
|
||||
Sqlite_engine_.Pragma_page_size(conn, 4096);
|
||||
// conn.Conn_term(); // close conn after PRAGMA adjusted
|
||||
return conn;
|
||||
}
|
||||
public static final Db_conn_bldr_wkr__sqlite I = new Db_conn_bldr_wkr__sqlite(); Db_conn_bldr_wkr__sqlite() {}
|
||||
}
|
||||
class Db_conn_bldr_wkr__mem implements Db_conn_bldr_wkr {
|
||||
private final HashAdp hash = HashAdp_.new_();
|
||||
public void Clear_for_tests() {hash.Clear(); Db_conn_pool.I.Clear();}
|
||||
public boolean Exists(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj;
|
||||
String io_url_str = io_url.Xto_api();
|
||||
return hash.Has(io_url_str);
|
||||
}
|
||||
public Db_conn Get(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj;
|
||||
String io_url_str = io_url.Xto_api();
|
||||
if (!hash.Has(io_url_str)) return null;
|
||||
return Db_conn_pool.I.Get_or_new__mem(io_url.Xto_api());
|
||||
}
|
||||
public Db_conn New(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj;
|
||||
String io_url_str = io_url.Xto_api();
|
||||
hash.Add(io_url_str, io_url_str);
|
||||
return Db_conn_pool.I.Get_or_new__mem(io_url.Xto_api());
|
||||
}
|
||||
public static final Db_conn_bldr_wkr__mem I = new Db_conn_bldr_wkr__mem(); Db_conn_bldr_wkr__mem() {}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
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.core.primitives.*;
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -16,11 +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.engines.mems.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.engines.nulls.*; import gplx.dbs.engines.mems.*; import gplx.dbs.engines.tdbs.*;
|
||||
import gplx.dbs.engines.sqlite.*; import gplx.dbs.engines.mysql.*; import gplx.dbs.engines.postgres.*;
|
||||
public class Db_conn_pool {
|
||||
private final HashAdp conn_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));}
|
||||
private final HashAdp conn_hash = HashAdp_.new_(); private final HashAdp engine_hash = HashAdp_.new_();
|
||||
public void Clear() {conn_hash.Clear();}
|
||||
public void Del(Db_url url) {conn_hash.Del(url.Xto_api());}
|
||||
public Db_conn Get_or_new__mem(String db) {return Get_or_new(Db_url__mem.new_(db));}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_url_.sqlite_(url));}
|
||||
public Db_conn Get_or_new(String s) {return Get_or_new(Db_url_.parse_(s));}
|
||||
public Db_conn Get_or_new(Db_url url) {
|
||||
Db_conn rv = (Db_conn)conn_hash.Fetch(url.Xto_api());
|
||||
if (rv == null) {
|
||||
@@ -31,19 +35,12 @@ public class Db_conn_pool {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public Db_conn 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 conn = new Db_conn(engine);
|
||||
conn_hash.AddReplace(url.Xto_api(), conn);
|
||||
return conn;
|
||||
}
|
||||
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();
|
||||
public static final Db_conn_pool I = new Db_conn_pool(); Db_conn_pool() {this.Init();}
|
||||
private void Init() {
|
||||
this.Engines__add(Null_engine._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
}
|
||||
}
|
||||
|
||||
64
140_dbs/src/gplx/dbs/Db_crt_.java
Normal file
64
140_dbs/src/gplx/dbs/Db_crt_.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 gplx.core.criterias.*;
|
||||
public class Db_crt_ {
|
||||
public static final Criteria Wildcard = Criteria_.All;
|
||||
public static Criteria_fld eq_(String key, Object val) {return Criteria_fld.new_(key, Criteria_.eq_(val));}
|
||||
public static Criteria_fld eqn_(String key, Object val) {return Criteria_fld.new_(key, Criteria_.eqn_(val));}
|
||||
public static Criteria_fld lt_(String key, Comparable val) {return Criteria_fld.new_(key, Criteria_.lt_(val));}
|
||||
public static Criteria_fld lte_(String key, Comparable val) {return Criteria_fld.new_(key, Criteria_.lte_(val));}
|
||||
public static Criteria_fld mt_(String key, Comparable val) {return Criteria_fld.new_(key, Criteria_.mt_(val));}
|
||||
public static Criteria_fld mte_(String key, Comparable val) {return Criteria_fld.new_(key, Criteria_.mte_(val));}
|
||||
public static Criteria_fld between_(String key, Comparable lhs, Comparable rhs) {return Criteria_fld.new_(key, Criteria_.between_(lhs, rhs));}
|
||||
public static Criteria_fld in_(String key, Object... vals) {return Criteria_fld.new_(key, Criteria_.in_(vals));}
|
||||
public static Criteria_fld like_(String key, String pattern) {return Criteria_fld.new_(key, Criteria_.like_(pattern));}
|
||||
public static Criteria_fld liken_(String key, String pattern) {return Criteria_fld.new_(key, Criteria_.liken_(pattern));}
|
||||
public static Criteria_fld eq_(String key) {return Criteria_fld.new_(key, Criteria_.eq_(null));}
|
||||
public static Criteria eq_many_(String... ary) {
|
||||
Criteria rv = null;
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Criteria crt = Db_crt_.eq_(ary[i], null);
|
||||
rv = (i == 0)? crt : Criteria_.And(rv, crt);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Criteria eq_many_wo_null(String... ary) {
|
||||
Criteria rv = null;
|
||||
int len = ary.length;
|
||||
int crt_idx = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
String itm = ary[i]; if (itm == Db_meta_fld.Key_null) continue;
|
||||
Criteria crt = Db_crt_.eq_(itm, null);
|
||||
rv = (crt_idx == 0) ? crt : Criteria_.And(rv, crt);
|
||||
++crt_idx;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Criteria eq_many_(KeyVal... array) {
|
||||
Criteria rv = null;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
KeyVal pair = array[i];
|
||||
Criteria crt = Db_crt_.eq_(pair.Key(), pair.Val());
|
||||
rv = (i == 0)? crt : Criteria_.And(rv, crt);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Criteria_fld wrap_(String key, Criteria crt) {return Criteria_fld.new_(key, crt);}
|
||||
}
|
||||
53
140_dbs/src/gplx/dbs/Db_crt_tst.java
Normal file
53
140_dbs/src/gplx/dbs/Db_crt_tst.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Db_crt_tst {
|
||||
@Before public void setup() {
|
||||
row = GfoNde_.vals_(GfoFldList_.new_().Add("id", IntClassXtn._).Add("name", StringClassXtn._), Object_.Ary(1, "me"));
|
||||
}
|
||||
@Test public void EqualTest() {
|
||||
crt = Db_crt_.eq_("id", 1);
|
||||
tst_Match(true, row, crt);
|
||||
}
|
||||
@Test public void EqualFalseTest() {
|
||||
crt = Db_crt_.eq_("id", 2);
|
||||
tst_Match(false, row, crt);
|
||||
}
|
||||
@Test public void AndCompositeTest() {
|
||||
crt = Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me"));
|
||||
tst_Match(true, row, crt);
|
||||
|
||||
crt = Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "you"));
|
||||
tst_Match(false, row, crt);
|
||||
}
|
||||
@Test public void OrCompositeTest() {
|
||||
crt = Criteria_.Or(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "you"));
|
||||
tst_Match(true, row, crt);
|
||||
|
||||
crt = Criteria_.Or(Db_crt_.eq_("id", 2), Db_crt_.eq_("name", "you"));
|
||||
tst_Match(false, row, crt);
|
||||
}
|
||||
|
||||
void tst_Match(boolean epxd, GfoNde row, Criteria crt) {
|
||||
boolean actl = crt.Matches(row);
|
||||
Tfds.Eq(epxd, actl);
|
||||
}
|
||||
GfoNde row; Criteria crt;
|
||||
}
|
||||
@@ -16,25 +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.*;
|
||||
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);
|
||||
public class Db_idx_itm {
|
||||
public String Xto_sql() {return sql;} private String sql;
|
||||
public static Db_idx_itm sql_(String sql) {
|
||||
Db_idx_itm rv = new Db_idx_itm();
|
||||
rv.sql = sql;
|
||||
return rv;
|
||||
}
|
||||
public static Db_idx_itm[] ary_sql_(String... ary) {
|
||||
int len = ary.length;
|
||||
Db_idx_itm[] rv = new Db_idx_itm[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
rv[i] = sql_(ary[i]);
|
||||
}
|
||||
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() {}
|
||||
}
|
||||
@@ -29,4 +29,5 @@ public class Db_meta_fld {
|
||||
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;
|
||||
public static final String[] Ary_empy = String_.Ary_empty;
|
||||
public static final String Key_null = null;
|
||||
}
|
||||
|
||||
@@ -19,26 +19,24 @@ package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_fld_list {
|
||||
private final OrderedHash flds = OrderedHash_.new_();
|
||||
private final ListAdp keys = ListAdp_.new_();
|
||||
public void Clear() {flds.Clear(); keys.Clear();}
|
||||
public Db_meta_fld Get_by(String name) {return (Db_meta_fld)flds.Fetch(name);}
|
||||
public String[] To_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[] To_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[] To_str_ary_exclude(String[] ary) {
|
||||
HashAdp ary_hash = HashAdp_.new_();
|
||||
ListAdp rv = ListAdp_.new_();
|
||||
int ary_len = ary.length;
|
||||
for (int i = 0; i < ary_len; ++i) {
|
||||
String ary_itm = ary[i];
|
||||
ary_hash.Add(ary_itm, ary_itm);
|
||||
}
|
||||
int fld_len = flds.Count();
|
||||
for (int i = 0; i < fld_len; ++i) {
|
||||
Db_meta_fld fld = (Db_meta_fld)flds.FetchAt(i);
|
||||
String fld_key = fld.Name();
|
||||
boolean include = true;
|
||||
for (int j = 0; j < ary_len; ++j) {
|
||||
String itm_key = ary[j];
|
||||
if (String_.Eq(fld_key, itm_key)) {
|
||||
include = false;
|
||||
break;
|
||||
}
|
||||
if (include)
|
||||
rv.Add(itm_key);
|
||||
}
|
||||
if (ary_hash.Has(fld_key)) continue;
|
||||
rv.Add(fld_key);
|
||||
}
|
||||
return rv.XtoStrAry();
|
||||
}
|
||||
|
||||
@@ -16,12 +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.dbs.sqls.*;
|
||||
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;}
|
||||
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 Tbl() {return tbl;} private final String tbl;
|
||||
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);}
|
||||
public String To_sql_create() {return Db_sqlbldr__sqlite.I.Bld_create_idx(this);}
|
||||
public static Db_meta_idx new_unique_by_name(String tbl, String idx_name, String... flds) {return new Db_meta_idx(tbl, idx_name, Bool_.Y, flds);}
|
||||
public static Db_meta_idx new_normal_by_name(String tbl, String idx_name, String... flds) {return new Db_meta_idx(tbl, idx_name, Bool_.N, flds);}
|
||||
public static Db_meta_idx new_unique_by_tbl(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, Bld_idx_name(tbl, name), Bool_.Y, flds);}
|
||||
public static Db_meta_idx new_normal_by_tbl(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, Bld_idx_name(tbl, name), Bool_.N, flds);}
|
||||
public static Db_meta_idx new_unique_by_tbl_wo_null(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, Bld_idx_name(tbl, name), Bool_.Y, String_.Ary_wo_null(flds));}
|
||||
public static Db_meta_idx new_normal_by_tbl_wo_null(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, Bld_idx_name(tbl, name), Bool_.N, String_.Ary_wo_null(flds));}
|
||||
public static String Bld_idx_name(String tbl, String suffix) {return String_.Concat(tbl, "__", suffix);}
|
||||
}
|
||||
|
||||
@@ -16,6 +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 gplx.dbs.sqls.*;
|
||||
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;
|
||||
@@ -23,6 +24,7 @@ public class Db_meta_tbl {
|
||||
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 String To_sql_create() {return Db_sqlbldr__sqlite.I.Bld_create_tbl(this);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld_list flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds.To_fld_ary(), 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);}
|
||||
|
||||
@@ -16,6 +16,9 @@ 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();
|
||||
public interface Db_qry {
|
||||
int Tid();
|
||||
boolean Exec_is_rdr();
|
||||
String Base_table();
|
||||
String Xto_sql();
|
||||
}
|
||||
51
140_dbs/src/gplx/dbs/Db_qry_.java
Normal file
51
140_dbs/src/gplx/dbs/Db_qry_.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.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.qrys.*;
|
||||
public class Db_qry_ {
|
||||
public static Db_qry_select select_cols_(String tbl, Criteria crt, String... cols){return select_().From_(tbl).Where_(crt).Cols_(cols);}
|
||||
public static Db_qry_select select_val_(String tbl, String col, Criteria crt) {return select_().From_(tbl).Where_(crt).Cols_(col);}
|
||||
public static Db_qry_select select_tbl_(String tbl) {return select_().From_(tbl);}
|
||||
public static Db_qry_select select_() {return Db_qry_select.new_();}
|
||||
public static Db_qry_delete delete_(String tbl, Criteria crt) {return Db_qry_delete.new_(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 = new Db_qry_insert(tbl);
|
||||
for (KeyVal pair : pairs)
|
||||
cmd.Arg_obj_(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public static Db_qry_update update_(String tbl, Criteria crt) {
|
||||
Db_qry_update update = Db_qry_update.new_();
|
||||
update.From_(tbl);
|
||||
update.Where_(crt);
|
||||
return update;
|
||||
}
|
||||
public static Db_qry_update update_common_(String tbl, Criteria crt, KeyVal... pairs) {
|
||||
Db_qry_update cmd = Db_qry_update.new_();
|
||||
cmd.From_(tbl); cmd.Where_(crt);
|
||||
for (KeyVal pair : pairs)
|
||||
cmd.Arg_obj_(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
}
|
||||
public static final Object WhereAll = null;
|
||||
public static Db_qry as_(Object obj) {return obj instanceof Db_qry ? (Db_qry)obj : null;}
|
||||
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;
|
||||
}
|
||||
@@ -17,7 +17,7 @@ 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 {
|
||||
public 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;
|
||||
@@ -25,25 +25,25 @@ class Db_rdr__basic implements Db_rdr {
|
||||
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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561(Read_str(i));}
|
||||
@gplx.Virtual public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561(Read_str(k));}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual 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));}}
|
||||
@gplx.Virtual public void Rls() {try {rdr.close();} catch (Exception e) {throw Err_.new_("close failed: err={0}", Err_.Message_lang(e));}}
|
||||
}
|
||||
|
||||
61
140_dbs/src/gplx/dbs/Db_stmt.java
Normal file
61
140_dbs/src/gplx/dbs/Db_stmt.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 gplx.dbs.engines.*;
|
||||
public interface Db_stmt extends RlsAble {
|
||||
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();
|
||||
int Exec_delete();
|
||||
DataRdr Exec_select();
|
||||
Db_rdr Exec_select_as_rdr();
|
||||
Object Exec_select_val();
|
||||
void Ctor_stmt(Db_engine engine, Db_qry qry);
|
||||
Db_stmt Clear();
|
||||
Db_stmt Reset_stmt();
|
||||
}
|
||||
58
140_dbs/src/gplx/dbs/Db_stmt_.java
Normal file
58
140_dbs/src/gplx/dbs/Db_stmt_.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
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.qrys.*;
|
||||
public class Db_stmt_ {
|
||||
public static final Db_stmt Null = new Db_stmt_sql();
|
||||
public static Db_stmt new_insert_(Db_conn conn, String tbl, String... flds) {
|
||||
Db_qry qry = Db_qry_insert.new_(tbl, flds);
|
||||
return conn.Stmt_new(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.Stmt_new(qry);
|
||||
}
|
||||
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 conn.Stmt_new(qry);
|
||||
}
|
||||
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 conn.Stmt_new(qry);
|
||||
}
|
||||
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 conn.Stmt_new(qry);
|
||||
}
|
||||
public static Db_stmt new_select_all_(Db_conn conn, String tbl) {
|
||||
return conn.Stmt_new(Db_qry_.select_tbl_(tbl));
|
||||
}
|
||||
public static Db_stmt new_select_as_rdr(Db_conn conn, Db_qry__select_in_tbl qry) {
|
||||
return conn.Stmt_new(qry);
|
||||
}
|
||||
public static Db_stmt new_select_as_rdr(Db_conn conn, String sql) {
|
||||
return conn.Stmt_new(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;}
|
||||
}
|
||||
48
140_dbs/src/gplx/dbs/Db_stmt_bldr.java
Normal file
48
140_dbs/src/gplx/dbs/Db_stmt_bldr.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_stmt_bldr {
|
||||
private Db_conn conn; private Db_stmt create, update, delete;
|
||||
private String tbl_name; private String[] flds_keys, flds_vals, flds_all;
|
||||
public void Conn_(Db_conn v, String tbl_name, Db_meta_fld_list flds, String... flds_keys) {
|
||||
Conn_(v, tbl_name, flds.To_str_ary(), flds.To_str_ary_exclude(flds_keys), flds_keys);
|
||||
}
|
||||
public void Conn_(Db_conn v, String tbl_name, String[] flds_vals, String... flds_keys) {
|
||||
Conn_(v, tbl_name, String_.Ary_add(flds_keys, flds_vals), flds_vals, flds_keys);
|
||||
}
|
||||
private void Conn_(Db_conn v, String tbl_name, String[] flds_all, String[] flds_vals, String... flds_keys) {
|
||||
this.conn = v; this.tbl_name = tbl_name;
|
||||
this.flds_all = flds_all; this.flds_vals = flds_vals; this.flds_keys = flds_keys;
|
||||
}
|
||||
public Db_stmt Get(byte cmd_mode) {
|
||||
switch (cmd_mode) {
|
||||
case Db_cmd_mode.Tid_create: if (create == null) create = conn.Stmt_insert(tbl_name, flds_all); return create;
|
||||
case Db_cmd_mode.Tid_update: if (update == null) update = conn.Stmt_update(tbl_name, flds_keys, flds_vals); return update;
|
||||
case Db_cmd_mode.Tid_delete: if (delete == null) delete = conn.Stmt_delete(tbl_name, flds_keys); return delete;
|
||||
case Db_cmd_mode.Tid_ignore: return Db_stmt_.Null;
|
||||
default: throw Err_.unhandled(cmd_mode);
|
||||
}
|
||||
}
|
||||
public void Batch_bgn() {conn.Txn_mgr().Txn_bgn_if_none();}
|
||||
public void Batch_end() {conn.Txn_mgr().Txn_end_all();}
|
||||
public void Rls() {
|
||||
create = Db_stmt_.Rls(create);
|
||||
update = Db_stmt_.Rls(update);
|
||||
delete = Db_stmt_.Rls(delete);
|
||||
}
|
||||
}
|
||||
40
140_dbs/src/gplx/dbs/Db_txn_mgr.java
Normal file
40
140_dbs/src/gplx/dbs/Db_txn_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.*;
|
||||
import gplx.dbs.engines.*;
|
||||
public class Db_txn_mgr {
|
||||
public Db_txn_mgr(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() {
|
||||
engine.Txn_bgn();
|
||||
++txn_depth;
|
||||
}
|
||||
public void Txn_end_all() {this.Txn_end();}
|
||||
public void Txn_end() {
|
||||
if (txn_depth == 0) return;
|
||||
engine.Txn_end();
|
||||
--txn_depth;
|
||||
txn_count = 0;
|
||||
}
|
||||
public void Txn_end_all_bgn_if_none() {
|
||||
this.Txn_end_all();
|
||||
this.Txn_bgn_if_none();
|
||||
}
|
||||
public int Txn_count() {return txn_count;} public void Txn_count_(int v) {txn_count = v;} int txn_count;
|
||||
}
|
||||
@@ -18,78 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_url {
|
||||
String Tid();
|
||||
String Database();
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -16,20 +16,21 @@ 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.*; import gplx.dbs.engines.sqlite.*;
|
||||
import gplx.dbs.engines.nulls.*; import gplx.dbs.engines.mems.*; import gplx.dbs.engines.sqlite.*; import gplx.dbs.engines.tdbs.*;
|
||||
import gplx.dbs.engines.mysql.*; import gplx.dbs.engines.postgres.*;
|
||||
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 final Db_url Null = Null_url._;
|
||||
public static final Db_url Test = Mysql_url.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 sqlite_(Io_url url) {return Sqlite_url.load_(url);}
|
||||
public static Db_url tdb_(Io_url url) {return Tdb_url.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;
|
||||
public static final String Key_tdb = Tdb_url.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(Null_url._).Add(Tdb_url._).Add(Mysql_url._).Add(Postgres_url._).Add(Sqlite_url._);
|
||||
this.Add(Db_url__mem.I);
|
||||
}
|
||||
public Db_url_pool Add(Db_url itm) {regy.AddReplace(itm.Tid(), itm); return this;}
|
||||
|
||||
@@ -21,7 +21,7 @@ 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 Database() {return database;} protected 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;}
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
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.core.strings.*;
|
||||
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")
|
||||
;
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
/*
|
||||
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.core.strings.*;
|
||||
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[] To_str_ary() {
|
||||
int len = this.Count();
|
||||
String[] rv = new String[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
rv[i] = fld.Fld();
|
||||
}
|
||||
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() {}
|
||||
}
|
||||
34
140_dbs/src/gplx/dbs/engines/Db_engine.java
Normal file
34
140_dbs/src/gplx/dbs/engines/Db_engine.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.engines; import gplx.*; import gplx.dbs.*;
|
||||
public interface Db_engine {
|
||||
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();
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
Object Exec_as_obj(Db_qry qry);
|
||||
void Exec_create_tbl(Db_meta_tbl meta);
|
||||
void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary);
|
||||
}
|
||||
90
140_dbs/src/gplx/dbs/engines/Db_engine_sql_base.java
Normal file
90
140_dbs/src/gplx/dbs/engines/Db_engine_sql_base.java
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
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; import gplx.*; import gplx.dbs.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.qrys.*; import gplx.dbs.sqls.*;
|
||||
public abstract class Db_engine_sql_base implements Db_engine {
|
||||
@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 = (Db_rdr__basic)New_rdr_clone();
|
||||
rv.Ctor((ResultSet)rdr, sql);
|
||||
return rv;
|
||||
}
|
||||
@gplx.Virtual public Db_rdr 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 = 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));}
|
||||
}
|
||||
private DataRdr Exec_as_rdr(String sql) {
|
||||
try {
|
||||
Statement cmd = New_stmt_exec(sql);
|
||||
cmd.execute(sql);
|
||||
ResultSet rdr = cmd.getResultSet();
|
||||
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));}
|
||||
}
|
||||
public void Exec_create_tbl(Db_meta_tbl meta) {Exec_as_int(meta.To_sql_create());}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_meta_idx idx = ary[i];
|
||||
usr_dlg.Plog_many("", "", "db.idx.create; db=~{0} idx=~{1}", url.Database(), idx.Name());
|
||||
Exec_as_int(idx.To_sql_create());
|
||||
}
|
||||
}
|
||||
@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();
|
||||
private Connection connection;
|
||||
public void Conn_open() {connection = Conn_new();}
|
||||
public void Conn_term() {
|
||||
if (connection == null) return; // connection never opened; just exit
|
||||
try {connection.close();}
|
||||
catch (Exception e) {throw Err_.err_(e, "Conn_term.fail; url={0} err={1}", url.Xto_raw(), Err_.Message_lang(e));}
|
||||
connection = null;
|
||||
}
|
||||
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));}
|
||||
}
|
||||
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", Url().Xto_raw());}
|
||||
}
|
||||
}
|
||||
@@ -18,20 +18,12 @@ 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);
|
||||
}
|
||||
}
|
||||
Db_engine__mem(Db_url url) {this.url = url;}
|
||||
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_engine New_clone(Db_url url) {return new Db_engine__mem(url);}
|
||||
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 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_();}
|
||||
@@ -42,5 +34,10 @@ public class Db_engine__mem implements Db_engine {
|
||||
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 void Exec_create_tbl(Db_meta_tbl meta) {
|
||||
Mem_tbl mem_tbl = new Mem_tbl();
|
||||
tbl_hash.AddReplace(meta.Name(), mem_tbl);
|
||||
}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {} // TODO: implement unique index
|
||||
public static final Db_engine__mem _ = new Db_engine__mem(); Db_engine__mem() {}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,9 @@ 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;
|
||||
@@ -33,25 +29,24 @@ public class Db_rdr__mem implements Db_rdr {
|
||||
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 byte[] Read_bry(String k) {return (byte[])row.Get_by(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 String Read_str(String k) {return (String)row.Get_by(k);}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.new_utf8__null((String)row.Get_at(i));} // NOTE: null b/c db can have NULL
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.new_utf8__null((String)row.Get_by(k));} // NOTE: null b/c db can have NULL
|
||||
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 DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561((String)row.Get_by(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 byte Read_byte(String k) {return Byte_.cast_(row.Get_by(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 int Read_int(String k) {return Int_.cast_(row.Get_by(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 long Read_long(String k) {return Long_.cast_(row.Get_by(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 float Read_float(String k) {return Float_.cast_(row.Get_by(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 double Read_double(String k) {return Double_.cast_(row.Get_by(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 boolean Read_bool_by_byte(String k) {return Byte_.cast_(row.Get_by(k)) == 1;}
|
||||
public void Rls() {}
|
||||
private int Ord_by_key(String k) {return Int_.cast_(ords.Fetch(k));}
|
||||
}
|
||||
|
||||
@@ -17,91 +17,90 @@ 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 final ListAdp val_list = ListAdp_.new_(); 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;
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private final ListAdp val_list = ListAdp_.new_();
|
||||
public Db_stmt__mem(Db_engine__mem engine, Db_qry qry) {Ctor_stmt(engine, qry);} private Db_engine__mem engine;
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {this.engine = (Db_engine__mem)engine; this.qry = qry;}
|
||||
public HashAdp Crts() {return crt_hash;} private final HashAdp crt_hash = HashAdp_.new_();
|
||||
public int Args_len() {return val_list.Count();}
|
||||
public Object Args_get_at(int i) {return val_list.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;
|
||||
val_list.Clear();
|
||||
crt_hash.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
this.Clear();
|
||||
}
|
||||
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);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
try {Add(k, where, 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);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
try {Add(k, where, 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);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
try {Add(k, where, 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);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
try {Add(k, where, 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);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
try {Add(k, where, 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);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
try {Add(k, where, 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);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
try {Add(k, where, 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);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_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);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
try {Add(k, where, 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, "", Bool_.N, bfr.Xto_str_and_clear());
|
||||
Add("", Bool_.N, bfr.Xto_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
@@ -122,10 +121,9 @@ public class Db_stmt__mem implements Db_stmt {
|
||||
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, String k, boolean where, Object v) {
|
||||
public Object Exec_select_val() {throw Err_.not_implemented_();}
|
||||
private void Add(String k, boolean where, Object v) {
|
||||
if (k == Db_meta_fld.Key_null) return; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
val_list.Add(v);
|
||||
if (where) {
|
||||
ListAdp list = (ListAdp)crt_hash.Fetch(k);
|
||||
|
||||
@@ -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.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.qrys.*;
|
||||
public class Mem_tbl {
|
||||
private final ListAdp rows = ListAdp_.new_(); private final ListAdp where_rows = ListAdp_.new_();
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
|
||||
48
140_dbs/src/gplx/dbs/engines/mysql/Mysql_engine.java
Normal file
48
140_dbs/src/gplx/dbs/engines/mysql/Mysql_engine.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.engines.mysql; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.sqls.*;
|
||||
import java.sql.*;
|
||||
public class Mysql_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Mysql_url.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(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Mysql_rdr.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Mysql_url url_as_mysql = (Mysql_url)url;
|
||||
return Conn_make_by_url("jdbc:mysql://localhost/" + url_as_mysql.Database() + "?characterEncoding=UTF8", url_as_mysql.Uid(), url_as_mysql.Pwd());
|
||||
}
|
||||
public static final Mysql_engine _ = new Mysql_engine(); Mysql_engine() {}
|
||||
}
|
||||
class Mysql_rdr extends Db_data_rdr {
|
||||
//PATCH:MYSQL:byte actually returned as int by Jdbc ResultSet (or MYSQL impmentation); convert to byte
|
||||
@Override public byte ReadByte(String key) {return ReadByteOr(key, Byte.MAX_VALUE);}
|
||||
@Override public byte ReadByteOr(String key, byte or) {
|
||||
Object val = Read(key);
|
||||
return val == null ? or : ((Integer)val).byteValue();
|
||||
}
|
||||
public static Mysql_rdr new_(ResultSet rdr, String commandText) {
|
||||
Mysql_rdr rv = new Mysql_rdr();
|
||||
rv.ctor_db_data_rdr(rdr, commandText);
|
||||
return rv;
|
||||
} Mysql_rdr() {}
|
||||
}
|
||||
41
140_dbs/src/gplx/dbs/engines/mysql/Mysql_url.java
Normal file
41
140_dbs/src/gplx/dbs/engines/mysql/Mysql_url.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mysql; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Mysql_url 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) {
|
||||
Mysql_url rv = new Mysql_url();
|
||||
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 Mysql_url _ = new Mysql_url(); Mysql_url() {}
|
||||
}
|
||||
35
140_dbs/src/gplx/dbs/engines/nulls/Null_engine.java
Normal file
35
140_dbs/src/gplx/dbs/engines/nulls/Null_engine.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.nulls; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Null_engine implements Db_engine {
|
||||
public String Tid() {return Null_url.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 Object Exec_as_obj(Db_qry cmd) {return cmd.Exec_is_rdr() ? (Object)DataRdr_.Null : -1;}
|
||||
public void Exec_create_tbl(Db_meta_tbl meta) {}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {}
|
||||
public static final Null_engine _ = new Null_engine(); Null_engine() {}
|
||||
}
|
||||
23
140_dbs/src/gplx/dbs/engines/nulls/Null_url.java
Normal file
23
140_dbs/src/gplx/dbs/engines/nulls/Null_url.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.nulls; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Null_url 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 Null_url _ = new Null_url(); Null_url() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
}
|
||||
35
140_dbs/src/gplx/dbs/engines/postgres/Postgres_engine.java
Normal file
35
140_dbs/src/gplx/dbs/engines/postgres/Postgres_engine.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.postgres; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.sqls.*;
|
||||
import java.sql.*;
|
||||
public class Postgres_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Postgres_url.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(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Db_data_rdr_.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Postgres_url url_as_postgres = (Postgres_url)url;
|
||||
return Conn_make_by_url("jdbc:" + url_as_postgres.Tid() + "://localhost/" + url_as_postgres.Database(), url_as_postgres.Uid(), url_as_postgres.Pwd());
|
||||
}
|
||||
public static final Postgres_engine _ = new Postgres_engine(); Postgres_engine() {}
|
||||
}
|
||||
42
140_dbs/src/gplx/dbs/engines/postgres/Postgres_url.java
Normal file
42
140_dbs/src/gplx/dbs/engines/postgres/Postgres_url.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.engines.postgres; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Postgres_url 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) {
|
||||
Postgres_url rv = new Postgres_url();
|
||||
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 Postgres_url _ = new Postgres_url(); Postgres_url() {}
|
||||
}
|
||||
131
140_dbs/src/gplx/dbs/engines/sqlite/Sqlite_engine.java
Normal file
131
140_dbs/src/gplx/dbs/engines/sqlite/Sqlite_engine.java
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import java.sql.*;
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Sqlite_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Sqlite_url.Tid_const;}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
Sqlite_engine rv = new Sqlite_engine();
|
||||
rv.Ctor(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Sqlite_rdr.new_(rdr, commandText);}
|
||||
@Override public Db_rdr New_rdr_clone() {return new Db_rdr__sqlite();}
|
||||
static boolean loaded = false;
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
if (!loaded) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
}
|
||||
catch (ClassNotFoundException e) {throw Err_.new_("could not load sqlite jdbc driver");}
|
||||
loaded = true;
|
||||
}
|
||||
Sqlite_url url_as_sqlite = (Sqlite_url)url;
|
||||
return Conn_make_by_url("jdbc:sqlite://" + String_.Replace(url_as_sqlite.Url().Raw(), "\\", "/"), "", "");
|
||||
}
|
||||
private boolean pragma_needed = true;
|
||||
@Override public void Txn_bgn() {
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA ENCODING=\"UTF-8\";"));
|
||||
// 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) {
|
||||
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
|
||||
Exec_as_obj(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));
|
||||
}
|
||||
public 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));}}
|
||||
@Override public boolean Read_bool_by_byte(String k) {
|
||||
try {
|
||||
int val = rdr.getInt(k);
|
||||
return val == 1;
|
||||
} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", k, Bool_.Cls_val_name, Err_.Message_lang(e));}
|
||||
}
|
||||
@Override public long Read_long(String k) {
|
||||
try {
|
||||
long val = rdr.getLong(k);
|
||||
Number n = (Number)val;
|
||||
return n.longValue();
|
||||
} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", k, Long_.Cls_val_name, Err_.Message_lang(e));}
|
||||
}
|
||||
@Override public float Read_float(String k) {
|
||||
try {
|
||||
Double val = (Double)rdr.getDouble(k);
|
||||
return val == null ? Float.NaN : val.floatValue();
|
||||
} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", k, Float_.Cls_val_name, Err_.Message_lang(e));}
|
||||
}
|
||||
@Override public DateAdp Read_date_by_str(String k) {
|
||||
try {
|
||||
String val = rdr.getString(k);
|
||||
return val == null ? null : DateAdp_.parse_fmt(val, "M/dd/yyyy hh:mm tt");
|
||||
} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", k, DateAdp_.Cls_ref_type, Err_.Message_lang(e));}
|
||||
}
|
||||
// @Override public DecimalAdp ReadDecimalOr(String key, DecimalAdp or) {
|
||||
// Object val = Read(key);
|
||||
// Double d = ((Double)val);
|
||||
// return val == null ? null : DecimalAdp_.double_(d);
|
||||
// }
|
||||
}
|
||||
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);
|
||||
Double d = ((Double)val);
|
||||
return val == null ? or : d.floatValue();
|
||||
}
|
||||
@Override public DecimalAdp ReadDecimal(String key) {return ReadDecimalOr(key, null);}
|
||||
@Override public DecimalAdp ReadDecimalOr(String key, DecimalAdp or) {
|
||||
Object val = Read(key);
|
||||
Double d = ((Double)val);
|
||||
return val == null ? or : DecimalAdp_.double_(d);
|
||||
}
|
||||
@Override public DateAdp ReadDate(String key) {return ReadDateOr(key, null);}
|
||||
@Override public DateAdp ReadDateOr(String key, DateAdp or) {
|
||||
Object val = Read(key);
|
||||
return val == null ? or : DateAdp_.parse_fmt((String)val, "M/dd/yyyy hh:mm tt");
|
||||
}
|
||||
@Override public boolean ReadBool(String key) {return ReadBoolOr(key, false);}
|
||||
@Override public boolean ReadBoolOr(String key, boolean or) {
|
||||
Object val = Read(key);
|
||||
return val == null ? or : Int_.cast_(val) == 1;
|
||||
}
|
||||
@Override public byte ReadByte(String key) {return ReadByteOr(key, Byte_.Zero);}
|
||||
@Override public byte ReadByteOr(String key, byte or) {
|
||||
Object val = Read(key);
|
||||
return val == null ? or : (byte)Int_.cast_(val);
|
||||
}
|
||||
@Override public long ReadLong(String key) {return ReadLongOr(key, Long_.MinValue);}
|
||||
@Override public long ReadLongOr(String key, long or) {
|
||||
Object val = Read(key);
|
||||
if (val == null) return or;
|
||||
Number n = (Number)val;
|
||||
return n.longValue();
|
||||
}
|
||||
public static Sqlite_rdr new_(ResultSet rdr, String commandText) {
|
||||
Sqlite_rdr rv = new Sqlite_rdr();
|
||||
rv.ctor_db_data_rdr(rdr, commandText);
|
||||
return rv;
|
||||
} Sqlite_rdr() {}
|
||||
}
|
||||
94
140_dbs/src/gplx/dbs/engines/sqlite/Sqlite_engine_.java
Normal file
94
140_dbs/src/gplx/dbs/engines/sqlite/Sqlite_engine_.java
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
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.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.primitives.*; import gplx.dbs.qrys.*; import gplx.dbs.utls.*; import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*;
|
||||
public class Sqlite_engine_ {
|
||||
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_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_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_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_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_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_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(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(Gfo_usr_dlg usr_dlg, Db_conn conn, String tbl, Db_meta_idx[] idx_ary) {
|
||||
conn.Txn_mgr().Txn_end_all(); // commit any pending transactions
|
||||
int len = idx_ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_meta_idx idx = idx_ary[i];
|
||||
String idx_sql = idx.To_sql_create();
|
||||
usr_dlg.Plog_many("", "", "creating index: ~{0} ~{1}", tbl, idx_sql);
|
||||
conn.Exec_qry(Db_qry_sql.ddl_(idx.To_sql_create()));
|
||||
usr_dlg.Log_many("", "", "index created: ~{0} ~{1}", tbl, idx_sql);
|
||||
}
|
||||
}
|
||||
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++) {
|
||||
String index = idxs[i].Xto_sql();
|
||||
usr_dlg.Plog_many("", "", "creating index: ~{0} ~{1}", file_id, index);
|
||||
p.Exec_qry(Db_qry_sql.ddl_(index));
|
||||
usr_dlg.Log_many("", "", "index created: ~{0} ~{1}", file_id, index);
|
||||
}
|
||||
}
|
||||
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_url connect = exists ? Sqlite_url.load_(url) : Sqlite_url.make_(url);
|
||||
Db_conn p = Db_conn_pool.I.Get_or_new(connect);
|
||||
if (!exists)
|
||||
Pragma_page_size(p, 4096);
|
||||
return p;
|
||||
}
|
||||
public static final int Stmt_arg_max = 999; // 999 is max number of variables allowed by sqlite
|
||||
public static final boolean Supports_read_binary_stream = false;
|
||||
public static final boolean Supports_indexed_by = true;
|
||||
public static String X_date_to_str(DateAdp v) {return v == Date_null ? "" : v.XtoStr_fmt_iso_8561();}
|
||||
public static final DateAdp Date_null = null;
|
||||
public static final byte Wildcard_byte = Byte_ascii.Hash;
|
||||
}
|
||||
@@ -16,14 +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.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_url__sqlite extends Db_url__base {
|
||||
public class Sqlite_url 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();
|
||||
Sqlite_url rv = new Sqlite_url();
|
||||
String url = m.ReadStr("data source");
|
||||
rv.url = Io_url_.new_any_(url);
|
||||
rv.Ctor("", url, raw, BldApi(m, KeyVal_.new_("version", "3")));
|
||||
rv.database = rv.url.NameOnly();
|
||||
return rv;
|
||||
}
|
||||
public static Db_url load_(Io_url url) {
|
||||
@@ -42,5 +43,5 @@ public class Db_url__sqlite extends Db_url__base {
|
||||
|
||||
));
|
||||
}
|
||||
public static final Db_url__sqlite _ = new Db_url__sqlite(); Db_url__sqlite() {}
|
||||
public static final Sqlite_url _ = new Sqlite_url(); Sqlite_url() {}
|
||||
}
|
||||
32
140_dbs/src/gplx/dbs/engines/tdbs/TdbConnectInfo_tst.java
Normal file
32
140_dbs/src/gplx/dbs/engines/tdbs/TdbConnectInfo_tst.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.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*;
|
||||
public class TdbConnectInfo_tst {
|
||||
@Test public void Full() {
|
||||
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_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_url connectInfo, Io_url url, String format) {
|
||||
Tfds.Eq(((Tdb_url)connectInfo).Url(), url);
|
||||
}
|
||||
}
|
||||
47
140_dbs/src/gplx/dbs/engines/tdbs/TdbDatabase.java
Normal file
47
140_dbs/src/gplx/dbs/engines/tdbs/TdbDatabase.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.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class TdbDatabase {
|
||||
public String Name() {return name;} public void Name_set(String v) {name = v;} private String name = "xmpl";
|
||||
public Io_url DbUrl() {return dbInfo;} Io_url dbInfo;
|
||||
public TdbFileList Files() {return files;} TdbFileList files;
|
||||
public TdbTableList Tables() {return tables;} TdbTableList tables;
|
||||
@gplx.Internal protected boolean IsNew() {return isNew;} @gplx.Internal protected void IsNew_set(boolean v) {isNew = v;} private boolean isNew;
|
||||
@gplx.Internal protected TdbFile MakeFile(Io_url url) {
|
||||
TdbFile rv = TdbFile.new_(FileId_next++, url);
|
||||
files.Add(rv);
|
||||
return rv;
|
||||
}
|
||||
@gplx.Internal protected TdbTable MakeTbl(String name, int fileId) {
|
||||
TdbFile file = files.FetchOrFail(fileId);
|
||||
TdbTable rv = TdbTable.new_(TableId_next++, name, file);
|
||||
tables.Add(rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static TdbDatabase new_(Io_url dbInfo) {TdbDatabase rv = new TdbDatabase(); rv.ctor(dbInfo); return rv;}
|
||||
void ctor(Io_url dbInfo) {
|
||||
this.dbInfo = dbInfo;
|
||||
TdbFile mainFile = TdbFile.new_(TdbFile.MainFileId, dbInfo);
|
||||
files = TdbFileList.new_(dbInfo, mainFile);
|
||||
tables = TdbTableList.new_(dbInfo);
|
||||
}
|
||||
int FileId_next = TdbFile.MainFileId + 1;
|
||||
int TableId_next = 1;
|
||||
// public static Io_url UrlOf(Db_url url) {return Io_url_.new_any_(url.ServerName());}
|
||||
}
|
||||
49
140_dbs/src/gplx/dbs/engines/tdbs/TdbDbLoadMgr.java
Normal file
49
140_dbs/src/gplx/dbs/engines/tdbs/TdbDbLoadMgr.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.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
class TdbDbLoadMgr {
|
||||
public TdbDatabase LoadTbls(Io_url dbInfo) {
|
||||
TdbDatabase db = TdbDatabase.new_(dbInfo);
|
||||
if (!Io_mgr._.ExistsFil(dbInfo)) {
|
||||
db.IsNew_set(true);
|
||||
return db;
|
||||
}
|
||||
DataRdr rdr = MakeDataRdr(dbInfo);
|
||||
LoadTblsByRdr(db, rdr);
|
||||
return db;
|
||||
}
|
||||
public void LoadTbl(TdbDatabase db, TdbTable tbl) {
|
||||
DataRdr rootRdr = MakeDataRdr(tbl.File().Path());
|
||||
LoadTblsByRdr(db, rootRdr);
|
||||
}
|
||||
void LoadTblsByRdr(TdbDatabase db, DataRdr rootRdr) {
|
||||
DataRdr rdr = rootRdr.Subs();
|
||||
while (rdr.MoveNextPeer()) {
|
||||
String name = rdr.NameOfNode();
|
||||
if (String_.Eq(name, TdbFileList.StoreTblName)) db.Files().DataObj_Rdr(rdr);
|
||||
else if (String_.Eq(name, TdbTableList.StoreTableName)) db.Tables().DataObj_Rdr(rdr, db.Files());
|
||||
else db.Tables().FetchOrFail(rdr.NameOfNode()).DataObj_Rdr(rdr);
|
||||
}
|
||||
if (db.Files().Count() == 0) throw Err_.new_("fatal error: db has no files").Add("connectInfo", db.DbUrl());
|
||||
}
|
||||
DataRdr MakeDataRdr(Io_url fil) {
|
||||
String text = Io_mgr._.LoadFilStr(fil);
|
||||
return TdbStores.rdr_(text);
|
||||
}
|
||||
public static TdbDbLoadMgr new_() {return new TdbDbLoadMgr();} TdbDbLoadMgr() {}
|
||||
}
|
||||
101
140_dbs/src/gplx/dbs/engines/tdbs/TdbDbLoadMgr_tst.java
Normal file
101
140_dbs/src/gplx/dbs/engines/tdbs/TdbDbLoadMgr_tst.java
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*;
|
||||
import gplx.stores.*; /*DsvDataRdr*/ import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
||||
public class TdbDbLoadMgr_tst {
|
||||
@Before public void setup() {
|
||||
Io_url dbInfo = Io_url_.mem_fil_("mem/dir/db0.dsv");
|
||||
db = TdbDatabase.new_(dbInfo);
|
||||
wtr = DsvDataWtr_.new_();
|
||||
}
|
||||
TdbDatabase db; TdbDbLoadMgr loadMgr = TdbDbLoadMgr.new_(); TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_();
|
||||
DataRdr rdr; DataWtr wtr;
|
||||
@Test public void ReadDbFiles() {
|
||||
String raw = String_.Concat_lines_crlf
|
||||
( "=======DIF======================, ,\" \",//"
|
||||
, "_files, ,\" \",#"
|
||||
, "==DEF==DIF======================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + "," + StringClassXtn.Key_const + ", ,\" \",$"
|
||||
, "id,url,format, ,\" \",@"
|
||||
, "==DATA=DIF======================, ,\" \",//"
|
||||
, "1,mem/dir/db0.dsv,dsv"
|
||||
, "2,C:\\file.dsv,dsv"
|
||||
);
|
||||
rdr = rdr_(raw);
|
||||
|
||||
db.Files().DataObj_Rdr(rdr);
|
||||
Tfds.Eq(db.Files().Count(), 2);
|
||||
TdbFile file2 = db.Files().FetchOrFail(2);
|
||||
Tfds.Eq(file2.Path().Raw(), "C:\\file.dsv");
|
||||
|
||||
db.Files().DataObj_Wtr(wtr);
|
||||
Tfds.Eq(wtr.XtoStr(), raw);
|
||||
}
|
||||
@Test public void ReadDbTbls() {
|
||||
String raw = String_.Concat_lines_crlf
|
||||
( "=======DIF======================, ,\" \",//"
|
||||
, "_tables, ,\" \",#"
|
||||
, "==DEF==DIF======================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + ",int, ,\" \",$"
|
||||
, "id,name,file_id, ,\" \",@"
|
||||
, "==DATA=DIF======================, ,\" \",//"
|
||||
, "1,tbl1,1"
|
||||
);
|
||||
rdr = rdr_(raw);
|
||||
|
||||
db.Tables().DataObj_Rdr(rdr, db.Files());
|
||||
Tfds.Eq(db.Tables().Count(), 1);
|
||||
TdbTable table = db.Tables().FetchOrFail("tbl1");
|
||||
Tfds.Eq(table.Name(), "tbl1");
|
||||
Tfds.Eq(table.File().Id(), 1);
|
||||
|
||||
db.Tables().DataObj_Wtr(wtr);
|
||||
Tfds.Eq(wtr.XtoStr(), raw);
|
||||
}
|
||||
@Test public void ReadTbl() {
|
||||
String raw = String_.Concat_lines_crlf
|
||||
( "=======DIF======================, ,\" \",//"
|
||||
, "tbl0, ,\" \",#"
|
||||
, "==DEF==DIF======================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + ", ,\" \",$"
|
||||
, "id,name, ,\" \",@"
|
||||
, "==DATA=DIF======================, ,\" \",//"
|
||||
, "0,me"
|
||||
);
|
||||
rdr = rdr_(raw);
|
||||
|
||||
db.MakeTbl("tbl0", TdbFile.MainFileId);
|
||||
db.Tables().FetchOrFail(rdr.NameOfNode()).DataObj_Rdr(rdr);
|
||||
Tfds.Eq(db.Tables().Count(), 1);
|
||||
TdbTable tbl = db.Tables().FetchOrFail("tbl0");
|
||||
Tfds.Eq(tbl.Rows().Count(), 1);
|
||||
|
||||
GfoNde row = tbl.Rows().FetchAt_asGfoNde(0);
|
||||
Tfds.Eq(row.Read("id"), 0);
|
||||
Tfds.Eq(row.Read("name"), "me");
|
||||
|
||||
tbl.DataObj_Wtr(wtr);
|
||||
Tfds.Eq(wtr.XtoStr(), raw);
|
||||
}
|
||||
DataRdr rdr_(String raw) {
|
||||
DataRdr rdr = DsvDataRdr_.dsv_(raw);
|
||||
rdr.MoveNextPeer(); // must move next as cur is not set and ReadProcs assume cur is set
|
||||
return rdr;
|
||||
}
|
||||
}
|
||||
63
140_dbs/src/gplx/dbs/engines/tdbs/TdbDbSaveMgr.java
Normal file
63
140_dbs/src/gplx/dbs/engines/tdbs/TdbDbSaveMgr.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
class TdbDbSaveMgr {
|
||||
public void SaveDb(TdbDatabase db) {
|
||||
for (Object filObj : db.Files()) {
|
||||
TdbFile fil = (TdbFile)filObj;
|
||||
SaveFile(db, fil);
|
||||
}
|
||||
}
|
||||
public void SaveFile(TdbDatabase db, TdbFile fil) {
|
||||
ListAdp tbls = FetchTablesWithSamePath(db, fil.Path());
|
||||
boolean isSaveNeeded = db.IsNew();
|
||||
for (Object tblObj : tbls) {
|
||||
TdbTable tbl = (TdbTable)tblObj;
|
||||
if (tbl.IsDirty()) {
|
||||
isSaveNeeded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isSaveNeeded) {
|
||||
SaveTblsToFile(db, fil, tbls);
|
||||
db.IsNew_set(false);
|
||||
}
|
||||
}
|
||||
void SaveTblsToFile(TdbDatabase db, TdbFile fil, ListAdp tbls) {
|
||||
DataWtr wtr = TdbStores.wtr_();
|
||||
if (fil.Id() == TdbFile.MainFileId) { // if MainFile, save critical Files and Tables data
|
||||
db.Files().DataObj_Wtr(wtr);
|
||||
db.Tables().DataObj_Wtr(wtr);
|
||||
}
|
||||
for (Object tblObj : tbls) {
|
||||
TdbTable tbl = (TdbTable)tblObj;
|
||||
tbl.DataObj_Wtr(wtr);
|
||||
}
|
||||
Io_mgr._.SaveFilStr(fil.Path(), wtr.XtoStr());
|
||||
}
|
||||
ListAdp FetchTablesWithSamePath(TdbDatabase db, Io_url filPath) {
|
||||
ListAdp list = ListAdp_.new_();
|
||||
for (Object tblObj : db.Tables()) {
|
||||
TdbTable tbl = (TdbTable)tblObj;
|
||||
if (tbl.File().Path().Eq (filPath))
|
||||
list.Add(tbl);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
public static TdbDbSaveMgr new_() {return new TdbDbSaveMgr();} TdbDbSaveMgr() {}
|
||||
}
|
||||
77
140_dbs/src/gplx/dbs/engines/tdbs/TdbDbSaveMgr_tst.java
Normal file
77
140_dbs/src/gplx/dbs/engines/tdbs/TdbDbSaveMgr_tst.java
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*;
|
||||
import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
||||
public class TdbDbSaveMgr_tst {
|
||||
@Before public void setup() {
|
||||
Io_url dbInfo = Io_url_.mem_fil_("mem/dir/db0.dsv");
|
||||
db = TdbDatabase.new_(dbInfo);
|
||||
wtr.Clear();
|
||||
} TdbDatabase db; TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_(); DataWtr wtr = DsvDataWtr_.new_();
|
||||
@Test public void WriteDbFils() {
|
||||
String expd = String_.Concat_lines_crlf
|
||||
( ""
|
||||
, ""
|
||||
, "================================, ,\" \",//"
|
||||
, "_files, ,\" \",#"
|
||||
, "================================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + "," + StringClassXtn.Key_const + ", ,\" \",$"
|
||||
, "id,url,format, ,\" \",@"
|
||||
, "================================, ,\" \",//"
|
||||
, "1,mem/dir/db0.dsv,dsv"
|
||||
);
|
||||
db.Files().DataObj_Wtr(wtr);
|
||||
String actl = wtr.XtoStr();
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
@Test public void WriteDbTbls() {
|
||||
String expd = String_.Concat_lines_crlf
|
||||
( ""
|
||||
, ""
|
||||
, "================================, ,\" \",//"
|
||||
, "_tables, ,\" \",#"
|
||||
, "================================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + ",int, ,\" \",$"
|
||||
, "id,name,file_id, ,\" \",@"
|
||||
, "================================, ,\" \",//"
|
||||
);
|
||||
db.Tables().DataObj_Wtr(wtr);
|
||||
String actl = wtr.XtoStr();
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
@Test public void WriteTbl() {
|
||||
String expd = String_.Concat_lines_crlf
|
||||
( ""
|
||||
, ""
|
||||
, "================================, ,\" \",//"
|
||||
, "tbl, ,\" \",#"
|
||||
, "================================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + ", ,\" \",$"
|
||||
, "id,name, ,\" \",@"
|
||||
, "================================, ,\" \",//"
|
||||
);
|
||||
TdbTable tbl = db.MakeTbl("tbl", TdbFile.MainFileId);
|
||||
tbl.Flds().Add("id", IntClassXtn._);
|
||||
tbl.Flds().Add("name", StringClassXtn._);
|
||||
|
||||
tbl.DataObj_Wtr(wtr);
|
||||
String actl = wtr.XtoStr();
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
}
|
||||
47
140_dbs/src/gplx/dbs/engines/tdbs/TdbDelete.java
Normal file
47
140_dbs/src/gplx/dbs/engines/tdbs/TdbDelete.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.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*; import gplx.lists.*; /*GfoNde*/ import gplx.dbs.qrys.*;
|
||||
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.Base_table());
|
||||
ListAdp deleted = ListAdp_.new_();
|
||||
int rv = 0;
|
||||
if (cmd.Where() == Db_qry_.WhereAll) {
|
||||
rv = tbl.Rows().Count();
|
||||
tbl.Rows().Clear();
|
||||
}
|
||||
else {
|
||||
Criteria crt = cmd.Where();
|
||||
for (int i = 0; i < tbl.Rows().Count(); i++) {
|
||||
GfoNde row = tbl.Rows().FetchAt_asGfoNde(i);
|
||||
if (crt.Matches(row))
|
||||
deleted.Add(row);
|
||||
}
|
||||
for (int i = 0; i < deleted.Count(); i++) {
|
||||
GfoNde row = (GfoNde)deleted.FetchAt(i);
|
||||
tbl.Rows().Del(row);
|
||||
rv++;
|
||||
}
|
||||
}
|
||||
if (rv > 0) tbl.IsDirty_set(true);
|
||||
return rv;
|
||||
}
|
||||
public static TdbDeleteWkr new_() {TdbDeleteWkr rv = new TdbDeleteWkr(); return rv;}
|
||||
}
|
||||
81
140_dbs/src/gplx/dbs/engines/tdbs/TdbEngine.java
Normal file
81
140_dbs/src/gplx/dbs/engines/tdbs/TdbEngine.java
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.dbs.qrys.*; import gplx.dbs.sqls.*;
|
||||
public class TdbEngine implements Db_engine {
|
||||
public String Tid() {return Tdb_url.Tid_const;}
|
||||
public Db_url Url() {return url;} private Db_url url;
|
||||
public TdbDatabase Db() {return db;} TdbDatabase db;
|
||||
public void Conn_open() {
|
||||
Tdb_url tdb_url = (Tdb_url)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 New_clone(Db_url url) {
|
||||
TdbEngine rv = new TdbEngine();
|
||||
rv.CtorTdbEngine(url);
|
||||
rv.Conn_open();
|
||||
return rv;
|
||||
}
|
||||
public Object Exec_as_obj(Db_qry qry) {
|
||||
Db_qryWkr wkr = (Db_qryWkr)wkrs.FetchOrFail(qry.Tid());
|
||||
return wkr.Exec(this, qry);
|
||||
}
|
||||
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);
|
||||
return tbl;
|
||||
}
|
||||
public void FlushAll() {
|
||||
saveMgr.SaveDb(db);
|
||||
}
|
||||
public void FlushTbl(TdbTable tbl) {
|
||||
saveMgr.SaveFile(db, tbl.File());
|
||||
}
|
||||
public void Exec_create_tbl(Db_meta_tbl meta) {throw Err_.not_implemented_();}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {throw Err_.not_implemented_();}
|
||||
|
||||
HashAdp wkrs = HashAdp_.new_(); TdbDbLoadMgr loadMgr = TdbDbLoadMgr.new_(); TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_();
|
||||
public static final TdbEngine _ = new TdbEngine();
|
||||
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;}
|
||||
}
|
||||
31
140_dbs/src/gplx/dbs/engines/tdbs/TdbFile.java
Normal file
31
140_dbs/src/gplx/dbs/engines/tdbs/TdbFile.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.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class TdbFile {
|
||||
public int Id() {return id;} int id;
|
||||
public Io_url Path() {return url;} Io_url url;
|
||||
|
||||
public static TdbFile new_(int id, Io_url url) {
|
||||
TdbFile rv = new TdbFile();
|
||||
rv.id = id; rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final int MainFileId = 1;
|
||||
public static TdbFile as_(Object obj) {return obj instanceof TdbFile ? (TdbFile)obj : null;}
|
||||
public static TdbFile cast_(Object obj) {try {return (TdbFile)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, TdbFile.class, obj);}}
|
||||
}
|
||||
63
140_dbs/src/gplx/dbs/engines/tdbs/TdbFileList.java
Normal file
63
140_dbs/src/gplx/dbs/engines/tdbs/TdbFileList.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.lists.*; /*OrderedHash_base*/ import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbFileList extends OrderedHash_base {
|
||||
public TdbFile FetchOrFail(int id) {return TdbFile.as_(FetchOrFail_base(id));}
|
||||
public void Add(TdbFile src) {Add_base(src.Id(), src);}
|
||||
|
||||
Io_url dbInfo;
|
||||
public static TdbFileList new_(Io_url dbInfo, TdbFile mainFile) {
|
||||
TdbFileList rv = new TdbFileList();
|
||||
rv.dbInfo = dbInfo;
|
||||
rv.Add(mainFile);
|
||||
rv.layout = DsvStoreLayout.dsv_full_();
|
||||
return rv;
|
||||
} TdbFileList() {}
|
||||
|
||||
@gplx.Internal protected void DataObj_Wtr(DataWtr wtr) {
|
||||
wtr.InitWtr(DsvStoreLayout.Key_const, layout);
|
||||
wtr.WriteTableBgn(StoreTblName, FldList);
|
||||
for (Object filObj : this) {
|
||||
TdbFile fil = (TdbFile)filObj;
|
||||
wtr.WriteLeafBgn("fil");
|
||||
wtr.WriteData(Fld_id, fil.Id());
|
||||
wtr.WriteData(Fld_path, fil.Path());
|
||||
wtr.WriteData("format", "dsv");
|
||||
wtr.WriteLeafEnd();
|
||||
}
|
||||
wtr.WriteNodeEnd();
|
||||
}
|
||||
@gplx.Internal protected void DataObj_Rdr(DataRdr rdr) {
|
||||
layout = TdbStores.FetchLayout(rdr);
|
||||
this.Clear();
|
||||
DataRdr subRdr = rdr.Subs();
|
||||
while (subRdr.MoveNextPeer()) {
|
||||
int id = subRdr.ReadInt(Fld_id);
|
||||
String url = subRdr.ReadStrOr(Fld_path, dbInfo.Raw());
|
||||
TdbFile file = (id == TdbFile.MainFileId)
|
||||
? TdbFile.new_(TdbFile.MainFileId, dbInfo) // not redundant; prevents error of MainFile in different url/format than connectInfo
|
||||
: TdbFile.new_(id, Io_url_.new_any_(url));
|
||||
this.Add(file);
|
||||
}
|
||||
}
|
||||
DsvStoreLayout layout;
|
||||
public static final String StoreTblName = "_files";
|
||||
static final String Fld_id = "id"; static final String Fld_path = "url";
|
||||
static final GfoFldList FldList = GfoFldList_.new_().Add(Fld_id, IntClassXtn._).Add(Fld_path, StringClassXtn._).Add("format", StringClassXtn._);
|
||||
}
|
||||
34
140_dbs/src/gplx/dbs/engines/tdbs/TdbFlush.java
Normal file
34
140_dbs/src/gplx/dbs/engines/tdbs/TdbFlush.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.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.lists.*; /*GfoNde*/ import gplx.dbs.qrys.*;
|
||||
class TdbFlushWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_flush cmd = Db_qry_flush.cast_(cmdObj);
|
||||
if (Array_.Len(cmd.TableNames()) == 0)
|
||||
engine.FlushAll();
|
||||
else {
|
||||
for (String tblName : cmd.TableNames()) {
|
||||
TdbTable tbl = engine.FetchTbl(tblName);
|
||||
if (tbl.IsDirty()) engine.FlushTbl(tbl);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
public static TdbFlushWkr new_() {TdbFlushWkr rv = new TdbFlushWkr(); return rv;}
|
||||
}
|
||||
119
140_dbs/src/gplx/dbs/engines/tdbs/TdbFlush_tst.java
Normal file
119
140_dbs/src/gplx/dbs/engines/tdbs/TdbFlush_tst.java
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*;
|
||||
import gplx.ios.*; /*IoMgrFxt*/ import gplx.dbs.qrys.*;
|
||||
public class TdbFlush_tst {
|
||||
@Before public void setup() {
|
||||
Io_mgr._.InitEngine_mem();
|
||||
engine = fx_engine.run_MakeEngine(dbPath);
|
||||
}
|
||||
TdbEngine engine; Io_url dbPath = Io_url_.mem_fil_("mem/dir/db0.dsv"); DateAdp time = DateAdp_.parse_gplx("2001-01-01");
|
||||
TdbEngineFxt fx_engine = TdbEngineFxt.new_(); IoMgrFxt fx_io = IoMgrFxt.new_();
|
||||
@Test public void FlushNewDb() {
|
||||
fx_engine.tst_FilesCount(engine, 1);
|
||||
fx_engine.tst_File(engine, 0, TdbFile.MainFileId, Io_url_.mem_fil_("mem/dir/db0.dsv"), "dsv");
|
||||
fx_io.tst_Exists(false, dbPath);
|
||||
|
||||
engine.FlushAll();
|
||||
fx_io.tst_Exists(true, dbPath);
|
||||
}
|
||||
@Test public void IgnoreFlushedDb() {
|
||||
engine.FlushAll();
|
||||
fx_io.tst_Exists(true, dbPath);
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time);
|
||||
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(true, dbPath, time);
|
||||
}
|
||||
@Test public void FlushNewTbl() {
|
||||
engine.FlushAll();
|
||||
fx_engine.run_MakeTbl(engine, "tbl0", TdbFile.MainFileId);
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time);
|
||||
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(false, dbPath, time);
|
||||
}
|
||||
@Test public void IgnoreFlushedTbl() {
|
||||
fx_engine.run_MakeTbl(engine, "tbl0", TdbFile.MainFileId);
|
||||
engine.FlushAll();
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time);
|
||||
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(true, dbPath, time);
|
||||
}
|
||||
@Test public void FlushDirtyTbl() {
|
||||
fx_engine.run_MakeTbl(engine, "tbl0", TdbFile.MainFileId);
|
||||
engine.FlushAll();
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time);
|
||||
|
||||
fx_engine.run_InsertRow(engine, "tbl0", 1);
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(false, dbPath, time);
|
||||
}
|
||||
@Test public void FlushDirtyFilOnly() {
|
||||
Io_url dbPathOther = Io_url_.mem_fil_("mem/dir/db1.dsv");
|
||||
TdbFile filOther = fx_engine.run_MakeFile(engine, dbPathOther); Tfds.Eq(false, Object_.Eq(filOther.Id(), TdbFile.MainFileId));
|
||||
fx_engine.run_MakeTbl(engine, "tbl0", TdbFile.MainFileId); fx_engine.run_MakeTbl(engine, "tbl1", filOther.Id());
|
||||
engine.FlushAll();
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time); fx_io.run_UpdateFilModifiedTime(dbPathOther, time);
|
||||
|
||||
fx_engine.run_InsertRow(engine, "tbl1", 1);
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(true, dbPath, time);
|
||||
fx_io.tst_QueryFilModified(false, dbPathOther, time);
|
||||
}
|
||||
}
|
||||
class TdbEngineFxt {
|
||||
public TdbEngine run_MakeEngine(Io_url url) {
|
||||
Db_url connectInfo = Db_url_.tdb_(url);
|
||||
TdbEngine engine = (TdbEngine)TdbEngine._.New_clone(connectInfo);
|
||||
engine.Conn_open();
|
||||
return engine;
|
||||
}
|
||||
public TdbFile run_MakeFile(TdbEngine engine, Io_url url) {return engine.Db().MakeFile(url);}
|
||||
public TdbTable run_MakeTbl(TdbEngine engine, String tblName, int srcId) {
|
||||
TdbTable rv = engine.Db().MakeTbl(tblName, srcId);
|
||||
rv.Flds().Add("id", IntClassXtn._);
|
||||
return rv;
|
||||
}
|
||||
public void run_InsertRow(TdbEngine engine, String tblName, int idVal) {
|
||||
Db_qry_insert cmd = new Db_qry_insert(tblName);
|
||||
cmd.Arg_("id", idVal);
|
||||
engine.Exec_as_obj(cmd);
|
||||
}
|
||||
|
||||
public void tst_FilesCount(TdbEngine engine, int count) {Tfds.Eq(engine.Db().Files().Count(), count);}
|
||||
public void tst_File(TdbEngine engine, int index, int id, Io_url url, String format) {
|
||||
TdbFile src = engine.Db().Files().FetchOrFail(id);
|
||||
Tfds.Eq(src.Path().Raw(), url.Raw());
|
||||
}
|
||||
public static TdbEngineFxt new_() {return new TdbEngineFxt();} TdbEngineFxt() {}
|
||||
}
|
||||
class IoMgrFxt {
|
||||
public void run_UpdateFilModifiedTime(Io_url url, DateAdp val) {Io_mgr._.UpdateFilModifiedTime(url, val);}
|
||||
public void tst_QueryFilModified(boolean expdMatch, Io_url url, DateAdp expt) {
|
||||
IoItmFil filItem = Io_mgr._.QueryFil(url);
|
||||
DateAdp actl = filItem.ModifiedTime();
|
||||
boolean actlMatch = String_.Eq(expt.XtoStr_gplx(), actl.XtoStr_gplx());
|
||||
Tfds.Eq(expdMatch, actlMatch, expt.XtoStr_gplx() + (expdMatch ? "!=" : "==") + actl.XtoStr_gplx());
|
||||
}
|
||||
public void tst_Exists(boolean expd, Io_url url) {Tfds.Eq(expd, Io_mgr._.ExistsFil(url));}
|
||||
|
||||
public static IoMgrFxt new_() {return new IoMgrFxt();} IoMgrFxt() {}
|
||||
}
|
||||
59
140_dbs/src/gplx/dbs/engines/tdbs/TdbInsert.java
Normal file
59
140_dbs/src/gplx/dbs/engines/tdbs/TdbInsert.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.lists.*; import gplx.dbs.qrys.*; 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.Base_table());
|
||||
tbl.IsDirty_set(true);
|
||||
return cmd.Select() == null
|
||||
? InsertRowsByVals(engine, tbl, cmd)
|
||||
: InsertRowsBySelect(engine, tbl, cmd);
|
||||
}
|
||||
int InsertRowsBySelect(TdbEngine engine, TdbTable tbl, Db_qry_insert insert) {
|
||||
int count = 0;
|
||||
DataRdr rdr = (DataRdr)TdbSelectWkr._.Exec(engine, insert.Select());
|
||||
Sql_select_fld_list insertFlds = insert.Cols(); int insertFldsCount = insertFlds.Count();
|
||||
GfoFldList selectFldsForNewRow = null;
|
||||
try {selectFldsForNewRow = insertFlds.XtoGfoFldLst(tbl);}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to generate flds for new row");}
|
||||
if (insertFldsCount > selectFldsForNewRow.Count()) throw Err_.new_("insert flds cannot exceed selectFlds").Add("insertFlds", insertFlds.XtoStr()).Add("selectFlds", selectFldsForNewRow.XtoStr());
|
||||
while (rdr.MoveNextPeer()) {
|
||||
count++;
|
||||
GfoNde row = GfoNde_.vals_(selectFldsForNewRow, new Object[insertFldsCount]);
|
||||
for (int i = 0; i < insertFldsCount; i++) {
|
||||
row.WriteAt(i, rdr.ReadAt(i)); // NOTE: SELECT and INSERT flds are in same order; ex: INSERT INTO (a, b) SELECT (b, a)
|
||||
}
|
||||
tbl.Rows().Add(row);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
int InsertRowsByVals(TdbEngine engine, TdbTable tbl, Db_qry_insert insert) {
|
||||
GfoNde row = GfoNde_.vals_(tbl.Flds(), new Object[tbl.Flds().Count()]);
|
||||
for (int i = 0; i < insert.Args().Count(); i++) {
|
||||
KeyVal kv = insert.Args().FetchAt(i);
|
||||
Db_arg arg = (Db_arg)kv.Val();
|
||||
row.Write(kv.Key(), arg.Val());
|
||||
}
|
||||
tbl.Rows().Add(row);
|
||||
return 1;
|
||||
}
|
||||
public static TdbInsertWkr new_() {TdbInsertWkr rv = new TdbInsertWkr(); return rv;}
|
||||
}
|
||||
99
140_dbs/src/gplx/dbs/engines/tdbs/TdbSelect.java
Normal file
99
140_dbs/src/gplx/dbs/engines/tdbs/TdbSelect.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.qrys.*; 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.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(), cmd.Limit());
|
||||
if (cmd.GroupBy() != null)
|
||||
rv = TdbGroupByWkr.GroupByExec(cmd, rv, tbl);
|
||||
if (cmd.OrderBy() != null) { // don't use null pattern here; if null ORDER BY, then don't call .Sort on GfoNdeList
|
||||
ComparerAble comparer = Sql_order_by_sorter.new_((Sql_order_by_itm[])cmd.OrderBy().Flds().Xto_ary(Sql_order_by_itm.class));
|
||||
rv.SortBy(comparer);
|
||||
}
|
||||
return GfoNdeRdr_.peers_(rv, false);
|
||||
}
|
||||
GfoNdeList FilterRecords(TdbTable tbl, Criteria crt, int limit) {
|
||||
GfoNdeList rv = GfoNdeList_.new_();
|
||||
int count = 0;
|
||||
for (int i = 0; i < tbl.Rows().Count(); i++) {
|
||||
GfoNde row = (GfoNde)tbl.Rows().FetchAt_asGfoNde(i);
|
||||
if (crt.Matches(row)) rv.Add(row);
|
||||
++count;
|
||||
if (count == limit) break;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static final TdbSelectWkr _ = new TdbSelectWkr(); TdbSelectWkr() {}
|
||||
}
|
||||
class TdbGroupByWkr {
|
||||
public static GfoNdeList GroupByExec(Db_qry_select select, GfoNdeList selectRows, TdbTable tbl) {
|
||||
GfoNdeList rv = GfoNdeList_.new_();
|
||||
OrderedHash groupByHash = OrderedHash_.new_();
|
||||
ListAdp groupByFlds = select.GroupBy().Flds();
|
||||
GfoFldList selectFldsForNewRow = select.Cols().Flds().XtoGfoFldLst(tbl);
|
||||
Sql_select_fld_list selectFlds = select.Cols().Flds();
|
||||
for (int rowIdx = 0; rowIdx < selectRows.Count(); rowIdx++) {
|
||||
GfoNde selectRow = selectRows.FetchAt_asGfoNde(rowIdx);
|
||||
GfoNde groupByRow = FindOrNew(selectFldsForNewRow, groupByFlds, selectRow, groupByHash, rv);
|
||||
for (int i = 0; i < selectFlds.Count(); i++) {
|
||||
Sql_select_fld_base selectFld = selectFlds.FetchAt(i);
|
||||
Object val = groupByRow.Read(selectFld.Alias()); // groupByRow is keyed by Alias; EX: Count(Id) AS CountOf
|
||||
groupByRow.WriteAt(i, selectFld.GroupBy_eval(val, selectRow.Read(selectFld.Fld()), selectFld.ValType()));
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
static GfoNde FindOrNew(GfoFldList selectFldsForNewRow, ListAdp groupByFlds, GfoNde selectRow, OrderedHash groupByRows, GfoNdeList rslt) {
|
||||
int len = groupByFlds.Count();
|
||||
OrderedHash curHash = groupByRows;
|
||||
GfoNde rv = null;
|
||||
for (int i = 0; i < len; i++) {
|
||||
String fld = (String)groupByFlds.FetchAt(i);
|
||||
boolean last = i == len - 1;
|
||||
Object val = selectRow.Read(fld);
|
||||
Object o = curHash.Fetch(val);
|
||||
if (last) {
|
||||
if (o == null) {
|
||||
Object[] valAry = new Object[selectFldsForNewRow.Count()];
|
||||
rv = GfoNde_.vals_(selectFldsForNewRow, valAry);
|
||||
curHash.Add(val, rv);
|
||||
rslt.Add(rv);
|
||||
}
|
||||
else
|
||||
rv = GfoNde_.as_(o);
|
||||
}
|
||||
else {
|
||||
if (o == null) {
|
||||
OrderedHash nextHash = OrderedHash_.new_();
|
||||
curHash.Add(val, nextHash);
|
||||
curHash = nextHash;
|
||||
}
|
||||
else {
|
||||
curHash = (OrderedHash)o;
|
||||
}
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
32
140_dbs/src/gplx/dbs/engines/tdbs/TdbStores.java
Normal file
32
140_dbs/src/gplx/dbs/engines/tdbs/TdbStores.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.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.stores.*;
|
||||
import gplx.stores.xmls.*; /*XmlDataRdr*/
|
||||
import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
||||
import gplx.lists.*; /*GfoNdeRdr*/
|
||||
class TdbStores {
|
||||
public static final String Dsv = "dsv";
|
||||
public static final String Xml = "xml";
|
||||
public static DataRdr rdr_(String text) {return DsvDataRdr_.dsv_(text);}
|
||||
public static DataWtr wtr_() {return DsvDataWtr_.new_();}
|
||||
@gplx.Internal protected static DsvStoreLayout FetchLayout(DataRdr rdr) {
|
||||
GfoNdeRdr ndeRdr = GfoNdeRdr_.as_(rdr); if (ndeRdr == null) return null; // can happen for non-Dsv Rdrs (ex: Xml)
|
||||
return DsvStoreLayout.as_(ndeRdr.UnderNde().EnvVars().Fetch(DsvStoreLayout.Key_const));
|
||||
}
|
||||
}
|
||||
65
140_dbs/src/gplx/dbs/engines/tdbs/TdbTable.java
Normal file
65
140_dbs/src/gplx/dbs/engines/tdbs/TdbTable.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.*; /*GfoNdeList*/ import gplx.stores.*; import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbTable {
|
||||
public int Id() {return id;} int id;
|
||||
public String Name() {return name;} private String name;
|
||||
public GfoFldList Flds() {return flds;} GfoFldList flds = GfoFldList_.new_();
|
||||
public GfoNdeList Rows() {return rows;} GfoNdeList rows = GfoNdeList_.new_();
|
||||
@gplx.Internal protected TdbFile File() {return file;} TdbFile file;
|
||||
@gplx.Internal protected boolean IsLoaded() {return isLoaded;} private boolean isLoaded = true;
|
||||
@gplx.Internal protected boolean IsDirty() {return isDirty;} public void IsDirty_set(boolean v) {isDirty = v;} private boolean isDirty = false;
|
||||
|
||||
public static TdbTable new_(int id, String name, TdbFile file) {TdbTable rv = new TdbTable(); rv.ctor(id, name, file); rv.isDirty = true; return rv;} TdbTable() {}
|
||||
public static TdbTable load_(int id, String name, TdbFile file) {TdbTable rv = new TdbTable(); rv.ctor(id, name, file); rv.isLoaded = false; return rv;}
|
||||
void ctor(int id, String name, TdbFile file) {
|
||||
this.id = id; this.name = name; this.file = file;
|
||||
layout = DsvStoreLayout.dsv_full_();
|
||||
}
|
||||
|
||||
@gplx.Internal protected void DataObj_Wtr(DataWtr wtr) {
|
||||
wtr.InitWtr(DsvStoreLayout.Key_const, layout);
|
||||
wtr.WriteTableBgn(name, flds);
|
||||
for (int rowIdx = 0; rowIdx < rows.Count(); rowIdx++) {
|
||||
GfoNde drow = rows.FetchAt_asGfoNde(rowIdx);
|
||||
wtr.WriteLeafBgn("row");
|
||||
for (int i = 0; i < drow.Flds().Count(); i++)
|
||||
wtr.WriteData(drow.Flds().FetchAt(i).Key(), drow.ReadAt(i));
|
||||
wtr.WriteLeafEnd();
|
||||
}
|
||||
wtr.WriteNodeEnd();
|
||||
isDirty = false;
|
||||
}
|
||||
@gplx.Internal protected void DataObj_Rdr(DataRdr rdr) {
|
||||
layout = TdbStores.FetchLayout(rdr);
|
||||
GfoNdeRdr ndeRdr = GfoNdeRdr_.as_(rdr );
|
||||
if (ndeRdr != null) {
|
||||
if (ndeRdr.UnderNde() == null) throw Err_.new_("ndeRdr.UnderNde is null").Add("name", rdr.NameOfNode());
|
||||
rows = ndeRdr.UnderNde().Subs();
|
||||
flds = ndeRdr.UnderNde().SubFlds();
|
||||
}
|
||||
else { // XmlRdr needs to load each row again...
|
||||
throw Err_.invalid_op_("TableLoad not implemented").Add("rdrType", ClassAdp_.NameOf_obj(rdr)).Add("rdrName", rdr.NameOfNode());
|
||||
}
|
||||
isLoaded = true;
|
||||
}
|
||||
DsvStoreLayout layout;
|
||||
public static TdbTable as_(Object obj) {return obj instanceof TdbTable ? (TdbTable)obj : null;}
|
||||
public static TdbTable cast_(Object obj) {try {return (TdbTable)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, TdbTable.class, obj);}}
|
||||
}
|
||||
63
140_dbs/src/gplx/dbs/engines/tdbs/TdbTableList.java
Normal file
63
140_dbs/src/gplx/dbs/engines/tdbs/TdbTableList.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.lists.*; /*OrderedHash_base*/ import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbTableList extends OrderedHash_base {
|
||||
public TdbTable Fetch(String name) {return TdbTable.as_(Fetch_base(name));}
|
||||
public TdbTable FetchOrFail(String name) {
|
||||
TdbTable rv = TdbTable.as_(Fetch(name)); if (rv == null) throw Err_.new_("could not find table; database file may not exist").Add("table", name);
|
||||
return rv;
|
||||
}
|
||||
public void Add(TdbTable dataTable) {Add_base(dataTable.Name(), dataTable);}
|
||||
|
||||
public static TdbTableList new_(Io_url dbInfo) {
|
||||
TdbTableList rv = new TdbTableList();
|
||||
rv.layout = DsvStoreLayout.dsv_full_();
|
||||
return rv;
|
||||
}
|
||||
@gplx.Internal protected void DataObj_Wtr(DataWtr wtr) {
|
||||
wtr.InitWtr(DsvStoreLayout.Key_const, layout);
|
||||
wtr.WriteTableBgn(StoreTableName, FldList);
|
||||
for (Object tblObj : this) {
|
||||
TdbTable tbl = (TdbTable)tblObj;
|
||||
wtr.WriteLeafBgn("tbl");
|
||||
wtr.WriteData(Fld_id, tbl.Id());
|
||||
wtr.WriteData(Fld_name, tbl.Name());
|
||||
wtr.WriteData(Fld_file_id, tbl.File().Id());
|
||||
wtr.WriteLeafEnd();
|
||||
}
|
||||
wtr.WriteNodeEnd();
|
||||
}
|
||||
@gplx.Internal protected void DataObj_Rdr(DataRdr rdr, TdbFileList files) {
|
||||
layout = TdbStores.FetchLayout(rdr);
|
||||
DataRdr subRdr = rdr.Subs();
|
||||
this.Clear();
|
||||
while (subRdr.MoveNextPeer()) {
|
||||
int id = subRdr.ReadInt(Fld_id);
|
||||
String name = subRdr.ReadStr(Fld_name);
|
||||
int file_id = subRdr.ReadInt(Fld_file_id);
|
||||
TdbFile file = files.FetchOrFail(file_id);
|
||||
TdbTable table = TdbTable.load_(id, name, file);
|
||||
this.Add(table);
|
||||
}
|
||||
}
|
||||
DsvStoreLayout layout;
|
||||
public static final String StoreTableName = "_tables";
|
||||
static final String Fld_id = "id"; static final String Fld_name = "name"; static final String Fld_file_id = "file_id";
|
||||
static final GfoFldList FldList = GfoFldList_.new_().Add(Fld_id, IntClassXtn._).Add(Fld_name, StringClassXtn._).Add(Fld_file_id, IntClassXtn._);
|
||||
}
|
||||
46
140_dbs/src/gplx/dbs/engines/tdbs/TdbUpdate.java
Normal file
46
140_dbs/src/gplx/dbs/engines/tdbs/TdbUpdate.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.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*; import gplx.lists.*; /*GfoNde*/
|
||||
import gplx.dbs.qrys.*;
|
||||
class TdbUpdateWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_update cmd = (Db_qry_update)cmdObj;
|
||||
|
||||
int rv = 0;
|
||||
TdbTable tbl = engine.FetchTbl(cmd.Base_table());
|
||||
Criteria crt = cmd.Where();
|
||||
for (int i = 0; i < tbl.Rows().Count(); i++) {
|
||||
GfoNde row = (GfoNde)tbl.Rows().FetchAt_asGfoNde(i);
|
||||
if (crt.Matches(row)) {
|
||||
UpdateRow(cmd, row);
|
||||
rv++;
|
||||
}
|
||||
}
|
||||
if (rv > 0) tbl.IsDirty_set(true);
|
||||
return rv;
|
||||
}
|
||||
void UpdateRow(Db_qry_update cmd, GfoNde row) {
|
||||
for (int i = 0; i < cmd.Args().Count(); i++) {
|
||||
KeyVal p = (KeyVal)cmd.Args().FetchAt(i);
|
||||
Db_arg prm = (Db_arg)p.Val();
|
||||
row.Write(p.Key(), prm.Val());
|
||||
}
|
||||
}
|
||||
public static TdbUpdateWkr new_() {TdbUpdateWkr rv = new TdbUpdateWkr(); return rv;}
|
||||
}
|
||||
37
140_dbs/src/gplx/dbs/engines/tdbs/Tdb_url.java
Normal file
37
140_dbs/src/gplx/dbs/engines/tdbs/Tdb_url.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Tdb_url 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()
|
||||
));
|
||||
} Tdb_url() {}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Tdb_url rv = new Tdb_url();
|
||||
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 Tdb_url _ = new Tdb_url();
|
||||
}
|
||||
24
140_dbs/src/gplx/dbs/qrys/Db_arg.java
Normal file
24
140_dbs/src/gplx/dbs/qrys/Db_arg.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_arg {
|
||||
public Db_arg(String key, Object val) {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;
|
||||
}
|
||||
69
140_dbs/src/gplx/dbs/qrys/Db_qry__select_in_tbl.java
Normal file
69
140_dbs/src/gplx/dbs/qrys/Db_qry__select_in_tbl.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*; import gplx.core.criterias.*;
|
||||
public class Db_qry__select_in_tbl implements Db_qry {
|
||||
public Db_qry__select_in_tbl(String base_table, String[] select_flds, String[] where_flds, String group_by_sql, String having_sql, String order_by_sql, String limit_sql) {
|
||||
this.base_table = base_table; this.select_flds = select_flds; this.where_flds = where_flds; this.group_by_sql = group_by_sql; this.having_sql = having_sql; this.order_by_sql = order_by_sql; this.limit_sql = limit_sql;
|
||||
}
|
||||
public int Tid() {return Db_qry_.Tid_select_in_tbl;}
|
||||
public boolean Exec_is_rdr() {return true;}
|
||||
public String Base_table() {return base_table;} private final String base_table;
|
||||
public Criteria Where() {return where;} private Criteria where;
|
||||
public String[] Select_flds() {return select_flds;} private final String[] select_flds;
|
||||
private final String[] where_flds;
|
||||
public void Where_sql(String_bldr sb) {
|
||||
if (where_flds == null) return;
|
||||
int where_flds_len = where_flds.length;
|
||||
if (where_flds_len == 0) return;
|
||||
for (int i = 0; i < where_flds_len; ++i) {
|
||||
if (i != 0) sb.Add("AND ");
|
||||
sb.Add(where_flds[i]).Add(" = ? ");
|
||||
}
|
||||
}
|
||||
public String Group_by_sql() {return group_by_sql;} private final String group_by_sql;
|
||||
public String Having_sql() {return having_sql;} private final String having_sql;
|
||||
public String Order_by_sql() {return order_by_sql;} public Db_qry__select_in_tbl Order_by_sql_(String v) {order_by_sql = v; return this;} private String order_by_sql;
|
||||
public String Limit_sql() {return limit_sql;} private final String limit_sql;
|
||||
public String XtoSql() {return Xto_sql();}
|
||||
public String Xto_sql() {
|
||||
synchronized (this) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add("SELECT ");
|
||||
int select_flds_len = select_flds.length;
|
||||
for (int i = 0; i < select_flds_len; ++i) {
|
||||
if (i != 0) sb.Add(",");
|
||||
sb.Add(select_flds[i]);
|
||||
}
|
||||
sb.Add(" FROM ").Add(base_table);
|
||||
if (where_flds != null && where_flds.length != 0) {sb.Add(" WHERE "); Where_sql(sb);}
|
||||
if (group_by_sql != null) sb.Add(group_by_sql);
|
||||
if (having_sql != null) sb.Add(having_sql);
|
||||
if (order_by_sql != null) {sb.Add(" ORDER BY "); sb.Add(order_by_sql);}
|
||||
if (limit_sql != null) sb.Add(limit_sql);
|
||||
return sb.XtoStr();
|
||||
}
|
||||
}
|
||||
public static Db_qry__select_in_tbl new_(String base_table, String[] where_flds, String[] select_flds) {
|
||||
Db_qry__select_in_tbl rv = new Db_qry__select_in_tbl(base_table, select_flds, where_flds, null, null, null, null);
|
||||
rv.where = where_flds.length == 0 ? Db_crt_.Wildcard : Db_crt_.eq_many_(where_flds);
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry__select_in_tbl as_(Object obj) {return obj instanceof Db_qry__select_in_tbl ? (Db_qry__select_in_tbl)obj : null;}
|
||||
public static final String[] Where_flds__all = String_.Ary_empty;
|
||||
}
|
||||
33
140_dbs/src/gplx/dbs/qrys/Db_qry_arg_owner.java
Normal file
33
140_dbs/src/gplx/dbs/qrys/Db_qry_arg_owner.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public interface Db_qry_arg_owner extends Db_qry {
|
||||
Db_qry_arg_owner From_(String tbl);
|
||||
Db_qry_arg_owner Key_arg_(String k, int v);
|
||||
Db_qry_arg_owner Key_arg_(String k, String v);
|
||||
Db_qry_arg_owner Arg_(String k, int v);
|
||||
Db_qry_arg_owner Arg_(String k, long v);
|
||||
Db_qry_arg_owner Arg_(String k, String v);
|
||||
Db_qry_arg_owner Arg_(String k, byte[] v);
|
||||
Db_qry_arg_owner Arg_(String k, DateAdp v);
|
||||
Db_qry_arg_owner Arg_(String k, DecimalAdp v);
|
||||
Db_qry_arg_owner Arg_byte_(String k, byte v);
|
||||
Db_qry_arg_owner Arg_bry_(String k, byte[] v);
|
||||
Db_qry_arg_owner Arg_obj_(String key, Object val);
|
||||
Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid);
|
||||
}
|
||||
31
140_dbs/src/gplx/dbs/qrys/Db_qry_delete.java
Normal file
31
140_dbs/src/gplx/dbs/qrys/Db_qry_delete.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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_delete implements Db_qry {
|
||||
Db_qry_delete(String base_table, Criteria where) {this.base_table = base_table; this.where = where;}
|
||||
public int Tid() {return Db_qry_.Tid_delete;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return base_table;} private final String base_table;
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public Criteria Where() {return where;} private final Criteria where;
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public static Db_qry_delete new_all_(String tbl) {return new Db_qry_delete(tbl, Criteria_.All);}
|
||||
public static Db_qry_delete new_(String tbl, String... where) {return new Db_qry_delete(tbl, Db_crt_.eq_many_(where));}
|
||||
public static Db_qry_delete new_(String tbl, Criteria where) {return new Db_qry_delete(tbl, where);}
|
||||
}
|
||||
44
140_dbs/src/gplx/dbs/qrys/Db_qry_dml_tst.java
Normal file
44
140_dbs/src/gplx/dbs/qrys/Db_qry_dml_tst.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Db_qry_dml_tst {
|
||||
@Test public void Delete_basic() {
|
||||
tst_XtoSql(Db_qry_delete.new_("tbl0", Db_crt_.eq_("fld0", "val0"))
|
||||
, "DELETE FROM tbl0 WHERE fld0='val0'");
|
||||
}
|
||||
@Test public void Insert_basic() {
|
||||
tst_XtoSql(new Db_qry_insert("tbl0").Arg_("id", 0).Arg_("name", "me").Arg_("time", DateAdp_.parse_gplx("2007-12-23"))
|
||||
, "INSERT INTO tbl0 (id, name, time) VALUES (0, 'me', '2007-12-23 00:00:00.000')");
|
||||
}
|
||||
@Test public void Update_basic() {
|
||||
Db_qry_update qry = Db_qry_update.new_();
|
||||
qry.From_("tbl0");
|
||||
qry.Where_(Db_crt_.eq_("id", 0)).Arg_("name", "me");
|
||||
tst_XtoSql(qry, "UPDATE tbl0 SET name='me' WHERE id=0");
|
||||
}
|
||||
@Test public void Update_all() {
|
||||
Db_qry_update qry = Db_qry_update.new_();
|
||||
qry.From_("tbl0");
|
||||
qry.Arg_("id", 1).Arg_("name", "me").Arg_("startTime", DateAdp_.parse_gplx("2007-12-23"));
|
||||
qry.Where_(Criteria_.And(Db_crt_.eq_("id", 0), Db_crt_.mt_("startTime", DateAdp_.parse_gplx("2005-01-01"))));
|
||||
tst_XtoSql(qry, "UPDATE tbl0 SET id=1, name='me', startTime='2007-12-23 00:00:00.000' WHERE (id=0 AND startTime>'2005-01-01 00:00:00.000')");
|
||||
}
|
||||
void tst_XtoSql(Db_qry qry, String expd) {Tfds.Eq(expd, qry.Xto_sql());}
|
||||
}
|
||||
37
140_dbs/src/gplx/dbs/qrys/Db_qry_flush.java
Normal file
37
140_dbs/src/gplx/dbs/qrys/Db_qry_flush.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_flush implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_flush;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return tableNames[0];}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
|
||||
public String[] TableNames() {return tableNames;} private String[] tableNames;
|
||||
|
||||
|
||||
public static Db_qry_flush as_(Object obj) {return obj instanceof Db_qry_flush ? (Db_qry_flush)obj : null;}
|
||||
public static Db_qry_flush cast_(Object obj) {try {return (Db_qry_flush)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, Db_qry_flush.class, obj);}}
|
||||
public static Db_qry_flush new_(String... ary) {
|
||||
Db_qry_flush rv = new Db_qry_flush();
|
||||
rv.tableNames = ary;
|
||||
return rv;
|
||||
} Db_qry_flush() {}
|
||||
}
|
||||
67
140_dbs/src/gplx/dbs/qrys/Db_qry_insert.java
Normal file
67
140_dbs/src/gplx/dbs/qrys/Db_qry_insert.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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_insert implements Db_qry_arg_owner {
|
||||
public Db_qry_insert(String base_table) {this.base_table = base_table;}
|
||||
public int Tid() {return Db_qry_.Tid_insert;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public String Base_table() {return base_table;} private String base_table;
|
||||
public String[] Cols_for_insert() {return cols_for_insert;} private String[] cols_for_insert;
|
||||
public Db_qry_arg_owner From_(String tbl) {base_table = tbl; return this;}
|
||||
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
|
||||
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.Xto_decimal(), Db_val_type.Tid_decimal);}
|
||||
public Db_qry_arg_owner Arg_(String k, DateAdp v) {return Arg_obj_type_(k, v, Db_val_type.Tid_date);}
|
||||
public Db_qry_arg_owner Arg_byte_(String k, byte v) {return Arg_obj_type_(k, v, Db_val_type.Tid_byte);}
|
||||
public Db_qry_arg_owner Arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_qry_arg_owner Arg_(String k, long v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int64);}
|
||||
public Db_qry_arg_owner Arg_(String k, String v) {return Arg_obj_type_(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_bry_(String k, byte[] v) {return Arg_obj_type_(k, v, Db_val_type.Tid_bry);}
|
||||
public Db_qry_arg_owner Arg_(String k, byte[] v) {return Arg_obj_type_(k, String_.new_utf8_(v), Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_obj_(String k, Object v) {return Arg_obj_type_(k, v, Db_val_type.Tid_null);}
|
||||
public Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid) {
|
||||
if (key == Db_meta_fld.Key_null) return this;
|
||||
Db_arg arg = new Db_arg(key, val).Val_tid_(val_tid);
|
||||
args.Add(arg.Key(), arg);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_arg_owner Key_arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_qry_arg_owner Key_arg_(String k, String v) {return Arg_obj_type_(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_qry_select Select() {return select;} Db_qry_select select;
|
||||
public Db_qry_insert Select_(Db_qry_select qry) {this.select = qry; return this;}
|
||||
public Db_qry_insert Cols_(String... ary) {
|
||||
if (cols == null) cols = Sql_select_fld_list.new_();
|
||||
for (String fld : ary)
|
||||
cols.Add(Sql_select_fld_.new_fld(Sql_select_fld_base.Tbl_null, fld, fld));
|
||||
return this;
|
||||
}
|
||||
public Sql_select_fld_list Cols() {return cols;} private Sql_select_fld_list cols;
|
||||
|
||||
public static Db_qry_insert new_() {return new Db_qry_insert();} Db_qry_insert() {}
|
||||
public static Db_qry_insert new_(String tbl, String... keys) {
|
||||
Db_qry_insert rv = Db_qry_insert.new_();
|
||||
rv.base_table = tbl;
|
||||
rv.cols_for_insert = keys;
|
||||
int len = keys.length;
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv.Arg_obj_(keys[i], null);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
130
140_dbs/src/gplx/dbs/qrys/Db_qry_select.java
Normal file
130
140_dbs/src/gplx/dbs/qrys/Db_qry_select.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_select implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_select;}
|
||||
public boolean Exec_is_rdr() {return true;}
|
||||
public String Base_table() {return from.BaseTable().TblName();}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public DataRdr Exec_qry_as_rdr(Db_conn conn) {return conn.Exec_qry_as_rdr(this);}
|
||||
public GfoNde ExecRdr_nde(Db_conn conn) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {return GfoNde_.rdr_(Exec_qry_as_rdr(conn));} finally {rdr.Rls();}
|
||||
}
|
||||
public Object ExecRdr_val(Db_conn conn) {
|
||||
DataRdr rdr = Exec_qry_as_rdr(conn);
|
||||
try {
|
||||
Object rv = null;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
rv = rdr.Read(cols.Flds().FetchAt(0).Fld()); // NOTE: need to access from flds for tdb
|
||||
}
|
||||
return rv;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
public static Object Rdr_to_val(DataRdr rdr) {
|
||||
try {
|
||||
Object rv = null;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
rv = rdr.ReadAt(0);
|
||||
}
|
||||
return rv;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
|
||||
public Sql_from From() {return from;} Sql_from from;
|
||||
public Db_qry_select From_(String tblName) {return From_(tblName, null);}
|
||||
public Db_qry_select From_(String tblName, String alias) {
|
||||
if (from != null) throw Err_.new_("super table already defined").Add("from", from.Tbls().Count());
|
||||
from = Sql_from.new_(Sql_tbl_src.new_().JoinType_(Sql_join_itmType.From).TblName_(tblName).Alias_(alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Join_(String name, String alias, Sql_join_itm... ary) {
|
||||
if (from == null) throw Err_.new_("super table is not defined");
|
||||
Sql_tbl_src tbl = Sql_tbl_src.new_().JoinType_(Sql_join_itmType.Inner).TblName_(name).Alias_(alias);
|
||||
for (Sql_join_itm itm : ary)
|
||||
tbl.JoinLinks().Add(itm);
|
||||
from.Tbls().Add(tbl);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Sql_select Cols() {return cols;} Sql_select cols = Sql_select.All;
|
||||
public String[] Cols_ary() {return cols.Flds().To_str_ary();}
|
||||
public Db_qry_select Cols_all_() {return this;}
|
||||
public Db_qry_select Cols_alias_(String expr, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(expr, alias);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_(String... ary) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
for (String itm : ary)
|
||||
cols.Add(itm);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_max(String fld) {return Cols_groupBy_max(fld, fld);}
|
||||
public Db_qry_select Cols_groupBy_max(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_.new_max(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_min(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_.new_min(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_count(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_.new_count(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_sum(String fld) {return Cols_groupBy_sum(fld, fld);}
|
||||
public Db_qry_select Cols_groupBy_sum(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_.new_sum(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Criteria Where() {return where;} public Db_qry_select Where_(Criteria crt) {where = crt; return this;} Criteria where;
|
||||
public Sql_order_by OrderBy() {return orderBy;} Sql_order_by orderBy = null;
|
||||
public Db_qry_select OrderBy_(String fieldName, boolean ascending) {
|
||||
Sql_order_by_itm item = Sql_order_by_itm.new_(fieldName, ascending);
|
||||
orderBy = Sql_order_by.new_(item);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select OrderBy_asc_(String fieldName) {return OrderBy_(fieldName, true);}
|
||||
public Db_qry_select OrderBy_many_(String... fldNames) {
|
||||
Sql_order_by_itm[] ary = new Sql_order_by_itm[fldNames.length];
|
||||
for (int i = 0; i < fldNames.length; i++)
|
||||
ary[i] = Sql_order_by_itm.new_(fldNames[i], true);
|
||||
orderBy = Sql_order_by.new_(ary);
|
||||
return this;
|
||||
}
|
||||
public Sql_group_by GroupBy() {return groupBy;} Sql_group_by groupBy = null;
|
||||
public Db_qry_select GroupBy_(String... flds) {
|
||||
if (groupBy != null) throw Err_.new_("group by already defined").Add("group", groupBy);
|
||||
groupBy = Sql_group_by.new_(flds);
|
||||
return this;
|
||||
}
|
||||
public String Indexed_by() {return indexed_by;} public Db_qry_select Indexed_by_(String v) {indexed_by = v; return this;} private String indexed_by;
|
||||
public Db_qry_select Distinct_() {cols.Distinct_set(true); return this;}
|
||||
public int Limit() {return limit;} int limit = -1; public static final int Limit_disabled = -1;
|
||||
public Db_qry_select Limit_(int v) {this.limit = v; return this;}
|
||||
|
||||
public static Db_qry_select new_() {return new Db_qry_select();} Db_qry_select() {}
|
||||
}
|
||||
89
140_dbs/src/gplx/dbs/qrys/Db_qry_select_tst.java
Normal file
89
140_dbs/src/gplx/dbs/qrys/Db_qry_select_tst.java
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_select_tst {
|
||||
@Before public void setup() {
|
||||
cmd = Db_qry_select.new_();
|
||||
} Db_qry_select cmd; String expd;
|
||||
@Test public void Basic() {
|
||||
cmd.Cols_("fld0", "fld1").From_("tbl0");
|
||||
expd = "SELECT fld0, fld1 FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderDoesNotMatter() {
|
||||
cmd.From_("tbl0").Cols_("fld0", "fld1");
|
||||
expd = "SELECT fld0, fld1 FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void DefaultAllFields() {
|
||||
cmd.From_("tbl0");
|
||||
expd = "SELECT * FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Where() {
|
||||
cmd.From_("tbl0").Where_(Db_crt_.eq_("fld0", 0));
|
||||
expd = "SELECT * FROM tbl0 WHERE fld0=0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Join() {
|
||||
cmd.From_("tbl0").Join_("tbl1", "t1", Sql_join_itm.new_("fld1", "tbl0", "fld0"));
|
||||
expd = "SELECT * FROM tbl0 INNER JOIN tbl1 t1 ON tbl0.fld0=t1.fld1";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderBy() {
|
||||
cmd.From_("tbl0").OrderBy_("fld0", true);
|
||||
expd = "SELECT * FROM tbl0 ORDER BY fld0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderByMany() {
|
||||
cmd.From_("tbl0").OrderBy_many_("fld0", "fld1");
|
||||
expd = "SELECT * FROM tbl0 ORDER BY fld0, fld1";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Limit() {
|
||||
cmd.From_("tbl0").Limit_(10);
|
||||
expd = "SELECT * FROM tbl0 LIMIT 10";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
// @Test public void GroupBy() {
|
||||
// cmd.From_("tbl0").groupBy_("fld0", "fld1");
|
||||
// expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1";
|
||||
// Tfds.Eq(cmd.XtoStr(), expd);
|
||||
// }
|
||||
// @Test public void Union() {
|
||||
// cmd.From_("tbl0").select("fld0").union_(qry2.from("tbl1").select("fld0"));
|
||||
// cmd.From_("tbl0").select("fld0").union_().from("tbl1").select("fld0"); // feasible, but will be bad later when trying to access Db_qry_select props
|
||||
// expd = "SELECT fld0 FROM tbl0 UNION SELECT fld0 FROM tbl1";
|
||||
// Tfds.Eq(cmd.XtoStr(), expd);
|
||||
// }
|
||||
// @Test public void Having() {
|
||||
// cmd.From_("tbl0").groupBy_("fld0", "fld1");
|
||||
// expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1 HAVING Count(fld0) > 1";
|
||||
// Tfds.Eq(cmd.XtoStr(), expd);
|
||||
// }
|
||||
void tst_XtoStr(Db_qry qry, String expd) {Tfds.Eq(expd, cmd.Xto_sql());}
|
||||
}
|
||||
82
140_dbs/src/gplx/dbs/qrys/Db_qry_sql.java
Normal file
82
140_dbs/src/gplx/dbs/qrys/Db_qry_sql.java
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_sql implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_sql;}
|
||||
public boolean Exec_is_rdr() {return isReader;} private boolean isReader;
|
||||
public String Base_table() {throw Err_.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);}
|
||||
static Db_qry_sql sql_(String sql) {
|
||||
Db_qry_sql rv = new Db_qry_sql();
|
||||
rv.sql = sql; rv.isReader = false;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_sql rdr_(String sql) {
|
||||
Db_qry_sql rv = new Db_qry_sql();
|
||||
rv.sql = sql; rv.isReader = true;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_sql as_(Object obj) {return obj instanceof Db_qry_sql ? (Db_qry_sql)obj : null;}
|
||||
public static Db_qry_sql cast_(Object obj) {try {return (Db_qry_sql)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, Db_qry_sql.class, obj);}}
|
||||
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/gplx/dbs/qrys/Db_qry_sql_tst.java
Normal file
47
140_dbs/src/gplx/dbs/qrys/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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Db_qry_sql_tst {
|
||||
@Before public void init() {fxt.Clear();} private Db_qry_sql_fxt fxt = new Db_qry_sql_fxt();
|
||||
@Test public void Insert() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_insert.new_("tbl", "k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9")
|
||||
, Object_.Ary(123, Bool_.Y, 1.23d, 123L, 123f, Byte_ascii.Num_1, "123", DateAdp_.parse_iso8561("1981-04-05T14:30:30"), DecimalAdp_.parse_("1.23"))
|
||||
, "INSERT INTO tbl (k1, k2, k3, k4, k5, k6, k7, k8, k9) VALUES (123, 1, 1.23, 123, 123, 1, '123', '1981-04-05 14:30:30.000', 1.23)"
|
||||
);
|
||||
}
|
||||
@Test public void Update() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_update.new_("tbl", String_.Ary("k1", "k2"), "k3", "k4")
|
||||
, Object_.Ary("v3", "v4", "v1", "v2")
|
||||
, "UPDATE tbl SET k3='v3', k4='v4' WHERE (k1='v1' AND k2='v2')"
|
||||
);
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_delete.new_("tbl", String_.Ary("k1", "k2"))
|
||||
, Object_.Ary("v1", "v2")
|
||||
, "DELETE FROM tbl WHERE (k1='v1' AND k2='v2')"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Db_qry_sql_fxt {
|
||||
public void Clear() {}
|
||||
public void Test_qry(Db_qry qry, Object[] vals, String expd) {Tfds.Eq(expd, Db_qry_sql.Gen_sql(qry, vals));}
|
||||
}
|
||||
61
140_dbs/src/gplx/dbs/qrys/Db_qry_update.java
Normal file
61
140_dbs/src/gplx/dbs/qrys/Db_qry_update.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.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_update implements Db_qry_arg_owner {
|
||||
public int Tid() {return Db_qry_.Tid_update;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public String Base_table() {return base_table;} private String base_table;
|
||||
public String[] Cols_for_update() {return cols_for_update;} private String[] cols_for_update;
|
||||
public Criteria Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = crt; return this;} private Criteria where;
|
||||
public Db_qry_arg_owner From_(String tbl) {base_table = tbl; return this;}
|
||||
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
|
||||
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.Xto_decimal(), Db_val_type.Tid_decimal);}
|
||||
public Db_qry_arg_owner Arg_(String k, DateAdp v) {return Arg_obj_type_(k, v, Db_val_type.Tid_date);}
|
||||
public Db_qry_arg_owner Arg_byte_(String k, byte v) {return Arg_obj_type_(k, v, Db_val_type.Tid_byte);}
|
||||
public Db_qry_arg_owner Arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_qry_arg_owner Arg_(String k, long v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int64);}
|
||||
public Db_qry_arg_owner Arg_(String k, String v) {return Arg_obj_type_(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_bry_(String k, byte[] v) {return Arg_obj_type_(k, v, Db_val_type.Tid_bry);}
|
||||
public Db_qry_arg_owner Arg_(String k, byte[] v) {return Arg_obj_type_(k, String_.new_utf8_(v), Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_obj_(String k, Object v) {return Arg_obj_type_(k, v, Db_val_type.Tid_null);}
|
||||
public Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid) {
|
||||
if (key == Db_meta_fld.Key_null) return this;
|
||||
Db_arg arg = new Db_arg(key, val).Val_tid_(val_tid);
|
||||
args.Add(arg.Key(), arg);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_arg_owner Key_arg_(String k, int v) {return Key_arg_obj_(k, v);}
|
||||
public Db_qry_arg_owner Key_arg_(String k, String v) {return Key_arg_obj_(k, v);}
|
||||
private Db_qry_arg_owner Key_arg_obj_(String k, Object v) {
|
||||
Criteria crt = Db_crt_.eq_(k, v);
|
||||
where = where == null ? crt : Criteria_.And(where, crt);
|
||||
return this;
|
||||
}
|
||||
public static Db_qry_update new_() {return new Db_qry_update();} Db_qry_update() {}
|
||||
public static Db_qry_update new_(String tbl, String[] where, String... update) {
|
||||
Db_qry_update rv = Db_qry_.update_(tbl, Db_crt_.eq_many_(where));
|
||||
rv.cols_for_update = update;
|
||||
int len = update.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
rv.Arg_obj_(update[i], null);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
159
140_dbs/src/gplx/dbs/qrys/Db_stmt_cmd.java
Normal file
159
140_dbs/src/gplx/dbs/qrys/Db_stmt_cmd.java
Normal file
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.sqls.*;
|
||||
public class Db_stmt_cmd implements Db_stmt {
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private Db_engine engine;
|
||||
private PreparedStatement stmt = null;
|
||||
private String sql; private int val_idx = 0;
|
||||
public Db_stmt_cmd(Db_engine engine, Db_qry qry) {Ctor_stmt(engine, qry);}
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {
|
||||
this.engine = engine;
|
||||
sql = qry.Tid() == Db_qry_.Tid_select_in_tbl ? ((Db_qry__select_in_tbl)qry).Xto_sql() : Sql_qry_wtr_.I.Xto_str(qry, true);
|
||||
Reset_stmt();
|
||||
}
|
||||
public Db_stmt Reset_stmt() {
|
||||
stmt = (PreparedStatement)engine.New_stmt_prep_as_obj(sql);
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setByte(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "byte", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setInt(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "int", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setLong(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "long", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setFloat(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "float", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setDouble(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "double", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setBigDecimal(++val_idx, v.Xto_decimal());} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "decimal", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setBytes(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "byte[]", v.length, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_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, Key_na, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setString(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "String", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {stmt.setBinaryStream(++val_idx, (java.io.InputStream)v.Under(), (int)rdr_len);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = stmt.execute(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.err_(e, "db_stmt.insert: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));
|
||||
}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.err_(e, "db_stmt.update: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));
|
||||
}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.err_(e, "db_stmt.insert: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));
|
||||
}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
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_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.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;
|
||||
try {stmt.clearBatch();}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to clear parameters;", sql);}
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
if (stmt == null) return; // Null instance
|
||||
try {
|
||||
if (stmt.getConnection().isClosed()) return; // do not close stmt if connection is already closed; throws null error; DATE:2015-02-11
|
||||
stmt.close();
|
||||
}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to close command: {0}", sql);}
|
||||
}
|
||||
}
|
||||
156
140_dbs/src/gplx/dbs/qrys/Db_stmt_sql.java
Normal file
156
140_dbs/src/gplx/dbs/qrys/Db_stmt_sql.java
Normal file
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.engines.*;
|
||||
public class Db_stmt_sql implements Db_stmt {// used for formatting SQL statements; not used for actual insert into database
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private final ListAdp args = ListAdp_.new_();
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
private final Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {}
|
||||
public Db_conn Conn() {return conn;} public void Conn_(Db_conn v) {this.conn = v;} Db_conn conn;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(k, Byte_.Xto_str(v));} catch (Exception e) {throw 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, Key_na, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(k, Int_.Xto_str(v));} catch (Exception e) {throw 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, Key_na, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(k, Long_.Xto_str(v));} catch (Exception e) {throw 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, Key_na, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(k, Float_.Xto_str(v));} catch (Exception e) {throw 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, Key_na, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(k, Double_.Xto_str(v));} catch (Exception e) {throw 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, Key_na, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(k, 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 Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {// HACK: convert to String b/c tdb does not support byte[]
|
||||
try {Add(k, Val_str_wrap(String_.new_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 Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_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, Key_na, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(k, Val_str_wrap(v));} catch (Exception e) {throw Err_.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(Key_na, bfr.Xto_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
private String Val_str_wrap(String v) {
|
||||
return "'" + String_.Replace(v, "'", "\\'") + "'";
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())) != 0; return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = conn.Exec_qry_as_rdr(Db_qry_sql.rdr_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Db_rdr Exec_select_as_rdr() {throw Err_.not_implemented_();}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {this.Clear();}
|
||||
public void Add(String k, String v) {
|
||||
if (k == Db_meta_fld.Key_null) return; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
args.Add(v);
|
||||
}
|
||||
public String Xto_sql() {
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, (Object[])args.Xto_ary_and_clear(Object.class));
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
public int Args_len() {return args.Count();}
|
||||
public String Args_get_at(int i) {return (String)args.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);
|
||||
int pos_prv = 0;
|
||||
tmp_bfr.Clear();
|
||||
while (true) {
|
||||
int pos_cur = Bry_finder.Find_fwd(src, Byte_ascii.Question, pos_prv);
|
||||
if (pos_cur == Bry_.NotFound) break;
|
||||
tmp_bfr.Add_mid(src, pos_prv, pos_cur);
|
||||
tmp_bfr.Add_byte(Byte_ascii.Tilde).Add_byte(Byte_ascii.Curly_bgn);
|
||||
tmp_bfr.Add_int_variable(arg_idx++);
|
||||
tmp_bfr.Add_byte(Byte_ascii.Curly_end);
|
||||
pos_prv = pos_cur + 1;
|
||||
}
|
||||
tmp_bfr.Add_mid(src, pos_prv, src.length);
|
||||
tmp_fmtr.Fmt_(tmp_bfr.Xto_bry_and_clear());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
29
140_dbs/src/gplx/dbs/qrys/Db_stmt_sql_tst.java
Normal file
29
140_dbs/src/gplx/dbs/qrys/Db_stmt_sql_tst.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Db_stmt_sql_tst {
|
||||
@Before public void init() {}
|
||||
@Test public void Basic() {
|
||||
Db_stmt_sql stmt = new Db_stmt_sql();
|
||||
stmt.Parse(null, "UPDATE tbl_0 SET col_0 = ? WHERE col_1 = ?");
|
||||
stmt.Add("col_0", "1");
|
||||
stmt.Add("col_1", "2");
|
||||
Tfds.Eq("UPDATE tbl_0 SET col_0 = 1 WHERE col_1 = 2", stmt.Xto_sql());
|
||||
}
|
||||
}
|
||||
35
140_dbs/src/gplx/dbs/qrys/Db_val_type.java
Normal file
35
140_dbs/src/gplx/dbs/qrys/Db_val_type.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_val_type {
|
||||
public static final byte // not serialized
|
||||
Tid_null = 0
|
||||
, Tid_bool = 1
|
||||
, Tid_byte = 2
|
||||
, Tid_int32 = 3
|
||||
, Tid_int64 = 4
|
||||
, Tid_date = 5
|
||||
, Tid_decimal = 6
|
||||
, Tid_float = 7
|
||||
, Tid_double = 8
|
||||
, Tid_bry = 9
|
||||
, Tid_varchar = 10
|
||||
, Tid_nvarchar = 11
|
||||
, Tid_rdr = 12
|
||||
;
|
||||
}
|
||||
23
140_dbs/src/gplx/dbs/sqls/Db_fld.java
Normal file
23
140_dbs/src/gplx/dbs/sqls/Db_fld.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_fld {
|
||||
public Db_fld(String name, byte type_tid) {this.name = name; this.type_tid = type_tid;}
|
||||
public String Name() {return name;} public Db_fld Name_(String v) {name = v; return this;} private String name;
|
||||
public byte Type_tid() {return type_tid;} public Db_fld Type_tid_(byte v) {type_tid = v; return this;} private byte type_tid;
|
||||
}
|
||||
36
140_dbs/src/gplx/dbs/sqls/Db_obj_ary_crt.java
Normal file
36
140_dbs/src/gplx/dbs/sqls/Db_obj_ary_crt.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Db_obj_ary_crt implements gplx.core.criterias.Criteria {
|
||||
public byte Tid() {return Criteria_.Tid_db_obj_ary;}
|
||||
public Db_fld[] Flds() {return flds;} public Db_obj_ary_crt Flds_(Db_fld[] v) {this.flds = v; return this;} private Db_fld[] flds;
|
||||
public Object[][] Vals() {return vals;} public void Vals_(Object[][] v) {this.vals = v;} private Object[][] vals;
|
||||
public void Val_from_args(HashAdp args) {throw Err_.not_implemented_();}
|
||||
public void Val_as_obj_(Object v) {throw Err_.not_implemented_();}
|
||||
public boolean Matches(Object obj) {return false;}
|
||||
public String XtoStr() {return "";}
|
||||
public static Db_obj_ary_crt new_(Db_fld... flds) {return new Db_obj_ary_crt().Flds_(flds);}
|
||||
public static Db_obj_ary_crt new_by_type(byte type_tid, String... names) {
|
||||
int len = names.length;
|
||||
Db_fld[] flds = new Db_fld[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
flds[i] = new Db_fld(names[i], type_tid);
|
||||
return new Db_obj_ary_crt().Flds_(flds);
|
||||
}
|
||||
}
|
||||
42
140_dbs/src/gplx/dbs/sqls/Db_obj_ary_tst.java
Normal file
42
140_dbs/src/gplx/dbs/sqls/Db_obj_ary_tst.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.core.strings.*; 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() {
|
||||
fxt.Init_fld("fld_0", ClassAdp_.Tid_int).Init_fld("fld_1", ClassAdp_.Tid_int).Init_vals(1, 10).Init_vals(2, 20).Test_sql("(fld_0=1 AND fld_1=10) OR (fld_0=2 AND fld_1=20)");
|
||||
}
|
||||
@Test public void Str() {
|
||||
fxt.Init_fld("fld_0", ClassAdp_.Tid_int).Init_fld("fld_1", ClassAdp_.Tid_str).Init_vals(1, "a").Init_vals(2, "b").Test_sql("(fld_0=1 AND fld_1='a') OR (fld_0=2 AND fld_1='b')");
|
||||
}
|
||||
}
|
||||
class Db_obj_ary_fxt {
|
||||
private Db_obj_ary_crt crt = new Db_obj_ary_crt();
|
||||
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_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));
|
||||
cmd_wtr.Append_db_obj_ary(sb, crt);
|
||||
Tfds.Eq(expd, sb.Xto_str_and_clear());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,9 @@ 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.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
interface Db_sqlbldr {}
|
||||
class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
public 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 ");
|
||||
@@ -25,7 +25,9 @@ class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
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(idx.Name());
|
||||
tmp_bfr.Add_str_ascii(" ON ");
|
||||
tmp_bfr.Add_str_ascii(idx.Tbl());
|
||||
tmp_bfr.Add_str_ascii(" (");
|
||||
String[] flds = idx.Flds();
|
||||
int flds_len = flds.length;
|
||||
@@ -60,7 +62,7 @@ class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
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_int: tmp_bfr.Add_str_ascii("integer"); break; // NOTE: must be integer, not int, else "int PRIMARY KEY AUTONUMBER" will fail; DATE:2015-02-12
|
||||
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;
|
||||
@@ -70,4 +72,5 @@ class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
default: throw Err_.unhandled(tid);
|
||||
}
|
||||
}
|
||||
public static final Db_sqlbldr__sqlite I = new Db_sqlbldr__sqlite(); Db_sqlbldr__sqlite() {}
|
||||
}
|
||||
@@ -15,13 +15,13 @@ 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.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
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);"
|
||||
fxt.Test_create_idx(Db_meta_idx.new_unique_by_tbl("tbl_name", "idx_name", "fld_1", "fld_2")
|
||||
, "CREATE UNIQUE INDEX IF NOT EXISTS tbl_name__idx_name ON tbl_name (fld_1, fld_2);"
|
||||
);
|
||||
}
|
||||
@Test public void Tbl_basic() {
|
||||
@@ -39,10 +39,10 @@ public class Db_sqlbldr_tst {
|
||||
fxt.Test_create_tbl(Db_meta_tbl.new_("tbl_name", flds.To_fld_ary())
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "CREATE TABLE tbl_name"
|
||||
, "( fld_int_pkey int NOT NULL PRIMARY KEY"
|
||||
, "( fld_int_pkey integer NOT NULL PRIMARY KEY"
|
||||
, ", fld_bool boolean NOT NULL"
|
||||
, ", fld_short smallint NOT NULL"
|
||||
, ", fld_int int NOT NULL"
|
||||
, ", fld_int integer NOT NULL"
|
||||
, ", fld_long bigint NOT NULL"
|
||||
, ", fld_float float NOT NULL"
|
||||
, ", fld_double double NOT NULL"
|
||||
@@ -54,7 +54,7 @@ public class Db_sqlbldr_tst {
|
||||
}
|
||||
}
|
||||
class Db_sqlbldr_fxt {
|
||||
private Db_sqlbldr__sqlite sqlbldr = new Db_sqlbldr__sqlite();
|
||||
private Db_sqlbldr__sqlite sqlbldr = Db_sqlbldr__sqlite.I;
|
||||
public void Test_create_idx(Db_meta_idx idx, String expd) {
|
||||
Tfds.Eq(expd, sqlbldr.Bld_create_idx(idx));
|
||||
}
|
||||
27
140_dbs/src/gplx/dbs/sqls/Sql_from.java
Normal file
27
140_dbs/src/gplx/dbs/sqls/Sql_from.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public 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() {}
|
||||
}
|
||||
28
140_dbs/src/gplx/dbs/sqls/Sql_group_by.java
Normal file
28
140_dbs/src/gplx/dbs/sqls/Sql_group_by.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public 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() {}
|
||||
}
|
||||
35
140_dbs/src/gplx/dbs/sqls/Sql_join_itm.java
Normal file
35
140_dbs/src/gplx/dbs/sqls/Sql_join_itm.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*;
|
||||
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;
|
||||
}
|
||||
}
|
||||
31
140_dbs/src/gplx/dbs/sqls/Sql_join_itmType.java
Normal file
31
140_dbs/src/gplx/dbs/sqls/Sql_join_itmType.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public 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")
|
||||
;
|
||||
}
|
||||
28
140_dbs/src/gplx/dbs/sqls/Sql_order_by.java
Normal file
28
140_dbs/src/gplx/dbs/sqls/Sql_order_by.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public 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() {}
|
||||
}
|
||||
31
140_dbs/src/gplx/dbs/sqls/Sql_order_by_itm.java
Normal file
31
140_dbs/src/gplx/dbs/sqls/Sql_order_by_itm.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public 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() {}
|
||||
}
|
||||
@@ -15,42 +15,9 @@ 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 {
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.lists.*;
|
||||
public 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;
|
||||
@@ -70,4 +37,4 @@ class Sql_order_by_sorter implements ComparerAble {
|
||||
rv.items = items;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ 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.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*;
|
||||
public interface Sql_qry_wtr {
|
||||
String Xto_str(Db_qry qry, boolean prepare);
|
||||
@@ -15,7 +15,7 @@ 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.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
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();}
|
||||
@@ -15,21 +15,24 @@ 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.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*; import gplx.core.criterias.*;
|
||||
class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
import gplx.dbs.qrys.*;
|
||||
public 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());
|
||||
synchronized (sb) {
|
||||
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) {
|
||||
@@ -89,6 +92,7 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
this.Xto_sql_col(sb, fld.XtoSql());
|
||||
}
|
||||
Bld_clause_from(sb, cmd.From());
|
||||
Bld_indexed_by(sb, cmd.Indexed_by());
|
||||
Bld_where(sb, cmd.Where());
|
||||
Bld_select_group_by(sb, cmd.GroupBy());
|
||||
Bld_select_order_by(sb, cmd.OrderBy());
|
||||
@@ -131,6 +135,10 @@ class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
}
|
||||
}
|
||||
}
|
||||
private void Bld_indexed_by(String_bldr sb, String idx_name) {
|
||||
if (idx_name == null) return;
|
||||
sb.Add(" INDEXED BY ").Add(idx_name);
|
||||
}
|
||||
private void Xto_sql_col(String_bldr sb, Object obj) {
|
||||
if (obj == null) throw Err_.null_("ColName");
|
||||
sb.Add_obj(obj); // FIXME: options for bracketing; ex: [name]
|
||||
99
140_dbs/src/gplx/dbs/sqls/Sql_qry_wtr_ansi_tst.java
Normal file
99
140_dbs/src/gplx/dbs/sqls/Sql_qry_wtr_ansi_tst.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_qry_wtr_ansi_tst {
|
||||
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")
|
||||
, "INSERT INTO people (id, name) VALUES (1, 'me')"
|
||||
);
|
||||
}
|
||||
@Test public void Delete() {
|
||||
Criteria crt = Db_crt_.eq_("id", 1);
|
||||
tst_XtoSql
|
||||
( Db_qry_.delete_("people", crt)
|
||||
, "DELETE FROM people WHERE id=1"
|
||||
);
|
||||
}
|
||||
@Test public void Update() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.update_("people", Db_crt_.eq_("id", 1)).Arg_("name", "me")
|
||||
, "UPDATE people SET name='me' WHERE id=1"
|
||||
);
|
||||
}
|
||||
@Test public void SelectAll() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people")
|
||||
, "SELECT * FROM people"
|
||||
);
|
||||
}
|
||||
@Test public void SelectFlds() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().Cols_("id", "name").From_("people")
|
||||
, "SELECT id, name FROM people"
|
||||
);
|
||||
}
|
||||
@Test public void SelectOrderBy() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people").OrderBy_("name", false)
|
||||
, "SELECT * FROM people ORDER BY name DESC"
|
||||
);
|
||||
}
|
||||
@Test public void SelectWhere() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people").Where_(Db_crt_.eq_("id", 1))
|
||||
, "SELECT * FROM people WHERE id=1"
|
||||
);
|
||||
}
|
||||
@Test public void Select_From_Alias() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people", "p")
|
||||
, "SELECT * FROM people p"
|
||||
);
|
||||
}
|
||||
@Test public void Select_Join_Alias() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people", "p").Join_("roles", "r", Sql_join_itm.same_("p", "id"))
|
||||
, "SELECT * FROM people p INNER JOIN roles r ON p.id=r.id"
|
||||
);
|
||||
}
|
||||
@Test public void Prepare() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.insert_("people").Arg_("id", 1).Arg_("name", "me")
|
||||
, "INSERT INTO people (id, name) VALUES (?, ?)"
|
||||
, true
|
||||
);
|
||||
|
||||
tst_XtoSql
|
||||
( Db_qry_.delete_("people", Db_crt_.eq_("id", 1))
|
||||
, "DELETE FROM people WHERE id=?"
|
||||
, true
|
||||
);
|
||||
|
||||
tst_XtoSql
|
||||
( Db_qry_.update_("people", Db_crt_.eq_("id", 1)).Arg_("name", "me")
|
||||
, "UPDATE people SET name=? WHERE id=?"
|
||||
, true
|
||||
);
|
||||
}
|
||||
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.Xto_str(cmd, prepare));}
|
||||
}
|
||||
60
140_dbs/src/gplx/dbs/sqls/Sql_qry_wtr_iosql_tst.java
Normal file
60
140_dbs/src/gplx/dbs/sqls/Sql_qry_wtr_iosql_tst.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.core.strings.*;
|
||||
import gplx.core.criterias.*; /*Criteria_base*/
|
||||
import gplx.ios.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_qry_wtr_iosql_tst {
|
||||
@Test public void Type() {
|
||||
fld = IoItm_base_.Prop_Type;
|
||||
tst_Write("type=1", ioCrt_(fld, Criteria_.eq_(IoItmDir.Type_Dir)));
|
||||
tst_Write("type=2", ioCrt_(fld, Criteria_.eq_(IoItmFil.Type_Fil)));
|
||||
}
|
||||
@Test public void Ext() {
|
||||
fld = IoItm_base_.Prop_Ext;
|
||||
tst_Write("ext='.txt'", ioCrt_(fld, Criteria_.eq_(".txt")));
|
||||
tst_Write("ext IN ('.txt', '.xml', '.html')", ioCrt_(fld, Criteria_.in_(".txt", ".xml", ".html")));
|
||||
tst_Write("ext NOT IN ('.dll', '.exe')", ioCrt_(fld, Criteria_.inn_(".dll", ".exe")));
|
||||
}
|
||||
@Test public void Title() {
|
||||
fld = IoItm_base_.Prop_Title;
|
||||
tst_Write("title='bin'", ioCrt_(fld, Criteria_.eq_("bin")));
|
||||
tst_Write("title NOT IN ('bin', 'obj')", ioCrt_(fld, Criteria_.inn_("bin", "obj")));
|
||||
}
|
||||
@Test public void Url() {
|
||||
fld = IoItm_base_.Prop_Path;
|
||||
tst_Write("url='C:\\fil.txt'", ioCrt_(fld, Criteria_.eq_("C:\\fil.txt")));
|
||||
tst_Write("url IOMATCH '*.txt'", ioCrt_(fld, Criteria_ioMatch.parse_(true, "*.txt", false)));
|
||||
}
|
||||
@Test public void Binary() {
|
||||
// parentheses around lhs and rhs
|
||||
tst_Write(
|
||||
"(type=1 OR type=2)"
|
||||
, Criteria_.Or
|
||||
( ioCrt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmDir.Type_Dir)), ioCrt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmFil.Type_Fil))
|
||||
));
|
||||
}
|
||||
Criteria ioCrt_(String fld, Criteria crt) {return Criteria_fld.new_(fld, crt);}
|
||||
String fld;
|
||||
void tst_Write(String expd, Criteria crt) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Sql_qry_wtr_ansi whereWtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.new_ansi();
|
||||
whereWtr.Bld_where_val(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,8 @@ 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.core.strings.*;
|
||||
import gplx.core.criterias.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.core.strings.*; import gplx.core.criterias.*; import gplx.dbs.qrys.*;
|
||||
public class Sql_qry_wtr_tst {
|
||||
private final Sql_qry_wtr_fxt fxt = new Sql_qry_wtr_fxt();
|
||||
@Test public void Val() {
|
||||
30
140_dbs/src/gplx/dbs/sqls/Sql_select.java
Normal file
30
140_dbs/src/gplx/dbs/sqls/Sql_select.java
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.core.strings.*;
|
||||
import gplx.dbs.engines.tdbs.*;
|
||||
public 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_.new_fld(Sql_select_fld_base.Tbl_null, fldName, fldName));}
|
||||
public void Add(String fldName, String alias) {flds.Add(Sql_select_fld_.new_fld(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() {}
|
||||
}
|
||||
69
140_dbs/src/gplx/dbs/sqls/Sql_select_fld_.java
Normal file
69
140_dbs/src/gplx/dbs/sqls/Sql_select_fld_.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
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_select_fld_ {
|
||||
public static Sql_select_fld_base new_fld(String tbl, String fld, String alias) {return new Sql_select_fld_fld(tbl, fld, alias);}
|
||||
public static Sql_select_fld_base new_count(String tbl, String fld, String alias) {return new Sql_select_fld_count(tbl, fld, alias);}
|
||||
public static Sql_select_fld_base new_sum(String tbl, String fld, String alias) {return new Sql_select_fld_sum(tbl, fld, alias);}
|
||||
public static Sql_select_fld_base new_min(String tbl, String fld, String alias) {return new Sql_select_fld_minMax(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld_base new_max(String tbl, String fld, String alias) {return new Sql_select_fld_minMax(CompareAble_.More, tbl, fld, alias);}
|
||||
}
|
||||
class Sql_select_fld_fld extends Sql_select_fld_base {
|
||||
public Sql_select_fld_fld(String tbl, String fld, String alias) {this.ctor_(tbl, fld, alias);}
|
||||
@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;
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_count extends Sql_select_fld_func_base {
|
||||
public Sql_select_fld_count(String tbl, String fld, String alias) {this.ctor_(tbl, fld, alias);}
|
||||
@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;
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_sum extends Sql_select_fld_func_base {
|
||||
public Sql_select_fld_sum(String tbl, String fld, String alias) {this.ctor_(tbl, fld, alias);}
|
||||
@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);
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_minMax extends Sql_select_fld_func_base {
|
||||
private final int compareType;
|
||||
public Sql_select_fld_minMax(int compareType, String tbl, String fld, String alias) {
|
||||
this.compareType = compareType;
|
||||
this.ctor_(tbl, fld, alias);
|
||||
}
|
||||
@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;
|
||||
}
|
||||
}
|
||||
45
140_dbs/src/gplx/dbs/sqls/Sql_select_fld_base.java
Normal file
45
140_dbs/src/gplx/dbs/sqls/Sql_select_fld_base.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public 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, "*", "*");}
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
57
140_dbs/src/gplx/dbs/sqls/Sql_select_fld_list.java
Normal file
57
140_dbs/src/gplx/dbs/sqls/Sql_select_fld_list.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*;
|
||||
import gplx.dbs.engines.tdbs.*;
|
||||
public 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[] To_str_ary() {
|
||||
int len = this.Count();
|
||||
String[] rv = new String[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
rv[i] = fld.Fld();
|
||||
}
|
||||
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() {}
|
||||
}
|
||||
29
140_dbs/src/gplx/dbs/sqls/Sql_tbl_src.java
Normal file
29
140_dbs/src/gplx/dbs/sqls/Sql_tbl_src.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*;
|
||||
public 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() {}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user