mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.1.4.1
This commit is contained in:
48
140_dbs/src/gplx/dbs/Db_conn.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn {
|
||||
private final Db_engine engine;
|
||||
private final ListAdp itm_list = ListAdp_.new_();
|
||||
public Db_conn(Db_engine engine) {
|
||||
this.engine = engine;
|
||||
this.txn_mgr = new Db_txn_mgr_base(engine);
|
||||
}
|
||||
public Db_url Url() {return engine.Url();}
|
||||
public Db_txn_mgr Txn_mgr() {return txn_mgr;} private final Db_txn_mgr txn_mgr;
|
||||
public Db_stmt New_stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt New_stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, cols));}
|
||||
public Db_stmt New_stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt New_stmt_select_all_where(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols));}
|
||||
public void Itms_add(Db_conn_itm itm) {itm_list.Add(itm);}
|
||||
public void Itms_del(Db_conn_itm itm) {itm_list.Del(itm);}
|
||||
public void Conn_term() {
|
||||
int len = itm_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_conn_itm itm = (Db_conn_itm)itm_list.FetchAt(i);
|
||||
itm.Conn_term();
|
||||
}
|
||||
engine.Conn_term();
|
||||
Db_conn_pool_old._.Del(this.Url()); // remove from pool, else rls'd instance will be cached and fail upon next use
|
||||
}
|
||||
public Db_stmt New_stmt(Db_qry qry) {return engine.New_stmt_prep(qry);}
|
||||
public int Exec_qry(Db_qry qry) {txn_mgr.Txn_count_(txn_mgr.Txn_count() + 1); return Int_.cast_(engine.Exec_as_obj(qry));}
|
||||
public DataRdr Exec_qry_as_rdr(Db_qry qry) {return DataRdr_.cast_(engine.Exec_as_obj(qry));}
|
||||
public int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
|
||||
public DataRdr Exec_sql_as_rdr(String sql) {return this.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));}
|
||||
}
|
||||
48
140_dbs/src/gplx/dbs/Db_conn_.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn_.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn_ {
|
||||
public static final Db_conn Null = new_and_open_(Db_url_.Null);
|
||||
public static Db_conn new_and_open_(Db_url url) {
|
||||
Db_engine prototype = Db_engine_regy._.Get(url.Tid());
|
||||
Db_engine engine = prototype.New_clone(url);
|
||||
engine.Conn_open(); // auto-open
|
||||
return new Db_conn(engine);
|
||||
}
|
||||
public static Db_conn Reg_itm(Db_conn_itm itm, Db_conn old_conn, Db_conn new_conn) {
|
||||
if (old_conn != null) old_conn.Itms_del(itm);
|
||||
new_conn.Itms_add(itm);
|
||||
return new_conn;
|
||||
}
|
||||
public static int Select_fld0_as_int_or(Db_conn p, String sql, int or) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = p.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));
|
||||
int rv = or;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
Object rv_obj = rdr.ReadAt(0);
|
||||
if (rv_obj != null) // Max(fil_id) will be NULL if tbl is empty
|
||||
rv = Int_.cast_or_(rv_obj, or);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
finally {
|
||||
rdr.Rls();
|
||||
}
|
||||
}
|
||||
}
|
||||
21
140_dbs/src/gplx/dbs/Db_conn_itm.java
Normal file
21
140_dbs/src/gplx/dbs/Db_conn_itm.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_conn_itm {
|
||||
void Conn_term();
|
||||
}
|
||||
34
140_dbs/src/gplx/dbs/Db_conn_mkr.java
Normal file
34
140_dbs/src/gplx/dbs/Db_conn_mkr.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_conn_mkr {
|
||||
Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref);
|
||||
}
|
||||
class Db_conn_mkr_sqlite implements Db_conn_mkr {
|
||||
public Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref) {return Sqlite_engine_.Conn_load_or_make_(db_url, created_ref);}
|
||||
public static final Db_conn_mkr_sqlite _ = new Db_conn_mkr_sqlite(); Db_conn_mkr_sqlite() {}
|
||||
}
|
||||
class Db_conn_mkr_mem implements Db_conn_mkr {
|
||||
private boolean create;
|
||||
public Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref create_ref) {create_ref.Val_(create); return null;}
|
||||
public static Db_conn_mkr_mem create_(boolean create) {
|
||||
Db_conn_mkr_mem rv = new Db_conn_mkr_mem();
|
||||
rv.create = create;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
25
140_dbs/src/gplx/dbs/Db_conn_mkr_.java
Normal file
25
140_dbs/src/gplx/dbs/Db_conn_mkr_.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn_mkr_ {
|
||||
public static final Db_conn_mkr
|
||||
Sqlite = Db_conn_mkr_sqlite._
|
||||
, Mem_create_y = Db_conn_mkr_mem.create_(Bool_.Y)
|
||||
, Mem_create_n = Db_conn_mkr_mem.create_(Bool_.N)
|
||||
;
|
||||
}
|
||||
48
140_dbs/src/gplx/dbs/Db_conn_pool.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn_pool.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.mems.*;
|
||||
public class Db_conn_pool {
|
||||
private final HashAdp dbm_hash = HashAdp_.new_(); private final HashAdp engine_hash = HashAdp_.new_();
|
||||
Db_conn_pool() {this.Init();}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_url_.sqlite_(url));}
|
||||
public Db_conn Get_or_new(Db_url url) {
|
||||
Db_conn rv = (Db_conn)dbm_hash.Fetch(url.Xto_api());
|
||||
if (rv == null) {
|
||||
Db_engine prime = (Db_engine)engine_hash.Fetch(url.Tid()); if (prime == null) throw Err_.new_("db engine prototype not found; tid={0}", url.Tid());
|
||||
Db_engine clone = prime.New_clone(url);
|
||||
rv = new Db_conn(clone);
|
||||
dbm_hash.Add(url.Xto_api(), rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Set_mem(String db, Db_meta_tbl... tbls) {
|
||||
Db_url url = Db_url__mem.new_(db);
|
||||
Db_engine__mem engine = new Db_engine__mem(url, tbls);
|
||||
Db_conn dbm = new Db_conn(engine);
|
||||
dbm_hash.AddReplace(url.Xto_api(), dbm);
|
||||
}
|
||||
private void Init() {
|
||||
this.Engines__add(Db_engine_null._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
}
|
||||
public void Engines__add(Db_engine... ary) {
|
||||
for (Db_engine itm : ary)
|
||||
engine_hash.Add(itm.Tid(), itm);
|
||||
}
|
||||
public static final Db_conn_pool I = new Db_conn_pool();
|
||||
}
|
||||
40
140_dbs/src/gplx/dbs/Db_conn_pool_old.java
Normal file
40
140_dbs/src/gplx/dbs/Db_conn_pool_old.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn_pool_old {
|
||||
private final OrderedHash hash = OrderedHash_.new_();
|
||||
public Db_conn Get_or_new(Db_url url) {return Get_or_new(url.Xto_raw());}
|
||||
public Db_conn Get_or_new(String raw) {
|
||||
Db_conn rv = (Db_conn)hash.Fetch(raw);
|
||||
if (rv == null) {
|
||||
rv = Db_conn_.new_and_open_(Db_url_.parse_(raw));
|
||||
hash.Add(raw, rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Del(Db_url url) {hash.Del(url.Xto_raw());}
|
||||
public void Clear() {
|
||||
int len = hash.Count();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Db_conn conn = (Db_conn)hash.FetchAt(0);
|
||||
conn.Conn_term();
|
||||
}
|
||||
hash.Clear();
|
||||
}
|
||||
public static final Db_conn_pool_old _ = new Db_conn_pool_old(); Db_conn_pool_old() {}
|
||||
}
|
||||
31
140_dbs/src/gplx/dbs/Db_meta_fld.java
Normal file
31
140_dbs/src/gplx/dbs/Db_meta_fld.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_fld {
|
||||
public Db_meta_fld(String name, int tid, int len, boolean nullable, boolean primary, boolean autoincrement) {
|
||||
this.name = name; this.tid = tid; this.len = len;
|
||||
this.nullable = nullable; this.primary = primary; this.autoincrement = autoincrement;
|
||||
}
|
||||
public int Tid() {return tid;} private final int tid;
|
||||
public String Name() {return name;} private final String name;
|
||||
public int Len() {return len;} private final int len;
|
||||
public boolean Nullable() {return nullable;} private final boolean nullable;
|
||||
public boolean Primary() {return primary;} private final boolean primary;
|
||||
public boolean Autoincrement() {return autoincrement;} private final boolean autoincrement;
|
||||
public static final int Tid_bool = 0, Tid_byte = 1, Tid_short = 2, Tid_int = 3, Tid_long = 4, Tid_float = 5, Tid_double = 6, Tid_str = 7, Tid_text = 8, Tid_bry = 9;
|
||||
}
|
||||
45
140_dbs/src/gplx/dbs/Db_meta_fld_list.java
Normal file
45
140_dbs/src/gplx/dbs/Db_meta_fld_list.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_fld_list {
|
||||
private final OrderedHash flds = OrderedHash_.new_();
|
||||
private final ListAdp keys = ListAdp_.new_();
|
||||
public Db_meta_fld Get_by(String name) {return (Db_meta_fld)flds.Fetch(name);}
|
||||
public String[] Xto_str_ary() {if (str_ary == null) str_ary = (String[])keys.Xto_ary(String.class); return str_ary;} private String[] str_ary;
|
||||
public Db_meta_fld[] Xto_fld_ary() {if (fld_ary == null) fld_ary = (Db_meta_fld[])flds.Xto_ary(Db_meta_fld.class); return fld_ary;} private Db_meta_fld[] fld_ary;
|
||||
public String Add_bool(String name) {return Add(name, Db_meta_fld.Tid_bool, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_byte(String name) {return Add(name, Db_meta_fld.Tid_byte, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_short(String name) {return Add(name, Db_meta_fld.Tid_short, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_int(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_int_pkey(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.N);}
|
||||
public String Add_int_pkey_autonum(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.Y);}
|
||||
public String Add_long(String name) {return Add(name, Db_meta_fld.Tid_long, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_float(String name) {return Add(name, Db_meta_fld.Tid_float, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_double(String name) {return Add(name, Db_meta_fld.Tid_double, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_str(String name, int len) {return Add(name, Db_meta_fld.Tid_str, len, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_text(String name) {return Add(name, Db_meta_fld.Tid_text, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_bry(String name) {return Add(name, Db_meta_fld.Tid_bry, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add(String name, int tid, int len, boolean nullable, boolean primary, boolean autoincrement) {
|
||||
Db_meta_fld fld = new Db_meta_fld(name, tid, len, nullable, primary, autoincrement);
|
||||
flds.Add(name, fld);
|
||||
keys.Add(name);
|
||||
return name;
|
||||
}
|
||||
private static final int Len_null = -1;
|
||||
public static Db_meta_fld_list new_() {return new Db_meta_fld_list();}
|
||||
}
|
||||
27
140_dbs/src/gplx/dbs/Db_meta_idx.java
Normal file
27
140_dbs/src/gplx/dbs/Db_meta_idx.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_idx {
|
||||
public Db_meta_idx(String tbl, String name, boolean unique, String[] flds) {this.tbl = tbl; this.name = name; this.unique = unique; this.flds = flds;}
|
||||
public String Name() {return name;} private final String name;
|
||||
public boolean Unique() {return unique;} private final boolean unique;
|
||||
public String[] Flds() {return flds;} private final String[] flds;
|
||||
public String Tbl() {return tbl;} private final String tbl;
|
||||
public static Db_meta_idx new_unique(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, name, Bool_.Y, flds);}
|
||||
public static Db_meta_idx new_normal(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, name, Bool_.N, flds);}
|
||||
}
|
||||
28
140_dbs/src/gplx/dbs/Db_meta_tbl.java
Normal file
28
140_dbs/src/gplx/dbs/Db_meta_tbl.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_tbl {
|
||||
public Db_meta_tbl(String name, Db_meta_fld[] flds, Db_meta_idx[] idxs) {
|
||||
this.name = name; this.flds = flds; this.idxs = idxs;
|
||||
}
|
||||
public String Name() {return name;} private final String name;
|
||||
public Db_meta_fld[] Flds() {return flds;} private final Db_meta_fld[] flds;
|
||||
public Db_meta_idx[] Idxs() {return idxs;} private final Db_meta_idx[] idxs;
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld[] flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds, idxs);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld... flds) {return new Db_meta_tbl(name, flds, null);}
|
||||
}
|
||||
42
140_dbs/src/gplx/dbs/Db_rdr.java
Normal file
42
140_dbs/src/gplx/dbs/Db_rdr.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_rdr {
|
||||
boolean Move_next();
|
||||
byte[] Read_bry(int i);
|
||||
byte[] Read_bry(String k);
|
||||
byte[] Read_bry_by_str(int i);
|
||||
byte[] Read_bry_by_str(String k);
|
||||
String Read_str(int i);
|
||||
String Read_str(String k);
|
||||
byte Read_byte(int i);
|
||||
byte Read_byte(String k);
|
||||
int Read_int(int i);
|
||||
int Read_int(String k);
|
||||
long Read_long(int i);
|
||||
long Read_long(String k);
|
||||
float Read_float(int i);
|
||||
float Read_float(String k);
|
||||
double Read_double(int i);
|
||||
double Read_double(String k);
|
||||
DateAdp Read_date_by_str(int i);
|
||||
DateAdp Read_date_by_str(String k);
|
||||
boolean Read_bool_by_byte(int i);
|
||||
boolean Read_bool_by_byte(String k);
|
||||
void Rls();
|
||||
}
|
||||
45
140_dbs/src/gplx/dbs/Db_rdr_.java
Normal file
45
140_dbs/src/gplx/dbs/Db_rdr_.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_rdr_ {
|
||||
public static final Db_rdr Null = new Db_rdr__null();
|
||||
}
|
||||
class Db_rdr__null implements Db_rdr {
|
||||
public boolean Move_next() {return false;}
|
||||
public byte[] Read_bry(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry(String k) {return Bry_.Empty;}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.Empty;}
|
||||
public byte Read_byte(int i) {return Byte_.Max_value_127;}
|
||||
public byte Read_byte(String k) {return Byte_.Max_value_127;}
|
||||
public String Read_str(int i) {return String_.Empty;}
|
||||
public String Read_str(String k) {return String_.Empty;}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.MinValue;}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.MinValue;}
|
||||
public int Read_int(int i) {return Int_.MinValue;}
|
||||
public int Read_int(String k) {return Int_.MinValue;}
|
||||
public long Read_long(int i) {return Long_.MinValue;}
|
||||
public long Read_long(String k) {return Long_.MinValue;}
|
||||
public float Read_float(int i) {return Float_.NaN;}
|
||||
public float Read_float(String k) {return Float_.NaN;}
|
||||
public double Read_double(int i) {return Double_.NaN;}
|
||||
public double Read_double(String k) {return Double_.NaN;}
|
||||
public boolean Read_bool_by_byte(int i) {return false;}
|
||||
public boolean Read_bool_by_byte(String k) {return false;}
|
||||
public void Rls() {}
|
||||
}
|
||||
49
140_dbs/src/gplx/dbs/Db_rdr__basic.java
Normal file
49
140_dbs/src/gplx/dbs/Db_rdr__basic.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import java.sql.ResultSet;
|
||||
class Db_rdr__basic implements Db_rdr {
|
||||
protected ResultSet rdr;
|
||||
public void Ctor(ResultSet rdr, String sql) {this.rdr = rdr; this.sql = sql;}
|
||||
public String Sql() {return sql;} private String sql;
|
||||
public boolean Move_next() {
|
||||
try {return rdr.next();}
|
||||
catch (Exception e) {throw Err_.new_fmt_("move_next failed; check column casting error in SQL: err={0} sql={1}", Err_.Message_lang(e), sql);}
|
||||
}
|
||||
public byte[] Read_bry(int i) {try {return rdr.getBytes(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry(String k) {try {return (byte[])rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry_by_str(int i) {try {return Bry_.new_utf8_(rdr.getString(i + 1));} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry_by_str(String k) {try {return Bry_.new_utf8_((String)rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public String Read_str(int i) {try {return rdr.getString(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public String Read_str(String k) {try {return (String)rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561(Read_str(i));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561(Read_str(k));}
|
||||
public int Read_int(int i) {try {return rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public int Read_int(String k) {try {return Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public long Read_long(int i) {try {return rdr.getLong(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public long Read_long(String k) {try {return Long_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public float Read_float(int i) {try {return rdr.getFloat(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public float Read_float(String k) {try {return Float_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public double Read_double(int i) {try {return rdr.getDouble(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public double Read_double(String k) {try {return Double_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(int i) {try {return rdr.getByte(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(String k) {try {return Byte_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public boolean Read_bool_by_byte(int i) {try {return rdr.getByte(i + 1) == 1;} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public boolean Read_bool_by_byte(String k) {try {return Byte_.cast_(rdr.getObject(k)) == 1;} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public void Rls() {try {rdr.close();} catch (Exception e) {throw Err_.new_("close failed: err={0}", Err_.Message_lang(e));}}
|
||||
}
|
||||
73
140_dbs/src/gplx/dbs/Db_sqlbldr__sqlite.java
Normal file
73
140_dbs/src/gplx/dbs/Db_sqlbldr__sqlite.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
interface Db_sqlbldr {}
|
||||
class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.reset_(1024);
|
||||
public String Bld_create_idx(Db_meta_idx idx) {
|
||||
tmp_bfr.Add_str_ascii("CREATE ");
|
||||
if (idx.Unique())
|
||||
tmp_bfr.Add_str_ascii("UNIQUE ");
|
||||
tmp_bfr.Add_str_ascii("INDEX ");
|
||||
tmp_bfr.Add_str_ascii("IF NOT EXISTS ");
|
||||
tmp_bfr.Add_str_ascii(idx.Tbl()).Add_str_ascii("__").Add_str_ascii(idx.Name());
|
||||
tmp_bfr.Add_str_ascii(" (");
|
||||
String[] flds = idx.Flds();
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
String fld = flds[i];
|
||||
if (i != 0) tmp_bfr.Add_str_ascii(", ");
|
||||
tmp_bfr.Add_str_ascii(fld);
|
||||
}
|
||||
tmp_bfr.Add_str_ascii(");");
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
public String Bld_create_tbl(Db_meta_tbl tbl) {
|
||||
tmp_bfr.Add_str_ascii("CREATE TABLE ").Add_str_ascii(tbl.Name()).Add_byte_nl();
|
||||
Db_meta_fld[] flds = tbl.Flds();
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
Db_meta_fld fld = flds[i];
|
||||
tmp_bfr.Add_byte(i == 0 ? Byte_ascii.Paren_bgn : Byte_ascii.Comma).Add_byte_space();
|
||||
tmp_bfr.Add_str_ascii(fld.Name()).Add_byte_space();
|
||||
Tid_to_sql(tmp_bfr, fld.Tid(), fld.Len()); tmp_bfr.Add_byte_space();
|
||||
tmp_bfr.Add_str_ascii(fld.Nullable() ? "NULL " : "NOT NULL ");
|
||||
if (fld.Primary()) tmp_bfr.Add_str_ascii("PRIMARY KEY ");
|
||||
if (fld.Autoincrement()) tmp_bfr.Add_str_ascii("AUTOINCREMENT ");
|
||||
tmp_bfr.Del_by_1(); // remove trailing space
|
||||
tmp_bfr.Add_byte_nl();
|
||||
}
|
||||
tmp_bfr.Add_str_ascii(");");
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
public void Tid_to_sql(Bry_bfr tmp_bfr, int tid, int len) {// REF: https://www.sqlite.org/datatype3.html
|
||||
switch (tid) {
|
||||
case Db_meta_fld.Tid_bool: tmp_bfr.Add_str_ascii("boolean"); break;
|
||||
case Db_meta_fld.Tid_byte: tmp_bfr.Add_str_ascii("tinyint"); break;
|
||||
case Db_meta_fld.Tid_short: tmp_bfr.Add_str_ascii("smallint"); break;
|
||||
case Db_meta_fld.Tid_int: tmp_bfr.Add_str_ascii("int"); break;
|
||||
case Db_meta_fld.Tid_long: tmp_bfr.Add_str_ascii("bigint"); break;
|
||||
case Db_meta_fld.Tid_float: tmp_bfr.Add_str_ascii("float"); break;
|
||||
case Db_meta_fld.Tid_double: tmp_bfr.Add_str_ascii("double"); break;
|
||||
case Db_meta_fld.Tid_str: tmp_bfr.Add_str_ascii("varchar(").Add_int_variable(len).Add_byte(Byte_ascii.Paren_end); break;
|
||||
case Db_meta_fld.Tid_text: tmp_bfr.Add_str_ascii("text"); break;
|
||||
case Db_meta_fld.Tid_bry: tmp_bfr.Add_str_ascii("blob"); break;
|
||||
default: throw Err_.unhandled(tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
64
140_dbs/src/gplx/dbs/Db_sqlbldr_tst.java
Normal file
64
140_dbs/src/gplx/dbs/Db_sqlbldr_tst.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_sqlbldr_tst {
|
||||
@Before public void setup() {} private final Db_sqlbldr_fxt fxt = new Db_sqlbldr_fxt();
|
||||
@Test public void Idx_unique() {
|
||||
fxt.Test_create_idx(Db_meta_idx.new_unique("tbl_name", "idx_name", "fld_1", "fld_2")
|
||||
, "CREATE UNIQUE INDEX IF NOT EXISTS tbl_name__idx_name (fld_1, fld_2);"
|
||||
);
|
||||
}
|
||||
@Test public void Tbl_basic() {
|
||||
Db_meta_fld_list flds = Db_meta_fld_list.new_();
|
||||
flds.Add_int_pkey("fld_int_pkey");
|
||||
flds.Add_bool("fld_bool");
|
||||
flds.Add_short("fld_short");
|
||||
flds.Add_int("fld_int");
|
||||
flds.Add_long("fld_long");
|
||||
flds.Add_float("fld_float");
|
||||
flds.Add_double("fld_double");
|
||||
flds.Add_str("fld_str", 123);
|
||||
flds.Add_text("fld_text");
|
||||
flds.Add_bry("fld_bry");
|
||||
fxt.Test_create_tbl(Db_meta_tbl.new_("tbl_name", flds.Xto_fld_ary())
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "CREATE TABLE tbl_name"
|
||||
, "( fld_int_pkey int NOT NULL PRIMARY KEY"
|
||||
, ", fld_bool boolean NOT NULL"
|
||||
, ", fld_short smallint NOT NULL"
|
||||
, ", fld_int int NOT NULL"
|
||||
, ", fld_long bigint NOT NULL"
|
||||
, ", fld_float float NOT NULL"
|
||||
, ", fld_double double NOT NULL"
|
||||
, ", fld_str varchar(123) NOT NULL"
|
||||
, ", fld_text text NOT NULL"
|
||||
, ", fld_bry blob NOT NULL"
|
||||
, ");"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Db_sqlbldr_fxt {
|
||||
private Db_sqlbldr__sqlite sqlbldr = new Db_sqlbldr__sqlite();
|
||||
public void Test_create_idx(Db_meta_idx idx, String expd) {
|
||||
Tfds.Eq(expd, sqlbldr.Bld_create_idx(idx));
|
||||
}
|
||||
public void Test_create_tbl(Db_meta_tbl tbl, String expd) {
|
||||
Tfds.Eq_str_lines(expd, sqlbldr.Bld_create_tbl(tbl));
|
||||
}
|
||||
}
|
||||
40
140_dbs/src/gplx/dbs/Db_sys_regy_mgr.java
Normal file
40
140_dbs/src/gplx/dbs/Db_sys_regy_mgr.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
class Db_sys_regy_mgr {
|
||||
private Db_sys_regy_tbl tbl;
|
||||
public Db_sys_regy_mgr(Db_url url, String name) {tbl = new Db_sys_regy_tbl(url, name);}
|
||||
public void Set(String grp, String key, String val) {
|
||||
if (tbl.Select_val_or(grp, key, null) == null)
|
||||
tbl.Insert(grp, key, val);
|
||||
else
|
||||
tbl.Update(grp, key, val);
|
||||
}
|
||||
public void Del(String grp, String key) {
|
||||
tbl.Delete(grp, key);
|
||||
}
|
||||
public String Get_val_as_str_or(String grp, String key, String or) {
|
||||
return tbl.Select_val_or(grp, key, or);
|
||||
}
|
||||
}
|
||||
class Db_sys_regy_itm {
|
||||
public Db_sys_regy_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private final String grp;
|
||||
public String Key() {return key;} private final String key;
|
||||
public String Val() {return val;} private final String val;
|
||||
}
|
||||
54
140_dbs/src/gplx/dbs/Db_sys_regy_mgr_tst.java
Normal file
54
140_dbs/src/gplx/dbs/Db_sys_regy_mgr_tst.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_sys_regy_mgr_tst {
|
||||
@Before public void init() {fxt.Init();} private Db_sys_regy_mgr_fxt fxt = new Db_sys_regy_mgr_fxt();
|
||||
@Test public void Insert() {
|
||||
fxt .Exec_set("grp", "key", "val_0")
|
||||
.Test_get("grp", "key", "val_0");
|
||||
}
|
||||
@Test public void Update() {
|
||||
fxt .Exec_set("grp", "key", "val_0")
|
||||
.Exec_set("grp", "key", "val_1")
|
||||
.Test_get("grp", "key", "val_1");
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt .Exec_set("grp", "key_0", "val_0")
|
||||
.Exec_set("grp", "key_1", "val_0")
|
||||
.Exec_del("grp", "key_1")
|
||||
.Test_get("grp", "key_0", null)
|
||||
.Test_get("grp", "key_1", null)
|
||||
;
|
||||
}
|
||||
}
|
||||
class Db_sys_regy_mgr_fxt {
|
||||
private Db_sys_regy_mgr sys_regy_mgr;
|
||||
public void Init() {
|
||||
if (sys_regy_mgr == null) {
|
||||
Db_conn_pool.I.Set_mem("test_db", Db_sys_regy_tbl.new_meta("test_regy"));
|
||||
sys_regy_mgr = new Db_sys_regy_mgr(Db_url_.mem_("test_db"), "test_regy");
|
||||
}
|
||||
}
|
||||
public Db_sys_regy_mgr_fxt Exec_set(String grp, String key, String val) {sys_regy_mgr.Set(grp, key, val); return this;}
|
||||
public Db_sys_regy_mgr_fxt Exec_del(String grp, String key) {sys_regy_mgr.Del(grp, key); return this;}
|
||||
public Db_sys_regy_mgr_fxt Test_get(String grp, String key, String expd_val) {
|
||||
Tfds.Eq(expd_val, sys_regy_mgr.Get_val_as_str_or(grp, key, null));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
61
140_dbs/src/gplx/dbs/Db_sys_regy_tbl.java
Normal file
61
140_dbs/src/gplx/dbs/Db_sys_regy_tbl.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
class Db_sys_regy_tbl implements Db_conn_itm {
|
||||
private final String tbl_name;
|
||||
private Db_conn conn;
|
||||
public void Conn_term() {}
|
||||
public Db_sys_regy_tbl(Db_url url, String tbl_name) {
|
||||
this.tbl_name = tbl_name;
|
||||
// this.meta = Db_sys_regy_tbl.new_meta(tbl_name);
|
||||
this.conn = Db_conn_pool.I.Get_or_new(url);
|
||||
conn.Itms_add(this);
|
||||
}
|
||||
// private Db_meta_tbl meta;
|
||||
public void Insert(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.New_stmt_insert(tbl_name, Flds.Xto_str_ary());
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Val_str(val).Exec_insert();
|
||||
}
|
||||
public void Update(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.New_stmt_update(tbl_name, String_.Ary(Fld_regy_grp, Fld_regy_key), Fld_regy_val);
|
||||
stmt.Clear().Val_str(val).Val_str(grp).Val_str(key).Exec_update();
|
||||
}
|
||||
public void Delete(String grp, String key) {
|
||||
Db_stmt stmt = conn.New_stmt_delete(tbl_name, Fld_regy_grp, Fld_regy_key);
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Exec_delete();
|
||||
}
|
||||
public String Select_val_or(String grp, String key, String or) {
|
||||
Db_stmt stmt = conn.New_stmt_select_all_where(tbl_name, Flds.Xto_str_ary(), Fld_regy_grp, Fld_regy_key);
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
try {
|
||||
rdr = stmt.Clear().Val_str(grp).Val_str(key).Exec_select_as_rdr();
|
||||
return rdr.Move_next() ? rdr.Read_str(Fld_regy_val) : or;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
private static final Db_meta_fld_list Flds = Db_meta_fld_list.new_();
|
||||
private static final String
|
||||
Fld_regy_grp = Flds.Add_str("regy_grp", 1024)
|
||||
, Fld_regy_key = Flds.Add_str("regy_key", 1024)
|
||||
, Fld_regy_val = Flds.Add_str("regy_val", 4096)
|
||||
;
|
||||
public static Db_meta_tbl new_meta(String tbl) {
|
||||
return Db_meta_tbl.new_(tbl, Flds.Xto_fld_ary()
|
||||
, Db_meta_idx.new_unique(tbl, "key", Flds.Xto_str_ary())
|
||||
);
|
||||
}
|
||||
}
|
||||
95
140_dbs/src/gplx/dbs/Db_url.java
Normal file
95
140_dbs/src/gplx/dbs/Db_url.java
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_url {
|
||||
String Tid();
|
||||
String Xto_raw();
|
||||
String Xto_api();
|
||||
Db_url New_self(String raw, GfoMsg m);
|
||||
}
|
||||
class Db_url__null extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "null_db";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {return this;}
|
||||
public static final Db_url__null _ = new Db_url__null(); Db_url__null() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
}
|
||||
class Db_url__mysql extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mysql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "uid", uid
|
||||
, "pwd", pwd
|
||||
, "charset", "utf8"
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__mysql rv = new Db_url__mysql();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("charset", "utf8")));
|
||||
rv.uid = m.ReadStr("uid");
|
||||
rv.pwd = m.ReadStr("pwd");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__mysql _ = new Db_url__mysql(); Db_url__mysql() {}
|
||||
}
|
||||
class Db_url__postgres extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "postgresql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "port", "5432"
|
||||
, "user id", uid
|
||||
, "password", pwd
|
||||
, "encoding", "unicode" // needed for 1.1 conn; otherwise, ascii
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__postgres rv = new Db_url__postgres();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("encoding", "unicode")));
|
||||
rv.uid = m.ReadStr("user id");
|
||||
rv.pwd = m.ReadStr("password");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__postgres _ = new Db_url__postgres(); Db_url__postgres() {}
|
||||
}
|
||||
class Db_url__tdb extends Db_url__base {
|
||||
public Io_url Url() {return url;} Io_url url;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "tdb";
|
||||
public static Db_url new_(Io_url url) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "url", url.Raw()
|
||||
));
|
||||
} Db_url__tdb() {}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__tdb rv = new Db_url__tdb();
|
||||
String urlStr = m.ReadStr("url");
|
||||
Io_url url = Io_url_.new_any_(urlStr);
|
||||
rv.Ctor(urlStr, url.NameOnly(), raw, BldApi(m));
|
||||
rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__tdb _ = new Db_url__tdb();
|
||||
}
|
||||
55
140_dbs/src/gplx/dbs/Db_url_.java
Normal file
55
140_dbs/src/gplx/dbs/Db_url_.java
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.mems.*;
|
||||
public class Db_url_ {
|
||||
public static final Db_url Null = Db_url__null._;
|
||||
public static final Db_url Test = Db_url__mysql.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static Db_url parse_(String raw) {return Db_url_pool._.Parse(raw);}
|
||||
public static Db_url sqlite_(Io_url url) {return Db_url__sqlite.load_(url);}
|
||||
public static Db_url tdb_(Io_url url) {return Db_url__tdb.new_(url);}
|
||||
public static Db_url mem_(String db) {return Db_url__mem.new_(db);}
|
||||
public static final String Key_tdb = Db_url__tdb.Tid_const;
|
||||
}
|
||||
class Db_url_pool {
|
||||
private OrderedHash regy = OrderedHash_.new_();
|
||||
public Db_url_pool() {
|
||||
this.Add(Db_url__null._).Add(Db_url__tdb._).Add(Db_url__mysql._).Add(Db_url__postgres._).Add(Db_url__sqlite._);
|
||||
this.Add(Db_url__mem.I);
|
||||
}
|
||||
public Db_url_pool Add(Db_url itm) {regy.AddReplace(itm.Tid(), itm); return this;}
|
||||
public Db_url Parse(String raw) {// assume each pair has format of: name=val;
|
||||
try {
|
||||
GfoMsg m = GfoMsg_.new_parse_("db_url");
|
||||
String[] terms = String_.Split(raw, ";");
|
||||
String url_tid = "";
|
||||
for (String term : terms) {
|
||||
if (String_.Len(term) == 0) continue;
|
||||
String[] kv = String_.Split(term, "=");
|
||||
if (String_.Eq(kv[0], "gplx_key"))
|
||||
url_tid = kv[1]; // NOTE: do not add to GfoMsg; will not be part of ApiStr
|
||||
else
|
||||
m.Add(kv[0], kv[1]);
|
||||
}
|
||||
Db_url prototype = (Db_url)regy.Fetch(url_tid);
|
||||
return prototype.New_self(raw, m);
|
||||
}
|
||||
catch(Exception exc) {throw Err_.parse_type_exc_(exc, Db_url.class, raw);}
|
||||
}
|
||||
public static final Db_url_pool _ = new Db_url_pool();
|
||||
}
|
||||
51
140_dbs/src/gplx/dbs/Db_url__base.java
Normal file
51
140_dbs/src/gplx/dbs/Db_url__base.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public abstract class Db_url__base implements Db_url {
|
||||
public abstract String Tid();
|
||||
public String Xto_raw() {return raw;} private String raw = "";
|
||||
public String Xto_api() {return api;} private String api = "";
|
||||
public String Database() {return database;} private String database = "";
|
||||
public String Server() {return server;} private String server = "";
|
||||
public abstract Db_url New_self(String raw, GfoMsg m);
|
||||
protected void Ctor(String server, String database, String raw, String api) {this.server = server; this.database = database; this.raw = raw; this.api = api;}
|
||||
protected static String BldApi(GfoMsg m, KeyVal... xtnAry) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
HashAdp hash = HashAdp_.new_();
|
||||
for (int i = 0; i < m.Args_count(); i++) {
|
||||
KeyVal kv = m.Args_getAt(i);
|
||||
sb.Add_fmt("{0}={1};", kv.Key(), kv.Val_to_str_or_empty());
|
||||
hash.AddKeyVal(kv.Key());
|
||||
}
|
||||
for (KeyVal xtn : xtnAry) {
|
||||
if (hash.Has(xtn.Key())) continue;
|
||||
sb.Add_fmt("{0}={1};", xtn.Key(), xtn.Val_to_str_or_empty());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
protected static String Bld_raw(String... ary) {
|
||||
Bry_bfr bfr = Bry_bfr.reset_(255);
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
String itm = ary[i];
|
||||
bfr.Add_str_utf8(itm);
|
||||
bfr.Add_byte(i % 2 == 0 ? Byte_ascii.Eq : Byte_ascii.Semic);
|
||||
}
|
||||
return bfr.Xto_str();
|
||||
}
|
||||
}
|
||||
46
140_dbs/src/gplx/dbs/Db_url__sqlite.java
Normal file
46
140_dbs/src/gplx/dbs/Db_url__sqlite.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_url__sqlite extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "sqlite";
|
||||
public Io_url Url() {return url;} private Io_url url;
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__sqlite rv = new Db_url__sqlite();
|
||||
String url = m.ReadStr("data source");
|
||||
rv.url = Io_url_.new_any_(url);
|
||||
rv.Ctor("", url, raw, BldApi(m, KeyVal_.new_("version", "3")));
|
||||
return rv;
|
||||
}
|
||||
public static Db_url load_(Io_url url) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
, "data source" , url.Xto_api()
|
||||
, "version" , "3"
|
||||
));
|
||||
}
|
||||
public static Db_url make_(Io_url url) {
|
||||
Io_mgr._.CreateDirIfAbsent(url.OwnerDir());
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
, "data source" , url.Xto_api()
|
||||
, "version" , "3"
|
||||
|
||||
));
|
||||
}
|
||||
public static final Db_url__sqlite _ = new Db_url__sqlite(); Db_url__sqlite() {}
|
||||
}
|
||||
46
140_dbs/src/gplx/dbs/Db_url_tst.java
Normal file
46
140_dbs/src/gplx/dbs/Db_url_tst.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_url_tst {
|
||||
@Before public void setup() {
|
||||
regy.Add(Db_url_mock._);
|
||||
} private final Db_url_pool regy = new Db_url_pool();
|
||||
@Test public void Parse() {
|
||||
tst_Parse("gplx_key=mock;id=1;", kv_("id", "1")); // one; gplx_key removed
|
||||
tst_Parse("gplx_key=mock;id=1;name=me;", kv_("id", "1"), kv_("name", "me")); // many
|
||||
tst_Parse("gplx_key=mock;id=1;name=me" , kv_("id", "1"), kv_("name", "me")); // no semi-colon at end
|
||||
}
|
||||
private KeyVal kv_(String key, Object val) {return KeyVal_.new_(key, val);}
|
||||
private void tst_Parse(String raw, KeyVal... expd) {
|
||||
Db_url_mock mock = (Db_url_mock)regy.Parse(raw);
|
||||
Tfds.Eq_ary_str(expd, mock.Kvs());
|
||||
}
|
||||
}
|
||||
class Db_url_mock extends Db_url__base {
|
||||
public KeyVal[] Kvs() {return kvs;} KeyVal[] kvs;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mock";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url_mock rv = new Db_url_mock();
|
||||
rv.kvs = new KeyVal[m.Args_count()];
|
||||
for (int i = 0; i < m.Args_count(); i++)
|
||||
rv.kvs[i] = m.Args_getAt(i);
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url_mock _ = new Db_url_mock(); Db_url_mock() {}
|
||||
}
|
||||
67
140_dbs/src/gplx/dbs/Sql_join_itm.java
Normal file
67
140_dbs/src/gplx/dbs/Sql_join_itm.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Sql_join_itm {
|
||||
public String SrcTbl() {return srcTbl;} public Sql_join_itm SrcTbl_(String v) {srcTbl = v; return this;} private String srcTbl;
|
||||
public String SrcFld() {return srcFld;} public Sql_join_itm SrcFld_(String v) {srcFld = v; return this;} private String srcFld;
|
||||
public String TrgFld() {return trgFld;} public Sql_join_itm TrgFld_(String v) {trgFld = v; return this;} private String trgFld;
|
||||
public String TrgFldOrSrcFld() {return trgFld == null ? srcFld : trgFld;}
|
||||
public static Sql_join_itm new_(String trgFld, String srcTbl, String srcFld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = trgFld; rv.srcTbl = srcTbl; rv.srcFld = srcFld;
|
||||
return rv;
|
||||
} Sql_join_itm() {}
|
||||
public static Sql_join_itm same_(String tbl, String fld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = fld; rv.srcTbl = tbl; rv.srcFld = fld;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
class Sql_from {
|
||||
public ListAdp Tbls() {return tbls;} ListAdp tbls = ListAdp_.new_();
|
||||
public Sql_tbl_src BaseTable() {return (Sql_tbl_src)tbls.FetchAt(0);}
|
||||
|
||||
public static Sql_from new_(Sql_tbl_src baseTable) {
|
||||
Sql_from rv = new Sql_from();
|
||||
rv.tbls.Add(baseTable);
|
||||
return rv;
|
||||
} Sql_from() {}
|
||||
}
|
||||
class Sql_tbl_src {
|
||||
public Sql_join_itmType JoinType() {return type;} public Sql_tbl_src JoinType_(Sql_join_itmType v) {this.type = v; return this;} Sql_join_itmType type = Sql_join_itmType.Inner;
|
||||
public ListAdp JoinLinks() {return joinLinks;} ListAdp joinLinks = ListAdp_.new_();
|
||||
public String TblName() {return tblName;} public Sql_tbl_src TblName_(String s) {tblName = s; return this;} private String tblName;
|
||||
public String Alias() {return alias;} public Sql_tbl_src Alias_(String s) {alias = s; return this;} private String alias;
|
||||
public void XtoSql(String_bldr sb) {
|
||||
sb.Add_many(tblName, alias == null ? "" : " " + alias);
|
||||
}
|
||||
public static Sql_tbl_src new_() {return new Sql_tbl_src();} Sql_tbl_src() {}
|
||||
}
|
||||
class Sql_join_itmType {
|
||||
public int Val() {return val;} int val;
|
||||
public String Name() {return name;} private String name;
|
||||
Sql_join_itmType(int v, String name) {this.val = v; this.name = name;}
|
||||
public static final Sql_join_itmType
|
||||
From = new Sql_join_itmType(0, "FROM")
|
||||
, Inner = new Sql_join_itmType(1, "INNER JOIN")
|
||||
, Left = new Sql_join_itmType(2, "LEFT JOIN")
|
||||
, Right = new Sql_join_itmType(3, "RIGHT JOIN")
|
||||
, Outer = new Sql_join_itmType(4, "OUTER JOIN")
|
||||
, Cross = new Sql_join_itmType(5, "CROSS JOIN")
|
||||
;
|
||||
}
|
||||
73
140_dbs/src/gplx/dbs/Sql_order_by.java
Normal file
73
140_dbs/src/gplx/dbs/Sql_order_by.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.lists.*; /*ComparerAble*/
|
||||
class Sql_order_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_order_by new_(Sql_order_by_itm... ary) {
|
||||
Sql_order_by rv = new Sql_order_by();
|
||||
for (Sql_order_by_itm itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_order_by() {}
|
||||
}
|
||||
class Sql_group_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_group_by new_(String... ary) {
|
||||
Sql_group_by rv = new Sql_group_by();
|
||||
for (String itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_group_by() {}
|
||||
}
|
||||
class Sql_order_by_itm {
|
||||
public String Name() {return name;} private String name;
|
||||
public boolean Ascending() {return ascending;} private boolean ascending;
|
||||
public String XtoSql() {
|
||||
String ascString = ascending ? "" : " DESC";
|
||||
return name + ascString;
|
||||
}
|
||||
public static Sql_order_by_itm new_(String name, boolean ascending) {
|
||||
Sql_order_by_itm rv = new Sql_order_by_itm();
|
||||
rv.name = name; rv.ascending = ascending;
|
||||
return rv;
|
||||
} Sql_order_by_itm() {}
|
||||
}
|
||||
class Sql_order_by_sorter implements ComparerAble {
|
||||
public int compare(Object lhsObj, Object rhsObj) {
|
||||
GfoNde lhs = (GfoNde)lhsObj; GfoNde rhs = (GfoNde)rhsObj;
|
||||
Sql_order_by_itm item = null; Object lhsData = null, rhsData = null;
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
item = items[i];
|
||||
lhsData = lhs.Read(item.Name()); rhsData = rhs.Read(item.Name());
|
||||
int compare = CompareAble_.Compare_obj(lhsData, rhsData);
|
||||
if (compare == CompareAble_.Same) continue;
|
||||
int ascendingVal = item.Ascending() ? 1 : -1;
|
||||
return compare * ascendingVal;
|
||||
}
|
||||
return CompareAble_.Same;
|
||||
}
|
||||
Sql_order_by_itm[] items;
|
||||
public static ComparerAble new_(Sql_order_by_itm[] items) {
|
||||
Sql_order_by_sorter rv = new Sql_order_by_sorter();
|
||||
rv.items = items;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
22
140_dbs/src/gplx/dbs/Sql_qry_wtr.java
Normal file
22
140_dbs/src/gplx/dbs/Sql_qry_wtr.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
public interface Sql_qry_wtr {
|
||||
String Xto_str(Db_qry qry, boolean prepare);
|
||||
}
|
||||
24
140_dbs/src/gplx/dbs/Sql_qry_wtr_.java
Normal file
24
140_dbs/src/gplx/dbs/Sql_qry_wtr_.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Sql_qry_wtr_ {
|
||||
public static Sql_qry_wtr new_ansi() {return new Sql_qry_wtr_ansi();}
|
||||
public static Sql_qry_wtr new_escape_backslash() {return new Sql_qry_wtr_ansi_escape_backslash();}
|
||||
public static final Sql_qry_wtr I = new Sql_qry_wtr_ansi();
|
||||
public static String Gen_placeholder_parameters(Db_qry qry) {return Sql_qry_wtr_.I.Xto_str(qry, true);} // replace arguments with ?; EX: UPDATE a SET b = ? WHERE c = ?;
|
||||
}
|
||||
286
140_dbs/src/gplx/dbs/Sql_qry_wtr_ansi.java
Normal file
286
140_dbs/src/gplx/dbs/Sql_qry_wtr_ansi.java
Normal file
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
private final String_bldr sb = String_bldr_.new_();
|
||||
private boolean prepare = false;
|
||||
public String Xto_str(Db_qry cmd, boolean prepare) {
|
||||
this.prepare = prepare;
|
||||
switch (cmd.Tid()) {
|
||||
case Db_qry_.Tid_insert: return Bld_qry_insert((Db_qry_insert)cmd);
|
||||
case Db_qry_.Tid_delete: return Bld_qry_delete((Db_qry_delete)cmd);
|
||||
case Db_qry_.Tid_update: return Bld_qry_update((Db_qry_update)cmd);
|
||||
case Db_qry_.Tid_select_in_tbl:
|
||||
case Db_qry_.Tid_select: return Bld_qry_select((Db_qry_select)cmd);
|
||||
case Db_qry_.Tid_sql: return ((Db_qry_sql)cmd).Xto_sql();
|
||||
default: throw Err_.unhandled(cmd.Tid());
|
||||
}
|
||||
}
|
||||
private String Bld_qry_delete(Db_qry_delete cmd) {
|
||||
sb.Add_many("DELETE FROM ", cmd.Base_table());
|
||||
Bld_where2(sb, cmd.Where());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_insert(Db_qry_insert cmd) {
|
||||
if (cmd.Select() != null) {
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < cmd.Cols().Count(); i++) {
|
||||
Sql_select_fld_base fld = cmd.Cols().FetchAt(i);
|
||||
sb.Add(fld.Alias());
|
||||
sb.Add(i == cmd.Cols().Count() - 1 ? ") " : ", ");
|
||||
}
|
||||
sb.Add(Bld_qry_select(cmd.Select()));
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
int arg_count = cmd.Args().Count(); if (arg_count == 0) throw Err_.new_("Db_qry_insert has no columns").Add("base_table", cmd.Base_table());
|
||||
int last = arg_count - 1;
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
sb.Add(" VALUES (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
Db_arg prm = (Db_arg)pair.Val();
|
||||
this.Bld_val(sb, prm);
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_update(Db_qry_update cmd) {
|
||||
int arg_count = cmd.Args().Count(); if (arg_count == 0) throw Err_.new_("Db_qry_update has no columns").Add("base_table", cmd.Base_table());
|
||||
sb.Add_many("UPDATE ", cmd.Base_table(), " SET ");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add("=");
|
||||
this.Bld_val(sb, (Db_arg)pair.Val());
|
||||
}
|
||||
Bld_where(sb, cmd.Where());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_select(Db_qry_select cmd) {
|
||||
sb.Add("SELECT ");
|
||||
if (cmd.Cols().Distinct()) sb.Add("DISTINCT ");
|
||||
Sql_select_fld_list flds = cmd.Cols().Flds();
|
||||
if (flds.Count() == 0) sb.Add("*");
|
||||
for (int i = 0; i < flds.Count(); i++) {
|
||||
Sql_select_fld_base fld = (Sql_select_fld_base)flds.FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, fld.XtoSql());
|
||||
}
|
||||
Bld_clause_from(sb, cmd.From());
|
||||
Bld_where(sb, cmd.Where());
|
||||
Bld_select_group_by(sb, cmd.GroupBy());
|
||||
Bld_select_order_by(sb, cmd.OrderBy());
|
||||
Bld_select_limit(sb, cmd.Limit());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private void Bld_select_group_by(String_bldr sb, Sql_group_by groupBy) {
|
||||
if (groupBy == null) return;
|
||||
sb.Add(" GROUP BY ");
|
||||
for (int i = 0; i < groupBy.Flds().Count(); i++) {
|
||||
String item = (String)groupBy.Flds().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item);
|
||||
}
|
||||
}
|
||||
private void Bld_select_order_by(String_bldr sb, Sql_order_by orderBy) {
|
||||
if (orderBy == null) return;
|
||||
sb.Add(" ORDER BY ");
|
||||
for (int i = 0; i < orderBy.Flds().Count(); i++) {
|
||||
Sql_order_by_itm item = (Sql_order_by_itm)orderBy.Flds().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item.XtoSql());
|
||||
}
|
||||
}
|
||||
private void Bld_select_limit(String_bldr sb, int limit) {
|
||||
if (limit == Db_qry_select.Limit_disabled) return;
|
||||
sb.Add(" LIMIT ").Add(limit);
|
||||
}
|
||||
private void Bld_clause_from(String_bldr sb, Sql_from from) {
|
||||
for (Object tblObj : from.Tbls()) {
|
||||
Sql_tbl_src tbl = (Sql_tbl_src)tblObj;
|
||||
sb.Add_many
|
||||
( " ", String_.Upper(tbl.JoinType().Name()), " ", tbl.TblName(), String_.FormatOrEmptyStrIfNull(" {0}", tbl.Alias())
|
||||
);
|
||||
String tblAliasForJoin = tbl.Alias() == null ? tbl.TblName() : tbl.Alias();
|
||||
for (int i = 0; i < tbl.JoinLinks().Count(); i++) {
|
||||
Sql_join_itm joinLink = (Sql_join_itm)tbl.JoinLinks().FetchAt(i);
|
||||
String conjunction = i == 0 ? " ON " : " AND ";
|
||||
sb.Add_many
|
||||
( conjunction, joinLink.SrcTbl(), ".", joinLink.SrcFld(), "=", tblAliasForJoin, ".", joinLink.TrgFldOrSrcFld()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void XtoSqlCol(String_bldr sb, Object obj) {
|
||||
if (obj == null) throw Err_.null_("ColName");
|
||||
sb.Add_obj(obj); // FIXME: options for bracketing; ex: [name]
|
||||
}
|
||||
public void Bld_val(String_bldr sb, Db_arg arg) {
|
||||
if (prepare) {
|
||||
sb.Add("?");
|
||||
return;
|
||||
}
|
||||
Object val = arg.Val();
|
||||
if (val == null) {
|
||||
sb.Add("NULL");
|
||||
return;
|
||||
}
|
||||
Class<?> val_type = val.getClass();
|
||||
if (val_type == Bool_.Cls_ref_type)
|
||||
sb.Add_obj(Bool_.Xto_int(Bool_.cast_(val))); // NOTE: save boolean to 0 or 1, b/c (a) db may not support bit datatype (sqllite) and (b) avoid i18n issues with "true"/"false"
|
||||
else if
|
||||
( val_type == Byte_.Cls_ref_type || val_type == Short_.Cls_ref_type
|
||||
|| val_type == Int_.Cls_ref_type || val_type == Long_.Cls_ref_type
|
||||
|| val_type == Float_.Cls_ref_type || val_type == Double_.Cls_ref_type
|
||||
)
|
||||
sb.Add(Object_.Xto_str_strict_or_null(val));
|
||||
else if (val_type == DateAdp.class)
|
||||
Bld_val_date(sb, arg, (DateAdp)val);
|
||||
else if (val_type == DecimalAdp.class) {
|
||||
DecimalAdp valDecimal = (DecimalAdp)val;
|
||||
sb.Add(valDecimal.Xto_str());
|
||||
}
|
||||
else {
|
||||
String valString = Object_.Xto_str_strict_or_null(val);
|
||||
Bld_val_str(sb, arg, valString);
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_str(String_bldr sb, Db_arg prm, String s) {
|
||||
sb.Add_many("'", String_.Replace(s, "'", "''"), "'"); // stupid escaping of '
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_date(String_bldr sb, Db_arg prm, DateAdp s) {
|
||||
sb.Add_many("'", s.XtoStr_gplx_long(), "'");
|
||||
}
|
||||
private void Bld_where(String_bldr sb, Sql_where where) {
|
||||
if (where == null || where.Crt() == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where(sb, where.Crt());
|
||||
}
|
||||
private void Bld_where2(String_bldr sb, Criteria crt) {
|
||||
if (crt == null || crt == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where(sb, crt);
|
||||
}
|
||||
public void Bld_where(String_bldr sb, Criteria crt) {
|
||||
Criteria_bool_base crt_bool = Criteria_bool_base.as_(crt);
|
||||
if (crt_bool != null) {
|
||||
sb.Add("(");
|
||||
Bld_where(sb, crt_bool.Lhs());
|
||||
sb.Add_many(" ", crt_bool.OpLiteral(), " ");
|
||||
Bld_where(sb, crt_bool.Rhs());
|
||||
sb.Add(")");
|
||||
return;
|
||||
}
|
||||
if (crt.Crt_tid() == Criteria_.Tid_db_obj_ary) {
|
||||
Append_db_obj_ary(sb, (Db_obj_ary_crt)crt);
|
||||
}
|
||||
else {
|
||||
Criteria_wrapper leaf = Criteria_wrapper.as_(crt); if (leaf == null) throw Err_.invalid_op_(crt.XtoStr());
|
||||
sb.Add(leaf.Name_of_GfoFldCrt());
|
||||
Bld_where_crt(sb, leaf.Crt_of_GfoFldCrt());
|
||||
}
|
||||
}
|
||||
private void Bld_where_crt(String_bldr sb, Criteria crt) {
|
||||
switch (crt.Crt_tid()) {
|
||||
case Criteria_.Tid_eq: Bld_where_eq(sb, Criteria_eq.as_(crt)); break;
|
||||
case Criteria_.Tid_comp: Bld_where_comp(sb, Criteria_comp.as_(crt)); break;
|
||||
case Criteria_.Tid_between: Bld_where_between(sb, Criteria_between.as_(crt)); break;
|
||||
case Criteria_.Tid_in: Bld_where_in(sb, Criteria_in.as_(crt)); break;
|
||||
case Criteria_.Tid_like: Bld_where_like(sb, Criteria_like.as_(crt)); break;
|
||||
case Criteria_.Tid_iomatch: Bld_where_iomatch(sb, Criteria_ioMatch.as_(crt)); break;
|
||||
default: throw Err_.unhandled(crt);
|
||||
}
|
||||
}
|
||||
private void Bld_where_eq(String_bldr sb, Criteria_eq crt) {
|
||||
sb.Add(crt.Negated() ? "!=" : "=");
|
||||
this.Bld_val(sb, Wrap(crt.Value()));
|
||||
}
|
||||
private void Bld_where_comp(String_bldr sb, Criteria_comp crt) {
|
||||
sb.Add_many(crt.XtoSymbol());
|
||||
this.Bld_val(sb, Wrap(crt.Value()));
|
||||
}
|
||||
private void Bld_where_between(String_bldr sb, Criteria_between crt) {
|
||||
sb.Add(crt.Negated() ? " NOT BETWEEN " : " BETWEEN ");
|
||||
this.Bld_val(sb, Wrap(crt.Lhs()));
|
||||
sb.Add(" AND ");
|
||||
this.Bld_val(sb, Wrap(crt.Rhs()));
|
||||
}
|
||||
private void Bld_where_like(String_bldr sb, Criteria_like crt) {
|
||||
sb.Add(crt.Negated() ? " NOT LIKE " : " LIKE ");
|
||||
this.Bld_val(sb, Wrap(crt.Pattern().Raw()));
|
||||
sb.Add_fmt(" ESCAPE '{0}'", crt.Pattern().Escape());
|
||||
}
|
||||
private void Bld_where_in(String_bldr sb, Criteria_in crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IN (" : " IN (");
|
||||
Object[] crt_vals = crt.Values();
|
||||
int len = crt_vals.length;
|
||||
int last = len - 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Object val = crt_vals[i];
|
||||
this.Bld_val(sb, Wrap(val));
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
}
|
||||
private void Bld_where_iomatch(String_bldr sb, Criteria_ioMatch crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IOMATCH " : " IOMATCH ");
|
||||
this.Bld_val(sb, Wrap(crt.Pattern().Raw()));
|
||||
}
|
||||
public void Append_db_obj_ary(String_bldr sb, Db_obj_ary_crt crt) {
|
||||
Object[][] ary = crt.Vals();
|
||||
int ary_len = ary.length;
|
||||
Db_fld[] flds = crt.Flds();
|
||||
for (int i = 0; i < ary_len; i++) {
|
||||
Object[] itm = (Object[])ary[i];
|
||||
int itm_len = itm.length;
|
||||
if (i != 0) sb.Add(" OR ");
|
||||
sb.Add("(");
|
||||
for (int j = 0; j < itm_len; j++) {
|
||||
if (j != 0) sb.Add(" AND ");
|
||||
Db_fld fld = flds[j];
|
||||
Object val = itm[j];
|
||||
boolean quote = false;
|
||||
switch (fld.Type_tid()) {
|
||||
case ClassAdp_.Tid_str:
|
||||
case ClassAdp_.Tid_char:
|
||||
case ClassAdp_.Tid_date:
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
sb.Add(fld.Name());
|
||||
sb.Add("=");
|
||||
if (quote) sb.Add("'");
|
||||
sb.Add(Object_.Xto_str_strict_or_empty(val));
|
||||
if (quote) sb.Add("'");
|
||||
}
|
||||
sb.Add(")");
|
||||
}
|
||||
}
|
||||
private Db_arg Wrap(Object val) {return new Db_arg("unknown", val);}
|
||||
}
|
||||
class Sql_qry_wtr_ansi_escape_backslash extends Sql_qry_wtr_ansi { @Override public void Bld_val_str(String_bldr sb, Db_arg prm, String s) {
|
||||
if (String_.Has(s, "\\")) s = String_.Replace(s, "\\", "\\\\");
|
||||
super.Bld_val_str(sb, prm, s);
|
||||
}
|
||||
}
|
||||
61
140_dbs/src/gplx/dbs/Sql_qry_wtr_tst.java
Normal file
61
140_dbs/src/gplx/dbs/Sql_qry_wtr_tst.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class Sql_qry_wtr_tst {
|
||||
private final Sql_qry_wtr_fxt fxt = new Sql_qry_wtr_fxt();
|
||||
@Test public void Val() {
|
||||
fxt.Test_val(null , "NULL");
|
||||
fxt.Test_val(true , "1");
|
||||
fxt.Test_val(false , "0");
|
||||
fxt.Test_val(1 , "1");
|
||||
fxt.Test_val(1.1 , "1.1");
|
||||
fxt.Test_val("a" , "'a'");
|
||||
fxt.Test_val("a'b" , "'a''b'");
|
||||
}
|
||||
@Test public void Where_basic() {
|
||||
fxt.Test_where(Db_crt_.eq_("id", 1), "id=1");
|
||||
fxt.Test_where(Db_crt_.eqn_("id", 1), "id!=1");
|
||||
fxt.Test_where(Db_crt_.mt_("id", 1), "id>1");
|
||||
fxt.Test_where(Db_crt_.mte_("id", 1), "id>=1");
|
||||
fxt.Test_where(Db_crt_.lt_("id", 1), "id<1");
|
||||
fxt.Test_where(Db_crt_.lte_("id", 1), "id<=1");
|
||||
fxt.Test_where(Db_crt_.between_("id", 1, 2), "id BETWEEN 1 AND 2");
|
||||
fxt.Test_where(Db_crt_.in_("id", 1, 2, 3), "id IN (1, 2, 3)");
|
||||
}
|
||||
@Test public void Where_and() {
|
||||
fxt.Test_where(Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 AND name='me')");
|
||||
fxt.Test_where(Criteria_.Or(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 OR name='me')");
|
||||
fxt.Test_where(Criteria_.Or(Db_crt_.eq_("id", 1), Criteria_.And(Db_crt_.eq_("name", "me"), Db_crt_.eq_("id", 1))), "(id=1 OR (name='me' AND id=1))");
|
||||
}
|
||||
}
|
||||
class Sql_qry_wtr_fxt {
|
||||
private final Sql_qry_wtr_ansi sql_wtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.new_ansi();
|
||||
public void Test_val(Object val, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Db_arg arg = new Db_arg("not needed", val);
|
||||
sql_wtr.Bld_val(sb, arg);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
public void Test_where(Criteria crt, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sql_wtr.Bld_where(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
}
|
||||
143
140_dbs/src/gplx/dbs/Sql_select.java
Normal file
143
140_dbs/src/gplx/dbs/Sql_select.java
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
class Sql_select {
|
||||
public Sql_select_fld_list Flds() {return flds;} Sql_select_fld_list flds = Sql_select_fld_list.new_();
|
||||
public boolean Distinct() {return distinct;} public void Distinct_set(boolean v) {distinct = v;} private boolean distinct;
|
||||
public void Add(String fldName) {flds.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fldName, fldName));}
|
||||
public void Add(String fldName, String alias) {flds.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fldName, alias));}
|
||||
public void Add(Sql_select_fld_base fld) {flds.Add(fld);}
|
||||
|
||||
public static final Sql_select All = all_(); static Sql_select all_() {Sql_select rv = new_(); rv.Add(Sql_select_fld_wild._); return rv;}
|
||||
public static Sql_select new_() {return new Sql_select();} Sql_select() {}
|
||||
}
|
||||
abstract class Sql_select_fld_base {
|
||||
public String Tbl() {return tbl;} public void Tbl_set(String val) {tbl = val;} private String tbl;
|
||||
public String Fld() {return fld;} public void Fld_set(String val) {fld = val;} private String fld;
|
||||
public String Alias() {return alias;} public void Alias_set(String val) {alias = val;} private String alias;
|
||||
public ClassXtn ValType() {return valType;} public void ValType_set(ClassXtn val) {valType = val;} ClassXtn valType = ObjectClassXtn._;
|
||||
public abstract Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type);
|
||||
@gplx.Virtual public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@gplx.Virtual public boolean Type_fld() {return true;}
|
||||
public abstract String XtoSql();
|
||||
public static final String Tbl_null = null;
|
||||
@gplx.Internal protected void ctor_(String tbl, String fld, String alias) {
|
||||
Tbl_set(tbl); Fld_set(fld); Alias_set(alias);
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_wild extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {throw Err_.new_("group by eval not allowed on *");}
|
||||
@Override public void GroupBy_type(GfoFld fld) {throw Err_.new_("group by type not allowed on *");}
|
||||
@Override public String XtoSql() {return "*";}
|
||||
public static final Sql_select_fld_wild _ = new Sql_select_fld_wild(); Sql_select_fld_wild() {this.ctor_(Tbl_null, "*", "*");}
|
||||
}
|
||||
class Sql_select_fld_fld extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {return curVal;}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@Override public String XtoSql() {
|
||||
String rv = Fld();
|
||||
if (Tbl() != Tbl_null)
|
||||
rv = Tbl() + "." + Fld();
|
||||
if (!String_.Eq(Alias(), Fld()))
|
||||
rv = rv + " AS " + Alias();
|
||||
return rv;
|
||||
}
|
||||
public static Sql_select_fld_fld new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_fld rv = new Sql_select_fld_fld();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_fld() {}
|
||||
}
|
||||
abstract class Sql_select_fld_func_base extends Sql_select_fld_base {
|
||||
public abstract String XtoSql_functionName();
|
||||
@Override public boolean Type_fld() {return false;}
|
||||
@Override public String XtoSql() {
|
||||
return String_.Format("{0}({1}) AS {2}", XtoSql_functionName(), Fld(), Alias());
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_count extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "COUNT";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return 1;
|
||||
return Int_.cast_(groupByVal) + 1;
|
||||
}
|
||||
public static Sql_select_fld_count new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_count rv = new Sql_select_fld_count();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_count() {}
|
||||
}
|
||||
class Sql_select_fld_sum extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "SUM";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return Int_.cast_(curVal);
|
||||
return Int_.cast_(groupByVal) + Int_.cast_(curVal);
|
||||
}
|
||||
public static Sql_select_fld_sum new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_sum rv = new Sql_select_fld_sum();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_sum() {}
|
||||
}
|
||||
class Sql_select_fld_minMax extends Sql_select_fld_func_base {
|
||||
int compareType = CompareAble_.Less;
|
||||
@Override public String XtoSql_functionName() {return compareType == CompareAble_.Less ? "MIN" : "MAX";}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return curVal;
|
||||
int compareVal = CompareAble_.Compare_obj(curVal, groupByVal);
|
||||
return compareVal * compareType > 0 ? curVal : groupByVal;
|
||||
}
|
||||
public static Sql_select_fld_minMax min_(String tbl, String fld, String alias) {return new_(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld_minMax max_(String tbl, String fld, String alias) {return new_(CompareAble_.More, tbl, fld, alias);}
|
||||
static Sql_select_fld_minMax new_(int compareType, String tbl, String fld, String alias) {
|
||||
Sql_select_fld_minMax rv = new Sql_select_fld_minMax();
|
||||
rv.compareType = compareType;
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_minMax() {}
|
||||
}
|
||||
class Sql_select_fld_list {
|
||||
public int Count() {return hash.Count();}
|
||||
public void Add(Sql_select_fld_base fld) {hash.Add(fld.Alias(), fld);}
|
||||
public Sql_select_fld_base FetchAt(int i) {return (Sql_select_fld_base)hash.FetchAt(i);}
|
||||
public Sql_select_fld_base FetchOrNull(String k) {return (Sql_select_fld_base)hash.Fetch(k);}
|
||||
public GfoFldList XtoGfoFldLst(TdbTable tbl) {
|
||||
GfoFldList rv = GfoFldList_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base selectFld = this.FetchAt(i);
|
||||
GfoFld fld = tbl.Flds().FetchOrNull(selectFld.Fld());
|
||||
if (fld == null) throw Err_.new_("fld not found in tbl").Add("fldName", selectFld.Fld()).Add("tblName", tbl.Name()).Add("tblFlds", tbl.Flds().XtoStr());
|
||||
if (rv.Has(selectFld.Alias())) throw Err_.new_("alias is not unique").Add("fldName", selectFld.Fld()).Add("flds", rv.XtoStr());
|
||||
selectFld.GroupBy_type(fld);
|
||||
rv.Add(selectFld.Alias(), selectFld.ValType());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String XtoStr() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
sb.Add_fmt("{0},{1}|", fld.Fld(), fld.Alias());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
OrderedHash hash = OrderedHash_.new_();
|
||||
public static Sql_select_fld_list new_() {return new Sql_select_fld_list();} Sql_select_fld_list() {}
|
||||
}
|
||||
32
140_dbs/src/gplx/dbs/Sql_where.java
Normal file
32
140_dbs/src/gplx/dbs/Sql_where.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
class Sql_where {
|
||||
public Criteria Crt() {return crt;} Criteria crt;
|
||||
public static Sql_where merge_or_new_(Sql_where where, Criteria crt) {
|
||||
return where == null
|
||||
? Sql_where.new_(crt)
|
||||
: Sql_where.new_(Criteria_.And(where.Crt(), crt));
|
||||
}
|
||||
public static Sql_where new_(Criteria crt) {
|
||||
Sql_where rv = new Sql_where();
|
||||
rv.crt = crt;
|
||||
return rv;
|
||||
} Sql_where() {}
|
||||
}
|
||||
46
140_dbs/src/gplx/dbs/engines/mems/Db_engine__mem.java
Normal file
46
140_dbs/src/gplx/dbs/engines/mems/Db_engine__mem.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_engine__mem implements Db_engine {
|
||||
private final HashAdp tbl_hash = HashAdp_.new_();
|
||||
public Db_engine__mem(Db_url url, Db_meta_tbl[] meta_tbls) {
|
||||
this.url = url;
|
||||
int tbls_len = meta_tbls.length;
|
||||
for (int i = 0; i < tbls_len; ++i) {
|
||||
Db_meta_tbl meta_tbl = meta_tbls[i];
|
||||
Mem_tbl tbl = new Mem_tbl();
|
||||
tbl_hash.Add(meta_tbl.Name(), tbl);
|
||||
}
|
||||
}
|
||||
public String Tid() {return Db_url__mem.Tid_const;}
|
||||
public Db_url Url() {return url;} private Db_url url;
|
||||
public Db_engine New_clone(Db_url url) {return this;}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt__mem(this, qry);}
|
||||
public Mem_tbl Tbls_get(String name) {return (Mem_tbl)tbl_hash.Fetch(name);}
|
||||
public void Txn_bgn() {++txn_count;} private int txn_count = 0;
|
||||
public void Txn_end() {--txn_count;}
|
||||
public Object Exec_as_obj(Db_qry qry) {throw Err_.not_implemented_();}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {
|
||||
if (txn_count != 0) throw Err_.new_("Conn_term.txns still open; txn_count={0}", txn_count);
|
||||
}
|
||||
public Db_rdr New_rdr_by_obj(Object rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public static final Db_engine__mem _ = new Db_engine__mem(); Db_engine__mem() {}
|
||||
}
|
||||
57
140_dbs/src/gplx/dbs/engines/mems/Db_rdr__mem.java
Normal file
57
140_dbs/src/gplx/dbs/engines/mems/Db_rdr__mem.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_rdr__mem implements Db_rdr {
|
||||
private final Mem_itm[] rows; private int row_idx = -1; private final int rows_len;
|
||||
private final HashAdp ords = HashAdp_.new_();
|
||||
private Mem_itm row;
|
||||
public Db_rdr__mem(String[] cols, Mem_itm[] rows) {
|
||||
this.rows = rows; this.rows_len = rows.length;
|
||||
int cols_len = cols.length;
|
||||
for (int i = 0; i < cols_len; ++i)
|
||||
ords.Add(cols[i], i);
|
||||
}
|
||||
public boolean Move_next() {
|
||||
boolean rv = ++row_idx < rows_len;
|
||||
if (rv)
|
||||
row = rows[row_idx];
|
||||
return rv;
|
||||
}
|
||||
public byte[] Read_bry(int i) {return (byte[])row.Get_at(i);}
|
||||
public byte[] Read_bry(String k) {return (byte[])row.Get_at(Ord_by_key(k));}
|
||||
public String Read_str(int i) {return (String)row.Get_at(i);}
|
||||
public String Read_str(String k) {return (String)row.Get_at(Ord_by_key(k));}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.new_utf8_((String)row.Get_at(i));}
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.new_utf8_((String)row.Get_at(Ord_by_key(k)));}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561((String)row.Get_at(i));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561((String)row.Get_at(Ord_by_key(k)));}
|
||||
public byte Read_byte(int i) {return Byte_.cast_(row.Get_at(i));}
|
||||
public byte Read_byte(String k) {return Byte_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public int Read_int(int i) {return Int_.cast_(row.Get_at(i));}
|
||||
public int Read_int(String k) {return Int_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public long Read_long(int i) {return Long_.cast_(row.Get_at(i));}
|
||||
public long Read_long(String k) {return Long_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public float Read_float(int i) {return Float_.cast_(row.Get_at(i));}
|
||||
public float Read_float(String k) {return Float_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public double Read_double(int i) {return Double_.cast_(row.Get_at(i));}
|
||||
public double Read_double(String k) {return Double_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public boolean Read_bool_by_byte(int i) {return Byte_.cast_(row.Get_at(i)) == 1;}
|
||||
public boolean Read_bool_by_byte(String k) {return Byte_.cast_(row.Get_at(Ord_by_key(k))) == 1;}
|
||||
public void Rls() {}
|
||||
private int Ord_by_key(String k) {return Int_.cast_(ords.Fetch(k));}
|
||||
}
|
||||
127
140_dbs/src/gplx/dbs/engines/mems/Db_stmt__mem.java
Normal file
127
140_dbs/src/gplx/dbs/engines/mems/Db_stmt__mem.java
Normal file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_stmt__mem implements Db_stmt {
|
||||
private int val_idx;
|
||||
public Db_stmt__mem(Db_engine__mem engine, Db_qry qry) {this.engine = engine; this.qry = qry;} private Db_engine__mem engine;
|
||||
public int Args_len() {return args.Count();}
|
||||
public Object Args_get_at(int i) {return args.FetchAt(i);}
|
||||
public Db_qry Qry() {return qry;} private Db_qry qry;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
this.Clear();
|
||||
}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_str(String k, String v) {return Add_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
gplx.ios.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(++val_idx, bfr.Xto_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Insert(this) == 1;
|
||||
}
|
||||
public int Exec_update() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Update(this);
|
||||
}
|
||||
public int Exec_delete() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Delete(this);
|
||||
}
|
||||
public DataRdr Exec_select() {throw Err_.not_implemented_();}
|
||||
public Db_rdr Exec_select_as_rdr() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Select(this);
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec select_val: tbl={0}", qry.Base_table());}
|
||||
}
|
||||
private void Add(int idx, Object v) {args.Add(v);} private ListAdp args = ListAdp_.new_();
|
||||
}
|
||||
33
140_dbs/src/gplx/dbs/engines/mems/Db_url__mem.java
Normal file
33
140_dbs/src/gplx/dbs/engines/mems/Db_url__mem.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_url__mem extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mem";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__mem rv = new Db_url__mem();
|
||||
rv.Ctor("", m.ReadStr("database"), raw, raw);
|
||||
return rv;
|
||||
}
|
||||
public static Db_url new_(String database) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "database", database
|
||||
));
|
||||
}
|
||||
public static final Db_url__mem I = new Db_url__mem(); Db_url__mem() {}
|
||||
}
|
||||
29
140_dbs/src/gplx/dbs/engines/mems/Mem_itm.java
Normal file
29
140_dbs/src/gplx/dbs/engines/mems/Mem_itm.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Mem_itm implements GfoInvkAble {
|
||||
private final OrderedHash hash = OrderedHash_.new_();
|
||||
public Object Get_by(String key) {return hash.Fetch(key);}
|
||||
public Object Get_at(int i) {return hash.FetchAt(i);}
|
||||
public void Set_by(String key, Object val) {hash.AddReplace(key, val);}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
Object rv = Get_by(k);
|
||||
if (rv == null) return GfoInvkAble_.Rv_unhandled;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
91
140_dbs/src/gplx/dbs/engines/mems/Mem_tbl.java
Normal file
91
140_dbs/src/gplx/dbs/engines/mems/Mem_tbl.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.criterias.*;
|
||||
public class Mem_tbl {
|
||||
private final ListAdp rows = ListAdp_.new_(); private final ListAdp where_rows = ListAdp_.new_();
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
Db_qry_insert qry = (Db_qry_insert)stmt.Qry();
|
||||
String[] cols = qry.Cols_for_insert(); int len = cols.length;
|
||||
Mem_itm itm = new Mem_itm();
|
||||
for (int i = 0; i < len; ++i)
|
||||
itm.Set_by(cols[i], stmt.Args_get_at(i));
|
||||
rows.Add(itm);
|
||||
return 1;
|
||||
}
|
||||
public int Update(Db_stmt__mem stmt) {
|
||||
Db_qry_update qry = (Db_qry_update)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Cols_where());
|
||||
int where_rows_len = where_rows.Count();
|
||||
String[] update_cols = qry.Cols_update(); int update_cols_len = update_cols.length;
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)where_rows.FetchAt(i);
|
||||
for (int j = 0; j < update_cols_len; ++j)
|
||||
itm.Set_by(update_cols[j], stmt.Args_get_at(j));
|
||||
}
|
||||
return where_rows_len;
|
||||
}
|
||||
public int Delete(Db_stmt__mem stmt) {
|
||||
Db_qry_delete qry = (Db_qry_delete)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Where_cols());
|
||||
int where_rows_len = where_rows.Count();
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)where_rows.FetchAt(i);
|
||||
rows.Del(itm);
|
||||
}
|
||||
return where_rows_len;
|
||||
}
|
||||
public Db_rdr Select(Db_stmt__mem stmt) {
|
||||
Db_qry__select_in_tbl qry = (Db_qry__select_in_tbl)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Where_flds());
|
||||
return new Db_rdr__mem(qry.Select_flds(), (Mem_itm[])where_rows.Xto_ary_and_clear(Mem_itm.class));
|
||||
}
|
||||
private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, String[] where_cols) {
|
||||
int where_len = where_cols.length;
|
||||
KeyVal[] where_kvs = Bld_where_kvs(stmt, where_cols, where_len);
|
||||
Bld_where_rows(where_rows, where_kvs, where_len);
|
||||
}
|
||||
private KeyVal[] Bld_where_kvs(Db_stmt__mem stmt, String[] keys, int keys_len) {
|
||||
KeyVal[] rv = new KeyVal[keys_len];
|
||||
for (int i = 0; i < keys_len; ++i)
|
||||
rv[i] = KeyVal_.new_(keys[i], stmt.Args_get_at(i));
|
||||
return rv;
|
||||
}
|
||||
private void Bld_where_rows(ListAdp rv, KeyVal[] where_kvs, int where_len) {
|
||||
rv.Clear();
|
||||
int rows_len = rows.Count();
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)rows.FetchAt(i);
|
||||
for (int j = 0; j < where_len; ++j) {
|
||||
KeyVal kv = where_kvs[j];
|
||||
Object itm_val = itm.Get_by(kv.Key());
|
||||
if (!Object_.Eq(itm_val, kv.Val())) break;
|
||||
}
|
||||
rv.Add(itm);
|
||||
}
|
||||
}
|
||||
// private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, Criteria crt) {
|
||||
// rv.Clear();
|
||||
// int rows_len = rows.Count();
|
||||
// for (int i = 0; i < rows_len; ++i) {
|
||||
// Mem_itm itm = (Mem_itm)rows.FetchAt(i);
|
||||
// if (crt.Matches(itm))
|
||||
// rv.Add(itm);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
19
140_dbs/src/gplx/dbs/sqls/Sql_dummy_class.java
Normal file
19
140_dbs/src/gplx/dbs/sqls/Sql_dummy_class.java
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Sql_dummy_class {}
|
||||
Reference in New Issue
Block a user