mirror of
https://github.com/gnosygnu/xowa.git
synced 2025-06-13 12:54:14 +00:00
v2.2.4.1
This commit is contained in:
parent
3df6db4b7b
commit
f495595da4
@ -20,6 +20,7 @@ public class Bool_obj_ref {
|
||||
public boolean Val() {return val;} private boolean val;
|
||||
public boolean Val_y() {return val;}
|
||||
public boolean Val_n() {return !val;}
|
||||
public String Val_as_str_yn() {return Yn.Xto_str(val);}
|
||||
public Bool_obj_ref Val_y_() {val = true; return this;}
|
||||
public Bool_obj_ref Val_n_() {val = false; return this;}
|
||||
public Bool_obj_ref Val_(boolean v) {val = v; return this;}
|
||||
|
@ -23,6 +23,7 @@ public class Int_obj_ref {
|
||||
public int Val_add(int v) {val += v; return val;}
|
||||
public Int_obj_ref Val_zero_() {val = 0; return this;}
|
||||
public Int_obj_ref Val_neg1_() {val = -1; return this;}
|
||||
public String Val_as_str() {return Int_.Xto_str(val);}
|
||||
@Override public String toString() {return Int_.Xto_str(val);}
|
||||
@Override public int hashCode() {return val;}
|
||||
@Override public boolean equals(Object obj) {return val == ((Int_obj_ref)obj).Val();}
|
||||
|
@ -40,6 +40,7 @@ public class Err_ { //_20110415
|
||||
public static Err find_failed_(String find) {return Err.hdr_("find failed").Add("find", find);}
|
||||
|
||||
public static Err null_(String obj) {return Err.hdr_("null obj").Add("obj", obj);}
|
||||
public static Err deprecated(String s) {return Err.hdr_("deprecated:" + s);}
|
||||
public static Err not_implemented_() {return not_implemented_msg_("method not implemented");}
|
||||
public static Err not_implemented_msg_(String hdr) {return Err.hdr_(hdr);}
|
||||
public static Err type_mismatch_exc_(Exception e, Class<?> t, Object o) {return type_mismatch_(t, o);} // NOTE: e passed to "soak" up variable for IDE
|
||||
|
@ -131,6 +131,7 @@ public class Bry_bfr {
|
||||
public Bry_bfr Add_byte_quote() {return Add_byte(Byte_ascii.Quote);}
|
||||
public Bry_bfr Add_byte_space() {return Add_byte(Byte_ascii.Space);}
|
||||
public Bry_bfr Add_byte_nl() {return Add_byte(Byte_ascii.NewLine);}
|
||||
public Bry_bfr Add_byte_dot() {return Add_byte(Byte_ascii.Dot);}
|
||||
public Bry_bfr Add_byte(byte val) {
|
||||
int newPos = bfr_len + 1;
|
||||
if (newPos > bfr_max) Resize(bfr_len * 2);
|
||||
|
@ -329,6 +329,16 @@ public class String_ implements GfoInvkAble {
|
||||
}
|
||||
|
||||
public static String[] Ary(String... ary) {return ary;}
|
||||
public static String[] Ary_wo_null(String... ary) {
|
||||
ListAdp list = ListAdp_.new_();
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
String itm = ary[i];
|
||||
if (itm == null) continue;
|
||||
list.Add(itm);
|
||||
}
|
||||
return list.XtoStrAry();
|
||||
}
|
||||
public static String AryXtoStr(String... ary) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (String s : ary)
|
||||
|
@ -30,6 +30,7 @@ public class Hash_adp_bry extends gplx.lists.HashAdp_base implements HashAdp {
|
||||
public Object Get_by_bry(byte[] src) {return super.Fetch_base(key_ref.Init(src));}
|
||||
public Object Get_by_mid(byte[] src, int bgn, int end) {return super.Fetch_base(key_ref.Init(src, bgn, end));}
|
||||
public Hash_adp_bry Add_bry_byte(byte[] key, byte val) {this.Add_base(key, Byte_obj_val.new_(val)); return this;}
|
||||
public Hash_adp_bry Add_bry_int(byte[] key, int val) {this.Add_base(key, Int_obj_val.new_(val)); return this;}
|
||||
public Hash_adp_bry Add_bry_bry(byte[] key) {this.Add_base(key, key); return this;}
|
||||
public Hash_adp_bry Add_str_byte(String key, byte val) {this.Add_base(Bry_.new_utf8_(key), Byte_obj_val.new_(val)); return this;}
|
||||
public Hash_adp_bry Add_str_obj(String key, Object val) {this.Add_base(Bry_.new_utf8_(key), val); return this;}
|
||||
|
@ -22,9 +22,9 @@ public class OrderedHash_ {
|
||||
public static OrderedHash new_bry_() {return new OrderedHash_bry();}
|
||||
}
|
||||
class OrderedHash_bry extends OrderedHash_base {
|
||||
private Bry_obj_ref lkp = Bry_obj_ref.null_();
|
||||
private final Bry_obj_ref tmp_ref = Bry_obj_ref.null_();
|
||||
@Override protected void Add_base(Object key, Object val) {super.Add_base(Bry_obj_ref.new_((byte[])key), val);}
|
||||
@Override protected void Del_base(Object key) {super.Del_base(lkp.Val_((byte[])key));}
|
||||
@Override protected boolean Has_base(Object key) {return super.Has_base(lkp.Val_((byte[])key));}
|
||||
@Override protected Object Fetch_base(Object key) {return super.Fetch_base(lkp.Val_((byte[])key));}
|
||||
@Override protected void Del_base(Object key) {synchronized (tmp_ref) {super.Del_base(tmp_ref.Val_((byte[])key));}}
|
||||
@Override protected boolean Has_base(Object key) {synchronized (tmp_ref) {return super.Has_base(tmp_ref.Val_((byte[])key));}}
|
||||
@Override protected Object Fetch_base(Object key) {synchronized (tmp_ref) {return super.Fetch_base(tmp_ref.Val_((byte[])key));}}
|
||||
}
|
||||
|
@ -94,6 +94,13 @@ public class Io_mgr { // exists primarily to gather all cmds under gplx namespac
|
||||
catch (Exception e) {throw Err_.new_("failed to load file").Add("url", url.Xto_api()).Add("e", Err_.Message_lang(e));}
|
||||
finally {stream.Rls();}
|
||||
}
|
||||
public byte[] LoadFilBry_loose(Io_url url) {return Bry_.new_utf8_(LoadFilStr_loose(url));}
|
||||
public String LoadFilStr_loose(Io_url url) {
|
||||
String rv = LoadFilStr_args(url).BomUtf8Convert_(Bool_.Y).MissingIgnored_(Bool_.Y).Exec();
|
||||
if (String_.Has(rv, "\r\n"))
|
||||
rv = String_.Replace(rv, "\r\n", "\n");
|
||||
return rv;
|
||||
}
|
||||
public void AppendFilBfr(Io_url url, Bry_bfr bfr) {AppendFilByt(url, bfr.Bfr(), 0, bfr.Len()); bfr.ClearAndReset();}
|
||||
public void AppendFilByt(Io_url url, byte[] val) {AppendFilByt(url, val, 0, val.length);}
|
||||
public void AppendFilByt(Io_url url, byte[] val, int len) {AppendFilByt(url, val, 0, len);}
|
||||
|
@ -155,7 +155,7 @@ public class IoEngine_system extends IoEngine_base {
|
||||
}
|
||||
IoItmFil QueryMkr_fil(IoUrlInfo urlInfo, File apiFil) {
|
||||
Io_url filUrl = Io_url_.new_inf_(apiFil.getPath(), urlInfo); // NOTE: may throw PathTooLongException when url is > 248 (exception messages states 260)
|
||||
long fil_len = apiFil.exists() ? apiFil.length() : IoItmFil.Size_Invalid; // NOTE: if file doesn't exist, set len to -1; needed for "boolean Exists() {return size != Size_Invalid;}"; DATE:2014-06-21
|
||||
long fil_len = apiFil.exists() ? apiFil.length() : IoItmFil.Size_invalid; // NOTE: if file doesn't exist, set len to -1; needed for "boolean Exists() {return size != Size_Invalid;}"; DATE:2014-06-21
|
||||
IoItmFil rv = IoItmFil_.new_(filUrl, fil_len, DateAdp_.MinValue, DateAdp_.unixtime_lcl_ms_(apiFil.lastModified()));
|
||||
rv.ReadOnly_(!apiFil.canWrite());
|
||||
return rv;
|
||||
@ -404,7 +404,7 @@ public class IoEngine_system extends IoEngine_base {
|
||||
src_conn = (HttpURLConnection)src_url.openConnection();
|
||||
// src_conn.setReadTimeout(5000); // do not set; if file does not exist, will wait 5 seconds before timing out; want to fail immediately
|
||||
String user_agent = xrg.User_agent(); if (user_agent != null) src_conn.setRequestProperty("User-Agent", user_agent);
|
||||
long content_length = Long_.parse_or_(src_conn.getHeaderField("Content-Length"), IoItmFil.Size_Invalid_int);
|
||||
long content_length = Long_.parse_or_(src_conn.getHeaderField("Content-Length"), IoItmFil.Size_invalid_int);
|
||||
xrg.Src_content_length_(content_length);
|
||||
if (xrg.Src_last_modified_query()) // NOTE: only files will have last modified (api calls will not); if no last_modified, then src_conn will throw get nullRef; avoid nullRef
|
||||
xrg.Src_last_modified_(DateAdp_.unixtime_lcl_ms_(src_conn.getLastModified()));
|
||||
@ -535,7 +535,7 @@ class Io_stream_rdr_http implements Io_stream_rdr {
|
||||
} private IoEngine_xrg_downloadFil xrg;
|
||||
public byte Tid() {return Io_stream_.Tid_file;}
|
||||
public Io_url Url() {return url;} public Io_stream_rdr Url_(Io_url v) {url = v; return this;} private Io_url url;
|
||||
public long Len() {return len;} public Io_stream_rdr Len_(long v) {len = v; return this;} private long len = IoItmFil.Size_Invalid; // NOTE: must default size to -1; DATE:2014-06-21
|
||||
public long Len() {return len;} public Io_stream_rdr Len_(long v) {len = v; return this;} private long len = IoItmFil.Size_invalid; // NOTE: must default size to -1; DATE:2014-06-21
|
||||
private String src_str; private HttpURLConnection src_conn; private java.io.BufferedInputStream src_stream;
|
||||
private Io_download_fmt xfer_fmt; private Gfo_usr_dlg prog_dlg;
|
||||
private boolean read_done = true, read_failed = false;
|
||||
@ -555,7 +555,7 @@ class Io_stream_rdr_http implements Io_stream_rdr {
|
||||
if (user_agent != null)
|
||||
src_conn.setRequestProperty("User-Agent", user_agent);
|
||||
// src_conn.setReadTimeout(5000); // do not set; if file does not exist, will wait 5 seconds before timing out; want to fail immediately
|
||||
long content_length = Long_.parse_or_(src_conn.getHeaderField("Content-Length"), IoItmFil.Size_Invalid_int);
|
||||
long content_length = Long_.parse_or_(src_conn.getHeaderField("Content-Length"), IoItmFil.Size_invalid_int);
|
||||
xrg.Src_content_length_(content_length);
|
||||
this.len = content_length;
|
||||
if (xrg.Src_last_modified_query()) // NOTE: only files will have last modified (api calls will not); if no last_modified, then src_conn will throw get nullRef; avoid nullRef
|
||||
|
@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.ios; import gplx.*;
|
||||
public class IoItmFil extends IoItm_base {
|
||||
@Override public int TypeId() {return IoItmFil.Type_Fil;} @Override public boolean Type_dir() {return false;} @Override public boolean Type_fil() {return true;} public static final int Type_Fil = 2;
|
||||
public boolean Exists() {return size != Size_Invalid;} // NOTE: questionable logic, but preserved for historical reasons; requires that length be set to -1 if !.exists
|
||||
public boolean Exists() {return size != Size_invalid;} // NOTE: questionable logic, but preserved for historical reasons; requires that length be set to -1 if !.exists
|
||||
public DateAdp ModifiedTime() {return modifiedTime;}
|
||||
public IoItmFil ModifiedTime_(DateAdp val) {modifiedTime = val; return this;} DateAdp modifiedTime;
|
||||
public IoItmFil ModifiedTime_(String val) {return ModifiedTime_(DateAdp_.parse_gplx(val));}
|
||||
@ -37,6 +37,6 @@ public class IoItmFil extends IoItm_base {
|
||||
else return super.Invk(ctx, ikey, k, m);
|
||||
}
|
||||
@gplx.Internal protected IoItmFil() {}
|
||||
public static final long Size_Invalid = -1;
|
||||
public static final int Size_Invalid_int = -1;
|
||||
public static final long Size_invalid = -1;
|
||||
public static final int Size_invalid_int = -1;
|
||||
}
|
||||
|
30
100_core/src_200_io/gplx/ios/Io_url_obj_ref.java
Normal file
30
100_core/src_200_io/gplx/ios/Io_url_obj_ref.java
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.ios; import gplx.*;
|
||||
public class Io_url_obj_ref {
|
||||
public Io_url Val() {return val;} public Io_url_obj_ref Val_(Io_url v) {val = v; return this;} private Io_url val;
|
||||
public String Val_as_str() {return val.Raw();}
|
||||
@Override public String toString() {return val.Raw();}
|
||||
@Override public int hashCode() {return val.hashCode();}
|
||||
@Override public boolean equals(Object obj) {return String_.Eq(val.Raw(), ((Io_url_obj_ref)obj).val.Raw());}
|
||||
public static Io_url_obj_ref new_(Io_url val) {
|
||||
Io_url_obj_ref rv = new Io_url_obj_ref();
|
||||
rv.val = val;
|
||||
return rv;
|
||||
} Io_url_obj_ref() {}
|
||||
}
|
@ -93,14 +93,14 @@ public class ProcessAdp implements GfoInvkAble, RlsAble {
|
||||
}
|
||||
static final String Invk_cmd = "cmd", Invk_cmd_ = "cmd_", Invk_args = "args", Invk_args_ = "args_", Invk_cmd_args_ = "cmd_args_", Invk_enabled = "enabled", Invk_enabled_ = "enabled_", Invk_mode_ = "mode_", Invk_timeout_ = "timeout_", Invk_tmp_dir_ = "tmp_dir_", Invk_owner = "owner";
|
||||
Bry_fmtr_eval_mgr cmd_url_eval;
|
||||
public static ProcessAdp ini_(GfoInvkAble owner, Gfo_usr_dlg gui_wtr, ProcessAdp process, Bry_fmtr_eval_mgr cmd_url_eval, byte run_mode, int timeout, String cmd_url_fmt, String args_fmt, String... args_keys) {
|
||||
public static ProcessAdp ini_(GfoInvkAble owner, Gfo_usr_dlg usr_dlg, ProcessAdp process, Bry_fmtr_eval_mgr cmd_url_eval, byte run_mode, int timeout, String cmd_url_fmt, String args_fmt, String... args_keys) {
|
||||
process.Run_mode_(run_mode).Thread_timeout_seconds_(timeout);
|
||||
process.cmd_url_eval = cmd_url_eval;
|
||||
Io_url cmd_url = Bry_fmtr_eval_mgr_.Eval_url(cmd_url_eval, Bry_.new_utf8_(cmd_url_fmt));
|
||||
process.Exe_url_(cmd_url).Tmp_dir_(cmd_url.OwnerDir());
|
||||
process.Args_fmtr().Fmt_(args_fmt).Keys_(args_keys);
|
||||
process.owner = owner;
|
||||
process.Prog_dlg_(gui_wtr);
|
||||
process.Prog_dlg_(usr_dlg);
|
||||
return process; // return process for chaining
|
||||
}
|
||||
public static String Escape_ampersands_if_process_is_cmd(boolean os_is_wnt, String exe_url, String exe_args) {
|
||||
|
@ -1,13 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="src_110_dbQry"/>
|
||||
<classpathentry kind="src" path="src_120_sql"/>
|
||||
<classpathentry kind="src" path="src_130_misc"/>
|
||||
<classpathentry kind="src" path="src_200_engine"/>
|
||||
<classpathentry kind="src" path="src_300_sqlDbs"/>
|
||||
<classpathentry kind="src" path="src_400_tdbs"/>
|
||||
<classpathentry kind="src" path="src_410_stores"/>
|
||||
<classpathentry kind="src" path="tst"/>
|
||||
<classpathentry kind="src" path="xtn"/>
|
||||
<classpathentry kind="src" path="/100_core"/>
|
||||
|
42
140_dbs/src/gplx/dbs/Db_cmd_mode.java
Normal file
42
140_dbs/src/gplx/dbs/Db_cmd_mode.java
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_cmd_mode {
|
||||
Db_cmd_mode(int val) {this.val = val;}
|
||||
public int Val() {return val;} int val;
|
||||
public Db_cmd_mode MarkUpdated() {return this == Retrieved ? Updated : this;} // Created/Deleted noops
|
||||
public boolean Modified() {return this == Created || this == Updated;}
|
||||
public static final byte Tid_create = 1, Tid_update = 2, Tid_delete = 3, Tid_ignore = 4;
|
||||
public static final Db_cmd_mode
|
||||
Created = new Db_cmd_mode(Tid_create)
|
||||
, Updated = new Db_cmd_mode(Tid_update)
|
||||
, Deleted = new Db_cmd_mode(Tid_delete)
|
||||
, Retrieved = new Db_cmd_mode(Tid_ignore)
|
||||
;
|
||||
public static byte To_update(byte cur) {
|
||||
switch (cur) {
|
||||
case Tid_create: // ignore update if item is already marked for create
|
||||
case Tid_delete: // ignore update if item is already marked for delete (might want to throw error)
|
||||
return cur;
|
||||
case Tid_ignore: // must mark for update
|
||||
case Tid_update: // return self
|
||||
return Tid_update;
|
||||
default: throw Err_.unhandled(cur);
|
||||
}
|
||||
}
|
||||
}
|
@ -16,36 +16,39 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.qrys.*;
|
||||
public class Db_conn {
|
||||
private final Db_engine engine;
|
||||
private final ListAdp itm_list = ListAdp_.new_();
|
||||
private final ListAdp rls_list = ListAdp_.new_();
|
||||
public Db_conn(Db_engine engine) {
|
||||
this.engine = engine;
|
||||
this.txn_mgr = new Db_txn_mgr_base(engine);
|
||||
this.txn_mgr = new Db_txn_mgr(engine);
|
||||
}
|
||||
public Db_url Url() {return engine.Url();}
|
||||
public Db_txn_mgr Txn_mgr() {return txn_mgr;} private final Db_txn_mgr txn_mgr;
|
||||
public Db_stmt New_stmt_insert(String tbl, Db_meta_fld_list flds) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, flds.To_str_ary()));}
|
||||
public Db_stmt New_stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt New_stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, cols));}
|
||||
public Db_stmt New_stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt New_stmt_select_all_where(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols));}
|
||||
public Db_stmt New_stmt_select_all_where(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary()));}
|
||||
public Db_stmt New_stmt_update_by_meta(String tbl, Db_meta_fld_list flds, String... where) {
|
||||
return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, flds.To_str_ary_exclude(where)));
|
||||
public Db_stmt Stmt_insert(String tbl, Db_meta_fld_list flds) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, flds.To_str_ary()));}
|
||||
public Db_stmt Stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt Stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, cols));}
|
||||
public Db_stmt Stmt_update_exclude(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, flds.To_str_ary_exclude(where)));}
|
||||
public Db_stmt Stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt Stmt_select(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols));}
|
||||
public Db_stmt Stmt_select(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary()));}
|
||||
public void Exec_create_tbl_and_idx(Db_meta_tbl meta) {
|
||||
engine.Exec_create_tbl(meta);
|
||||
engine.Exec_create_idx(Gfo_usr_dlg_.Null, meta.Idxs());
|
||||
}
|
||||
public void Itms_add(Db_conn_itm itm) {itm_list.Add(itm);}
|
||||
public void Itms_del(Db_conn_itm itm) {itm_list.Del(itm);}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... idxs) {engine.Exec_create_idx(usr_dlg, idxs);}
|
||||
public Db_stmt Rls_reg(Db_stmt stmt) {rls_list.Add(stmt); return stmt;}
|
||||
public void Conn_term() {
|
||||
int len = itm_list.Count();
|
||||
int len = rls_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_conn_itm itm = (Db_conn_itm)itm_list.FetchAt(i);
|
||||
itm.Conn_term();
|
||||
RlsAble itm = (RlsAble)rls_list.FetchAt(i);
|
||||
itm.Rls();
|
||||
}
|
||||
engine.Conn_term();
|
||||
Db_conn_pool_old._.Del(this.Url()); // remove from pool, else rls'd instance will be cached and fail upon next use
|
||||
// Db_conn_pool.I.Del(this.Url()); // remove from pool, else rls'd instance will be cached and fail upon next use
|
||||
}
|
||||
public Db_stmt New_stmt(Db_qry qry) {return engine.New_stmt_prep(qry);}
|
||||
public Db_stmt Stmt_new(Db_qry qry) {return engine.New_stmt_prep(qry);}
|
||||
public int Exec_qry(Db_qry qry) {txn_mgr.Txn_count_(txn_mgr.Txn_count() + 1); return Int_.cast_(engine.Exec_as_obj(qry));}
|
||||
public DataRdr Exec_qry_as_rdr(Db_qry qry) {return DataRdr_.cast_(engine.Exec_as_obj(qry));}
|
||||
public int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
|
||||
|
@ -16,19 +16,9 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Db_conn_ {
|
||||
public static final Db_conn Null = new_and_open_(Db_url_.Null);
|
||||
public static Db_conn new_and_open_(Db_url url) {
|
||||
Db_engine prototype = Db_engine_regy._.Get(url.Tid());
|
||||
Db_engine engine = prototype.New_clone(url);
|
||||
engine.Conn_open(); // auto-open
|
||||
return new Db_conn(engine);
|
||||
}
|
||||
public static Db_conn Reg_itm(Db_conn_itm itm, Db_conn old_conn, Db_conn new_conn) {
|
||||
if (old_conn != null) old_conn.Itms_del(itm);
|
||||
new_conn.Itms_add(itm);
|
||||
return new_conn;
|
||||
}
|
||||
public static final Db_conn Null = Db_conn_pool.I.Get_or_new(Db_url_.Null);
|
||||
public static int Select_fld0_as_int_or(Db_conn p, String sql, int or) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
|
@ -16,25 +16,16 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
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 class Db_conn_bldr {
|
||||
private Db_conn_bldr_wkr wkr;
|
||||
public void Reg_default_sqlite() {wkr = Db_conn_bldr_wkr__sqlite.I; wkr.Clear_for_tests();}
|
||||
public void Reg_default_mem() {wkr = Db_conn_bldr_wkr__mem.I; wkr.Clear_for_tests();}
|
||||
public Db_conn Get(String type, Object url_obj) {return wkr.Get(type, url_obj);}
|
||||
public Db_conn New(String type, Object url_obj) {return wkr.New(type, url_obj);}
|
||||
public Db_conn_bldr_data Get_or_new(String type, Object url_obj) {
|
||||
boolean exists = wkr.Exists(type, url_obj);
|
||||
Db_conn conn = exists ? Get(type, url_obj) : New(type, url_obj);
|
||||
return new Db_conn_bldr_data(conn, exists);
|
||||
}
|
||||
public 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() {}
|
||||
public static final Db_conn_bldr I = new Db_conn_bldr(); Db_conn_bldr() {}
|
||||
}
|
@ -15,8 +15,10 @@ 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.fsdb; import gplx.*;
|
||||
public class Fsdb_cfg_tbl_ {
|
||||
public static Fsdb_cfg_tbl new_sql_() {return new Fsdb_cfg_tbl_sql();}
|
||||
public static Fsdb_cfg_tbl new_mem_() {return new Fsdb_cfg_tbl_mem();}
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn_bldr_data {
|
||||
public Db_conn_bldr_data(Db_conn conn, boolean exists) {this.conn = conn; this.exists = exists;}
|
||||
public Db_conn Conn() {return conn;} private final Db_conn conn;
|
||||
public boolean Exists() {return exists;} private final boolean exists;
|
||||
public boolean Created() {return !exists;}
|
||||
}
|
68
140_dbs/src/gplx/dbs/Db_conn_bldr_wkr.java
Normal file
68
140_dbs/src/gplx/dbs/Db_conn_bldr_wkr.java
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.sqlite.*;
|
||||
public interface Db_conn_bldr_wkr {
|
||||
void Clear_for_tests();
|
||||
boolean Exists(String type, Object url_obj);
|
||||
Db_conn Get(String type, Object url_obj);
|
||||
Db_conn New(String type, Object url_obj);
|
||||
}
|
||||
class Db_conn_bldr_wkr__sqlite implements Db_conn_bldr_wkr {
|
||||
public void Clear_for_tests() {}
|
||||
public boolean Exists(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj; return Io_mgr._.ExistsFil(io_url);
|
||||
}
|
||||
public Db_conn Get(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj; if (!Io_mgr._.ExistsFil(io_url)) return null;
|
||||
Db_url db_url = Db_url_.sqlite_(io_url);
|
||||
return Db_conn_pool.I.Get_or_new(db_url);
|
||||
}
|
||||
public Db_conn New(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj;
|
||||
Io_mgr._.CreateDirIfAbsent(io_url.OwnerDir()); // must assert that dir exists
|
||||
Db_url db_url = Sqlite_url.make_(io_url);
|
||||
Db_conn conn = Db_conn_pool.I.Get_or_new(db_url);
|
||||
Sqlite_engine_.Pragma_page_size(conn, 4096);
|
||||
// conn.Conn_term(); // close conn after PRAGMA adjusted
|
||||
return conn;
|
||||
}
|
||||
public static final Db_conn_bldr_wkr__sqlite I = new Db_conn_bldr_wkr__sqlite(); Db_conn_bldr_wkr__sqlite() {}
|
||||
}
|
||||
class Db_conn_bldr_wkr__mem implements Db_conn_bldr_wkr {
|
||||
private final HashAdp hash = HashAdp_.new_();
|
||||
public void Clear_for_tests() {hash.Clear(); Db_conn_pool.I.Clear();}
|
||||
public boolean Exists(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj;
|
||||
String io_url_str = io_url.Xto_api();
|
||||
return hash.Has(io_url_str);
|
||||
}
|
||||
public Db_conn Get(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj;
|
||||
String io_url_str = io_url.Xto_api();
|
||||
if (!hash.Has(io_url_str)) return null;
|
||||
return Db_conn_pool.I.Get_or_new__mem(io_url.Xto_api());
|
||||
}
|
||||
public Db_conn New(String type, Object url_obj) {
|
||||
Io_url io_url = (Io_url)url_obj;
|
||||
String io_url_str = io_url.Xto_api();
|
||||
hash.Add(io_url_str, io_url_str);
|
||||
return Db_conn_pool.I.Get_or_new__mem(io_url.Xto_api());
|
||||
}
|
||||
public static final Db_conn_bldr_wkr__mem I = new Db_conn_bldr_wkr__mem(); Db_conn_bldr_wkr__mem() {}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.primitives.*;
|
||||
public interface Db_conn_mkr {
|
||||
Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref);
|
||||
}
|
||||
class Db_conn_mkr_sqlite implements Db_conn_mkr {
|
||||
public Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref) {return Sqlite_engine_.Conn_load_or_make_(db_url, created_ref);}
|
||||
public static final Db_conn_mkr_sqlite _ = new Db_conn_mkr_sqlite(); Db_conn_mkr_sqlite() {}
|
||||
}
|
||||
class Db_conn_mkr_mem implements Db_conn_mkr {
|
||||
private boolean create;
|
||||
public Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref create_ref) {create_ref.Val_(create); return null;}
|
||||
public static Db_conn_mkr_mem create_(boolean create) {
|
||||
Db_conn_mkr_mem rv = new Db_conn_mkr_mem();
|
||||
rv.create = create;
|
||||
return rv;
|
||||
}
|
||||
}
|
@ -16,11 +16,15 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.mems.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.engines.nulls.*; import gplx.dbs.engines.mems.*; import gplx.dbs.engines.tdbs.*;
|
||||
import gplx.dbs.engines.sqlite.*; import gplx.dbs.engines.mysql.*; import gplx.dbs.engines.postgres.*;
|
||||
public class Db_conn_pool {
|
||||
private final HashAdp conn_hash = HashAdp_.new_(); private final HashAdp engine_hash = HashAdp_.new_();
|
||||
Db_conn_pool() {this.Init();}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_url_.sqlite_(url));}
|
||||
private final HashAdp conn_hash = HashAdp_.new_(); private final HashAdp engine_hash = HashAdp_.new_();
|
||||
public void Clear() {conn_hash.Clear();}
|
||||
public void Del(Db_url url) {conn_hash.Del(url.Xto_api());}
|
||||
public Db_conn Get_or_new__mem(String db) {return Get_or_new(Db_url__mem.new_(db));}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_url_.sqlite_(url));}
|
||||
public Db_conn Get_or_new(String s) {return Get_or_new(Db_url_.parse_(s));}
|
||||
public Db_conn Get_or_new(Db_url url) {
|
||||
Db_conn rv = (Db_conn)conn_hash.Fetch(url.Xto_api());
|
||||
if (rv == null) {
|
||||
@ -31,19 +35,12 @@ public class Db_conn_pool {
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public Db_conn Set_mem(String db, Db_meta_tbl... tbls) {
|
||||
Db_url url = Db_url__mem.new_(db);
|
||||
Db_engine__mem engine = new Db_engine__mem(url, tbls);
|
||||
Db_conn conn = new Db_conn(engine);
|
||||
conn_hash.AddReplace(url.Xto_api(), conn);
|
||||
return conn;
|
||||
}
|
||||
private void Init() {
|
||||
this.Engines__add(Db_engine_null._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
}
|
||||
public void Engines__add(Db_engine... ary) {
|
||||
for (Db_engine itm : ary)
|
||||
engine_hash.Add(itm.Tid(), itm);
|
||||
}
|
||||
public static final Db_conn_pool I = new Db_conn_pool();
|
||||
public static final Db_conn_pool I = new Db_conn_pool(); Db_conn_pool() {this.Init();}
|
||||
private void Init() {
|
||||
this.Engines__add(Null_engine._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
}
|
||||
}
|
||||
|
@ -37,11 +37,20 @@ public class Db_crt_ {
|
||||
Criteria crt = Db_crt_.eq_(ary[i], null);
|
||||
rv = (i == 0)? crt : Criteria_.And(rv, crt);
|
||||
}
|
||||
// if (rv == null) return Wildcard;
|
||||
// return rv.Tid() == Criteria_.Tid_wrapper ? (Criteria_fld)rv : Criteria_fld.new_(Criteria_fld.Key_null, rv);
|
||||
return rv;
|
||||
}
|
||||
// public static Criteria_fld and(Criteria and) {return Criteria_fld.new_(Criteria_fld.Key_null, and);}
|
||||
public static Criteria eq_many_wo_null(String... ary) {
|
||||
Criteria rv = null;
|
||||
int len = ary.length;
|
||||
int crt_idx = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
String itm = ary[i]; if (itm == Db_meta_fld.Key_null) continue;
|
||||
Criteria crt = Db_crt_.eq_(itm, null);
|
||||
rv = (crt_idx == 0) ? crt : Criteria_.And(rv, crt);
|
||||
++crt_idx;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Criteria eq_many_(KeyVal... array) {
|
||||
Criteria rv = null;
|
||||
for (int i = 0; i < array.length; i++) {
|
@ -29,4 +29,5 @@ public class Db_meta_fld {
|
||||
public boolean Autoincrement() {return autoincrement;} private final boolean autoincrement;
|
||||
public static final int Tid_bool = 0, Tid_byte = 1, Tid_short = 2, Tid_int = 3, Tid_long = 4, Tid_float = 5, Tid_double = 6, Tid_str = 7, Tid_text = 8, Tid_bry = 9;
|
||||
public static final String[] Ary_empy = String_.Ary_empty;
|
||||
public static final String Key_null = null;
|
||||
}
|
||||
|
@ -19,26 +19,24 @@ package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_fld_list {
|
||||
private final OrderedHash flds = OrderedHash_.new_();
|
||||
private final ListAdp keys = ListAdp_.new_();
|
||||
public void Clear() {flds.Clear(); keys.Clear();}
|
||||
public Db_meta_fld Get_by(String name) {return (Db_meta_fld)flds.Fetch(name);}
|
||||
public String[] To_str_ary() {if (str_ary == null) str_ary = (String[])keys.Xto_ary(String.class); return str_ary;} private String[] str_ary;
|
||||
public Db_meta_fld[] To_fld_ary() {if (fld_ary == null) fld_ary = (Db_meta_fld[])flds.Xto_ary(Db_meta_fld.class); return fld_ary;} private Db_meta_fld[] fld_ary;
|
||||
public String[] To_str_ary_exclude(String[] ary) {
|
||||
HashAdp ary_hash = HashAdp_.new_();
|
||||
ListAdp rv = ListAdp_.new_();
|
||||
int ary_len = ary.length;
|
||||
for (int i = 0; i < ary_len; ++i) {
|
||||
String ary_itm = ary[i];
|
||||
ary_hash.Add(ary_itm, ary_itm);
|
||||
}
|
||||
int fld_len = flds.Count();
|
||||
for (int i = 0; i < fld_len; ++i) {
|
||||
Db_meta_fld fld = (Db_meta_fld)flds.FetchAt(i);
|
||||
String fld_key = fld.Name();
|
||||
boolean include = true;
|
||||
for (int j = 0; j < ary_len; ++j) {
|
||||
String itm_key = ary[j];
|
||||
if (String_.Eq(fld_key, itm_key)) {
|
||||
include = false;
|
||||
break;
|
||||
}
|
||||
if (include)
|
||||
rv.Add(itm_key);
|
||||
}
|
||||
if (ary_hash.Has(fld_key)) continue;
|
||||
rv.Add(fld_key);
|
||||
}
|
||||
return rv.XtoStrAry();
|
||||
}
|
||||
|
@ -16,12 +16,19 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_meta_idx {
|
||||
public Db_meta_idx(String tbl, String name, boolean unique, String[] flds) {this.tbl = tbl; this.name = name; this.unique = unique; this.flds = flds;}
|
||||
Db_meta_idx(String tbl, String name, boolean unique, String[] flds) {this.tbl = tbl; this.name = name; this.unique = unique; this.flds = flds;}
|
||||
public String Tbl() {return tbl;} private final String tbl;
|
||||
public String Name() {return name;} private final String name;
|
||||
public boolean Unique() {return unique;} private final boolean unique;
|
||||
public String[] Flds() {return flds;} private final String[] flds;
|
||||
public String Tbl() {return tbl;} private final String tbl;
|
||||
public static Db_meta_idx new_unique(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, name, Bool_.Y, flds);}
|
||||
public static Db_meta_idx new_normal(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, name, Bool_.N, flds);}
|
||||
public String To_sql_create() {return Db_sqlbldr__sqlite.I.Bld_create_idx(this);}
|
||||
public static Db_meta_idx new_unique_by_name(String tbl, String idx_name, String... flds) {return new Db_meta_idx(tbl, idx_name, Bool_.Y, flds);}
|
||||
public static Db_meta_idx new_normal_by_name(String tbl, String idx_name, String... flds) {return new Db_meta_idx(tbl, idx_name, Bool_.N, flds);}
|
||||
public static Db_meta_idx new_unique_by_tbl(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, Bld_idx_name(tbl, name), Bool_.Y, flds);}
|
||||
public static Db_meta_idx new_normal_by_tbl(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, Bld_idx_name(tbl, name), Bool_.N, flds);}
|
||||
public static Db_meta_idx new_unique_by_tbl_wo_null(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, Bld_idx_name(tbl, name), Bool_.Y, String_.Ary_wo_null(flds));}
|
||||
public static Db_meta_idx new_normal_by_tbl_wo_null(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, Bld_idx_name(tbl, name), Bool_.N, String_.Ary_wo_null(flds));}
|
||||
public static String Bld_idx_name(String tbl, String suffix) {return String_.Concat(tbl, "__", suffix);}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_meta_tbl {
|
||||
public Db_meta_tbl(String name, Db_meta_fld[] flds, Db_meta_idx[] idxs) {
|
||||
this.name = name; this.flds = flds; this.idxs = idxs;
|
||||
@ -23,6 +24,7 @@ public class Db_meta_tbl {
|
||||
public String Name() {return name;} private final String name;
|
||||
public Db_meta_fld[] Flds() {return flds;} private final Db_meta_fld[] flds;
|
||||
public Db_meta_idx[] Idxs() {return idxs;} private final Db_meta_idx[] idxs;
|
||||
public String To_sql_create() {return Db_sqlbldr__sqlite.I.Bld_create_tbl(this);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld_list flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds.To_fld_ary(), idxs);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld[] flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds, idxs);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld... flds) {return new Db_meta_tbl(name, flds, null);}
|
||||
|
@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.criterias.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.qrys.*;
|
||||
public class Db_qry_ {
|
||||
public static Db_qry_select select_cols_(String tbl, Criteria crt, String... cols){return select_().From_(tbl).Where_(crt).Cols_(cols);}
|
||||
public static Db_qry_select select_val_(String tbl, String col, Criteria crt) {return select_().From_(tbl).Where_(crt).Cols_(col);}
|
@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import java.sql.ResultSet;
|
||||
class Db_rdr__basic implements Db_rdr {
|
||||
public class Db_rdr__basic implements Db_rdr {
|
||||
protected ResultSet rdr;
|
||||
public void Ctor(ResultSet rdr, String sql) {this.rdr = rdr; this.sql = sql;}
|
||||
public String Sql() {return sql;} private String sql;
|
||||
@ -25,25 +25,25 @@ class Db_rdr__basic implements Db_rdr {
|
||||
try {return rdr.next();}
|
||||
catch (Exception e) {throw Err_.new_fmt_("move_next failed; check column casting error in SQL: err={0} sql={1}", Err_.Message_lang(e), sql);}
|
||||
}
|
||||
public byte[] Read_bry(int i) {try {return rdr.getBytes(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry(String k) {try {return (byte[])rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry_by_str(int i) {try {return Bry_.new_utf8_(rdr.getString(i + 1));} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry_by_str(String k) {try {return Bry_.new_utf8_((String)rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public String Read_str(int i) {try {return rdr.getString(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public String Read_str(String k) {try {return (String)rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561(Read_str(i));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561(Read_str(k));}
|
||||
public int Read_int(int i) {try {return rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public int Read_int(String k) {try {return Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public long Read_long(int i) {try {return rdr.getLong(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public long Read_long(String k) {try {return Long_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public float Read_float(int i) {try {return rdr.getFloat(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public float Read_float(String k) {try {return Float_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public double Read_double(int i) {try {return rdr.getDouble(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public double Read_double(String k) {try {return Double_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(int i) {try {return rdr.getByte(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(String k) {try {return Byte_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public boolean Read_bool_by_byte(int i) {try {return rdr.getByte(i + 1) == 1;} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public boolean Read_bool_by_byte(String k) {try {return Byte_.cast_(rdr.getObject(k)) == 1;} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public void Rls() {try {rdr.close();} catch (Exception e) {throw Err_.new_("close failed: err={0}", Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte[] Read_bry(int i) {try {return rdr.getBytes(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte[] Read_bry(String k) {try {return (byte[])rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte[] Read_bry_by_str(int i) {try {return Bry_.new_utf8_(rdr.getString(i + 1));} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte[] Read_bry_by_str(String k) {try {return Bry_.new_utf8_((String)rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public String Read_str(int i) {try {return rdr.getString(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public String Read_str(String k) {try {return (String)rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561(Read_str(i));}
|
||||
@gplx.Virtual public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561(Read_str(k));}
|
||||
@gplx.Virtual public int Read_int(int i) {try {return rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public int Read_int(String k) {try {return Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public long Read_long(int i) {try {return rdr.getLong(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public long Read_long(String k) {try {return Long_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public float Read_float(int i) {try {return rdr.getFloat(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public float Read_float(String k) {try {return Float_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public double Read_double(int i) {try {return rdr.getDouble(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public double Read_double(String k) {try {return Double_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(int i) {try {return rdr.getByte(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(String k) {try {return Byte_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public boolean Read_bool_by_byte(int i) {try {return rdr.getByte(i + 1) == 1;} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public boolean Read_bool_by_byte(String k) {try {return Byte_.cast_(rdr.getObject(k)) == 1;} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public void Rls() {try {rdr.close();} catch (Exception e) {throw Err_.new_("close failed: err={0}", Err_.Message_lang(e));}}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.*;
|
||||
public interface Db_stmt extends RlsAble {
|
||||
Db_stmt Crt_bool_as_byte(String k, boolean v);
|
||||
Db_stmt Val_bool_as_byte(String k, boolean v);
|
||||
@ -54,6 +55,7 @@ public interface Db_stmt extends RlsAble {
|
||||
DataRdr Exec_select();
|
||||
Db_rdr Exec_select_as_rdr();
|
||||
Object Exec_select_val();
|
||||
void Ctor_stmt(Db_engine engine, Db_qry qry);
|
||||
Db_stmt Clear();
|
||||
Db_stmt Reset_stmt();
|
||||
}
|
@ -16,36 +16,37 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Db_stmt_ {
|
||||
public static final Db_stmt Null = new Db_stmt_sql();
|
||||
public static Db_stmt new_insert_(Db_conn conn, String tbl, String... flds) {
|
||||
Db_qry qry = Db_qry_insert.new_(tbl, flds);
|
||||
return conn.New_stmt(qry);
|
||||
return conn.Stmt_new(qry);
|
||||
}
|
||||
public static Db_stmt new_update_(Db_conn conn, String tbl, String[] where, String... flds) {
|
||||
Db_qry qry = Db_qry_update.new_(tbl, where, flds);
|
||||
return conn.New_stmt(qry);
|
||||
return conn.Stmt_new(qry);
|
||||
}
|
||||
public static Db_stmt new_delete_(Db_conn conn, String tbl, String... where) {
|
||||
Db_qry_delete qry = Db_qry_.delete_(tbl, Db_crt_.eq_many_(where));
|
||||
return conn.New_stmt(qry);
|
||||
return conn.Stmt_new(qry);
|
||||
}
|
||||
public static Db_stmt new_select_(Db_conn conn, String tbl, String[] where, String... flds) {
|
||||
Db_qry_select qry = Db_qry_.select_cols_(tbl, Db_crt_.eq_many_(where), flds);
|
||||
return conn.New_stmt(qry);
|
||||
return conn.Stmt_new(qry);
|
||||
}
|
||||
public static Db_stmt new_select_in_(Db_conn conn, String tbl, String in_fld, Object[] in_vals, String... flds) {
|
||||
Db_qry_select qry = Db_qry_.select_cols_(tbl, Db_crt_.in_(in_fld, in_vals), flds).OrderBy_asc_(in_fld);
|
||||
return conn.New_stmt(qry);
|
||||
return conn.Stmt_new(qry);
|
||||
}
|
||||
public static Db_stmt new_select_all_(Db_conn conn, String tbl) {
|
||||
return conn.New_stmt(Db_qry_.select_tbl_(tbl));
|
||||
return conn.Stmt_new(Db_qry_.select_tbl_(tbl));
|
||||
}
|
||||
public static Db_stmt new_select_as_rdr(Db_conn conn, Db_qry__select_in_tbl qry) {
|
||||
return conn.New_stmt(qry);
|
||||
return conn.Stmt_new(qry);
|
||||
}
|
||||
public static Db_stmt new_select_as_rdr(Db_conn conn, String sql) {
|
||||
return conn.New_stmt(Db_qry_sql.rdr_(sql));
|
||||
return conn.Stmt_new(Db_qry_sql.rdr_(sql));
|
||||
}
|
||||
public static Err err_(Exception e, Db_stmt stmt, String proc) {
|
||||
throw Err_.err_(e, String_.Format("db stmt failed: proc=~{0} err=~{1}", proc, Err_.Message_gplx_brief(e)));
|
48
140_dbs/src/gplx/dbs/Db_stmt_bldr.java
Normal file
48
140_dbs/src/gplx/dbs/Db_stmt_bldr.java
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_stmt_bldr {
|
||||
private Db_conn conn; private Db_stmt create, update, delete;
|
||||
private String tbl_name; private String[] flds_keys, flds_vals, flds_all;
|
||||
public void Conn_(Db_conn v, String tbl_name, Db_meta_fld_list flds, String... flds_keys) {
|
||||
Conn_(v, tbl_name, flds.To_str_ary(), flds.To_str_ary_exclude(flds_keys), flds_keys);
|
||||
}
|
||||
public void Conn_(Db_conn v, String tbl_name, String[] flds_vals, String... flds_keys) {
|
||||
Conn_(v, tbl_name, String_.Ary_add(flds_keys, flds_vals), flds_vals, flds_keys);
|
||||
}
|
||||
private void Conn_(Db_conn v, String tbl_name, String[] flds_all, String[] flds_vals, String... flds_keys) {
|
||||
this.conn = v; this.tbl_name = tbl_name;
|
||||
this.flds_all = flds_all; this.flds_vals = flds_vals; this.flds_keys = flds_keys;
|
||||
}
|
||||
public Db_stmt Get(byte cmd_mode) {
|
||||
switch (cmd_mode) {
|
||||
case Db_cmd_mode.Tid_create: if (create == null) create = conn.Stmt_insert(tbl_name, flds_all); return create;
|
||||
case Db_cmd_mode.Tid_update: if (update == null) update = conn.Stmt_update(tbl_name, flds_keys, flds_vals); return update;
|
||||
case Db_cmd_mode.Tid_delete: if (delete == null) delete = conn.Stmt_delete(tbl_name, flds_keys); return delete;
|
||||
case Db_cmd_mode.Tid_ignore: return Db_stmt_.Null;
|
||||
default: throw Err_.unhandled(cmd_mode);
|
||||
}
|
||||
}
|
||||
public void Batch_bgn() {conn.Txn_mgr().Txn_bgn_if_none();}
|
||||
public void Batch_end() {conn.Txn_mgr().Txn_end_all();}
|
||||
public void Rls() {
|
||||
create = Db_stmt_.Rls(create);
|
||||
update = Db_stmt_.Rls(update);
|
||||
delete = Db_stmt_.Rls(delete);
|
||||
}
|
||||
}
|
@ -16,17 +16,9 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_txn_mgr {
|
||||
int Txn_depth();
|
||||
void Txn_bgn_if_none();
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
void Txn_end_all();
|
||||
void Txn_end_all_bgn_if_none();
|
||||
int Txn_count(); void Txn_count_(int v);
|
||||
}
|
||||
class Db_txn_mgr_base implements Db_txn_mgr {
|
||||
public Db_txn_mgr_base(Db_engine engine) {this.engine = engine;} private final Db_engine engine;
|
||||
import gplx.dbs.engines.*;
|
||||
public class Db_txn_mgr {
|
||||
public Db_txn_mgr(Db_engine engine) {this.engine = engine;} private final Db_engine engine;
|
||||
public int Txn_depth() {return txn_depth;} int txn_depth; // NOTE: only support 1 level for now;
|
||||
public void Txn_bgn_if_none() {if (txn_depth == 0) this.Txn_bgn();}
|
||||
public void Txn_bgn() {
|
@ -18,78 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_url {
|
||||
String Tid();
|
||||
String Database();
|
||||
String Xto_raw();
|
||||
String Xto_api();
|
||||
Db_url New_self(String raw, GfoMsg m);
|
||||
}
|
||||
class Db_url__null extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "null_db";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {return this;}
|
||||
public static final Db_url__null _ = new Db_url__null(); Db_url__null() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
}
|
||||
class Db_url__mysql extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mysql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "uid", uid
|
||||
, "pwd", pwd
|
||||
, "charset", "utf8"
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__mysql rv = new Db_url__mysql();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("charset", "utf8")));
|
||||
rv.uid = m.ReadStr("uid");
|
||||
rv.pwd = m.ReadStr("pwd");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__mysql _ = new Db_url__mysql(); Db_url__mysql() {}
|
||||
}
|
||||
class Db_url__postgres extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "postgresql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "port", "5432"
|
||||
, "user id", uid
|
||||
, "password", pwd
|
||||
, "encoding", "unicode" // needed for 1.1 conn; otherwise, ascii
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__postgres rv = new Db_url__postgres();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("encoding", "unicode")));
|
||||
rv.uid = m.ReadStr("user id");
|
||||
rv.pwd = m.ReadStr("password");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__postgres _ = new Db_url__postgres(); Db_url__postgres() {}
|
||||
}
|
||||
class Db_url__tdb extends Db_url__base {
|
||||
public Io_url Url() {return url;} Io_url url;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "tdb";
|
||||
public static Db_url new_(Io_url url) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "url", url.Raw()
|
||||
));
|
||||
} Db_url__tdb() {}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__tdb rv = new Db_url__tdb();
|
||||
String urlStr = m.ReadStr("url");
|
||||
Io_url url = Io_url_.new_any_(urlStr);
|
||||
rv.Ctor(urlStr, url.NameOnly(), raw, BldApi(m));
|
||||
rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__tdb _ = new Db_url__tdb();
|
||||
}
|
||||
|
@ -16,20 +16,21 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.mems.*; import gplx.dbs.engines.sqlite.*;
|
||||
import gplx.dbs.engines.nulls.*; import gplx.dbs.engines.mems.*; import gplx.dbs.engines.sqlite.*; import gplx.dbs.engines.tdbs.*;
|
||||
import gplx.dbs.engines.mysql.*; import gplx.dbs.engines.postgres.*;
|
||||
public class Db_url_ {
|
||||
public static final Db_url Null = Db_url__null._;
|
||||
public static final Db_url Test = Db_url__mysql.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static final Db_url Null = Null_url._;
|
||||
public static final Db_url Test = Mysql_url.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static Db_url parse_(String raw) {return Db_url_pool._.Parse(raw);}
|
||||
public static Db_url sqlite_(Io_url url) {return Db_url__sqlite.load_(url);}
|
||||
public static Db_url tdb_(Io_url url) {return Db_url__tdb.new_(url);}
|
||||
public static Db_url sqlite_(Io_url url) {return Sqlite_url.load_(url);}
|
||||
public static Db_url tdb_(Io_url url) {return Tdb_url.new_(url);}
|
||||
public static Db_url mem_(String db) {return Db_url__mem.new_(db);}
|
||||
public static final String Key_tdb = Db_url__tdb.Tid_const;
|
||||
public static final String Key_tdb = Tdb_url.Tid_const;
|
||||
}
|
||||
class Db_url_pool {
|
||||
private OrderedHash regy = OrderedHash_.new_();
|
||||
public Db_url_pool() {
|
||||
this.Add(Db_url__null._).Add(Db_url__tdb._).Add(Db_url__mysql._).Add(Db_url__postgres._).Add(Db_url__sqlite._);
|
||||
this.Add(Null_url._).Add(Tdb_url._).Add(Mysql_url._).Add(Postgres_url._).Add(Sqlite_url._);
|
||||
this.Add(Db_url__mem.I);
|
||||
}
|
||||
public Db_url_pool Add(Db_url itm) {regy.AddReplace(itm.Tid(), itm); return this;}
|
||||
|
@ -21,7 +21,7 @@ public abstract class Db_url__base implements Db_url {
|
||||
public abstract String Tid();
|
||||
public String Xto_raw() {return raw;} private String raw = "";
|
||||
public String Xto_api() {return api;} private String api = "";
|
||||
public String Database() {return database;} private String database = "";
|
||||
public String Database() {return database;} protected String database = "";
|
||||
public String Server() {return server;} private String server = "";
|
||||
public abstract Db_url New_self(String raw, GfoMsg m);
|
||||
protected void Ctor(String server, String database, String raw, String api) {this.server = server; this.database = database; this.raw = raw; this.api = api;}
|
||||
|
@ -1,67 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.strings.*;
|
||||
public class Sql_join_itm {
|
||||
public String SrcTbl() {return srcTbl;} public Sql_join_itm SrcTbl_(String v) {srcTbl = v; return this;} private String srcTbl;
|
||||
public String SrcFld() {return srcFld;} public Sql_join_itm SrcFld_(String v) {srcFld = v; return this;} private String srcFld;
|
||||
public String TrgFld() {return trgFld;} public Sql_join_itm TrgFld_(String v) {trgFld = v; return this;} private String trgFld;
|
||||
public String TrgFldOrSrcFld() {return trgFld == null ? srcFld : trgFld;}
|
||||
public static Sql_join_itm new_(String trgFld, String srcTbl, String srcFld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = trgFld; rv.srcTbl = srcTbl; rv.srcFld = srcFld;
|
||||
return rv;
|
||||
} Sql_join_itm() {}
|
||||
public static Sql_join_itm same_(String tbl, String fld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = fld; rv.srcTbl = tbl; rv.srcFld = fld;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
class Sql_from {
|
||||
public ListAdp Tbls() {return tbls;} ListAdp tbls = ListAdp_.new_();
|
||||
public Sql_tbl_src BaseTable() {return (Sql_tbl_src)tbls.FetchAt(0);}
|
||||
public static Sql_from new_(Sql_tbl_src baseTable) {
|
||||
Sql_from rv = new Sql_from();
|
||||
rv.tbls.Add(baseTable);
|
||||
return rv;
|
||||
} Sql_from() {}
|
||||
}
|
||||
class Sql_tbl_src {
|
||||
public Sql_join_itmType JoinType() {return type;} public Sql_tbl_src JoinType_(Sql_join_itmType v) {this.type = v; return this;} Sql_join_itmType type = Sql_join_itmType.Inner;
|
||||
public ListAdp JoinLinks() {return joinLinks;} ListAdp joinLinks = ListAdp_.new_();
|
||||
public String TblName() {return tblName;} public Sql_tbl_src TblName_(String s) {tblName = s; return this;} private String tblName;
|
||||
public String Alias() {return alias;} public Sql_tbl_src Alias_(String s) {alias = s; return this;} private String alias;
|
||||
public void XtoSql(String_bldr sb) {
|
||||
sb.Add_many(tblName, alias == null ? "" : " " + alias);
|
||||
}
|
||||
public static Sql_tbl_src new_() {return new Sql_tbl_src();} Sql_tbl_src() {}
|
||||
}
|
||||
class Sql_join_itmType {
|
||||
public int Val() {return val;} int val;
|
||||
public String Name() {return name;} private String name;
|
||||
Sql_join_itmType(int v, String name) {this.val = v; this.name = name;}
|
||||
public static final Sql_join_itmType
|
||||
From = new Sql_join_itmType(0, "FROM")
|
||||
, Inner = new Sql_join_itmType(1, "INNER JOIN")
|
||||
, Left = new Sql_join_itmType(2, "LEFT JOIN")
|
||||
, Right = new Sql_join_itmType(3, "RIGHT JOIN")
|
||||
, Outer = new Sql_join_itmType(4, "OUTER JOIN")
|
||||
, Cross = new Sql_join_itmType(5, "CROSS JOIN")
|
||||
;
|
||||
}
|
@ -1,153 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.strings.*;
|
||||
class Sql_select {
|
||||
public Sql_select_fld_list Flds() {return flds;} Sql_select_fld_list flds = Sql_select_fld_list.new_();
|
||||
public boolean Distinct() {return distinct;} public void Distinct_set(boolean v) {distinct = v;} private boolean distinct;
|
||||
public void Add(String fldName) {flds.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fldName, fldName));}
|
||||
public void Add(String fldName, String alias) {flds.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fldName, alias));}
|
||||
public void Add(Sql_select_fld_base fld) {flds.Add(fld);}
|
||||
|
||||
public static final Sql_select All = all_(); static Sql_select all_() {Sql_select rv = new_(); rv.Add(Sql_select_fld_wild._); return rv;}
|
||||
public static Sql_select new_() {return new Sql_select();} Sql_select() {}
|
||||
}
|
||||
abstract class Sql_select_fld_base {
|
||||
public String Tbl() {return tbl;} public void Tbl_set(String val) {tbl = val;} private String tbl;
|
||||
public String Fld() {return fld;} public void Fld_set(String val) {fld = val;} private String fld;
|
||||
public String Alias() {return alias;} public void Alias_set(String val) {alias = val;} private String alias;
|
||||
public ClassXtn ValType() {return valType;} public void ValType_set(ClassXtn val) {valType = val;} ClassXtn valType = ObjectClassXtn._;
|
||||
public abstract Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type);
|
||||
@gplx.Virtual public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@gplx.Virtual public boolean Type_fld() {return true;}
|
||||
public abstract String XtoSql();
|
||||
public static final String Tbl_null = null;
|
||||
@gplx.Internal protected void ctor_(String tbl, String fld, String alias) {
|
||||
Tbl_set(tbl); Fld_set(fld); Alias_set(alias);
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_wild extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {throw Err_.new_("group by eval not allowed on *");}
|
||||
@Override public void GroupBy_type(GfoFld fld) {throw Err_.new_("group by type not allowed on *");}
|
||||
@Override public String XtoSql() {return "*";}
|
||||
public static final Sql_select_fld_wild _ = new Sql_select_fld_wild(); Sql_select_fld_wild() {this.ctor_(Tbl_null, "*", "*");}
|
||||
}
|
||||
class Sql_select_fld_fld extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {return curVal;}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@Override public String XtoSql() {
|
||||
String rv = Fld();
|
||||
if (Tbl() != Tbl_null)
|
||||
rv = Tbl() + "." + Fld();
|
||||
if (!String_.Eq(Alias(), Fld()))
|
||||
rv = rv + " AS " + Alias();
|
||||
return rv;
|
||||
}
|
||||
public static Sql_select_fld_fld new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_fld rv = new Sql_select_fld_fld();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_fld() {}
|
||||
}
|
||||
abstract class Sql_select_fld_func_base extends Sql_select_fld_base {
|
||||
public abstract String XtoSql_functionName();
|
||||
@Override public boolean Type_fld() {return false;}
|
||||
@Override public String XtoSql() {
|
||||
return String_.Format("{0}({1}) AS {2}", XtoSql_functionName(), Fld(), Alias());
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_count extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "COUNT";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return 1;
|
||||
return Int_.cast_(groupByVal) + 1;
|
||||
}
|
||||
public static Sql_select_fld_count new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_count rv = new Sql_select_fld_count();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_count() {}
|
||||
}
|
||||
class Sql_select_fld_sum extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "SUM";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return Int_.cast_(curVal);
|
||||
return Int_.cast_(groupByVal) + Int_.cast_(curVal);
|
||||
}
|
||||
public static Sql_select_fld_sum new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_sum rv = new Sql_select_fld_sum();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_sum() {}
|
||||
}
|
||||
class Sql_select_fld_minMax extends Sql_select_fld_func_base {
|
||||
int compareType = CompareAble_.Less;
|
||||
@Override public String XtoSql_functionName() {return compareType == CompareAble_.Less ? "MIN" : "MAX";}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return curVal;
|
||||
int compareVal = CompareAble_.Compare_obj(curVal, groupByVal);
|
||||
return compareVal * compareType > 0 ? curVal : groupByVal;
|
||||
}
|
||||
public static Sql_select_fld_minMax min_(String tbl, String fld, String alias) {return new_(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld_minMax max_(String tbl, String fld, String alias) {return new_(CompareAble_.More, tbl, fld, alias);}
|
||||
static Sql_select_fld_minMax new_(int compareType, String tbl, String fld, String alias) {
|
||||
Sql_select_fld_minMax rv = new Sql_select_fld_minMax();
|
||||
rv.compareType = compareType;
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_minMax() {}
|
||||
}
|
||||
class Sql_select_fld_list {
|
||||
public int Count() {return hash.Count();}
|
||||
public void Add(Sql_select_fld_base fld) {hash.Add(fld.Alias(), fld);}
|
||||
public Sql_select_fld_base FetchAt(int i) {return (Sql_select_fld_base)hash.FetchAt(i);}
|
||||
public Sql_select_fld_base FetchOrNull(String k) {return (Sql_select_fld_base)hash.Fetch(k);}
|
||||
public GfoFldList XtoGfoFldLst(TdbTable tbl) {
|
||||
GfoFldList rv = GfoFldList_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base selectFld = this.FetchAt(i);
|
||||
GfoFld fld = tbl.Flds().FetchOrNull(selectFld.Fld());
|
||||
if (fld == null) throw Err_.new_("fld not found in tbl").Add("fldName", selectFld.Fld()).Add("tblName", tbl.Name()).Add("tblFlds", tbl.Flds().XtoStr());
|
||||
if (rv.Has(selectFld.Alias())) throw Err_.new_("alias is not unique").Add("fldName", selectFld.Fld()).Add("flds", rv.XtoStr());
|
||||
selectFld.GroupBy_type(fld);
|
||||
rv.Add(selectFld.Alias(), selectFld.ValType());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String[] To_str_ary() {
|
||||
int len = this.Count();
|
||||
String[] rv = new String[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
rv[i] = fld.Fld();
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String XtoStr() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
sb.Add_fmt("{0},{1}|", fld.Fld(), fld.Alias());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
OrderedHash hash = OrderedHash_.new_();
|
||||
public static Sql_select_fld_list new_() {return new Sql_select_fld_list();} Sql_select_fld_list() {}
|
||||
}
|
34
140_dbs/src/gplx/dbs/engines/Db_engine.java
Normal file
34
140_dbs/src/gplx/dbs/engines/Db_engine.java
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines; import gplx.*; import gplx.dbs.*;
|
||||
public interface Db_engine {
|
||||
String Tid();
|
||||
Db_url Url();
|
||||
Db_engine New_clone(Db_url url);
|
||||
Db_rdr New_rdr_by_obj(Object o, String sql); // Object o:ResultSet if desktop; Cursor if android
|
||||
Db_stmt New_stmt_prep(Db_qry qry);
|
||||
Object New_stmt_prep_as_obj(String sql);
|
||||
DataRdr New_rdr(java.sql.ResultSet rdr, String sql);
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
Object Exec_as_obj(Db_qry qry);
|
||||
void Exec_create_tbl(Db_meta_tbl meta);
|
||||
void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary);
|
||||
}
|
@ -15,20 +15,20 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines; import gplx.*; import gplx.dbs.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
abstract class Db_engine_sql_base implements Db_engine {
|
||||
import gplx.dbs.engines.*; import gplx.dbs.qrys.*; import gplx.dbs.sqls.*;
|
||||
public abstract class Db_engine_sql_base implements Db_engine {
|
||||
@gplx.Internal protected void Ctor(Db_url url) {this.url = url;}
|
||||
public abstract String Tid();
|
||||
public Db_url Url() {return url;} protected Db_url url;
|
||||
public abstract Db_engine New_clone(Db_url url);
|
||||
public Db_rdr New_rdr_by_obj(Object rdr, String sql) {
|
||||
Db_rdr__basic rv = New_rdr_clone();
|
||||
Db_rdr__basic rv = (Db_rdr__basic)New_rdr_clone();
|
||||
rv.Ctor((ResultSet)rdr, sql);
|
||||
return rv;
|
||||
}
|
||||
@gplx.Virtual public Db_rdr__basic New_rdr_clone() {return new Db_rdr__basic();}
|
||||
@gplx.Virtual public Db_rdr New_rdr_clone() {return new Db_rdr__basic();}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt_cmd(this, qry);}
|
||||
public void Txn_bgn() {Exec_as_obj(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));}
|
||||
public void Txn_end() {Exec_as_obj(Db_qry_sql.xtn_("COMMIT TRANSACTION;"));}
|
||||
@ -53,6 +53,15 @@ abstract class Db_engine_sql_base implements Db_engine {
|
||||
}
|
||||
catch (Exception exc) {throw Err_.err_(exc, "exec reader failed").Add("sql", sql).Add("err", Err_.Message_gplx_brief(exc));}
|
||||
}
|
||||
public void Exec_create_tbl(Db_meta_tbl meta) {Exec_as_int(meta.To_sql_create());}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_meta_idx idx = ary[i];
|
||||
usr_dlg.Plog_many("", "", "db.idx.create; db=~{0} idx=~{1}", url.Database(), idx.Name());
|
||||
Exec_as_int(idx.To_sql_create());
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public DataRdr New_rdr(ResultSet rdr, String sql) {return gplx.stores.Db_data_rdr_.new_(rdr, sql);}
|
||||
@gplx.Virtual public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_ansi();}
|
||||
@gplx.Internal protected abstract Connection Conn_new();
|
@ -18,20 +18,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_engine__mem implements Db_engine {
|
||||
private final HashAdp tbl_hash = HashAdp_.new_();
|
||||
public Db_engine__mem(Db_url url, Db_meta_tbl[] meta_tbls) {
|
||||
this.url = url;
|
||||
int tbls_len = meta_tbls.length;
|
||||
for (int i = 0; i < tbls_len; ++i) {
|
||||
Db_meta_tbl meta_tbl = meta_tbls[i];
|
||||
Mem_tbl tbl = new Mem_tbl();
|
||||
tbl_hash.Add(meta_tbl.Name(), tbl);
|
||||
}
|
||||
}
|
||||
Db_engine__mem(Db_url url) {this.url = url;}
|
||||
public String Tid() {return Db_url__mem.Tid_const;}
|
||||
public Db_url Url() {return url;} private Db_url url;
|
||||
public Db_engine New_clone(Db_url url) {return this;}
|
||||
public Db_engine New_clone(Db_url url) {return new Db_engine__mem(url);}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt__mem(this, qry);}
|
||||
public Mem_tbl Tbls_get(String name) {return (Mem_tbl)tbl_hash.Fetch(name);}
|
||||
public Mem_tbl Tbls_get(String name) {return (Mem_tbl)tbl_hash.Fetch(name);}
|
||||
public void Txn_bgn() {++txn_count;} private int txn_count = 0;
|
||||
public void Txn_end() {--txn_count;}
|
||||
public Object Exec_as_obj(Db_qry qry) {throw Err_.not_implemented_();}
|
||||
@ -42,5 +34,10 @@ public class Db_engine__mem implements Db_engine {
|
||||
public Db_rdr New_rdr_by_obj(Object rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public void Exec_create_tbl(Db_meta_tbl meta) {
|
||||
Mem_tbl mem_tbl = new Mem_tbl();
|
||||
tbl_hash.AddReplace(meta.Name(), mem_tbl);
|
||||
}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {} // TODO: implement unique index
|
||||
public static final Db_engine__mem _ = new Db_engine__mem(); Db_engine__mem() {}
|
||||
}
|
||||
|
@ -18,13 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_rdr__mem implements Db_rdr {
|
||||
private final Mem_itm[] rows; private int row_idx = -1; private final int rows_len;
|
||||
private final HashAdp ords = HashAdp_.new_();
|
||||
private Mem_itm row;
|
||||
public Db_rdr__mem(String[] cols, Mem_itm[] rows) {
|
||||
this.rows = rows; this.rows_len = rows.length;
|
||||
int cols_len = cols.length;
|
||||
for (int i = 0; i < cols_len; ++i)
|
||||
ords.Add(cols[i], i);
|
||||
}
|
||||
public boolean Move_next() {
|
||||
boolean rv = ++row_idx < rows_len;
|
||||
@ -33,25 +29,24 @@ public class Db_rdr__mem implements Db_rdr {
|
||||
return rv;
|
||||
}
|
||||
public byte[] Read_bry(int i) {return (byte[])row.Get_at(i);}
|
||||
public byte[] Read_bry(String k) {return (byte[])row.Get_at(Ord_by_key(k));}
|
||||
public byte[] Read_bry(String k) {return (byte[])row.Get_by(k);}
|
||||
public String Read_str(int i) {return (String)row.Get_at(i);}
|
||||
public String Read_str(String k) {return (String)row.Get_at(Ord_by_key(k));}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.new_utf8_((String)row.Get_at(i));}
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.new_utf8_((String)row.Get_at(Ord_by_key(k)));}
|
||||
public String Read_str(String k) {return (String)row.Get_by(k);}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.new_utf8__null((String)row.Get_at(i));} // NOTE: null b/c db can have NULL
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.new_utf8__null((String)row.Get_by(k));} // NOTE: null b/c db can have NULL
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561((String)row.Get_at(i));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561((String)row.Get_at(Ord_by_key(k)));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561((String)row.Get_by(k));}
|
||||
public byte Read_byte(int i) {return Byte_.cast_(row.Get_at(i));}
|
||||
public byte Read_byte(String k) {return Byte_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public byte Read_byte(String k) {return Byte_.cast_(row.Get_by(k));}
|
||||
public int Read_int(int i) {return Int_.cast_(row.Get_at(i));}
|
||||
public int Read_int(String k) {return Int_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public int Read_int(String k) {return Int_.cast_(row.Get_by(k));}
|
||||
public long Read_long(int i) {return Long_.cast_(row.Get_at(i));}
|
||||
public long Read_long(String k) {return Long_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public long Read_long(String k) {return Long_.cast_(row.Get_by(k));}
|
||||
public float Read_float(int i) {return Float_.cast_(row.Get_at(i));}
|
||||
public float Read_float(String k) {return Float_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public float Read_float(String k) {return Float_.cast_(row.Get_by(k));}
|
||||
public double Read_double(int i) {return Double_.cast_(row.Get_at(i));}
|
||||
public double Read_double(String k) {return Double_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public double Read_double(String k) {return Double_.cast_(row.Get_by(k));}
|
||||
public boolean Read_bool_by_byte(int i) {return Byte_.cast_(row.Get_at(i)) == 1;}
|
||||
public boolean Read_bool_by_byte(String k) {return Byte_.cast_(row.Get_at(Ord_by_key(k))) == 1;}
|
||||
public boolean Read_bool_by_byte(String k) {return Byte_.cast_(row.Get_by(k)) == 1;}
|
||||
public void Rls() {}
|
||||
private int Ord_by_key(String k) {return Int_.cast_(ords.Fetch(k));}
|
||||
}
|
||||
|
@ -17,91 +17,90 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_stmt__mem implements Db_stmt {
|
||||
private final ListAdp val_list = ListAdp_.new_(); private int val_idx;
|
||||
public Db_stmt__mem(Db_engine__mem engine, Db_qry qry) {this.engine = engine; this.qry = qry;} private Db_engine__mem engine;
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private final ListAdp val_list = ListAdp_.new_();
|
||||
public Db_stmt__mem(Db_engine__mem engine, Db_qry qry) {Ctor_stmt(engine, qry);} private Db_engine__mem engine;
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {this.engine = (Db_engine__mem)engine; this.qry = qry;}
|
||||
public HashAdp Crts() {return crt_hash;} private final HashAdp crt_hash = HashAdp_.new_();
|
||||
public int Args_len() {return val_list.Count();}
|
||||
public Object Args_get_at(int i) {return val_list.FetchAt(i);}
|
||||
public Db_qry Qry() {return qry;} private Db_qry qry;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
val_list.Clear();
|
||||
crt_hash.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
this.Clear();
|
||||
}
|
||||
public void Rls() {this.Clear();}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
try {Add(k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
try {Add(k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
try {Add(k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
try {Add(k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
try {Add(k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
try {Add(k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
try {Add(k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_str(String k, String v) {return Add_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, null, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(++val_idx, k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
try {Add(k, where, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
gplx.ios.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(++val_idx, "", Bool_.N, bfr.Xto_str_and_clear());
|
||||
Add("", Bool_.N, bfr.Xto_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
@ -122,10 +121,9 @@ public class Db_stmt__mem implements Db_stmt {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Select(this);
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec select_val: tbl={0}", qry.Base_table());}
|
||||
}
|
||||
private void Add(int idx, String k, boolean where, Object v) {
|
||||
public Object Exec_select_val() {throw Err_.not_implemented_();}
|
||||
private void Add(String k, boolean where, Object v) {
|
||||
if (k == Db_meta_fld.Key_null) return; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
val_list.Add(v);
|
||||
if (where) {
|
||||
ListAdp list = (ListAdp)crt_hash.Fetch(k);
|
||||
|
@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.qrys.*;
|
||||
public class Mem_tbl {
|
||||
private final ListAdp rows = ListAdp_.new_(); private final ListAdp where_rows = ListAdp_.new_();
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
|
@ -15,11 +15,11 @@ 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.stores.*; import gplx.dbs.sqls.*;
|
||||
package gplx.dbs.engines.mysql; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.sqls.*;
|
||||
import java.sql.*;
|
||||
class Mysql_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Db_url__mysql.Tid_const;}
|
||||
public class Mysql_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Mysql_url.Tid_const;}
|
||||
@Override public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_escape_backslash();}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
Mysql_engine rv = new Mysql_engine();
|
||||
@ -28,10 +28,10 @@ class Mysql_engine extends Db_engine_sql_base {
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Mysql_rdr.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Db_url__mysql url_as_mysql = (Db_url__mysql)url;
|
||||
Mysql_url url_as_mysql = (Mysql_url)url;
|
||||
return Conn_make_by_url("jdbc:mysql://localhost/" + url_as_mysql.Database() + "?characterEncoding=UTF8", url_as_mysql.Uid(), url_as_mysql.Pwd());
|
||||
}
|
||||
@gplx.Internal protected static final Mysql_engine _ = new Mysql_engine(); Mysql_engine() {}
|
||||
public static final Mysql_engine _ = new Mysql_engine(); Mysql_engine() {}
|
||||
}
|
||||
class Mysql_rdr extends Db_data_rdr {
|
||||
//PATCH:MYSQL:byte actually returned as int by Jdbc ResultSet (or MYSQL impmentation); convert to byte
|
41
140_dbs/src/gplx/dbs/engines/mysql/Mysql_url.java
Normal file
41
140_dbs/src/gplx/dbs/engines/mysql/Mysql_url.java
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mysql; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Mysql_url extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mysql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "uid", uid
|
||||
, "pwd", pwd
|
||||
, "charset", "utf8"
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Mysql_url rv = new Mysql_url();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("charset", "utf8")));
|
||||
rv.uid = m.ReadStr("uid");
|
||||
rv.pwd = m.ReadStr("pwd");
|
||||
return rv;
|
||||
}
|
||||
public static final Mysql_url _ = new Mysql_url(); Mysql_url() {}
|
||||
}
|
@ -15,23 +15,9 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_engine {
|
||||
String Tid();
|
||||
Db_url Url();
|
||||
Db_engine New_clone(Db_url url);
|
||||
Db_rdr New_rdr_by_obj(Object o, String sql); // Object o:ResultSet if desktop; Cursor if android
|
||||
Db_stmt New_stmt_prep(Db_qry qry);
|
||||
Object New_stmt_prep_as_obj(String sql);
|
||||
DataRdr New_rdr(java.sql.ResultSet rdr, String sql);
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
Object Exec_as_obj(Db_qry qry);
|
||||
}
|
||||
class Db_engine_null implements Db_engine {
|
||||
public String Tid() {return Db_url__null.Tid_const;}
|
||||
package gplx.dbs.engines.nulls; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Null_engine implements Db_engine {
|
||||
public String Tid() {return Null_url.Tid_const;}
|
||||
public Db_url Url() {return Db_url_.Null;}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {}
|
||||
@ -43,5 +29,7 @@ class Db_engine_null implements Db_engine {
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public Object Exec_as_obj(Db_qry cmd) {return cmd.Exec_is_rdr() ? (Object)DataRdr_.Null : -1;}
|
||||
public static final Db_engine_null _ = new Db_engine_null(); Db_engine_null() {}
|
||||
public void Exec_create_tbl(Db_meta_tbl meta) {}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {}
|
||||
public static final Null_engine _ = new Null_engine(); Null_engine() {}
|
||||
}
|
23
140_dbs/src/gplx/dbs/engines/nulls/Null_url.java
Normal file
23
140_dbs/src/gplx/dbs/engines/nulls/Null_url.java
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.nulls; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Null_url extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "null_db";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {return this;}
|
||||
public static final Null_url _ = new Null_url(); Null_url() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
}
|
@ -15,11 +15,11 @@ 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.stores.*; import gplx.dbs.sqls.*;
|
||||
package gplx.dbs.engines.postgres; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.sqls.*;
|
||||
import java.sql.*;
|
||||
class Postgres_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Db_url__postgres.Tid_const;}
|
||||
public class Postgres_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Postgres_url.Tid_const;}
|
||||
@Override public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_escape_backslash();}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
Postgres_engine rv = new Postgres_engine();
|
||||
@ -28,8 +28,8 @@ class Postgres_engine extends Db_engine_sql_base {
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Db_data_rdr_.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Db_url__postgres url_as_postgres = (Db_url__postgres)url;
|
||||
Postgres_url url_as_postgres = (Postgres_url)url;
|
||||
return Conn_make_by_url("jdbc:" + url_as_postgres.Tid() + "://localhost/" + url_as_postgres.Database(), url_as_postgres.Uid(), url_as_postgres.Pwd());
|
||||
}
|
||||
@gplx.Internal protected static final Postgres_engine _ = new Postgres_engine(); Postgres_engine() {}
|
||||
public static final Postgres_engine _ = new Postgres_engine(); Postgres_engine() {}
|
||||
}
|
42
140_dbs/src/gplx/dbs/engines/postgres/Postgres_url.java
Normal file
42
140_dbs/src/gplx/dbs/engines/postgres/Postgres_url.java
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.postgres; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Postgres_url extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "postgresql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "port", "5432"
|
||||
, "user id", uid
|
||||
, "password", pwd
|
||||
, "encoding", "unicode" // needed for 1.1 conn; otherwise, ascii
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Postgres_url rv = new Postgres_url();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("encoding", "unicode")));
|
||||
rv.uid = m.ReadStr("user id");
|
||||
rv.pwd = m.ReadStr("password");
|
||||
return rv;
|
||||
}
|
||||
public static final Postgres_url _ = new Postgres_url(); Postgres_url() {}
|
||||
}
|
@ -15,18 +15,19 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import java.sql.*;
|
||||
import gplx.stores.*; import gplx.dbs.engines.sqlite.*;
|
||||
class Sqlite_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Db_url__sqlite.Tid_const;}
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Sqlite_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Sqlite_url.Tid_const;}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
Sqlite_engine rv = new Sqlite_engine();
|
||||
rv.Ctor(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Sqlite_rdr.new_(rdr, commandText);}
|
||||
@Override public Db_rdr__basic New_rdr_clone() {return new Db_rdr__sqlite();}
|
||||
@Override public Db_rdr New_rdr_clone() {return new Db_rdr__sqlite();}
|
||||
static boolean loaded = false;
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
if (!loaded) {
|
||||
@ -36,8 +37,8 @@ class Sqlite_engine extends Db_engine_sql_base {
|
||||
catch (ClassNotFoundException e) {throw Err_.new_("could not load sqlite jdbc driver");}
|
||||
loaded = true;
|
||||
}
|
||||
Db_url__sqlite url_as_sqlite = (Db_url__sqlite)url;
|
||||
return Conn_make_by_url("jdbc:sqlite://" + String_.Replace(url_as_sqlite.Database(), "\\", "/"), "", "");
|
||||
Sqlite_url url_as_sqlite = (Sqlite_url)url;
|
||||
return Conn_make_by_url("jdbc:sqlite://" + String_.Replace(url_as_sqlite.Url().Raw(), "\\", "/"), "", "");
|
||||
}
|
||||
private boolean pragma_needed = true;
|
||||
@Override public void Txn_bgn() {
|
||||
@ -53,11 +54,41 @@ class Sqlite_engine extends Db_engine_sql_base {
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA cache_size=4000;")); // too many will also cause out of memory
|
||||
Exec_as_obj(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));
|
||||
}
|
||||
@gplx.Internal protected static final Sqlite_engine _ = new Sqlite_engine(); Sqlite_engine() {}
|
||||
public static final Sqlite_engine _ = new Sqlite_engine(); Sqlite_engine() {}
|
||||
}
|
||||
class Db_rdr__sqlite extends Db_rdr__basic { @Override public byte Read_byte(int i) {try {return (byte)rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@Override public byte Read_byte(String k) {try {return (byte)Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
}
|
||||
@Override public boolean Read_bool_by_byte(String k) {
|
||||
try {
|
||||
int val = rdr.getInt(k);
|
||||
return val == 1;
|
||||
} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", k, Bool_.Cls_val_name, Err_.Message_lang(e));}
|
||||
}
|
||||
@Override public long Read_long(String k) {
|
||||
try {
|
||||
long val = rdr.getLong(k);
|
||||
Number n = (Number)val;
|
||||
return n.longValue();
|
||||
} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", k, Long_.Cls_val_name, Err_.Message_lang(e));}
|
||||
}
|
||||
@Override public float Read_float(String k) {
|
||||
try {
|
||||
Double val = (Double)rdr.getDouble(k);
|
||||
return val == null ? Float.NaN : val.floatValue();
|
||||
} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", k, Float_.Cls_val_name, Err_.Message_lang(e));}
|
||||
}
|
||||
@Override public DateAdp Read_date_by_str(String k) {
|
||||
try {
|
||||
String val = rdr.getString(k);
|
||||
return val == null ? null : DateAdp_.parse_fmt(val, "M/dd/yyyy hh:mm tt");
|
||||
} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", k, DateAdp_.Cls_ref_type, Err_.Message_lang(e));}
|
||||
}
|
||||
// @Override public DecimalAdp ReadDecimalOr(String key, DecimalAdp or) {
|
||||
// Object val = Read(key);
|
||||
// Double d = ((Double)val);
|
||||
// return val == null ? null : DecimalAdp_.double_(d);
|
||||
// }
|
||||
}
|
||||
class Sqlite_rdr extends Db_data_rdr { @Override public float ReadFloat(String key) {return ReadFloatOr(key, Float.NaN);}
|
||||
@Override public float ReadFloatOr(String key, float or) {
|
||||
Object val = Read(key);
|
@ -15,8 +15,8 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.primitives.*; import gplx.dbs.engines.sqlite.*;
|
||||
package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.primitives.*; import gplx.dbs.qrys.*; import gplx.dbs.utls.*; import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*;
|
||||
public class Sqlite_engine_ {
|
||||
public static void Db_attach(Db_conn p, String alias, String url) {
|
||||
String s = String_.Format("ATTACH '{0}' AS {1};", url, alias);
|
||||
@ -50,11 +50,21 @@ public class Sqlite_engine_ {
|
||||
Db_qry qry = Db_qry_sql.ddl_(String_.Format("ALTER TABLE {0} RENAME TO {1};", src, trg));
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Pragma_page_size_4096(Db_conn p) {Pragma_page_size(p, 4096);}
|
||||
public static void Pragma_page_size(Db_conn p, int val) {
|
||||
Db_qry qry = Db_qry_sql.ddl_("PRAGMA page_size = " + Int_.Xto_str(val) + ";");
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Idx_create(Gfo_usr_dlg usr_dlg, Db_conn conn, String tbl, Db_meta_idx[] idx_ary) {
|
||||
conn.Txn_mgr().Txn_end_all(); // commit any pending transactions
|
||||
int len = idx_ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_meta_idx idx = idx_ary[i];
|
||||
String idx_sql = idx.To_sql_create();
|
||||
usr_dlg.Plog_many("", "", "creating index: ~{0} ~{1}", tbl, idx_sql);
|
||||
conn.Exec_qry(Db_qry_sql.ddl_(idx.To_sql_create()));
|
||||
usr_dlg.Log_many("", "", "index created: ~{0} ~{1}", tbl, idx_sql);
|
||||
}
|
||||
}
|
||||
public static void Idx_create(Db_conn p, Db_idx_itm... idxs) {Idx_create(Gfo_usr_dlg_.Null, p, "", idxs);}
|
||||
public static void Idx_create(Gfo_usr_dlg usr_dlg, Db_conn p, String file_id, Db_idx_itm... idxs) {
|
||||
int len = idxs.length;
|
||||
@ -66,24 +76,16 @@ public class Sqlite_engine_ {
|
||||
usr_dlg.Log_many("", "", "index created: ~{0} ~{1}", file_id, index);
|
||||
}
|
||||
}
|
||||
public static Db_conn Conn_load_or_fail_(Io_url url) {
|
||||
boolean exists = Io_mgr._.ExistsFil(url);
|
||||
if (!exists) throw Err_.new_fmt_("db does not exist; url=~{0}", url.Raw());
|
||||
Db_url connect = Db_url__sqlite.load_(url);
|
||||
return Db_conn_.new_and_open_(connect);
|
||||
}
|
||||
public static Db_conn Conn_load_or_make_(Io_url url) {return Conn_load_or_make_(url, Bool_obj_ref.n_());}
|
||||
public static Db_conn Conn_load_or_make_(Io_url url, Bool_obj_ref created) {
|
||||
boolean exists = Io_mgr._.ExistsFil(url);
|
||||
created.Val_(!exists);
|
||||
Db_url connect = exists ? Db_url__sqlite.load_(url) : Db_url__sqlite.make_(url);
|
||||
Db_conn p = Db_conn_.new_and_open_(connect);
|
||||
Db_url connect = exists ? Sqlite_url.load_(url) : Sqlite_url.make_(url);
|
||||
Db_conn p = Db_conn_pool.I.Get_or_new(connect);
|
||||
if (!exists)
|
||||
Pragma_page_size(p, 4096);
|
||||
return p;
|
||||
}
|
||||
public static Db_engine New_engine() {return Sqlite_engine._;}
|
||||
public static final int Stmt_arg_max = 999; // 999 is max number of variables allowed by sqlite
|
||||
public static final int Stmt_arg_max = 999; // 999 is max number of variables allowed by sqlite
|
||||
public static final boolean Supports_read_binary_stream = false;
|
||||
public static final boolean Supports_indexed_by = true;
|
||||
public static String X_date_to_str(DateAdp v) {return v == Date_null ? "" : v.XtoStr_fmt_iso_8561();}
|
@ -16,14 +16,15 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_url__sqlite extends Db_url__base {
|
||||
public class Sqlite_url extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "sqlite";
|
||||
public Io_url Url() {return url;} private Io_url url;
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__sqlite rv = new Db_url__sqlite();
|
||||
Sqlite_url rv = new Sqlite_url();
|
||||
String url = m.ReadStr("data source");
|
||||
rv.url = Io_url_.new_any_(url);
|
||||
rv.Ctor("", url, raw, BldApi(m, KeyVal_.new_("version", "3")));
|
||||
rv.database = rv.url.NameOnly();
|
||||
return rv;
|
||||
}
|
||||
public static Db_url load_(Io_url url) {
|
||||
@ -42,5 +43,5 @@ public class Db_url__sqlite extends Db_url__base {
|
||||
|
||||
));
|
||||
}
|
||||
public static final Db_url__sqlite _ = new Db_url__sqlite(); Db_url__sqlite() {}
|
||||
public static final Sqlite_url _ = new Sqlite_url(); Sqlite_url() {}
|
||||
}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*;
|
||||
public class TdbConnectInfo_tst {
|
||||
@Test public void Full() {
|
||||
@ -27,6 +27,6 @@ public class TdbConnectInfo_tst {
|
||||
tst_Parse(connectInfo, Io_url_.new_any_("C:\\dir\\xmpl.tdb"), "dsv");
|
||||
}
|
||||
void tst_Parse(Db_url connectInfo, Io_url url, String format) {
|
||||
Tfds.Eq(((Db_url__tdb)connectInfo).Url(), url);
|
||||
Tfds.Eq(((Tdb_url)connectInfo).Url(), url);
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class TdbDatabase {
|
||||
public String Name() {return name;} public void Name_set(String v) {name = v;} private String name = "xmpl";
|
||||
public Io_url DbUrl() {return dbInfo;} Io_url dbInfo;
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
class TdbDbLoadMgr {
|
||||
public TdbDatabase LoadTbls(Io_url dbInfo) {
|
||||
TdbDatabase db = TdbDatabase.new_(dbInfo);
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*;
|
||||
import gplx.stores.*; /*DsvDataRdr*/ import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
||||
public class TdbDbLoadMgr_tst {
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
class TdbDbSaveMgr {
|
||||
public void SaveDb(TdbDatabase db) {
|
||||
for (Object filObj : db.Files()) {
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*;
|
||||
import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
||||
public class TdbDbSaveMgr_tst {
|
@ -15,9 +15,8 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.criterias.*;
|
||||
import gplx.lists.*; /*GfoNde*/
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*; import gplx.lists.*; /*GfoNde*/ import gplx.dbs.qrys.*;
|
||||
class TdbDeleteWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_delete cmd = (Db_qry_delete)cmdObj;
|
@ -15,14 +15,14 @@ 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.sqls.*;
|
||||
class TdbEngine implements Db_engine {
|
||||
public String Tid() {return Db_url__tdb.Tid_const;}
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.dbs.qrys.*; import gplx.dbs.sqls.*;
|
||||
public class TdbEngine implements Db_engine {
|
||||
public String Tid() {return Tdb_url.Tid_const;}
|
||||
public Db_url Url() {return url;} private Db_url url;
|
||||
public TdbDatabase Db() {return db;} TdbDatabase db;
|
||||
public void Conn_open() {
|
||||
Db_url__tdb tdb_url = (Db_url__tdb)url;
|
||||
Tdb_url tdb_url = (Tdb_url)url;
|
||||
String url_str = tdb_url.Server();
|
||||
db = loadMgr.LoadTbls(Io_url_.new_any_(url_str));
|
||||
}
|
||||
@ -32,6 +32,7 @@ class TdbEngine implements Db_engine {
|
||||
public Db_engine New_clone(Db_url url) {
|
||||
TdbEngine rv = new TdbEngine();
|
||||
rv.CtorTdbEngine(url);
|
||||
rv.Conn_open();
|
||||
return rv;
|
||||
}
|
||||
public Object Exec_as_obj(Db_qry qry) {
|
||||
@ -53,9 +54,11 @@ class TdbEngine implements Db_engine {
|
||||
public void FlushTbl(TdbTable tbl) {
|
||||
saveMgr.SaveFile(db, tbl.File());
|
||||
}
|
||||
public void Exec_create_tbl(Db_meta_tbl meta) {throw Err_.not_implemented_();}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {throw Err_.not_implemented_();}
|
||||
|
||||
HashAdp wkrs = HashAdp_.new_(); TdbDbLoadMgr loadMgr = TdbDbLoadMgr.new_(); TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_();
|
||||
@gplx.Internal protected static final TdbEngine _ = new TdbEngine();
|
||||
public static final TdbEngine _ = new TdbEngine();
|
||||
void CtorTdbEngine(Db_url url) {
|
||||
this.url = url;
|
||||
wkrs.Add(Db_qry_.Tid_select, TdbSelectWkr._);
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class TdbFile {
|
||||
public int Id() {return id;} int id;
|
||||
public Io_url Path() {return url;} Io_url url;
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.lists.*; /*OrderedHash_base*/ import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbFileList extends OrderedHash_base {
|
||||
public TdbFile FetchOrFail(int id) {return TdbFile.as_(FetchOrFail_base(id));}
|
@ -15,8 +15,8 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.lists.*; /*GfoNde*/
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.lists.*; /*GfoNde*/ import gplx.dbs.qrys.*;
|
||||
class TdbFlushWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_flush cmd = Db_qry_flush.cast_(cmdObj);
|
@ -15,9 +15,9 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import org.junit.*;
|
||||
import gplx.ios.*; /*IoMgrFxt*/
|
||||
import gplx.ios.*; /*IoMgrFxt*/ import gplx.dbs.qrys.*;
|
||||
public class TdbFlush_tst {
|
||||
@Before public void setup() {
|
||||
Io_mgr._.InitEngine_mem();
|
@ -15,8 +15,8 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.lists.*; import gplx.dbs.sqls.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.lists.*; import gplx.dbs.qrys.*; import gplx.dbs.sqls.*;
|
||||
class TdbInsertWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_insert cmd = (Db_qry_insert)cmdObj;
|
@ -15,8 +15,8 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.qrys.*; import gplx.dbs.sqls.*;
|
||||
import gplx.lists.*; /*ComparerAble*/ import gplx.stores.*; /*GfoNdeRdr*/
|
||||
class TdbSelectWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.stores.*;
|
||||
import gplx.stores.xmls.*; /*XmlDataRdr*/
|
||||
import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.*; /*GfoNdeList*/ import gplx.stores.*; import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbTable {
|
||||
public int Id() {return id;} int id;
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.lists.*; /*OrderedHash_base*/ import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbTableList extends OrderedHash_base {
|
||||
public TdbTable Fetch(String name) {return TdbTable.as_(Fetch_base(name));}
|
@ -15,9 +15,9 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.criterias.*;
|
||||
import gplx.lists.*; /*GfoNde*/
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.core.criterias.*; import gplx.lists.*; /*GfoNde*/
|
||||
import gplx.dbs.qrys.*;
|
||||
class TdbUpdateWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_update cmd = (Db_qry_update)cmdObj;
|
37
140_dbs/src/gplx/dbs/engines/tdbs/Tdb_url.java
Normal file
37
140_dbs/src/gplx/dbs/engines/tdbs/Tdb_url.java
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Tdb_url extends Db_url__base {
|
||||
public Io_url Url() {return url;} Io_url url;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "tdb";
|
||||
public static Db_url new_(Io_url url) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "url", url.Raw()
|
||||
));
|
||||
} Tdb_url() {}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Tdb_url rv = new Tdb_url();
|
||||
String urlStr = m.ReadStr("url");
|
||||
Io_url url = Io_url_.new_any_(urlStr);
|
||||
rv.Ctor(urlStr, url.NameOnly(), raw, BldApi(m));
|
||||
rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final Tdb_url _ = new Tdb_url();
|
||||
}
|
@ -15,9 +15,9 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_arg {
|
||||
@gplx.Internal protected Db_arg(String key, Object val) {this.key = key; this.val = val;}
|
||||
public Db_arg(String key, Object val) {this.key = key; this.val = val;}
|
||||
public String Key() {return key;} private String key;
|
||||
public Object Val() {return val;} public void Val_(Object v) {this.val = v;} private Object val;
|
||||
public byte Val_tid() {return val_tid;} public Db_arg Val_tid_(byte v) {val_tid = v; return this;} private byte val_tid = Db_val_type.Tid_null;
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*; import gplx.core.criterias.*;
|
||||
public class Db_qry__select_in_tbl implements Db_qry {
|
||||
public Db_qry__select_in_tbl(String base_table, String[] select_flds, String[] where_flds, String group_by_sql, String having_sql, String order_by_sql, String limit_sql) {
|
||||
@ -42,24 +42,26 @@ public class Db_qry__select_in_tbl implements Db_qry {
|
||||
public String Limit_sql() {return limit_sql;} private final String limit_sql;
|
||||
public String XtoSql() {return Xto_sql();}
|
||||
public String Xto_sql() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add("SELECT ");
|
||||
int select_flds_len = select_flds.length;
|
||||
for (int i = 0; i < select_flds_len; ++i) {
|
||||
if (i != 0) sb.Add(",");
|
||||
sb.Add(select_flds[i]);
|
||||
synchronized (this) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add("SELECT ");
|
||||
int select_flds_len = select_flds.length;
|
||||
for (int i = 0; i < select_flds_len; ++i) {
|
||||
if (i != 0) sb.Add(",");
|
||||
sb.Add(select_flds[i]);
|
||||
}
|
||||
sb.Add(" FROM ").Add(base_table);
|
||||
if (where_flds != null && where_flds.length != 0) {sb.Add(" WHERE "); Where_sql(sb);}
|
||||
if (group_by_sql != null) sb.Add(group_by_sql);
|
||||
if (having_sql != null) sb.Add(having_sql);
|
||||
if (order_by_sql != null) {sb.Add(" ORDER BY "); sb.Add(order_by_sql);}
|
||||
if (limit_sql != null) sb.Add(limit_sql);
|
||||
return sb.XtoStr();
|
||||
}
|
||||
sb.Add(" FROM ").Add(base_table);
|
||||
if (where_flds != null && where_flds.length != 0) {sb.Add(" WHERE "); Where_sql(sb);}
|
||||
if (group_by_sql != null) sb.Add(group_by_sql);
|
||||
if (having_sql != null) sb.Add(having_sql);
|
||||
if (order_by_sql != null) {sb.Add(" ORDER BY "); sb.Add(order_by_sql);}
|
||||
if (limit_sql != null) sb.Add(limit_sql);
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public static Db_qry__select_in_tbl new_(String base_table, String[] where_flds, String[] select_flds) {
|
||||
Db_qry__select_in_tbl rv = new Db_qry__select_in_tbl(base_table, select_flds, where_flds, null, null, null, null);
|
||||
rv.where = Db_crt_.eq_many_(where_flds);
|
||||
rv.where = where_flds.length == 0 ? Db_crt_.Wildcard : Db_crt_.eq_many_(where_flds);
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry__select_in_tbl as_(Object obj) {return obj instanceof Db_qry__select_in_tbl ? (Db_qry__select_in_tbl)obj : null;}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public interface Db_qry_arg_owner extends Db_qry {
|
||||
Db_qry_arg_owner From_(String tbl);
|
||||
Db_qry_arg_owner Key_arg_(String k, int v);
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_delete implements Db_qry {
|
||||
Db_qry_delete(String base_table, Criteria where) {this.base_table = base_table; this.where = where;}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Db_qry_dml_tst {
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_flush implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_flush;}
|
||||
@ -24,7 +24,7 @@ public class Db_qry_flush implements Db_qry {
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
|
||||
@gplx.Internal protected String[] TableNames() {return tableNames;} private String[] tableNames;
|
||||
public String[] TableNames() {return tableNames;} private String[] tableNames;
|
||||
|
||||
|
||||
public static Db_qry_flush as_(Object obj) {return obj instanceof Db_qry_flush ? (Db_qry_flush)obj : null;}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_insert implements Db_qry_arg_owner {
|
||||
public Db_qry_insert(String base_table) {this.base_table = base_table;}
|
||||
@ -37,6 +37,7 @@ public class Db_qry_insert implements Db_qry_arg_owner {
|
||||
public Db_qry_arg_owner Arg_(String k, byte[] v) {return Arg_obj_type_(k, String_.new_utf8_(v), Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_obj_(String k, Object v) {return Arg_obj_type_(k, v, Db_val_type.Tid_null);}
|
||||
public Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid) {
|
||||
if (key == Db_meta_fld.Key_null) return this;
|
||||
Db_arg arg = new Db_arg(key, val).Val_tid_(val_tid);
|
||||
args.Add(arg.Key(), arg);
|
||||
return this;
|
||||
@ -48,10 +49,10 @@ public class Db_qry_insert implements Db_qry_arg_owner {
|
||||
public Db_qry_insert Cols_(String... ary) {
|
||||
if (cols == null) cols = Sql_select_fld_list.new_();
|
||||
for (String fld : ary)
|
||||
cols.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fld, fld));
|
||||
cols.Add(Sql_select_fld_.new_fld(Sql_select_fld_base.Tbl_null, fld, fld));
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected Sql_select_fld_list Cols() {return cols;} private Sql_select_fld_list cols;
|
||||
public Sql_select_fld_list Cols() {return cols;} private Sql_select_fld_list cols;
|
||||
|
||||
public static Db_qry_insert new_() {return new Db_qry_insert();} Db_qry_insert() {}
|
||||
public static Db_qry_insert new_(String tbl, String... keys) {
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_select implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_select;}
|
||||
@ -47,8 +47,7 @@ public class Db_qry_select implements Db_qry {
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
|
||||
|
||||
@gplx.Internal protected Sql_from From() {return from;} Sql_from from;
|
||||
public Sql_from From() {return from;} Sql_from from;
|
||||
public Db_qry_select From_(String tblName) {return From_(tblName, null);}
|
||||
public Db_qry_select From_(String tblName, String alias) {
|
||||
if (from != null) throw Err_.new_("super table already defined").Add("from", from.Tbls().Count());
|
||||
@ -64,7 +63,7 @@ public class Db_qry_select implements Db_qry {
|
||||
return this;
|
||||
}
|
||||
|
||||
@gplx.Internal protected Sql_select Cols() {return cols;} Sql_select cols = Sql_select.All;
|
||||
public Sql_select Cols() {return cols;} Sql_select cols = Sql_select.All;
|
||||
public String[] Cols_ary() {return cols.Flds().To_str_ary();}
|
||||
public Db_qry_select Cols_all_() {return this;}
|
||||
public Db_qry_select Cols_alias_(String expr, String alias) {
|
||||
@ -81,28 +80,28 @@ public class Db_qry_select implements Db_qry {
|
||||
public Db_qry_select Cols_groupBy_max(String fld) {return Cols_groupBy_max(fld, fld);}
|
||||
public Db_qry_select Cols_groupBy_max(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_minMax.max_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
cols.Add(Sql_select_fld_.new_max(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_min(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_minMax.min_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
cols.Add(Sql_select_fld_.new_min(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_count(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_count.new_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
cols.Add(Sql_select_fld_.new_count(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_sum(String fld) {return Cols_groupBy_sum(fld, fld);}
|
||||
public Db_qry_select Cols_groupBy_sum(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_sum.new_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
cols.Add(Sql_select_fld_.new_sum(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Criteria Where() {return where;} public Db_qry_select Where_(Criteria crt) {where = crt; return this;} Criteria where;
|
||||
@gplx.Internal protected Sql_order_by OrderBy() {return orderBy;} Sql_order_by orderBy = null;
|
||||
public Sql_order_by OrderBy() {return orderBy;} Sql_order_by orderBy = null;
|
||||
public Db_qry_select OrderBy_(String fieldName, boolean ascending) {
|
||||
Sql_order_by_itm item = Sql_order_by_itm.new_(fieldName, ascending);
|
||||
orderBy = Sql_order_by.new_(item);
|
||||
@ -116,14 +115,15 @@ public class Db_qry_select implements Db_qry {
|
||||
orderBy = Sql_order_by.new_(ary);
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected Sql_group_by GroupBy() {return groupBy;} Sql_group_by groupBy = null;
|
||||
public Sql_group_by GroupBy() {return groupBy;} Sql_group_by groupBy = null;
|
||||
public Db_qry_select GroupBy_(String... flds) {
|
||||
if (groupBy != null) throw Err_.new_("group by already defined").Add("group", groupBy);
|
||||
groupBy = Sql_group_by.new_(flds);
|
||||
return this;
|
||||
}
|
||||
public String Indexed_by() {return indexed_by;} public Db_qry_select Indexed_by_(String v) {indexed_by = v; return this;} private String indexed_by;
|
||||
public Db_qry_select Distinct_() {cols.Distinct_set(true); return this;}
|
||||
@gplx.Internal protected int Limit() {return limit;} int limit = -1; public static final int Limit_disabled = -1;
|
||||
public int Limit() {return limit;} int limit = -1; public static final int Limit_disabled = -1;
|
||||
public Db_qry_select Limit_(int v) {this.limit = v; return this;}
|
||||
|
||||
public static Db_qry_select new_() {return new Db_qry_select();} Db_qry_select() {}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_select_tst {
|
||||
@Before public void setup() {
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_sql implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_sql;}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Db_qry_sql_tst {
|
||||
@Before public void init() {fxt.Clear();} private Db_qry_sql_fxt fxt = new Db_qry_sql_fxt();
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_update implements Db_qry_arg_owner {
|
||||
public int Tid() {return Db_qry_.Tid_update;}
|
||||
@ -37,6 +37,7 @@ public class Db_qry_update implements Db_qry_arg_owner {
|
||||
public Db_qry_arg_owner Arg_(String k, byte[] v) {return Arg_obj_type_(k, String_.new_utf8_(v), Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_obj_(String k, Object v) {return Arg_obj_type_(k, v, Db_val_type.Tid_null);}
|
||||
public Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid) {
|
||||
if (key == Db_meta_fld.Key_null) return this;
|
||||
Db_arg arg = new Db_arg(key, val).Val_tid_(val_tid);
|
||||
args.Add(arg.Key(), arg);
|
||||
return this;
|
@ -15,14 +15,16 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
class Db_stmt_cmd implements Db_stmt {
|
||||
private final Db_engine engine;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.sqls.*;
|
||||
public class Db_stmt_cmd implements Db_stmt {
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private Db_engine engine;
|
||||
private PreparedStatement stmt = null;
|
||||
private String sql; private int val_idx = 0;
|
||||
public Db_stmt_cmd(Db_engine engine, Db_qry qry) {
|
||||
public Db_stmt_cmd(Db_engine engine, Db_qry qry) {Ctor_stmt(engine, qry);}
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {
|
||||
this.engine = engine;
|
||||
sql = qry.Tid() == Db_qry_.Tid_select_in_tbl ? ((Db_qry__select_in_tbl)qry).Xto_sql() : Sql_qry_wtr_.I.Xto_str(qry, true);
|
||||
Reset_stmt();
|
||||
@ -33,66 +35,74 @@ class Db_stmt_cmd implements Db_stmt {
|
||||
}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {stmt.setByte(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setByte(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "byte", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {stmt.setInt(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setInt(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "int", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {stmt.setLong(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setLong(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "long", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {stmt.setFloat(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setFloat(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "float", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {stmt.setDouble(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setDouble(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "double", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {stmt.setBigDecimal(++val_idx, v.Xto_decimal());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setBigDecimal(++val_idx, v.Xto_decimal());} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "decimal", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {stmt.setBytes(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setBytes(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "byte[]", v.length, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_str(String k, String v) {return Add_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, null, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {stmt.setString(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
if (k == Db_meta_fld.Key_null) return this; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
try {stmt.setString(++val_idx, v);} catch (Exception e) {this.Rls(); throw Err_.err_(e, "failed to add value: type={0} val={1} sql={2}", "String", v, sql);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
@ -100,13 +110,28 @@ class Db_stmt_cmd implements Db_stmt {
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = stmt.execute(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));}
|
||||
try {boolean rv = stmt.execute(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.err_(e, "db_stmt.insert: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));
|
||||
}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
try {int rv = stmt.executeUpdate(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.err_(e, "db_stmt.update: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));
|
||||
}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
try {int rv = stmt.executeUpdate(); return rv;}
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.err_(e, "db_stmt.insert: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));
|
||||
}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = engine.New_rdr(stmt.executeQuery(), sql); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
@ -124,8 +149,11 @@ class Db_stmt_cmd implements Db_stmt {
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
if (stmt == null) return; // Null instance
|
||||
try {stmt.close();}
|
||||
if (stmt == null) return; // Null instance
|
||||
try {
|
||||
if (stmt.getConnection().isClosed()) return; // do not close stmt if connection is already closed; throws null error; DATE:2015-02-11
|
||||
stmt.close();
|
||||
}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to close command: {0}", sql);}
|
||||
}
|
||||
}
|
@ -15,82 +15,85 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.engines.*;
|
||||
public class Db_stmt_sql implements Db_stmt {// used for formatting SQL statements; not used for actual insert into database
|
||||
private Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
private Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
private int val_idx = 0;
|
||||
private static final String Key_na = ""; // key is not_available; only called by procs with signature of Val(<type> v);
|
||||
private final ListAdp args = ListAdp_.new_();
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
private final Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
public void Ctor_stmt(Db_engine engine, Db_qry qry) {}
|
||||
public Db_conn Conn() {return conn;} public void Conn_(Db_conn v) {this.conn = v;} Db_conn conn;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(++val_idx, Byte_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
try {Add(k, Byte_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, Int_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
try {Add(k, Int_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, Long_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
try {Add(k, Long_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, Float_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
try {Add(k, Float_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, Double_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
try {Add(k, Double_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, v.Xto_str());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
try {Add(k, v.Xto_str());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {// HACK: convert to String b/c tdb does not support byte[]
|
||||
try {Add(++val_idx, Val_str_wrap(String_.new_utf8_(v)));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
try {Add(k, Val_str_wrap(String_.new_utf8_(v)));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, null, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_str(String k, String v) {return Add_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, null, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, Key_na, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(++val_idx, Val_str_wrap(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
try {Add(k, Val_str_wrap(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
gplx.ios.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(++val_idx, bfr.Xto_str_and_clear());
|
||||
Add(Key_na, bfr.Xto_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
@ -114,15 +117,14 @@ public class Db_stmt_sql implements Db_stmt {// used for formatting SQL statemen
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
this.Clear();
|
||||
public void Rls() {this.Clear();}
|
||||
public void Add(String k, String v) {
|
||||
if (k == Db_meta_fld.Key_null) return; // key is explicitly null; ignore; allows version_2+ type definitions
|
||||
args.Add(v);
|
||||
}
|
||||
public void Add(String v) {Add(-1, v);}
|
||||
public void Add(int idx, String v) {args.Add(v);} private ListAdp args = ListAdp_.new_();
|
||||
public String Xto_sql() {
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, (Object[])args.Xto_ary_and_clear(Object.class));
|
||||
return tmp_bfr.Xto_str_and_clear();
|
@ -15,15 +15,15 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Db_stmt_sql_tst {
|
||||
@Before public void init() {}
|
||||
@Test public void Basic() {
|
||||
Db_stmt_sql stmt = new Db_stmt_sql();
|
||||
stmt.Parse(null, "UPDATE tbl_0 SET col_0 = ? WHERE col_1 = ?");
|
||||
stmt.Add("1");
|
||||
stmt.Add("2");
|
||||
stmt.Add("col_0", "1");
|
||||
stmt.Add("col_1", "2");
|
||||
Tfds.Eq("UPDATE tbl_0 SET col_0 = 1 WHERE col_1 = 2", stmt.Xto_sql());
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_val_type {
|
||||
public static final byte // not serialized
|
||||
Tid_null = 0
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_fld {
|
||||
public Db_fld(String name, byte type_tid) {this.name = name; this.type_tid = type_tid;}
|
||||
public String Name() {return name;} public Db_fld Name_(String v) {name = v; return this;} private String name;
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Db_obj_ary_crt implements gplx.core.criterias.Criteria {
|
||||
public byte Tid() {return Criteria_.Tid_db_obj_ary;}
|
@ -15,7 +15,7 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.core.strings.*; import gplx.dbs.sqls.*;
|
||||
public class Db_obj_ary_tst {
|
||||
@Before public void init() {} private Db_obj_ary_fxt fxt = new Db_obj_ary_fxt();
|
@ -15,9 +15,9 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
interface Db_sqlbldr {}
|
||||
class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
public class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.reset_(1024);
|
||||
public String Bld_create_idx(Db_meta_idx idx) {
|
||||
tmp_bfr.Add_str_ascii("CREATE ");
|
||||
@ -25,7 +25,9 @@ class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
tmp_bfr.Add_str_ascii("UNIQUE ");
|
||||
tmp_bfr.Add_str_ascii("INDEX ");
|
||||
tmp_bfr.Add_str_ascii("IF NOT EXISTS ");
|
||||
tmp_bfr.Add_str_ascii(idx.Tbl()).Add_str_ascii("__").Add_str_ascii(idx.Name());
|
||||
tmp_bfr.Add_str_ascii(idx.Name());
|
||||
tmp_bfr.Add_str_ascii(" ON ");
|
||||
tmp_bfr.Add_str_ascii(idx.Tbl());
|
||||
tmp_bfr.Add_str_ascii(" (");
|
||||
String[] flds = idx.Flds();
|
||||
int flds_len = flds.length;
|
||||
@ -60,7 +62,7 @@ class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
case Db_meta_fld.Tid_bool: tmp_bfr.Add_str_ascii("boolean"); break;
|
||||
case Db_meta_fld.Tid_byte: tmp_bfr.Add_str_ascii("tinyint"); break;
|
||||
case Db_meta_fld.Tid_short: tmp_bfr.Add_str_ascii("smallint"); break;
|
||||
case Db_meta_fld.Tid_int: tmp_bfr.Add_str_ascii("int"); break;
|
||||
case Db_meta_fld.Tid_int: tmp_bfr.Add_str_ascii("integer"); break; // NOTE: must be integer, not int, else "int PRIMARY KEY AUTONUMBER" will fail; DATE:2015-02-12
|
||||
case Db_meta_fld.Tid_long: tmp_bfr.Add_str_ascii("bigint"); break;
|
||||
case Db_meta_fld.Tid_float: tmp_bfr.Add_str_ascii("float"); break;
|
||||
case Db_meta_fld.Tid_double: tmp_bfr.Add_str_ascii("double"); break;
|
||||
@ -70,4 +72,5 @@ class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
default: throw Err_.unhandled(tid);
|
||||
}
|
||||
}
|
||||
public static final Db_sqlbldr__sqlite I = new Db_sqlbldr__sqlite(); Db_sqlbldr__sqlite() {}
|
||||
}
|
@ -15,13 +15,13 @@ GNU Affero General Public License for more details.
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Db_sqlbldr_tst {
|
||||
@Before public void setup() {} private final Db_sqlbldr_fxt fxt = new Db_sqlbldr_fxt();
|
||||
@Test public void Idx_unique() {
|
||||
fxt.Test_create_idx(Db_meta_idx.new_unique("tbl_name", "idx_name", "fld_1", "fld_2")
|
||||
, "CREATE UNIQUE INDEX IF NOT EXISTS tbl_name__idx_name (fld_1, fld_2);"
|
||||
fxt.Test_create_idx(Db_meta_idx.new_unique_by_tbl("tbl_name", "idx_name", "fld_1", "fld_2")
|
||||
, "CREATE UNIQUE INDEX IF NOT EXISTS tbl_name__idx_name ON tbl_name (fld_1, fld_2);"
|
||||
);
|
||||
}
|
||||
@Test public void Tbl_basic() {
|
||||
@ -39,10 +39,10 @@ public class Db_sqlbldr_tst {
|
||||
fxt.Test_create_tbl(Db_meta_tbl.new_("tbl_name", flds.To_fld_ary())
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "CREATE TABLE tbl_name"
|
||||
, "( fld_int_pkey int NOT NULL PRIMARY KEY"
|
||||
, "( fld_int_pkey integer NOT NULL PRIMARY KEY"
|
||||
, ", fld_bool boolean NOT NULL"
|
||||
, ", fld_short smallint NOT NULL"
|
||||
, ", fld_int int NOT NULL"
|
||||
, ", fld_int integer NOT NULL"
|
||||
, ", fld_long bigint NOT NULL"
|
||||
, ", fld_float float NOT NULL"
|
||||
, ", fld_double double NOT NULL"
|
||||
@ -54,7 +54,7 @@ public class Db_sqlbldr_tst {
|
||||
}
|
||||
}
|
||||
class Db_sqlbldr_fxt {
|
||||
private Db_sqlbldr__sqlite sqlbldr = new Db_sqlbldr__sqlite();
|
||||
private Db_sqlbldr__sqlite sqlbldr = Db_sqlbldr__sqlite.I;
|
||||
public void Test_create_idx(Db_meta_idx idx, String expd) {
|
||||
Tfds.Eq(expd, sqlbldr.Bld_create_idx(idx));
|
||||
}
|
27
140_dbs/src/gplx/dbs/sqls/Sql_from.java
Normal file
27
140_dbs/src/gplx/dbs/sqls/Sql_from.java
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Sql_from {
|
||||
public ListAdp Tbls() {return tbls;} ListAdp tbls = ListAdp_.new_();
|
||||
public Sql_tbl_src BaseTable() {return (Sql_tbl_src)tbls.FetchAt(0);}
|
||||
public static Sql_from new_(Sql_tbl_src baseTable) {
|
||||
Sql_from rv = new Sql_from();
|
||||
rv.tbls.Add(baseTable);
|
||||
return rv;
|
||||
} Sql_from() {}
|
||||
}
|
@ -16,4 +16,13 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Sql_dummy_class {}
|
||||
public class Sql_group_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_group_by new_(String... ary) {
|
||||
Sql_group_by rv = new Sql_group_by();
|
||||
for (String itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_group_by() {}
|
||||
}
|
35
140_dbs/src/gplx/dbs/sqls/Sql_join_itm.java
Normal file
35
140_dbs/src/gplx/dbs/sqls/Sql_join_itm.java
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*;
|
||||
public class Sql_join_itm {
|
||||
public String SrcTbl() {return srcTbl;} public Sql_join_itm SrcTbl_(String v) {srcTbl = v; return this;} private String srcTbl;
|
||||
public String SrcFld() {return srcFld;} public Sql_join_itm SrcFld_(String v) {srcFld = v; return this;} private String srcFld;
|
||||
public String TrgFld() {return trgFld;} public Sql_join_itm TrgFld_(String v) {trgFld = v; return this;} private String trgFld;
|
||||
public String TrgFldOrSrcFld() {return trgFld == null ? srcFld : trgFld;}
|
||||
public static Sql_join_itm new_(String trgFld, String srcTbl, String srcFld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = trgFld; rv.srcTbl = srcTbl; rv.srcFld = srcFld;
|
||||
return rv;
|
||||
} Sql_join_itm() {}
|
||||
public static Sql_join_itm same_(String tbl, String fld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = fld; rv.srcTbl = tbl; rv.srcFld = fld;
|
||||
return rv;
|
||||
}
|
||||
}
|
@ -15,16 +15,17 @@ 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_obj_state {
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Sql_join_itmType {
|
||||
public int Val() {return val;} int val;
|
||||
public Db_obj_state MarkUpdated() {return this == Retrieved ? Updated : this;} // Created/Deleted noops
|
||||
public boolean Modified() {return this == Created || this == Updated;}
|
||||
Db_obj_state(int val) {this.val = val;}
|
||||
public static final Db_obj_state
|
||||
Created = new Db_obj_state(1)
|
||||
, Retrieved = new Db_obj_state(2)
|
||||
, Updated = new Db_obj_state(3)
|
||||
, Deleted = new Db_obj_state(4)
|
||||
;
|
||||
public String Name() {return name;} private String name;
|
||||
Sql_join_itmType(int v, String name) {this.val = v; this.name = name;}
|
||||
public static final Sql_join_itmType
|
||||
From = new Sql_join_itmType(0, "FROM")
|
||||
, Inner = new Sql_join_itmType(1, "INNER JOIN")
|
||||
, Left = new Sql_join_itmType(2, "LEFT JOIN")
|
||||
, Right = new Sql_join_itmType(3, "RIGHT JOIN")
|
||||
, Outer = new Sql_join_itmType(4, "OUTER JOIN")
|
||||
, Cross = new Sql_join_itmType(5, "CROSS JOIN")
|
||||
;
|
||||
}
|
28
140_dbs/src/gplx/dbs/sqls/Sql_order_by.java
Normal file
28
140_dbs/src/gplx/dbs/sqls/Sql_order_by.java
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Sql_order_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_order_by new_(Sql_order_by_itm... ary) {
|
||||
Sql_order_by rv = new Sql_order_by();
|
||||
for (Sql_order_by_itm itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_order_by() {}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user