1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

'v3.3.4.1'

This commit is contained in:
gnosygnu
2016-03-27 23:44:59 -04:00
parent de67253a9c
commit baaef32df2
903 changed files with 13339 additions and 8695 deletions

View File

@@ -18,10 +18,16 @@ 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 final Sql_qry_wtr
public static final Sql_qry_wtr
Basic = new Sql_core_wtr()
, Mysql = new Sql_core_wtr__mysql()
, Sqlite = 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, "'", "''") + "'";
}
public static String Gen_placeholder_parameters(Db_qry qry) {return Sql_qry_wtr_.Sqlite.To_sql_str(qry, true);} // replace arguments with ?; EX: UPDATE a SET b = ? WHERE c = ?;
}

View File

@@ -16,8 +16,8 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
public class Sql_from_itm {
public Sql_from_itm(Sql_tbl_itm base_tbl) {
public class Sql_from_clause {
public Sql_from_clause(Sql_tbl_itm base_tbl) {
this.Base_tbl = base_tbl;
Tbls.Add(base_tbl);
}

View File

@@ -16,13 +16,13 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
public class Sql_group_itm {
public class Sql_group_clause {
public List_adp Flds() {return flds;} List_adp flds = List_adp_.new_();
public static Sql_group_itm new_(String... ary) {
Sql_group_itm rv = new Sql_group_itm();
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_itm() {}
} Sql_group_clause() {}
}

View File

@@ -16,8 +16,8 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
public class Sql_join_itm {
public Sql_join_itm(String trg_fld, String src_tbl, String src_fld) {
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;
@@ -26,5 +26,9 @@ public class Sql_join_itm {
public final String Src_fld;
public final String Trg_fld;
public static final Sql_join_itm[] Ary__empty = new Sql_join_itm[0];
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];
}

View 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;
}
}

View File

@@ -16,7 +16,7 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
public class Sql_order_itm {
public Sql_order_itm(Sql_order_fld[] flds) {this.Flds = flds;}
public final Sql_order_fld[] Flds;
public class Sql_select_clause {
public boolean Distinct = false;
public final Sql_select_fld_list Flds = new Sql_select_fld_list();
}

View File

@@ -21,37 +21,40 @@ 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;
public final String Fld;
public final String Alias;
public abstract String To_sql();
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;
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(Tbl_null, Fld_wildcard, Fld_wildcard);}
@Override public String To_sql() {return Fld_wildcard;}
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();
public static final String Fld_wildcard = "*";
// 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_sql() {
@Override public String To_fld_sql() {
String rv = Fld;
if (this.Tbl != Tbl_null)
if (this.Tbl != Tbl__null)
rv = this.Tbl + "." + Fld;
if (!String_.Eq(Alias, Fld))
rv = rv + " AS " + Alias;

View File

@@ -19,7 +19,7 @@ package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sq
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_sql() {
@Override public String To_fld_sql() {
return String_.Format("{0}({1}) AS {2}", XtoSql_functionName(), Fld, Alias);
}
}

View File

@@ -18,7 +18,8 @@ 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 Get_at(int i) {return (Sql_select_fld)hash.Get_at(i);}
public void Add(Sql_select_fld fld) {hash.Add(fld.Alias, fld);}
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);}
}

View File

@@ -1,25 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.sqls.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.sqls.*;
public class Sql_select_itm {
public boolean Distinct = false;
public final Sql_select_fld_list Flds = new Sql_select_fld_list();
public static final Sql_select_itm All = all_();
private static Sql_select_itm all_() {Sql_select_itm rv = new Sql_select_itm(); rv.Flds.Add(Sql_select_fld_wild.Instance); return rv;}
}

View File

@@ -17,7 +17,7 @@ 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_itm[] join_flds) {
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;
@@ -28,7 +28,8 @@ public class Sql_tbl_itm {
public final String Db;
public final String Name;
public final String Alias;
public final Sql_join_itm[] Join_flds;
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;

View File

@@ -17,9 +17,9 @@ 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_itm {
public class Sql_where_clause {
public Criteria Root = Criteria_.All;
public static final Object Where__null = null;
public static final Sql_where_itm All = new Sql_where_itm();
public static final Sql_where_clause All = new Sql_where_clause();
}

View File

@@ -68,13 +68,13 @@ public class Sql_core_wtr implements Sql_qry_wtr {
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);
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);
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 ? ")" : ", ");
@@ -85,7 +85,7 @@ public class Sql_core_wtr implements Sql_qry_wtr {
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);
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("=");

View File

@@ -18,20 +18,20 @@ 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_itm from) {
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)
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_itm[] flds = tbl.Join_flds; int flds_len = flds.length;
Sql_join_fld[] flds = tbl.Join_flds; int flds_len = flds.length;
for (int j = 0; j < flds_len; ++j) {
Sql_join_itm join_fld = flds[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);

View File

@@ -34,6 +34,7 @@ public class Sql_schema_wtr {
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);
}

View File

@@ -29,7 +29,7 @@ public class Sql_select_wtr {
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_sql());
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());
@@ -40,7 +40,7 @@ public class Sql_select_wtr {
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_itm groupBy) {
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++) {
@@ -49,12 +49,12 @@ public class Sql_select_wtr {
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_itm orderBy) {
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;
int len = orderBy.Flds().length;
for (int i = 0; i < len; ++i) {
Sql_order_fld item = orderBy.Flds[i];
Sql_order_fld item = orderBy.Flds()[i];
if (i > 0) bfr.Add_str_a7(", ");
bfr.Add_str_a7(item.To_sql());
}

View File

@@ -21,8 +21,8 @@ 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_itm where_itm) {
if (where_itm == Sql_where_itm.Where__null) return;
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) {