mirror of
https://github.com/gnosygnu/xowa.git
synced 2025-06-13 12:54:14 +00:00
v2.1.4.1
This commit is contained in:
parent
1b0042ef8a
commit
efaf9dcd00
@ -56,7 +56,7 @@ class Gfo_msg_root_fxt {
|
||||
public void Tst_data_new_many(String path, String key, String fmt, Object... vals) {
|
||||
Gfo_msg_data data = root.Data_new_many(Gfo_msg_itm_.Cmd_note, path, key, fmt, vals);
|
||||
if (expd_item_uid != -1) Tfds.Eq(expd_item_uid, data.Item().Uid());;
|
||||
if (expd_item_fmtr_arg_exists != Bool_.__byte) Tfds.Eq(Bool_.int_(expd_item_fmtr_arg_exists), data.Item().Fmtr().Fmt_args_exist());
|
||||
if (expd_item_fmtr_arg_exists != Bool_.__byte) Tfds.Eq(Bool_.By_int(expd_item_fmtr_arg_exists), data.Item().Fmtr().Fmt_args_exist());
|
||||
if (expd_data_str != null) Tfds.Eq(expd_data_str, data.Item().Gen_str_many(data.Vals()));
|
||||
}
|
||||
}
|
||||
|
@ -47,8 +47,6 @@ public class Array_ {
|
||||
rv[i + curReplacePos] = add[i];
|
||||
for (int i = curReplacePos + addInsertPos; i < curLen; i++) // copy old after curReplacePos
|
||||
rv[i + newLen] = cur[i];
|
||||
// tst_ReplaceInsert(ary_obj(0, 1, 4, 5) , ary_obj(1, 2, 3), 1, 1, ary_obj(0, 1, 2, 3, 4, 5));
|
||||
// tst_ReplaceInsert(ary_obj(0, 1, 2, 4, 5), ary_obj(1, 2, 3), 1, 2, ary_obj(0, 1, 2, 3, 4, 5));//3,4 -> 4,5
|
||||
return rv;
|
||||
}
|
||||
public static Object Resize(Object src, int trgLen) {
|
||||
|
@ -16,36 +16,35 @@ You should have 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 Bool_ implements GfoInvkAble {
|
||||
public static final boolean[] Ary_empty = new boolean[0];
|
||||
public static final Class<?> ClassOf = Boolean.class;
|
||||
public static int Xto_int(boolean v) {return v ? 1 : 0;}
|
||||
public static String Xto_str_lower(boolean v) {return v ? "true" : "false";}
|
||||
public static byte Xto_byte(boolean v) {return v ? Y_byte : N_byte;}
|
||||
public static boolean cast_(Object obj) {try {return (Boolean)obj;} catch (Exception e) {throw Err_.type_mismatch_exc_(e, boolean.class, obj);}}
|
||||
public static boolean cast_or_(Object obj, boolean v) {try {return (Boolean)obj;} catch (Exception e) {Err_.Noop(e); return v;}}
|
||||
public static boolean int_(int v) {return v != 0;}
|
||||
public static boolean TypeMatch(Class<?> type) {return type == boolean.class || type == Boolean.class;}
|
||||
public class Bool_ implements GfoInvkAble {
|
||||
public static final String Cls_val_name = "boolean";
|
||||
public static final Class<?> Cls_ref_type = Boolean.class;
|
||||
public static final boolean N = false , Y = true;
|
||||
public static final byte N_byte = 0 , Y_byte = 1 , __byte = 127;
|
||||
public static final int N_int = 0 , Y_int = 1 , __int = -1;
|
||||
public static final String N_str = "n" , Y_str = "y";
|
||||
public static final byte[] Y_bry = new byte[] {Byte_ascii.Ltr_y}, N_bry = new byte[] {Byte_ascii.Ltr_n};
|
||||
public static final String True_str = "true", False_str = "false";
|
||||
public static final byte[] True_bry = Bry_.new_ascii_(True_str), False_bry = Bry_.new_ascii_(False_str);
|
||||
public static boolean cast_(Object obj) {try {return (Boolean)obj;} catch (Exception e) {throw Err_.type_mismatch_exc_(e, boolean.class, obj);}}
|
||||
public static boolean cast_or_(Object obj, boolean v) {try {return (Boolean)obj;} catch (Exception e) {Err_.Noop(e); return v;}}
|
||||
public static boolean By_int(int v) {return v != 0;}
|
||||
public static boolean parse_(String raw) {
|
||||
if ( String_.Eq(raw, "true")
|
||||
|| String_.Eq(raw, "True") // needed for Store_Wtr(){boolVal.toString();}
|
||||
)
|
||||
)
|
||||
return true;
|
||||
else if ( String_.Eq(raw, "false")
|
||||
|| String_.Eq(raw, "False")
|
||||
)
|
||||
)
|
||||
return false;
|
||||
throw Err_.parse_type_(boolean.class, raw);
|
||||
}
|
||||
public static final int N_int = 0, Y_int = 1, __int = -1;
|
||||
public static final byte N_byte = 0, Y_byte = 1, __byte = 127;
|
||||
public static final boolean N = false, Y = true;
|
||||
public static final String N_str = "n", Y_str = "y";
|
||||
public static final String True_str = "true", False_str = "false";
|
||||
public static final byte[] True_bry = Bry_.new_ascii_(True_str), False_bry = Bry_.new_ascii_(False_str);
|
||||
public static final byte[] Y_bry = new byte[] {Byte_ascii.Ltr_y}, N_bry = new byte[] {Byte_ascii.Ltr_n};
|
||||
public static byte Xto_byte(boolean v) {return v ? Y_byte : N_byte;}
|
||||
public static int Xto_int(boolean v) {return v ? 1 : 0;}
|
||||
public static String Xto_str_lower(boolean v) {return v ? "true" : "false";}
|
||||
public static final boolean[] Ary_empty = new boolean[0];
|
||||
public static final Bool_ Gfs = new Bool_();
|
||||
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_to_str)) {
|
||||
boolean v = m.ReadBool(GfsCore_.Arg_primitive);
|
||||
|
@ -16,8 +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;
|
||||
public class Bool_obj_ref {
|
||||
public class Bool_obj_ref {
|
||||
public boolean Val() {return val;} private boolean val;
|
||||
public boolean Val_y() {return val;}
|
||||
public boolean Val_n() {return !val;}
|
||||
public Bool_obj_ref Val_y_() {val = true; return this;}
|
||||
public Bool_obj_ref Val_n_() {val = false; return this;}
|
||||
public Bool_obj_ref Val_(boolean v) {val = v; return this;}
|
||||
|
@ -16,13 +16,14 @@ You should have 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 Bool_obj_val {
|
||||
public class Bool_obj_val {
|
||||
Bool_obj_val(int v) {val = v;} private final int val;
|
||||
public boolean Val() {return val == 1;}
|
||||
Bool_obj_val(int v) {val = v;} int val;
|
||||
public static final Bool_obj_val
|
||||
Null = new Bool_obj_val(-1)
|
||||
, False = new Bool_obj_val(0)
|
||||
, True = new Bool_obj_val(1);
|
||||
Null = new Bool_obj_val(-1)
|
||||
, False = new Bool_obj_val(0)
|
||||
, True = new Bool_obj_val(1)
|
||||
;
|
||||
public static Bool_obj_val read_(Object o) {String s = String_.as_(o); return s == null ? (Bool_obj_val)o : parse_(s);}
|
||||
public static Bool_obj_val parse_(String raw) {
|
||||
if (String_.Eq(raw, "y")) return Bool_obj_val.True;
|
||||
|
@ -19,11 +19,11 @@ package gplx;
|
||||
import java.lang.*;
|
||||
import gplx.ios.*;
|
||||
public class Bry_ {
|
||||
public static final String Cls_name = "byte[]";
|
||||
public static final String Cls_val_name = "byte[]";
|
||||
public static final int NotFound = -1;
|
||||
public static final byte[] Empty = new byte[0];
|
||||
public static final byte[][] Ary_empty = new byte[0][];
|
||||
public static final Class<?> ClassOf = byte[].class;
|
||||
public static final Class<?> Cls_ref_type = byte[].class;
|
||||
public static byte[] bytes_(byte... ary) {return ary;}
|
||||
public static byte[] ints_ (int... ary) {
|
||||
int len = ary.length;
|
||||
|
@ -172,7 +172,7 @@ public class Bry__tst {
|
||||
byte[] ary_(int... ary) {
|
||||
byte[] rv = new byte[ary.length];
|
||||
for (int i = 0; i < ary.length; i++)
|
||||
rv[i] = Byte_.int_(ary[i]);
|
||||
rv[i] = Byte_.By_int(ary[i]);
|
||||
return rv;
|
||||
}
|
||||
void tst_IncrementLast(byte[] ary, byte[] expd) {Tfds.Eq_ary(expd, Bry_.Increment_last(Bry_.Copy(ary)));}
|
||||
|
@ -170,6 +170,7 @@ public class Bry_bfr {
|
||||
Add_int_fixed(val, digit_len);
|
||||
return this;
|
||||
}
|
||||
public Bry_bfr Add_int_digits(int digits, int val) {return Add_int(val, Int_.Log10(val), digits);}
|
||||
public Bry_bfr Add_int_fixed(int val, int digits) {return Add_int(val, Int_.Log10(val), digits);}
|
||||
public Bry_bfr Add_int(int val, int valLog, int arySlots) {
|
||||
int aryBgn = bfr_len, aryEnd = bfr_len + arySlots;
|
||||
|
@ -35,7 +35,7 @@ public class Bry_finder {
|
||||
return Bry_finder.Not_found;
|
||||
}
|
||||
public static int Move_fwd(byte[] src, byte[] lkp, int cur, int end) {
|
||||
int rv = Find_fwd(src, lkp, 0, src.length);
|
||||
int rv = Find_fwd(src, lkp, cur, src.length);
|
||||
return rv == Bry_finder.Not_found ? rv : rv + lkp.length;
|
||||
}
|
||||
public static int Find_fwd(byte[] src, byte[] lkp) {return Find(src, lkp, 0 , src.length, true);}
|
||||
|
@ -16,19 +16,19 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Byte_ {
|
||||
public static final byte MinValue = Byte.MIN_VALUE;
|
||||
public static final String Cls_name = "byte";
|
||||
public static byte[] Ary(byte... ary) {return ary;}
|
||||
public static byte[] Ary_by_ints(int... ary) {
|
||||
int ary_len = ary.length;
|
||||
byte[] rv = new byte[ary_len];
|
||||
for (int i = 0; i < ary_len; i++)
|
||||
rv[i] = int_(ary[i]);
|
||||
return rv;
|
||||
}
|
||||
public static String Xto_str(byte v) {return new Byte(v).toString();}
|
||||
public static int Xto_int(byte v) {return v < 0 ? (int)v + 256 : v;}
|
||||
public class Byte_ {
|
||||
public static final String Cls_val_name = "byte";
|
||||
public static final Class<?> Cls_ref_type = Byte.class;
|
||||
public static final byte
|
||||
Zero = 0
|
||||
, Min_value = Byte.MIN_VALUE
|
||||
, Max_value_127 = 127
|
||||
;
|
||||
public static byte cast_(Object o) {try {return (Byte)o;} catch (Exception e) {throw Err_.type_mismatch_exc_(e, byte.class, o);}}
|
||||
public static byte parse_(String raw) {return Byte.parseByte(raw);}
|
||||
public static byte By_int(int v) {return v > 127 ? (byte)(v - 256) : (byte)v;} // PERF?: (byte)(v & 0xff)
|
||||
public static int Xto_int(byte v) {return v < 0 ? (int)v + 256 : v;}
|
||||
public static String Xto_str(byte v) {return new Byte(v).toString();}
|
||||
public static boolean In(byte v, byte... ary) {
|
||||
for (byte itm : ary)
|
||||
if (v == itm) return true;
|
||||
@ -39,11 +39,12 @@ public class Byte_ {
|
||||
else if (lhs < rhs) return CompareAble_.Less;
|
||||
else return CompareAble_.More;
|
||||
}
|
||||
public static byte cast_(Object o) {try {return (Byte)o;} catch (Exception e) {throw Err_.type_mismatch_exc_(e, byte.class, o);}}
|
||||
public static byte parse_(String raw) {return Byte.parseByte(raw);}
|
||||
public static byte int_(int v) {return v > 127 ? (byte)(v - 256) : (byte)v;} // PERF?: (byte)(v & 0xff)
|
||||
public static byte Xto_boolean_byte(boolean v) {
|
||||
return v ? Bool_.Y_byte : Bool_.N_byte;
|
||||
public static byte[] Ary(byte... ary) {return ary;}
|
||||
public static byte[] Ary_by_ints(int... ary) {
|
||||
int ary_len = ary.length;
|
||||
byte[] rv = new byte[ary_len];
|
||||
for (int i = 0; i < ary_len; i++)
|
||||
rv[i] = By_int(ary[i]);
|
||||
return rv;
|
||||
}
|
||||
public static final byte Zero = 0, MaxValue_127 = 127;
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class Byte__tst {
|
||||
tst_int_( 128, 128); // NOTE: JAVA defines byte as -128 -> 127
|
||||
tst_int_( 255, 255);
|
||||
tst_int_( 256, 0); // NOTE: 256 will cast to 1; (byte)256 works same in both JAVA/.NET
|
||||
} void tst_int_(int v, int expd) {Tfds.Eq((byte)expd, Byte_.int_(v));} // WORKAROUND/JAVA: expd is of type int b/c java promotes numbers to ints
|
||||
} void tst_int_(int v, int expd) {Tfds.Eq((byte)expd, Byte_.By_int(v));} // WORKAROUND/JAVA: expd is of type int b/c java promotes numbers to ints
|
||||
@Test public void XtoInt() {
|
||||
tst_XtoInt( 0, 0);
|
||||
tst_XtoInt( 127, 127);
|
||||
|
@ -102,7 +102,9 @@ public class Byte_ascii {
|
||||
, Quote_bry = new byte[] {Byte_ascii.Quote}
|
||||
, Pipe_bry = new byte[] {Byte_ascii.Pipe}
|
||||
, Underline_bry = new byte[] {Byte_ascii.Underline}
|
||||
, Slash_bry = new byte[] {Byte_ascii.Slash}
|
||||
, Asterisk_bry = new byte[] {Byte_ascii.Asterisk}
|
||||
, Dash_bry = new byte[] {Byte_ascii.Dash}
|
||||
, Space_bry = new byte[] {Byte_ascii.Space}
|
||||
;
|
||||
}
|
||||
|
@ -17,8 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Double_ {
|
||||
public static final Class<?> ClassOf = Double.class;
|
||||
public static final String Cls_name = "double";
|
||||
public static final Class<?> Cls_ref_type = Double.class;
|
||||
public static final String Cls_val_name = "double";
|
||||
public static final double Inf_pos = Double.POSITIVE_INFINITY;;
|
||||
public static final double NaN = Double.NaN;;
|
||||
public static final byte[] NaN_bry = Bry_.new_ascii_("NaN");
|
||||
|
@ -17,7 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Float_ {
|
||||
public static final String Cls_name = "float";
|
||||
public static final Class<?> Cls_ref_type = Float.class;
|
||||
public static final String Cls_val_name = "float";
|
||||
public static final float NaN = Float.NaN;;
|
||||
public static boolean IsNaN(float v) {return Float.isNaN(v);}
|
||||
public static int RoundUp(float val) {
|
||||
|
@ -17,8 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Int_ implements GfoInvkAble {
|
||||
public static final Class<?> ClassOf = Integer.class;
|
||||
public static final String Cls_name = "int";
|
||||
public static final String Cls_val_name = "int";
|
||||
public static final Class<?> Cls_ref_type = Integer.class;
|
||||
public static final int Base1 = 1;
|
||||
public static final int Const_dlm_len = 1;
|
||||
public static final int Const_position_after_char = 1;
|
||||
|
@ -17,9 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Long_ {
|
||||
public static final Class<?> Cls_ref_type = Long.class;
|
||||
public static final long MinValue = Long.MIN_VALUE;
|
||||
public static final long MaxValue = Long.MAX_VALUE;
|
||||
public static final String Cls_name = "long";
|
||||
public static final String Cls_val_name = "long";
|
||||
public static final long Neg1 = -1;
|
||||
public static final int Log10Ary_len = 21;
|
||||
public static long[] Log10Ary = new long[]
|
||||
|
@ -27,20 +27,19 @@ public class Object_ {
|
||||
public static String Xto_str_strict_or_null(Object v) {return v == null ? null : ToString_lang(v);}
|
||||
public static String Xto_str_strict_or_null_mark(Object v) {return v == null ? String_.Null_mark : ToString_lang(v);}
|
||||
public static String Xto_str_strict_or_empty(Object v) {return v == null ? String_.Empty : ToString_lang(v);}
|
||||
static String ToString_lang(Object v) {
|
||||
if (v == null) return null;
|
||||
private static String ToString_lang(Object v) {
|
||||
Class<?> c = v.getClass();
|
||||
if (ClassAdp_.Eq(c, Bry_.ClassOf)) return String_.new_utf8_((byte[])v);
|
||||
else if (ClassAdp_.Eq(c, String_.ClassOf)) return (String)v;
|
||||
if (ClassAdp_.Eq(c, String_.Cls_ref_type)) return (String)v;
|
||||
else if (ClassAdp_.Eq(c, Bry_.Cls_ref_type)) return String_.new_utf8_((byte[])v);
|
||||
else return v.toString();
|
||||
}
|
||||
public static String Xto_str_loose_or(Object v, String or) { // tries to pretty-print doubles; also standardizes true/false; DATE:2014-07-14
|
||||
if (v == null) return null;
|
||||
Class<?> c = ClassAdp_.ClassOf_obj(v);
|
||||
if (ClassAdp_.Eq(c, Bry_.ClassOf)) return String_.new_utf8_((byte[])v);
|
||||
else if (ClassAdp_.Eq(c, String_.ClassOf)) return (String)v;
|
||||
else if (ClassAdp_.Eq(c, Bool_.ClassOf)) return Bool_.cast_(v) ? Bool_.True_str : Bool_.False_str; // always return true / false
|
||||
else if (ClassAdp_.Eq(c, Double_.ClassOf)) return Double_.Xto_str_loose(Double_.cast_(v));
|
||||
if (ClassAdp_.Eq(c, String_.Cls_ref_type)) return (String)v;
|
||||
else if (ClassAdp_.Eq(c, Bry_.Cls_ref_type)) return String_.new_utf8_((byte[])v);
|
||||
else if (ClassAdp_.Eq(c, Bool_.Cls_ref_type)) return Bool_.cast_(v) ? Bool_.True_str : Bool_.False_str; // always return "true" / "false"
|
||||
else if (ClassAdp_.Eq(c, Double_.Cls_ref_type)) return Double_.Xto_str_loose(Double_.cast_(v));
|
||||
else return v.toString();
|
||||
}
|
||||
}
|
@ -17,5 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Short_ {
|
||||
public static final Class<?> Cls_ref_type = Short.class;
|
||||
public static short cast_(Object obj) {try {return (Short)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, short.class, obj);}}
|
||||
}
|
||||
|
@ -18,8 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx;
|
||||
import java.lang.*;
|
||||
public class String_ implements GfoInvkAble {
|
||||
public static final Class<?> ClassOf = String.class;
|
||||
public static final String Cls_name = "str" + "ing";
|
||||
public static final Class<?> Cls_ref_type = String.class;
|
||||
public static final String Cls_val_name = "str" + "ing";
|
||||
public static final int Find_none = -1, Pos_neg1 = -1;
|
||||
public static final String Null = null, Empty = "", Null_mark = "<<NULL>>", Tab = "\t", Lf = "\n", CrLf = "\r\n";
|
||||
public static String cast_(Object v) {return (String)v;}
|
||||
|
@ -25,6 +25,7 @@ import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
public class DateAdp_ implements GfoInvkAble {
|
||||
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();
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
|
@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class DecimalAdp_ {
|
||||
public static final Class<?> Cls_ref_type = DecimalAdp.class;
|
||||
public static DecimalAdp as_(Object obj) {return obj instanceof DecimalAdp ? (DecimalAdp)obj : null;}
|
||||
public static final DecimalAdp Zero = new DecimalAdp(0);
|
||||
public static final DecimalAdp One = new DecimalAdp(1);
|
||||
|
@ -84,7 +84,7 @@ public class KeyVal_ {
|
||||
Ary_xto_str_nested(bfr, indent + 1, (KeyVal[])val);
|
||||
continue; // don't add \n below
|
||||
}
|
||||
else if (ClassAdp_.Eq(val_type, Bool_.ClassOf)) { // val is boolean
|
||||
else if (ClassAdp_.Eq(val_type, Bool_.Cls_ref_type)) { // val is boolean
|
||||
boolean val_as_bool = Bool_.cast_(val);
|
||||
bfr.Add(val_as_bool ? Bool_.True_bry : Bool_.False_bry); // add "true" or "false"; don't call toString
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ public interface ListAdp extends EnumerAble {
|
||||
String[] XtoStrAry();
|
||||
Object Xto_ary(Class<?> memberType);
|
||||
Object Xto_ary_and_clear(Class<?> memberType);
|
||||
Object[] Xto_obj_ary();
|
||||
|
||||
void Reverse();
|
||||
void Sort();
|
||||
|
@ -77,6 +77,7 @@ class ListAdp_null implements ListAdp {
|
||||
public Object Xto_ary_and_clear(Class<?> memberType) {return Object_.Ary_empty;}
|
||||
public String XtoStr() {return "< NULL LIST >";}
|
||||
public String[] XtoStrAry() {return new String[0];}
|
||||
public Object[] Xto_obj_ary() {return Object_.Ary_empty;}
|
||||
public java.util.Iterator iterator() {return Iterator_null._;}
|
||||
public void Reverse() {}
|
||||
public void SetAt(int i, Object o) {}
|
||||
|
@ -25,6 +25,12 @@ public abstract class ListAdp_base implements ListAdp, GfoInvkAble {
|
||||
Array_.Set(rv, i, list[i]);
|
||||
return rv;
|
||||
}
|
||||
public Object[] Xto_obj_ary() {
|
||||
Object[] rv = new Object[count];
|
||||
for (int i = 0; i < count; ++i)
|
||||
rv[i] = list[i];
|
||||
return rv;
|
||||
}
|
||||
public java.util.Iterator iterator() {
|
||||
if (count == 0)
|
||||
return Iterator_null._;
|
||||
|
@ -30,16 +30,16 @@ public class HexDecUtl {
|
||||
public static int parse_or_(byte[] raw, int or) {return parse_or_(raw, 0, raw.length, or);}
|
||||
public static int parse_or_(byte[] raw, int bgn, int end, int or) {
|
||||
int rv = 0; int factor = 1;
|
||||
byte b = Byte_.MaxValue_127;
|
||||
byte b = Byte_.Max_value_127;
|
||||
for (int i = end - 1; i >= bgn; i--) {
|
||||
switch (raw[i]) {
|
||||
case Byte_ascii.Num_0: b = 0; break; case Byte_ascii.Num_1: b = 1; break; case Byte_ascii.Num_2: b = 2; break; case Byte_ascii.Num_3: b = 3; break; case Byte_ascii.Num_4: b = 4; break;
|
||||
case Byte_ascii.Num_5: b = 5; break; case Byte_ascii.Num_6: b = 6; break; case Byte_ascii.Num_7: b = 7; break; case Byte_ascii.Num_8: b = 8; break; case Byte_ascii.Num_9: b = 9; break;
|
||||
case Byte_ascii.Ltr_A: b = 10; break; case Byte_ascii.Ltr_B: b = 11; break; case Byte_ascii.Ltr_C: b = 12; break; case Byte_ascii.Ltr_D: b = 13; break; case Byte_ascii.Ltr_E: b = 14; break; case Byte_ascii.Ltr_F: b = 15; break;
|
||||
case Byte_ascii.Ltr_a: b = 10; break; case Byte_ascii.Ltr_b: b = 11; break; case Byte_ascii.Ltr_c: b = 12; break; case Byte_ascii.Ltr_d: b = 13; break; case Byte_ascii.Ltr_e: b = 14; break; case Byte_ascii.Ltr_f: b = 15; break;
|
||||
default: b = Byte_.MaxValue_127; break;
|
||||
default: b = Byte_.Max_value_127; break;
|
||||
}
|
||||
if (b == Byte_.MaxValue_127) return or;
|
||||
if (b == Byte_.Max_value_127) return or;
|
||||
rv += b * factor;
|
||||
factor *= 16;
|
||||
}
|
||||
|
@ -455,6 +455,7 @@ public class IoEngine_system extends IoEngine_base {
|
||||
try {
|
||||
if (src_stream != null) src_stream.close();
|
||||
if (src_conn != null) src_conn.disconnect();
|
||||
src_conn.getInputStream().close();
|
||||
} catch (Exception exc) {
|
||||
Err_.Noop(exc);
|
||||
}
|
||||
|
@ -25,10 +25,31 @@ public class IoItmDir_ {
|
||||
rv.ctor_IoItmBase_url(url);
|
||||
return rv;
|
||||
}
|
||||
public static IoItmDir sub_(String name) {
|
||||
IoItmDir rv = new IoItmDir(Bool_.Y);
|
||||
rv.ctor_IoItmBase_url(Io_url_.mem_dir_("mem/" + name));
|
||||
return rv;
|
||||
}
|
||||
static IoItmDir null_() {
|
||||
IoItmDir rv = new IoItmDir(true); // TODO: NULL should be removed
|
||||
rv.ctor_IoItmBase_url(Io_url_.Null);
|
||||
rv.Exists_set(false);
|
||||
return rv;
|
||||
}
|
||||
public static void Make(IoItmDir dir) {
|
||||
Io_mgr._.CreateDir(dir.Url());
|
||||
int len = dir.SubDirs().Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
IoItmDir sub_dir = (IoItmDir)dir.SubDirs().FetchAt(i);
|
||||
Make(sub_dir);
|
||||
}
|
||||
len = dir.SubFils().Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
IoItmFil sub_fil = (IoItmFil)dir.SubFils().FetchAt(i);
|
||||
String text = String_.Repeat("a", (int)sub_fil.Size());
|
||||
Io_url sub_url = sub_fil.Url();
|
||||
Io_mgr._.SaveFilStr(sub_url, text);
|
||||
Io_mgr._.UpdateFilModifiedTime(sub_url, sub_fil.ModifiedTime());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,13 @@ package gplx.ios; import gplx.*;
|
||||
public class IoItmFil_ {
|
||||
public static IoItmFil as_(Object obj) {return obj instanceof IoItmFil ? (IoItmFil)obj : null;}
|
||||
public static final String
|
||||
Prop_Size = "size"
|
||||
, Prop_Modified = "modified";
|
||||
Prop_Size = "size"
|
||||
, Prop_Modified = "modified";
|
||||
public static IoItmFil new_(Io_url url, long size, DateAdp created, DateAdp modified) {return new IoItmFil().ctor_IoItmFil(url, size, modified);}
|
||||
public static IoItmFil sub_(String name, long size, DateAdp modified) {
|
||||
IoItmFil rv = new IoItmFil();
|
||||
rv.ctor_IoItmFil(Io_url_.mem_fil_("mem/" + name), size, modified);
|
||||
rv.Name_(name);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
@ -51,4 +51,3 @@ public abstract class IoItm_base implements GfoInvkAble, CompareAble {
|
||||
@gplx.Internal protected void ctor_IoItmBase_url(Io_url url) {this.url = url; this.name = url.NameAndExt();}
|
||||
@gplx.Internal protected void ctor_IoItmBase_name(String name) {this.name = name;}
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,13 @@ public class Io_size_ {
|
||||
String[] unit = Io_size_.Units[pow];
|
||||
return valDecimal.Xto_str("#,###.000") + " " + String_.PadBgn(unit[0], 2, " ");
|
||||
}
|
||||
public static String Xto_str(long val, int exp_1024, String val_fmt, String unit_pad, boolean round_0_to_1) {
|
||||
long exp_val = (long)Math_.Pow(1024, exp_1024);
|
||||
DecimalAdp val_as_decimal = DecimalAdp_.divide_(val, exp_val);
|
||||
if (round_0_to_1 && val_as_decimal.Comp_lt(1)) val_as_decimal = DecimalAdp_.One;
|
||||
String[] unit = Io_size_.Units[exp_1024];
|
||||
return val_as_decimal.Xto_str(val_fmt) + " " + String_.PadBgn(unit[0], 2, unit_pad);
|
||||
}
|
||||
public static long parse_or_(String raw, long or) {
|
||||
if (raw == null || raw == String_.Empty) return or;
|
||||
String[] terms = String_.Split(raw, " ");
|
||||
@ -46,11 +53,12 @@ public class Io_size_ {
|
||||
val = val.Op_mult(1024);
|
||||
curPow--;
|
||||
}
|
||||
DecimalAdp comp = val.Op_truncate_decimal();
|
||||
if (!val.Eq(comp)) return or;
|
||||
// DELETED:do not check for fractional bytes; EX: 10.7 GB DATE:2015-01-06
|
||||
// DecimalAdp comp = val.Op_truncate_decimal();
|
||||
// if (!val.Eq(comp)) return or;
|
||||
return val.Xto_long();
|
||||
}
|
||||
static int parse_unitPow_(String unitStr) {
|
||||
private static int parse_unitPow_(String unitStr) {
|
||||
int unitLen = Array_.Len(Units);
|
||||
int unitPow = -1;
|
||||
for (int i = 0; i < unitLen; i++) {
|
||||
@ -59,16 +67,7 @@ public class Io_size_ {
|
||||
}
|
||||
return unitPow;
|
||||
}
|
||||
static String UnitsXtoStr() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
int len = Array_.Len(Units);
|
||||
for (int i = 0; i < len; i++) {
|
||||
String[] eny = Units[i];
|
||||
sb.Add_fmt("{0},{1};", eny[0], eny[1]);
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
static final String[][] Units = new String[][]
|
||||
private static final String[][] Units = new String[][]
|
||||
{ String_.Ary("B", "BYTE")
|
||||
, String_.Ary("KB", "KILOBYTE")
|
||||
, String_.Ary("MB", "MEGABYTE")
|
||||
|
@ -18,39 +18,52 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.ios; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Io_size__tst {
|
||||
@Test public void XtoLong() {
|
||||
tst_XtoLong("1", 1);
|
||||
tst_XtoLong("1 KB", 1024);
|
||||
tst_XtoLong("1 MB", 1024 * 1024);
|
||||
tst_XtoLong("1 GB", 1024 * 1024 * 1024);
|
||||
tst_XtoLong("12 kb", 12 * 1024);
|
||||
tst_XtoLong("1.5 kb", 1024 + 512); // 1536
|
||||
tst_XtoLong("1.5 mb", (long)(1024 * 1024 * 1.5));
|
||||
tst_XtoLong("-1", -1); // NOTE: negative bytes allowed
|
||||
private Io_size__fxt fxt = new Io_size__fxt();
|
||||
@Test public void XtoLong() {
|
||||
fxt.Test_XtoLong("1", 1);
|
||||
fxt.Test_XtoLong("1 KB", 1024);
|
||||
fxt.Test_XtoLong("1 MB", 1024 * 1024);
|
||||
fxt.Test_XtoLong("1 GB", 1024 * 1024 * 1024);
|
||||
fxt.Test_XtoLong("12 kb", 12 * 1024);
|
||||
fxt.Test_XtoLong("1.5 kb", 1024 + 512); // 1536
|
||||
fxt.Test_XtoLong("1.5 mb", (long)(1024 * 1024 * 1.5));
|
||||
fxt.Test_XtoLong("-1", -1); // NOTE: negative bytes allowed
|
||||
|
||||
tst_XtoLongFail("1 kilobite");
|
||||
tst_XtoLongFail("1 BB");
|
||||
tst_XtoLongFail("1.1");
|
||||
tst_XtoLongFail("1.51 kb");
|
||||
fxt.Test_XtoLongFail("1 kilobite");
|
||||
fxt.Test_XtoLongFail("1 BB");
|
||||
// fxt.Test_XtoLongFail("1.1"); // DELETED:do not check for fractional bytes; EX: 10.7 GB DATE:2015-01-06
|
||||
// fxt.Test_XtoLongFail("1.51 kb");
|
||||
}
|
||||
void tst_XtoLong(String raw, long expd) {Tfds.Eq(expd, Io_size_.parse_or_(raw, Long_.MinValue));}
|
||||
void tst_XtoLongFail(String raw) {
|
||||
@Test public void XtoStr() {
|
||||
fxt.Test_XtoStr(1, "1.000 B");
|
||||
fxt.Test_XtoStr(1024, "1.000 KB");
|
||||
fxt.Test_XtoStr(1536, "1.500 KB");
|
||||
fxt.Test_XtoStr(1024 * 1024, "1.000 MB");
|
||||
fxt.Test_XtoStr(1016, "1,016.000 B"); // NOTE: 1016 is not 1.016 KB
|
||||
}
|
||||
@Test public void Xto_str_full() {
|
||||
fxt.Test_Xto_str( 500, 1, "#,###", " ", Bool_.Y, "1 KB");
|
||||
fxt.Test_Xto_str( 1000, 1, "#,###", " ", Bool_.Y, "1 KB");
|
||||
fxt.Test_Xto_str( 2000, 1, "#,###", " ", Bool_.Y, "2 KB");
|
||||
fxt.Test_Xto_str( 1234567, 1, "#,###", " ", Bool_.Y, "1,206 KB");
|
||||
fxt.Test_Xto_str(1234567890, 1, "#,###", " ", Bool_.Y, "1,205,633 KB");
|
||||
}
|
||||
@Test public void EqualsTest() {
|
||||
fxt.Test_Equals("1", "1");
|
||||
fxt.Test_Equals("1 kb", "1 kb");
|
||||
fxt.Test_Equals("1024", "1 kb");
|
||||
fxt.Test_Equals("1048576", "1 mb");
|
||||
fxt.Test_Equals("1024 kb", "1 mb");
|
||||
fxt.Test_Equals("1.5 kb", "1536 b");
|
||||
}
|
||||
}
|
||||
class Io_size__fxt {
|
||||
public void Test_XtoLong(String raw, long expd) {Tfds.Eq(expd, Io_size_.parse_or_(raw, Long_.MinValue));}
|
||||
public void Test_XtoLongFail(String raw) {
|
||||
long val = Io_size_.parse_or_(raw, Long_.MinValue);
|
||||
if (val != Long_.MinValue) Tfds.Fail("expd parse failure; raw=" + raw);
|
||||
}
|
||||
@Test public void XtoStr() {
|
||||
tst_XtoStr(1, "1.000 B");
|
||||
tst_XtoStr(1024, "1.000 KB");
|
||||
tst_XtoStr(1536, "1.500 KB");
|
||||
tst_XtoStr(1024 * 1024, "1.000 MB");
|
||||
tst_XtoStr(1016, "1,016.000 B"); // NOTE: 1016 is not 1.016 KB
|
||||
} void tst_XtoStr(long val, String expd) {Tfds.Eq(expd, Io_size_.Xto_str(val));}
|
||||
@Test public void EqualsTest() {
|
||||
tst_Equals("1", "1");
|
||||
tst_Equals("1 kb", "1 kb");
|
||||
tst_Equals("1024", "1 kb");
|
||||
tst_Equals("1048576", "1 mb");
|
||||
tst_Equals("1024 kb", "1 mb");
|
||||
tst_Equals("1.5 kb", "1536 b");
|
||||
} void tst_Equals(String lhs, String rhs) {Tfds.Eq(Io_size_.parse_or_(lhs, Long_.MinValue), Io_size_.parse_or_(rhs, Long_.MinValue));}
|
||||
public void Test_Equals(String lhs, String rhs) {Tfds.Eq(Io_size_.parse_or_(lhs, Long_.MinValue), Io_size_.parse_or_(rhs, Long_.MinValue));}
|
||||
public void Test_XtoStr(long val, String expd) {Tfds.Eq(expd, Io_size_.Xto_str(val));}
|
||||
public void Test_Xto_str(long val, int exp_1024, String val_fmt, String unit_pad, boolean round_0_to_1, String expd) {Tfds.Eq(expd, Io_size_.Xto_str(val, exp_1024, val_fmt, unit_pad, round_0_to_1));}
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ class XtoStrWkr_gplx implements XtoStrWkr {
|
||||
String rv = null;
|
||||
if (type == String.class) rv = String_.cast_(o);
|
||||
else if (Int_.TypeMatch(type)) return Int_.Xto_str(Int_.cast_(o));
|
||||
else if (Bool_.TypeMatch(type)) return Yn.Xto_str(Bool_.cast_(o));
|
||||
else if (ClassAdp_.Eq(type, Bool_.Cls_ref_type)) return Yn.Xto_str(Bool_.cast_(o));
|
||||
else if (type == DateAdp.class) return DateAdp_.cast_(o).XtoStr_gplx();
|
||||
else rv = Object_.Xto_str_strict_or_empty(o);
|
||||
return String_.Replace(rv, "'", "''");
|
||||
|
@ -41,7 +41,7 @@ class DataRdr_null implements DataRdr {
|
||||
public long ReadLong(String key) {return Long_.MinValue;} public long ReadLongOr(String key, long or) {return or;}
|
||||
public double ReadDouble(String key) {return Double_.NaN;} public double ReadDoubleOr(String key, double or) {return or;}
|
||||
public float ReadFloat(String key) {return Float_.NaN;} public float ReadFloatOr(String key, float or) {return or;}
|
||||
public byte ReadByte(String key) {return Byte_.MinValue;} public byte ReadByteOr(String key, byte or) {return or;}
|
||||
public byte ReadByte(String key) {return Byte_.Min_value;} public byte ReadByteOr(String key, byte or) {return or;}
|
||||
public DecimalAdp ReadDecimal(String key) {return DecimalAdp_.Zero;}public DecimalAdp ReadDecimalOr(String key, DecimalAdp or) {return or;}
|
||||
public DateAdp ReadDate(String key) {return DateAdp_.MinValue;} public DateAdp ReadDateOr(String key, DateAdp or) {return or;}
|
||||
public gplx.ios.Io_stream_rdr ReadRdr(String key) {return gplx.ios.Io_stream_rdr_.Null;}
|
||||
|
@ -116,9 +116,9 @@ class GfsCore_ {
|
||||
Object primitive = null;
|
||||
if (invk == null) { // rv is primitive; find appropriate mgr
|
||||
Class<?> type = rv.getClass();
|
||||
if (type == String.class) invk = String_.Gfs;
|
||||
else if (Int_.TypeMatch(type)) invk = Int_.Gfs;
|
||||
else if (Bool_.TypeMatch(type)) invk = Bool_.Gfs;
|
||||
if (type == String.class) invk = String_.Gfs;
|
||||
else if (Int_.TypeMatch(type)) invk = Int_.Gfs;
|
||||
else if (ClassAdp_.Eq(type, Bool_.Cls_ref_type)) invk = Bool_.Gfs;
|
||||
else throw Err_.new_("unknown primitive").Add("type", ClassAdp_.NameOf_type(type)).Add("obj", Object_.Xto_str_strict_or_null_mark(rv));
|
||||
primitive = rv;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src_100_core"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="src_110_dbQry"/>
|
||||
<classpathentry kind="src" path="src_120_sql"/>
|
||||
<classpathentry kind="src" path="src_130_misc"/>
|
||||
|
48
140_dbs/src/gplx/dbs/Db_conn.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn.java
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn {
|
||||
private final Db_engine engine;
|
||||
private final ListAdp itm_list = ListAdp_.new_();
|
||||
public Db_conn(Db_engine engine) {
|
||||
this.engine = engine;
|
||||
this.txn_mgr = new Db_txn_mgr_base(engine);
|
||||
}
|
||||
public Db_url Url() {return engine.Url();}
|
||||
public Db_txn_mgr Txn_mgr() {return txn_mgr;} private final Db_txn_mgr txn_mgr;
|
||||
public Db_stmt New_stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
|
||||
public Db_stmt New_stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, cols));}
|
||||
public Db_stmt New_stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
|
||||
public Db_stmt New_stmt_select_all_where(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols));}
|
||||
public void Itms_add(Db_conn_itm itm) {itm_list.Add(itm);}
|
||||
public void Itms_del(Db_conn_itm itm) {itm_list.Del(itm);}
|
||||
public void Conn_term() {
|
||||
int len = itm_list.Count();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_conn_itm itm = (Db_conn_itm)itm_list.FetchAt(i);
|
||||
itm.Conn_term();
|
||||
}
|
||||
engine.Conn_term();
|
||||
Db_conn_pool_old._.Del(this.Url()); // remove from pool, else rls'd instance will be cached and fail upon next use
|
||||
}
|
||||
public Db_stmt New_stmt(Db_qry qry) {return engine.New_stmt_prep(qry);}
|
||||
public int Exec_qry(Db_qry qry) {txn_mgr.Txn_count_(txn_mgr.Txn_count() + 1); return Int_.cast_(engine.Exec_as_obj(qry));}
|
||||
public DataRdr Exec_qry_as_rdr(Db_qry qry) {return DataRdr_.cast_(engine.Exec_as_obj(qry));}
|
||||
public int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
|
||||
public DataRdr Exec_sql_as_rdr(String sql) {return this.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));}
|
||||
}
|
48
140_dbs/src/gplx/dbs/Db_conn_.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn_.java
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn_ {
|
||||
public static final Db_conn Null = new_and_open_(Db_url_.Null);
|
||||
public static Db_conn new_and_open_(Db_url url) {
|
||||
Db_engine prototype = Db_engine_regy._.Get(url.Tid());
|
||||
Db_engine engine = prototype.New_clone(url);
|
||||
engine.Conn_open(); // auto-open
|
||||
return new Db_conn(engine);
|
||||
}
|
||||
public static Db_conn Reg_itm(Db_conn_itm itm, Db_conn old_conn, Db_conn new_conn) {
|
||||
if (old_conn != null) old_conn.Itms_del(itm);
|
||||
new_conn.Itms_add(itm);
|
||||
return new_conn;
|
||||
}
|
||||
public static int Select_fld0_as_int_or(Db_conn p, String sql, int or) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = p.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));
|
||||
int rv = or;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
Object rv_obj = rdr.ReadAt(0);
|
||||
if (rv_obj != null) // Max(fil_id) will be NULL if tbl is empty
|
||||
rv = Int_.cast_or_(rv_obj, or);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
finally {
|
||||
rdr.Rls();
|
||||
}
|
||||
}
|
||||
}
|
21
140_dbs/src/gplx/dbs/Db_conn_itm.java
Normal file
21
140_dbs/src/gplx/dbs/Db_conn_itm.java
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_conn_itm {
|
||||
void Conn_term();
|
||||
}
|
34
140_dbs/src/gplx/dbs/Db_conn_mkr.java
Normal file
34
140_dbs/src/gplx/dbs/Db_conn_mkr.java
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_conn_mkr {
|
||||
Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref);
|
||||
}
|
||||
class Db_conn_mkr_sqlite implements Db_conn_mkr {
|
||||
public Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref created_ref) {return Sqlite_engine_.Conn_load_or_make_(db_url, created_ref);}
|
||||
public static final Db_conn_mkr_sqlite _ = new Db_conn_mkr_sqlite(); Db_conn_mkr_sqlite() {}
|
||||
}
|
||||
class Db_conn_mkr_mem implements Db_conn_mkr {
|
||||
private boolean create;
|
||||
public Db_conn Load_or_make_(Io_url db_url, Bool_obj_ref create_ref) {create_ref.Val_(create); return null;}
|
||||
public static Db_conn_mkr_mem create_(boolean create) {
|
||||
Db_conn_mkr_mem rv = new Db_conn_mkr_mem();
|
||||
rv.create = create;
|
||||
return rv;
|
||||
}
|
||||
}
|
25
140_dbs/src/gplx/dbs/Db_conn_mkr_.java
Normal file
25
140_dbs/src/gplx/dbs/Db_conn_mkr_.java
Normal file
@ -0,0 +1,25 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn_mkr_ {
|
||||
public static final Db_conn_mkr
|
||||
Sqlite = Db_conn_mkr_sqlite._
|
||||
, Mem_create_y = Db_conn_mkr_mem.create_(Bool_.Y)
|
||||
, Mem_create_n = Db_conn_mkr_mem.create_(Bool_.N)
|
||||
;
|
||||
}
|
48
140_dbs/src/gplx/dbs/Db_conn_pool.java
Normal file
48
140_dbs/src/gplx/dbs/Db_conn_pool.java
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.mems.*;
|
||||
public class Db_conn_pool {
|
||||
private final HashAdp dbm_hash = HashAdp_.new_(); private final HashAdp engine_hash = HashAdp_.new_();
|
||||
Db_conn_pool() {this.Init();}
|
||||
public Db_conn Get_or_new__sqlite(Io_url url) {return Get_or_new(Db_url_.sqlite_(url));}
|
||||
public Db_conn Get_or_new(Db_url url) {
|
||||
Db_conn rv = (Db_conn)dbm_hash.Fetch(url.Xto_api());
|
||||
if (rv == null) {
|
||||
Db_engine prime = (Db_engine)engine_hash.Fetch(url.Tid()); if (prime == null) throw Err_.new_("db engine prototype not found; tid={0}", url.Tid());
|
||||
Db_engine clone = prime.New_clone(url);
|
||||
rv = new Db_conn(clone);
|
||||
dbm_hash.Add(url.Xto_api(), rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Set_mem(String db, Db_meta_tbl... tbls) {
|
||||
Db_url url = Db_url__mem.new_(db);
|
||||
Db_engine__mem engine = new Db_engine__mem(url, tbls);
|
||||
Db_conn dbm = new Db_conn(engine);
|
||||
dbm_hash.AddReplace(url.Xto_api(), dbm);
|
||||
}
|
||||
private void Init() {
|
||||
this.Engines__add(Db_engine_null._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
}
|
||||
public void Engines__add(Db_engine... ary) {
|
||||
for (Db_engine itm : ary)
|
||||
engine_hash.Add(itm.Tid(), itm);
|
||||
}
|
||||
public static final Db_conn_pool I = new Db_conn_pool();
|
||||
}
|
40
140_dbs/src/gplx/dbs/Db_conn_pool_old.java
Normal file
40
140_dbs/src/gplx/dbs/Db_conn_pool_old.java
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_conn_pool_old {
|
||||
private final OrderedHash hash = OrderedHash_.new_();
|
||||
public Db_conn Get_or_new(Db_url url) {return Get_or_new(url.Xto_raw());}
|
||||
public Db_conn Get_or_new(String raw) {
|
||||
Db_conn rv = (Db_conn)hash.Fetch(raw);
|
||||
if (rv == null) {
|
||||
rv = Db_conn_.new_and_open_(Db_url_.parse_(raw));
|
||||
hash.Add(raw, rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Del(Db_url url) {hash.Del(url.Xto_raw());}
|
||||
public void Clear() {
|
||||
int len = hash.Count();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Db_conn conn = (Db_conn)hash.FetchAt(0);
|
||||
conn.Conn_term();
|
||||
}
|
||||
hash.Clear();
|
||||
}
|
||||
public static final Db_conn_pool_old _ = new Db_conn_pool_old(); Db_conn_pool_old() {}
|
||||
}
|
31
140_dbs/src/gplx/dbs/Db_meta_fld.java
Normal file
31
140_dbs/src/gplx/dbs/Db_meta_fld.java
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_fld {
|
||||
public Db_meta_fld(String name, int tid, int len, boolean nullable, boolean primary, boolean autoincrement) {
|
||||
this.name = name; this.tid = tid; this.len = len;
|
||||
this.nullable = nullable; this.primary = primary; this.autoincrement = autoincrement;
|
||||
}
|
||||
public int Tid() {return tid;} private final int tid;
|
||||
public String Name() {return name;} private final String name;
|
||||
public int Len() {return len;} private final int len;
|
||||
public boolean Nullable() {return nullable;} private final boolean nullable;
|
||||
public boolean Primary() {return primary;} private final boolean primary;
|
||||
public boolean Autoincrement() {return autoincrement;} private final boolean autoincrement;
|
||||
public static final int Tid_bool = 0, Tid_byte = 1, Tid_short = 2, Tid_int = 3, Tid_long = 4, Tid_float = 5, Tid_double = 6, Tid_str = 7, Tid_text = 8, Tid_bry = 9;
|
||||
}
|
45
140_dbs/src/gplx/dbs/Db_meta_fld_list.java
Normal file
45
140_dbs/src/gplx/dbs/Db_meta_fld_list.java
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_fld_list {
|
||||
private final OrderedHash flds = OrderedHash_.new_();
|
||||
private final ListAdp keys = ListAdp_.new_();
|
||||
public Db_meta_fld Get_by(String name) {return (Db_meta_fld)flds.Fetch(name);}
|
||||
public String[] Xto_str_ary() {if (str_ary == null) str_ary = (String[])keys.Xto_ary(String.class); return str_ary;} private String[] str_ary;
|
||||
public Db_meta_fld[] Xto_fld_ary() {if (fld_ary == null) fld_ary = (Db_meta_fld[])flds.Xto_ary(Db_meta_fld.class); return fld_ary;} private Db_meta_fld[] fld_ary;
|
||||
public String Add_bool(String name) {return Add(name, Db_meta_fld.Tid_bool, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_byte(String name) {return Add(name, Db_meta_fld.Tid_byte, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_short(String name) {return Add(name, Db_meta_fld.Tid_short, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_int(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_int_pkey(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.N);}
|
||||
public String Add_int_pkey_autonum(String name) {return Add(name, Db_meta_fld.Tid_int, Len_null, Bool_.N, Bool_.Y, Bool_.Y);}
|
||||
public String Add_long(String name) {return Add(name, Db_meta_fld.Tid_long, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_float(String name) {return Add(name, Db_meta_fld.Tid_float, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_double(String name) {return Add(name, Db_meta_fld.Tid_double, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_str(String name, int len) {return Add(name, Db_meta_fld.Tid_str, len, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_text(String name) {return Add(name, Db_meta_fld.Tid_text, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add_bry(String name) {return Add(name, Db_meta_fld.Tid_bry, Len_null, Bool_.N, Bool_.N, Bool_.N);}
|
||||
public String Add(String name, int tid, int len, boolean nullable, boolean primary, boolean autoincrement) {
|
||||
Db_meta_fld fld = new Db_meta_fld(name, tid, len, nullable, primary, autoincrement);
|
||||
flds.Add(name, fld);
|
||||
keys.Add(name);
|
||||
return name;
|
||||
}
|
||||
private static final int Len_null = -1;
|
||||
public static Db_meta_fld_list new_() {return new Db_meta_fld_list();}
|
||||
}
|
27
140_dbs/src/gplx/dbs/Db_meta_idx.java
Normal file
27
140_dbs/src/gplx/dbs/Db_meta_idx.java
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_idx {
|
||||
public Db_meta_idx(String tbl, String name, boolean unique, String[] flds) {this.tbl = tbl; this.name = name; this.unique = unique; this.flds = flds;}
|
||||
public String Name() {return name;} private final String name;
|
||||
public boolean Unique() {return unique;} private final boolean unique;
|
||||
public String[] Flds() {return flds;} private final String[] flds;
|
||||
public String Tbl() {return tbl;} private final String tbl;
|
||||
public static Db_meta_idx new_unique(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, name, Bool_.Y, flds);}
|
||||
public static Db_meta_idx new_normal(String tbl, String name, String... flds) {return new Db_meta_idx(tbl, name, Bool_.N, flds);}
|
||||
}
|
28
140_dbs/src/gplx/dbs/Db_meta_tbl.java
Normal file
28
140_dbs/src/gplx/dbs/Db_meta_tbl.java
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_meta_tbl {
|
||||
public Db_meta_tbl(String name, Db_meta_fld[] flds, Db_meta_idx[] idxs) {
|
||||
this.name = name; this.flds = flds; this.idxs = idxs;
|
||||
}
|
||||
public String Name() {return name;} private final String name;
|
||||
public Db_meta_fld[] Flds() {return flds;} private final Db_meta_fld[] flds;
|
||||
public Db_meta_idx[] Idxs() {return idxs;} private final Db_meta_idx[] idxs;
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld[] flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds, idxs);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld... flds) {return new Db_meta_tbl(name, flds, null);}
|
||||
}
|
42
140_dbs/src/gplx/dbs/Db_rdr.java
Normal file
42
140_dbs/src/gplx/dbs/Db_rdr.java
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_rdr {
|
||||
boolean Move_next();
|
||||
byte[] Read_bry(int i);
|
||||
byte[] Read_bry(String k);
|
||||
byte[] Read_bry_by_str(int i);
|
||||
byte[] Read_bry_by_str(String k);
|
||||
String Read_str(int i);
|
||||
String Read_str(String k);
|
||||
byte Read_byte(int i);
|
||||
byte Read_byte(String k);
|
||||
int Read_int(int i);
|
||||
int Read_int(String k);
|
||||
long Read_long(int i);
|
||||
long Read_long(String k);
|
||||
float Read_float(int i);
|
||||
float Read_float(String k);
|
||||
double Read_double(int i);
|
||||
double Read_double(String k);
|
||||
DateAdp Read_date_by_str(int i);
|
||||
DateAdp Read_date_by_str(String k);
|
||||
boolean Read_bool_by_byte(int i);
|
||||
boolean Read_bool_by_byte(String k);
|
||||
void Rls();
|
||||
}
|
45
140_dbs/src/gplx/dbs/Db_rdr_.java
Normal file
45
140_dbs/src/gplx/dbs/Db_rdr_.java
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_rdr_ {
|
||||
public static final Db_rdr Null = new Db_rdr__null();
|
||||
}
|
||||
class Db_rdr__null implements Db_rdr {
|
||||
public boolean Move_next() {return false;}
|
||||
public byte[] Read_bry(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry(String k) {return Bry_.Empty;}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.Empty;}
|
||||
public byte Read_byte(int i) {return Byte_.Max_value_127;}
|
||||
public byte Read_byte(String k) {return Byte_.Max_value_127;}
|
||||
public String Read_str(int i) {return String_.Empty;}
|
||||
public String Read_str(String k) {return String_.Empty;}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.MinValue;}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.MinValue;}
|
||||
public int Read_int(int i) {return Int_.MinValue;}
|
||||
public int Read_int(String k) {return Int_.MinValue;}
|
||||
public long Read_long(int i) {return Long_.MinValue;}
|
||||
public long Read_long(String k) {return Long_.MinValue;}
|
||||
public float Read_float(int i) {return Float_.NaN;}
|
||||
public float Read_float(String k) {return Float_.NaN;}
|
||||
public double Read_double(int i) {return Double_.NaN;}
|
||||
public double Read_double(String k) {return Double_.NaN;}
|
||||
public boolean Read_bool_by_byte(int i) {return false;}
|
||||
public boolean Read_bool_by_byte(String k) {return false;}
|
||||
public void Rls() {}
|
||||
}
|
49
140_dbs/src/gplx/dbs/Db_rdr__basic.java
Normal file
49
140_dbs/src/gplx/dbs/Db_rdr__basic.java
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import java.sql.ResultSet;
|
||||
class Db_rdr__basic implements Db_rdr {
|
||||
protected ResultSet rdr;
|
||||
public void Ctor(ResultSet rdr, String sql) {this.rdr = rdr; this.sql = sql;}
|
||||
public String Sql() {return sql;} private String sql;
|
||||
public boolean Move_next() {
|
||||
try {return rdr.next();}
|
||||
catch (Exception e) {throw Err_.new_fmt_("move_next failed; check column casting error in SQL: err={0} sql={1}", Err_.Message_lang(e), sql);}
|
||||
}
|
||||
public byte[] Read_bry(int i) {try {return rdr.getBytes(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry(String k) {try {return (byte[])rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry_by_str(int i) {try {return Bry_.new_utf8_(rdr.getString(i + 1));} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public byte[] Read_bry_by_str(String k) {try {return Bry_.new_utf8_((String)rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public String Read_str(int i) {try {return rdr.getString(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public String Read_str(String k) {try {return (String)rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561(Read_str(i));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561(Read_str(k));}
|
||||
public int Read_int(int i) {try {return rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public int Read_int(String k) {try {return Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public long Read_long(int i) {try {return rdr.getLong(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public long Read_long(String k) {try {return Long_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public float Read_float(int i) {try {return rdr.getFloat(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public float Read_float(String k) {try {return Float_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public double Read_double(int i) {try {return rdr.getDouble(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public double Read_double(String k) {try {return Double_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(int i) {try {return rdr.getByte(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(String k) {try {return Byte_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public boolean Read_bool_by_byte(int i) {try {return rdr.getByte(i + 1) == 1;} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public boolean Read_bool_by_byte(String k) {try {return Byte_.cast_(rdr.getObject(k)) == 1;} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
public void Rls() {try {rdr.close();} catch (Exception e) {throw Err_.new_("close failed: err={0}", Err_.Message_lang(e));}}
|
||||
}
|
73
140_dbs/src/gplx/dbs/Db_sqlbldr__sqlite.java
Normal file
73
140_dbs/src/gplx/dbs/Db_sqlbldr__sqlite.java
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
interface Db_sqlbldr {}
|
||||
class Db_sqlbldr__sqlite implements Db_sqlbldr {
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.reset_(1024);
|
||||
public String Bld_create_idx(Db_meta_idx idx) {
|
||||
tmp_bfr.Add_str_ascii("CREATE ");
|
||||
if (idx.Unique())
|
||||
tmp_bfr.Add_str_ascii("UNIQUE ");
|
||||
tmp_bfr.Add_str_ascii("INDEX ");
|
||||
tmp_bfr.Add_str_ascii("IF NOT EXISTS ");
|
||||
tmp_bfr.Add_str_ascii(idx.Tbl()).Add_str_ascii("__").Add_str_ascii(idx.Name());
|
||||
tmp_bfr.Add_str_ascii(" (");
|
||||
String[] flds = idx.Flds();
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
String fld = flds[i];
|
||||
if (i != 0) tmp_bfr.Add_str_ascii(", ");
|
||||
tmp_bfr.Add_str_ascii(fld);
|
||||
}
|
||||
tmp_bfr.Add_str_ascii(");");
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
public String Bld_create_tbl(Db_meta_tbl tbl) {
|
||||
tmp_bfr.Add_str_ascii("CREATE TABLE ").Add_str_ascii(tbl.Name()).Add_byte_nl();
|
||||
Db_meta_fld[] flds = tbl.Flds();
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
Db_meta_fld fld = flds[i];
|
||||
tmp_bfr.Add_byte(i == 0 ? Byte_ascii.Paren_bgn : Byte_ascii.Comma).Add_byte_space();
|
||||
tmp_bfr.Add_str_ascii(fld.Name()).Add_byte_space();
|
||||
Tid_to_sql(tmp_bfr, fld.Tid(), fld.Len()); tmp_bfr.Add_byte_space();
|
||||
tmp_bfr.Add_str_ascii(fld.Nullable() ? "NULL " : "NOT NULL ");
|
||||
if (fld.Primary()) tmp_bfr.Add_str_ascii("PRIMARY KEY ");
|
||||
if (fld.Autoincrement()) tmp_bfr.Add_str_ascii("AUTOINCREMENT ");
|
||||
tmp_bfr.Del_by_1(); // remove trailing space
|
||||
tmp_bfr.Add_byte_nl();
|
||||
}
|
||||
tmp_bfr.Add_str_ascii(");");
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
public void Tid_to_sql(Bry_bfr tmp_bfr, int tid, int len) {// REF: https://www.sqlite.org/datatype3.html
|
||||
switch (tid) {
|
||||
case Db_meta_fld.Tid_bool: tmp_bfr.Add_str_ascii("boolean"); break;
|
||||
case Db_meta_fld.Tid_byte: tmp_bfr.Add_str_ascii("tinyint"); break;
|
||||
case Db_meta_fld.Tid_short: tmp_bfr.Add_str_ascii("smallint"); break;
|
||||
case Db_meta_fld.Tid_int: tmp_bfr.Add_str_ascii("int"); break;
|
||||
case Db_meta_fld.Tid_long: tmp_bfr.Add_str_ascii("bigint"); break;
|
||||
case Db_meta_fld.Tid_float: tmp_bfr.Add_str_ascii("float"); break;
|
||||
case Db_meta_fld.Tid_double: tmp_bfr.Add_str_ascii("double"); break;
|
||||
case Db_meta_fld.Tid_str: tmp_bfr.Add_str_ascii("varchar(").Add_int_variable(len).Add_byte(Byte_ascii.Paren_end); break;
|
||||
case Db_meta_fld.Tid_text: tmp_bfr.Add_str_ascii("text"); break;
|
||||
case Db_meta_fld.Tid_bry: tmp_bfr.Add_str_ascii("blob"); break;
|
||||
default: throw Err_.unhandled(tid);
|
||||
}
|
||||
}
|
||||
}
|
64
140_dbs/src/gplx/dbs/Db_sqlbldr_tst.java
Normal file
64
140_dbs/src/gplx/dbs/Db_sqlbldr_tst.java
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_sqlbldr_tst {
|
||||
@Before public void setup() {} private final Db_sqlbldr_fxt fxt = new Db_sqlbldr_fxt();
|
||||
@Test public void Idx_unique() {
|
||||
fxt.Test_create_idx(Db_meta_idx.new_unique("tbl_name", "idx_name", "fld_1", "fld_2")
|
||||
, "CREATE UNIQUE INDEX IF NOT EXISTS tbl_name__idx_name (fld_1, fld_2);"
|
||||
);
|
||||
}
|
||||
@Test public void Tbl_basic() {
|
||||
Db_meta_fld_list flds = Db_meta_fld_list.new_();
|
||||
flds.Add_int_pkey("fld_int_pkey");
|
||||
flds.Add_bool("fld_bool");
|
||||
flds.Add_short("fld_short");
|
||||
flds.Add_int("fld_int");
|
||||
flds.Add_long("fld_long");
|
||||
flds.Add_float("fld_float");
|
||||
flds.Add_double("fld_double");
|
||||
flds.Add_str("fld_str", 123);
|
||||
flds.Add_text("fld_text");
|
||||
flds.Add_bry("fld_bry");
|
||||
fxt.Test_create_tbl(Db_meta_tbl.new_("tbl_name", flds.Xto_fld_ary())
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "CREATE TABLE tbl_name"
|
||||
, "( fld_int_pkey int NOT NULL PRIMARY KEY"
|
||||
, ", fld_bool boolean NOT NULL"
|
||||
, ", fld_short smallint NOT NULL"
|
||||
, ", fld_int int NOT NULL"
|
||||
, ", fld_long bigint NOT NULL"
|
||||
, ", fld_float float NOT NULL"
|
||||
, ", fld_double double NOT NULL"
|
||||
, ", fld_str varchar(123) NOT NULL"
|
||||
, ", fld_text text NOT NULL"
|
||||
, ", fld_bry blob NOT NULL"
|
||||
, ");"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Db_sqlbldr_fxt {
|
||||
private Db_sqlbldr__sqlite sqlbldr = new Db_sqlbldr__sqlite();
|
||||
public void Test_create_idx(Db_meta_idx idx, String expd) {
|
||||
Tfds.Eq(expd, sqlbldr.Bld_create_idx(idx));
|
||||
}
|
||||
public void Test_create_tbl(Db_meta_tbl tbl, String expd) {
|
||||
Tfds.Eq_str_lines(expd, sqlbldr.Bld_create_tbl(tbl));
|
||||
}
|
||||
}
|
40
140_dbs/src/gplx/dbs/Db_sys_regy_mgr.java
Normal file
40
140_dbs/src/gplx/dbs/Db_sys_regy_mgr.java
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
class Db_sys_regy_mgr {
|
||||
private Db_sys_regy_tbl tbl;
|
||||
public Db_sys_regy_mgr(Db_url url, String name) {tbl = new Db_sys_regy_tbl(url, name);}
|
||||
public void Set(String grp, String key, String val) {
|
||||
if (tbl.Select_val_or(grp, key, null) == null)
|
||||
tbl.Insert(grp, key, val);
|
||||
else
|
||||
tbl.Update(grp, key, val);
|
||||
}
|
||||
public void Del(String grp, String key) {
|
||||
tbl.Delete(grp, key);
|
||||
}
|
||||
public String Get_val_as_str_or(String grp, String key, String or) {
|
||||
return tbl.Select_val_or(grp, key, or);
|
||||
}
|
||||
}
|
||||
class Db_sys_regy_itm {
|
||||
public Db_sys_regy_itm(String grp, String key, String val) {this.grp = grp; this.key = key; this.val = val;}
|
||||
public String Grp() {return grp;} private final String grp;
|
||||
public String Key() {return key;} private final String key;
|
||||
public String Val() {return val;} private final String val;
|
||||
}
|
54
140_dbs/src/gplx/dbs/Db_sys_regy_mgr_tst.java
Normal file
54
140_dbs/src/gplx/dbs/Db_sys_regy_mgr_tst.java
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_sys_regy_mgr_tst {
|
||||
@Before public void init() {fxt.Init();} private Db_sys_regy_mgr_fxt fxt = new Db_sys_regy_mgr_fxt();
|
||||
@Test public void Insert() {
|
||||
fxt .Exec_set("grp", "key", "val_0")
|
||||
.Test_get("grp", "key", "val_0");
|
||||
}
|
||||
@Test public void Update() {
|
||||
fxt .Exec_set("grp", "key", "val_0")
|
||||
.Exec_set("grp", "key", "val_1")
|
||||
.Test_get("grp", "key", "val_1");
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt .Exec_set("grp", "key_0", "val_0")
|
||||
.Exec_set("grp", "key_1", "val_0")
|
||||
.Exec_del("grp", "key_1")
|
||||
.Test_get("grp", "key_0", null)
|
||||
.Test_get("grp", "key_1", null)
|
||||
;
|
||||
}
|
||||
}
|
||||
class Db_sys_regy_mgr_fxt {
|
||||
private Db_sys_regy_mgr sys_regy_mgr;
|
||||
public void Init() {
|
||||
if (sys_regy_mgr == null) {
|
||||
Db_conn_pool.I.Set_mem("test_db", Db_sys_regy_tbl.new_meta("test_regy"));
|
||||
sys_regy_mgr = new Db_sys_regy_mgr(Db_url_.mem_("test_db"), "test_regy");
|
||||
}
|
||||
}
|
||||
public Db_sys_regy_mgr_fxt Exec_set(String grp, String key, String val) {sys_regy_mgr.Set(grp, key, val); return this;}
|
||||
public Db_sys_regy_mgr_fxt Exec_del(String grp, String key) {sys_regy_mgr.Del(grp, key); return this;}
|
||||
public Db_sys_regy_mgr_fxt Test_get(String grp, String key, String expd_val) {
|
||||
Tfds.Eq(expd_val, sys_regy_mgr.Get_val_as_str_or(grp, key, null));
|
||||
return this;
|
||||
}
|
||||
}
|
61
140_dbs/src/gplx/dbs/Db_sys_regy_tbl.java
Normal file
61
140_dbs/src/gplx/dbs/Db_sys_regy_tbl.java
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
class Db_sys_regy_tbl implements Db_conn_itm {
|
||||
private final String tbl_name;
|
||||
private Db_conn conn;
|
||||
public void Conn_term() {}
|
||||
public Db_sys_regy_tbl(Db_url url, String tbl_name) {
|
||||
this.tbl_name = tbl_name;
|
||||
// this.meta = Db_sys_regy_tbl.new_meta(tbl_name);
|
||||
this.conn = Db_conn_pool.I.Get_or_new(url);
|
||||
conn.Itms_add(this);
|
||||
}
|
||||
// private Db_meta_tbl meta;
|
||||
public void Insert(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.New_stmt_insert(tbl_name, Flds.Xto_str_ary());
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Val_str(val).Exec_insert();
|
||||
}
|
||||
public void Update(String grp, String key, String val) {
|
||||
Db_stmt stmt = conn.New_stmt_update(tbl_name, String_.Ary(Fld_regy_grp, Fld_regy_key), Fld_regy_val);
|
||||
stmt.Clear().Val_str(val).Val_str(grp).Val_str(key).Exec_update();
|
||||
}
|
||||
public void Delete(String grp, String key) {
|
||||
Db_stmt stmt = conn.New_stmt_delete(tbl_name, Fld_regy_grp, Fld_regy_key);
|
||||
stmt.Clear().Val_str(grp).Val_str(key).Exec_delete();
|
||||
}
|
||||
public String Select_val_or(String grp, String key, String or) {
|
||||
Db_stmt stmt = conn.New_stmt_select_all_where(tbl_name, Flds.Xto_str_ary(), Fld_regy_grp, Fld_regy_key);
|
||||
Db_rdr rdr = Db_rdr_.Null;
|
||||
try {
|
||||
rdr = stmt.Clear().Val_str(grp).Val_str(key).Exec_select_as_rdr();
|
||||
return rdr.Move_next() ? rdr.Read_str(Fld_regy_val) : or;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
private static final Db_meta_fld_list Flds = Db_meta_fld_list.new_();
|
||||
private static final String
|
||||
Fld_regy_grp = Flds.Add_str("regy_grp", 1024)
|
||||
, Fld_regy_key = Flds.Add_str("regy_key", 1024)
|
||||
, Fld_regy_val = Flds.Add_str("regy_val", 4096)
|
||||
;
|
||||
public static Db_meta_tbl new_meta(String tbl) {
|
||||
return Db_meta_tbl.new_(tbl, Flds.Xto_fld_ary()
|
||||
, Db_meta_idx.new_unique(tbl, "key", Flds.Xto_str_ary())
|
||||
);
|
||||
}
|
||||
}
|
95
140_dbs/src/gplx/dbs/Db_url.java
Normal file
95
140_dbs/src/gplx/dbs/Db_url.java
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_url {
|
||||
String Tid();
|
||||
String Xto_raw();
|
||||
String Xto_api();
|
||||
Db_url New_self(String raw, GfoMsg m);
|
||||
}
|
||||
class Db_url__null extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "null_db";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {return this;}
|
||||
public static final Db_url__null _ = new Db_url__null(); Db_url__null() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
}
|
||||
class Db_url__mysql extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mysql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "uid", uid
|
||||
, "pwd", pwd
|
||||
, "charset", "utf8"
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__mysql rv = new Db_url__mysql();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("charset", "utf8")));
|
||||
rv.uid = m.ReadStr("uid");
|
||||
rv.pwd = m.ReadStr("pwd");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__mysql _ = new Db_url__mysql(); Db_url__mysql() {}
|
||||
}
|
||||
class Db_url__postgres extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "postgresql";
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
public static Db_url new_(String server, String database, String uid, String pwd) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "server", server
|
||||
, "database", database
|
||||
, "port", "5432"
|
||||
, "user id", uid
|
||||
, "password", pwd
|
||||
, "encoding", "unicode" // needed for 1.1 conn; otherwise, ascii
|
||||
));
|
||||
}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__postgres rv = new Db_url__postgres();
|
||||
rv.Ctor(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("encoding", "unicode")));
|
||||
rv.uid = m.ReadStr("user id");
|
||||
rv.pwd = m.ReadStr("password");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__postgres _ = new Db_url__postgres(); Db_url__postgres() {}
|
||||
}
|
||||
class Db_url__tdb extends Db_url__base {
|
||||
public Io_url Url() {return url;} Io_url url;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "tdb";
|
||||
public static Db_url new_(Io_url url) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "url", url.Raw()
|
||||
));
|
||||
} Db_url__tdb() {}
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__tdb rv = new Db_url__tdb();
|
||||
String urlStr = m.ReadStr("url");
|
||||
Io_url url = Io_url_.new_any_(urlStr);
|
||||
rv.Ctor(urlStr, url.NameOnly(), raw, BldApi(m));
|
||||
rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url__tdb _ = new Db_url__tdb();
|
||||
}
|
55
140_dbs/src/gplx/dbs/Db_url_.java
Normal file
55
140_dbs/src/gplx/dbs/Db_url_.java
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.mems.*;
|
||||
public class Db_url_ {
|
||||
public static final Db_url Null = Db_url__null._;
|
||||
public static final Db_url Test = Db_url__mysql.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static Db_url parse_(String raw) {return Db_url_pool._.Parse(raw);}
|
||||
public static Db_url sqlite_(Io_url url) {return Db_url__sqlite.load_(url);}
|
||||
public static Db_url tdb_(Io_url url) {return Db_url__tdb.new_(url);}
|
||||
public static Db_url mem_(String db) {return Db_url__mem.new_(db);}
|
||||
public static final String Key_tdb = Db_url__tdb.Tid_const;
|
||||
}
|
||||
class Db_url_pool {
|
||||
private OrderedHash regy = OrderedHash_.new_();
|
||||
public Db_url_pool() {
|
||||
this.Add(Db_url__null._).Add(Db_url__tdb._).Add(Db_url__mysql._).Add(Db_url__postgres._).Add(Db_url__sqlite._);
|
||||
this.Add(Db_url__mem.I);
|
||||
}
|
||||
public Db_url_pool Add(Db_url itm) {regy.AddReplace(itm.Tid(), itm); return this;}
|
||||
public Db_url Parse(String raw) {// assume each pair has format of: name=val;
|
||||
try {
|
||||
GfoMsg m = GfoMsg_.new_parse_("db_url");
|
||||
String[] terms = String_.Split(raw, ";");
|
||||
String url_tid = "";
|
||||
for (String term : terms) {
|
||||
if (String_.Len(term) == 0) continue;
|
||||
String[] kv = String_.Split(term, "=");
|
||||
if (String_.Eq(kv[0], "gplx_key"))
|
||||
url_tid = kv[1]; // NOTE: do not add to GfoMsg; will not be part of ApiStr
|
||||
else
|
||||
m.Add(kv[0], kv[1]);
|
||||
}
|
||||
Db_url prototype = (Db_url)regy.Fetch(url_tid);
|
||||
return prototype.New_self(raw, m);
|
||||
}
|
||||
catch(Exception exc) {throw Err_.parse_type_exc_(exc, Db_url.class, raw);}
|
||||
}
|
||||
public static final Db_url_pool _ = new Db_url_pool();
|
||||
}
|
51
140_dbs/src/gplx/dbs/Db_url__base.java
Normal file
51
140_dbs/src/gplx/dbs/Db_url__base.java
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public abstract class Db_url__base implements Db_url {
|
||||
public abstract String Tid();
|
||||
public String Xto_raw() {return raw;} private String raw = "";
|
||||
public String Xto_api() {return api;} private String api = "";
|
||||
public String Database() {return database;} private String database = "";
|
||||
public String Server() {return server;} private String server = "";
|
||||
public abstract Db_url New_self(String raw, GfoMsg m);
|
||||
protected void Ctor(String server, String database, String raw, String api) {this.server = server; this.database = database; this.raw = raw; this.api = api;}
|
||||
protected static String BldApi(GfoMsg m, KeyVal... xtnAry) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
HashAdp hash = HashAdp_.new_();
|
||||
for (int i = 0; i < m.Args_count(); i++) {
|
||||
KeyVal kv = m.Args_getAt(i);
|
||||
sb.Add_fmt("{0}={1};", kv.Key(), kv.Val_to_str_or_empty());
|
||||
hash.AddKeyVal(kv.Key());
|
||||
}
|
||||
for (KeyVal xtn : xtnAry) {
|
||||
if (hash.Has(xtn.Key())) continue;
|
||||
sb.Add_fmt("{0}={1};", xtn.Key(), xtn.Val_to_str_or_empty());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
protected static String Bld_raw(String... ary) {
|
||||
Bry_bfr bfr = Bry_bfr.reset_(255);
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
String itm = ary[i];
|
||||
bfr.Add_str_utf8(itm);
|
||||
bfr.Add_byte(i % 2 == 0 ? Byte_ascii.Eq : Byte_ascii.Semic);
|
||||
}
|
||||
return bfr.Xto_str();
|
||||
}
|
||||
}
|
46
140_dbs/src/gplx/dbs/Db_url__sqlite.java
Normal file
46
140_dbs/src/gplx/dbs/Db_url__sqlite.java
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_url__sqlite extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "sqlite";
|
||||
public Io_url Url() {return url;} private Io_url url;
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__sqlite rv = new Db_url__sqlite();
|
||||
String url = m.ReadStr("data source");
|
||||
rv.url = Io_url_.new_any_(url);
|
||||
rv.Ctor("", url, raw, BldApi(m, KeyVal_.new_("version", "3")));
|
||||
return rv;
|
||||
}
|
||||
public static Db_url load_(Io_url url) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
, "data source" , url.Xto_api()
|
||||
, "version" , "3"
|
||||
));
|
||||
}
|
||||
public static Db_url make_(Io_url url) {
|
||||
Io_mgr._.CreateDirIfAbsent(url.OwnerDir());
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key" , Tid_const
|
||||
, "data source" , url.Xto_api()
|
||||
, "version" , "3"
|
||||
|
||||
));
|
||||
}
|
||||
public static final Db_url__sqlite _ = new Db_url__sqlite(); Db_url__sqlite() {}
|
||||
}
|
46
140_dbs/src/gplx/dbs/Db_url_tst.java
Normal file
46
140_dbs/src/gplx/dbs/Db_url_tst.java
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_url_tst {
|
||||
@Before public void setup() {
|
||||
regy.Add(Db_url_mock._);
|
||||
} private final Db_url_pool regy = new Db_url_pool();
|
||||
@Test public void Parse() {
|
||||
tst_Parse("gplx_key=mock;id=1;", kv_("id", "1")); // one; gplx_key removed
|
||||
tst_Parse("gplx_key=mock;id=1;name=me;", kv_("id", "1"), kv_("name", "me")); // many
|
||||
tst_Parse("gplx_key=mock;id=1;name=me" , kv_("id", "1"), kv_("name", "me")); // no semi-colon at end
|
||||
}
|
||||
private KeyVal kv_(String key, Object val) {return KeyVal_.new_(key, val);}
|
||||
private void tst_Parse(String raw, KeyVal... expd) {
|
||||
Db_url_mock mock = (Db_url_mock)regy.Parse(raw);
|
||||
Tfds.Eq_ary_str(expd, mock.Kvs());
|
||||
}
|
||||
}
|
||||
class Db_url_mock extends Db_url__base {
|
||||
public KeyVal[] Kvs() {return kvs;} KeyVal[] kvs;
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mock";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url_mock rv = new Db_url_mock();
|
||||
rv.kvs = new KeyVal[m.Args_count()];
|
||||
for (int i = 0; i < m.Args_count(); i++)
|
||||
rv.kvs[i] = m.Args_getAt(i);
|
||||
return rv;
|
||||
}
|
||||
public static final Db_url_mock _ = new Db_url_mock(); Db_url_mock() {}
|
||||
}
|
67
140_dbs/src/gplx/dbs/Sql_join_itm.java
Normal file
67
140_dbs/src/gplx/dbs/Sql_join_itm.java
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Sql_join_itm {
|
||||
public String SrcTbl() {return srcTbl;} public Sql_join_itm SrcTbl_(String v) {srcTbl = v; return this;} private String srcTbl;
|
||||
public String SrcFld() {return srcFld;} public Sql_join_itm SrcFld_(String v) {srcFld = v; return this;} private String srcFld;
|
||||
public String TrgFld() {return trgFld;} public Sql_join_itm TrgFld_(String v) {trgFld = v; return this;} private String trgFld;
|
||||
public String TrgFldOrSrcFld() {return trgFld == null ? srcFld : trgFld;}
|
||||
public static Sql_join_itm new_(String trgFld, String srcTbl, String srcFld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = trgFld; rv.srcTbl = srcTbl; rv.srcFld = srcFld;
|
||||
return rv;
|
||||
} Sql_join_itm() {}
|
||||
public static Sql_join_itm same_(String tbl, String fld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = fld; rv.srcTbl = tbl; rv.srcFld = fld;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
class Sql_from {
|
||||
public ListAdp Tbls() {return tbls;} ListAdp tbls = ListAdp_.new_();
|
||||
public Sql_tbl_src BaseTable() {return (Sql_tbl_src)tbls.FetchAt(0);}
|
||||
|
||||
public static Sql_from new_(Sql_tbl_src baseTable) {
|
||||
Sql_from rv = new Sql_from();
|
||||
rv.tbls.Add(baseTable);
|
||||
return rv;
|
||||
} Sql_from() {}
|
||||
}
|
||||
class Sql_tbl_src {
|
||||
public Sql_join_itmType JoinType() {return type;} public Sql_tbl_src JoinType_(Sql_join_itmType v) {this.type = v; return this;} Sql_join_itmType type = Sql_join_itmType.Inner;
|
||||
public ListAdp JoinLinks() {return joinLinks;} ListAdp joinLinks = ListAdp_.new_();
|
||||
public String TblName() {return tblName;} public Sql_tbl_src TblName_(String s) {tblName = s; return this;} private String tblName;
|
||||
public String Alias() {return alias;} public Sql_tbl_src Alias_(String s) {alias = s; return this;} private String alias;
|
||||
public void XtoSql(String_bldr sb) {
|
||||
sb.Add_many(tblName, alias == null ? "" : " " + alias);
|
||||
}
|
||||
public static Sql_tbl_src new_() {return new Sql_tbl_src();} Sql_tbl_src() {}
|
||||
}
|
||||
class Sql_join_itmType {
|
||||
public int Val() {return val;} int val;
|
||||
public String Name() {return name;} private String name;
|
||||
Sql_join_itmType(int v, String name) {this.val = v; this.name = name;}
|
||||
public static final Sql_join_itmType
|
||||
From = new Sql_join_itmType(0, "FROM")
|
||||
, Inner = new Sql_join_itmType(1, "INNER JOIN")
|
||||
, Left = new Sql_join_itmType(2, "LEFT JOIN")
|
||||
, Right = new Sql_join_itmType(3, "RIGHT JOIN")
|
||||
, Outer = new Sql_join_itmType(4, "OUTER JOIN")
|
||||
, Cross = new Sql_join_itmType(5, "CROSS JOIN")
|
||||
;
|
||||
}
|
73
140_dbs/src/gplx/dbs/Sql_order_by.java
Normal file
73
140_dbs/src/gplx/dbs/Sql_order_by.java
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.lists.*; /*ComparerAble*/
|
||||
class Sql_order_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_order_by new_(Sql_order_by_itm... ary) {
|
||||
Sql_order_by rv = new Sql_order_by();
|
||||
for (Sql_order_by_itm itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_order_by() {}
|
||||
}
|
||||
class Sql_group_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_group_by new_(String... ary) {
|
||||
Sql_group_by rv = new Sql_group_by();
|
||||
for (String itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_group_by() {}
|
||||
}
|
||||
class Sql_order_by_itm {
|
||||
public String Name() {return name;} private String name;
|
||||
public boolean Ascending() {return ascending;} private boolean ascending;
|
||||
public String XtoSql() {
|
||||
String ascString = ascending ? "" : " DESC";
|
||||
return name + ascString;
|
||||
}
|
||||
public static Sql_order_by_itm new_(String name, boolean ascending) {
|
||||
Sql_order_by_itm rv = new Sql_order_by_itm();
|
||||
rv.name = name; rv.ascending = ascending;
|
||||
return rv;
|
||||
} Sql_order_by_itm() {}
|
||||
}
|
||||
class Sql_order_by_sorter implements ComparerAble {
|
||||
public int compare(Object lhsObj, Object rhsObj) {
|
||||
GfoNde lhs = (GfoNde)lhsObj; GfoNde rhs = (GfoNde)rhsObj;
|
||||
Sql_order_by_itm item = null; Object lhsData = null, rhsData = null;
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
item = items[i];
|
||||
lhsData = lhs.Read(item.Name()); rhsData = rhs.Read(item.Name());
|
||||
int compare = CompareAble_.Compare_obj(lhsData, rhsData);
|
||||
if (compare == CompareAble_.Same) continue;
|
||||
int ascendingVal = item.Ascending() ? 1 : -1;
|
||||
return compare * ascendingVal;
|
||||
}
|
||||
return CompareAble_.Same;
|
||||
}
|
||||
Sql_order_by_itm[] items;
|
||||
public static ComparerAble new_(Sql_order_by_itm[] items) {
|
||||
Sql_order_by_sorter rv = new Sql_order_by_sorter();
|
||||
rv.items = items;
|
||||
return rv;
|
||||
}
|
||||
}
|
22
140_dbs/src/gplx/dbs/Sql_qry_wtr.java
Normal file
22
140_dbs/src/gplx/dbs/Sql_qry_wtr.java
Normal file
@ -0,0 +1,22 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
public interface Sql_qry_wtr {
|
||||
String Xto_str(Db_qry qry, boolean prepare);
|
||||
}
|
24
140_dbs/src/gplx/dbs/Sql_qry_wtr_.java
Normal file
24
140_dbs/src/gplx/dbs/Sql_qry_wtr_.java
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Sql_qry_wtr_ {
|
||||
public static Sql_qry_wtr new_ansi() {return new Sql_qry_wtr_ansi();}
|
||||
public static Sql_qry_wtr new_escape_backslash() {return new Sql_qry_wtr_ansi_escape_backslash();}
|
||||
public static final Sql_qry_wtr I = new Sql_qry_wtr_ansi();
|
||||
public static String Gen_placeholder_parameters(Db_qry qry) {return Sql_qry_wtr_.I.Xto_str(qry, true);} // replace arguments with ?; EX: UPDATE a SET b = ? WHERE c = ?;
|
||||
}
|
286
140_dbs/src/gplx/dbs/Sql_qry_wtr_ansi.java
Normal file
286
140_dbs/src/gplx/dbs/Sql_qry_wtr_ansi.java
Normal file
@ -0,0 +1,286 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
private final String_bldr sb = String_bldr_.new_();
|
||||
private boolean prepare = false;
|
||||
public String Xto_str(Db_qry cmd, boolean prepare) {
|
||||
this.prepare = prepare;
|
||||
switch (cmd.Tid()) {
|
||||
case Db_qry_.Tid_insert: return Bld_qry_insert((Db_qry_insert)cmd);
|
||||
case Db_qry_.Tid_delete: return Bld_qry_delete((Db_qry_delete)cmd);
|
||||
case Db_qry_.Tid_update: return Bld_qry_update((Db_qry_update)cmd);
|
||||
case Db_qry_.Tid_select_in_tbl:
|
||||
case Db_qry_.Tid_select: return Bld_qry_select((Db_qry_select)cmd);
|
||||
case Db_qry_.Tid_sql: return ((Db_qry_sql)cmd).Xto_sql();
|
||||
default: throw Err_.unhandled(cmd.Tid());
|
||||
}
|
||||
}
|
||||
private String Bld_qry_delete(Db_qry_delete cmd) {
|
||||
sb.Add_many("DELETE FROM ", cmd.Base_table());
|
||||
Bld_where2(sb, cmd.Where());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_insert(Db_qry_insert cmd) {
|
||||
if (cmd.Select() != null) {
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < cmd.Cols().Count(); i++) {
|
||||
Sql_select_fld_base fld = cmd.Cols().FetchAt(i);
|
||||
sb.Add(fld.Alias());
|
||||
sb.Add(i == cmd.Cols().Count() - 1 ? ") " : ", ");
|
||||
}
|
||||
sb.Add(Bld_qry_select(cmd.Select()));
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
int arg_count = cmd.Args().Count(); if (arg_count == 0) throw Err_.new_("Db_qry_insert has no columns").Add("base_table", cmd.Base_table());
|
||||
int last = arg_count - 1;
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
sb.Add(" VALUES (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
Db_arg prm = (Db_arg)pair.Val();
|
||||
this.Bld_val(sb, prm);
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_update(Db_qry_update cmd) {
|
||||
int arg_count = cmd.Args().Count(); if (arg_count == 0) throw Err_.new_("Db_qry_update has no columns").Add("base_table", cmd.Base_table());
|
||||
sb.Add_many("UPDATE ", cmd.Base_table(), " SET ");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add("=");
|
||||
this.Bld_val(sb, (Db_arg)pair.Val());
|
||||
}
|
||||
Bld_where(sb, cmd.Where());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_select(Db_qry_select cmd) {
|
||||
sb.Add("SELECT ");
|
||||
if (cmd.Cols().Distinct()) sb.Add("DISTINCT ");
|
||||
Sql_select_fld_list flds = cmd.Cols().Flds();
|
||||
if (flds.Count() == 0) sb.Add("*");
|
||||
for (int i = 0; i < flds.Count(); i++) {
|
||||
Sql_select_fld_base fld = (Sql_select_fld_base)flds.FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, fld.XtoSql());
|
||||
}
|
||||
Bld_clause_from(sb, cmd.From());
|
||||
Bld_where(sb, cmd.Where());
|
||||
Bld_select_group_by(sb, cmd.GroupBy());
|
||||
Bld_select_order_by(sb, cmd.OrderBy());
|
||||
Bld_select_limit(sb, cmd.Limit());
|
||||
return sb.Xto_str_and_clear();
|
||||
}
|
||||
private void Bld_select_group_by(String_bldr sb, Sql_group_by groupBy) {
|
||||
if (groupBy == null) return;
|
||||
sb.Add(" GROUP BY ");
|
||||
for (int i = 0; i < groupBy.Flds().Count(); i++) {
|
||||
String item = (String)groupBy.Flds().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item);
|
||||
}
|
||||
}
|
||||
private void Bld_select_order_by(String_bldr sb, Sql_order_by orderBy) {
|
||||
if (orderBy == null) return;
|
||||
sb.Add(" ORDER BY ");
|
||||
for (int i = 0; i < orderBy.Flds().Count(); i++) {
|
||||
Sql_order_by_itm item = (Sql_order_by_itm)orderBy.Flds().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item.XtoSql());
|
||||
}
|
||||
}
|
||||
private void Bld_select_limit(String_bldr sb, int limit) {
|
||||
if (limit == Db_qry_select.Limit_disabled) return;
|
||||
sb.Add(" LIMIT ").Add(limit);
|
||||
}
|
||||
private void Bld_clause_from(String_bldr sb, Sql_from from) {
|
||||
for (Object tblObj : from.Tbls()) {
|
||||
Sql_tbl_src tbl = (Sql_tbl_src)tblObj;
|
||||
sb.Add_many
|
||||
( " ", String_.Upper(tbl.JoinType().Name()), " ", tbl.TblName(), String_.FormatOrEmptyStrIfNull(" {0}", tbl.Alias())
|
||||
);
|
||||
String tblAliasForJoin = tbl.Alias() == null ? tbl.TblName() : tbl.Alias();
|
||||
for (int i = 0; i < tbl.JoinLinks().Count(); i++) {
|
||||
Sql_join_itm joinLink = (Sql_join_itm)tbl.JoinLinks().FetchAt(i);
|
||||
String conjunction = i == 0 ? " ON " : " AND ";
|
||||
sb.Add_many
|
||||
( conjunction, joinLink.SrcTbl(), ".", joinLink.SrcFld(), "=", tblAliasForJoin, ".", joinLink.TrgFldOrSrcFld()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void XtoSqlCol(String_bldr sb, Object obj) {
|
||||
if (obj == null) throw Err_.null_("ColName");
|
||||
sb.Add_obj(obj); // FIXME: options for bracketing; ex: [name]
|
||||
}
|
||||
public void Bld_val(String_bldr sb, Db_arg arg) {
|
||||
if (prepare) {
|
||||
sb.Add("?");
|
||||
return;
|
||||
}
|
||||
Object val = arg.Val();
|
||||
if (val == null) {
|
||||
sb.Add("NULL");
|
||||
return;
|
||||
}
|
||||
Class<?> val_type = val.getClass();
|
||||
if (val_type == Bool_.Cls_ref_type)
|
||||
sb.Add_obj(Bool_.Xto_int(Bool_.cast_(val))); // NOTE: save boolean to 0 or 1, b/c (a) db may not support bit datatype (sqllite) and (b) avoid i18n issues with "true"/"false"
|
||||
else if
|
||||
( val_type == Byte_.Cls_ref_type || val_type == Short_.Cls_ref_type
|
||||
|| val_type == Int_.Cls_ref_type || val_type == Long_.Cls_ref_type
|
||||
|| val_type == Float_.Cls_ref_type || val_type == Double_.Cls_ref_type
|
||||
)
|
||||
sb.Add(Object_.Xto_str_strict_or_null(val));
|
||||
else if (val_type == DateAdp.class)
|
||||
Bld_val_date(sb, arg, (DateAdp)val);
|
||||
else if (val_type == DecimalAdp.class) {
|
||||
DecimalAdp valDecimal = (DecimalAdp)val;
|
||||
sb.Add(valDecimal.Xto_str());
|
||||
}
|
||||
else {
|
||||
String valString = Object_.Xto_str_strict_or_null(val);
|
||||
Bld_val_str(sb, arg, valString);
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_str(String_bldr sb, Db_arg prm, String s) {
|
||||
sb.Add_many("'", String_.Replace(s, "'", "''"), "'"); // stupid escaping of '
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_date(String_bldr sb, Db_arg prm, DateAdp s) {
|
||||
sb.Add_many("'", s.XtoStr_gplx_long(), "'");
|
||||
}
|
||||
private void Bld_where(String_bldr sb, Sql_where where) {
|
||||
if (where == null || where.Crt() == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where(sb, where.Crt());
|
||||
}
|
||||
private void Bld_where2(String_bldr sb, Criteria crt) {
|
||||
if (crt == null || crt == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where(sb, crt);
|
||||
}
|
||||
public void Bld_where(String_bldr sb, Criteria crt) {
|
||||
Criteria_bool_base crt_bool = Criteria_bool_base.as_(crt);
|
||||
if (crt_bool != null) {
|
||||
sb.Add("(");
|
||||
Bld_where(sb, crt_bool.Lhs());
|
||||
sb.Add_many(" ", crt_bool.OpLiteral(), " ");
|
||||
Bld_where(sb, crt_bool.Rhs());
|
||||
sb.Add(")");
|
||||
return;
|
||||
}
|
||||
if (crt.Crt_tid() == Criteria_.Tid_db_obj_ary) {
|
||||
Append_db_obj_ary(sb, (Db_obj_ary_crt)crt);
|
||||
}
|
||||
else {
|
||||
Criteria_wrapper leaf = Criteria_wrapper.as_(crt); if (leaf == null) throw Err_.invalid_op_(crt.XtoStr());
|
||||
sb.Add(leaf.Name_of_GfoFldCrt());
|
||||
Bld_where_crt(sb, leaf.Crt_of_GfoFldCrt());
|
||||
}
|
||||
}
|
||||
private void Bld_where_crt(String_bldr sb, Criteria crt) {
|
||||
switch (crt.Crt_tid()) {
|
||||
case Criteria_.Tid_eq: Bld_where_eq(sb, Criteria_eq.as_(crt)); break;
|
||||
case Criteria_.Tid_comp: Bld_where_comp(sb, Criteria_comp.as_(crt)); break;
|
||||
case Criteria_.Tid_between: Bld_where_between(sb, Criteria_between.as_(crt)); break;
|
||||
case Criteria_.Tid_in: Bld_where_in(sb, Criteria_in.as_(crt)); break;
|
||||
case Criteria_.Tid_like: Bld_where_like(sb, Criteria_like.as_(crt)); break;
|
||||
case Criteria_.Tid_iomatch: Bld_where_iomatch(sb, Criteria_ioMatch.as_(crt)); break;
|
||||
default: throw Err_.unhandled(crt);
|
||||
}
|
||||
}
|
||||
private void Bld_where_eq(String_bldr sb, Criteria_eq crt) {
|
||||
sb.Add(crt.Negated() ? "!=" : "=");
|
||||
this.Bld_val(sb, Wrap(crt.Value()));
|
||||
}
|
||||
private void Bld_where_comp(String_bldr sb, Criteria_comp crt) {
|
||||
sb.Add_many(crt.XtoSymbol());
|
||||
this.Bld_val(sb, Wrap(crt.Value()));
|
||||
}
|
||||
private void Bld_where_between(String_bldr sb, Criteria_between crt) {
|
||||
sb.Add(crt.Negated() ? " NOT BETWEEN " : " BETWEEN ");
|
||||
this.Bld_val(sb, Wrap(crt.Lhs()));
|
||||
sb.Add(" AND ");
|
||||
this.Bld_val(sb, Wrap(crt.Rhs()));
|
||||
}
|
||||
private void Bld_where_like(String_bldr sb, Criteria_like crt) {
|
||||
sb.Add(crt.Negated() ? " NOT LIKE " : " LIKE ");
|
||||
this.Bld_val(sb, Wrap(crt.Pattern().Raw()));
|
||||
sb.Add_fmt(" ESCAPE '{0}'", crt.Pattern().Escape());
|
||||
}
|
||||
private void Bld_where_in(String_bldr sb, Criteria_in crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IN (" : " IN (");
|
||||
Object[] crt_vals = crt.Values();
|
||||
int len = crt_vals.length;
|
||||
int last = len - 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Object val = crt_vals[i];
|
||||
this.Bld_val(sb, Wrap(val));
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
}
|
||||
private void Bld_where_iomatch(String_bldr sb, Criteria_ioMatch crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IOMATCH " : " IOMATCH ");
|
||||
this.Bld_val(sb, Wrap(crt.Pattern().Raw()));
|
||||
}
|
||||
public void Append_db_obj_ary(String_bldr sb, Db_obj_ary_crt crt) {
|
||||
Object[][] ary = crt.Vals();
|
||||
int ary_len = ary.length;
|
||||
Db_fld[] flds = crt.Flds();
|
||||
for (int i = 0; i < ary_len; i++) {
|
||||
Object[] itm = (Object[])ary[i];
|
||||
int itm_len = itm.length;
|
||||
if (i != 0) sb.Add(" OR ");
|
||||
sb.Add("(");
|
||||
for (int j = 0; j < itm_len; j++) {
|
||||
if (j != 0) sb.Add(" AND ");
|
||||
Db_fld fld = flds[j];
|
||||
Object val = itm[j];
|
||||
boolean quote = false;
|
||||
switch (fld.Type_tid()) {
|
||||
case ClassAdp_.Tid_str:
|
||||
case ClassAdp_.Tid_char:
|
||||
case ClassAdp_.Tid_date:
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
sb.Add(fld.Name());
|
||||
sb.Add("=");
|
||||
if (quote) sb.Add("'");
|
||||
sb.Add(Object_.Xto_str_strict_or_empty(val));
|
||||
if (quote) sb.Add("'");
|
||||
}
|
||||
sb.Add(")");
|
||||
}
|
||||
}
|
||||
private Db_arg Wrap(Object val) {return new Db_arg("unknown", val);}
|
||||
}
|
||||
class Sql_qry_wtr_ansi_escape_backslash extends Sql_qry_wtr_ansi { @Override public void Bld_val_str(String_bldr sb, Db_arg prm, String s) {
|
||||
if (String_.Has(s, "\\")) s = String_.Replace(s, "\\", "\\\\");
|
||||
super.Bld_val_str(sb, prm, s);
|
||||
}
|
||||
}
|
61
140_dbs/src/gplx/dbs/Sql_qry_wtr_tst.java
Normal file
61
140_dbs/src/gplx/dbs/Sql_qry_wtr_tst.java
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class Sql_qry_wtr_tst {
|
||||
private final Sql_qry_wtr_fxt fxt = new Sql_qry_wtr_fxt();
|
||||
@Test public void Val() {
|
||||
fxt.Test_val(null , "NULL");
|
||||
fxt.Test_val(true , "1");
|
||||
fxt.Test_val(false , "0");
|
||||
fxt.Test_val(1 , "1");
|
||||
fxt.Test_val(1.1 , "1.1");
|
||||
fxt.Test_val("a" , "'a'");
|
||||
fxt.Test_val("a'b" , "'a''b'");
|
||||
}
|
||||
@Test public void Where_basic() {
|
||||
fxt.Test_where(Db_crt_.eq_("id", 1), "id=1");
|
||||
fxt.Test_where(Db_crt_.eqn_("id", 1), "id!=1");
|
||||
fxt.Test_where(Db_crt_.mt_("id", 1), "id>1");
|
||||
fxt.Test_where(Db_crt_.mte_("id", 1), "id>=1");
|
||||
fxt.Test_where(Db_crt_.lt_("id", 1), "id<1");
|
||||
fxt.Test_where(Db_crt_.lte_("id", 1), "id<=1");
|
||||
fxt.Test_where(Db_crt_.between_("id", 1, 2), "id BETWEEN 1 AND 2");
|
||||
fxt.Test_where(Db_crt_.in_("id", 1, 2, 3), "id IN (1, 2, 3)");
|
||||
}
|
||||
@Test public void Where_and() {
|
||||
fxt.Test_where(Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 AND name='me')");
|
||||
fxt.Test_where(Criteria_.Or(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 OR name='me')");
|
||||
fxt.Test_where(Criteria_.Or(Db_crt_.eq_("id", 1), Criteria_.And(Db_crt_.eq_("name", "me"), Db_crt_.eq_("id", 1))), "(id=1 OR (name='me' AND id=1))");
|
||||
}
|
||||
}
|
||||
class Sql_qry_wtr_fxt {
|
||||
private final Sql_qry_wtr_ansi sql_wtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.new_ansi();
|
||||
public void Test_val(Object val, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Db_arg arg = new Db_arg("not needed", val);
|
||||
sql_wtr.Bld_val(sb, arg);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
public void Test_where(Criteria crt, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sql_wtr.Bld_where(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
}
|
143
140_dbs/src/gplx/dbs/Sql_select.java
Normal file
143
140_dbs/src/gplx/dbs/Sql_select.java
Normal file
@ -0,0 +1,143 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
class Sql_select {
|
||||
public Sql_select_fld_list Flds() {return flds;} Sql_select_fld_list flds = Sql_select_fld_list.new_();
|
||||
public boolean Distinct() {return distinct;} public void Distinct_set(boolean v) {distinct = v;} private boolean distinct;
|
||||
public void Add(String fldName) {flds.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fldName, fldName));}
|
||||
public void Add(String fldName, String alias) {flds.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fldName, alias));}
|
||||
public void Add(Sql_select_fld_base fld) {flds.Add(fld);}
|
||||
|
||||
public static final Sql_select All = all_(); static Sql_select all_() {Sql_select rv = new_(); rv.Add(Sql_select_fld_wild._); return rv;}
|
||||
public static Sql_select new_() {return new Sql_select();} Sql_select() {}
|
||||
}
|
||||
abstract class Sql_select_fld_base {
|
||||
public String Tbl() {return tbl;} public void Tbl_set(String val) {tbl = val;} private String tbl;
|
||||
public String Fld() {return fld;} public void Fld_set(String val) {fld = val;} private String fld;
|
||||
public String Alias() {return alias;} public void Alias_set(String val) {alias = val;} private String alias;
|
||||
public ClassXtn ValType() {return valType;} public void ValType_set(ClassXtn val) {valType = val;} ClassXtn valType = ObjectClassXtn._;
|
||||
public abstract Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type);
|
||||
@gplx.Virtual public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@gplx.Virtual public boolean Type_fld() {return true;}
|
||||
public abstract String XtoSql();
|
||||
public static final String Tbl_null = null;
|
||||
@gplx.Internal protected void ctor_(String tbl, String fld, String alias) {
|
||||
Tbl_set(tbl); Fld_set(fld); Alias_set(alias);
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_wild extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {throw Err_.new_("group by eval not allowed on *");}
|
||||
@Override public void GroupBy_type(GfoFld fld) {throw Err_.new_("group by type not allowed on *");}
|
||||
@Override public String XtoSql() {return "*";}
|
||||
public static final Sql_select_fld_wild _ = new Sql_select_fld_wild(); Sql_select_fld_wild() {this.ctor_(Tbl_null, "*", "*");}
|
||||
}
|
||||
class Sql_select_fld_fld extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {return curVal;}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@Override public String XtoSql() {
|
||||
String rv = Fld();
|
||||
if (Tbl() != Tbl_null)
|
||||
rv = Tbl() + "." + Fld();
|
||||
if (!String_.Eq(Alias(), Fld()))
|
||||
rv = rv + " AS " + Alias();
|
||||
return rv;
|
||||
}
|
||||
public static Sql_select_fld_fld new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_fld rv = new Sql_select_fld_fld();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_fld() {}
|
||||
}
|
||||
abstract class Sql_select_fld_func_base extends Sql_select_fld_base {
|
||||
public abstract String XtoSql_functionName();
|
||||
@Override public boolean Type_fld() {return false;}
|
||||
@Override public String XtoSql() {
|
||||
return String_.Format("{0}({1}) AS {2}", XtoSql_functionName(), Fld(), Alias());
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_count extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "COUNT";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return 1;
|
||||
return Int_.cast_(groupByVal) + 1;
|
||||
}
|
||||
public static Sql_select_fld_count new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_count rv = new Sql_select_fld_count();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_count() {}
|
||||
}
|
||||
class Sql_select_fld_sum extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "SUM";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return Int_.cast_(curVal);
|
||||
return Int_.cast_(groupByVal) + Int_.cast_(curVal);
|
||||
}
|
||||
public static Sql_select_fld_sum new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_sum rv = new Sql_select_fld_sum();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_sum() {}
|
||||
}
|
||||
class Sql_select_fld_minMax extends Sql_select_fld_func_base {
|
||||
int compareType = CompareAble_.Less;
|
||||
@Override public String XtoSql_functionName() {return compareType == CompareAble_.Less ? "MIN" : "MAX";}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return curVal;
|
||||
int compareVal = CompareAble_.Compare_obj(curVal, groupByVal);
|
||||
return compareVal * compareType > 0 ? curVal : groupByVal;
|
||||
}
|
||||
public static Sql_select_fld_minMax min_(String tbl, String fld, String alias) {return new_(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld_minMax max_(String tbl, String fld, String alias) {return new_(CompareAble_.More, tbl, fld, alias);}
|
||||
static Sql_select_fld_minMax new_(int compareType, String tbl, String fld, String alias) {
|
||||
Sql_select_fld_minMax rv = new Sql_select_fld_minMax();
|
||||
rv.compareType = compareType;
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_minMax() {}
|
||||
}
|
||||
class Sql_select_fld_list {
|
||||
public int Count() {return hash.Count();}
|
||||
public void Add(Sql_select_fld_base fld) {hash.Add(fld.Alias(), fld);}
|
||||
public Sql_select_fld_base FetchAt(int i) {return (Sql_select_fld_base)hash.FetchAt(i);}
|
||||
public Sql_select_fld_base FetchOrNull(String k) {return (Sql_select_fld_base)hash.Fetch(k);}
|
||||
public GfoFldList XtoGfoFldLst(TdbTable tbl) {
|
||||
GfoFldList rv = GfoFldList_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base selectFld = this.FetchAt(i);
|
||||
GfoFld fld = tbl.Flds().FetchOrNull(selectFld.Fld());
|
||||
if (fld == null) throw Err_.new_("fld not found in tbl").Add("fldName", selectFld.Fld()).Add("tblName", tbl.Name()).Add("tblFlds", tbl.Flds().XtoStr());
|
||||
if (rv.Has(selectFld.Alias())) throw Err_.new_("alias is not unique").Add("fldName", selectFld.Fld()).Add("flds", rv.XtoStr());
|
||||
selectFld.GroupBy_type(fld);
|
||||
rv.Add(selectFld.Alias(), selectFld.ValType());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String XtoStr() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
sb.Add_fmt("{0},{1}|", fld.Fld(), fld.Alias());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
OrderedHash hash = OrderedHash_.new_();
|
||||
public static Sql_select_fld_list new_() {return new Sql_select_fld_list();} Sql_select_fld_list() {}
|
||||
}
|
32
140_dbs/src/gplx/dbs/Sql_where.java
Normal file
32
140_dbs/src/gplx/dbs/Sql_where.java
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
class Sql_where {
|
||||
public Criteria Crt() {return crt;} Criteria crt;
|
||||
public static Sql_where merge_or_new_(Sql_where where, Criteria crt) {
|
||||
return where == null
|
||||
? Sql_where.new_(crt)
|
||||
: Sql_where.new_(Criteria_.And(where.Crt(), crt));
|
||||
}
|
||||
public static Sql_where new_(Criteria crt) {
|
||||
Sql_where rv = new Sql_where();
|
||||
rv.crt = crt;
|
||||
return rv;
|
||||
} Sql_where() {}
|
||||
}
|
46
140_dbs/src/gplx/dbs/engines/mems/Db_engine__mem.java
Normal file
46
140_dbs/src/gplx/dbs/engines/mems/Db_engine__mem.java
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_engine__mem implements Db_engine {
|
||||
private final HashAdp tbl_hash = HashAdp_.new_();
|
||||
public Db_engine__mem(Db_url url, Db_meta_tbl[] meta_tbls) {
|
||||
this.url = url;
|
||||
int tbls_len = meta_tbls.length;
|
||||
for (int i = 0; i < tbls_len; ++i) {
|
||||
Db_meta_tbl meta_tbl = meta_tbls[i];
|
||||
Mem_tbl tbl = new Mem_tbl();
|
||||
tbl_hash.Add(meta_tbl.Name(), tbl);
|
||||
}
|
||||
}
|
||||
public String Tid() {return Db_url__mem.Tid_const;}
|
||||
public Db_url Url() {return url;} private Db_url url;
|
||||
public Db_engine New_clone(Db_url url) {return this;}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return new Db_stmt__mem(this, qry);}
|
||||
public Mem_tbl Tbls_get(String name) {return (Mem_tbl)tbl_hash.Fetch(name);}
|
||||
public void Txn_bgn() {++txn_count;} private int txn_count = 0;
|
||||
public void Txn_end() {--txn_count;}
|
||||
public Object Exec_as_obj(Db_qry qry) {throw Err_.not_implemented_();}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {
|
||||
if (txn_count != 0) throw Err_.new_("Conn_term.txns still open; txn_count={0}", txn_count);
|
||||
}
|
||||
public Db_rdr New_rdr_by_obj(Object rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {throw Err_.not_implemented_();}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public static final Db_engine__mem _ = new Db_engine__mem(); Db_engine__mem() {}
|
||||
}
|
57
140_dbs/src/gplx/dbs/engines/mems/Db_rdr__mem.java
Normal file
57
140_dbs/src/gplx/dbs/engines/mems/Db_rdr__mem.java
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_rdr__mem implements Db_rdr {
|
||||
private final Mem_itm[] rows; private int row_idx = -1; private final int rows_len;
|
||||
private final HashAdp ords = HashAdp_.new_();
|
||||
private Mem_itm row;
|
||||
public Db_rdr__mem(String[] cols, Mem_itm[] rows) {
|
||||
this.rows = rows; this.rows_len = rows.length;
|
||||
int cols_len = cols.length;
|
||||
for (int i = 0; i < cols_len; ++i)
|
||||
ords.Add(cols[i], i);
|
||||
}
|
||||
public boolean Move_next() {
|
||||
boolean rv = ++row_idx < rows_len;
|
||||
if (rv)
|
||||
row = rows[row_idx];
|
||||
return rv;
|
||||
}
|
||||
public byte[] Read_bry(int i) {return (byte[])row.Get_at(i);}
|
||||
public byte[] Read_bry(String k) {return (byte[])row.Get_at(Ord_by_key(k));}
|
||||
public String Read_str(int i) {return (String)row.Get_at(i);}
|
||||
public String Read_str(String k) {return (String)row.Get_at(Ord_by_key(k));}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.new_utf8_((String)row.Get_at(i));}
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.new_utf8_((String)row.Get_at(Ord_by_key(k)));}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561((String)row.Get_at(i));}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561((String)row.Get_at(Ord_by_key(k)));}
|
||||
public byte Read_byte(int i) {return Byte_.cast_(row.Get_at(i));}
|
||||
public byte Read_byte(String k) {return Byte_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public int Read_int(int i) {return Int_.cast_(row.Get_at(i));}
|
||||
public int Read_int(String k) {return Int_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public long Read_long(int i) {return Long_.cast_(row.Get_at(i));}
|
||||
public long Read_long(String k) {return Long_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public float Read_float(int i) {return Float_.cast_(row.Get_at(i));}
|
||||
public float Read_float(String k) {return Float_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public double Read_double(int i) {return Double_.cast_(row.Get_at(i));}
|
||||
public double Read_double(String k) {return Double_.cast_(row.Get_at(Ord_by_key(k)));}
|
||||
public boolean Read_bool_by_byte(int i) {return Byte_.cast_(row.Get_at(i)) == 1;}
|
||||
public boolean Read_bool_by_byte(String k) {return Byte_.cast_(row.Get_at(Ord_by_key(k))) == 1;}
|
||||
public void Rls() {}
|
||||
private int Ord_by_key(String k) {return Int_.cast_(ords.Fetch(k));}
|
||||
}
|
127
140_dbs/src/gplx/dbs/engines/mems/Db_stmt__mem.java
Normal file
127
140_dbs/src/gplx/dbs/engines/mems/Db_stmt__mem.java
Normal file
@ -0,0 +1,127 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_stmt__mem implements Db_stmt {
|
||||
private int val_idx;
|
||||
public Db_stmt__mem(Db_engine__mem engine, Db_qry qry) {this.engine = engine; this.qry = qry;} private Db_engine__mem engine;
|
||||
public int Args_len() {return args.Count();}
|
||||
public Object Args_get_at(int i) {return args.FetchAt(i);}
|
||||
public Db_qry Qry() {return qry;} private Db_qry qry;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
this.Clear();
|
||||
}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_str(String k, String v) {return Add_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
gplx.ios.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(++val_idx, bfr.Xto_str_and_clear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Insert(this) == 1;
|
||||
}
|
||||
public int Exec_update() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Update(this);
|
||||
}
|
||||
public int Exec_delete() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Delete(this);
|
||||
}
|
||||
public DataRdr Exec_select() {throw Err_.not_implemented_();}
|
||||
public Db_rdr Exec_select_as_rdr() {
|
||||
Mem_tbl tbl = engine.Tbls_get(qry.Base_table());
|
||||
return tbl.Select(this);
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec select_val: tbl={0}", qry.Base_table());}
|
||||
}
|
||||
private void Add(int idx, Object v) {args.Add(v);} private ListAdp args = ListAdp_.new_();
|
||||
}
|
33
140_dbs/src/gplx/dbs/engines/mems/Db_url__mem.java
Normal file
33
140_dbs/src/gplx/dbs/engines/mems/Db_url__mem.java
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Db_url__mem extends Db_url__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "mem";
|
||||
@Override public Db_url New_self(String raw, GfoMsg m) {
|
||||
Db_url__mem rv = new Db_url__mem();
|
||||
rv.Ctor("", m.ReadStr("database"), raw, raw);
|
||||
return rv;
|
||||
}
|
||||
public static Db_url new_(String database) {
|
||||
return Db_url_.parse_(Bld_raw
|
||||
( "gplx_key", Tid_const
|
||||
, "database", database
|
||||
));
|
||||
}
|
||||
public static final Db_url__mem I = new Db_url__mem(); Db_url__mem() {}
|
||||
}
|
29
140_dbs/src/gplx/dbs/engines/mems/Mem_itm.java
Normal file
29
140_dbs/src/gplx/dbs/engines/mems/Mem_itm.java
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Mem_itm implements GfoInvkAble {
|
||||
private final OrderedHash hash = OrderedHash_.new_();
|
||||
public Object Get_by(String key) {return hash.Fetch(key);}
|
||||
public Object Get_at(int i) {return hash.FetchAt(i);}
|
||||
public void Set_by(String key, Object val) {hash.AddReplace(key, val);}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
Object rv = Get_by(k);
|
||||
if (rv == null) return GfoInvkAble_.Rv_unhandled;
|
||||
return rv;
|
||||
}
|
||||
}
|
91
140_dbs/src/gplx/dbs/engines/mems/Mem_tbl.java
Normal file
91
140_dbs/src/gplx/dbs/engines/mems/Mem_tbl.java
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.criterias.*;
|
||||
public class Mem_tbl {
|
||||
private final ListAdp rows = ListAdp_.new_(); private final ListAdp where_rows = ListAdp_.new_();
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
Db_qry_insert qry = (Db_qry_insert)stmt.Qry();
|
||||
String[] cols = qry.Cols_for_insert(); int len = cols.length;
|
||||
Mem_itm itm = new Mem_itm();
|
||||
for (int i = 0; i < len; ++i)
|
||||
itm.Set_by(cols[i], stmt.Args_get_at(i));
|
||||
rows.Add(itm);
|
||||
return 1;
|
||||
}
|
||||
public int Update(Db_stmt__mem stmt) {
|
||||
Db_qry_update qry = (Db_qry_update)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Cols_where());
|
||||
int where_rows_len = where_rows.Count();
|
||||
String[] update_cols = qry.Cols_update(); int update_cols_len = update_cols.length;
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)where_rows.FetchAt(i);
|
||||
for (int j = 0; j < update_cols_len; ++j)
|
||||
itm.Set_by(update_cols[j], stmt.Args_get_at(j));
|
||||
}
|
||||
return where_rows_len;
|
||||
}
|
||||
public int Delete(Db_stmt__mem stmt) {
|
||||
Db_qry_delete qry = (Db_qry_delete)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Where_cols());
|
||||
int where_rows_len = where_rows.Count();
|
||||
for (int i = 0; i < where_rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)where_rows.FetchAt(i);
|
||||
rows.Del(itm);
|
||||
}
|
||||
return where_rows_len;
|
||||
}
|
||||
public Db_rdr Select(Db_stmt__mem stmt) {
|
||||
Db_qry__select_in_tbl qry = (Db_qry__select_in_tbl)stmt.Qry();
|
||||
Select_rows_where(where_rows, stmt, qry.Where_flds());
|
||||
return new Db_rdr__mem(qry.Select_flds(), (Mem_itm[])where_rows.Xto_ary_and_clear(Mem_itm.class));
|
||||
}
|
||||
private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, String[] where_cols) {
|
||||
int where_len = where_cols.length;
|
||||
KeyVal[] where_kvs = Bld_where_kvs(stmt, where_cols, where_len);
|
||||
Bld_where_rows(where_rows, where_kvs, where_len);
|
||||
}
|
||||
private KeyVal[] Bld_where_kvs(Db_stmt__mem stmt, String[] keys, int keys_len) {
|
||||
KeyVal[] rv = new KeyVal[keys_len];
|
||||
for (int i = 0; i < keys_len; ++i)
|
||||
rv[i] = KeyVal_.new_(keys[i], stmt.Args_get_at(i));
|
||||
return rv;
|
||||
}
|
||||
private void Bld_where_rows(ListAdp rv, KeyVal[] where_kvs, int where_len) {
|
||||
rv.Clear();
|
||||
int rows_len = rows.Count();
|
||||
for (int i = 0; i < rows_len; ++i) {
|
||||
Mem_itm itm = (Mem_itm)rows.FetchAt(i);
|
||||
for (int j = 0; j < where_len; ++j) {
|
||||
KeyVal kv = where_kvs[j];
|
||||
Object itm_val = itm.Get_by(kv.Key());
|
||||
if (!Object_.Eq(itm_val, kv.Val())) break;
|
||||
}
|
||||
rv.Add(itm);
|
||||
}
|
||||
}
|
||||
// private void Select_rows_where(ListAdp rv, Db_stmt__mem stmt, Criteria crt) {
|
||||
// rv.Clear();
|
||||
// int rows_len = rows.Count();
|
||||
// for (int i = 0; i < rows_len; ++i) {
|
||||
// Mem_itm itm = (Mem_itm)rows.FetchAt(i);
|
||||
// if (crt.Matches(itm))
|
||||
// rv.Add(itm);
|
||||
// }
|
||||
// }
|
||||
}
|
19
140_dbs/src/gplx/dbs/sqls/Sql_dummy_class.java
Normal file
19
140_dbs/src/gplx/dbs/sqls/Sql_dummy_class.java
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
public class Sql_dummy_class {}
|
24
140_dbs/src_110_dbQry/gplx/dbs/Db_arg.java
Normal file
24
140_dbs/src_110_dbQry/gplx/dbs/Db_arg.java
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_arg {
|
||||
@gplx.Internal protected Db_arg(String key, Object val) {this.key = key; this.val = val;}
|
||||
public String Key() {return key;} private String key;
|
||||
public Object Val() {return val;} public void Val_(Object v) {this.val = v;} private Object val;
|
||||
public byte Val_tid() {return val_tid;} public Db_arg Val_tid_(byte v) {val_tid = v; return this;} private byte val_tid = Db_val_type.Tid_null;
|
||||
}
|
@ -17,8 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_qry {
|
||||
int Tid();
|
||||
String KeyOfDb_qry();
|
||||
boolean ExecRdrAble();
|
||||
String XtoSql();
|
||||
int Tid();
|
||||
boolean Exec_is_rdr();
|
||||
String Base_table();
|
||||
String Xto_sql();
|
||||
}
|
||||
|
@ -22,11 +22,11 @@ public class Db_qry_ {
|
||||
public static Db_qry_select select_val_(String tbl, String col, Criteria crt) {return select_().From_(tbl).Where_(crt).Cols_(col);}
|
||||
public static Db_qry_select select_tbl_(String tbl) {return select_().From_(tbl);}
|
||||
public static Db_qry_select select_() {return Db_qry_select.new_();}
|
||||
public static Db_qry_delete delete_(String tbl, Criteria crt) {return Db_qry_delete.new_().BaseTable_(tbl).Where_(crt);}
|
||||
public static Db_qry_delete delete_tbl_(String tbl) {return Db_qry_delete.new_().BaseTable_(tbl);}
|
||||
public static Db_qry_insert insert_(String tbl) {return Db_qry_insert.new_().BaseTable_(tbl);}
|
||||
public static Db_qry_delete delete_(String tbl, Criteria crt) {return Db_qry_delete.new_(tbl, crt);}
|
||||
public static Db_qry_delete delete_tbl_(String tbl) {return Db_qry_delete.new_(tbl);}
|
||||
public static Db_qry_insert insert_(String tbl) {return new Db_qry_insert(tbl);}
|
||||
public static Db_qry_insert insert_common_(String tbl, KeyVal... pairs) {
|
||||
Db_qry_insert cmd = Db_qry_insert.new_().BaseTable_(tbl);
|
||||
Db_qry_insert cmd = new Db_qry_insert(tbl);
|
||||
for (KeyVal pair : pairs)
|
||||
cmd.Arg_obj_(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
@ -47,14 +47,5 @@ public class Db_qry_ {
|
||||
}
|
||||
public static final Object WhereAll = null;
|
||||
public static Db_qry as_(Object obj) {return obj instanceof Db_qry ? (Db_qry)obj : null;}
|
||||
public static final int Tid_basic = 0, Tid_select_in_tbl = 1;
|
||||
}
|
||||
interface Db_qryWkr {
|
||||
Object Exec(Db_engine engine, Db_qry cmd);
|
||||
}
|
||||
class Db_qryWkr_ {
|
||||
public static final Db_qryWkr Null = new Db_qryWrk_null();
|
||||
}
|
||||
class Db_qryWrk_null implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engine, Db_qry cmd) {return null;}
|
||||
public static final int Tid_insert = 0, Tid_delete = 1, Tid_update = 2, Tid_select = 3, Tid_sql = 4, Tid_select_in_tbl = 5, Tid_flush = 6;
|
||||
}
|
||||
|
@ -20,8 +20,10 @@ public class Db_qry__select_in_tbl implements Db_qry {
|
||||
public Db_qry__select_in_tbl(String tbl_name, String[] select_flds, String[] where_flds, String group_by_sql, String having_sql, String order_by_sql, String limit_sql) {
|
||||
this.tbl_name = tbl_name; this.select_flds = select_flds; this.where_flds = where_flds; this.group_by_sql = group_by_sql; this.having_sql = having_sql; this.order_by_sql = order_by_sql; this.limit_sql = limit_sql;
|
||||
}
|
||||
public int Tid() {return Db_qry_.Tid_select_in_tbl;}
|
||||
public String Tbl_name() {return tbl_name;} private final String tbl_name;
|
||||
public int Tid() {return Db_qry_.Tid_select_in_tbl;}
|
||||
public boolean Exec_is_rdr() {return true;}
|
||||
public String Base_table() {return tbl_name;} private final String tbl_name;
|
||||
public String From() {return tbl_name;}
|
||||
public String[] Select_flds() {return select_flds;} private final String[] select_flds;
|
||||
public String[] Where_flds() {return where_flds;} private final String[] where_flds;
|
||||
public void Where_sql(String_bldr sb) {
|
||||
@ -37,9 +39,6 @@ public class Db_qry__select_in_tbl implements Db_qry {
|
||||
public String Having_sql() {return having_sql;} private final String having_sql;
|
||||
public String Order_by_sql() {return order_by_sql;} public Db_qry__select_in_tbl Order_by_sql_(String v) {order_by_sql = v; return this;} private String order_by_sql;
|
||||
public String Limit_sql() {return limit_sql;} private final String limit_sql;
|
||||
public static Db_qry__select_in_tbl new_(String tbl_name, String[] where_flds, String[] select_flds) {return new Db_qry__select_in_tbl(tbl_name, select_flds, where_flds, null, null, null, null);}
|
||||
public String KeyOfDb_qry() {return "select_in_tbl";}
|
||||
public boolean ExecRdrAble() {return true;}
|
||||
public String XtoSql() {return Xto_sql();}
|
||||
public String Xto_sql() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
@ -57,5 +56,9 @@ public class Db_qry__select_in_tbl implements Db_qry {
|
||||
if (limit_sql != null) sb.Add(limit_sql);
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public static Db_qry__select_in_tbl new_(String tbl_name, String[] where_flds, String[] select_flds) {
|
||||
Db_qry__select_in_tbl rv = new Db_qry__select_in_tbl(tbl_name, select_flds, where_flds, null, null, null, null);
|
||||
return rv;
|
||||
}
|
||||
public static final String[] Where_flds__all = null;
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_qry_arg_owner extends Db_qry {
|
||||
Db_qry_arg_owner From_(String tbl);
|
||||
|
||||
Db_qry_arg_owner Key_arg_(String k, int v);
|
||||
Db_qry_arg_owner Key_arg_(String k, String v);
|
||||
Db_qry_arg_owner Arg_(String k, int v);
|
||||
@ -32,9 +31,3 @@ public interface Db_qry_arg_owner extends Db_qry {
|
||||
Db_qry_arg_owner Arg_obj_(String key, Object val);
|
||||
Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid);
|
||||
}
|
||||
class Db_arg {
|
||||
public String Key() {return key;} private String key;
|
||||
public Object Val() {return val;} public Db_arg Val_(Object v) {val = v; return this;} Object val;
|
||||
public byte Val_tid() {return val_tid;} public Db_arg Val_tid_(byte v) {val_tid = v; return this;} private byte val_tid = Db_val_type.Tid_null;
|
||||
@gplx.Internal protected Db_arg(String key, Object val) {this.key = key; this.val = val;}
|
||||
}
|
||||
|
@ -16,24 +16,26 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_delete implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_basic;}
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "DELETE";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public Db_qry_delete Where_add_(String key, int val) {
|
||||
Criteria crt = Db_crt_.eq_(key, val);
|
||||
where = Sql_where.merge_or_new_(where, crt);
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} public Db_qry_delete BaseTable_(String baseTable_) {baseTable = baseTable_; return this;} private String baseTable;
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_delete Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
|
||||
public static Db_qry_delete new_() {return new Db_qry_delete();} Db_qry_delete() {}
|
||||
import gplx.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_delete implements Db_qry {
|
||||
Db_qry_delete(String base_table, Criteria where) {this.base_table = base_table; this.where = where;}
|
||||
public int Tid() {return Db_qry_.Tid_delete;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return base_table;} private final String base_table;
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public Criteria Where() {return where;} private final Criteria where;
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
|
||||
public String[] Where_cols() {return where_cols;} private String[] where_cols;
|
||||
public static Db_qry_delete new_all_(String tbl) {
|
||||
Db_qry_delete rv = new Db_qry_delete();
|
||||
rv.baseTable = tbl;
|
||||
Db_qry_delete rv = new_(tbl, Criteria_.All);
|
||||
rv.where_cols = String_.Ary_empty;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_delete new_(String tbl, String... where) {
|
||||
Db_qry_delete rv = Db_qry_delete.new_(tbl, Db_crt_.eq_many_(where));
|
||||
rv.where_cols = where;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_delete new_(String tbl, Criteria where) {return new Db_qry_delete(tbl, where);}
|
||||
}
|
||||
|
@ -20,11 +20,11 @@ import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_dml_tst {
|
||||
@Test public void Delete_basic() {
|
||||
tst_XtoSql(Db_qry_delete.new_().BaseTable_("tbl0").Where_(Db_crt_.eq_("fld0", "val0"))
|
||||
tst_XtoSql(Db_qry_delete.new_("tbl0", Db_crt_.eq_("fld0", "val0"))
|
||||
, "DELETE FROM tbl0 WHERE fld0='val0'");
|
||||
}
|
||||
@Test public void Insert_basic() {
|
||||
tst_XtoSql(Db_qry_insert.new_().BaseTable_("tbl0").Arg_("id", 0).Arg_("name", "me").Arg_("time", DateAdp_.parse_gplx("2007-12-23"))
|
||||
tst_XtoSql(new Db_qry_insert("tbl0").Arg_("id", 0).Arg_("name", "me").Arg_("time", DateAdp_.parse_gplx("2007-12-23"))
|
||||
, "INSERT INTO tbl0 (id, name, time) VALUES (0, 'me', '2007-12-23 00:00:00.000')");
|
||||
}
|
||||
@Test public void Update_basic() {
|
||||
@ -40,5 +40,5 @@ public class Db_qry_dml_tst {
|
||||
qry.Where_(Criteria_.And(Db_crt_.eq_("id", 0), Db_crt_.mt_("startTime", DateAdp_.parse_gplx("2005-01-01"))));
|
||||
tst_XtoSql(qry, "UPDATE tbl0 SET id=1, name='me', startTime='2007-12-23 00:00:00.000' WHERE (id=0 AND startTime>'2005-01-01 00:00:00.000')");
|
||||
}
|
||||
void tst_XtoSql(Db_qry qry, String expd) {Tfds.Eq(expd, qry.XtoSql());}
|
||||
void tst_XtoSql(Db_qry qry, String expd) {Tfds.Eq(expd, qry.Xto_sql());}
|
||||
}
|
||||
|
@ -16,14 +16,16 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_flush implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_basic;}
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "FLUSH";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
public int Tid() {return Db_qry_.Tid_flush;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return tableNames[0];}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
|
||||
@gplx.Internal protected String[] TableNames() {return tableNames;} private String[] tableNames;
|
||||
|
||||
|
||||
public static Db_qry_flush as_(Object obj) {return obj instanceof Db_qry_flush ? (Db_qry_flush)obj : null;}
|
||||
public static Db_qry_flush cast_(Object obj) {try {return (Db_qry_flush)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, Db_qry_flush.class, obj);}}
|
||||
|
@ -16,13 +16,15 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_insert implements Db_qry_arg_owner {
|
||||
public int Tid() {return Db_qry_.Tid_basic;}
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "INSERT";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public Db_qry_arg_owner From_(String tbl) {baseTable = tbl; return this;}
|
||||
public Db_qry_insert(String base_table) {this.base_table = base_table;}
|
||||
public int Tid() {return Db_qry_.Tid_insert;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return base_table;} private String base_table;
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public Db_qry_arg_owner From_(String tbl) {base_table = tbl; return this;}
|
||||
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.Xto_decimal(), Db_val_type.Tid_decimal);}
|
||||
public Db_qry_arg_owner Arg_(String k, DateAdp v) {return Arg_obj_type_(k, v, Db_val_type.Tid_date);}
|
||||
public Db_qry_arg_owner Arg_byte_(String k, byte v) {return Arg_obj_type_(k, v, Db_val_type.Tid_byte);}
|
||||
@ -47,9 +49,18 @@ public class Db_qry_insert implements Db_qry_arg_owner {
|
||||
cols.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fld, fld));
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} public Db_qry_insert BaseTable_(String val) {baseTable = val; return this;} private String baseTable;
|
||||
@gplx.Internal protected KeyValHash Args() {return args;} KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected Sql_select_fld_list Cols() {return cols;} Sql_select_fld_list cols;
|
||||
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected Sql_select_fld_list Cols() {return cols;} private Sql_select_fld_list cols;
|
||||
|
||||
public String[] Cols_for_insert() {return cols_for_insert;} private String[] cols_for_insert;
|
||||
public static Db_qry_insert new_() {return new Db_qry_insert();} Db_qry_insert() {}
|
||||
public static Db_qry_insert new_(String tbl, String... keys) {
|
||||
Db_qry_insert rv = Db_qry_insert.new_();
|
||||
rv.base_table = tbl;
|
||||
rv.cols_for_insert = keys;
|
||||
int len = keys.length;
|
||||
for (int i = 0; i < len; ++i)
|
||||
rv.Arg_obj_(keys[i], null);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
@ -16,18 +16,19 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_select implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_basic;}
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "SELECT";
|
||||
public boolean ExecRdrAble() {return true;}
|
||||
public DataRdr Exec_qry_as_rdr(Db_provider provider) {return provider.Exec_qry_as_rdr(this);}
|
||||
public GfoNde ExecRdr_nde(Db_provider provider) {
|
||||
public int Tid() {return Db_qry_.Tid_select;}
|
||||
public boolean Exec_is_rdr() {return true;}
|
||||
public String Base_table() {return from.BaseTable().TblName();}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public DataRdr Exec_qry_as_rdr(Db_conn conn) {return conn.Exec_qry_as_rdr(this);}
|
||||
public GfoNde ExecRdr_nde(Db_conn conn) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {return GfoNde_.rdr_(Exec_qry_as_rdr(provider));} finally {rdr.Rls();}
|
||||
try {return GfoNde_.rdr_(Exec_qry_as_rdr(conn));} finally {rdr.Rls();}
|
||||
}
|
||||
public Object ExecRdr_val(Db_provider provider) {
|
||||
DataRdr rdr = Exec_qry_as_rdr(provider);
|
||||
public Object ExecRdr_val(Db_conn conn) {
|
||||
DataRdr rdr = Exec_qry_as_rdr(conn);
|
||||
try {
|
||||
Object rv = null;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
@ -46,7 +47,6 @@ public class Db_qry_select implements Db_qry {
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
|
||||
@gplx.Internal protected Sql_from From() {return from;} Sql_from from;
|
||||
public Db_qry_select From_(String tblName) {return From_(tblName, null);}
|
||||
|
@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_select_tst {
|
||||
@Before public void setup() {
|
||||
cmd = Db_qry_select.new_();
|
||||
@ -85,5 +85,5 @@ public class Db_qry_select_tst {
|
||||
// expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1 HAVING Count(fld0) > 1";
|
||||
// Tfds.Eq(cmd.XtoStr(), expd);
|
||||
// }
|
||||
void tst_XtoStr(Db_qry qry, String expd) {Tfds.Eq(expd, cmd.XtoSql());}
|
||||
void tst_XtoStr(Db_qry qry, String expd) {Tfds.Eq(expd, cmd.Xto_sql());}
|
||||
}
|
||||
|
@ -16,12 +16,13 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
public class Db_qry_sql implements Db_qry {
|
||||
public int Tid() {return Db_qry_.Tid_basic;}
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "SQL";
|
||||
public boolean ExecRdrAble() {return isReader;} private boolean isReader;
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return sql;} private String sql;
|
||||
public int Tid() {return Db_qry_.Tid_sql;}
|
||||
public boolean Exec_is_rdr() {return isReader;} private boolean isReader;
|
||||
public String Base_table() {throw Err_.not_implemented_();}
|
||||
public String Xto_sql() {return sql;} private String sql;
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public static Db_qry_sql dml_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql ddl_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql xtn_(String sql) {return sql_(sql);}
|
||||
@ -37,4 +38,45 @@ public class Db_qry_sql implements Db_qry {
|
||||
}
|
||||
public static Db_qry_sql as_(Object obj) {return obj instanceof Db_qry_sql ? (Db_qry_sql)obj : null;}
|
||||
public static Db_qry_sql cast_(Object obj) {try {return (Db_qry_sql)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, Db_qry_sql.class, obj);}}
|
||||
public static String Gen_sql(Db_qry qry, Object... args) {
|
||||
byte[] src = Bry_.new_utf8_(Sql_qry_wtr_.Gen_placeholder_parameters(qry));
|
||||
int src_len = src.length;
|
||||
int args_idx = 0, args_len = args.length, pos = 0;
|
||||
Bry_bfr bfr = Bry_bfr.new_(src_len);
|
||||
while (pos < src_len) {
|
||||
int question_pos = Bry_finder.Find_fwd(src, Byte_ascii.Question, pos);
|
||||
if (question_pos == Bry_.NotFound)
|
||||
question_pos = src_len;
|
||||
bfr.Add_mid(src, pos, question_pos);
|
||||
if (args_idx < args_len)
|
||||
Gen_sql_arg(bfr, args[args_idx++]);
|
||||
pos = question_pos + 1;
|
||||
}
|
||||
return bfr.Xto_str_and_clear();
|
||||
}
|
||||
private static void Gen_sql_arg(Bry_bfr bfr, Object val) {
|
||||
if (val == null) {bfr.Add(Bry_null); return;}
|
||||
Class<?> val_type = val.getClass();
|
||||
if (ClassAdp_.Eq(val_type, Int_.Cls_ref_type))
|
||||
bfr.Add_int_variable(Int_.cast_(val));
|
||||
else if (ClassAdp_.Eq(val_type, Bool_.Cls_ref_type))
|
||||
bfr.Add_int_fixed(1, Bool_.Xto_int(Bool_.cast_(val))); // NOTE: save boolean to 0 or 1, b/c (a) db may not support bit datatype (sqllite) and (b) avoid i18n issues with "true"/"false"
|
||||
else if (ClassAdp_.Eq(val_type, Double_.Cls_ref_type))
|
||||
bfr.Add_double(Double_.cast_(val));
|
||||
else if (ClassAdp_.Eq(val_type, Long_.Cls_ref_type))
|
||||
bfr.Add_long_variable(Long_.cast_(val));
|
||||
else if (ClassAdp_.Eq(val_type, Float_.Cls_ref_type))
|
||||
bfr.Add_float(Float_.cast_(val));
|
||||
else if (ClassAdp_.Eq(val_type, Byte_.Cls_ref_type))
|
||||
bfr.Add_byte(Byte_.cast_(val));
|
||||
else if (ClassAdp_.Eq(val_type, DateAdp_.Cls_ref_type))
|
||||
bfr.Add_byte_apos().Add_str(DateAdp_.cast_(val).XtoStr_gplx_long()).Add_byte_apos();
|
||||
else if (ClassAdp_.Eq(val_type, DecimalAdp_.Cls_ref_type))
|
||||
bfr.Add_str(DecimalAdp_.cast_(val).Xto_str());
|
||||
else {
|
||||
byte[] val_bry = Bry_.new_utf8_(Object_.Xto_str_strict_or_null(val));
|
||||
val_bry = Bry_.Replace(val_bry, Byte_ascii.Apos_bry, Bry_escape_apos);
|
||||
bfr.Add_byte_apos().Add(val_bry).Add_byte_apos();
|
||||
}
|
||||
} private static final byte[] Bry_null = Bry_.new_utf8_("NULL"), Bry_escape_apos = Bry_.new_ascii_("''");
|
||||
}
|
||||
|
47
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_sql_tst.java
Normal file
47
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_sql_tst.java
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_qry_sql_tst {
|
||||
@Before public void init() {fxt.Clear();} private Db_qry_sql_fxt fxt = new Db_qry_sql_fxt();
|
||||
@Test public void Insert() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_insert.new_("tbl", "k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9")
|
||||
, Object_.Ary(123, Bool_.Y, 1.23d, 123L, 123f, Byte_ascii.Num_1, "123", DateAdp_.parse_iso8561("1981-04-05T14:30:30"), DecimalAdp_.parse_("1.23"))
|
||||
, "INSERT INTO tbl (k1, k2, k3, k4, k5, k6, k7, k8, k9) VALUES (123, 1, 1.23, 123, 123, 1, '123', '1981-04-05 14:30:30.000', 1.23)"
|
||||
);
|
||||
}
|
||||
@Test public void Update() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_update.new_("tbl", String_.Ary("k1", "k2"), "k3", "k4")
|
||||
, Object_.Ary("v3", "v4", "v1", "v2")
|
||||
, "UPDATE tbl SET k3='v3', k4='v4' WHERE (k1='v1' AND k2='v2')"
|
||||
);
|
||||
}
|
||||
@Test public void Delete() {
|
||||
fxt.Test_qry
|
||||
( Db_qry_delete.new_("tbl", String_.Ary("k1", "k2"))
|
||||
, Object_.Ary("v1", "v2")
|
||||
, "DELETE FROM tbl WHERE (k1='v1' AND k2='v2')"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Db_qry_sql_fxt {
|
||||
public void Clear() {}
|
||||
public void Test_qry(Db_qry qry, Object[] vals, String expd) {Tfds.Eq(expd, Db_qry_sql.Gen_sql(qry, vals));}
|
||||
}
|
@ -16,14 +16,13 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Db_qry_update implements Db_qry_arg_owner {
|
||||
public int Tid() {return Db_qry_.Tid_basic;}
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "UPDATE";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
|
||||
public int Tid() {return Db_qry_.Tid_update;}
|
||||
public boolean Exec_is_rdr() {return false;}
|
||||
public String Base_table() {return baseTable;}
|
||||
public String Xto_sql() {return Sql_qry_wtr_.I.Xto_str(this, false);}
|
||||
public int Exec_qry(Db_conn conn) {return conn.Exec_qry(this);}
|
||||
public Db_qry_arg_owner From_(String tbl) {baseTable = tbl; return this;}
|
||||
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.Xto_decimal(), Db_val_type.Tid_decimal);}
|
||||
public Db_qry_arg_owner Arg_(String k, DateAdp v) {return Arg_obj_type_(k, v, Db_val_type.Tid_date);}
|
||||
@ -46,9 +45,19 @@ public class Db_qry_update implements Db_qry_arg_owner {
|
||||
where = Sql_where.merge_or_new_(where, crt);
|
||||
return this;
|
||||
}
|
||||
public KeyValHash Args() {return args;} private final KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} private String baseTable;
|
||||
@gplx.Internal protected KeyValHash Args() {return args;} final KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
|
||||
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} private Sql_where where;
|
||||
public String[] Cols_where() {return cols_where;} private String[] cols_where;
|
||||
public String[] Cols_update() {return cols_update;} private String[] cols_update;
|
||||
public static Db_qry_update new_() {return new Db_qry_update();} Db_qry_update() {}
|
||||
public static Db_qry_update new_(String tbl, String[] where, String... update) {
|
||||
Db_qry_update rv = Db_qry_.update_(tbl, Db_crt_.eq_many_(where));
|
||||
rv.cols_update = update;
|
||||
rv.cols_where = where;
|
||||
int len = update.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
rv.Arg_obj_(update[i], null);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
@ -17,19 +17,36 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_stmt extends RlsAble {
|
||||
Db_provider Provider();
|
||||
Db_stmt Val_bool_(boolean v);
|
||||
Db_stmt Val_byte_(byte v);
|
||||
Db_stmt Val_byte_by_bool_(boolean v);
|
||||
Db_stmt Val_int_(int v);
|
||||
Db_stmt Val_long_(long v);
|
||||
Db_stmt Val_float_(float v);
|
||||
Db_stmt Val_double_(double v);
|
||||
Db_stmt Val_decimal_(DecimalAdp v);
|
||||
Db_stmt Val_bry_(byte[] v);
|
||||
Db_stmt Val_bry_by_str_(String v);
|
||||
Db_stmt Val_str_by_bry_(byte[] v);
|
||||
Db_stmt Val_str_(String v);
|
||||
Db_stmt Crt_bool_as_byte(String k, boolean v);
|
||||
Db_stmt Val_bool_as_byte(String k, boolean v);
|
||||
Db_stmt Val_bool_as_byte(boolean v);
|
||||
Db_stmt Crt_byte(String k, byte v);
|
||||
Db_stmt Val_byte(String k, byte v);
|
||||
Db_stmt Val_byte(byte v);
|
||||
Db_stmt Crt_int(String k, int v);
|
||||
Db_stmt Val_int(String k, int v);
|
||||
Db_stmt Val_int(int v);
|
||||
Db_stmt Crt_long(String k, long v);
|
||||
Db_stmt Val_long(String k, long v);
|
||||
Db_stmt Val_long(long v);
|
||||
Db_stmt Crt_float(String k, float v);
|
||||
Db_stmt Val_float(String k, float v);
|
||||
Db_stmt Val_float(float v);
|
||||
Db_stmt Crt_double(String k, double v);
|
||||
Db_stmt Val_double(String k, double v);
|
||||
Db_stmt Val_double(double v);
|
||||
Db_stmt Crt_decimal(String k, DecimalAdp v);
|
||||
Db_stmt Val_decimal(String k, DecimalAdp v);
|
||||
Db_stmt Val_decimal(DecimalAdp v);
|
||||
Db_stmt Crt_bry(String k, byte[] v);
|
||||
Db_stmt Val_bry(String k, byte[] v);
|
||||
Db_stmt Val_bry(byte[] v);
|
||||
Db_stmt Crt_str(String k, String v);
|
||||
Db_stmt Val_str(String k, String v);
|
||||
Db_stmt Val_str(String v);
|
||||
Db_stmt Crt_bry_as_str(String k, byte[] v);
|
||||
Db_stmt Val_bry_as_str(String k, byte[] v);
|
||||
Db_stmt Val_bry_as_str(byte[] v);
|
||||
Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr rdr, long rdr_len);
|
||||
boolean Exec_insert();
|
||||
int Exec_update();
|
||||
@ -38,5 +55,5 @@ public interface Db_stmt extends RlsAble {
|
||||
Db_rdr Exec_select_as_rdr();
|
||||
Object Exec_select_val();
|
||||
Db_stmt Clear();
|
||||
Db_stmt New();
|
||||
Db_stmt Reset_stmt();
|
||||
}
|
||||
|
@ -18,39 +18,40 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_stmt_ {
|
||||
public static final Db_stmt Null = new Db_stmt_sql();
|
||||
public static Db_stmt new_insert_(Db_provider provider, String tbl, String... flds) {
|
||||
Db_qry_insert qry = Db_qry_insert.new_().BaseTable_(tbl);
|
||||
int len = flds.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
qry.Arg_obj_(flds[i], null);
|
||||
return provider.Prepare(qry);
|
||||
public static Db_stmt new_insert_(Db_conn conn, String tbl, String... flds) {
|
||||
Db_qry qry = Db_qry_insert.new_(tbl, flds);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_update_(Db_provider provider, String tbl, String[] where, String... flds) {
|
||||
Db_qry_update qry = Db_qry_.update_(tbl, Db_crt_.eq_many_(where));
|
||||
int len = flds.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
qry.Arg_obj_(flds[i], null);
|
||||
return provider.Prepare(qry);
|
||||
public static Db_stmt new_update_(Db_conn conn, String tbl, String[] where, String... flds) {
|
||||
Db_qry qry = Db_qry_update.new_(tbl, where, flds);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_delete_(Db_provider provider, String tbl, String... where) {
|
||||
public static Db_stmt new_delete_(Db_conn conn, String tbl, String... where) {
|
||||
Db_qry_delete qry = Db_qry_.delete_(tbl, Db_crt_.eq_many_(where));
|
||||
return provider.Prepare(qry);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_select_(Db_provider provider, String tbl, String[] where, String... flds) {
|
||||
public static Db_stmt new_select_(Db_conn conn, String tbl, String[] where, String... flds) {
|
||||
Db_qry_select qry = Db_qry_.select_cols_(tbl, Db_crt_.eq_many_(where), flds);
|
||||
return provider.Prepare(qry);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_select_in_(Db_provider provider, String tbl, String in_fld, Object[] in_vals, String... flds) {
|
||||
public static Db_stmt new_select_in_(Db_conn conn, String tbl, String in_fld, Object[] in_vals, String... flds) {
|
||||
Db_qry_select qry = Db_qry_.select_cols_(tbl, Db_crt_.in_(in_fld, in_vals), flds).OrderBy_asc_(in_fld);
|
||||
return provider.Prepare(qry);
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_select_all_(Db_provider provider, String tbl) {
|
||||
return provider.Prepare(Db_qry_.select_tbl_(tbl));
|
||||
public static Db_stmt new_select_all_(Db_conn conn, String tbl) {
|
||||
return conn.New_stmt(Db_qry_.select_tbl_(tbl));
|
||||
}
|
||||
public static Db_stmt new_select_as_rdr(Db_provider provider, Db_qry__select_in_tbl qry) {
|
||||
return provider.Prepare(qry);
|
||||
public static Db_stmt new_select_as_rdr(Db_conn conn, Db_qry__select_in_tbl qry) {
|
||||
return conn.New_stmt(qry);
|
||||
}
|
||||
public static Db_stmt new_select_as_rdr(Db_provider provider, String sql) {
|
||||
return provider.Prepare(Db_qry_sql.rdr_(sql));
|
||||
public static Db_stmt new_select_as_rdr(Db_conn conn, String sql) {
|
||||
return conn.New_stmt(Db_qry_sql.rdr_(sql));
|
||||
}
|
||||
public static Err err_(Exception e, Db_stmt stmt, String proc) {
|
||||
throw Err_.err_(e, String_.Format("db stmt failed: proc=~{0} err=~{1}", proc, Err_.Message_gplx_brief(e)));
|
||||
}
|
||||
public static Err err_(Exception e, String tbl, String proc) {
|
||||
throw Err_.err_(e, String_.Format("db call failed: tbl=~{0} proc=~{1} err=~{2}", tbl, proc, Err_.Message_gplx_brief(e)));
|
||||
}
|
||||
public static Db_stmt Rls(Db_stmt v) {if (v != null) v.Rls(); return null;}
|
||||
}
|
||||
|
@ -17,32 +17,32 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_stmt_bldr implements RlsAble {
|
||||
private Db_provider provider;
|
||||
private Db_conn conn;
|
||||
private Db_stmt create, update, delete;
|
||||
private String tbl_name; private String[] flds_keys, flds_vals, flds_all;
|
||||
public Db_stmt_bldr(String tbl_name, String[] flds_keys, String... flds_vals) {
|
||||
this.tbl_name = tbl_name; this.flds_keys = flds_keys; this.flds_vals = flds_vals;
|
||||
flds_all = String_.Ary_add(flds_keys, flds_vals);
|
||||
}
|
||||
public Db_stmt_bldr Init(Db_provider v) {
|
||||
this.provider = v;
|
||||
provider.Txn_mgr().Txn_bgn_if_none();
|
||||
public Db_stmt_bldr Init(Db_conn v) {
|
||||
this.conn = v;
|
||||
conn.Txn_mgr().Txn_bgn_if_none();
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Get(byte cmd_mode) {
|
||||
switch (cmd_mode) {
|
||||
case Db_cmd_mode.Create: if (create == null) create = Db_stmt_.new_insert_(provider, tbl_name, flds_all); return create;
|
||||
case Db_cmd_mode.Update: if (update == null) update = Db_stmt_.new_update_(provider, tbl_name, flds_keys, flds_vals); return update;
|
||||
case Db_cmd_mode.Delete: if (delete == null) delete = Db_stmt_.new_delete_(provider, tbl_name, flds_keys); return delete;
|
||||
case Db_cmd_mode.Create: if (create == null) create = Db_stmt_.new_insert_(conn, tbl_name, flds_all); return create;
|
||||
case Db_cmd_mode.Update: if (update == null) update = Db_stmt_.new_update_(conn, tbl_name, flds_keys, flds_vals); return update;
|
||||
case Db_cmd_mode.Delete: if (delete == null) delete = Db_stmt_.new_delete_(conn, tbl_name, flds_keys); return delete;
|
||||
case Db_cmd_mode.Ignore: return Db_stmt_.Null;
|
||||
default: throw Err_.unhandled(cmd_mode);
|
||||
}
|
||||
}
|
||||
public void Commit() {
|
||||
provider.Txn_mgr().Txn_end_all();
|
||||
conn.Txn_mgr().Txn_end_all();
|
||||
}
|
||||
public void Rls() {
|
||||
provider = null;
|
||||
conn = null;
|
||||
create = Rls(create);
|
||||
update = Rls(update);
|
||||
delete = Rls(delete);
|
||||
|
@ -17,57 +17,81 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import java.sql.*;
|
||||
import gplx.dbs.sqls.*;
|
||||
class Db_stmt_cmd implements Db_stmt {
|
||||
private Db_engine engine;
|
||||
private final Db_engine engine;
|
||||
private PreparedStatement stmt = null;
|
||||
private String sql;
|
||||
private int val_idx = 0;
|
||||
public Db_stmt_cmd(Db_provider provider, Db_qry qry) {
|
||||
this.provider = provider; this.engine = provider.Engine();
|
||||
sql = qry.Tid() == Db_qry_.Tid_select_in_tbl ? ((Db_qry__select_in_tbl)qry).Xto_sql() : Sql_cmd_wtr_.Ansi.XtoSqlQry(qry, true);
|
||||
New();
|
||||
private String sql; private int val_idx = 0;
|
||||
public Db_stmt_cmd(Db_engine engine, Db_qry qry) {
|
||||
this.engine = engine;
|
||||
sql = qry.Tid() == Db_qry_.Tid_select_in_tbl ? ((Db_qry__select_in_tbl)qry).Xto_sql() : Sql_qry_wtr_.I.Xto_str(qry, true);
|
||||
Reset_stmt();
|
||||
}
|
||||
public Db_stmt New() {
|
||||
stmt = (PreparedStatement)engine.New_db_cmd(sql);
|
||||
public Db_stmt Reset_stmt() {
|
||||
stmt = (PreparedStatement)engine.New_stmt_prep_as_obj(sql);
|
||||
return this;
|
||||
}
|
||||
public Db_provider Provider() {return provider;} Db_provider provider;
|
||||
public Db_stmt Val_bool_(boolean v) {
|
||||
try {stmt.setBoolean(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_byte_by_bool_(boolean v) {return Val_byte_(v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Val_byte_(byte v) {
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {stmt.setByte(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_int_(int v) {
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {stmt.setInt(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_long_(long v) {
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {stmt.setLong(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_float_(float v) {
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {stmt.setFloat(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_double_(double v) {
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {stmt.setDouble(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_decimal_(DecimalAdp v) {
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {stmt.setBigDecimal(++val_idx, v.Xto_decimal());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_bry_by_str_(String v) {return Val_bry_(Bry_.new_utf8_(v));}
|
||||
public Db_stmt Val_bry_(byte[] v) {
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {
|
||||
try {stmt.setBytes(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_str_by_bry_(byte[] v) {return Val_str_(String_.new_utf8_(v));}
|
||||
public Db_stmt Val_str_(String v) {
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_str(String k, String v) {return Add_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {stmt.setString(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
@ -85,13 +109,13 @@ class Db_stmt_cmd implements Db_stmt {
|
||||
try {int rv = stmt.executeUpdate(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = engine.NewDataRdr(stmt.executeQuery(), sql); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
try {DataRdr rv = engine.New_rdr(stmt.executeQuery(), sql); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public Db_rdr Exec_select_as_rdr() {
|
||||
try {return engine.New_db_rdr(stmt.executeQuery(), sql);} catch (Exception e) {throw Err_.err_(e, "select failed: sql={0}", sql);}
|
||||
try {return engine.New_rdr_by_obj(stmt.executeQuery(), sql);} catch (Exception e) {throw Err_.err_(e, "select failed: sql={0}", sql);}
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(engine.NewDataRdr(stmt.executeQuery(), sql)); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(engine.New_rdr(stmt.executeQuery(), sql)); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
|
@ -16,48 +16,73 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
class Db_stmt_sql implements Db_stmt {
|
||||
public class Db_stmt_sql implements Db_stmt {// used for formatting SQL statements; not used for actual insert into database
|
||||
private Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
private Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
private int val_idx = 0;
|
||||
public Db_provider Provider() {return provider;} public void Provider_(Db_provider v) {this.provider = v;} Db_provider provider;
|
||||
public Db_stmt New() {return this;}
|
||||
public Db_stmt Val_bool_(boolean v) {
|
||||
try {Add(++val_idx, v ? "true" : "false");} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_byte_by_bool_(boolean v) {return Val_byte_(v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Val_byte_(byte v) {
|
||||
public Db_conn Conn() {return conn;} public void Conn_(Db_conn v) {this.conn = v;} Db_conn conn;
|
||||
public Db_stmt Reset_stmt() {return this;}
|
||||
public Db_stmt Crt_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(String k, boolean v) {return Add_byte_by_bool(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bool_as_byte(boolean v) {return Add_byte_by_bool(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte_by_bool(boolean where, String k, boolean v) {return Add_byte(where, k, v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Crt_byte(String k, byte v) {return Add_byte(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_byte(String k, byte v) {return Add_byte(Bool_.N, k, v);}
|
||||
public Db_stmt Val_byte(byte v) {return Add_byte(Bool_.N, null, v);}
|
||||
private Db_stmt Add_byte(boolean where, String k, byte v) {
|
||||
try {Add(++val_idx, Byte_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_int_(int v) {
|
||||
public Db_stmt Crt_int(String k, int v) {return Add_int(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_int(String k, int v) {return Add_int(Bool_.N, k, v);}
|
||||
public Db_stmt Val_int(int v) {return Add_int(Bool_.N, null, v);}
|
||||
private Db_stmt Add_int(boolean where, String k, int v) {
|
||||
try {Add(++val_idx, Int_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_long_(long v) {
|
||||
public Db_stmt Crt_long(String k, long v) {return Add_long(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_long(String k, long v) {return Add_long(Bool_.N, k, v);}
|
||||
public Db_stmt Val_long(long v) {return Add_long(Bool_.N, null, v);}
|
||||
private Db_stmt Add_long(boolean where, String k, long v) {
|
||||
try {Add(++val_idx, Long_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_float_(float v) {
|
||||
public Db_stmt Crt_float(String k, float v) {return Add_float(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_float(String k, float v) {return Add_float(Bool_.N, k, v);}
|
||||
public Db_stmt Val_float(float v) {return Add_float(Bool_.N, null, v);}
|
||||
private Db_stmt Add_float(boolean where, String k, float v) {
|
||||
try {Add(++val_idx, Float_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_double_(double v) {
|
||||
public Db_stmt Crt_double(String k, double v) {return Add_double(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_double(String k, double v) {return Add_double(Bool_.N, k, v);}
|
||||
public Db_stmt Val_double(double v) {return Add_double(Bool_.N, null, v);}
|
||||
private Db_stmt Add_double(boolean where, String k, double v) {
|
||||
try {Add(++val_idx, Double_.Xto_str(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_decimal_(DecimalAdp v) {
|
||||
public Db_stmt Crt_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_decimal(String k, DecimalAdp v) {return Add_decimal(Bool_.N, k, v);}
|
||||
public Db_stmt Val_decimal(DecimalAdp v) {return Add_decimal(Bool_.N, null, v);}
|
||||
private Db_stmt Add_decimal(boolean where, String k, DecimalAdp v) {
|
||||
try {Add(++val_idx, v.Xto_str());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_bry_by_str_(String v) {return Val_bry_(Bry_.new_utf8_(v));}
|
||||
public Db_stmt Val_bry_(byte[] v) { // HACK: convert to String b/c tdb does not support byte[]
|
||||
public Db_stmt Crt_bry(String k, byte[] v) {return Add_bry(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry(String k, byte[] v) {return Add_bry(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry(byte[] v) {return Add_bry(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry(boolean where, String k, byte[] v) {// HACK: convert to String b/c tdb does not support byte[]
|
||||
try {Add(++val_idx, Val_str_wrap(String_.new_utf8_(v)));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_str_by_bry_(byte[] v) {return Val_str_(String_.new_utf8_(v));}
|
||||
public Db_stmt Val_str_(String v) {
|
||||
public Db_stmt Crt_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_bry_as_str(String k, byte[] v) {return Add_bry_as_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_bry_as_str(byte[] v) {return Add_bry_as_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_bry_as_str(boolean where, String k, byte[] v) {return Add_str(where, k, String_.new_utf8_(v));}
|
||||
public Db_stmt Crt_str(String k, String v) {return Add_str(Bool_.Y, k, v);}
|
||||
public Db_stmt Val_str(String k, String v) {return Add_str(Bool_.N, k, v);}
|
||||
public Db_stmt Val_str(String v) {return Add_str(Bool_.N, null, v);}
|
||||
private Db_stmt Add_str(boolean where, String k, String v) {
|
||||
try {Add(++val_idx, Val_str_wrap(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
@ -69,20 +94,20 @@ class Db_stmt_sql implements Db_stmt {
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
String Val_str_wrap(String v) {
|
||||
private String Val_str_wrap(String v) {
|
||||
return "'" + String_.Replace(v, "'", "\\'") + "'";
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())) != 0; return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
try {boolean rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())) != 0; return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
try {int rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
try {int rv = conn.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = provider.Exec_qry_as_rdr(Db_qry_sql.rdr_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
try {DataRdr rv = conn.Exec_qry_as_rdr(Db_qry_sql.rdr_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Db_rdr Exec_select_as_rdr() {throw Err_.not_implemented_();}
|
||||
public Object Exec_select_val() {
|
||||
@ -97,13 +122,17 @@ class Db_stmt_sql implements Db_stmt {
|
||||
this.Clear();
|
||||
}
|
||||
public void Add(String v) {Add(-1, v);}
|
||||
public void Add(int idx, String v) {args.Add(v);} ListAdp args = ListAdp_.new_();
|
||||
public void Add(int idx, String v) {args.Add(v);} private ListAdp args = ListAdp_.new_();
|
||||
public String Xto_sql() {
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, (Object[])args.Xto_ary_and_clear(Object.class));
|
||||
return tmp_bfr.Xto_str_and_clear();
|
||||
}
|
||||
String sql_orig;
|
||||
public Db_stmt Parse(String sql_str) {
|
||||
public int Args_len() {return args.Count();}
|
||||
public String Args_get_at(int i) {return (String)args.FetchAt(i);}
|
||||
private String sql_orig;
|
||||
public Db_qry Qry() {return qry;} private Db_qry qry;
|
||||
public Db_stmt Parse(Db_qry qry, String sql_str) {
|
||||
this.qry = qry;
|
||||
this.sql_orig = sql_str;
|
||||
int arg_idx = 0;
|
||||
byte[] src = Bry_.new_utf8_(sql_str);
|
||||
|
@ -21,7 +21,7 @@ public class Db_stmt_sql_tst {
|
||||
@Before public void init() {}
|
||||
@Test public void Basic() {
|
||||
Db_stmt_sql stmt = new Db_stmt_sql();
|
||||
stmt.Parse("UPDATE tbl_0 SET col_0 = ? WHERE col_1 = ?");
|
||||
stmt.Parse(null, "UPDATE tbl_0 SET col_0 = ? WHERE col_1 = ?");
|
||||
stmt.Add("1");
|
||||
stmt.Add("2");
|
||||
Tfds.Eq("UPDATE tbl_0 SET col_0 = 1 WHERE col_1 = 2", stmt.Xto_sql());
|
||||
|
@ -26,7 +26,7 @@ public interface Db_txn_mgr {
|
||||
int Txn_count(); void Txn_count_(int v);
|
||||
}
|
||||
class Db_txn_mgr_base implements Db_txn_mgr {
|
||||
public Db_txn_mgr_base(Db_engine engine) {this.engine = engine;} private Db_engine engine;
|
||||
public Db_txn_mgr_base(Db_engine engine) {this.engine = engine;} private final Db_engine engine;
|
||||
public int Txn_depth() {return txn_depth;} int txn_depth; // NOTE: only support 1 level for now;
|
||||
public void Txn_bgn_if_none() {if (txn_depth == 0) this.Txn_bgn();}
|
||||
public void Txn_bgn() {
|
||||
|
@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
import org.junit.*; import gplx.dbs.sqls.*;
|
||||
public class Db_obj_ary_tst {
|
||||
@Before public void init() {} private Db_obj_ary_fxt fxt = new Db_obj_ary_fxt();
|
||||
@Test public void Int() {
|
||||
@ -31,7 +31,7 @@ class Db_obj_ary_fxt {
|
||||
public Db_obj_ary_fxt Init_fld(String name, byte tid) {flds_list.Add(new Db_fld(name, tid)); return this;} private ListAdp flds_list = ListAdp_.new_();
|
||||
public Db_obj_ary_fxt Init_vals(Object... ary) {vals_list.Add(ary); return this;} private ListAdp vals_list = ListAdp_.new_();
|
||||
public Db_obj_ary_fxt Test_sql(String expd) {
|
||||
Sql_cmd_wtr_ansi cmd_wtr = (Sql_cmd_wtr_ansi)Sql_cmd_wtr_ansi_.default_();
|
||||
Sql_qry_wtr_ansi cmd_wtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.I;
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
crt.Flds_((Db_fld[])flds_list.Xto_ary_and_clear(Db_fld.class));
|
||||
crt.Vals_((Object[][])vals_list.Xto_ary_and_clear(Object[].class));
|
||||
|
@ -17,12 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class PoolIds {
|
||||
public int FetchNext(Db_provider provider, String url) {
|
||||
public int FetchNext(Db_conn conn, String url) {
|
||||
Db_qry_select cmd = Db_qry_.select_().From_(Tbl_Name).Where_(Db_crt_.eq_(Fld_id_path, url));
|
||||
int rv = 0;//boolean isNew = true;
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = cmd.Exec_qry_as_rdr(provider);
|
||||
rdr = cmd.Exec_qry_as_rdr(conn);
|
||||
if (rdr.MoveNextPeer()) {
|
||||
rv = rdr.ReadInt(Fld_id_next_id);
|
||||
}
|
||||
@ -31,17 +31,17 @@ public class PoolIds {
|
||||
return rv;
|
||||
}
|
||||
public int FetchNextAndCommit(String dbInfo, String url) {
|
||||
Db_provider provider = Db_provider_pool._.Get_or_new(dbInfo);
|
||||
int rv = PoolIds._.FetchNext(provider, url);
|
||||
PoolIds._.Commit(provider, url, rv + 1);
|
||||
Db_conn conn = Db_conn_pool_old._.Get_or_new(dbInfo);
|
||||
int rv = PoolIds._.FetchNext(conn, url);
|
||||
PoolIds._.Commit(conn, url, rv + 1);
|
||||
return rv;
|
||||
}
|
||||
public void Commit(Db_provider provider, String url, int val) {
|
||||
int rv = provider.Exec_qry(Db_qry_.update_(Tbl_Name, Db_crt_.eq_(Fld_id_path, url)).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
public void Commit(Db_conn conn, String url, int val) {
|
||||
int rv = conn.Exec_qry(Db_qry_.update_(Tbl_Name, Db_crt_.eq_(Fld_id_path, url)).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
if (rv == 0) {
|
||||
rv = provider.Exec_qry(Db_qry_.insert_(Tbl_Name).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
rv = conn.Exec_qry(Db_qry_.insert_(Tbl_Name).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
}
|
||||
if (rv != 1) throw Err_.new_("failed to update nextId").Add("provider", provider.Conn_info().Str_raw()).Add("url", url).Add("nextId", val);
|
||||
if (rv != 1) throw Err_.new_("failed to update nextId").Add("url", url).Add("nextId", val);
|
||||
}
|
||||
public static final String Tbl_Name = "pool_ids";
|
||||
@gplx.Internal protected static final String Fld_id_path = "id_path";
|
||||
|
@ -17,42 +17,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_engine {
|
||||
String Key();
|
||||
String Conn_info_tid();
|
||||
Db_conn_info Conn_info();
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
String Tid();
|
||||
Db_url Url();
|
||||
Db_engine New_clone(Db_url url);
|
||||
Db_rdr New_rdr_by_obj(Object o, String sql); // Object o:ResultSet if desktop; Cursor if android
|
||||
Db_stmt New_stmt_prep(Db_qry qry);
|
||||
Object New_stmt_prep_as_obj(String sql);
|
||||
DataRdr New_rdr(java.sql.ResultSet rdr, String sql);
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
Db_engine Make_new(Db_conn_info conn_info);
|
||||
|
||||
Db_rdr New_db_rdr(Object o, String sql); // ResultSet if desktop; Cursor if android
|
||||
Db_stmt New_db_stmt(Db_provider provider, Db_qry sql);
|
||||
Object Execute(Db_qry cmd);
|
||||
DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql);
|
||||
Object New_db_cmd(String sql);
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
Object Exec_as_obj(Db_qry qry);
|
||||
}
|
||||
class Db_engine_null implements Db_engine {
|
||||
public String Key() {return Db_conn_info_.Null.Key();}
|
||||
public String Conn_info_tid() {return Db_conn_info_.Null.Key();}
|
||||
public Db_conn_info Conn_info() {return Db_conn_info_.Null;}
|
||||
public String Tid() {return Db_url__null.Tid_const;}
|
||||
public Db_url Url() {return Db_url_.Null;}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {}
|
||||
public Db_engine New_clone(Db_url url) {return this;}
|
||||
public Db_rdr New_rdr_by_obj(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public Db_stmt New_stmt_prep(Db_qry qry) {return Db_stmt_.Null;}
|
||||
public Object New_stmt_prep_as_obj(String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr New_rdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public Db_engine Make_new(Db_conn_info conn_info) {return this;}
|
||||
|
||||
public Object Execute(Db_qry cmd) {return cmd.ExecRdrAble() ? (Object)DataRdr_.Null : -1;}
|
||||
public Object New_db_cmd(String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public Db_rdr New_db_rdr(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public Db_stmt New_db_stmt(Db_provider provider, Db_qry qry) {return Db_stmt_.Null;}
|
||||
public Object Exec_as_obj(Db_qry cmd) {return cmd.Exec_is_rdr() ? (Object)DataRdr_.Null : -1;}
|
||||
public static final Db_engine_null _ = new Db_engine_null(); Db_engine_null() {}
|
||||
}
|
||||
class ExecSqlWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmd) {
|
||||
Db_engine_sql_base engine = (Db_engine_sql_base)engineObj;
|
||||
String sql = engine.SqlWtr().XtoSqlQry(cmd, false); // Tfds.Write(sql);
|
||||
return cmd.ExecRdrAble() ? (Object)engine.ExecuteReader(sql) : engine.ExecuteNonQuery(sql);
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public class Db_engine_regy {
|
||||
.Add(Sqlite_engine._)
|
||||
;
|
||||
}
|
||||
public Db_engine_regy Add(Db_engine engine) {hash.Add(engine.Key(), engine); return this;}
|
||||
public Db_engine_regy Add(Db_engine engine) {hash.Add(engine.Tid(), engine); return this;}
|
||||
public Db_engine Get(String key) {return (Db_engine)hash.FetchOrFail(key);}
|
||||
public static final Db_engine_regy _ = new Db_engine_regy(1);
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user