mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Source: Restore broken commit
This commit is contained in:
23
140_dbs/src/gplx/dbs/sqls/Sql_qry_wtr.java
Normal file
23
140_dbs/src/gplx/dbs/sqls/Sql_qry_wtr.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.wtrs.*;
|
||||
public interface Sql_qry_wtr {
|
||||
String To_sql_str(Db_qry qry, boolean mode_is_prep);
|
||||
Sql_schema_wtr Schema_wtr();
|
||||
}
|
||||
29
140_dbs/src/gplx/dbs/sqls/Sql_qry_wtr_.java
Normal file
29
140_dbs/src/gplx/dbs/sqls/Sql_qry_wtr_.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.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.sqls.wtrs.*;
|
||||
public class Sql_qry_wtr_ {
|
||||
public static Sql_qry_wtr New__basic() {return new Sql_core_wtr();}
|
||||
public static Sql_qry_wtr New__mysql() {return new Sql_core_wtr__mysql();}
|
||||
public static Sql_qry_wtr New__sqlite() {return new Sql_core_wtr__sqlite();}
|
||||
|
||||
public static final byte Like_wildcard = Byte_ascii.Percent;
|
||||
public static String Quote_arg(String s) { // only for constructing DEBUG SQL strings
|
||||
return "'" + String_.Replace(s, "'", "''") + "'";
|
||||
}
|
||||
}
|
||||
36
140_dbs/src/gplx/dbs/sqls/itms/Db_obj_ary_crt.java
Normal file
36
140_dbs/src/gplx/dbs/sqls/itms/Db_obj_ary_crt.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
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.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Db_obj_ary_crt implements gplx.core.criterias.Criteria {
|
||||
public byte Tid() {return Criteria_.Tid_db_obj_ary;}
|
||||
public Db_obj_ary_fld[] Flds() {return flds;} public Db_obj_ary_crt Flds_(Db_obj_ary_fld[] v) {this.flds = v; return this;} private Db_obj_ary_fld[] flds;
|
||||
public Object[][] Vals() {return vals;} public void Vals_(Object[][] v) {this.vals = v;} private Object[][] vals;
|
||||
public void Val_from_args(Hash_adp args) {throw Err_.new_unimplemented();}
|
||||
public void Val_as_obj_(Object v) {throw Err_.new_unimplemented();}
|
||||
public boolean Matches(Object obj) {return false;}
|
||||
public String To_str() {return "";}
|
||||
public static Db_obj_ary_crt new_(Db_obj_ary_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_obj_ary_fld[] flds = new Db_obj_ary_fld[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
flds[i] = new Db_obj_ary_fld(names[i], type_tid);
|
||||
return new Db_obj_ary_crt().Flds_(flds);
|
||||
}
|
||||
}
|
||||
23
140_dbs/src/gplx/dbs/sqls/itms/Db_obj_ary_fld.java
Normal file
23
140_dbs/src/gplx/dbs/sqls/itms/Db_obj_ary_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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Db_obj_ary_fld {
|
||||
public Db_obj_ary_fld(String name, int type_tid) {this.name = name; this.type_tid = type_tid;}
|
||||
public String Name() {return name;} public Db_obj_ary_fld Name_(String v) {name = v; return this;} private String name;
|
||||
public int Type_tid() {return type_tid;} public Db_obj_ary_fld Type_tid_(int v) {type_tid = v; return this;} private int type_tid;
|
||||
}
|
||||
44
140_dbs/src/gplx/dbs/sqls/itms/Db_obj_ary_tst.java
Normal file
44
140_dbs/src/gplx/dbs/sqls/itms/Db_obj_ary_tst.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
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.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import org.junit.*; import gplx.core.strings.*; import gplx.dbs.sqls.*;
|
||||
import gplx.dbs.sqls.wtrs.*;
|
||||
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", Type_adp_.Tid__int).Init_fld("fld_1", Type_adp_.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", Type_adp_.Tid__int).Init_fld("fld_1", Type_adp_.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 final Db_obj_ary_crt crt = new Db_obj_ary_crt();
|
||||
private final Sql_wtr_ctx ctx = new Sql_wtr_ctx(false);
|
||||
public Db_obj_ary_fxt Init_fld(String name, int tid) {flds_list.Add(new Db_obj_ary_fld(name, tid)); return this;} private List_adp flds_list = List_adp_.New();
|
||||
public Db_obj_ary_fxt Init_vals(Object... ary) {vals_list.Add(ary); return this;} private List_adp vals_list = List_adp_.New();
|
||||
public Db_obj_ary_fxt Test_sql(String expd) {
|
||||
Sql_core_wtr cmd_wtr = (Sql_core_wtr)Sql_qry_wtr_.New__basic();
|
||||
crt.Flds_((Db_obj_ary_fld[])flds_list.To_ary_and_clear(Db_obj_ary_fld.class));
|
||||
crt.Vals_((Object[][])vals_list.To_ary_and_clear(Object[].class));
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
cmd_wtr.Where_wtr().Bld_where__db_obj(bfr, ctx, crt);
|
||||
Tfds.Eq(expd, bfr.To_str_and_clear());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
26
140_dbs/src/gplx/dbs/sqls/itms/Sql_from_clause.java
Normal file
26
140_dbs/src/gplx/dbs/sqls/itms/Sql_from_clause.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
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.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_from_clause {
|
||||
public Sql_from_clause(Sql_tbl_itm base_tbl) {
|
||||
this.Base_tbl = base_tbl;
|
||||
Tbls.Add(base_tbl);
|
||||
}
|
||||
public final List_adp Tbls = List_adp_.New();
|
||||
public final Sql_tbl_itm Base_tbl;
|
||||
}
|
||||
28
140_dbs/src/gplx/dbs/sqls/itms/Sql_group_clause.java
Normal file
28
140_dbs/src/gplx/dbs/sqls/itms/Sql_group_clause.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_group_clause {
|
||||
public List_adp Flds() {return flds;} List_adp flds = List_adp_.New();
|
||||
|
||||
public static Sql_group_clause new_(String... ary) {
|
||||
Sql_group_clause rv = new Sql_group_clause();
|
||||
for (String itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_group_clause() {}
|
||||
}
|
||||
34
140_dbs/src/gplx/dbs/sqls/itms/Sql_join_fld.java
Normal file
34
140_dbs/src/gplx/dbs/sqls/itms/Sql_join_fld.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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_join_fld {
|
||||
public Sql_join_fld(String trg_fld, String src_tbl, String src_fld) {
|
||||
this.Trg_fld = trg_fld;
|
||||
this.Src_tbl = src_tbl;
|
||||
this.Src_fld = src_fld;
|
||||
}
|
||||
public final String Src_tbl;
|
||||
public final String Src_fld;
|
||||
public final String Trg_fld;
|
||||
|
||||
public String To_fld_sql(boolean fld_is_src, String trg_tbl) {
|
||||
return fld_is_src ? Src_tbl + "." + Src_fld : trg_tbl + "." + Trg_fld;
|
||||
}
|
||||
|
||||
public static final Sql_join_fld[] Ary__empty = new Sql_join_fld[0];
|
||||
}
|
||||
27
140_dbs/src/gplx/dbs/sqls/itms/Sql_order_clause.java
Normal file
27
140_dbs/src/gplx/dbs/sqls/itms/Sql_order_clause.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_order_clause {
|
||||
private final List_adp list = List_adp_.New();
|
||||
private Sql_order_fld[] ary;
|
||||
public void Flds__add(Sql_order_fld fld) {list.Add(fld);}
|
||||
public Sql_order_fld[] Flds() {
|
||||
if (ary == null) ary = (Sql_order_fld[])list.To_ary_and_clear(Sql_order_fld.class);
|
||||
return ary;
|
||||
}
|
||||
}
|
||||
37
140_dbs/src/gplx/dbs/sqls/itms/Sql_order_fld.java
Normal file
37
140_dbs/src/gplx/dbs/sqls/itms/Sql_order_fld.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_order_fld {
|
||||
public Sql_order_fld(String tbl, String name, byte sort) {this.Tbl = tbl; this.Name = name; this.Sort = sort;}
|
||||
public final String Tbl;
|
||||
public final String Name;
|
||||
public final byte Sort;
|
||||
public String To_sql() {
|
||||
String rv = this.Name;
|
||||
if (Tbl != null) rv = Tbl + "." + rv;
|
||||
switch (Sort) {
|
||||
case Sort__asc: rv += " ASC"; break;
|
||||
case Sort__dsc: rv += " DESC"; break;
|
||||
case Sort__nil: break;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static final String Tbl__null = String_.Null;
|
||||
public static final byte Sort__asc = Bool_.Y_byte, Sort__dsc = Bool_.N_byte, Sort__nil = Bool_.__byte;
|
||||
}
|
||||
41
140_dbs/src/gplx/dbs/sqls/itms/Sql_order_fld_sorter.java
Normal file
41
140_dbs/src/gplx/dbs/sqls/itms/Sql_order_fld_sorter.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.gfo_ndes.*;
|
||||
import gplx.core.lists.*;
|
||||
public class Sql_order_fld_sorter implements ComparerAble {
|
||||
public int compare(Object lhsObj, Object rhsObj) {
|
||||
GfoNde lhs = (GfoNde)lhsObj; GfoNde rhs = (GfoNde)rhsObj;
|
||||
Sql_order_fld 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.Sort == Sql_order_fld.Sort__dsc ? -1 : 1;
|
||||
return compare * ascendingVal;
|
||||
}
|
||||
return CompareAble_.Same;
|
||||
}
|
||||
Sql_order_fld[] items;
|
||||
public static ComparerAble new_(Sql_order_fld[] items) {
|
||||
Sql_order_fld_sorter rv = new Sql_order_fld_sorter();
|
||||
rv.items = items;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
22
140_dbs/src/gplx/dbs/sqls/itms/Sql_select_clause.java
Normal file
22
140_dbs/src/gplx/dbs/sqls/itms/Sql_select_clause.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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_select_clause {
|
||||
public boolean Distinct = false;
|
||||
public final Sql_select_fld_list Flds = new Sql_select_fld_list();
|
||||
}
|
||||
66
140_dbs/src/gplx/dbs/sqls/itms/Sql_select_fld.java
Normal file
66
140_dbs/src/gplx/dbs/sqls/itms/Sql_select_fld.java
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
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.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.gfo_ndes.*; import gplx.core.type_xtns.*;
|
||||
public abstract class Sql_select_fld {
|
||||
public Sql_select_fld(String tbl, String fld, String alias) {
|
||||
this.Tbl = tbl; this.Fld = fld; this.Alias = alias;
|
||||
}
|
||||
public final String Tbl; // tbl_alias; EX: "t1."
|
||||
public final String Fld; // fld_key; EX: "fld"
|
||||
public final String Alias; // alias; EX: " AS [Field Name]"; NOTE: must be fld name if no alias defined; EX: "SELECT fld1" should have tbl='', fld='fld1' and alias='fld1'
|
||||
public abstract String To_fld_sql(); // EX: "t1.fld AS [Field Name]"
|
||||
public String To_fld_alias() {return Alias;} // EX: "t1.fld AS [Field Name]" -> "Field Name"; "t1.fld1" -> "fld1"
|
||||
public String To_fld_key() {return Tbl == Tbl__null ? Fld : Tbl + "." + Fld;}
|
||||
|
||||
public static final String Tbl__null = null, Fld__wildcard = "*";
|
||||
public static Sql_select_fld New_fld (String tbl, String fld, String alias) {return new Sql_select_fld_col(tbl, fld, alias);}
|
||||
public static Sql_select_fld Wildcard = Sql_select_fld_wild.Instance;
|
||||
public static Sql_select_fld New_count (String tbl, String fld, String alias) {return new Sql_select_fld_count(tbl, fld, alias);}
|
||||
public static Sql_select_fld New_sum (String tbl, String fld, String alias) {return new Sql_select_fld_sum(tbl, fld, alias);}
|
||||
public static Sql_select_fld New_min (String tbl, String fld, String alias) {return new Sql_select_fld_minMax(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld New_max (String tbl, String fld, String alias) {return new Sql_select_fld_minMax(CompareAble_.More, tbl, fld, alias);}
|
||||
public static String Bld_tbl_w_fld(String tbl, String fld) {return tbl == null ? fld : tbl + "." + fld;}
|
||||
|
||||
// tdb related functions
|
||||
public ClassXtn Val_type() {return val_type;} public void Val_type_(ClassXtn val) {val_type = val;} private ClassXtn val_type = ObjectClassXtn.Instance;
|
||||
public abstract Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type);
|
||||
@gplx.Virtual public void GroupBy_type(ClassXtn type) {this.Val_type_(type);}
|
||||
}
|
||||
class Sql_select_fld_wild extends Sql_select_fld { Sql_select_fld_wild() {super(Sql_select_fld.Tbl__null, Fld__wildcard, Fld__wildcard);}
|
||||
@Override public String To_fld_sql() {return Fld__wildcard;}
|
||||
|
||||
public static final Sql_select_fld_wild Instance = new Sql_select_fld_wild();
|
||||
|
||||
// tdb-related functions
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {throw Err_.new_wo_type("group by eval not allowed on *");}
|
||||
@Override public void GroupBy_type(ClassXtn type) {throw Err_.new_wo_type("group by type not allowed on *");}
|
||||
}
|
||||
class Sql_select_fld_col extends Sql_select_fld { public Sql_select_fld_col(String tbl, String fld, String alias) {super(tbl, fld, alias);}
|
||||
@Override public String To_fld_sql() {
|
||||
String rv = Fld;
|
||||
if (this.Tbl != Tbl__null)
|
||||
rv = this.Tbl + "." + Fld;
|
||||
if (!String_.Eq(Alias, Fld))
|
||||
rv = rv + " AS " + Alias;
|
||||
return rv;
|
||||
}
|
||||
|
||||
// tdb-related functions
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {return curVal;}
|
||||
}
|
||||
52
140_dbs/src/gplx/dbs/sqls/itms/Sql_select_fld_func.java
Normal file
52
140_dbs/src/gplx/dbs/sqls/itms/Sql_select_fld_func.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
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.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.type_xtns.*;
|
||||
abstract class Sql_select_fld_func extends Sql_select_fld { public Sql_select_fld_func(String tbl, String fld, String alias) {super(tbl, fld, alias);}
|
||||
public abstract String XtoSql_functionName();
|
||||
@Override public String To_fld_sql() {
|
||||
return String_.Format("{0}({1}) AS {2}", XtoSql_functionName(), Fld, Alias);
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_count extends Sql_select_fld_func { public Sql_select_fld_count(String tbl, String fld, String alias) {super(tbl, fld, alias);}
|
||||
@Override public String XtoSql_functionName() {return "COUNT";}
|
||||
@Override public void GroupBy_type(ClassXtn type) {this.Val_type_(IntClassXtn.Instance);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return 1;
|
||||
return Int_.cast(groupByVal) + 1;
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_sum extends Sql_select_fld_func { public Sql_select_fld_sum(String tbl, String fld, String alias) {super(tbl, fld, alias);}
|
||||
@Override public String XtoSql_functionName() {return "SUM";}
|
||||
@Override public void GroupBy_type(ClassXtn type) {this.Val_type_(IntClassXtn.Instance);}
|
||||
@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);
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_minMax extends Sql_select_fld_func { private final int compareType;
|
||||
public Sql_select_fld_minMax(int compareType, String tbl, String fld, String alias) {super(tbl, fld, alias);
|
||||
this.compareType = compareType;
|
||||
}
|
||||
@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;
|
||||
}
|
||||
}
|
||||
25
140_dbs/src/gplx/dbs/sqls/itms/Sql_select_fld_list.java
Normal file
25
140_dbs/src/gplx/dbs/sqls/itms/Sql_select_fld_list.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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_select_fld_list {
|
||||
private final Ordered_hash hash = Ordered_hash_.New();
|
||||
public int Len() {return hash.Count();}
|
||||
public Sql_select_fld_list Clear() {hash.Clear();return this;}
|
||||
public Sql_select_fld Get_at(int i) {return (Sql_select_fld)hash.Get_at(i);}
|
||||
public void Add(Sql_select_fld fld) {hash.Add(fld.Alias, fld);}
|
||||
}
|
||||
44
140_dbs/src/gplx/dbs/sqls/itms/Sql_tbl_itm.java
Normal file
44
140_dbs/src/gplx/dbs/sqls/itms/Sql_tbl_itm.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
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.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_tbl_itm {
|
||||
public Sql_tbl_itm(int join_tid, String db, String name, String alias, Sql_join_fld[] join_flds) {
|
||||
this.Join_tid = join_tid;
|
||||
this.Db = db;
|
||||
this.Name = name;
|
||||
this.Alias = alias;
|
||||
this.Join_flds = join_flds;
|
||||
}
|
||||
public final int Join_tid;
|
||||
public final String Db;
|
||||
public final String Name;
|
||||
public final String Alias;
|
||||
public boolean Db_enabled = true;
|
||||
public final Sql_join_fld[] Join_flds;
|
||||
|
||||
public static final String Alias__null = String_.Null;
|
||||
public static final String Db__null = String_.Null;
|
||||
public static final int
|
||||
Tid__from = 0 // "FROM"
|
||||
, Tid__inner = 1 // "INNER JOIN"
|
||||
, Tid__left = 2 // "LEFT JOIN"
|
||||
, Tid__right = 3 // "RIGHT JOIN"
|
||||
, Tid__outer = 4 // "OUTER JOIN"
|
||||
, Tid__cross = 5 // "CROSS JOIN"
|
||||
;
|
||||
}
|
||||
25
140_dbs/src/gplx/dbs/sqls/itms/Sql_where_clause.java
Normal file
25
140_dbs/src/gplx/dbs/sqls/itms/Sql_where_clause.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.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.criterias.*;
|
||||
public class Sql_where_clause {
|
||||
public Criteria Root = Criteria_.All;
|
||||
public static final Object Where__null = null;
|
||||
|
||||
public static final Sql_where_clause All = new Sql_where_clause();
|
||||
}
|
||||
105
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_core_wtr.java
Normal file
105
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_core_wtr.java
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
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.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.qrys.*; import gplx.dbs.sqls.wtrs.*; import gplx.dbs.sqls.itms.*;
|
||||
public class Sql_core_wtr implements Sql_qry_wtr {
|
||||
private final Bry_bfr bfr = Bry_bfr_.New_w_size(64);
|
||||
public byte[] Seq__nl = Byte_ascii.Space_bry;
|
||||
public byte Seq__quote = Byte_ascii.Apos, Seq__escape = Byte_ascii.Backslash;
|
||||
public Sql_core_wtr() {
|
||||
this.val_wtr = Make__val_wtr();
|
||||
this.from_wtr = Make__from_wtr();
|
||||
this.where_wtr = Make__where_wtr(this, val_wtr);
|
||||
this.select_wtr = Make__select_wtr(this);
|
||||
this.schema_wtr = Make__schema_wtr();
|
||||
}
|
||||
public Sql_schema_wtr Schema_wtr() {return schema_wtr;} private final Sql_schema_wtr schema_wtr;
|
||||
public Sql_val_wtr Val_wtr() {return val_wtr;} private final Sql_val_wtr val_wtr;
|
||||
public Sql_from_wtr From_wtr() {return from_wtr;} private final Sql_from_wtr from_wtr;
|
||||
public Sql_where_wtr Where_wtr() {return where_wtr;} private final Sql_where_wtr where_wtr;
|
||||
public Sql_select_wtr Select_wtr() {return select_wtr;} private final Sql_select_wtr select_wtr;
|
||||
public String To_sql_str(Db_qry qry, boolean mode_is_prep) {
|
||||
synchronized (bfr) {
|
||||
Sql_wtr_ctx ctx = new Sql_wtr_ctx(mode_is_prep);
|
||||
switch (qry.Tid()) {
|
||||
case Db_qry_.Tid_insert: return Bld_qry_insert(ctx, (Db_qry_insert)qry);
|
||||
case Db_qry_.Tid_delete: return Bld_qry_delete(ctx, (Db_qry_delete)qry);
|
||||
case Db_qry_.Tid_update: return Bld_qry_update(ctx, (Db_qry_update)qry);
|
||||
case Db_qry_.Tid_select_in_tbl:
|
||||
case Db_qry_.Tid_select: select_wtr.Bld_qry_select(bfr, ctx, (Db_qry__select_cmd)qry); return bfr.To_str_and_clear();
|
||||
case Db_qry_.Tid_pragma: return ((gplx.dbs.engines.sqlite.Sqlite_pragma)qry).To_sql__exec(this);
|
||||
case Db_qry_.Tid_sql: return ((Db_qry_sql)qry).To_sql__exec(this);
|
||||
default: throw Err_.new_unhandled(qry.Tid());
|
||||
}
|
||||
}
|
||||
}
|
||||
private String Bld_qry_delete(Sql_wtr_ctx ctx, Db_qry_delete qry) {
|
||||
bfr.Add_str_u8_many("DELETE FROM ", qry.Base_table());
|
||||
where_wtr.Bld_where(bfr, ctx, qry.Where());
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_insert(Sql_wtr_ctx ctx, Db_qry_insert qry) {
|
||||
if (qry.Select() != null) {
|
||||
bfr.Add_str_u8_many("INSERT INTO ", qry.Base_table(), " (");
|
||||
int cols_len = qry.Cols().Len();
|
||||
for (int i = 0; i < cols_len; i++) {
|
||||
Sql_select_fld fld = qry.Cols().Get_at(i);
|
||||
bfr.Add_str_a7(fld.Alias);
|
||||
bfr.Add_str_a7(i == cols_len - 1 ? ") " : ", ");
|
||||
}
|
||||
select_wtr.Bld_qry_select(bfr, ctx, qry.Select());
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
int arg_count = qry.Args().Count(); if (arg_count == 0) throw Err_.new_wo_type("Db_qry_insert has no columns", "base_table", qry.Base_table());
|
||||
int last = arg_count - 1;
|
||||
bfr.Add_str_u8_many("INSERT INTO ", qry.Base_table(), " (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
Keyval pair = qry.Args().Get_at(i);
|
||||
this.Bld_col_name(bfr, pair.Key());
|
||||
bfr.Add_str_a7(i == last ? ")" : ", ");
|
||||
}
|
||||
bfr.Add_str_a7(" VALUES (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
Keyval pair = qry.Args().Get_at(i);
|
||||
Db_arg arg = (Db_arg)pair.Val();
|
||||
val_wtr.Bld_val(bfr, ctx, arg.Val);
|
||||
bfr.Add_str_a7(i == last ? ")" : ", ");
|
||||
}
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_update(Sql_wtr_ctx ctx, Db_qry_update qry) {
|
||||
int arg_count = qry.Args().Count(); if (arg_count == 0) throw Err_.new_wo_type("Db_qry_update has no columns", "base_table", qry.Base_table());
|
||||
bfr.Add_str_u8_many("UPDATE ", qry.Base_table(), " SET ");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
Keyval pair = qry.Args().Get_at(i);
|
||||
if (i > 0) bfr.Add_str_a7(", ");
|
||||
this.Bld_col_name(bfr, pair.Key());
|
||||
bfr.Add_str_a7("=");
|
||||
Db_arg arg = (Db_arg)pair.Val();
|
||||
val_wtr.Bld_val(bfr, ctx, arg.Val);
|
||||
}
|
||||
where_wtr.Bld_where(bfr, ctx, qry.Where());
|
||||
return bfr.To_str_and_clear();
|
||||
}
|
||||
public void Bld_col_name(Bry_bfr bfr, String key) {bfr.Add_str_u8(key);}
|
||||
@gplx.Virtual protected Sql_val_wtr Make__val_wtr () {return new Sql_val_wtr();}
|
||||
@gplx.Virtual protected Sql_from_wtr Make__from_wtr () {return new Sql_from_wtr();}
|
||||
@gplx.Virtual protected Sql_select_wtr Make__select_wtr(Sql_core_wtr qry_wtr) {return new Sql_select_wtr(qry_wtr);}
|
||||
@gplx.Virtual protected Sql_where_wtr Make__where_wtr (Sql_core_wtr qry_wtr, Sql_val_wtr val_wtr) {return new Sql_where_wtr(qry_wtr, val_wtr);}
|
||||
@gplx.Virtual protected Sql_schema_wtr Make__schema_wtr() {return new Sql_schema_wtr();}
|
||||
}
|
||||
20
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_core_wtr__mysql.java
Normal file
20
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_core_wtr__mysql.java
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
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.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_core_wtr__mysql extends Sql_core_wtr { @Override protected Sql_val_wtr Make__val_wtr() {return new Sql_val_wtr_mysql();}
|
||||
}
|
||||
21
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_core_wtr__sqlite.java
Normal file
21
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_core_wtr__sqlite.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.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_core_wtr__sqlite extends Sql_core_wtr { @Override protected Sql_val_wtr Make__val_wtr() {return new Sql_val_wtr_sqlite();}
|
||||
@Override protected Sql_select_wtr Make__select_wtr(Sql_core_wtr qry_wtr) {return new Sql_select_wtr_sqlite(qry_wtr);}
|
||||
}
|
||||
36
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_core_wtr_fxt.java
Normal file
36
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_core_wtr_fxt.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
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.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.criterias.*;
|
||||
class Sql_core_wtr_fxt {
|
||||
private final Sql_core_wtr__sqlite wtr = new Sql_core_wtr__sqlite();
|
||||
private final Sql_wtr_ctx ctx = new Sql_wtr_ctx(false);
|
||||
private final Bry_bfr bfr = Bry_bfr_.New();
|
||||
public Sql_core_wtr_fxt Sql_wtr_(Sql_qry_wtr v) {sql_wtr = v; return this;} private Sql_qry_wtr sql_wtr = Sql_qry_wtr_.New__sqlite();
|
||||
public void Test__val(Object val, String expd) {
|
||||
wtr.Val_wtr().Bld_val(bfr, ctx, val);
|
||||
Tfds.Eq_str(expd, bfr.To_str_and_clear());
|
||||
}
|
||||
public void Test__where(Criteria crt, String... expd) {
|
||||
wtr.Where_wtr().Bld_where_elem(bfr, ctx, crt);
|
||||
Tfds.Eq_str_lines(String_.Concat_lines_nl_skip_last(expd), bfr.To_str_and_clear());
|
||||
}
|
||||
public void Test__qry(Db_qry qry, String expd) {
|
||||
Tfds.Eq_str_lines(expd, sql_wtr.To_sql_str(qry, Bool_.N));
|
||||
}
|
||||
}
|
||||
54
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_from_wtr.java
Normal file
54
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_from_wtr.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.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.dbs.sqls.itms.*;
|
||||
public class Sql_from_wtr {
|
||||
public void Bld_clause_from(Bry_bfr bfr, Sql_from_clause from) {
|
||||
List_adp tbls = from.Tbls; int tbls_len = tbls.Count();
|
||||
for (int i = 0; i < tbls_len; ++i) {
|
||||
Sql_tbl_itm tbl = (Sql_tbl_itm)tbls.Get_at(i);
|
||||
bfr.Add_byte_space().Add_str_a7(Bld_join(tbl.Join_tid)).Add_byte_space();
|
||||
if (tbl.Db != Sql_tbl_itm.Db__null && tbl.Db_enabled)
|
||||
bfr.Add_str_u8(tbl.Db).Add_byte_dot();
|
||||
bfr.Add_str_u8(tbl.Name);
|
||||
if (tbl.Alias != Sql_tbl_itm.Alias__null)
|
||||
bfr.Add_byte_space().Add_str_u8(tbl.Alias);
|
||||
String tbl_alias = tbl.Alias == null ? tbl.Name : tbl.Alias;
|
||||
Sql_join_fld[] flds = tbl.Join_flds; int flds_len = flds.length;
|
||||
for (int j = 0; j < flds_len; ++j) {
|
||||
Sql_join_fld join_fld = flds[j];
|
||||
bfr.Add_str_a7(j == 0 ? " ON " : " AND ");
|
||||
bfr.Add_str_u8(join_fld.Src_tbl).Add_byte_dot().Add_str_u8(join_fld.Src_fld);
|
||||
bfr.Add(Bry__join_eq);
|
||||
bfr.Add_str_u8(tbl_alias).Add_byte_dot().Add_str_u8(join_fld.Trg_fld);
|
||||
}
|
||||
}
|
||||
}
|
||||
private String Bld_join(int tid) {
|
||||
switch (tid) {
|
||||
case Sql_tbl_itm.Tid__from : return "FROM";
|
||||
case Sql_tbl_itm.Tid__inner : return "INNER JOIN";
|
||||
case Sql_tbl_itm.Tid__left : return "LEFT JOIN";
|
||||
case Sql_tbl_itm.Tid__right : return "RIGHT JOIN";
|
||||
case Sql_tbl_itm.Tid__outer : return "OUTER JOIN";
|
||||
case Sql_tbl_itm.Tid__cross : return "CROSS JOIN";
|
||||
default : throw Err_.new_unhandled_default(tid);
|
||||
}
|
||||
}
|
||||
private static final byte[] Bry__join_eq = Bry_.new_a7(" = ");
|
||||
}
|
||||
33
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_from_wtr_tst.java
Normal file
33
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_from_wtr_tst.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.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import org.junit.*;
|
||||
public class Sql_from_wtr_tst {
|
||||
private final Sql_core_wtr_fxt fxt = new Sql_core_wtr_fxt();
|
||||
@Test public void Abrv() {
|
||||
fxt.Test__qry(Db_qry_.select_().Cols_all_().From_("tbl", "t"), "SELECT * FROM tbl t");
|
||||
}
|
||||
@Test public void Db() {
|
||||
fxt.Test__qry(Db_qry_.select_().Cols_all_().From_("db", "tbl", "t"), "SELECT * FROM db.tbl t");
|
||||
}
|
||||
@Test public void Join() {
|
||||
fxt.Test__qry
|
||||
( Db_qry_.select_().Cols_all_().From_("src", "s").Join_("trg", "t", Db_qry_.New_join__join("trg_id", "s", "src_id"))
|
||||
, "SELECT * FROM src s INNER JOIN trg t ON s.src_id = t.trg_id");
|
||||
}
|
||||
}
|
||||
99
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_qry_wtr__ansi__tst.java
Normal file
99
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_qry_wtr__ansi__tst.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
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.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import org.junit.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_qry_wtr__ansi__tst {
|
||||
Sql_qry_wtr sqlWtr = Sql_qry_wtr_.New__basic();
|
||||
@Test public void Insert() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.insert_("people").Val_int("id", 1).Val_str("name", "me")
|
||||
, "INSERT INTO people (id, name) VALUES (1, 'me')"
|
||||
);
|
||||
}
|
||||
@Test public void Delete() {
|
||||
Criteria crt = Db_crt_.New_eq("id", 1);
|
||||
tst_XtoSql
|
||||
( Db_qry_.delete_("people", crt)
|
||||
, "DELETE FROM people WHERE id = 1"
|
||||
);
|
||||
}
|
||||
@Test public void Update() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.update_("people", Db_crt_.New_eq("id", 1)).Val_str("name", "me")
|
||||
, "UPDATE people SET name='me' WHERE id = 1"
|
||||
);
|
||||
}
|
||||
@Test public void SelectAll() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people")
|
||||
, "SELECT * FROM people"
|
||||
);
|
||||
}
|
||||
@Test public void SelectFlds() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().Cols_("id", "name").From_("people")
|
||||
, "SELECT id, name FROM people"
|
||||
);
|
||||
}
|
||||
@Test public void SelectOrderBy() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people").Order_("name", false)
|
||||
, "SELECT * FROM people ORDER BY name DESC"
|
||||
);
|
||||
}
|
||||
@Test public void SelectWhere() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people").Where_(Db_crt_.New_eq("id", 1))
|
||||
, "SELECT * FROM people WHERE id = 1"
|
||||
);
|
||||
}
|
||||
@Test public void Select_From_Alias() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people", "p")
|
||||
, "SELECT * FROM people p"
|
||||
);
|
||||
}
|
||||
@Test public void Select_Join_Alias() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people", "p").Join_("roles", "r", Db_qry_.New_join__same("p", "id"))
|
||||
, "SELECT * FROM people p INNER JOIN roles r ON p.id = r.id"
|
||||
);
|
||||
}
|
||||
@Test public void Prepare() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.insert_("people").Val_int("id", 1).Val_str("name", "me")
|
||||
, "INSERT INTO people (id, name) VALUES (?, ?)"
|
||||
, true
|
||||
);
|
||||
|
||||
tst_XtoSql
|
||||
( Db_qry_.delete_("people", Db_crt_.New_eq("id", 1))
|
||||
, "DELETE FROM people WHERE id = ?"
|
||||
, true
|
||||
);
|
||||
|
||||
tst_XtoSql
|
||||
( Db_qry_.update_("people", Db_crt_.New_eq("id", 1)).Val_str("name", "me")
|
||||
, "UPDATE people SET name=? WHERE id = ?"
|
||||
, true
|
||||
);
|
||||
}
|
||||
void tst_XtoSql(Db_qry cmd, String expd) {tst_XtoSql(cmd, expd, false);}
|
||||
void tst_XtoSql(Db_qry cmd, String expd, boolean prepare) {Tfds.Eq(expd, sqlWtr.To_sql_str(cmd, prepare));}
|
||||
}
|
||||
61
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_qry_wtr__iosql__tst.java
Normal file
61
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_qry_wtr__iosql__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.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import org.junit.*; import gplx.core.strings.*;
|
||||
import gplx.core.criterias.*; /*Criteria_base*/
|
||||
import gplx.core.ios.*; import gplx.dbs.sqls.*; import gplx.dbs.sqls.wtrs.*;
|
||||
public class Sql_qry_wtr__iosql__tst {
|
||||
@Test public void Type() {
|
||||
fld = IoItm_base_.Prop_Type;
|
||||
tst_Write("type = 1", ioCrt_(fld, Criteria_.eq_(IoItmDir.Type_Dir)));
|
||||
tst_Write("type = 2", ioCrt_(fld, Criteria_.eq_(IoItmFil.Type_Fil)));
|
||||
}
|
||||
@Test public void Ext() {
|
||||
fld = IoItm_base_.Prop_Ext;
|
||||
tst_Write("ext = '.txt'", ioCrt_(fld, Criteria_.eq_(".txt")));
|
||||
tst_Write("ext IN ('.txt', '.xml', '.html')", ioCrt_(fld, Criteria_.in_(".txt", ".xml", ".html")));
|
||||
tst_Write("ext NOT IN ('.dll', '.exe')", ioCrt_(fld, Criteria_.inn_(".dll", ".exe")));
|
||||
}
|
||||
@Test public void Title() {
|
||||
fld = IoItm_base_.Prop_Title;
|
||||
tst_Write("title = 'bin'", ioCrt_(fld, Criteria_.eq_("bin")));
|
||||
tst_Write("title NOT IN ('bin', 'obj')", ioCrt_(fld, Criteria_.inn_("bin", "obj")));
|
||||
}
|
||||
@Test public void Url() {
|
||||
fld = IoItm_base_.Prop_Path;
|
||||
tst_Write("url = 'C:\\fil.txt'", ioCrt_(fld, Criteria_.eq_("C:\\fil.txt")));
|
||||
tst_Write("url IOMATCH '*.txt'", ioCrt_(fld, Criteria_ioMatch.parse(true, "*.txt", false)));
|
||||
}
|
||||
@Test public void Binary() {
|
||||
// parentheses around lhs and rhs
|
||||
tst_Write(
|
||||
"(type = 1 OR type = 2)"
|
||||
, Criteria_.Or
|
||||
( ioCrt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmDir.Type_Dir)), ioCrt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmFil.Type_Fil))
|
||||
));
|
||||
}
|
||||
Criteria ioCrt_(String fld, Criteria crt) {return Criteria_fld.new_(fld, crt);}
|
||||
String fld;
|
||||
private final Sql_wtr_ctx ctx = new Sql_wtr_ctx(false);
|
||||
void tst_Write(String expd, Criteria crt) {
|
||||
Sql_where_wtr where_wtr = ((Sql_core_wtr)Sql_qry_wtr_.New__basic()).Where_wtr();
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
where_wtr.Bld_where_elem(bfr, ctx, crt);
|
||||
Tfds.Eq(expd, bfr.To_str_and_clear());
|
||||
}
|
||||
}
|
||||
105
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_schema_wtr.java
Normal file
105
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_schema_wtr.java
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
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.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.dbs.metas.*;
|
||||
public class Sql_schema_wtr {
|
||||
private Bry_bfr tmp_bfr = Bry_bfr_.Reset(255);
|
||||
public Sql_schema_wtr Bfr_(Bry_bfr bfr) {this.tmp_bfr = bfr; return this;}
|
||||
public String Bld_create_idx(Dbmeta_idx_itm idx) {
|
||||
tmp_bfr.Add_str_a7("CREATE ");
|
||||
if (idx.Unique())
|
||||
tmp_bfr.Add_str_a7("UNIQUE ");
|
||||
tmp_bfr.Add_str_a7("INDEX ");
|
||||
tmp_bfr.Add_str_a7("IF NOT EXISTS ");
|
||||
tmp_bfr.Add_str_a7(idx.Name());
|
||||
tmp_bfr.Add_str_a7(" ON ");
|
||||
tmp_bfr.Add_str_a7(idx.Tbl());
|
||||
tmp_bfr.Add_str_a7(" (");
|
||||
Dbmeta_idx_fld[] flds = idx.Flds;
|
||||
int flds_len = flds.length;
|
||||
for (int i = 0; i < flds_len; ++i) {
|
||||
Dbmeta_idx_fld fld = flds[i];
|
||||
if (fld == null) continue; // fld will be null when tbl has Dbmetafld.Key_null (to support obsoleted schemas)
|
||||
if (i != 0) tmp_bfr.Add_str_a7(", ");
|
||||
tmp_bfr.Add_str_a7(fld.Name);
|
||||
}
|
||||
tmp_bfr.Add_str_a7(");");
|
||||
return tmp_bfr.To_str_and_clear();
|
||||
}
|
||||
public String Bld_create_tbl(Dbmeta_tbl_itm tbl) {
|
||||
tmp_bfr.Add_str_a7("CREATE TABLE IF NOT EXISTS ").Add_str_a7(tbl.Name()).Add_byte_nl();
|
||||
Dbmeta_fld_mgr flds = tbl.Flds();
|
||||
int len = flds.Len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Dbmeta_fld_itm fld = flds.Get_at(i);
|
||||
tmp_bfr.Add_byte(i == 0 ? Byte_ascii.Paren_bgn : Byte_ascii.Comma).Add_byte_space();
|
||||
Bld_fld(tmp_bfr, fld);
|
||||
tmp_bfr.Add_byte_nl();
|
||||
}
|
||||
tmp_bfr.Add_str_a7(");");
|
||||
return tmp_bfr.To_str_and_clear();
|
||||
}
|
||||
public String Bld_alter_tbl_add(String tbl, Dbmeta_fld_itm fld) {
|
||||
tmp_bfr.Add_str_a7("ALTER TABLE ").Add_str_a7(tbl).Add_str_a7(" ADD ");
|
||||
Bld_fld(tmp_bfr, fld);
|
||||
tmp_bfr.Add_byte_semic();
|
||||
return tmp_bfr.To_str_and_clear();
|
||||
}
|
||||
public String Bld_drop_tbl(String tbl) {
|
||||
return String_.Format("DROP TABLE IF EXISTS {0};", tbl);
|
||||
}
|
||||
private void Bld_fld(Bry_bfr tmp_bfr, Dbmeta_fld_itm fld) {
|
||||
tmp_bfr.Add_str_a7(fld.Name()).Add_byte_space();
|
||||
Tid_to_sql(tmp_bfr, fld.Type().Tid_ansi(), fld.Type().Len_1()); tmp_bfr.Add_byte_space();
|
||||
switch (fld.Nullable_tid()) {
|
||||
case Dbmeta_fld_itm.Nullable_unknown:
|
||||
case Dbmeta_fld_itm.Nullable_not_null: tmp_bfr.Add_str_a7("NOT NULL "); break;
|
||||
case Dbmeta_fld_itm.Nullable_null: tmp_bfr.Add_str_a7("NULL "); break;
|
||||
}
|
||||
if (fld.Default() != Dbmeta_fld_itm.Default_value_null) {
|
||||
tmp_bfr.Add_str_a7("DEFAULT ");
|
||||
boolean quote = Bool_.N;
|
||||
switch (fld.Type().Tid_ansi()) {
|
||||
case Dbmeta_fld_tid.Tid__str: case Dbmeta_fld_tid.Tid__text: quote = Bool_.Y; break;
|
||||
}
|
||||
if (quote) tmp_bfr.Add_byte_apos();
|
||||
tmp_bfr.Add_str_u8(Object_.Xto_str_strict_or_null(fld.Default()));
|
||||
if (quote) tmp_bfr.Add_byte_apos();
|
||||
tmp_bfr.Add_byte_space();
|
||||
}
|
||||
if (fld.Primary()) tmp_bfr.Add_str_a7("PRIMARY KEY ");
|
||||
if (fld.Autonum()) tmp_bfr.Add_str_a7("AUTOINCREMENT ");
|
||||
tmp_bfr.Del_by_1(); // remove trailing space
|
||||
}
|
||||
public static void Tid_to_sql(Bry_bfr tmp_bfr, int tid, int len) {// REF: https://www.sqlite.org/datatype3.html
|
||||
switch (tid) {
|
||||
case Dbmeta_fld_tid.Tid__bool: tmp_bfr.Add_str_a7("boolean"); break;
|
||||
case Dbmeta_fld_tid.Tid__byte: tmp_bfr.Add_str_a7("tinyint"); break;
|
||||
case Dbmeta_fld_tid.Tid__short: tmp_bfr.Add_str_a7("smallint"); break;
|
||||
case Dbmeta_fld_tid.Tid__int: tmp_bfr.Add_str_a7("integer"); break; // NOTE: must be integer, not int, else "int PRIMARY KEY AUTONUMBER" will fail; DATE:2015-02-12
|
||||
case Dbmeta_fld_tid.Tid__long: tmp_bfr.Add_str_a7("bigint"); break;
|
||||
case Dbmeta_fld_tid.Tid__float: tmp_bfr.Add_str_a7("float"); break;
|
||||
case Dbmeta_fld_tid.Tid__double: tmp_bfr.Add_str_a7("double"); break;
|
||||
case Dbmeta_fld_tid.Tid__str: tmp_bfr.Add_str_a7("varchar(").Add_int_variable(len).Add_byte(Byte_ascii.Paren_end); break;
|
||||
case Dbmeta_fld_tid.Tid__text: tmp_bfr.Add_str_a7("text"); break;
|
||||
case Dbmeta_fld_tid.Tid__bry: tmp_bfr.Add_str_a7("blob"); break;
|
||||
default: throw Err_.new_unhandled(tid);
|
||||
}
|
||||
}
|
||||
// public static final Sql_schema_wtr Instance = new Sql_schema_wtr();
|
||||
}
|
||||
68
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_schema_wtr_tst.java
Normal file
68
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_schema_wtr_tst.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import org.junit.*;
|
||||
public class Sql_schema_wtr_tst {
|
||||
@Before public void setup() {} private final Sql_schema_wtr_fxt fxt = new Sql_schema_wtr_fxt();
|
||||
@Test public void Idx_unique() {
|
||||
fxt.Test_create_idx(Dbmeta_idx_itm.new_unique_by_tbl("tbl_name", "idx_name", "fld_1", "fld_2")
|
||||
, "CREATE UNIQUE INDEX IF NOT EXISTS tbl_name__idx_name ON tbl_name (fld_1, fld_2);"
|
||||
);
|
||||
}
|
||||
@Test public void Tbl_basic() {
|
||||
Dbmeta_fld_list flds = new Dbmeta_fld_list();
|
||||
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(Dbmeta_tbl_itm.New("tbl_name", flds.To_fld_ary())
|
||||
, String_.Concat_lines_nl_skip_last
|
||||
( "CREATE TABLE IF NOT EXISTS tbl_name"
|
||||
, "( fld_int_pkey integer NOT NULL PRIMARY KEY"
|
||||
, ", fld_bool boolean NOT NULL"
|
||||
, ", fld_short smallint NOT NULL"
|
||||
, ", fld_int integer 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"
|
||||
, ");"
|
||||
));
|
||||
}
|
||||
@Test public void Tbl_alter_tbl_add() {
|
||||
Dbmeta_fld_list flds = new Dbmeta_fld_list();
|
||||
flds.Add_int_dflt("fld_int", -1);
|
||||
flds.Add_str_dflt("fld_str", 255, "a");
|
||||
fxt.Test_alter_tbl_add("tbl_name", flds.Get_by("fld_int"), "ALTER TABLE tbl_name ADD fld_int integer NOT NULL DEFAULT -1;");
|
||||
fxt.Test_alter_tbl_add("tbl_name", flds.Get_by("fld_str"), "ALTER TABLE tbl_name ADD fld_str varchar(255) NOT NULL DEFAULT 'a';");
|
||||
}
|
||||
}
|
||||
class Sql_schema_wtr_fxt {
|
||||
private Sql_schema_wtr sqlbldr = Sql_qry_wtr_.New__sqlite().Schema_wtr();
|
||||
public void Test_create_idx(Dbmeta_idx_itm idx, String expd) {Tfds.Eq(expd, sqlbldr.Bld_create_idx(idx));}
|
||||
public void Test_create_tbl(Dbmeta_tbl_itm tbl, String expd) {Tfds.Eq_str_lines(expd, sqlbldr.Bld_create_tbl(tbl));}
|
||||
public void Test_alter_tbl_add(String tbl, Dbmeta_fld_itm fld, String expd) {Tfds.Eq_str_lines(expd, sqlbldr.Bld_alter_tbl_add(tbl, fld));}
|
||||
}
|
||||
80
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_select_wtr.java
Normal file
80
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_select_wtr.java
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
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.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.dbs.qrys.*; import gplx.dbs.sqls.itms.*;
|
||||
public class Sql_select_wtr {
|
||||
private final Sql_core_wtr qry_wtr;
|
||||
public Sql_select_wtr(Sql_core_wtr qry_wtr) {this.qry_wtr = qry_wtr;}
|
||||
public void Bld_qry_select(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry) {
|
||||
bfr.Add_str_a7("SELECT ");
|
||||
if (qry.Cols().Distinct) bfr.Add_str_a7("DISTINCT ");
|
||||
Sql_select_fld_list flds = qry.Cols().Flds;
|
||||
int flds_len = flds.Len();
|
||||
if (flds_len == 0) bfr.Add_str_a7("*");
|
||||
for (int i = 0; i < flds_len; i++) {
|
||||
Sql_select_fld fld = (Sql_select_fld)flds.Get_at(i);
|
||||
if (i > 0) bfr.Add_str_a7(", ");
|
||||
qry_wtr.Bld_col_name(bfr, fld.To_fld_sql());
|
||||
}
|
||||
qry_wtr.From_wtr().Bld_clause_from(bfr, qry.From());
|
||||
Bld_indexed_by(bfr, ctx, qry, qry.Indexed_by());
|
||||
qry_wtr.Where_wtr().Bld_where(bfr, ctx, qry.Where_itm());
|
||||
Bld_select_group_by(bfr, ctx, qry, qry.GroupBy());
|
||||
Bld_select_order_by(bfr, ctx, qry, qry.Order());
|
||||
Bld_select_limit(bfr, ctx, qry, qry.Limit());
|
||||
if (qry.Offset() != Db_qry__select_cmd.Offset__disabled)
|
||||
Bld_offset(bfr, ctx, qry, qry.Offset());
|
||||
}
|
||||
private void Bld_select_group_by(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, Sql_group_clause groupBy) {
|
||||
if (groupBy == null) return;
|
||||
bfr.Add_str_a7(" GROUP BY ");
|
||||
for (int i = 0; i < groupBy.Flds().Count(); i++) {
|
||||
String item = (String)groupBy.Flds().Get_at(i);
|
||||
if (i > 0) bfr.Add_str_a7(", ");
|
||||
bfr.Add_str_a7(item);
|
||||
}
|
||||
}
|
||||
private void Bld_select_order_by(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, Sql_order_clause orderBy) {
|
||||
if (orderBy == null) return;
|
||||
bfr.Add_str_a7(" ORDER BY ");
|
||||
int len = orderBy.Flds().length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Sql_order_fld item = orderBy.Flds()[i];
|
||||
if (i > 0) bfr.Add_str_a7(", ");
|
||||
bfr.Add_str_a7(item.To_sql());
|
||||
}
|
||||
}
|
||||
protected void Bld_select_limit(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, int limit) {
|
||||
if (limit == Db_qry__select_cmd.Limit__disabled) return;
|
||||
bfr.Add_str_a7(" LIMIT ").Add_int_variable(limit);
|
||||
}
|
||||
@gplx.Virtual protected void Bld_offset(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, int offset) {
|
||||
bfr.Add_str_a7(" OFFSET ").Add_int_variable(offset);
|
||||
}
|
||||
private void Bld_indexed_by(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, String idx_name) {
|
||||
if (idx_name == null) return;
|
||||
// ( "SELECT {0}, {1}, {2}, {3} FROM {4} INDEXED BY {4}__title WHERE {1} = {5} AND {2} BETWEEN '{6}' AND '{7}' ORDER BY {3} DESC LIMIT {8};"
|
||||
bfr.Add_str_a7(" INDEXED BY ").Add_str_a7(idx_name);
|
||||
}
|
||||
}
|
||||
class Sql_select_wtr_sqlite extends Sql_select_wtr { public Sql_select_wtr_sqlite(Sql_core_wtr qry_wtr) {super(qry_wtr);}
|
||||
@Override protected void Bld_offset(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_qry__select_cmd qry, int offset) {
|
||||
if (qry.Limit() == Db_qry__select_cmd.Limit__disabled) Bld_select_limit(bfr, ctx, qry, -1); // SQLite requires a LIMIT if OFFSET is specified
|
||||
super.Bld_offset(bfr, ctx, qry, offset);
|
||||
}
|
||||
}
|
||||
28
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_select_wtr_tst.java
Normal file
28
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_select_wtr_tst.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import org.junit.*;
|
||||
public class Sql_select_wtr_tst {
|
||||
private final Sql_core_wtr_fxt fxt = new Sql_core_wtr_fxt();
|
||||
@Test public void Offset__automatically_add_limit() {
|
||||
fxt.Test__qry(Db_qry_.select_tbl_("tbl").Offset_(1), "SELECT * FROM tbl LIMIT -1 OFFSET 1");
|
||||
}
|
||||
@Test public void Offset__do_not_overwrite_limit() {
|
||||
fxt.Test__qry(Db_qry_.select_tbl_("tbl").Limit_(20).Offset_(1), "SELECT * FROM tbl LIMIT 20 OFFSET 1");
|
||||
}
|
||||
}
|
||||
91
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_val_wtr.java
Normal file
91
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_val_wtr.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.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_val_wtr {
|
||||
// private final Bry_bfr tmp_bfr = Bry_bfr_.New(32);
|
||||
public byte Seq__quote = Byte_ascii.Apos, Seq__escape = Byte_ascii.Backslash;
|
||||
public void Bld_val(Bry_bfr bfr, Sql_wtr_ctx ctx, Object val) {
|
||||
if (ctx.Mode_is_prep) {
|
||||
bfr.Add_byte(Byte_ascii.Question);
|
||||
return;
|
||||
}
|
||||
if (val == null) {
|
||||
bfr.Add_str_a7("NULL");
|
||||
return;
|
||||
}
|
||||
int tid_type = Type_adp_.To_tid_type(val.getClass());
|
||||
switch (tid_type) {
|
||||
case Type_adp_.Tid__bool: Bld_val__bool (bfr, Bool_.Cast(val)); break;
|
||||
case Type_adp_.Tid__byte: Bld_val__byte (bfr, Byte_.cast(val)); break;
|
||||
case Type_adp_.Tid__short: Bld_val__short (bfr, Short_.cast(val)); break;
|
||||
case Type_adp_.Tid__int: Bld_val__int (bfr, Int_.cast(val)); break;
|
||||
case Type_adp_.Tid__long: Bld_val__long (bfr, Long_.cast(val)); break;
|
||||
case Type_adp_.Tid__float: Bld_val__float (bfr, Float_.cast(val)); break;
|
||||
case Type_adp_.Tid__double: Bld_val__double (bfr, Double_.cast(val)); break;
|
||||
case Type_adp_.Tid__decimal: Bld_val__decimal (bfr, Decimal_adp_.cast(val)); break;
|
||||
case Type_adp_.Tid__str: Bld_val__str (bfr, String_.cast(val)); break;
|
||||
case Type_adp_.Tid__date: Bld_val__date (bfr, DateAdp_.cast(val)); break;
|
||||
case Type_adp_.Tid__obj: Bld_val__str (bfr, Object_.Xto_str_strict_or_null(val)); break;
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public void Bld_val__bool (Bry_bfr bfr, boolean val) {bfr.Add_int_digits(1, val ? 1 : 0);} // NOTE: save boolean to 0 or 1 b/c sqlite doesn't support true / false //{bfr.Add_str_a7(val ? "true" : "false");}
|
||||
@gplx.Virtual public void Bld_val__byte (Bry_bfr bfr, byte val) {bfr.Add_byte_variable(val);}
|
||||
@gplx.Virtual public void Bld_val__short (Bry_bfr bfr, short val) {bfr.Add_short_variable(val);}
|
||||
@gplx.Virtual public void Bld_val__int (Bry_bfr bfr, int val) {bfr.Add_int_variable(val);}
|
||||
@gplx.Virtual public void Bld_val__long (Bry_bfr bfr, long val) {bfr.Add_long_variable(val);}
|
||||
@gplx.Virtual public void Bld_val__float (Bry_bfr bfr, float val) {bfr.Add_float(val);}
|
||||
@gplx.Virtual public void Bld_val__double (Bry_bfr bfr, double val) {bfr.Add_double(val);}
|
||||
@gplx.Virtual public void Bld_val__date (Bry_bfr bfr, DateAdp val) {bfr.Add_str_u8_many("'", val.XtoStr_gplx_long(), "'");}
|
||||
@gplx.Virtual public void Bld_val__decimal (Bry_bfr bfr, Decimal_adp val) {bfr.Add_str_u8_many("'", val.To_str(), "'");}
|
||||
@gplx.Virtual public void Bld_val__str (Bry_bfr bfr, String val) {
|
||||
// byte[] bry = Bry_.new_u8(val); int len = bry.length; int pos = 0; int prv = -1; boolean dirty = false;
|
||||
// while (true) {
|
||||
// if (pos == len) break;
|
||||
// byte b = bry[pos];
|
||||
// if (b == Seq__quote) {
|
||||
// if (!dirty) {dirty = true; if (prv != -1) {tmp_bfr.Add_mid(bry, prv, pos); prv = -1;}}
|
||||
// tmp_bfr.Add_byte(Seq__quote).Add_byte(Seq__quote); // double-up
|
||||
// }
|
||||
//// else if (b == Seq__escape) {
|
||||
//// if (!dirty) {dirty = true; if (prv != -1) {tmp_bfr.Add_mid(bry, prv, pos); prv = -1;}}
|
||||
//// tmp_bfr.Add_byte(Seq__escape).Add_byte(Seq__escape); // double-up
|
||||
//// }
|
||||
// else
|
||||
// if (prv == -1) prv = pos;
|
||||
// ++pos;
|
||||
// }
|
||||
// if (dirty && prv != -1) tmp_bfr.Add_mid(bry, prv, len);
|
||||
|
||||
bfr.Add_byte(Seq__quote);
|
||||
bfr.Add_str_u8(String_.Replace(val, "'", "''"));
|
||||
// if (dirty)
|
||||
// bfr.Add_bfr_and_clear(tmp_bfr);
|
||||
// else
|
||||
// bfr.Add(bry);
|
||||
bfr.Add_byte(Seq__quote);
|
||||
}
|
||||
}
|
||||
class Sql_val_wtr_sqlite extends Sql_val_wtr { @Override public void Bld_val__bool(Bry_bfr bfr, boolean val) {
|
||||
bfr.Add_int_digits(1, val ? 1 : 0); // NOTE: save boolean to 0 or 1 b/c sqlite doesn't support true / false
|
||||
}
|
||||
}
|
||||
class Sql_val_wtr_mysql extends Sql_val_wtr { @Override public void Bld_val__str(Bry_bfr bfr, String val) {
|
||||
if (String_.Has(val, "\\")) val = String_.Replace(val, "\\", "\\\\");
|
||||
super.Bld_val__str(bfr, val);
|
||||
}
|
||||
}
|
||||
39
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_val_wtr_tst.java
Normal file
39
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_val_wtr_tst.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
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.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import org.junit.*;
|
||||
public class Sql_val_wtr_tst {
|
||||
private final Sql_core_wtr_fxt fxt = new Sql_core_wtr_fxt();
|
||||
@Test public void Null() {fxt.Test__val(null , "NULL");}
|
||||
@Test public void Bool__n() {fxt.Test__val(Bool_.N , "0");}
|
||||
@Test public void Bool__y() {fxt.Test__val(Bool_.Y , "1");}
|
||||
@Test public void Byte() {fxt.Test__val(Byte_.By_int(2) , "2");}
|
||||
@Test public void Short() {fxt.Test__val(Short_.By_int(3) , "3");}
|
||||
@Test public void Int() {fxt.Test__val(4 , "4");}
|
||||
@Test public void Long() {fxt.Test__val(5 , "5");}
|
||||
@Test public void Float() {fxt.Test__val(6.1f , "6.1");}
|
||||
@Test public void Double() {fxt.Test__val(7.1d , "7.1");}
|
||||
@Test public void Decimal() {fxt.Test__val(Decimal_adp_.float_(8) , "'8'");}
|
||||
@Test public void Str() {fxt.Test__val("abc" , "'abc'");}
|
||||
@Test public void Str__apos_mid() {fxt.Test__val("a'b" , "'a''b'");}
|
||||
@Test public void Str__apos_bgn() {fxt.Test__val("'ab" , "'''ab'");}
|
||||
@Test public void Str__apos_end() {fxt.Test__val("ab'" , "'ab'''");}
|
||||
@Test public void Str__apos_many() {fxt.Test__val("a'b'c" , "'a''b''c'");}
|
||||
@Test public void Str__back() {fxt.Test__val("a\\b" , "'a\\b'");}
|
||||
@Test public void Date() {fxt.Test__val(DateAdp_.parse_gplx("2016-02-03") , "'2016-02-03 00:00:00.000'");}
|
||||
}
|
||||
145
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_where_wtr.java
Normal file
145
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_where_wtr.java
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
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.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import gplx.core.criterias.*; import gplx.dbs.sqls.itms.*;
|
||||
public class Sql_where_wtr {
|
||||
private final Sql_core_wtr qry_wtr;
|
||||
private final Sql_val_wtr val_wtr;
|
||||
public Sql_where_wtr(Sql_core_wtr qry_wtr, Sql_val_wtr val_wtr) {this.qry_wtr = qry_wtr; this.val_wtr = val_wtr;}
|
||||
public void Bld_where(Bry_bfr bfr, Sql_wtr_ctx ctx, Sql_where_clause where_itm) {
|
||||
if (where_itm == Sql_where_clause.Where__null) return;
|
||||
Bld_where(bfr, ctx, where_itm.Root);
|
||||
}
|
||||
public void Bld_where(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria crt) {
|
||||
if (crt == null) return;
|
||||
if (crt.Tid() == Criteria_.Tid_wrapper) {
|
||||
Criteria_fld crt_fld = (Criteria_fld)crt;
|
||||
Criteria crt_inner = crt_fld.Crt();
|
||||
switch (crt_inner.Tid()) {
|
||||
case Criteria_.Tid_const:
|
||||
case Criteria_.Tid_not:
|
||||
case Criteria_.Tid_and:
|
||||
case Criteria_.Tid_or: crt = crt_inner; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
if (crt.Tid() == Criteria_.Tid_const) return;
|
||||
bfr.Add_str_a7(" WHERE ");
|
||||
Bld_where_elem(bfr, ctx, crt);
|
||||
}
|
||||
public void Bld_where_elem(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria crt) {
|
||||
if (crt == null) return; // handle empty crt; EX: SELECT * FROM tbl;
|
||||
Criteria_bool_base crt_bool = Criteria_bool_base.as_(crt);
|
||||
if (crt_bool != null) {
|
||||
bfr.Add_str_a7("(");
|
||||
Bld_where_elem(bfr, ctx, crt_bool.Lhs());
|
||||
bfr.Add_str_u8_many(" ", crt_bool.Op_literal(), " ");
|
||||
Bld_where_elem(bfr, ctx, crt_bool.Rhs());
|
||||
bfr.Add_str_a7(")");
|
||||
return;
|
||||
}
|
||||
if (crt.Tid() == Criteria_.Tid_db_obj_ary) {
|
||||
Bld_where__db_obj(bfr, ctx, (Db_obj_ary_crt)crt);
|
||||
}
|
||||
else {
|
||||
Criteria_fld leaf = Criteria_fld.as_(crt); if (leaf == null) throw Err_.new_invalid_op(crt.To_str());
|
||||
String leaf_pre = leaf.Pre(); if (leaf_pre != Criteria_fld.Pre_null) bfr.Add_str_u8(leaf_pre).Add_byte_dot();
|
||||
qry_wtr.Bld_col_name(bfr, leaf.Key());
|
||||
Criteria leaf_crt = leaf.Crt();
|
||||
switch (leaf_crt.Tid()) {
|
||||
case Criteria_.Tid_eq: Bld_where__eq (bfr, ctx, (Criteria_eq)leaf_crt); break;
|
||||
case Criteria_.Tid_comp: Bld_where__comp (bfr, ctx, (Criteria_comp)leaf_crt); break;
|
||||
case Criteria_.Tid_between: Bld_where__between (bfr, ctx, (Criteria_between)leaf_crt); break;
|
||||
case Criteria_.Tid_in: Bld_where__in (bfr, ctx, (Criteria_in)leaf_crt); break;
|
||||
case Criteria_.Tid_like: Bld_where__like (bfr, ctx, (Criteria_like)leaf_crt); break;
|
||||
case Criteria_.Tid_iomatch: Bld_where__iomatch (bfr, ctx, (Criteria_ioMatch)leaf_crt); break;
|
||||
default: throw Err_.new_unhandled(leaf_crt);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void Bld_where__eq(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria_eq crt) {
|
||||
bfr.Add_str_a7(crt.Neg() ? " != " : " = ");
|
||||
val_wtr.Bld_val(bfr, ctx, crt.Val());
|
||||
}
|
||||
private void Bld_where__comp(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria_comp crt) {
|
||||
int comp_tid = crt.Comp_mode();
|
||||
bfr.Add_byte_space();
|
||||
bfr.Add_byte(comp_tid < CompareAble_.Same ? Byte_ascii.Angle_bgn : Byte_ascii.Angle_end);
|
||||
if (comp_tid % 2 == CompareAble_.Same) bfr.Add_byte_eq();
|
||||
bfr.Add_byte_space();
|
||||
val_wtr.Bld_val(bfr, ctx, crt.Val());
|
||||
}
|
||||
private void Bld_where__between(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria_between crt) {
|
||||
bfr.Add_str_a7(crt.Neg() ? " NOT BETWEEN " : " BETWEEN ");
|
||||
val_wtr.Bld_val(bfr, ctx, crt.Lo());
|
||||
bfr.Add_str_a7(" AND ");
|
||||
val_wtr.Bld_val(bfr, ctx, crt.Hi());
|
||||
}
|
||||
private void Bld_where__like(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria_like crt) {
|
||||
bfr.Add_str_a7(crt.Neg() ? " NOT LIKE " : " LIKE ");
|
||||
val_wtr.Bld_val(bfr, ctx, crt.Pattern().Raw());
|
||||
bfr.Add_str_u8(String_.Format(" ESCAPE '{0}'", crt.Pattern().Escape()));
|
||||
}
|
||||
private void Bld_where__in(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria_in crt) {
|
||||
bfr.Add_str_a7(crt.Neg() ? " NOT IN " : " IN ");
|
||||
Object[] ary = crt.Ary();
|
||||
int len = crt.Ary_len();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (i == 0)
|
||||
bfr.Add_byte(Byte_ascii.Paren_bgn);
|
||||
else
|
||||
bfr.Add_byte(Byte_ascii.Comma).Add_byte_space();
|
||||
val_wtr.Bld_val(bfr, ctx, ary[i]);
|
||||
}
|
||||
bfr.Add_byte(Byte_ascii.Paren_end);
|
||||
}
|
||||
private void Bld_where__iomatch(Bry_bfr bfr, Sql_wtr_ctx ctx, Criteria_ioMatch crt) {
|
||||
bfr.Add_str_a7(crt.Neg() ? " NOT IOMATCH " : " IOMATCH ");
|
||||
val_wtr.Bld_val(bfr, ctx, crt.Pattern().Raw());
|
||||
}
|
||||
public void Bld_where__db_obj(Bry_bfr bfr, Sql_wtr_ctx ctx, Db_obj_ary_crt crt) {
|
||||
Object[][] ary = crt.Vals();
|
||||
int ary_len = ary.length;
|
||||
Db_obj_ary_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) bfr.Add_str_a7(" OR ");
|
||||
bfr.Add_str_a7("(");
|
||||
for (int j = 0; j < itm_len; j++) {
|
||||
if (j != 0) bfr.Add_str_a7(" AND ");
|
||||
Db_obj_ary_fld fld = flds[j];
|
||||
Object val = itm[j];
|
||||
boolean quote = false;
|
||||
switch (fld.Type_tid()) {
|
||||
case Type_adp_.Tid__str:
|
||||
case Type_adp_.Tid__char:
|
||||
case Type_adp_.Tid__date:
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
bfr.Add_str_a7(fld.Name());
|
||||
bfr.Add_str_a7("=");
|
||||
if (quote) bfr.Add_str_a7("'");
|
||||
bfr.Add_str_a7(Object_.Xto_str_strict_or_empty(val));
|
||||
if (quote) bfr.Add_str_a7("'");
|
||||
}
|
||||
bfr.Add_str_a7(")");
|
||||
}
|
||||
}
|
||||
}
|
||||
55
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_where_wtr_tst.java
Normal file
55
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_where_wtr_tst.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.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
import org.junit.*; import gplx.core.criterias.*;
|
||||
public class Sql_where_wtr_tst {
|
||||
private final Sql_core_wtr_fxt fxt = new Sql_core_wtr_fxt();
|
||||
@Test public void Eq() {fxt.Test__where(Db_crt_.New_eq ("fld", 1) , "fld = 1");}
|
||||
@Test public void Eq_not() {fxt.Test__where(Db_crt_.New_eq_not ("fld", 1) , "fld != 1");}
|
||||
@Test public void Eq_pre() {fxt.Test__where(Db_crt_.New_eq ("a", "fld", 1) , "a.fld = 1");}
|
||||
@Test public void Lt() {fxt.Test__where(Db_crt_.New_lt ("fld", 1) , "fld < 1");}
|
||||
@Test public void Lte() {fxt.Test__where(Db_crt_.New_lte ("fld", 1) , "fld <= 1");}
|
||||
@Test public void Mt() {fxt.Test__where(Db_crt_.New_mt ("fld", 1) , "fld > 1");}
|
||||
@Test public void Mte() {fxt.Test__where(Db_crt_.New_mte ("fld", 1) , "fld >= 1");}
|
||||
@Test public void Between() {fxt.Test__where(Db_crt_.New_between ("fld", 1, 3) , "fld BETWEEN 1 AND 3");}
|
||||
@Test public void In() {fxt.Test__where(Db_crt_.New_in ("fld", 1, 2, 3) , "fld IN (1, 2, 3)");}
|
||||
@Test public void Like() {fxt.Test__where(Db_crt_.New_like ("fld", "A%") , "fld LIKE 'A%' ESCAPE '|'");}
|
||||
@Test public void And__subs__2() {
|
||||
fxt.Test__where
|
||||
( Criteria_.And
|
||||
( Db_crt_.New_eq("id", 1)
|
||||
, Db_crt_.New_eq("name", "me")
|
||||
), "(id = 1 AND name = 'me')");
|
||||
}
|
||||
@Test public void Or__subs__2() {
|
||||
fxt.Test__where
|
||||
( Criteria_.Or
|
||||
( Db_crt_.New_eq("id", 1)
|
||||
, Db_crt_.New_eq("name", "me")
|
||||
), "(id = 1 OR name = 'me')");
|
||||
}
|
||||
@Test public void Nested() {
|
||||
fxt.Test__where
|
||||
( Criteria_.Or
|
||||
( Db_crt_.New_eq("id", 1)
|
||||
, Criteria_.And
|
||||
( Db_crt_.New_eq("name", "me")
|
||||
, Db_crt_.New_eq("id", 2))
|
||||
), "(id = 1 OR (name = 'me' AND id = 2))");
|
||||
}
|
||||
}
|
||||
22
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_wtr_ctx.java
Normal file
22
140_dbs/src/gplx/dbs/sqls/wtrs/Sql_wtr_ctx.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.sqls.wtrs; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
|
||||
public class Sql_wtr_ctx {
|
||||
public Sql_wtr_ctx(boolean mode_is_prep) {this.Mode_is_prep = mode_is_prep;}
|
||||
public final boolean Mode_is_prep;
|
||||
}
|
||||
Reference in New Issue
Block a user