mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v1.6.5.1
This commit is contained in:
23
140_dbs/src_120_sql/gplx/dbs/Db_fld.java
Normal file
23
140_dbs/src_120_sql/gplx/dbs/Db_fld.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
public class Db_fld {
|
||||
public Db_fld(String name, byte type_tid) {this.name = name; this.type_tid = type_tid;}
|
||||
public String Name() {return name;} public Db_fld Name_(String v) {name = v; return this;} private String name;
|
||||
public byte Type_tid() {return type_tid;} public Db_fld Type_tid_(byte v) {type_tid = v; return this;} private byte type_tid;
|
||||
}
|
||||
34
140_dbs/src_120_sql/gplx/dbs/Db_obj_ary_crt.java
Normal file
34
140_dbs/src_120_sql/gplx/dbs/Db_obj_ary_crt.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.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_obj_ary_crt implements gplx.criterias.Criteria {
|
||||
public byte Crt_tid() {return Criteria_.Tid_db_obj_ary;}
|
||||
public Db_fld[] Flds() {return flds;} public Db_obj_ary_crt Flds_(Db_fld[] v) {this.flds = v; return this;} private Db_fld[] flds;
|
||||
public Object[][] Vals() {return vals;} public void Vals_(Object[][] v) {this.vals = v;} private Object[][] vals;
|
||||
public boolean Matches(Object obj) {return false;}
|
||||
public String XtoStr() {return "";}
|
||||
public static Db_obj_ary_crt new_(Db_fld... flds) {return new Db_obj_ary_crt().Flds_(flds);}
|
||||
public static Db_obj_ary_crt new_by_type(byte type_tid, String... names) {
|
||||
int len = names.length;
|
||||
Db_fld[] flds = new Db_fld[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
flds[i] = new Db_fld(names[i], type_tid);
|
||||
return new Db_obj_ary_crt().Flds_(flds);
|
||||
}
|
||||
}
|
||||
42
140_dbs/src_120_sql/gplx/dbs/Db_obj_ary_tst.java
Normal file
42
140_dbs/src_120_sql/gplx/dbs/Db_obj_ary_tst.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_obj_ary_tst {
|
||||
@Before public void init() {} private Db_obj_ary_fxt fxt = new Db_obj_ary_fxt();
|
||||
@Test public void Int() {
|
||||
fxt.Init_fld("fld_0", ClassAdp_.Tid_int).Init_fld("fld_1", ClassAdp_.Tid_int).Init_vals(1, 10).Init_vals(2, 20).Test_sql("(fld_0=1 AND fld_1=10) OR (fld_0=2 AND fld_1=20)");
|
||||
}
|
||||
@Test public void Str() {
|
||||
fxt.Init_fld("fld_0", ClassAdp_.Tid_int).Init_fld("fld_1", ClassAdp_.Tid_str).Init_vals(1, "a").Init_vals(2, "b").Test_sql("(fld_0=1 AND fld_1='a') OR (fld_0=2 AND fld_1='b')");
|
||||
}
|
||||
}
|
||||
class Db_obj_ary_fxt {
|
||||
private Db_obj_ary_crt crt = new Db_obj_ary_crt();
|
||||
public Db_obj_ary_fxt Init_fld(String name, byte tid) {flds_list.Add(new Db_fld(name, tid)); return this;} private ListAdp flds_list = ListAdp_.new_();
|
||||
public Db_obj_ary_fxt Init_vals(Object... ary) {vals_list.Add(ary); return this;} private ListAdp vals_list = ListAdp_.new_();
|
||||
public Db_obj_ary_fxt Test_sql(String expd) {
|
||||
Sql_cmd_wtr_ansi cmd_wtr = (Sql_cmd_wtr_ansi)Sql_cmd_wtr_ansi_.default_();
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
crt.Flds_((Db_fld[])flds_list.XtoAryAndClear(Db_fld.class));
|
||||
crt.Vals_((Object[][])vals_list.XtoAryAndClear(Object[].class));
|
||||
cmd_wtr.Append_db_obj_ary(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStrAndClear());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
32
140_dbs/src_120_sql/gplx/dbs/Sql_cmd_wtr.java
Normal file
32
140_dbs/src_120_sql/gplx/dbs/Sql_cmd_wtr.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.*;
|
||||
interface Sql_cmd_wtr {
|
||||
String XtoSqlQry(Db_qry qry, boolean prepare);
|
||||
String XtoSql_insert(Db_qry_insert qry);
|
||||
String XtoSql_delete(Db_qry_delete qry);
|
||||
String XtoSql_update(Db_qry_update qry);
|
||||
String XtoSql_select(Db_qry_select qry);
|
||||
void BldWhere(String_bldr sb, Criteria crt);
|
||||
void BldValStr(String_bldr sb, Db_arg prm);
|
||||
}
|
||||
class Sql_cmd_wtr_ {
|
||||
public static final Sql_cmd_wtr Ansi = Sql_cmd_wtr_ansi_.default_();
|
||||
public static final Sql_cmd_wtr BackslashSensitive = Sql_cmd_wtr_ansi_.backslash_sensitive_();
|
||||
}
|
||||
288
140_dbs/src_120_sql/gplx/dbs/Sql_cmd_wtr_ansi.java
Normal file
288
140_dbs/src_120_sql/gplx/dbs/Sql_cmd_wtr_ansi.java
Normal file
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
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.*; /*Criteria_bool_base*/
|
||||
class Sql_cmd_wtr_ansi implements Sql_cmd_wtr {
|
||||
boolean prepare = false;
|
||||
public String XtoSqlQry(Db_qry cmd, boolean prepare) {
|
||||
String key = cmd.KeyOfDb_qry(); this.prepare = prepare;
|
||||
if (String_.Eq(key, Db_qry_insert.KeyConst)) return XtoSql_insert((Db_qry_insert)cmd);
|
||||
else if (String_.Eq(key, Db_qry_delete.KeyConst)) return XtoSql_delete((Db_qry_delete)cmd);
|
||||
else if (String_.Eq(key, Db_qry_update.KeyConst)) return XtoSql_update((Db_qry_update)cmd);
|
||||
else if (String_.Eq(key, Db_qry_select.KeyConst)) return XtoSql_select((Db_qry_select)cmd);
|
||||
else if (String_.Eq(key, Db_qry_sql.KeyConst)) return ((Db_qry_sql)cmd).XtoSql();
|
||||
else throw Err_.unhandled(cmd.KeyOfDb_qry());
|
||||
}
|
||||
public String XtoSql_delete(Db_qry_delete cmd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add_many("DELETE FROM ", cmd.BaseTable());
|
||||
BldWhere(sb, cmd.Where());
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public String XtoSql_insert(Db_qry_insert cmd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
if (cmd.Select() != null) {
|
||||
sb.Add_many("INSERT INTO ", cmd.BaseTable(), " (");
|
||||
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(XtoSql_select(cmd.Select()));
|
||||
return sb.XtoStr();
|
||||
}
|
||||
int argCount = cmd.Args().Count();
|
||||
if (argCount == 0) throw Err_.new_("Db_qry_insert has no columns").Add("baseTable", cmd.BaseTable());
|
||||
int last = argCount - 1;
|
||||
sb.Add_many("INSERT INTO ", cmd.BaseTable(), " (");
|
||||
for (int i = 0; i < argCount; 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 < argCount; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
Db_arg prm = (Db_arg)pair.Val();
|
||||
this.BldValStr(sb, prm);
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public String XtoSql_update(Db_qry_update cmd) {
|
||||
int argCount = cmd.Args().Count();
|
||||
if (argCount == 0) throw Err_.new_("Db_qry_update has no columns").Add("baseTable", cmd.BaseTable());
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add_many("UPDATE ", cmd.BaseTable(), " SET ");
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add("=");
|
||||
this.BldValStr(sb, (Db_arg)pair.Val());
|
||||
}
|
||||
BldWhere(sb, cmd.Where());
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public String XtoSql_select(Db_qry_select cmd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
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());
|
||||
}
|
||||
XtoSql_from(sb, cmd.From());
|
||||
BldWhere(sb, cmd.Where());
|
||||
XtoSql_group_by(sb, cmd.GroupBy());
|
||||
XtoSql_order_by(sb, cmd.OrderBy());
|
||||
XtoSql_limit(sb, cmd.Limit());
|
||||
return sb.XtoStr();
|
||||
}
|
||||
void XtoSql_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);
|
||||
}
|
||||
}
|
||||
void XtoSql_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());
|
||||
}
|
||||
}
|
||||
void XtoSql_limit(String_bldr sb, int limit) {
|
||||
if (limit == Db_qry_select.Limit_disabled) return;
|
||||
sb.Add(" LIMIT ").Add(limit);
|
||||
}
|
||||
void XtoSql_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()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
public 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 BldValStr(String_bldr sb, Db_arg prm) {
|
||||
if (prepare) {
|
||||
sb.Add("?");
|
||||
return;
|
||||
}
|
||||
Object val = prm.Val();
|
||||
if (val == null) {
|
||||
sb.Add("NULL");
|
||||
return;
|
||||
}
|
||||
Class<?> valType = val.getClass();
|
||||
if (valType == Boolean.class)
|
||||
sb.Add_obj(Bool_.XtoInt(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
|
||||
( valType == Byte.class || valType == Short.class
|
||||
|| valType == Integer.class || valType == Long.class
|
||||
|| valType == Float.class || valType == Double.class
|
||||
)
|
||||
sb.Add(Object_.XtoStr_OrNull(val));
|
||||
else if (valType == DateAdp.class)
|
||||
XtoSqlVal_DateAdp(sb, prm, (DateAdp)val);
|
||||
else if (valType == DecimalAdp.class) {
|
||||
DecimalAdp valDecimal = (DecimalAdp)val;
|
||||
sb.Add(valDecimal.XtoStr());
|
||||
}
|
||||
// else if (valType == System.Enum.class)
|
||||
// sb.Add_any(Enm_.XtoInt(val)); // save enum as 0 or 1, since (a) no db supports enum datatype; (b) names are fungible; (c) int is less space than name
|
||||
else {
|
||||
String valString = Object_.XtoStr_OrNull(val);
|
||||
XtoSqlVal_Str(sb, prm, valString);
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public void XtoSqlVal_Str(String_bldr sb, Db_arg prm, String s) {
|
||||
sb.Add_many("'", String_.Replace(s, "'", "''"), "'"); // stupid escaping of '
|
||||
}
|
||||
@gplx.Virtual public void XtoSqlVal_DateAdp(String_bldr sb, Db_arg prm, DateAdp s) {
|
||||
sb.Add_many("'", s.XtoStr_gplx_long(), "'"); // stupid escaping of '
|
||||
}
|
||||
void BldWhere(String_bldr sb, Sql_where where) {
|
||||
if (where == null || where.Crt() == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.BldWhere(sb, where.Crt());
|
||||
}
|
||||
public void BldWhere(String_bldr sb, Criteria crt) {
|
||||
Criteria_bool_base boolOp = Criteria_bool_base.as_(crt);
|
||||
if (boolOp != null) {
|
||||
sb.Add("(");
|
||||
BldWhere(sb, boolOp.Lhs());
|
||||
sb.Add_many(" ", boolOp.OpLiteral(), " ");
|
||||
BldWhere(sb, boolOp.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());
|
||||
AppendWhereItem(sb, leaf.Crt_of_GfoFldCrt());
|
||||
}
|
||||
}
|
||||
void AppendWhereItem(String_bldr sb, Criteria crt) {
|
||||
switch (crt.Crt_tid()) {
|
||||
case Criteria_.Tid_eq: AppendEqual(sb, Criteria_eq.as_(crt)); break;
|
||||
case Criteria_.Tid_comp: AppendCompare(sb, Criteria_comp.as_(crt)); break;
|
||||
case Criteria_.Tid_between: AppendBetween(sb, Criteria_between.as_(crt)); break;
|
||||
case Criteria_.Tid_in: AppendIn(sb, Criteria_in.as_(crt)); break;
|
||||
case Criteria_.Tid_like: AppendLike(sb, Criteria_like.as_(crt)); break;
|
||||
case Criteria_.Tid_iomatch: AppendIoMatch(sb, Criteria_ioMatch.as_(crt)); break;
|
||||
default: throw Err_.unhandled(crt);
|
||||
}
|
||||
}
|
||||
void AppendEqual(String_bldr sb, Criteria_eq crt) {
|
||||
sb.Add(crt.Negated() ? "!=" : "=");
|
||||
this.BldValStr(sb, Wrap(crt.Value()));
|
||||
}
|
||||
void AppendCompare(String_bldr sb, Criteria_comp crt) {
|
||||
sb.Add_many(crt.XtoSymbol());
|
||||
this.BldValStr(sb, Wrap(crt.Value()));
|
||||
}
|
||||
void AppendBetween(String_bldr sb, Criteria_between crt) {
|
||||
sb.Add(crt.Negated() ? " NOT BETWEEN " : " BETWEEN ");
|
||||
this.BldValStr(sb, Wrap(crt.Lhs()));
|
||||
sb.Add(" AND ");
|
||||
this.BldValStr(sb, Wrap(crt.Rhs()));
|
||||
}
|
||||
void AppendLike(String_bldr sb, Criteria_like crt) {
|
||||
sb.Add(crt.Negated() ? " NOT LIKE " : " LIKE ");
|
||||
this.BldValStr(sb, Wrap(crt.Pattern().Raw()));
|
||||
sb.Add_fmt(" ESCAPE '{0}'", crt.Pattern().Escape());
|
||||
}
|
||||
void AppendIn(String_bldr sb, Criteria_in crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IN (" : " IN (");
|
||||
int last = crt.Values().length - 1;
|
||||
for (int i = 0; i < crt.Values().length; i++) {
|
||||
Object val = crt.Values()[i];
|
||||
this.BldValStr(sb, Wrap(val));
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
}
|
||||
void AppendIoMatch(String_bldr sb, Criteria_ioMatch crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IOMATCH " : " IOMATCH ");
|
||||
this.BldValStr(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_.XtoStr_OrEmpty(val));
|
||||
if (quote) sb.Add("'");
|
||||
}
|
||||
sb.Add(")");
|
||||
}
|
||||
}
|
||||
Db_arg Wrap(Object val) {return new Db_arg("unknown", val);}
|
||||
}
|
||||
class Sql_cmd_wtr_ansi_ {
|
||||
public static Sql_cmd_wtr default_() {return new Sql_cmd_wtr_ansi();}
|
||||
public static Sql_cmd_wtr backslash_sensitive_() {return Sql_cmd_wtr_ansi_backslashSensitive.new_();}
|
||||
}
|
||||
class Sql_cmd_wtr_ansi_backslashSensitive extends Sql_cmd_wtr_ansi { @Override public void XtoSqlVal_Str(String_bldr sb, Db_arg prm, String s) {
|
||||
if (String_.Has(s, "\\")) s = String_.Replace(s, "\\", "\\\\");
|
||||
super.XtoSqlVal_Str(sb, prm, s);
|
||||
}
|
||||
public static Sql_cmd_wtr_ansi_backslashSensitive new_() {return new Sql_cmd_wtr_ansi_backslashSensitive();} Sql_cmd_wtr_ansi_backslashSensitive() {}
|
||||
}
|
||||
67
140_dbs/src_120_sql/gplx/dbs/Sql_join_itm.java
Normal file
67
140_dbs/src_120_sql/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_120_sql/gplx/dbs/Sql_order_by.java
Normal file
73
140_dbs/src_120_sql/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;
|
||||
}
|
||||
}
|
||||
143
140_dbs/src_120_sql/gplx/dbs/Sql_select.java
Normal file
143
140_dbs/src_120_sql/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() {}
|
||||
}
|
||||
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() {}
|
||||
}
|
||||
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() {}
|
||||
}
|
||||
32
140_dbs/src_120_sql/gplx/dbs/Sql_where.java
Normal file
32
140_dbs/src_120_sql/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() {}
|
||||
}
|
||||
Reference in New Issue
Block a user