mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
v2.4.1.1
This commit is contained in:
parent
80b9928b5c
commit
18dcd3f89e
@ -24,7 +24,8 @@ public class Err_ { //_20110415
|
||||
public static Err new_(String hdr) {return Err.hdr_(hdr);}
|
||||
public static Err new_key_(String key, String hdr) {return Err.hdr_(hdr).Key_(key);}
|
||||
public static Err err_key_(Exception exc, String key, String hdr) {return Err.exc_(exc, hdr).Key_(key);}
|
||||
public static Err err_(Exception exc, String hdr) {return Err.exc_(exc, hdr);}
|
||||
public static Err err_(Exception e) {return Err.exc_(e, Message_gplx_brief(e));}
|
||||
public static Err err_(Exception e, String hdr) {return Err.exc_(e, hdr);}
|
||||
public static Err err_(Exception e, String fmt, Object... args) {return Err.exc_(e, String_.Format(fmt, args));}
|
||||
public static Err cast_(Exception ignore, Class<?> t, Object o) {
|
||||
String o_str = "";
|
||||
|
@ -50,11 +50,16 @@ public class Array_ {
|
||||
rv[i + newLen] = cur[i];
|
||||
return rv;
|
||||
}
|
||||
public static Object Resize(Object src, int trgLen) {
|
||||
Object trg = Create(ComponentType(src), trgLen);
|
||||
int srcLen = Array.getLength(src);
|
||||
int copyLen = srcLen > trgLen ? trgLen : srcLen; // trgLen can either expand or shrink
|
||||
CopyTo(src, 0, trg, 0, copyLen);
|
||||
public static Object Resize_add_one(Object src, int src_len, Object new_obj) {
|
||||
Object rv = Resize(src, src_len + 1);
|
||||
Set(rv, src_len, new_obj);
|
||||
return rv;
|
||||
}
|
||||
public static Object Resize(Object src, int trg_len) {
|
||||
Object trg = Create(ComponentType(src), trg_len);
|
||||
int src_len = Array.getLength(src);
|
||||
int copy_len = src_len > trg_len ? trg_len : src_len; // trg_len can either expand or shrink
|
||||
CopyTo(src, 0, trg, 0, copy_len);
|
||||
return trg;
|
||||
}
|
||||
public static String XtoStr(Object ary) {
|
||||
|
@ -18,26 +18,50 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx;
|
||||
import gplx.core.primitives.*;
|
||||
public class Bry_bfr {
|
||||
private Bry_bfr_mkr_mgr mkr_mgr; private int reset;
|
||||
public byte[] Bfr() {return bfr;} private byte[] bfr;
|
||||
@gplx.Internal protected int Bfr_max() {return bfr_max;} private int bfr_max;
|
||||
public int Len() {return bfr_len;} private int bfr_len;
|
||||
public boolean Len_eq_0() {return bfr_len == 0;}
|
||||
public boolean Len_gt_0() {return bfr_len > 0;}
|
||||
public void Bfr_init(byte[] bfr, int bfr_len) {
|
||||
this.bfr = bfr;
|
||||
this.bfr_len = bfr_len;
|
||||
this.bfr_max = bfr.length; // NOTE: must sync bfr_max, else will fail later during add; bfr will think bfr has .length of bfr_max, when it actually has .length of bfr_len; DATE:2014-03-09
|
||||
synchronized (this) {
|
||||
this.bfr = bfr;
|
||||
this.bfr_len = bfr_len;
|
||||
this.bfr_max = bfr.length; // NOTE: must sync bfr_max, else will fail later during add; bfr will think bfr has .length of bfr_max, when it actually has .length of bfr_len; DATE:2014-03-09
|
||||
}
|
||||
}
|
||||
@gplx.Internal protected int Mkr_itm() {return mkr_itm;} private int mkr_itm = -1;
|
||||
@gplx.Internal protected Bry_bfr_mkr_mgr Mkr_mgr() {return mkr_mgr;} Bry_bfr_mkr_mgr mkr_mgr;
|
||||
@gplx.Internal protected Bry_bfr Mkr_(Bry_bfr_mkr_mgr mkr_mgr, int itm) {this.mkr_mgr = mkr_mgr; this.mkr_itm = itm; return this;}
|
||||
public Bry_bfr Mkr_rls() {mkr_mgr.Rls(this); return this;}
|
||||
private void Mkr_clear() {
|
||||
if (mkr_mgr != null) mkr_mgr.Rls(this);
|
||||
mkr_mgr = null;
|
||||
mkr_itm = -1;
|
||||
public Bry_bfr Mkr_rls() {
|
||||
if (mkr_mgr != null) {
|
||||
synchronized (mkr_mgr) {
|
||||
mkr_mgr.Rls(mkr_idx);
|
||||
}
|
||||
synchronized (this) {
|
||||
this.mkr_mgr = null;
|
||||
this.mkr_idx = -1;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
private Bry_bfr Reset_(int v) {reset = v; return this;} private int reset;
|
||||
public void Clear_and_rls() {
|
||||
this.Clear();
|
||||
this.Mkr_rls();
|
||||
}
|
||||
public String To_str_and_rls() {return String_.new_utf8_(To_bry_and_rls());}
|
||||
public byte[] To_bry_and_rls() {
|
||||
byte[] rv = null;
|
||||
synchronized (bfr) {
|
||||
rv = Xto_bry();
|
||||
this.Clear();
|
||||
if (reset > 0) Reset_if_gt(reset);
|
||||
synchronized (mkr_mgr) {
|
||||
mkr_mgr.Rls(mkr_idx);
|
||||
}
|
||||
mkr_mgr = null;
|
||||
mkr_idx = -1;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
private Bry_bfr Reset_(int v) {reset = v; return this;}
|
||||
public Bry_bfr Reset_if_gt(int limit) {
|
||||
if (bfr_max > limit) {
|
||||
this.bfr_max = limit;
|
||||
@ -46,7 +70,12 @@ public class Bry_bfr {
|
||||
bfr_len = 0;
|
||||
return this;
|
||||
}
|
||||
public Bry_bfr Clear() {bfr_len = 0; return this;}
|
||||
public Bry_bfr Clear() {
|
||||
synchronized (this) {
|
||||
this.bfr_len = 0;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public Bry_bfr ClearAndReset() {bfr_len = 0; if (reset > 0) Reset_if_gt(reset); return this;}
|
||||
public byte Get_at_last_or_nil_if_empty() {return bfr_len == 0 ? Byte_ascii.Nil : bfr[bfr_len - 1];}
|
||||
public Bry_bfr Add_safe(byte[] val) {return val == null ? this : Add(val);}
|
||||
@ -491,7 +520,7 @@ public class Bry_bfr {
|
||||
}
|
||||
public void Rls() {
|
||||
bfr = null;
|
||||
Mkr_clear();
|
||||
this.Mkr_rls();
|
||||
}
|
||||
@Override public int hashCode() {return Bry_obj_ref.CalcHashCode(bfr, 0, bfr_len);}
|
||||
@Override public boolean equals(Object obj) {return obj == null ? false : Bry_.Match(bfr, 0, bfr_len, ((Bry_obj_ref)obj).Val());} // NOTE: strange, but null check needed; throws null error; PAGE:c:File:Eug<EFBFBD>ne_Delacroix_-_La_libert<EFBFBD>_guidant_le_peuple.jpg
|
||||
@ -499,6 +528,15 @@ public class Bry_bfr {
|
||||
bfr_max = v;
|
||||
bfr = Bry_.Resize(bfr, 0, v);
|
||||
}
|
||||
@gplx.Internal protected int Mkr_idx() {return mkr_idx;} private int mkr_idx = -1;
|
||||
@gplx.Internal protected boolean Mkr_idx_is_null() {return mkr_idx == -1;}
|
||||
@gplx.Internal protected int Bfr_max() {return bfr_max;} private int bfr_max;
|
||||
@gplx.Internal protected Bry_bfr Mkr_init(Bry_bfr_mkr_mgr mkr_mgr, int itm) {
|
||||
synchronized (this) {
|
||||
this.mkr_mgr = mkr_mgr; this.mkr_idx = itm;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public static Bry_bfr new_() {return new Bry_bfr(16);}
|
||||
public static Bry_bfr new_(int v) {return new Bry_bfr(v);}
|
||||
public static Bry_bfr reset_(int v) {return new Bry_bfr(16).Reset_(v);} // PERF: set initial size to 16, not reset val; allows for faster "startup"; DATE:2014-06-14
|
||||
|
@ -24,7 +24,8 @@ public class Bry_bfr_mkr {
|
||||
public Bry_bfr Get_k004() {return mkr_k004.Get();}
|
||||
public Bry_bfr Get_m001() {return mkr_m001.Get();}
|
||||
public void Rls(Bry_bfr v) {
|
||||
v.Mkr_mgr().Rls(v);
|
||||
v.Mkr_rls();
|
||||
// v.Mkr_mgr().Rls(v);
|
||||
}
|
||||
public void Reset_if_gt(int v) {
|
||||
for (byte i = Tid_b128; i <= Tid_m001; i++)
|
||||
@ -71,7 +72,7 @@ class Bry_bfr_mkr_mgr {
|
||||
for (int i = 0; i < ary_max; i++) {
|
||||
Bry_bfr itm = ary[i];
|
||||
if (itm != null) {
|
||||
if (itm.Mkr_mgr() != null) throw Err_.new_("failed to clear bfr: " + Int_.Xto_str(i));
|
||||
if (!itm.Mkr_idx_is_null()) throw Err_.new_("failed to clear bfr: " + Int_.Xto_str(i));
|
||||
itm.Clear();
|
||||
}
|
||||
ary[i] = null;
|
||||
@ -117,7 +118,7 @@ class Bry_bfr_mkr_mgr {
|
||||
ary[rv_idx] = rv;
|
||||
}
|
||||
}
|
||||
rv.Mkr_(this, rv_idx);
|
||||
rv.Mkr_init(this, rv_idx);
|
||||
return rv.Clear(); // NOTE: ALWAYS call Clear when doing Get. caller may forget to call Clear, and reused bfr may have leftover bytes. unit tests will not catch, and difficult to spot in app
|
||||
}
|
||||
}
|
||||
@ -131,16 +132,26 @@ class Bry_bfr_mkr_mgr {
|
||||
Array_.CopyTo(free, 0, new_free, 0, free_len);
|
||||
free = new_free;
|
||||
}
|
||||
public void Rls(Bry_bfr v) {
|
||||
// public void Rls(Bry_bfr v) {
|
||||
// synchronized (thread_lock) {
|
||||
// int idx = v.Mkr_itm();
|
||||
// if (idx == -1) throw Err_mgr._.fmt_("gplx.Bry_bfr", "rls_failed", "rls called on bfr that was not created by factory");
|
||||
// int new_ary_len = nxt_idx - 1;
|
||||
// if (idx == new_ary_len)
|
||||
// nxt_idx = new_ary_len;
|
||||
// else
|
||||
// free[free_len++] = idx;
|
||||
// v.Mkr_(null, -1);
|
||||
// }
|
||||
// }
|
||||
public void Rls(int idx) {
|
||||
synchronized (thread_lock) {
|
||||
int idx = v.Mkr_itm();
|
||||
if (idx == -1) throw Err_mgr._.fmt_("gplx.Bry_bfr", "rls_failed", "rls called on bfr that was not created by factory");
|
||||
int new_ary_len = nxt_idx - 1;
|
||||
if (idx == new_ary_len)
|
||||
nxt_idx = new_ary_len;
|
||||
else
|
||||
free[free_len++] = idx;
|
||||
v.Mkr_(null, -1);
|
||||
}
|
||||
}
|
||||
public static final Bry_bfr[] Ary_empty = new Bry_bfr[0];
|
||||
|
@ -56,7 +56,7 @@ class Bry_bfr_mkr_fxt {
|
||||
}
|
||||
public Bry_bfr_mkr_fxt Rls(int i) {
|
||||
Bry_bfr bfr = mkr.Ary()[i];
|
||||
mkr.Rls(bfr);
|
||||
bfr.Mkr_rls();
|
||||
return this;
|
||||
}
|
||||
public Bry_bfr_mkr_fxt Tst_idxs(int... expd) {
|
||||
@ -65,7 +65,7 @@ class Bry_bfr_mkr_fxt {
|
||||
for (int i = 0; i < actl_len; i++) {
|
||||
Bry_bfr bfr = mkr.Ary()[i];
|
||||
int actl_val = Bry_bfr_mkr_tst.Int_null;
|
||||
if (bfr != null) actl_val = bfr.Mkr_itm();
|
||||
if (bfr != null) actl_val = bfr.Mkr_idx();
|
||||
actl[i] = actl_val;
|
||||
}
|
||||
Tfds.Eq_ary(expd, actl);
|
||||
|
@ -134,9 +134,9 @@ public class Int_ implements GfoInvkAble {
|
||||
if (val < 0) throw Err_.new_("key must be >= 0").Add("key", key).Add("val", val);
|
||||
return this;
|
||||
}
|
||||
public static String Xto_str_pad_bgn_space(int v, int reqdPlaces) {return Xto_str_pad_bgn(v, reqdPlaces, Byte_ascii.Space, true);} // EX: 1, 3 returns " 1"
|
||||
public static String Xto_str_pad_bgn(int v, int reqdPlaces) {return Xto_str_pad_bgn(v, reqdPlaces, Byte_ascii.Num_0, true);} // EX: 1, 3 returns "001"
|
||||
static String Xto_str_pad_bgn(int val, int places, byte pad_chr, boolean bgn) {
|
||||
public static String Xto_str_pad_bgn_space(int v, int reqdPlaces) {return Xto_str_pad_bgn_zero(v, reqdPlaces, Byte_ascii.Space, true);} // EX: 1, 3 returns " 1"
|
||||
public static String Xto_str_pad_bgn_zero(int v, int reqdPlaces) {return Xto_str_pad_bgn_zero(v, reqdPlaces, Byte_ascii.Num_0, true);} // EX: 1, 3 returns "001"
|
||||
static String Xto_str_pad_bgn_zero(int val, int places, byte pad_chr, boolean bgn) {
|
||||
int len = DigitCount(val);
|
||||
int pad_len = places - len; if (pad_len < 0) return Int_.Xto_str(val);
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
@ -165,7 +165,7 @@ public class Int_ implements GfoInvkAble {
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_XtoStr_PadBgn)) {
|
||||
int v = m.ReadInt(GfsCore_.Arg_primitive), pad = m.ReadInt("pad");
|
||||
return ctx.Deny() ? (Object)this : Xto_str_pad_bgn(v, pad);
|
||||
return ctx.Deny() ? (Object)this : Xto_str_pad_bgn_zero(v, pad);
|
||||
}
|
||||
else if (ctx.Match(k, "Add")) {
|
||||
int v = m.ReadInt(GfsCore_.Arg_primitive), operand = m.ReadInt("operand");
|
||||
@ -204,10 +204,11 @@ public class Int_ implements GfoInvkAble {
|
||||
if (rvLen < 8) rv = String_.Repeat("0", 8 - rvLen) + rv;
|
||||
return String_.Upper(rv);
|
||||
}
|
||||
public static String Xto_str(int[] ary) {
|
||||
public static String Xto_str(int[] ary) {return Xto_str(ary, " ");}
|
||||
public static String Xto_str(int[] ary, String dlm) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < ary.length; i++)
|
||||
sb.Add_spr_unless_first(Int_.Xto_str(ary[i]), " ", i);
|
||||
sb.Add_spr_unless_first(Int_.Xto_str(ary[i]), dlm, i);
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public static int[] Ary_parse(String raw_str, int reqd_len, int[] or) {
|
||||
|
@ -26,7 +26,7 @@ public class Int__tst {
|
||||
tst_XtoStr_PadLeft_Zeroes(-12 , 3, "-12"); // negative
|
||||
tst_XtoStr_PadLeft_Zeroes(-123 , 3, "-123"); // negative
|
||||
tst_XtoStr_PadLeft_Zeroes(-1234 , 3, "-1234"); // negative
|
||||
} void tst_XtoStr_PadLeft_Zeroes(int val, int zeros, String expd) {Tfds.Eq(expd, Int_.Xto_str_pad_bgn(val, zeros));}
|
||||
} void tst_XtoStr_PadLeft_Zeroes(int val, int zeros, String expd) {Tfds.Eq(expd, Int_.Xto_str_pad_bgn_zero(val, zeros));}
|
||||
@Test public void parseOr_() {
|
||||
tst_ParseOr("", -1); // empty
|
||||
tst_ParseOr("123", 123); // single
|
||||
|
@ -25,6 +25,7 @@ import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
public class DateAdp_ implements GfoInvkAble {
|
||||
public static final String Cls_ref_name = "Date";
|
||||
public static final Class<?> Cls_ref_type = DateAdp.class;
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_Now)) return Now();
|
||||
|
@ -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;
|
||||
public class UuidAdp {
|
||||
public class Guid_adp {
|
||||
public String XtoStr() {return guid.toString();}
|
||||
public UuidAdp(java.util.UUID guid) {this.guid = guid;} java.util.UUID guid;
|
||||
public Guid_adp(java.util.UUID guid) {this.guid = guid;} java.util.UUID guid;
|
||||
}
|
@ -16,8 +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;
|
||||
public class UuidAdp_ {
|
||||
public static final UuidAdp Null = parse_("00000000-0000-0000-0000-000000000000");
|
||||
public static UuidAdp random_() {return new UuidAdp(java.util.UUID.randomUUID());}
|
||||
public static UuidAdp parse_(String s) {return new UuidAdp(java.util.UUID.fromString(s));}
|
||||
public class Guid_adp_ {
|
||||
public static final String Cls_ref_name = "Guid";
|
||||
public static final Guid_adp Empty = parse_("00000000-0000-0000-0000-000000000000");
|
||||
public static Guid_adp random_() {return new Guid_adp(java.util.UUID.randomUUID());}
|
||||
public static Guid_adp parse_(String s) {return new Guid_adp(java.util.UUID.fromString(s));}
|
||||
}
|
@ -17,12 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import org.junit.*;
|
||||
public class UuidAdp__tst {
|
||||
public class Guid_adp__tst {
|
||||
@Test public void parse_() {
|
||||
tst_parse_("467ffb41-cdfe-402f-b22b-be855425784b");
|
||||
}
|
||||
void tst_parse_(String s) {
|
||||
UuidAdp uuid = UuidAdp_.parse_(s);
|
||||
Guid_adp uuid = Guid_adp_.parse_(s);
|
||||
Tfds.Eq(uuid.XtoStr(), s);
|
||||
}
|
||||
}
|
@ -116,7 +116,7 @@ public class TimeSpanAdp_ {
|
||||
zeros = first && !fmt_padZeros ? 1 : padZerosAry[i]; // if first, don't zero pad (avoid "01")
|
||||
dlm = first ? "" : Sprs[i]; // if first, don't use dlm (avoid ":01")
|
||||
sb.Add(dlm);
|
||||
sb.Add(Int_.Xto_str_pad_bgn(val, zeros));
|
||||
sb.Add(Int_.Xto_str_pad_bgn_zero(val, zeros));
|
||||
first = false;
|
||||
}
|
||||
return sb.XtoStr();
|
||||
|
@ -18,6 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx;
|
||||
public class Yn {
|
||||
public static final String Y = "y", N = "n";
|
||||
public static boolean parse_by_char_or(String v, boolean or) {
|
||||
if (String_.Eq(v, Y)) return true;
|
||||
else if (String_.Eq(v, N)) return false;
|
||||
else return or;
|
||||
}
|
||||
public static boolean parse_or_n_(String v) {return parse_or_(v, false);}
|
||||
public static int parse_as_int(String v) {
|
||||
if (v == null) return Bool_.__int;
|
||||
|
@ -65,7 +65,7 @@ public class OrderedHash_base extends HashAdp_base implements OrderedHash, GfoIn
|
||||
int count = ordered.Count();
|
||||
int pad = String_.Len(Int_.Xto_str(count));
|
||||
for (int i = 0; i < count; i++) {
|
||||
sb .Add(Int_.Xto_str_pad_bgn(i, pad))
|
||||
sb .Add(Int_.Xto_str_pad_bgn_zero(i, pad))
|
||||
.Add(":").Add(ordered.FetchAt(i).toString())
|
||||
.Add(Op_sys.Cur().Nl_str());
|
||||
}
|
||||
|
@ -50,6 +50,10 @@ public class Io_mgr { // exists primarily to gather all cmds under gplx namespac
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public void Create_fil_ary(Io_fil[] fil_ary) {
|
||||
for (Io_fil fil : fil_ary)
|
||||
SaveFilStr(fil.Url(), fil.Data());
|
||||
}
|
||||
public Io_url[] QueryDir_fils(Io_url dir) {return QueryDir_args(dir).ExecAsUrlAry();}
|
||||
public IoEngine_xrg_queryDir QueryDir_args(Io_url dir) {return IoEngine_xrg_queryDir.new_(dir);}
|
||||
public void DeleteDirSubs(Io_url url) {IoEngine_xrg_deleteDir.new_(url).Exec();}
|
||||
@ -133,5 +137,6 @@ public class Io_mgr { // exists primarily to gather all cmds under gplx namespac
|
||||
public IoEngine_xrg_downloadFil DownloadFil_args(String src, Io_url trg) {return IoEngine_xrg_downloadFil.new_(src, trg);}
|
||||
public static final Io_mgr _ = new Io_mgr(); public Io_mgr() {}
|
||||
public static final int Len_kb = 1024, Len_mb = 1048576, Len_gb = 1073741824, Len_gb_2 = 2147483647;
|
||||
public static final long Len_mb_long = Len_mb;
|
||||
public static final long Len_null = -1;
|
||||
}
|
||||
|
@ -542,7 +542,7 @@ class Io_stream_rdr_http implements Io_stream_rdr {
|
||||
public Io_stream_rdr_http(IoEngine_xrg_downloadFil xrg) {
|
||||
this.xrg = xrg;
|
||||
} private IoEngine_xrg_downloadFil xrg;
|
||||
public byte Tid() {return Io_stream_.Tid_file;}
|
||||
public byte Tid() {return Io_stream_.Tid_raw;}
|
||||
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
|
||||
private String src_str; private HttpURLConnection src_conn; private java.io.BufferedInputStream src_stream;
|
||||
|
@ -22,7 +22,7 @@ public class IoEngine_xrg_recycleFil extends IoEngine_xrg_fil_affects1_base {
|
||||
|
||||
public int Mode() {return mode;} public IoEngine_xrg_recycleFil Mode_(int v) {mode = v; return this;} int mode;
|
||||
public String AppName() {return appName;} public IoEngine_xrg_recycleFil AppName_(String val) {appName = val; return this;} private String appName = "unknown_app";
|
||||
public UuidAdp Uuid() {return uuid;} public IoEngine_xrg_recycleFil Uuid_(UuidAdp val) {uuid = val; return this;} UuidAdp uuid;
|
||||
public Guid_adp Uuid() {return uuid;} public IoEngine_xrg_recycleFil Uuid_(Guid_adp val) {uuid = val; return this;} Guid_adp uuid;
|
||||
public boolean Uuid_include() {return uuid_include;} public IoEngine_xrg_recycleFil Uuid_include_() {uuid_include = true; return this;} private boolean uuid_include;
|
||||
public DateAdp Time() {return time;} public IoEngine_xrg_recycleFil Time_(DateAdp val) {time = val; return this;} DateAdp time;
|
||||
public ListAdp RootDirNames() {return rootDirNames;} public IoEngine_xrg_recycleFil RootDirNames_(ListAdp val) {rootDirNames = val; return this;} ListAdp rootDirNames;
|
||||
@ -49,7 +49,7 @@ public class IoEngine_xrg_recycleFil extends IoEngine_xrg_fil_affects1_base {
|
||||
public IoEngine_xrg_recycleFil(int v) {
|
||||
mode = v;
|
||||
time = DateAdp_.Now();
|
||||
uuid = UuidAdp_.random_();
|
||||
uuid = Guid_adp_.random_();
|
||||
rootDirNames = ListAdp_.new_(); rootDirNames.Add("z_trash");
|
||||
}
|
||||
public static IoEngine_xrg_recycleFil sysm_(Io_url url) {return new IoEngine_xrg_recycleFil(SysmConst);}
|
||||
|
@ -15,13 +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.dbs; import gplx.*;
|
||||
import gplx.dbs.schemas.*;
|
||||
public class Gfdb_db_base {
|
||||
public Db_conn Conn() {return conn;} private Db_conn conn;
|
||||
public Schema_db_mgr Schema() {return schema;} private Schema_db_mgr schema = new Schema_db_mgr();
|
||||
public void Init(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
schema.Init(conn);
|
||||
}
|
||||
package gplx.ios; import gplx.*;
|
||||
public class Io_fil_mkr {
|
||||
private final ListAdp list = ListAdp_.new_();
|
||||
public Io_fil_mkr Add(String url, String data) {return Add(Io_url_.mem_fil_(url), data);}
|
||||
public Io_fil_mkr Add(Io_url url, String data) {list.Add(new Io_fil(url, data)); return this;}
|
||||
public Io_fil[] To_ary() {return (Io_fil[])list.Xto_ary(Io_fil.class);}
|
||||
}
|
@ -91,6 +91,9 @@ public class Io_size_ {
|
||||
long rv = parse_or_(v, Long_.MinValue); if (rv == Long_.MinValue) throw Err_.new_fmt_("invalid val: {0}", v);
|
||||
return rv;
|
||||
}
|
||||
public static String To_str_mb(long v) {return Long_.Xto_str(v / Io_mgr.Len_mb_long);}
|
||||
public static long To_long_by_msg_mb(GfoMsg m) {return m.ReadLong("v") * Io_mgr.Len_mb_long;}
|
||||
public static long To_long_by_int_mb(int v) {return (long)v * Io_mgr.Len_mb_long;}
|
||||
}
|
||||
class Io_size_fmtr_arg implements Bry_fmtr_arg {
|
||||
public long Val() {return val;} public Io_size_fmtr_arg Val_(long v) {val = v; return this;} long val;
|
||||
|
@ -17,6 +17,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.ios; import gplx.*;
|
||||
public class Io_stream_ { // SERIALIZED
|
||||
public static final byte Tid_null = 0, Tid_file = 1, Tid_zip = 2, Tid_gzip = 3, Tid_bzip2 = 4;
|
||||
public static final byte Tid_null = 0, Tid_raw = 1, Tid_zip = 2, Tid_gzip = 3, Tid_bzip2 = 4;
|
||||
public static final String Ext_zip = ".zip", Ext_gz = ".gz", Ext_bz2 = ".bz2";
|
||||
|
||||
public static String Obsolete_to_str(byte v) {
|
||||
switch (v) {
|
||||
case Io_stream_.Tid_raw : return ".xdat";
|
||||
case Io_stream_.Tid_zip : return ".zip";
|
||||
case Io_stream_.Tid_gzip : return ".gz";
|
||||
case Io_stream_.Tid_bzip2 : return ".bz2";
|
||||
default : throw Err_.unhandled(v);
|
||||
}
|
||||
}
|
||||
public static byte Obsolete_to_tid(String v) {
|
||||
if (String_.Eq(v, ".xdat")) return Io_stream_.Tid_raw;
|
||||
else if (String_.Eq(v, ".zip")) return Io_stream_.Tid_zip;
|
||||
else if (String_.Eq(v, ".gz")) return Io_stream_.Tid_gzip;
|
||||
else if (String_.Eq(v, ".bz2")) return Io_stream_.Tid_bzip2;
|
||||
else throw Err_.unhandled(v);
|
||||
}
|
||||
public static String To_str(byte v) {
|
||||
switch (v) {
|
||||
case Io_stream_.Tid_raw : return "raw";
|
||||
case Io_stream_.Tid_zip : return "zip";
|
||||
case Io_stream_.Tid_gzip : return "gzip";
|
||||
case Io_stream_.Tid_bzip2 : return "bzip2";
|
||||
default : throw Err_.unhandled(v);
|
||||
}
|
||||
}
|
||||
public static byte To_tid(String v) {
|
||||
if (String_.Eq(v, "raw")) return Io_stream_.Tid_raw;
|
||||
else if (String_.Eq(v, "zip")) return Io_stream_.Tid_zip;
|
||||
else if (String_.Eq(v, "gzip")) return Io_stream_.Tid_gzip;
|
||||
else if (String_.Eq(v, "bzip2")) return Io_stream_.Tid_bzip2;
|
||||
else throw Err_.unhandled(v);
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public class Io_stream_rdr_ {
|
||||
}
|
||||
public static Io_stream_rdr new_by_tid_(byte tid) {
|
||||
switch (tid) {
|
||||
case Io_stream_.Tid_file: return new Io_stream_rdr_file();
|
||||
case Io_stream_.Tid_raw: return new Io_stream_rdr_file();
|
||||
case Io_stream_.Tid_zip: return new Io_stream_rdr_zip();
|
||||
case Io_stream_.Tid_gzip: return new Io_stream_rdr_gzip();
|
||||
case Io_stream_.Tid_bzip2: return new Io_stream_rdr_bzip2();
|
||||
@ -124,7 +124,7 @@ class Io_stream_rdr_adp implements Io_stream_rdr {
|
||||
private java.io.InputStream strm;
|
||||
public Io_stream_rdr_adp(java.io.InputStream strm) {this.strm = strm;}
|
||||
public Object Under() {return strm;}
|
||||
public byte Tid() {return Io_stream_.Tid_file;}
|
||||
public byte Tid() {return Io_stream_.Tid_raw;}
|
||||
public Io_url Url() {return url;} public Io_stream_rdr Url_(Io_url v) {this.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 = Io_mgr.Len_null;
|
||||
public void Open_mem(byte[] v) {}
|
||||
@ -172,7 +172,7 @@ abstract class Io_stream_rdr_base implements Io_stream_rdr {
|
||||
public abstract java.io.InputStream Wrap_stream(java.io.InputStream stream);
|
||||
}
|
||||
class Io_stream_rdr_file extends Io_stream_rdr_base {
|
||||
@Override public byte Tid() {return Io_stream_.Tid_file;}
|
||||
@Override public byte Tid() {return Io_stream_.Tid_raw;}
|
||||
public Io_stream_rdr Open() {
|
||||
try {
|
||||
if (!Io_mgr._.Exists(url))
|
||||
|
@ -35,7 +35,7 @@ public class Io_stream_wtr_ {
|
||||
}
|
||||
public static Io_stream_wtr new_by_tid_(byte v) {
|
||||
switch (v) {
|
||||
case gplx.ios.Io_stream_.Tid_file : return new Io_stream_wtr_file();
|
||||
case gplx.ios.Io_stream_.Tid_raw : return new Io_stream_wtr_file();
|
||||
case gplx.ios.Io_stream_.Tid_zip : return new Io_stream_wtr_zip();
|
||||
case gplx.ios.Io_stream_.Tid_gzip : return new Io_stream_wtr_gzip();
|
||||
case gplx.ios.Io_stream_.Tid_bzip2 : return new Io_stream_wtr_bzip2();
|
||||
@ -172,7 +172,7 @@ class Io_stream_wtr_zip implements Io_stream_wtr {
|
||||
}
|
||||
class Io_stream_wtr_file implements Io_stream_wtr {
|
||||
IoStream bry_stream;
|
||||
@Override public byte Tid() {return Io_stream_.Tid_file;}
|
||||
@Override public byte Tid() {return Io_stream_.Tid_raw;}
|
||||
public Io_url Url() {return url;} public Io_stream_wtr Url_(Io_url v) {url = v; return this;} Io_url url;
|
||||
public void Trg_bfr_(Bry_bfr v) {trg_bfr = v;} private Bry_bfr trg_bfr; java.io.ByteArrayOutputStream mem_stream;
|
||||
public Io_stream_wtr Open() {
|
||||
|
@ -45,8 +45,14 @@ public class Op_sys {
|
||||
public static final Op_sys Drd = new_unx_flavor_(Tid_drd, "windows", Bitness_32);
|
||||
public static final Op_sys Wnt = new_wnt_(Sub_tid_unknown, Bitness_32);
|
||||
public static Op_sys Cur() {return cur_op_sys;} static Op_sys cur_op_sys = new_auto_identify_();
|
||||
static Op_sys new_wnt_(byte bitness, byte sub_tid) {return new Op_sys(Tid_wnt , sub_tid , "windows", bitness, "\r\n", Byte_ascii.Backslash , Bool_.N, new byte[] {Byte_ascii.Slash, Byte_ascii.Backslash, Byte_ascii.Lt, Byte_ascii.Gt, Byte_ascii.Colon, Byte_ascii.Pipe, Byte_ascii.Question, Byte_ascii.Asterisk, Byte_ascii.Quote});}
|
||||
static Op_sys new_unx_flavor_(byte tid, String os_name, byte bitness) {return new Op_sys(tid , Sub_tid_unknown , os_name , bitness, "\n" , Byte_ascii.Slash , Bool_.Y, new byte[] {Byte_ascii.Slash});}
|
||||
public static String Fsys_path_to_lnx(String v) {
|
||||
return cur_op_sys.Tid_is_wnt() ? String_.Replace(v, Wnt.fsys_dir_spr_str, Lnx.fsys_dir_spr_str) : v;
|
||||
}
|
||||
public static String Fsys_path_to_wnt(String v) {
|
||||
return cur_op_sys.Tid_is_wnt() ? String_.Replace(v, Lnx.fsys_dir_spr_str, Wnt.fsys_dir_spr_str) : v;
|
||||
}
|
||||
private static Op_sys new_wnt_(byte bitness, byte sub_tid) {return new Op_sys(Tid_wnt , sub_tid , "windows", bitness, "\r\n", Byte_ascii.Backslash , Bool_.N, new byte[] {Byte_ascii.Slash, Byte_ascii.Backslash, Byte_ascii.Lt, Byte_ascii.Gt, Byte_ascii.Colon, Byte_ascii.Pipe, Byte_ascii.Question, Byte_ascii.Asterisk, Byte_ascii.Quote});}
|
||||
private static Op_sys new_unx_flavor_(byte tid, String os_name, byte bitness) {return new Op_sys(tid , Sub_tid_unknown , os_name , bitness, "\n" , Byte_ascii.Slash , Bool_.Y, new byte[] {Byte_ascii.Slash});}
|
||||
static final String GRP_KEY = "gplx.op_sys";
|
||||
// public static Op_sys Cur_() {cur_op_sys = new_auto_identify_(); return cur_op_sys;}
|
||||
static Op_sys new_auto_identify_() {
|
||||
|
@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Gfo_usr_dlg_ {
|
||||
public static Gfo_usr_dlg _ = Gfo_usr_dlg_null._;
|
||||
public static Gfo_usr_dlg I = Gfo_usr_dlg_null._;
|
||||
public static final Gfo_usr_dlg Null = Gfo_usr_dlg_null._;
|
||||
}
|
||||
class Gfo_usr_dlg_null implements Gfo_usr_dlg {
|
||||
|
@ -207,14 +207,14 @@ class TfdsMsgBldr {
|
||||
for (int i = 0; i < list.Count(); i++) {
|
||||
TfdsEqAryItm itm = (TfdsEqAryItm)list.FetchAt(i);
|
||||
sb.Add_fmt_line("{0}: {1} {2} {3}"
|
||||
, Int_.Xto_str_pad_bgn(itm.Idx(), 4)
|
||||
, Int_.Xto_str_pad_bgn_zero(itm.Idx(), 4)
|
||||
, String_.PadBgn(itm.Lhs(), lhsLenMax, " ")
|
||||
, itm.Eq() ? "==" : "!="
|
||||
, String_.PadBgn(itm.Rhs(), rhsLenMax, " ")
|
||||
);
|
||||
}
|
||||
// String compSym = isEq ? " " : "!=";
|
||||
// String result = String_.Format("{0}: {1}{2} {3} {4}", Int_.Xto_str_pad_bgn(i, 4), lhsString, String_.CrLf + "\t\t", compSym, rhsString);
|
||||
// String result = String_.Format("{0}: {1}{2} {3} {4}", Int_.Xto_str_pad_bgn_zero(i, 4), lhsString, String_.CrLf + "\t\t", compSym, rhsString);
|
||||
// foreach (Object obj in list) {
|
||||
// String itmComparison = (String)obj;
|
||||
// sb.Add_fmt_line("{0}{1}", "\t\t", itmComparison);
|
||||
|
@ -45,7 +45,7 @@ public class IoEngine_fil_basic_memory_tst extends IoEngine_fil_basic_base {
|
||||
list.DelAt(0); // remove drive
|
||||
IoEngine_xrg_recycleFil recycleXrg = bin.Send_xrg(fil)
|
||||
.RootDirNames_(list)
|
||||
.AppName_("gplx.test").Time_(DateAdp_.parse_gplx("20100102_115559123")).Uuid_(UuidAdp_.parse_("467ffb41-cdfe-402f-b22b-be855425784b"));
|
||||
.AppName_("gplx.test").Time_(DateAdp_.parse_gplx("20100102_115559123")).Uuid_(Guid_adp_.parse_("467ffb41-cdfe-402f-b22b-be855425784b"));
|
||||
recycleXrg.Exec();
|
||||
fx.tst_ExistsPaths(false, fil);
|
||||
fx.tst_ExistsPaths(true, recycleXrg.RecycleUrl());
|
||||
|
@ -37,7 +37,7 @@ public class IoEngine_fil_basic_system_tst extends IoEngine_fil_basic_base {
|
||||
ListAdp list = root.XtoNames(); list.DelAt(0); // remove drive
|
||||
IoEngine_xrg_recycleFil recycleXrg = bin.Send_xrg(fil)
|
||||
.RootDirNames_(list)
|
||||
.AppName_("gplx.test").Time_(DateAdp_.parse_gplx("20100102_115559123")).Uuid_(UuidAdp_.parse_("467ffb41-cdfe-402f-b22b-be855425784b"));
|
||||
.AppName_("gplx.test").Time_(DateAdp_.parse_gplx("20100102_115559123")).Uuid_(Guid_adp_.parse_("467ffb41-cdfe-402f-b22b-be855425784b"));
|
||||
recycleXrg.Exec();
|
||||
fx.tst_ExistsPaths(false, fil);
|
||||
fx.tst_ExistsPaths(true, recycleXrg.RecycleUrl());
|
||||
|
@ -25,7 +25,7 @@ public class IoEngine_xrg_recycleFil_tst {
|
||||
tst_GenRecycleUrl(recycle_(), Io_url_.mem_fil_("mem/z_trash/20100102/gplx.images;115559123;;fil.txt"));
|
||||
tst_GenRecycleUrl(recycle_().Uuid_include_(), Io_url_.mem_fil_("mem/z_trash/20100102/gplx.images;115559123;467ffb41-cdfe-402f-b22b-be855425784b;fil.txt"));
|
||||
}
|
||||
IoEngine_xrg_recycleFil recycle_() {return IoEngine_xrg_recycleFil.gplx_(Io_url_.mem_fil_("mem/dir/fil.txt")).AppName_("gplx.images").Uuid_(UuidAdp_.parse_("467ffb41-cdfe-402f-b22b-be855425784b")).Time_(DateAdp_.parse_gplx("20100102_115559123"));}
|
||||
IoEngine_xrg_recycleFil recycle_() {return IoEngine_xrg_recycleFil.gplx_(Io_url_.mem_fil_("mem/dir/fil.txt")).AppName_("gplx.images").Uuid_(Guid_adp_.parse_("467ffb41-cdfe-402f-b22b-be855425784b")).Time_(DateAdp_.parse_gplx("20100102_115559123"));}
|
||||
void tst_GenRecycleUrl(IoEngine_xrg_recycleFil xrg, Io_url expd) {
|
||||
Tfds.Eq(expd, xrg.RecycleUrl());
|
||||
}
|
||||
|
@ -67,12 +67,12 @@ class GfmlStringHighlighter {
|
||||
int gap = nxtMarkBgn - endPos;
|
||||
if (gap > 0) {
|
||||
int gapDigits = Int_.DigitCount(gap);
|
||||
posBfr.Add_fmt("[{0}]", Int_.Xto_str_pad_bgn(gap, gapDigits));
|
||||
posBfr.Add_fmt("[{0}]", Int_.Xto_str_pad_bgn_zero(gap, gapDigits));
|
||||
rawBfr.Add_fmt("[{0}]", String_.Repeat(".", gapDigits));
|
||||
symBfr.Add_fmt(" {0} ", String_.Repeat(" ", gapDigits));
|
||||
}
|
||||
if (curMark.Sym() != ' ')
|
||||
symList.Add(String_.Format("[{0}] {1} {2}", Int_.Xto_str_pad_bgn(curMark.Pos(), rawLenDigits), curMark.Sym(), curMark.Msg()));
|
||||
symList.Add(String_.Format("[{0}] {1} {2}", Int_.Xto_str_pad_bgn_zero(curMark.Pos(), rawLenDigits), curMark.Sym(), curMark.Msg()));
|
||||
}
|
||||
if (rawBfrBgn == 0) {
|
||||
posBfr.Add_at(0, "<");
|
||||
|
@ -8,7 +8,7 @@
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/110_gfml"/>
|
||||
<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.12-bin.jar"/>
|
||||
<classpathentry kind="lib" path="lib/postgresql-8.4-701.jdbc4.jar"/>
|
||||
<classpathentry kind="lib" path="lib/sqlite-jdbc-3.7.15-M1.jar"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="lib" path="lib/sqlite-jdbc-3.7.15-M1.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
@ -1,43 +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.*;
|
||||
public class Db_batch_wkr__attach implements Db_batch_wkr {
|
||||
private final Db_conn conn;
|
||||
private final ListAdp list = ListAdp_.new_();
|
||||
public Db_batch_wkr__attach Add(String alias, Io_url url) {list.Add(new Db_batch_wkr__attach_itm(alias, url)); return this;}
|
||||
public Db_batch_wkr__attach(Db_conn conn) {this.conn = conn;}
|
||||
public void Batch_bgn() {
|
||||
int len = list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_batch_wkr__attach_itm itm = (Db_batch_wkr__attach_itm)list.FetchAt(i);
|
||||
conn.Exec_env_db_attach(itm.Alias(), itm.Url());
|
||||
}
|
||||
}
|
||||
public void Batch_end() {
|
||||
int len = list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_batch_wkr__attach_itm itm = (Db_batch_wkr__attach_itm)list.FetchAt(i);
|
||||
conn.Exec_env_db_detach(itm.Alias());
|
||||
}
|
||||
}
|
||||
}
|
||||
class Db_batch_wkr__attach_itm {
|
||||
public Db_batch_wkr__attach_itm(String alias, Io_url url) {this.alias = alias; this.url = url;}
|
||||
public String Alias() {return alias;} private final String alias;
|
||||
public Io_url Url() {return url;} private final Io_url url;
|
||||
}
|
@ -1,26 +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.*;
|
||||
public class Db_batch_wkr__msg implements Db_batch_wkr {
|
||||
private final Gfo_usr_dlg usr_dlg; private final String msg_pre;
|
||||
public Db_batch_wkr__msg(Gfo_usr_dlg usr_dlg, String msg_pre) {this.usr_dlg = usr_dlg; this.msg_pre = msg_pre;}
|
||||
public Gfo_usr_dlg Usr_dlg() {return usr_dlg;}
|
||||
public String Msg() {return msg;} public Db_batch_wkr__msg Msg_(String v) {msg = v; return this;} private String msg;
|
||||
public void Batch_bgn() {usr_dlg.Plog_many("", "", "bgn:" + msg_pre + "." + msg);}
|
||||
public void Batch_end() {usr_dlg.Plog_many("", "", "end:" + msg_pre + "." + msg);}
|
||||
}
|
@ -18,18 +18,15 @@ 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 rls_list = ListAdp_.new_();
|
||||
public Db_conn(Db_engine engine) {
|
||||
this.engine = 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 void Txn_bgn() {txn_mgr.Txn_bgn();}
|
||||
public void Txn_commit() {txn_mgr.Txn_end(); txn_mgr.Txn_bgn();}
|
||||
public void Txn_end() {txn_mgr.Txn_end();}
|
||||
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()));}
|
||||
private final ListAdp rls_list = ListAdp_.new_(); private final Db_engine engine;
|
||||
public Db_conn(Db_engine engine) {this.engine = engine;}
|
||||
public Db_conn_info Conn_info() {return engine.Conn_info();}
|
||||
public void Txn_bgn() {engine.Txn_bgn("");}
|
||||
public void Txn_bgn(String name) {engine.Txn_bgn(name);}
|
||||
public void Txn_end() {engine.Txn_end();}
|
||||
public void Txn_cxl() {engine.Txn_cxl();}
|
||||
public void Txn_sav() {engine.Txn_sav();}
|
||||
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_wo_autonum()));}
|
||||
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)));}
|
||||
@ -37,46 +34,40 @@ public class Db_conn {
|
||||
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, null));}
|
||||
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(), null));}
|
||||
public Db_stmt Stmt_select_order(String tbl, Db_meta_fld_list flds, String[] where, String... orderbys) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary(), orderbys));}
|
||||
public void Exec_create_tbl_and_idx(Db_meta_tbl meta) {
|
||||
engine.Exec_ddl_create_tbl(meta);
|
||||
engine.Exec_ddl_create_idx(Gfo_usr_dlg_.Null, meta.Idxs());
|
||||
}
|
||||
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... idxs) {engine.Exec_ddl_create_idx(usr_dlg, idxs);}
|
||||
public void Exec_env_db_attach(String alias, Io_url db_url) {engine.Exec_env_db_attach(alias, db_url);}
|
||||
public void Exec_env_db_detach(String alias) {engine.Exec_env_db_detach(alias);}
|
||||
public void Exec_ddl_append_fld(String tbl, Db_meta_fld fld) {engine.Exec_ddl_append_fld(tbl, fld);}
|
||||
public Db_stmt Rls_reg(Db_stmt stmt) {rls_list.Add(stmt); return stmt;}
|
||||
public void Conn_term() {
|
||||
public Db_stmt Stmt_select_order(String tbl, String[] flds, String[] where, String... orderbys) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds, orderbys));}
|
||||
public Db_stmt Stmt_new(Db_qry qry) {return engine.New_stmt_prep(qry);}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {engine.Env_db_attach(alias, db_url);}
|
||||
public void Env_db_detach(String alias) {engine.Env_db_detach(alias);}
|
||||
public void Env_vacuum() {Exec_sql_plog_ntx("vacuuming: url=" + this.Conn_info().Xto_api(), "VACUUM;");}
|
||||
public void Ddl_create_tbl(Db_meta_tbl meta) {engine.Ddl_create_tbl(meta); engine.Ddl_create_idx(Gfo_usr_dlg_.Null, meta.Idxs());}
|
||||
public void Ddl_create_idx(Db_meta_idx... idxs) {engine.Ddl_create_idx(Gfo_usr_dlg_.I, idxs);}
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... idxs) {engine.Ddl_create_idx(usr_dlg, idxs);}
|
||||
public void Ddl_append_fld(String tbl, Db_meta_fld fld) {engine.Ddl_append_fld(tbl, fld);}
|
||||
public void Ddl_delete_tbl(String tbl) {engine.Ddl_delete_tbl(tbl);}
|
||||
public void Rls_reg(RlsAble rls) {rls_list.Add(rls);}
|
||||
public void Rls_conn() {
|
||||
int len = rls_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
RlsAble itm = (RlsAble)rls_list.FetchAt(i);
|
||||
itm.Rls();
|
||||
}
|
||||
engine.Conn_term();
|
||||
// Db_conn_pool.I.Del(this.Url()); // remove from pool, else rls'd instance will be cached and fail upon next use
|
||||
Db_conn_pool.I.Del(engine.Conn_info());
|
||||
}
|
||||
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 int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
|
||||
public Db_rdr Exec_sql_as_rdr2(String sql) {return this.Stmt_new(Db_qry_sql.dml_(sql)).Exec_select__rls_auto();}
|
||||
public int Exec_sql_plog_ntx(String msg, String sql) {return Exec_sql_plog(Bool_.N, msg, sql);}
|
||||
public int Exec_sql_plog_txn(String msg, String sql) {return Exec_sql_plog(Bool_.Y, msg, sql);}
|
||||
public int Exec_sql_plog(boolean txn, String msg, String sql) {
|
||||
Gfo_usr_dlg_.I.Plog_many("", "", "bgn: " + msg);
|
||||
if (txn) this.Txn_bgn();
|
||||
int rv = Exec_sql(sql);
|
||||
if (txn) this.Txn_end();
|
||||
Gfo_usr_dlg_.I.Plog_many("", "", "end: " + msg);
|
||||
return rv;
|
||||
}
|
||||
public int Exec_qry(Db_qry qry) {return Int_.cast_(engine.Exec_as_obj(qry));}
|
||||
public DataRdr Exec_qry_as_rdr(Db_qry qry) {return DataRdr_.cast_(engine.Exec_as_obj(qry));}
|
||||
public int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
|
||||
public DataRdr Exec_sql_as_rdr(String sql) {return this.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));}
|
||||
public void Exec_sql_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... idxs) {engine.Exec_ddl_create_idx(usr_dlg, idxs);}
|
||||
public void Exec_sql(Db_batch_wkr... wkrs) {
|
||||
int len = wkrs.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_batch_wkr wkr = wkrs[i];
|
||||
wkr.Batch_bgn();
|
||||
wkr.Batch_end();
|
||||
}
|
||||
}
|
||||
public void Exec_sql__vacuum(Db_batch_wkr__msg msg) {
|
||||
msg.Msg_("vaccuum");
|
||||
Exec_sql(msg, Batch_sql("VACCUUM;"));
|
||||
}
|
||||
public void Exec_sql__idx(Db_batch_wkr__msg msg, Db_meta_idx... idxs) {
|
||||
engine.Exec_ddl_create_idx(msg.Usr_dlg(), idxs);
|
||||
}
|
||||
public Db_batch_wkr__msg Batch_msg(Gfo_usr_dlg usr_dlg, String msg_pre) {return new Db_batch_wkr__msg(usr_dlg, msg_pre);}
|
||||
public Db_batch_wkr__attach Batch_attach(String alias, Io_url url) {return new Db_batch_wkr__attach(this).Add(alias, url);}
|
||||
public Db_batch_wkr__sql Batch_sql(String... lines) {return new Db_batch_wkr__sql(this, lines);}
|
||||
public int Exec_sql_args(String sql, Object... args) {return this.Exec_qry(Db_qry_sql.dml_(String_.Format(sql, args)));}
|
||||
public DataRdr Exec_sql_as_rdr(String sql) {return this.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ 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 = Db_conn_pool.I.Get_or_new(Db_url_.Null);
|
||||
public static final Db_conn Empty = Db_conn_pool.I.Get_or_new(Db_conn_info_.Null);
|
||||
public static int Select_fld0_as_int_or(Db_conn p, String sql, int or) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
|
@ -20,11 +20,12 @@ 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);
|
||||
public boolean Exists(Io_url url) {return wkr.Exists(url);}
|
||||
public Db_conn Get(Io_url url) {return wkr.Get(url);}
|
||||
public Db_conn New(Io_url url) {return wkr.New(url);}
|
||||
public Db_conn_bldr_data Get_or_new(Io_url url) {
|
||||
boolean exists = wkr.Exists(url);
|
||||
Db_conn conn = exists ? Get(url) : New(url);
|
||||
return new Db_conn_bldr_data(conn, exists);
|
||||
}
|
||||
public static final Db_conn_bldr I = new Db_conn_bldr(); Db_conn_bldr() {}
|
||||
|
@ -19,24 +19,21 @@ 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);
|
||||
boolean Exists(Io_url url);
|
||||
Db_conn Get(Io_url url);
|
||||
Db_conn New(Io_url url);
|
||||
}
|
||||
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);
|
||||
public boolean Exists(Io_url url) {return Io_mgr._.ExistsFil(url);}
|
||||
public Db_conn Get(Io_url url) {
|
||||
if (!Io_mgr._.ExistsFil(url)) return null;
|
||||
Db_conn_info db_url = Db_conn_info_.sqlite_(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);
|
||||
public Db_conn New(Io_url url) {
|
||||
Io_mgr._.CreateDirIfAbsent(url.OwnerDir()); // must assert that dir exists
|
||||
Db_conn_info db_url = Sqlite_conn_info.make_(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
|
||||
@ -47,22 +44,19 @@ class Db_conn_bldr_wkr__sqlite implements Db_conn_bldr_wkr {
|
||||
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();
|
||||
public boolean Exists(Io_url url) {
|
||||
String io_url_str = 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();
|
||||
public Db_conn Get(Io_url url) {
|
||||
String io_url_str = url.Xto_api();
|
||||
if (!hash.Has(io_url_str)) return null;
|
||||
return Db_conn_pool.I.Get_or_new__mem(io_url.Xto_api());
|
||||
return Db_conn_pool.I.Get_or_new__mem(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();
|
||||
public Db_conn New(Io_url url) {
|
||||
String io_url_str = url.Xto_api();
|
||||
hash.Add(io_url_str, io_url_str);
|
||||
return Db_conn_pool.I.Get_or_new__mem(io_url.Xto_api());
|
||||
return Db_conn_pool.I.Get_or_new__mem(url.Xto_api());
|
||||
}
|
||||
public static final Db_conn_bldr_wkr__mem I = new Db_conn_bldr_wkr__mem(); Db_conn_bldr_wkr__mem() {}
|
||||
}
|
||||
|
@ -16,10 +16,10 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_url {
|
||||
public interface Db_conn_info {
|
||||
String Tid();
|
||||
String Database();
|
||||
String Xto_raw();
|
||||
String Xto_api();
|
||||
Db_url New_self(String raw, GfoMsg m);
|
||||
Db_conn_info New_self(String raw, GfoMsg m);
|
||||
}
|
@ -18,23 +18,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
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 = 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 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 = Tdb_url.Tid_const;
|
||||
public class Db_conn_info_ {
|
||||
public static final Db_conn_info Null = Null_conn_info._;
|
||||
public static final Db_conn_info Test = Mysql_conn_info.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static Db_conn_info parse_(String raw) {return Db_conn_info_pool._.Parse(raw);}
|
||||
public static Db_conn_info sqlite_(Io_url url) {return Sqlite_conn_info.load_(url);}
|
||||
public static Db_conn_info tdb_(Io_url url) {return Tdb_conn_info.new_(url);}
|
||||
public static Db_conn_info mem_(String db) {return Db_conn_info__mem.new_(db);}
|
||||
public static final String Key_tdb = Tdb_conn_info.Tid_const;
|
||||
}
|
||||
class Db_url_pool {
|
||||
class Db_conn_info_pool {
|
||||
private OrderedHash regy = OrderedHash_.new_();
|
||||
public Db_url_pool() {
|
||||
this.Add(Null_url._).Add(Tdb_url._).Add(Mysql_url._).Add(Postgres_url._).Add(Sqlite_url._);
|
||||
this.Add(Db_url__mem.I);
|
||||
public Db_conn_info_pool() {
|
||||
this.Add(Null_conn_info._).Add(Tdb_conn_info._).Add(Mysql_conn_info._).Add(Postgres_conn_info._).Add(Sqlite_conn_info._);
|
||||
this.Add(Db_conn_info__mem.I);
|
||||
}
|
||||
public Db_url_pool Add(Db_url itm) {regy.AddReplace(itm.Tid(), itm); return this;}
|
||||
public Db_url Parse(String raw) {// assume each pair has format of: name=val;
|
||||
public Db_conn_info_pool Add(Db_conn_info itm) {regy.AddReplace(itm.Tid(), itm); return this;}
|
||||
public Db_conn_info Parse(String raw) {// assume each pair has format of: name=val;
|
||||
try {
|
||||
GfoMsg m = GfoMsg_.new_parse_("db_url");
|
||||
String[] terms = String_.Split(raw, ";");
|
||||
@ -47,10 +47,10 @@ class Db_url_pool {
|
||||
else
|
||||
m.Add(kv[0], kv[1]);
|
||||
}
|
||||
Db_url prototype = (Db_url)regy.Fetch(url_tid);
|
||||
Db_conn_info prototype = (Db_conn_info)regy.Fetch(url_tid);
|
||||
return prototype.New_self(raw, m);
|
||||
}
|
||||
catch(Exception exc) {throw Err_.parse_type_exc_(exc, Db_url.class, raw);}
|
||||
catch(Exception exc) {throw Err_.parse_type_exc_(exc, Db_conn_info.class, raw);}
|
||||
}
|
||||
public static final Db_url_pool _ = new Db_url_pool();
|
||||
public static final Db_conn_info_pool _ = new Db_conn_info_pool();
|
||||
}
|
@ -17,13 +17,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.core.strings.*;
|
||||
public abstract class Db_url__base implements Db_url {
|
||||
public abstract class Db_conn_info__base implements Db_conn_info {
|
||||
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;} protected String database = "";
|
||||
public String Server() {return server;} private String server = "";
|
||||
public abstract Db_url New_self(String raw, GfoMsg m);
|
||||
public abstract Db_conn_info New_self(String raw, GfoMsg m);
|
||||
protected void Ctor(String server, String database, String raw, String api) {this.server = server; this.database = database; this.raw = raw; this.api = api;}
|
||||
protected static String BldApi(GfoMsg m, KeyVal... xtnAry) {
|
||||
String_bldr sb = String_bldr_.new_();
|
@ -17,10 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_url_tst {
|
||||
public class Db_conn_info_tst {
|
||||
@Before public void setup() {
|
||||
regy.Add(Db_url_mock._);
|
||||
} private final Db_url_pool regy = new Db_url_pool();
|
||||
regy.Add(Db_conn_info_mock._);
|
||||
} private final Db_conn_info_pool regy = new Db_conn_info_pool();
|
||||
@Test public void Parse() {
|
||||
tst_Parse("gplx_key=mock;id=1;", kv_("id", "1")); // one; gplx_key removed
|
||||
tst_Parse("gplx_key=mock;id=1;name=me;", kv_("id", "1"), kv_("name", "me")); // many
|
||||
@ -28,19 +28,19 @@ public class Db_url_tst {
|
||||
}
|
||||
private KeyVal kv_(String key, Object val) {return KeyVal_.new_(key, val);}
|
||||
private void tst_Parse(String raw, KeyVal... expd) {
|
||||
Db_url_mock mock = (Db_url_mock)regy.Parse(raw);
|
||||
Db_conn_info_mock mock = (Db_conn_info_mock)regy.Parse(raw);
|
||||
Tfds.Eq_ary_str(expd, mock.Kvs());
|
||||
}
|
||||
}
|
||||
class Db_url_mock extends Db_url__base {
|
||||
class Db_conn_info_mock extends Db_conn_info__base {
|
||||
public KeyVal[] Kvs() {return kvs;} KeyVal[] kvs;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mock";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url_mock rv = new Db_url_mock();
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Db_conn_info_mock rv = new Db_conn_info_mock();
|
||||
rv.kvs = new KeyVal[m.Args_count()];
|
||||
for (int i = 0; i < m.Args_count(); i++)
|
||||
rv.kvs[i] = m.Args_getAt(i);
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url_mock _ = new Db_url_mock(); Db_url_mock() {}
|
||||
public static final Db_conn_info_mock _ = new Db_conn_info_mock(); Db_conn_info_mock() {}
|
||||
}
|
@ -21,11 +21,11 @@ import gplx.dbs.engines.sqlite.*; import gplx.dbs.engines.mysql.*; import gplx.d
|
||||
public class Db_conn_pool {
|
||||
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) {
|
||||
public void Del(Db_conn_info url) {conn_hash.Del(url.Xto_api());}
|
||||
public Db_conn Get_or_new__mem(String db) {return Get_or_new(Db_conn_info__mem.new_(db));}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_conn_info_.sqlite_(url));}
|
||||
public Db_conn Get_or_new(String s) {return Get_or_new(Db_conn_info_.parse_(s));}
|
||||
public Db_conn Get_or_new(Db_conn_info url) {
|
||||
Db_conn rv = (Db_conn)conn_hash.Fetch(url.Xto_api());
|
||||
if (rv == null) {
|
||||
Db_engine prime = (Db_engine)engine_hash.Fetch(url.Tid()); if (prime == null) throw Err_.new_("db engine prototype not found; tid={0}", url.Tid());
|
||||
|
@ -17,16 +17,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_fld {
|
||||
public Db_meta_fld(String name, int tid, int len, boolean nullable, boolean primary, boolean autoincrement, Object default_value) {
|
||||
public Db_meta_fld(String name, int tid, int len, boolean nullable, boolean primary, boolean autonum, Object default_value) {
|
||||
this.name = name; this.tid = tid; this.len = len;
|
||||
this.nullable = nullable; this.primary = primary; this.autoincrement = autoincrement; this.default_value = default_value;
|
||||
this.nullable = nullable; this.primary = primary; this.autonum = autonum; this.default_value = default_value;
|
||||
}
|
||||
public int Tid() {return tid;} private final int tid;
|
||||
public String Name() {return name;} private final String name;
|
||||
public int Len() {return len;} private final int len;
|
||||
public boolean Nullable() {return nullable;} private final boolean nullable;
|
||||
public boolean Primary() {return primary;} private final boolean primary;
|
||||
public boolean Autoincrement() {return autoincrement;} private final boolean autoincrement;
|
||||
public boolean Autonum() {return autonum;} private final boolean autonum;
|
||||
public Object Default_value() {return default_value;} private final Object default_value;
|
||||
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 Key_null = null;
|
||||
|
@ -23,6 +23,16 @@ public class Db_meta_fld_list {
|
||||
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_wo_autonum() {
|
||||
int len = flds.Count();
|
||||
ListAdp rv = ListAdp_.new_();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_meta_fld fld = (Db_meta_fld)flds.FetchAt(i);
|
||||
if (fld.Autonum()) continue;
|
||||
rv.Add(fld.Name());
|
||||
}
|
||||
return (String[])rv.Xto_ary(String.class);
|
||||
}
|
||||
public String[] To_str_ary_exclude(String[] ary) {
|
||||
HashAdp ary_hash = HashAdp_.new_();
|
||||
ListAdp rv = ListAdp_.new_();
|
||||
@ -46,11 +56,14 @@ public class Db_meta_fld_list {
|
||||
public String Add_int(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.N, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_int_pkey(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_int_pkey_autonum(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.Y, Db_meta_fld.Default_value_null);}
|
||||
public String Add_int_autonum(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_int_dflt(String name, int dflt) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.N, Bool_.N, dflt);}
|
||||
public String Add_long(String name) {return Add(name, Db_meta_fld.Tid_long, Len_null, Bool_.N, Bool_.N, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_float(String name) {return Add(name, Db_meta_fld.Tid_float, Len_null, Bool_.N, Bool_.N, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_double(String name) {return Add(name, Db_meta_fld.Tid_double, Len_null, Bool_.N, Bool_.N, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_str(String name, int len) {return Add(name, Db_meta_fld.Tid_str, len, Bool_.N, Bool_.N, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_str_pkey(String name, int len) {return Add(name, Db_meta_fld.Tid_str, len, Bool_.N, Bool_.Y, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_str_null(String name, int len) {return Add(name, Db_meta_fld.Tid_str, len, Bool_.Y, Bool_.N, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
public String Add_str_dflt(String name, int len, String dflt)
|
||||
{return Add(name, Db_meta_fld.Tid_str, len, Bool_.N, Bool_.N, Bool_.N, dflt);}
|
||||
public String Add_text(String name) {return Add(name, Db_meta_fld.Tid_text, Len_null, Bool_.N, Bool_.N, Bool_.N, Db_meta_fld.Default_value_null);}
|
||||
|
@ -31,4 +31,5 @@ public class Db_meta_idx {
|
||||
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);}
|
||||
public static final Db_meta_idx[] Ary_empty = new Db_meta_idx[0];
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ 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) {
|
||||
if (idxs == null) idxs = Db_meta_idx.Ary_empty; // empty params will pass idxs of null; set to idxs[0] else null ref when calling create_table
|
||||
this.name = name; this.flds = flds; this.idxs = idxs;
|
||||
}
|
||||
public String Name() {return name;} private final String name;
|
||||
|
@ -17,9 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_rdr_ {
|
||||
public static final Db_rdr Null = new Db_rdr__null();
|
||||
public static final Db_rdr Empty = new Db_rdr__empty();
|
||||
}
|
||||
class Db_rdr__null implements Db_rdr {
|
||||
class Db_rdr__empty implements Db_rdr {
|
||||
public boolean Move_next() {return false;}
|
||||
public byte[] Read_bry(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry(String k) {return Bry_.Empty;}
|
||||
|
@ -19,7 +19,8 @@ package gplx.dbs; import gplx.*;
|
||||
import java.sql.ResultSet;
|
||||
public class Db_rdr__basic implements Db_rdr {
|
||||
protected ResultSet rdr;
|
||||
public void Ctor(ResultSet rdr, String sql) {this.rdr = rdr; this.sql = sql;}
|
||||
private Db_stmt stmt;
|
||||
public void Ctor(Db_stmt stmt, ResultSet rdr, String sql) {this.stmt = stmt; this.rdr = rdr; this.sql = sql;}
|
||||
public String Sql() {return sql;} private String sql;
|
||||
public boolean Move_next() {
|
||||
try {return rdr.next();}
|
||||
@ -45,5 +46,9 @@ public class Db_rdr__basic implements Db_rdr {
|
||||
@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));}}
|
||||
@gplx.Virtual public void Rls() {
|
||||
try {rdr.close();}
|
||||
catch (Exception e) {throw Err_.new_("close failed: err={0}", Err_.Message_lang(e));}
|
||||
if (stmt != null) stmt.Rls();
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,8 @@ public interface Db_stmt extends RlsAble {
|
||||
int Exec_update();
|
||||
int Exec_delete();
|
||||
DataRdr Exec_select();
|
||||
Db_rdr Exec_select_as_rdr();
|
||||
Db_rdr Exec_select__rls_manual();
|
||||
Db_rdr Exec_select__rls_auto();
|
||||
Object Exec_select_val();
|
||||
void Ctor_stmt(Db_engine engine, Db_qry qry);
|
||||
Db_stmt Clear();
|
||||
|
@ -38,8 +38,8 @@ public class Db_stmt_bldr {
|
||||
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 Batch_bgn() {conn.Txn_bgn(tbl_name);}
|
||||
public void Batch_end() {conn.Txn_end();}
|
||||
public void Rls() {
|
||||
create = Db_stmt_.Rls(create);
|
||||
update = Db_stmt_.Rls(update);
|
||||
|
@ -1,40 +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.dbs.engines.*;
|
||||
public class Db_txn_mgr {
|
||||
public Db_txn_mgr(Db_engine engine) {this.engine = engine;} private final Db_engine engine;
|
||||
public int Txn_depth() {return txn_depth;} int txn_depth; // NOTE: only support 1 level for now;
|
||||
public void Txn_bgn_if_none() {if (txn_depth == 0) this.Txn_bgn();}
|
||||
public void Txn_bgn() {
|
||||
engine.Txn_bgn();
|
||||
++txn_depth;
|
||||
}
|
||||
public void Txn_end_all() {this.Txn_end();}
|
||||
public void Txn_end() {
|
||||
if (txn_depth == 0) return;
|
||||
engine.Txn_end();
|
||||
--txn_depth;
|
||||
txn_count = 0;
|
||||
}
|
||||
public void Txn_end_all_bgn_if_none() {
|
||||
this.Txn_end_all();
|
||||
this.Txn_bgn_if_none();
|
||||
}
|
||||
public int Txn_count() {return txn_count;} public void Txn_count_(int v) {txn_count = v;} int txn_count;
|
||||
}
|
@ -18,20 +18,24 @@ 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_conn_info Conn_info();
|
||||
Db_engine New_clone(Db_conn_info url);
|
||||
Db_rdr New_rdr__rls_manual (Object rdr_obj, String sql); // Object o:ResultSet if desktop; Cursor if android
|
||||
Db_rdr New_rdr__rls_auto (Db_stmt stmt, Object rdr_obj, 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_bgn(String name);
|
||||
void Txn_end();
|
||||
void Txn_cxl();
|
||||
void Txn_sav();
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
Object Exec_as_obj(Db_qry qry);
|
||||
void Exec_ddl_create_tbl(Db_meta_tbl meta);
|
||||
void Exec_ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary);
|
||||
void Exec_ddl_append_fld(String tbl, Db_meta_fld fld);
|
||||
void Exec_env_db_attach(String alias, Io_url db_url);
|
||||
void Exec_env_db_detach(String alias);
|
||||
void Ddl_create_tbl(Db_meta_tbl meta);
|
||||
void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary);
|
||||
void Ddl_append_fld(String tbl, Db_meta_fld fld);
|
||||
void Ddl_delete_tbl(String tbl);
|
||||
void Env_db_attach(String alias, Io_url db_url);
|
||||
void Env_db_detach(String alias);
|
||||
}
|
||||
|
@ -19,19 +19,18 @@ package gplx.dbs.engines; import gplx.*; import gplx.dbs.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.engines.*; import gplx.dbs.qrys.*; import gplx.dbs.sqls.*;
|
||||
public abstract class Db_engine_sql_base implements Db_engine {
|
||||
@gplx.Internal protected void Ctor(Db_url url) {this.url = url;}
|
||||
@gplx.Internal protected void Ctor(Db_conn_info conn_info) {this.conn_info = conn_info;}
|
||||
public abstract String Tid();
|
||||
public Db_url Url() {return url;} protected Db_url url;
|
||||
public abstract Db_engine New_clone(Db_url url);
|
||||
public Db_rdr New_rdr_by_obj(Object rdr, String sql) {
|
||||
Db_rdr__basic rv = (Db_rdr__basic)New_rdr_clone();
|
||||
rv.Ctor((ResultSet)rdr, sql);
|
||||
return rv;
|
||||
}
|
||||
public Db_conn_info Conn_info() {return conn_info;} protected Db_conn_info conn_info;
|
||||
public abstract Db_engine New_clone(Db_conn_info conn_info);
|
||||
public Db_rdr New_rdr__rls_manual(Object rdr_obj, String sql) {return New_rdr(null, rdr_obj, sql);}
|
||||
public Db_rdr New_rdr__rls_auto(Db_stmt stmt, Object rdr_obj, String sql) {return New_rdr(stmt, rdr_obj, sql);}
|
||||
@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;"));}
|
||||
@gplx.Virtual public void Txn_bgn(String name) {Exec_as_obj(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));}
|
||||
@gplx.Virtual public void Txn_end() {Exec_as_obj(Db_qry_sql.xtn_("COMMIT TRANSACTION;"));}
|
||||
@gplx.Virtual public void Txn_cxl() {Exec_as_obj(Db_qry_sql.xtn_("ROLLBACK TRANSACTION;"));}
|
||||
@gplx.Virtual public void Txn_sav() {this.Txn_end(); this.Txn_bgn("");}
|
||||
public Object Exec_as_obj(Db_qry qry) {
|
||||
if (qry.Tid() == Db_qry_.Tid_flush) return null; // ignore flush (delete-db) statements
|
||||
String sql = this.SqlWtr().Xto_str(qry, false); // DBG: Tfds.Write(sql);
|
||||
@ -42,7 +41,7 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
Statement cmd = New_stmt_exec(sql);
|
||||
return cmd.executeUpdate(sql);
|
||||
}
|
||||
catch (Exception exc) {throw Err_.err_(exc, "exec nonQuery failed").Add("sql", sql).Add("err", Err_.Message_gplx_brief(exc));}
|
||||
catch (Exception exc) {throw Err_.new_("db.engine:exec failed; url={0} sql={1} err={2}", conn_info.Xto_api(), sql, Err_.Message_gplx_brief(exc));}
|
||||
}
|
||||
private DataRdr Exec_as_rdr(String sql) {
|
||||
try {
|
||||
@ -51,31 +50,35 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
ResultSet rdr = cmd.getResultSet();
|
||||
return New_rdr(rdr, sql);
|
||||
}
|
||||
catch (Exception exc) {throw Err_.err_(exc, "exec reader failed").Add("sql", sql).Add("err", Err_.Message_gplx_brief(exc));}
|
||||
catch (Exception exc) {throw Err_.new_("db.engine:rdr failed; url={0} sql={1} err={2}", conn_info.Xto_api(), sql, Err_.Message_gplx_brief(exc));}
|
||||
}
|
||||
public void Exec_ddl_create_tbl(Db_meta_tbl meta) {Exec_as_int(meta.To_sql_create());}
|
||||
public void Exec_ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {
|
||||
public void Ddl_create_tbl(Db_meta_tbl tbl) {Exec_as_int(tbl.To_sql_create());}
|
||||
public void Ddl_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());
|
||||
usr_dlg.Plog_many("", "", "creating database index (please wait); db=~{0} idx=~{1}", conn_info.Database(), idx.Name());
|
||||
Exec_as_int(idx.To_sql_create());
|
||||
}
|
||||
}
|
||||
public void Exec_ddl_append_fld(String tbl, Db_meta_fld fld) {
|
||||
Exec_as_int(Db_sqlbldr__sqlite.I.Bld_alter_tbl_add(tbl, fld));
|
||||
}
|
||||
@gplx.Virtual public void Exec_env_db_attach(String alias, Io_url db_url) {}
|
||||
@gplx.Virtual public void Exec_env_db_detach(String alias) {}
|
||||
public void Ddl_append_fld(String tbl, Db_meta_fld fld) {Exec_as_int(Db_sqlbldr__sqlite.I.Bld_alter_tbl_add(tbl, fld));}
|
||||
public void Ddl_delete_tbl(String tbl) {Exec_as_int(Db_sqlbldr__sqlite.I.Bld_drop_tbl(tbl));}
|
||||
@gplx.Virtual public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
@gplx.Virtual public void Env_db_detach(String alias) {}
|
||||
@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();}
|
||||
private Db_rdr New_rdr(Db_stmt stmt, Object rdr, String sql) {
|
||||
Db_rdr__basic rv = (Db_rdr__basic)New_rdr_clone();
|
||||
rv.Ctor(stmt, (ResultSet)rdr, sql);
|
||||
return rv;
|
||||
}
|
||||
@gplx.Internal protected abstract Connection Conn_new();
|
||||
private Connection connection;
|
||||
protected Connection connection;
|
||||
public void Conn_open() {connection = Conn_new();}
|
||||
public void Conn_term() {
|
||||
if (connection == null) return; // connection never opened; just exit
|
||||
try {connection.close();}
|
||||
catch (Exception e) {throw Err_.err_(e, "Conn_term.fail; url={0} err={1}", url.Xto_raw(), Err_.Message_lang(e));}
|
||||
catch (Exception e) {throw Err_.err_(e, "Conn_term.fail; url={0} err={1}", conn_info.Xto_raw(), Err_.Message_lang(e));}
|
||||
connection = null;
|
||||
}
|
||||
public Object New_stmt_prep_as_obj(String sql) {
|
||||
@ -90,6 +93,6 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
}
|
||||
protected Connection Conn_make_by_url(String url, String uid, String pwd) {
|
||||
try {return DriverManager.getConnection(url, uid, pwd);}
|
||||
catch (SQLException e) {throw Err_.err_(e, "connection open failed").Add("ConnectInfo", Url().Xto_raw());}
|
||||
catch (SQLException e) {throw Err_.err_(e, "connection open failed").Add("ConnectInfo", Conn_info().Xto_raw());}
|
||||
}
|
||||
}
|
||||
|
@ -16,18 +16,18 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_url__mem extends Db_url__base {
|
||||
public class Db_conn_info__mem extends Db_conn_info__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mem";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__mem rv = new Db_url__mem();
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Db_conn_info__mem rv = new Db_conn_info__mem();
|
||||
rv.Ctor("", m.ReadStr("database"), raw, raw);
|
||||
return rv;
|
||||
}
|
||||
public static Db_url new_(String database) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
public static Db_conn_info new_(String database) {
|
||||
return Db_conn_info_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "database", database
|
||||
));
|
||||
}
|
||||
public static final Db_url__mem I = new Db_url__mem(); Db_url__mem() {}
|
||||
public static final Db_conn_info__mem I = new Db_conn_info__mem(); Db_conn_info__mem() {}
|
||||
}
|
@ -18,29 +18,33 @@ 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_();
|
||||
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 new Db_engine__mem(url);}
|
||||
Db_engine__mem(Db_conn_info conn_info) {this.conn_info = conn_info;}
|
||||
public String Tid() {return Db_conn_info__mem.Tid_const;}
|
||||
public Db_conn_info Conn_info() {return conn_info;} private Db_conn_info conn_info;
|
||||
public Db_engine New_clone(Db_conn_info conn_info) {return new Db_engine__mem(conn_info);}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt__mem(this, qry);}
|
||||
public Mem_tbl Tbls_get(String name) {return (Mem_tbl)tbl_hash.Fetch(name);}
|
||||
public void Txn_bgn() {++txn_count;} private int txn_count = 0;
|
||||
public void Txn_end() {--txn_count;}
|
||||
public void Txn_bgn(String name) {++txn_count;} private int txn_count = 0;
|
||||
public void Txn_end() {--txn_count;}
|
||||
public void Txn_cxl() {--txn_count;}
|
||||
public void Txn_sav() {this.Txn_end(); this.Txn_bgn("");}
|
||||
public Object Exec_as_obj(Db_qry qry) {throw Err_.not_implemented_();}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {
|
||||
if (txn_count != 0) throw Err_.new_("Conn_term.txns still open; txn_count={0}", txn_count);
|
||||
}
|
||||
public Db_rdr New_rdr_by_obj(Object rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public Db_rdr New_rdr__rls_manual(Object rdr_obj, String sql) {throw Err_.not_implemented_();}
|
||||
public Db_rdr New_rdr__rls_auto(Db_stmt stmt, Object rdr_obj, 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_ddl_create_tbl(Db_meta_tbl meta) {
|
||||
Mem_tbl mem_tbl = new Mem_tbl();
|
||||
public void Ddl_create_tbl(Db_meta_tbl meta) {
|
||||
Mem_tbl mem_tbl = new Mem_tbl(meta);
|
||||
tbl_hash.AddReplace(meta.Name(), mem_tbl);
|
||||
}
|
||||
public void Exec_ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {} // TODO: implement unique index
|
||||
public void Exec_ddl_append_fld(String tbl, Db_meta_fld fld) {}
|
||||
public void Exec_env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Exec_env_db_detach(String alias) {}
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {} // TODO: implement unique index
|
||||
public void Ddl_append_fld(String tbl, Db_meta_fld fld) {}
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
public static final Db_engine__mem _ = new Db_engine__mem(); Db_engine__mem() {}
|
||||
}
|
||||
|
@ -18,12 +18,13 @@ 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 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_();
|
||||
private final OrderedHash val_list = OrderedHash_.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 Object Args_get_at(int i) {return val_list.FetchAt(i);}
|
||||
public Object Args_get_by(String k) {return val_list.Fetch(k);}
|
||||
public Db_qry Qry() {return qry;} private Db_qry qry;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Clear() {
|
||||
@ -117,14 +118,15 @@ public class Db_stmt__mem implements Db_stmt {
|
||||
return tbl.Delete(this);
|
||||
}
|
||||
public DataRdr Exec_select() {throw Err_.not_implemented_();}
|
||||
public Db_rdr Exec_select_as_rdr() {
|
||||
public Db_rdr Exec_select__rls_auto() {return this.Exec_select__rls_manual();}
|
||||
public Db_rdr Exec_select__rls_manual() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Select(this);
|
||||
}
|
||||
}
|
||||
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 (k == Db_meta_fld.Key_null) return; // key is explicitly null; ignore; allows schema_2+ type definitions
|
||||
val_list.Add_if_new(k, v); // NOTE: only add if new; WHERE with IN will call Add many times; fld_ttl IN ('A.png', 'B.png');
|
||||
if (where) {
|
||||
ListAdp list = (ListAdp)crt_hash.Fetch(k);
|
||||
if (list == null) {
|
||||
|
@ -16,18 +16,33 @@ You should have received 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.dbs.qrys.*;
|
||||
import gplx.core.primitives.*; 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_();
|
||||
private final HashAdp autonum_hash = HashAdp_.new_();
|
||||
private final Db_meta_tbl meta;
|
||||
public Mem_tbl(Db_meta_tbl meta) {this.meta = meta;}
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
Db_qry_insert qry = (Db_qry_insert)stmt.Qry();
|
||||
String[] cols = qry.Cols_for_insert(); int len = cols.length;
|
||||
Mem_itm itm = new Mem_itm();
|
||||
for (int i = 0; i < len; ++i)
|
||||
itm.Set_by(cols[i], stmt.Args_get_at(i));
|
||||
Db_meta_fld[] flds = meta.Flds();
|
||||
int len = flds.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_meta_fld fld = flds[i];
|
||||
String fld_name = fld.Name();
|
||||
Object val = fld.Autonum() ? Autonum_calc(fld_name) : stmt.Args_get_by(fld_name);
|
||||
itm.Set_by(fld_name, val);
|
||||
}
|
||||
rows.Add(itm);
|
||||
return 1;
|
||||
}
|
||||
private int Autonum_calc(String name) {
|
||||
Int_obj_ref autonum_itm = (Int_obj_ref)autonum_hash.Fetch(name);
|
||||
if (autonum_itm == null) {
|
||||
autonum_itm = Int_obj_ref.new_(0);
|
||||
autonum_hash.Add(name, autonum_itm);
|
||||
}
|
||||
return autonum_itm.Val_add();
|
||||
}
|
||||
public int Update(Db_stmt__mem stmt) {
|
||||
Db_qry_update qry = (Db_qry_update)stmt.Qry();
|
||||
qry.Where().Val_from_args(stmt.Crts());
|
||||
|
@ -16,12 +16,12 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mysql; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Mysql_url extends Db_url__base {
|
||||
public class Mysql_conn_info extends Db_conn_info__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
|
||||
public static Db_conn_info new_(String server, String database, String uid, String pwd) {
|
||||
return Db_conn_info_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
@ -30,12 +30,12 @@ public class Mysql_url extends Db_url__base {
|
||||
, "charset", "utf8"
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Mysql_url rv = new Mysql_url();
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Mysql_conn_info rv = new Mysql_conn_info();
|
||||
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() {}
|
||||
public static final Mysql_conn_info _ = new Mysql_conn_info(); Mysql_conn_info() {}
|
||||
}
|
@ -19,17 +19,17 @@ package gplx.dbs.engines.mysql; import gplx.*; import gplx.dbs.*; import gplx.db
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.sqls.*;
|
||||
import java.sql.*;
|
||||
public class Mysql_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Mysql_url.Tid_const;}
|
||||
@Override public String Tid() {return Mysql_conn_info.Tid_const;}
|
||||
@Override public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_escape_backslash();}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
@Override public Db_engine New_clone(Db_conn_info connectInfo) {
|
||||
Mysql_engine rv = new Mysql_engine();
|
||||
rv.Ctor(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Mysql_rdr.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Mysql_url url_as_mysql = (Mysql_url)url;
|
||||
return Conn_make_by_url("jdbc:mysql://localhost/" + url_as_mysql.Database() + "?characterEncoding=UTF8", url_as_mysql.Uid(), url_as_mysql.Pwd());
|
||||
Mysql_conn_info conn_info_as_mysql = (Mysql_conn_info)conn_info;
|
||||
return Conn_make_by_url("jdbc:mysql://localhost/" + conn_info_as_mysql.Database() + "?characterEncoding=UTF8", conn_info_as_mysql.Uid(), conn_info_as_mysql.Pwd());
|
||||
}
|
||||
public static final Mysql_engine _ = new Mysql_engine(); Mysql_engine() {}
|
||||
}
|
||||
|
@ -16,8 +16,8 @@ You should have received 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 {
|
||||
public class Null_conn_info extends Db_conn_info__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", "");}
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {return this;}
|
||||
public static final Null_conn_info _ = new Null_conn_info(); Null_conn_info() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
}
|
@ -15,24 +15,28 @@ 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.*;
|
||||
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 String Tid() {return Null_conn_info.Tid_const;}
|
||||
public Db_conn_info Conn_info() {return Db_conn_info_.Null;}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {}
|
||||
public Db_engine New_clone(Db_url url) {return this;}
|
||||
public Db_rdr New_rdr_by_obj(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public Db_engine New_clone(Db_conn_info url) {return this;}
|
||||
public Db_rdr New_rdr__rls_manual (Object rdr_obj, String sql) {return Db_rdr_.Empty;}
|
||||
public Db_rdr New_rdr__rls_auto (Db_stmt stmt, Object rdr_obj, String sql) {return Db_rdr_.Empty;}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return Db_stmt_.Null;}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public void Txn_bgn(String name) {}
|
||||
public void Txn_end() {}
|
||||
public void Txn_cxl() {}
|
||||
public void Txn_sav() {}
|
||||
public Object Exec_as_obj(Db_qry cmd) {return cmd.Exec_is_rdr() ? (Object)DataRdr_.Null : -1;}
|
||||
public void Exec_ddl_create_tbl(Db_meta_tbl meta) {}
|
||||
public void Exec_ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {}
|
||||
public void Exec_ddl_append_fld(String tbl, Db_meta_fld fld) {}
|
||||
public void Exec_env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Exec_env_db_detach(String alias) {}
|
||||
public void Ddl_create_tbl(Db_meta_tbl meta) {}
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {}
|
||||
public void Ddl_append_fld(String tbl, Db_meta_fld fld) {}
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
public static final Null_engine _ = new Null_engine(); Null_engine() {}
|
||||
}
|
||||
|
@ -16,12 +16,12 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.postgres; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Postgres_url extends Db_url__base {
|
||||
public class Postgres_conn_info extends Db_conn_info__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
|
||||
public static Db_conn_info new_(String server, String database, String uid, String pwd) {
|
||||
return Db_conn_info_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
@ -31,12 +31,12 @@ public class Postgres_url extends Db_url__base {
|
||||
, "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();
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Postgres_conn_info rv = new Postgres_conn_info();
|
||||
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() {}
|
||||
public static final Postgres_conn_info _ = new Postgres_conn_info(); Postgres_conn_info() {}
|
||||
}
|
@ -19,17 +19,17 @@ package gplx.dbs.engines.postgres; import gplx.*; import gplx.dbs.*; import gplx
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.sqls.*;
|
||||
import java.sql.*;
|
||||
public class Postgres_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Postgres_url.Tid_const;}
|
||||
@Override public String Tid() {return Postgres_conn_info.Tid_const;}
|
||||
@Override public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_escape_backslash();}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
@Override public Db_engine New_clone(Db_conn_info connectInfo) {
|
||||
Postgres_engine rv = new Postgres_engine();
|
||||
rv.Ctor(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Db_data_rdr_.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
Postgres_url url_as_postgres = (Postgres_url)url;
|
||||
return Conn_make_by_url("jdbc:" + url_as_postgres.Tid() + "://localhost/" + url_as_postgres.Database(), url_as_postgres.Uid(), url_as_postgres.Pwd());
|
||||
Postgres_conn_info conn_info_as_postgres = (Postgres_conn_info)conn_info;
|
||||
return Conn_make_by_url("jdbc:" + conn_info_as_postgres.Tid() + "://localhost/" + conn_info_as_postgres.Database(), conn_info_as_postgres.Uid(), conn_info_as_postgres.Pwd());
|
||||
}
|
||||
public static final Postgres_engine _ = new Postgres_engine(); Postgres_engine() {}
|
||||
}
|
||||
|
@ -16,32 +16,32 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Sqlite_url extends Db_url__base {
|
||||
public class Sqlite_conn_info extends Db_conn_info__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) {
|
||||
Sqlite_url rv = new Sqlite_url();
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Sqlite_conn_info rv = new Sqlite_conn_info();
|
||||
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) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
public static Db_conn_info load_(Io_url url) {
|
||||
return Db_conn_info_.parse_(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
, "data source" , url.Xto_api()
|
||||
, "version" , "3"
|
||||
));
|
||||
}
|
||||
public static Db_url make_(Io_url url) {
|
||||
public static Db_conn_info make_(Io_url url) {
|
||||
Io_mgr._.CreateDirIfAbsent(url.OwnerDir());
|
||||
return Db_url_.parse_(Bld_raw
|
||||
return Db_conn_info_.parse_(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
, "data source" , url.Xto_api()
|
||||
, "version" , "3"
|
||||
|
||||
));
|
||||
}
|
||||
public static final Sqlite_url _ = new Sqlite_url(); Sqlite_url() {}
|
||||
public static final Sqlite_conn_info _ = new Sqlite_conn_info(); Sqlite_conn_info() {}
|
||||
}
|
@ -20,16 +20,24 @@ import java.sql.*;
|
||||
import gplx.stores.*; import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Sqlite_engine extends Db_engine_sql_base {
|
||||
@Override public String Tid() {return Sqlite_url.Tid_const;}
|
||||
@Override public Db_engine New_clone(Db_url connectInfo) {
|
||||
private final Sqlite_txn_mgr txn_mgr;
|
||||
Sqlite_engine() {
|
||||
this.txn_mgr = new Sqlite_txn_mgr(this);
|
||||
}
|
||||
@Override public String Tid() {return Sqlite_conn_info.Tid_const;}
|
||||
@Override public Db_engine New_clone(Db_conn_info connectInfo) {
|
||||
Sqlite_engine rv = new Sqlite_engine();
|
||||
rv.Ctor(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr New_rdr(ResultSet rdr, String commandText) {return Sqlite_rdr.new_(rdr, commandText);}
|
||||
@Override public Db_rdr New_rdr_clone() {return new Db_rdr__sqlite();}
|
||||
@Override public void Exec_env_db_attach(String alias, Io_url db_url) {Exec_as_int(String_.Format("ATTACH '{0}' AS {1};", db_url.Raw(), alias));}
|
||||
@Override public void Exec_env_db_detach(String alias) {Exec_as_int(String_.Format("DETACH {0};", alias));}
|
||||
@Override public void Env_db_attach(String alias, Io_url db_url) {Exec_as_int(String_.Format("ATTACH '{0}' AS {1};", db_url.Raw(), alias));}
|
||||
@Override public void Env_db_detach(String alias) {Exec_as_int(String_.Format("DETACH {0};", alias));}
|
||||
@Override public void Txn_bgn(String name) {txn_mgr.Txn_bgn(name);}
|
||||
@Override public void Txn_end() {txn_mgr.Txn_end();}
|
||||
@Override public void Txn_cxl() {txn_mgr.Txn_cxl();}
|
||||
@Override public void Txn_sav() {txn_mgr.Txn_sav();}
|
||||
static boolean loaded = false;
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
if (!loaded) {
|
||||
@ -39,24 +47,11 @@ public class Sqlite_engine extends Db_engine_sql_base {
|
||||
catch (ClassNotFoundException e) {throw Err_.new_("could not load sqlite jdbc driver");}
|
||||
loaded = true;
|
||||
}
|
||||
Sqlite_url url_as_sqlite = (Sqlite_url)url;
|
||||
return Conn_make_by_url("jdbc:sqlite://" + String_.Replace(url_as_sqlite.Url().Raw(), "\\", "/"), "", "");
|
||||
Sqlite_conn_info conn_info_as_sqlite = (Sqlite_conn_info)conn_info;
|
||||
Connection rv = Conn_make_by_url("jdbc:sqlite://" + String_.Replace(conn_info_as_sqlite.Url().Raw(), "\\", "/"), "", "");
|
||||
return rv;
|
||||
}
|
||||
private boolean pragma_needed = true;
|
||||
@Override public void Txn_bgn() {
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA ENCODING=\"UTF-8\";"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA journal_mode = OFF;")); // will cause out of memory
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA journal_mode = MEMORY;"));
|
||||
if (pragma_needed) {
|
||||
Exec_as_obj(Db_qry_sql.xtn_("PRAGMA synchronous = OFF;"));
|
||||
pragma_needed = false;
|
||||
}
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA temp_store = MEMORY;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA locking_mode = EXCLUSIVE;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA cache_size=4000;")); // too many will also cause out of memory
|
||||
Exec_as_obj(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));
|
||||
}
|
||||
public static final Sqlite_engine _ = new Sqlite_engine(); Sqlite_engine() {}
|
||||
public static final Sqlite_engine _ = new 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));}}
|
||||
|
@ -55,7 +55,6 @@ public class Sqlite_engine_ {
|
||||
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];
|
||||
@ -68,7 +67,6 @@ public class Sqlite_engine_ {
|
||||
public static void Idx_create(Db_conn p, Db_idx_itm... idxs) {Idx_create(Gfo_usr_dlg_.Null, p, "", idxs);}
|
||||
public static void Idx_create(Gfo_usr_dlg usr_dlg, Db_conn p, String file_id, Db_idx_itm... idxs) {
|
||||
int len = idxs.length;
|
||||
p.Txn_mgr().Txn_end_all(); // commit any pending transactions
|
||||
for (int i = 0; i < len; i++) {
|
||||
String index = idxs[i].Xto_sql();
|
||||
usr_dlg.Plog_many("", "", "creating index: ~{0} ~{1}", file_id, index);
|
||||
@ -79,7 +77,7 @@ public class Sqlite_engine_ {
|
||||
public static Db_conn Conn_load_or_make_(Io_url url, Bool_obj_ref created) {
|
||||
boolean exists = Io_mgr._.ExistsFil(url);
|
||||
created.Val_(!exists);
|
||||
Db_url connect = exists ? Sqlite_url.load_(url) : Sqlite_url.make_(url);
|
||||
Db_conn_info connect = exists ? Sqlite_conn_info.load_(url) : Sqlite_conn_info.make_(url);
|
||||
Db_conn p = Db_conn_pool.I.Get_or_new(connect);
|
||||
if (!exists)
|
||||
Pragma_page_size(p, 4096);
|
||||
|
70
140_dbs/src/gplx/dbs/engines/sqlite/Sqlite_txn_mgr.java
Normal file
70
140_dbs/src/gplx/dbs/engines/sqlite/Sqlite_txn_mgr.java
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Sqlite_txn_mgr {
|
||||
private final ListAdp txn_list = ListAdp_.new_();
|
||||
public Sqlite_txn_mgr(Db_engine engine) {this.engine = engine;} private final Db_engine engine;
|
||||
private boolean pragma_needed = Bool_.Y, txn_started = Bool_.N; // NOTE: txns only support 1 level; SQLite fails when nesting transactions; DATE:2015-03-11
|
||||
public void Txn_bgn(String name) {
|
||||
if (String_.Len_eq_0(name)) name = "unnamed";
|
||||
if (pragma_needed) {
|
||||
pragma_needed = false;
|
||||
engine.Exec_as_obj(Db_qry_sql.xtn_("PRAGMA synchronous = OFF;"));
|
||||
}
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA ENCODING=\"UTF-8\";"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA journal_mode = OFF;")); // will cause out of memory
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA journal_mode = MEMORY;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA temp_store = MEMORY;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA locking_mode = EXCLUSIVE;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA cache_size=4000;")); // too many will also cause out of memory
|
||||
if (txn_started) {
|
||||
engine.Exec_as_obj(Db_qry_sql.xtn_(String_.Format("SAVEPOINT {0};", name)));
|
||||
}
|
||||
else {
|
||||
txn_started = true;
|
||||
engine.Exec_as_obj(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));
|
||||
}
|
||||
txn_list.Add(name);
|
||||
}
|
||||
public void Txn_end() {
|
||||
if (txn_list.Count() == 0) {Gfo_usr_dlg_.I.Warn_many("", "", "no txns in stack;"); return;}
|
||||
String txn_last = (String)txn_list.PopLast();
|
||||
if (txn_list.Count() == 0) {// no txns left; commit it
|
||||
engine.Exec_as_obj(Db_qry_sql.xtn_("COMMIT TRANSACTION;"));
|
||||
txn_started = false;
|
||||
}
|
||||
else
|
||||
engine.Exec_as_obj(Db_qry_sql.xtn_(String_.Format("RELEASE SAVEPOINT {0};", txn_last)));
|
||||
}
|
||||
public void Txn_cxl() {
|
||||
if (txn_list.Count() == 0) {Gfo_usr_dlg_.I.Warn_many("", "", "no txns in stack;"); return;}
|
||||
String txn_last = (String)txn_list.PopLast();
|
||||
if (txn_list.Count() == 0) {// no txns left; rollback
|
||||
engine.Exec_as_obj(Db_qry_sql.xtn_("ROLLBACK TRANSACTION;"));
|
||||
txn_started = false;
|
||||
}
|
||||
else
|
||||
engine.Exec_as_obj(Db_qry_sql.xtn_(String_.Format("ROLBACK TRANSACTION TO SAVEPOINT {0};", txn_last)));
|
||||
}
|
||||
public void Txn_sav() {
|
||||
if (txn_list.Count() == 0) {Gfo_usr_dlg_.I.Warn_many("", "", "no txns in stack;"); return;}
|
||||
String name = (String)txn_list.FetchAt(txn_list.Count() - 1);
|
||||
this.Txn_end(); this.Txn_bgn(name);
|
||||
}
|
||||
}
|
@ -19,14 +19,14 @@ package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs
|
||||
import org.junit.*;
|
||||
public class TdbConnectInfo_tst {
|
||||
@Test public void Full() {
|
||||
Db_url connectInfo = Db_url_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb;format=dsv;");
|
||||
Db_conn_info connectInfo = Db_conn_info_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb;format=dsv;");
|
||||
tst_Parse(connectInfo, Io_url_.new_any_("C:\\dir\\xmpl.tdb"), "dsv");
|
||||
}
|
||||
@Test public void DefaultFormat() {
|
||||
Db_url connectInfo = Db_url_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb"); // dsv Format inferred
|
||||
Db_conn_info connectInfo = Db_conn_info_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb"); // dsv Format inferred
|
||||
tst_Parse(connectInfo, Io_url_.new_any_("C:\\dir\\xmpl.tdb"), "dsv");
|
||||
}
|
||||
void tst_Parse(Db_url connectInfo, Io_url url, String format) {
|
||||
Tfds.Eq(((Tdb_url)connectInfo).Url(), url);
|
||||
void tst_Parse(Db_conn_info connectInfo, Io_url url, String format) {
|
||||
Tfds.Eq(((Tdb_conn_info)connectInfo).Url(), url);
|
||||
}
|
||||
}
|
||||
|
@ -43,5 +43,5 @@ public class TdbDatabase {
|
||||
}
|
||||
int FileId_next = TdbFile.MainFileId + 1;
|
||||
int TableId_next = 1;
|
||||
// public static Io_url UrlOf(Db_url url) {return Io_url_.new_any_(url.ServerName());}
|
||||
// public static Io_url UrlOf(Db_conn_info url) {return Io_url_.new_any_(url.ServerName());}
|
||||
}
|
||||
|
@ -18,20 +18,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.engines.tdbs; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.dbs.qrys.*; import gplx.dbs.sqls.*;
|
||||
public class TdbEngine implements Db_engine {
|
||||
public String Tid() {return Tdb_url.Tid_const;}
|
||||
public Db_url Url() {return url;} private Db_url url;
|
||||
public String Tid() {return Tdb_conn_info.Tid_const;}
|
||||
public Db_conn_info Conn_info() {return conn_info;} private Db_conn_info conn_info;
|
||||
public TdbDatabase Db() {return db;} TdbDatabase db;
|
||||
public void Conn_open() {
|
||||
Tdb_url tdb_url = (Tdb_url)url;
|
||||
Tdb_conn_info tdb_url = (Tdb_conn_info)conn_info;
|
||||
String url_str = tdb_url.Server();
|
||||
db = loadMgr.LoadTbls(Io_url_.new_any_(url_str));
|
||||
}
|
||||
public void Conn_term() {}
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public Db_engine New_clone(Db_url url) {
|
||||
public void Txn_bgn(String name) {}
|
||||
public void Txn_end() {}
|
||||
public void Txn_cxl() {}
|
||||
public void Txn_sav() {}
|
||||
public Db_engine New_clone(Db_conn_info conn_info) {
|
||||
TdbEngine rv = new TdbEngine();
|
||||
rv.CtorTdbEngine(url);
|
||||
rv.CtorTdbEngine(conn_info);
|
||||
rv.Conn_open();
|
||||
return rv;
|
||||
}
|
||||
@ -41,7 +43,8 @@ public class TdbEngine implements Db_engine {
|
||||
}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt_sql().Parse(qry, Sql_qry_wtr_.I.Xto_str(qry, true));}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public Db_rdr New_rdr_by_obj(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public Db_rdr New_rdr__rls_manual(Object rdr_obj, String sql) {return Db_rdr_.Empty;}
|
||||
public Db_rdr New_rdr__rls_auto(Db_stmt stmt, Object rdr_obj, String sql) {return Db_rdr_.Empty;}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public TdbTable FetchTbl(String name) {
|
||||
TdbTable tbl = db.Tables().FetchOrFail(name);
|
||||
@ -54,16 +57,17 @@ public class TdbEngine implements Db_engine {
|
||||
public void FlushTbl(TdbTable tbl) {
|
||||
saveMgr.SaveFile(db, tbl.File());
|
||||
}
|
||||
public void Exec_ddl_create_tbl(Db_meta_tbl meta) {throw Err_.not_implemented_();}
|
||||
public void Exec_ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {throw Err_.not_implemented_();}
|
||||
public void Exec_ddl_append_fld(String tbl, Db_meta_fld fld) {throw Err_.not_implemented_();}
|
||||
public void Exec_env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Exec_env_db_detach(String alias) {}
|
||||
public void Ddl_create_tbl(Db_meta_tbl meta) {throw Err_.not_implemented_();}
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... ary) {throw Err_.not_implemented_();}
|
||||
public void Ddl_append_fld(String tbl, Db_meta_fld fld) {throw Err_.not_implemented_();}
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
|
||||
HashAdp wkrs = HashAdp_.new_(); TdbDbLoadMgr loadMgr = TdbDbLoadMgr.new_(); TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_();
|
||||
public static final TdbEngine _ = new TdbEngine();
|
||||
void CtorTdbEngine(Db_url url) {
|
||||
this.url = url;
|
||||
void CtorTdbEngine(Db_conn_info conn_info) {
|
||||
this.conn_info = conn_info;
|
||||
wkrs.Add(Db_qry_.Tid_select, TdbSelectWkr._);
|
||||
wkrs.Add(Db_qry_.Tid_insert, TdbInsertWkr.new_());
|
||||
wkrs.Add(Db_qry_.Tid_update, TdbUpdateWkr.new_());
|
||||
|
@ -81,7 +81,7 @@ public class TdbFlush_tst {
|
||||
}
|
||||
class TdbEngineFxt {
|
||||
public TdbEngine run_MakeEngine(Io_url url) {
|
||||
Db_url connectInfo = Db_url_.tdb_(url);
|
||||
Db_conn_info connectInfo = Db_conn_info_.tdb_(url);
|
||||
TdbEngine engine = (TdbEngine)TdbEngine._.New_clone(connectInfo);
|
||||
engine.Conn_open();
|
||||
return engine;
|
||||
|
@ -16,22 +16,22 @@ You should have received 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 class Tdb_conn_info extends Db_conn_info__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
|
||||
public static Db_conn_info new_(Io_url url) {
|
||||
return Db_conn_info_.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();
|
||||
} Tdb_conn_info() {}
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {
|
||||
Tdb_conn_info rv = new Tdb_conn_info();
|
||||
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();
|
||||
public static final Tdb_conn_info _ = new Tdb_conn_info();
|
||||
}
|
@ -114,7 +114,7 @@ public class Db_stmt_cmd implements Db_stmt {
|
||||
catch (Exception e) {
|
||||
this.Rls();
|
||||
Reset_stmt();
|
||||
throw Err_.err_(e, "db_stmt.insert: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));
|
||||
throw Err_.err_(e, "db_stmt.insert: url={0} sql={1} err={2}", engine.Conn_info(), sql, Err_.Message_gplx_brief(e));
|
||||
}
|
||||
}
|
||||
public int Exec_update() {
|
||||
@ -135,9 +135,12 @@ public class Db_stmt_cmd implements Db_stmt {
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = engine.New_rdr(stmt.executeQuery(), sql); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public Db_rdr Exec_select_as_rdr() {
|
||||
try {return engine.New_rdr_by_obj(stmt.executeQuery(), sql);} catch (Exception e) {throw Err_.err_(e, "select failed: sql={0}", sql);}
|
||||
}
|
||||
public Db_rdr Exec_select__rls_auto() {
|
||||
try {return engine.New_rdr__rls_auto(this, stmt.executeQuery(), sql);} catch (Exception e) {throw Err_.err_(e, "select failed: sql={0}", sql);}
|
||||
}
|
||||
public Db_rdr Exec_select__rls_manual() {
|
||||
try {return engine.New_rdr__rls_manual(stmt.executeQuery(), sql);} catch (Exception e) {throw Err_.err_(e, "select failed: sql={0}", sql);}
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry__select_cmd.Rdr_to_val(engine.New_rdr(stmt.executeQuery(), sql)); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
@ -153,6 +156,7 @@ public class Db_stmt_cmd implements Db_stmt {
|
||||
try {
|
||||
if (stmt.getConnection().isClosed()) return; // do not close stmt if connection is already closed; throws null error; DATE:2015-02-11
|
||||
stmt.close();
|
||||
stmt = null;
|
||||
}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to close command: {0}", sql);}
|
||||
}
|
||||
|
@ -112,7 +112,8 @@ public class Db_stmt_sql implements Db_stmt {// used for formatting SQL statemen
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = conn.Exec_qry_as_rdr(Db_qry_sql.rdr_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Db_rdr Exec_select_as_rdr() {throw Err_.not_implemented_();}
|
||||
public Db_rdr Exec_select__rls_auto() {return Db_rdr_.Empty;}
|
||||
public Db_rdr Exec_select__rls_manual() {return Db_rdr_.Empty;}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry__select_cmd.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
|
@ -58,6 +58,9 @@ public class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
tmp_bfr.Add_byte_semic();
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
public String Bld_drop_tbl(String tbl) {
|
||||
return String_.Format("DROP TABLE IF EXISTS {0};", tbl);
|
||||
}
|
||||
private void Bld_fld(Bry_bfr tmp_bfr, Db_meta_fld fld) {
|
||||
tmp_bfr.Add_str_ascii(fld.Name()).Add_byte_space();
|
||||
Tid_to_sql(tmp_bfr, fld.Tid(), fld.Len()); tmp_bfr.Add_byte_space();
|
||||
@ -74,7 +77,7 @@ public class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
tmp_bfr.Add_byte_space();
|
||||
}
|
||||
if (fld.Primary()) tmp_bfr.Add_str_ascii("PRIMARY KEY ");
|
||||
if (fld.Autoincrement()) tmp_bfr.Add_str_ascii("AUTOINCREMENT ");
|
||||
if (fld.Autonum()) tmp_bfr.Add_str_ascii("AUTOINCREMENT ");
|
||||
tmp_bfr.Del_by_1(); // remove trailing space
|
||||
}
|
||||
public static void Tid_to_sql(Bry_bfr tmp_bfr, int tid, int len) {// REF: https://www.sqlite.org/datatype3.html
|
||||
|
@ -17,21 +17,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.utls; import gplx.*; import gplx.dbs.*;
|
||||
public abstract class Db_in_wkr__base {
|
||||
protected abstract int Interval();
|
||||
protected abstract Db_qry Make_qry (Object db_ctx, int bgn, int end);
|
||||
@gplx.Virtual protected int Interval() {return gplx.dbs.engines.sqlite.Sqlite_engine_.Stmt_arg_max - 10;} // -10 for safety's sake
|
||||
protected abstract Db_qry Make_qry (int bgn, int end);
|
||||
protected abstract void Fill_stmt (Db_stmt stmt, int bgn, int end);
|
||||
protected abstract void Read_data (Cancelable cancelable, Object db_ctx, Db_rdr rdr);
|
||||
public void Select_in(Cancelable cancelable, Object db_ctx, Db_conn conn, int full_bgn, int full_end) {
|
||||
Db_rdr rdr = Db_rdr_.Null; Db_stmt stmt = Db_stmt_.Null;
|
||||
protected abstract void Read_data (Cancelable cancelable, Db_rdr rdr);
|
||||
@gplx.Virtual protected boolean Show_progress() {return false;}
|
||||
public void Select_in(Cancelable cancelable, Db_conn conn, int full_bgn, int full_end) {
|
||||
int part_len = Interval();
|
||||
Gfo_usr_dlg usr_dlg = Gfo_usr_dlg_.I;
|
||||
boolean show_progress = this.Show_progress();
|
||||
for (int part_bgn = full_bgn; part_bgn < full_end; part_bgn += part_len) {
|
||||
int part_end = part_bgn + part_len;
|
||||
if (part_end > full_end) part_end = full_end;
|
||||
Db_stmt stmt = Db_stmt_.Null; Db_rdr rdr = Db_rdr_.Empty;
|
||||
try {
|
||||
stmt = conn.Stmt_new(Make_qry(db_ctx, part_bgn, part_end));
|
||||
if (show_progress) usr_dlg.Prog_many("", "", "reading: count=~{0}", part_end);
|
||||
stmt = conn.Stmt_new(Make_qry(part_bgn, part_end));
|
||||
Fill_stmt(stmt, part_bgn, part_end);
|
||||
rdr = stmt.Exec_select_as_rdr();
|
||||
Read_data(cancelable, db_ctx, rdr);
|
||||
rdr = stmt.Exec_select__rls_manual();
|
||||
Read_data(cancelable, rdr);
|
||||
}
|
||||
finally {rdr.Rls(); stmt.Rls();}
|
||||
}
|
||||
|
@ -1,26 +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.utls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.primitives.*;
|
||||
class Db_sys_id_mgr {
|
||||
private Db_sys_regy_mgr db_regy;
|
||||
public Db_sys_id_mgr(Db_sys_regy_mgr db_regy) {this.db_regy = db_regy;}
|
||||
public int Get_next(String key) {return db_regy.Get_val_as_int_or(Grp_key, key, 1);}
|
||||
public void Set_next(String key, int v) {db_regy.Set(Grp_key, key, Int_.Xto_str(v));}
|
||||
private static final String Grp_key = "gplx.next_id";
|
||||
}
|
@ -1,47 +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.utls; import gplx.*; import gplx.dbs.*;
|
||||
class Db_sys_regy_mgr {
|
||||
private Db_sys_regy_tbl tbl;
|
||||
public Db_sys_regy_mgr(Db_url url, String name, boolean create) {
|
||||
tbl = new Db_sys_regy_tbl(url, name);
|
||||
if (create) tbl.Create_table();
|
||||
}
|
||||
public void Set(String grp, String key, String val) {
|
||||
if (tbl.Select_val_or(grp, key, null) == null)
|
||||
tbl.Insert(grp, key, val);
|
||||
else
|
||||
tbl.Update(grp, key, val);
|
||||
}
|
||||
public void Del(String grp, String key) {
|
||||
tbl.Delete(grp, key);
|
||||
}
|
||||
public String Get_val_as_str_or(String grp, String key, String or) {
|
||||
return tbl.Select_val_or(grp, key, or);
|
||||
}
|
||||
public int Get_val_as_int_or(String grp, String key, int or) {
|
||||
String rv = tbl.Select_val_or(grp, key, null);
|
||||
return rv == null ? or : Int_.parse_or_(rv, or);
|
||||
}
|
||||
}
|
||||
class Db_sys_regy_itm {
|
||||
public Db_sys_regy_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private final String grp;
|
||||
public String Key() {return key;} private final String key;
|
||||
public String Val() {return val;} private final String val;
|
||||
}
|
@ -1,54 +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.utls; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Db_sys_regy_mgr_tst {
|
||||
@Before public void init() {fxt.Init();} private Db_sys_regy_mgr_fxt fxt = new Db_sys_regy_mgr_fxt();
|
||||
@Test public void Insert() {
|
||||
fxt .Exec_set("grp", "key", "val_0")
|
||||
.Test_get("grp", "key", "val_0");
|
||||
}
|
||||
@Test public void Update() {
|
||||
fxt .Exec_set("grp", "key", "val_0")
|
||||
.Exec_set("grp", "key", "val_1")
|
||||
.Test_get("grp", "key", "val_1");
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt .Exec_set("grp", "key_0", "val_0")
|
||||
.Exec_set("grp", "key_1", "val_1")
|
||||
.Exec_del("grp", "key_1")
|
||||
.Test_get("grp", "key_0", "val_0")
|
||||
.Test_get("grp", "key_1", null)
|
||||
;
|
||||
}
|
||||
}
|
||||
class Db_sys_regy_mgr_fxt {
|
||||
private Db_sys_regy_mgr sys_regy_mgr;
|
||||
public void Init() {
|
||||
if (sys_regy_mgr == null) {
|
||||
Db_conn_pool.I.Get_or_new__mem("test_db");
|
||||
sys_regy_mgr = new Db_sys_regy_mgr(Db_url_.mem_("test_db"), "test_regy", true);
|
||||
}
|
||||
}
|
||||
public Db_sys_regy_mgr_fxt Exec_set(String grp, String key, String val) {sys_regy_mgr.Set(grp, key, val); return this;}
|
||||
public Db_sys_regy_mgr_fxt Exec_del(String grp, String key) {sys_regy_mgr.Del(grp, key); return this;}
|
||||
public Db_sys_regy_mgr_fxt Test_get(String grp, String key, String expd_val) {
|
||||
Tfds.Eq(expd_val, sys_regy_mgr.Get_val_as_str_or(grp, key, null));
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,60 +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.utls; import gplx.*; import gplx.dbs.*;
|
||||
class Db_sys_regy_tbl {
|
||||
private final String tbl_name;
|
||||
private static final Db_meta_fld_list Flds = Db_meta_fld_list.new_();
|
||||
private static final String
|
||||
Fld_regy_grp = Flds.Add_str("regy_grp", 1024)
|
||||
, Fld_regy_key = Flds.Add_str("regy_key", 1024)
|
||||
, Fld_regy_val = Flds.Add_str("regy_val", 4096)
|
||||
;
|
||||
public static Db_meta_tbl new_meta(String tbl) {
|
||||
return Db_meta_tbl.new_(tbl, Flds.To_fld_ary()
|
||||
, Db_meta_idx.new_unique_by_tbl(tbl, "key", Flds.To_str_ary())
|
||||
);
|
||||
}
|
||||
private Db_meta_tbl meta;
|
||||
private Db_conn conn;
|
||||
public Db_sys_regy_tbl(Db_url url, String tbl_name) {
|
||||
this.tbl_name = tbl_name;
|
||||
this.meta = Db_sys_regy_tbl.new_meta(tbl_name);
|
||||
this.conn = Db_conn_pool.I.Get_or_new(url);
|
||||
}
|
||||
public void Create_table() {conn.Exec_create_tbl_and_idx(meta);}
|
||||
public void Insert(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.Stmt_insert(tbl_name, Flds.To_str_ary());
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Val_str(val).Exec_insert();
|
||||
}
|
||||
public void Update(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.Stmt_update(tbl_name, String_.Ary(Fld_regy_grp, Fld_regy_key), Fld_regy_val);
|
||||
stmt.Clear().Val_str(val).Crt_str(Fld_regy_grp, grp).Crt_str(Fld_regy_key, key).Exec_update();
|
||||
}
|
||||
public void Delete(String grp, String key) {
|
||||
Db_stmt stmt = conn.Stmt_delete(tbl_name, Fld_regy_grp, Fld_regy_key);
|
||||
stmt.Clear().Crt_str(Fld_regy_grp, grp).Crt_str(Fld_regy_key, key).Exec_delete();
|
||||
}
|
||||
public String Select_val_or(String grp, String key, String or) {
|
||||
Db_stmt stmt = conn.Stmt_select(tbl_name, Flds.To_str_ary(), Fld_regy_grp, Fld_regy_key);
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
try {
|
||||
rdr = stmt.Clear().Crt_str(Fld_regy_grp, grp).Crt_str(Fld_regy_key, key).Exec_select_as_rdr();
|
||||
return rdr.Move_next() ? rdr.Read_str(Fld_regy_val) : or;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ package gplx.dbs.utls; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class PoolIds_tst {
|
||||
@Before public void setup() {
|
||||
conn = Db_conn_pool.I.Get_or_new(Db_url_.Test);
|
||||
conn = Db_conn_pool.I.Get_or_new(Db_conn_info_.Test);
|
||||
Db_qry_fxt.DeleteAll(conn, PoolIds.Tbl_Name);
|
||||
mgr = PoolIds._;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.stores; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.dbs.*; /*Db_url*/
|
||||
import gplx.dbs.*; /*Db_conn_info*/
|
||||
public class DbMaprMgr_tst {
|
||||
@Before public void setup() {
|
||||
mgr = DbMaprMgr.new_().RootIndexFlds_(DbMaprArg.new_("id", "disc_id"))
|
||||
@ -37,9 +37,9 @@ public class DbMaprMgr_tst {
|
||||
. Flds_add(MockStream.id_idk, "stream_id").Flds_add(MockStream.name_idk, "stream_name")
|
||||
. ConstantFlds_add("stream_type", 1)
|
||||
)));
|
||||
wtr = DbMaprWtr.new_by_url_(Db_url_.Test);
|
||||
wtr = DbMaprWtr.new_by_url_(Db_conn_info_.Test);
|
||||
wtr.EnvVars().Add(DbMaprWtr.Key_Mgr, mgr);
|
||||
conn = Db_conn_pool.I.Get_or_new(Db_url_.Test);
|
||||
conn = Db_conn_pool.I.Get_or_new(Db_conn_info_.Test);
|
||||
Db_qry_fxt.DeleteAll(conn, "mock_discs", "mock_titles", "mock_chapters", "mock_streams");
|
||||
} DbMaprMgr mgr; DbMaprWtr wtr; Db_conn conn; MockDisc disc; MockTitle title; MockChapter chapter; MockStream audio, subtitle; SrlMgr rdr;
|
||||
@Test public void PurgeObjTree() {
|
||||
@ -133,7 +133,7 @@ public class DbMaprMgr_tst {
|
||||
Tfds.Eq("subtitle1", ((MockStream)t.Subtitles().FetchAt(0)).Name());
|
||||
}
|
||||
DbMaprRdr rdr_() {
|
||||
DbMaprRdr rv = DbMaprRdr.new_(Db_url_.Test, Db_crt_.eq_("disc_id", 1));
|
||||
DbMaprRdr rv = DbMaprRdr.new_(Db_conn_info_.Test, Db_crt_.eq_("disc_id", 1));
|
||||
rv.EnvVars().Add(DbMaprWtr.Key_Mgr, mgr);
|
||||
return rv;
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ public class DbMaprRdr extends DataRdr_base implements SrlMgr {
|
||||
HashAdp tables = HashAdp_.new_();
|
||||
Db_conn conn; Criteria rootCrt;
|
||||
DbMaprMgr mgr; ListAdp rowStack = ListAdp_.new_();
|
||||
public static DbMaprRdr new_(Db_url dbInfo, Criteria rootCrt) {
|
||||
public static DbMaprRdr new_(Db_conn_info dbInfo, Criteria rootCrt) {
|
||||
DbMaprRdr rv = new DbMaprRdr();
|
||||
rv.conn = Db_conn_pool.I.Get_or_new(dbInfo); rv.rootCrt = rootCrt;
|
||||
return rv;
|
||||
|
@ -84,7 +84,7 @@ public class DbMaprWtr extends DataWtr_base implements DataWtr {
|
||||
public String XtoStr() {return "";}
|
||||
@Override public SrlMgr SrlMgr_new(Object o) {return new DbMaprWtr();}
|
||||
DbMaprMgr mgr; Db_conn conn; String curTableName; Db_qry_insert insertCmd;
|
||||
public static DbMaprWtr new_by_url_(Db_url url) {
|
||||
public static DbMaprWtr new_by_url_(Db_conn_info url) {
|
||||
DbMaprWtr rv = new DbMaprWtr();
|
||||
rv.conn = Db_conn_pool.I.Get_or_new(url);
|
||||
return rv;
|
||||
|
@ -44,11 +44,11 @@ public class Db_conn_fxt implements RlsAble {
|
||||
Tfds.Eq_ary(actlValAry, expdValAry);
|
||||
return record;
|
||||
}
|
||||
public void Rls() {conn.Conn_term();}
|
||||
public void Rls() {conn.Rls_conn();}
|
||||
|
||||
public static Db_conn Mysql() {return Db_conn_pool.I.Get_or_new(Mysql_url.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
|
||||
public static Db_conn Tdb(String fileName) {return Db_conn_pool.I.Get_or_new(Db_url_.tdb_(Tfds.RscDir.GenSubDir_nest("140_dbs", "tdbs").GenSubFil(fileName)));}
|
||||
public static Db_conn Postgres() {return Db_conn_pool.I.Get_or_new(Postgres_url.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
|
||||
public static Db_conn Sqlite() {return Db_conn_pool.I.Get_or_new(Sqlite_url.load_(Tfds.RscDir.GenSubFil_nest("140_dbs", "sqlite", "unit_tests.db")));}
|
||||
public static Db_conn Mysql() {return Db_conn_pool.I.Get_or_new(Mysql_conn_info.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
|
||||
public static Db_conn Tdb(String fileName) {return Db_conn_pool.I.Get_or_new(Db_conn_info_.tdb_(Tfds.RscDir.GenSubDir_nest("140_dbs", "tdbs").GenSubFil(fileName)));}
|
||||
public static Db_conn Postgres() {return Db_conn_pool.I.Get_or_new(Postgres_conn_info.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
|
||||
public static Db_conn Sqlite() {return Db_conn_pool.I.Get_or_new(Sqlite_conn_info.load_(Tfds.RscDir.GenSubFil_nest("140_dbs", "sqlite", "unit_tests.db")));}
|
||||
public static final boolean SkipPostgres = Tfds.SkipDb || true;
|
||||
}
|
@ -51,12 +51,12 @@ INSERT INTO dbs_multiple_data_types VALUES (1, 'John Doe', B'1', '3/30/2006 10:2
|
||||
class DataTypes_base_fxt {
|
||||
public Db_conn Conn() {return conn;} Db_conn conn;
|
||||
public DataTypes_base_fxt() {}
|
||||
public void Rls() {conn.Conn_term();}
|
||||
public void Rls() {conn.Rls_conn();}
|
||||
public String Select_FloatStr() {return select_FloatStr;} public DataTypes_base_fxt Select_FloatStr_(String v) {select_FloatStr = v; return this;} private String select_FloatStr;
|
||||
public void RunAll(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
this.Select_hook(select_FloatStr);
|
||||
conn.Conn_term();
|
||||
conn.Rls_conn();
|
||||
}
|
||||
public void Select_hook(String floatStr) {
|
||||
DataRdr rdr = Db_qry_.select_tbl_("dbs_multiple_data_types").Exec_qry_as_rdr(conn);
|
||||
|
@ -25,7 +25,7 @@ public abstract class GroupBys_base_tst {
|
||||
Db_qry_.delete_tbl_("dbs_group_bys").Exec_qry(conn);
|
||||
}
|
||||
@After public void teardown() {
|
||||
conn.Conn_term();
|
||||
conn.Rls_conn();
|
||||
}
|
||||
protected void GroupBy_1fld_hook() {
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
|
@ -26,7 +26,7 @@ public abstract class InsertIntos_base_tst {
|
||||
conn.Exec_qry(Db_qry_delete.new_("dbs_insert_intos"));
|
||||
}
|
||||
@After public void teardown() {
|
||||
conn.Conn_term();
|
||||
conn.Rls_conn();
|
||||
}
|
||||
protected void Select_hook() {
|
||||
conn.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
|
@ -25,7 +25,7 @@ public abstract class Joins_base_tst {
|
||||
Db_qry_delete.new_("dbs_join1").Exec_qry(conn);
|
||||
}
|
||||
@After public void teardown() {
|
||||
conn.Conn_term();
|
||||
conn.Rls_conn();
|
||||
}
|
||||
protected void InnerJoin_hook() {
|
||||
conn.Exec_qry(new Db_qry_insert("dbs_crud_ops").Arg_("id", 0).Arg_("name", "me"));
|
||||
|
@ -23,7 +23,7 @@ public abstract class OrderBys_base_tst {
|
||||
fx.Conn_(conn);
|
||||
Db_qry_delete.new_("dbs_crud_ops").Exec_qry(conn);
|
||||
} protected Db_conn_fxt fx = new Db_conn_fxt();
|
||||
@After public void teardown() {conn.Conn_term();}
|
||||
@After public void teardown() {conn.Rls_conn();}
|
||||
protected abstract Db_conn provider_(); protected Db_conn conn;
|
||||
protected void Basic_hook() {
|
||||
fx.tst_ExecDml(1, new Db_qry_insert("dbs_crud_ops").Arg_("id", 1).Arg_("name", "you"));
|
||||
|
@ -81,7 +81,7 @@ public class SqliteDbMain {
|
||||
}
|
||||
long time_elapsed = (Env_.TickCount() - time_bgn);
|
||||
// provider.Txn_mgr().Txn_end();
|
||||
provider.Conn_term();
|
||||
provider.Rls_conn();
|
||||
Tfds.Write(time_elapsed, number / time_elapsed);
|
||||
// 250; 260
|
||||
Tfds.Write("");
|
||||
|
@ -194,7 +194,7 @@ public class Swt_kit implements Gfui_kit {
|
||||
return rv;
|
||||
}
|
||||
catch (Exception e) {
|
||||
Gfo_usr_dlg_._.Warn_many("", "", "error while calculating font height; err=~{0}", Err_.Message_gplx_brief(e));
|
||||
Gfo_usr_dlg_.I.Warn_many("", "", "error while calculating font height; err=~{0}", Err_.Message_gplx_brief(e));
|
||||
return 8;
|
||||
}
|
||||
}
|
||||
|
@ -18,10 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.core.enums; import gplx.*; import gplx.core.*;
|
||||
class Gfo_enum_grp {
|
||||
// private OrderedHash itms = OrderedHash_.new_();
|
||||
public Gfo_enum_grp(UuidAdp uid, String key, int id, String name, int sort, String xtn) {
|
||||
public Gfo_enum_grp(Guid_adp uid, String key, int id, String name, int sort, String xtn) {
|
||||
this.uid = uid; this.key = key; this.id = id; this.name = name; this.sort = sort; this.xtn = xtn;
|
||||
}
|
||||
public UuidAdp Uid() {return uid;} private UuidAdp uid;
|
||||
public Guid_adp Uid() {return uid;} private Guid_adp uid;
|
||||
public String Key() {return key;} private String key;
|
||||
public int Id() {return id;} private int id;
|
||||
public String Name() {return name;} private String name;
|
||||
@ -29,10 +29,10 @@ class Gfo_enum_grp {
|
||||
public String Xtn() {return xtn;} private String xtn;
|
||||
}
|
||||
class Gfo_enum_itm {
|
||||
public Gfo_enum_itm(UuidAdp uid, String key, int id, String name, int sort, String xtn) {
|
||||
public Gfo_enum_itm(Guid_adp uid, String key, int id, String name, int sort, String xtn) {
|
||||
this.uid = uid; this.key = key; this.id = id; this.name = name; this.sort = sort; this.xtn = xtn;
|
||||
}
|
||||
public UuidAdp Uid() {return uid;} private UuidAdp uid;
|
||||
public Guid_adp Uid() {return uid;} private Guid_adp uid;
|
||||
public String Key() {return key;} private String key;
|
||||
public int Id() {return id;} private int id;
|
||||
public String Name() {return name;} private String name;
|
||||
|
54
400_xowa/src/gplx/dbs/Db_attach_cmd.java
Normal file
54
400_xowa/src/gplx/dbs/Db_attach_cmd.java
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.sqlite.*;
|
||||
public class Db_attach_cmd {
|
||||
private final boolean diff_db;
|
||||
private final Db_conn conn; private final String attach_name; private final Io_url attach_url;
|
||||
private final ListAdp sql_list = ListAdp_.new_();
|
||||
Db_attach_cmd(Db_conn conn, String attach_name, Io_url attach_url) {
|
||||
this.conn = conn; this.attach_name = attach_name; this.attach_url = attach_url;
|
||||
Sqlite_conn_info conn_info = (Sqlite_conn_info)conn.Conn_info();
|
||||
this.diff_db = !String_.Eq(conn_info.Url().Raw(), attach_url.Raw());
|
||||
}
|
||||
public Db_attach_cmd Add_fmt(String msg, String sql_fmt, Object... sql_args) {
|
||||
String sql = String_.Format(sql_fmt, sql_args);
|
||||
sql = String_.Replace(sql, "<attach_db>", diff_db ? attach_name + "." : ""); // replace <attach> with either "attach_db." or "";
|
||||
sql_list.Add(new Db_exec_sql_by_attach_itm(msg, sql));
|
||||
return this;
|
||||
}
|
||||
public void Exec() {
|
||||
Gfo_usr_dlg usr_dlg = Gfo_usr_dlg_.I;
|
||||
if (diff_db) conn.Env_db_attach(attach_name, attach_url);
|
||||
conn.Txn_bgn(attach_name); // NOTE: BEGIN TRAN must occur after ATTACH else sqlite will throw error
|
||||
int len = sql_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_exec_sql_by_attach_itm itm = (Db_exec_sql_by_attach_itm)sql_list.FetchAt(i);
|
||||
usr_dlg.Plog_many("", "", itm.Msg());
|
||||
conn.Exec_sql(itm.Sql());
|
||||
}
|
||||
conn.Txn_end();
|
||||
if (diff_db) conn.Env_db_detach(attach_name);
|
||||
}
|
||||
public static Db_attach_cmd new_(Db_conn conn, String attach_name, Io_url attach_url) {return new Db_attach_cmd(conn, attach_name, attach_url);}
|
||||
}
|
||||
class Db_exec_sql_by_attach_itm {
|
||||
public Db_exec_sql_by_attach_itm(String msg, String sql) {this.msg = msg; this.sql = sql;}
|
||||
public String Msg() {return msg;} private final String msg;
|
||||
public String Sql() {return sql;} private final String sql;
|
||||
}
|
35
400_xowa/src/gplx/dbs/Db_attach_rdr.java
Normal file
35
400_xowa/src/gplx/dbs/Db_attach_rdr.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; import gplx.*;
|
||||
import gplx.dbs.engines.sqlite.*;
|
||||
public class Db_attach_rdr {
|
||||
private final boolean diff_db;
|
||||
private final Db_conn conn; private final String attach_name; private final Io_url attach_url;
|
||||
public Db_attach_rdr(Db_conn conn, String attach_name, Io_url attach_url) {
|
||||
this.conn = conn; this.attach_name = attach_name; this.attach_url = attach_url;
|
||||
Sqlite_conn_info conn_info = (Sqlite_conn_info)conn.Conn_info();
|
||||
this.diff_db = !String_.Eq(conn_info.Url().Raw(), attach_url.Raw());
|
||||
}
|
||||
public Db_rdr Exec_as_rdr(String sql) {
|
||||
if (diff_db) conn.Env_db_attach(attach_name, attach_url);
|
||||
return conn.Exec_sql_as_rdr2(sql);
|
||||
}
|
||||
public void Rls() {
|
||||
if (diff_db) conn.Env_db_detach(attach_name);
|
||||
}
|
||||
}
|
@ -1,63 +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.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_cfg_grp {
|
||||
private OrderedHash itms = OrderedHash_.new_();
|
||||
public Db_cfg_grp(String grp) {this.grp = grp;}
|
||||
public String Grp() {return grp;} private String grp;
|
||||
public void Insert(String key, String val) {
|
||||
if (itms.Has(key)) throw Err_.new_fmt_("cfg_grp.Insert failed; key={0}", key);
|
||||
Fsm_cfg_itm itm = new Fsm_cfg_itm(grp, key, val);
|
||||
itms.Add(key, itm);
|
||||
}
|
||||
public void Update(String key, String val) {
|
||||
Fsm_cfg_itm itm = (Fsm_cfg_itm)itms.Fetch(key);
|
||||
if (itm == null) throw Err_.new_fmt_("cfg_grp.Update failed; key={0}", key);
|
||||
itm.Val_(val);
|
||||
}
|
||||
public void Upsert(String key, String val) {
|
||||
Fsm_cfg_itm itm = (Fsm_cfg_itm)itms.Fetch(key);
|
||||
if (itm == null) {
|
||||
itm = new Fsm_cfg_itm(grp, key, val);
|
||||
itms.Add(key, itm);
|
||||
}
|
||||
else
|
||||
itm.Val_(val);
|
||||
}
|
||||
public boolean Get_yn_or_y(String key) {return Get_yn_or(key, Bool_.Y);}
|
||||
public boolean Get_yn_or_n(String key) {return Get_yn_or(key, Bool_.N);}
|
||||
public boolean Get_yn_or(String key, boolean or) {
|
||||
String rv = Get_str_or(key, null);
|
||||
return rv == null ? or : Yn.parse_(rv);
|
||||
}
|
||||
public int Get_int_or(String key, int or) {
|
||||
String rv = Get_str_or(key, null);
|
||||
return rv == null ? or : Int_.parse_(rv);
|
||||
}
|
||||
public String Get_str_or(String key, String or) {
|
||||
Fsm_cfg_itm itm = (Fsm_cfg_itm)itms.Fetch(key);
|
||||
return itm == null ? or : itm.Val();
|
||||
}
|
||||
public static final Db_cfg_grp Null = new Db_cfg_grp(); Db_cfg_grp() {}
|
||||
}
|
||||
class Fsm_cfg_itm {
|
||||
public Fsm_cfg_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private String grp;
|
||||
public String Key() {return key;} private String key;
|
||||
public String Val() {return val;} public Fsm_cfg_itm Val_(String v) {val = v; return this;} private String val;
|
||||
}
|
34
400_xowa/src/gplx/dbs/cfgs/Db_cfg_hash.java
Normal file
34
400_xowa/src/gplx/dbs/cfgs/Db_cfg_hash.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.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_cfg_hash {
|
||||
private final String grp; private final OrderedHash hash = OrderedHash_.new_();
|
||||
public Db_cfg_hash(String grp) {this.grp = grp;}
|
||||
public int Len() {return hash.Count();}
|
||||
public Db_cfg_itm Get_at(int i) {return (Db_cfg_itm)hash.FetchAt(i);}
|
||||
public Db_cfg_itm Get(String key) {
|
||||
Db_cfg_itm rv = (Db_cfg_itm)hash.Fetch(key);
|
||||
return rv == null ? Db_cfg_itm.Empty : rv;
|
||||
}
|
||||
public void Set(String key, String val) {hash.Del(key); Add(key, val);}
|
||||
public void Add(String key, String val) {
|
||||
if (hash.Has(key)) throw Err_.new_fmt_("itm exists; grp={0} key={01}", grp, key);
|
||||
Db_cfg_itm itm = new Db_cfg_itm(grp, key, val);
|
||||
hash.Add(key, itm);
|
||||
}
|
||||
}
|
59
400_xowa/src/gplx/dbs/cfgs/Db_cfg_itm.java
Normal file
59
400_xowa/src/gplx/dbs/cfgs/Db_cfg_itm.java
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
public class Db_cfg_itm {
|
||||
public Db_cfg_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private final String grp;
|
||||
public String Key() {return key;} private final String key;
|
||||
public String Val() {return val;} public Db_cfg_itm Val_(String v) {val = v; return this;} private String val;
|
||||
public String To_str_or(String or) {return val == null ? or : val;}
|
||||
public byte[] To_bry_or(byte[] or) {try {return val == null ? or : Bry_.new_utf8_(val) ;} catch (Exception e) {throw err_parse(e, Bry_.Cls_val_name);}}
|
||||
public int To_int_or(int or) {try {return val == null ? or : Int_.parse_or_(val, or) ;} catch (Exception e) {throw err_parse(e, Int_.Cls_val_name);}}
|
||||
public long To_long_or(long or) {try {return val == null ? or : Long_.parse_or_(val, or) ;} catch (Exception e) {throw err_parse(e, Long_.Cls_val_name);}}
|
||||
public byte To_byte_or(byte or) {try {return val == null ? or : Byte_.parse_or_(val, or) ;} catch (Exception e) {throw err_parse(e, Byte_.Cls_val_name);}}
|
||||
public boolean To_yn_or_n() {return To_yn_or(Bool_.N);}
|
||||
public boolean To_yn_or(boolean or) {try {return val == null ? or : Yn.parse_by_char_or(val, or);} catch (Exception e) {throw err_parse(e, Bool_.Cls_val_name);}}
|
||||
public DateAdp To_date_or(DateAdp or) {try {return val == null ? or : DateAdp_.parse_gplx(val) ;} catch (Exception e) {throw err_parse(e, DateAdp_.Cls_ref_name);}}
|
||||
public Guid_adp To_guid_or(Guid_adp or) {try {return val == null ? or : Guid_adp_.parse_(val) ;} catch (Exception e) {throw err_parse(e, Guid_adp_.Cls_ref_name);}}
|
||||
public boolean To_bool() {Fail_if_null(); try {return Yn.parse_(val) ;} catch (Exception e) {throw err_parse(e, Bool_.Cls_val_name);}}
|
||||
public byte To_byte() {Fail_if_null(); try {return Byte_.parse_(val) ;} catch (Exception e) {throw err_parse(e, Byte_.Cls_val_name);}}
|
||||
public int To_int() {Fail_if_null(); try {return Int_.parse_(val) ;} catch (Exception e) {throw err_parse(e, Int_.Cls_val_name);}}
|
||||
public String To_str() {Fail_if_null(); return val;}
|
||||
private void Fail_if_null() {if (val == null) throw Err_.new_("cfg.val is empty; grp={0} key={1}", grp, key); }
|
||||
private Err err_parse(Exception e, String type) {return Err_.new_("cfg.val is not parseable; grp={0} key={1} val={2} type={3}", grp, key, val, type);}
|
||||
|
||||
private static final String Grp_none = "";
|
||||
public static Db_cfg_itm new_str (String key, String val) {return new Db_cfg_itm(Grp_none , key, val);}
|
||||
public static Db_cfg_itm new_str (String grp, String key, String val) {return new Db_cfg_itm(grp , key, val);}
|
||||
public static Db_cfg_itm new_bry (String key, byte[] val) {return new Db_cfg_itm(Grp_none , key, String_.new_utf8_(val));}
|
||||
public static Db_cfg_itm new_bry (String grp, String key, byte[] val) {return new Db_cfg_itm(grp , key, String_.new_utf8_(val));}
|
||||
public static Db_cfg_itm new_int (String key, int val) {return new Db_cfg_itm(Grp_none , key, Int_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_int (String grp, String key, int val) {return new Db_cfg_itm(grp , key, Int_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_long (String key, long val) {return new Db_cfg_itm(Grp_none , key, Long_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_long (String grp, String key, long val) {return new Db_cfg_itm(grp , key, Long_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_byte (String key, byte val) {return new Db_cfg_itm(Grp_none , key, Byte_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_byte (String grp, String key, byte val) {return new Db_cfg_itm(grp , key, Byte_.Xto_str(val));}
|
||||
public static Db_cfg_itm new_yn (String key, boolean val) {return new Db_cfg_itm(Grp_none , key, Yn.Xto_str(val));}
|
||||
public static Db_cfg_itm new_yn (String grp, String key, boolean val) {return new Db_cfg_itm(grp , key, Yn.Xto_str(val));}
|
||||
public static Db_cfg_itm new_DateAdp (String key, DateAdp val) {return new Db_cfg_itm(Grp_none , key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public static Db_cfg_itm new_DateAdp (String grp, String key, DateAdp val) {return new Db_cfg_itm(grp , key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public static Db_cfg_itm new_guid (String key, Guid_adp val) {return new Db_cfg_itm(Grp_none , key, val.XtoStr());}
|
||||
public static Db_cfg_itm new_guid (String grp, String key, Guid_adp val) {return new Db_cfg_itm(grp , key, val.XtoStr());}
|
||||
|
||||
public static final Db_cfg_itm Empty = new Db_cfg_itm("empty", "empty", null);
|
||||
}
|
@ -16,72 +16,104 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.cfgs; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.*;
|
||||
public class Db_cfg_tbl {
|
||||
private String tbl_name; private final Db_meta_fld_list flds = Db_meta_fld_list.new_();
|
||||
private String fld_grp, fld_key, fld_val;
|
||||
private Db_conn conn; private Db_stmt stmt_insert, stmt_update, stmt_select;
|
||||
public Db_conn Conn() {return conn;}
|
||||
public void Conn_(Db_conn new_conn, boolean created, boolean schema_is_1, String tbl_v1, String tbl_v2) {
|
||||
this.conn = new_conn; flds.Clear();
|
||||
String fld_prefix = "";
|
||||
if (schema_is_1) {
|
||||
tbl_name = tbl_v1;
|
||||
fld_prefix = "cfg_";
|
||||
}
|
||||
else
|
||||
tbl_name = tbl_v2;
|
||||
fld_grp = flds.Add_str(fld_prefix + "grp", 255);
|
||||
fld_key = flds.Add_str(fld_prefix + "key", 255);
|
||||
fld_val = flds.Add_str(fld_prefix + "val", 1024);
|
||||
if (created) {
|
||||
Db_meta_tbl meta = Db_meta_tbl.new_(tbl_name, flds
|
||||
, Db_meta_idx.new_unique_by_tbl(tbl_name, "main", fld_grp, fld_key, fld_val)
|
||||
);
|
||||
conn.Exec_create_tbl_and_idx(meta);
|
||||
}
|
||||
stmt_insert = stmt_update = stmt_select = null;
|
||||
import gplx.core.primitives.*;
|
||||
public class Db_cfg_tbl implements RlsAble {
|
||||
private final String tbl_name; private final Db_meta_fld_list flds = Db_meta_fld_list.new_();
|
||||
private final String fld_grp, fld_key, fld_val;
|
||||
private Db_stmt stmt_insert, stmt_update, stmt_select;
|
||||
public Db_conn Conn() {return conn;} private final Db_conn conn;
|
||||
public Db_cfg_tbl(Db_conn conn, String tbl_name) {
|
||||
this.conn = conn; this.tbl_name = tbl_name;
|
||||
this.fld_grp = flds.Add_str("cfg_grp", 255);
|
||||
this.fld_key = flds.Add_str("cfg_key", 255);
|
||||
this.fld_val = flds.Add_str("cfg_val", 1024);
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public void Insert(String grp, String key, String val) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Rls_reg(conn.Stmt_insert(tbl_name, flds));
|
||||
public void Rls() {
|
||||
stmt_insert = Db_stmt_.Rls(stmt_insert);
|
||||
stmt_update = Db_stmt_.Rls(stmt_update);
|
||||
stmt_select = Db_stmt_.Rls(stmt_select);
|
||||
}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Db_meta_tbl.new_(tbl_name, flds, Db_meta_idx.new_unique_by_tbl(tbl_name, "main", fld_grp, fld_key, fld_val)));}
|
||||
public void Delete_val(String grp, String key) {conn.Stmt_delete(tbl_name, fld_grp, fld_key).Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_delete();}
|
||||
public void Delete_grp(String grp) {conn.Stmt_delete(tbl_name, fld_grp).Crt_str(fld_grp, grp).Exec_delete();}
|
||||
public void Delete_all() {conn.Stmt_delete(tbl_name, Db_meta_fld.Ary_empy).Exec_delete();}
|
||||
public void Insert_yn (String grp, String key, boolean val) {Insert_str(grp, key, val ? "y" : "n");}
|
||||
public void Insert_byte (String grp, String key, byte val) {Insert_str(grp, key, Byte_.Xto_str(val));}
|
||||
public void Insert_int (String grp, String key, int val) {Insert_str(grp, key, Int_.Xto_str(val));}
|
||||
public void Insert_long (String grp, String key, long val) {Insert_str(grp, key, Long_.Xto_str(val));}
|
||||
public void Insert_date (String grp, String key, DateAdp val) {Insert_str(grp, key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public void Insert_guid (String grp, String key, Guid_adp val) {Insert_str(grp, key, val.XtoStr());}
|
||||
public void Insert_bry (String grp, String key, byte[] val) {Insert_str(grp, key, String_.new_utf8_(val));}
|
||||
public void Insert_str (String grp, String key, String val) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear().Val_str(fld_grp, grp).Val_str(fld_key, key).Val_str(fld_val, val).Exec_insert();
|
||||
}
|
||||
public void Update(String grp, String key, String val) {
|
||||
if (stmt_update == null) stmt_update = conn.Rls_reg(conn.Stmt_update_exclude(tbl_name, flds, fld_grp, fld_key));
|
||||
}
|
||||
public void Update_yn (String grp, String key, boolean val) {Update_str(grp, key, val ? "y" : "n");}
|
||||
public void Update_byte (String grp, String key, byte val) {Update_str(grp, key, Byte_.Xto_str(val));}
|
||||
public void Update_int (String grp, String key, int val) {Update_str(grp, key, Int_.Xto_str(val));}
|
||||
public void Update_long (String grp, String key, long val) {Update_str(grp, key, Long_.Xto_str(val));}
|
||||
public void Update_date (String grp, String key, DateAdp val) {Update_str(grp, key, val.XtoStr_fmt_yyyyMMdd_HHmmss());}
|
||||
public void Update_guid (String grp, String key, Guid_adp val) {Update_str(grp, key, val.XtoStr());}
|
||||
public void Update_bry (String grp, String key, byte[] val) {Update_str(grp, key, String_.new_utf8_(val));}
|
||||
public void Update_str (String grp, String key, String val) {
|
||||
if (stmt_update == null) stmt_update = conn.Stmt_update_exclude(tbl_name, flds, fld_grp, fld_key);
|
||||
stmt_update.Clear().Val_str(fld_val, val).Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_update();
|
||||
}
|
||||
public int Select_as_int_or_fail(String grp, String key) {
|
||||
int rv = Select_as_int_or(grp, key, Int_.MinValue);
|
||||
if (rv == Int_.MinValue) throw Err_.new_fmt_("dbs.cfg_tbl.Select_as_int_or_fail: tbl={0} grp={1} key={2}", tbl_name, grp, key);
|
||||
public void Upsert_str (String grp, String key, String val) {
|
||||
String cur_val = this.Select_str_or(grp, key, null);
|
||||
if (cur_val == null) this.Insert_str(grp, key, val);
|
||||
else this.Update_str(grp, key, val);
|
||||
}
|
||||
public boolean Select_yn (String grp, String key) {String val = Select_str(grp, key); return Parse_yn (grp, key, val);}
|
||||
public byte Select_byte (String grp, String key) {String val = Select_str(grp, key); return Parse_byte (grp, key, val);}
|
||||
public int Select_int (String grp, String key) {String val = Select_str(grp, key); return Parse_int (grp, key, val);}
|
||||
public long Select_long (String grp, String key) {String val = Select_str(grp, key); return Parse_long (grp, key, val);}
|
||||
public byte[] Select_bry (String grp, String key) {String val = Select_str(grp, key); return Parse_bry (grp, key, val);}
|
||||
public DateAdp Select_date (String grp, String key) {String val = Select_str(grp, key); return Parse_date (grp, key, val);}
|
||||
public Guid_adp Select_guid (String grp, String key) {String val = Select_str(grp, key); return Parse_guid (grp, key, val);}
|
||||
public boolean Select_yn_or (String grp, String key, boolean or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_yn (grp, key, val);}
|
||||
public byte Select_byte_or (String grp, String key, byte or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_byte (grp, key, val);}
|
||||
public int Select_int_or (String grp, String key, int or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_int (grp, key, val);}
|
||||
public long Select_long_or (String grp, String key, long or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_long (grp, key, val);}
|
||||
public byte[] Select_bry_or (String grp, String key, byte[] or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_bry (grp, key, val);}
|
||||
public DateAdp Select_date_or (String grp, String key, DateAdp or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_date (grp, key, val);}
|
||||
public Guid_adp Select_guid_or (String grp, String key, Guid_adp or) {String val = Select_str_or(grp, key, null) ; return val == null ? or : Parse_guid (grp, key, val);}
|
||||
public String Select_str (String grp, String key) {
|
||||
String rv = Select_str_or(grp, key, null); if (rv == null) throw Err_.new_("cfg.missing; grp={0} key={1}", grp, key);
|
||||
return rv;
|
||||
}
|
||||
public long Select_as_long_or(String grp, String key, long or) {return Long_.parse_or_(Select_as_str_or(grp, key, null), or);}
|
||||
public byte Select_as_byte_or(String grp, String key, byte or) {return Byte_.parse_or_(Select_as_str_or(grp, key, null), or);}
|
||||
public int Select_as_int_or(String grp, String key, int or) {return Int_.parse_or_(Select_as_str_or(grp, key, null), or);}
|
||||
public String Select_as_str_or(String grp, String key, String or) {
|
||||
if (stmt_select == null) stmt_select = conn.Rls_reg(conn.Stmt_select(tbl_name, String_.Ary(fld_val), fld_grp, fld_key));
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
try {
|
||||
rdr = stmt_select.Clear()
|
||||
.Crt_str(fld_grp, grp)
|
||||
.Crt_str(fld_key, key)
|
||||
.Exec_select_as_rdr();
|
||||
return rdr.Move_next() ? rdr.Read_str(fld_val) : or;
|
||||
} finally {rdr.Rls();}
|
||||
public String Select_str_or (String grp, String key, String or) {
|
||||
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, String_.Ary(fld_val), fld_grp, fld_key);
|
||||
Db_rdr rdr = stmt_select.Clear().Crt_str(fld_grp, grp).Crt_str(fld_key, key).Exec_select__rls_manual();
|
||||
try {return rdr.Move_next() ? rdr.Read_str(fld_val) : or;} finally {rdr.Rls();}
|
||||
}
|
||||
public Db_cfg_grp Select_as_grp(String grp) {
|
||||
Db_cfg_grp rv = null;
|
||||
Db_stmt stmt = conn.Stmt_select(tbl_name, flds, fld_grp);
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
public Db_cfg_hash Select_as_hash(String grp) {
|
||||
Db_cfg_hash rv = new Db_cfg_hash(grp);
|
||||
Db_rdr rdr = conn.Stmt_select(tbl_name, flds, fld_grp).Crt_str(fld_grp, grp).Exec_select__rls_auto();
|
||||
try {
|
||||
rdr = stmt.Clear().Crt_str(fld_grp, grp).Exec_select_as_rdr();
|
||||
while (rdr.Move_next()) {
|
||||
if (rv == null) rv = new Db_cfg_grp(grp);
|
||||
rv.Upsert(rdr.Read_str(fld_key), rdr.Read_str(fld_val));
|
||||
rv.Add(rdr.Read_str(fld_key), rdr.Read_str(fld_val));
|
||||
}
|
||||
}
|
||||
finally {rdr.Rls();}
|
||||
return rv == null ? Db_cfg_grp.Null : rv;
|
||||
return rv;
|
||||
}
|
||||
public void Rls() {conn.Conn_term();}
|
||||
// NOTE: Assert guarantees that a value exists in database and returns it (Select + Insert); (1) String val = Assert('grp', 'key', 'val'); (2) Update('grp', 'key', 'val2');
|
||||
public boolean Assert_yn (String grp, String key, boolean or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_yn (grp, key, or); return or;} return Parse_yn (grp, key, val);}
|
||||
public byte Assert_byte (String grp, String key, byte or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_byte (grp, key, or); return or;} return Parse_byte (grp, key, val);}
|
||||
public int Assert_int (String grp, String key, int or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_int (grp, key, or); return or;} return Parse_int (grp, key, val);}
|
||||
public long Assert_long (String grp, String key, long or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_long (grp, key, or); return or;} return Parse_long (grp, key, val);}
|
||||
public byte[] Assert_bry (String grp, String key, byte[] or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_bry (grp, key, or); return or;} return Parse_bry (grp, key, val);}
|
||||
public DateAdp Assert_date (String grp, String key, DateAdp or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_date (grp, key, or); return or;} return Parse_date (grp, key, val);}
|
||||
public Guid_adp Assert_guid (String grp, String key, Guid_adp or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_guid (grp, key, or); return or;} return Parse_guid (grp, key, val);}
|
||||
public String Assert_str (String grp, String key, String or) {String val = Select_str_or(grp, key, null) ; if (val == null) {Insert_str (grp, key, or); return or;} return val;}
|
||||
private boolean Parse_yn (String grp, String key, String val) {try {return Yn.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Bool_.Cls_val_name);}}
|
||||
private byte Parse_byte (String grp, String key, String val) {try {return Byte_.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Byte_.Cls_val_name);}}
|
||||
private int Parse_int (String grp, String key, String val) {try {return Int_.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Int_.Cls_val_name);}}
|
||||
private long Parse_long (String grp, String key, String val) {try {return Long_.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Long_.Cls_val_name);}}
|
||||
private byte[] Parse_bry (String grp, String key, String val) {try {return Bry_.new_utf8_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Bry_.Cls_val_name);}}
|
||||
private DateAdp Parse_date (String grp, String key, String val) {try {return DateAdp_.parse_gplx(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, DateAdp_.Cls_ref_name);}}
|
||||
private Guid_adp Parse_guid (String grp, String key, String val) {try {return Guid_adp_.parse_(val) ;} catch (Exception e) {throw err_parse(e, grp, key, val, Guid_adp_.Cls_ref_name);}}
|
||||
private Err err_parse(Exception e, String grp, String key, String val, String type) {return Err_.new_("cfg.val is not parseable; grp={0} key={1} val={2} type={3}", grp, key, val, type);}
|
||||
}
|
||||
|
@ -18,31 +18,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs.schemas; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Schema_loader_mgr_ {
|
||||
public static final Schema_loader_mgr Null = new Schema_loader_mgr__null();
|
||||
public static final Schema_loader_mgr Sqlite = new Schema_loader_mgr__sqlite();
|
||||
public static final Schema_loader_mgr Null = new Schema_loader_mgr__null();
|
||||
public static final Schema_loader_mgr Sqlite = new Schema_loader_mgr__sqlite();
|
||||
}
|
||||
class Schema_loader_mgr__null implements Schema_loader_mgr {
|
||||
public void Load(Schema_db_mgr db_mgr, Db_conn conn) {}
|
||||
}
|
||||
class Schema_loader_mgr__sqlite implements Schema_loader_mgr {
|
||||
public void Load(Schema_db_mgr db_mgr, Db_conn conn) {
|
||||
Gfo_usr_dlg_._.Log_many("", "", "db.schema.load.bgn: conn=~{0}", conn.Url().Xto_api());
|
||||
Gfo_usr_dlg_.I.Log_many("", "", "db.schema.load.bgn: conn=~{0}", conn.Conn_info().Xto_api());
|
||||
Schema_tbl_mgr tbl_mgr = db_mgr.Tbl_mgr();
|
||||
Db_qry__select_in_tbl qry = Db_qry__select_in_tbl.new_("sqlite_master", String_.Ary_empty, String_.Ary("type", "name", "sql"), Db_qry__select_in_tbl.Order_by_null);
|
||||
Db_stmt stmt = Db_stmt_.new_select_as_rdr(conn, qry);
|
||||
Db_rdr rdr = stmt.Exec_select_as_rdr();
|
||||
while (rdr.Move_next()) {
|
||||
int type = Schema_itm_tid.Xto_int(rdr.Read_str(0));
|
||||
switch (type) {
|
||||
case Schema_itm_tid.Tid_table:
|
||||
Schema_tbl_itm tbl_itm = new Schema_tbl_itm(rdr.Read_str(1), rdr.Read_str(2));
|
||||
tbl_mgr.Add(tbl_itm);
|
||||
break;
|
||||
case Schema_itm_tid.Tid_index: break; // noop for now
|
||||
default: throw Err_.unhandled(type);
|
||||
Db_rdr rdr = conn.Stmt_new(qry).Exec_select__rls_auto();
|
||||
try {
|
||||
while (rdr.Move_next()) {
|
||||
int type = Schema_itm_tid.Xto_int(rdr.Read_str(0));
|
||||
switch (type) {
|
||||
case Schema_itm_tid.Tid_table:
|
||||
Schema_tbl_itm tbl_itm = new Schema_tbl_itm(rdr.Read_str(1), rdr.Read_str(2));
|
||||
tbl_mgr.Add(tbl_itm);
|
||||
break;
|
||||
case Schema_itm_tid.Tid_index: break; // noop for now
|
||||
default: throw Err_.unhandled(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
rdr.Rls();
|
||||
Gfo_usr_dlg_._.Log_many("", "", "db.schema.load.end");
|
||||
} finally {rdr.Rls();}
|
||||
Gfo_usr_dlg_.I.Log_many("", "", "db.schema.load.end");
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user