mirror of https://github.com/gnosygnu/xowa
parent
6d179ca59d
commit
de67253a9c
@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.diffs; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.diffs.itms.*;
|
||||
import gplx.dbs.diffs.builds.*;
|
||||
public class Gdif_core {
|
||||
private final Db_conn conn;
|
||||
private final Gdif_job_tbl job_tbl;
|
||||
private final Gdif_cmd_tbl cmd_tbl;
|
||||
private final Gdif_txn_tbl txn_tbl;
|
||||
public Gdif_core(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
this.db = new Gdif_db(conn);
|
||||
this.job_tbl = db.Job_tbl();
|
||||
this.cmd_tbl = db.Cmd_tbl();
|
||||
this.txn_tbl = db.Txn_tbl();
|
||||
}
|
||||
public Gdif_db Db() {return db;} private final Gdif_db db;
|
||||
public Gdif_job_itm New_job(String name, String made_by) {
|
||||
int job_id = conn.Sys_mgr().Autonum_next(job_tbl.Tbl_name(), job_tbl.Fld_job_id());
|
||||
return job_tbl.Insert(job_id, name, made_by, DateAdp_.Now().XtoUtc(), "");
|
||||
}
|
||||
public Gdif_cmd_itm New_cmd(Gdif_bldr_ctx ctx, int tid) {
|
||||
ctx.Cur_cmd_count++;
|
||||
return cmd_tbl.Insert(ctx.Cur_job.Id, ctx.Cur_cmd_count, tid, "");
|
||||
}
|
||||
public Gdif_txn_itm New_txn(Gdif_bldr_ctx ctx, int cmd_id, int owner_txn) {
|
||||
ctx.Cur_txn_count++;
|
||||
return txn_tbl.Insert(ctx.Cur_job.Id, ctx.Cur_txn_count, cmd_id, owner_txn);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.diffs; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.dbs.diffs.itms.*;
|
||||
public class Gdif_db {
|
||||
public Gdif_db(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
this.job_tbl = new Gdif_job_tbl(conn);
|
||||
this.cmd_tbl = new Gdif_cmd_tbl(conn);
|
||||
this.txn_tbl = new Gdif_txn_tbl(conn);
|
||||
}
|
||||
public Db_conn Conn() {return conn;} private final Db_conn conn;
|
||||
public Gdif_job_tbl Job_tbl() {return job_tbl;} private final Gdif_job_tbl job_tbl;
|
||||
public Gdif_cmd_tbl Cmd_tbl() {return cmd_tbl;} private final Gdif_cmd_tbl cmd_tbl;
|
||||
public Gdif_txn_tbl Txn_tbl() {return txn_tbl;} private final Gdif_txn_tbl txn_tbl;
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
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.diffs.builds; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
|
||||
import gplx.dbs.diffs.itms.*;
|
||||
public class Gdif_bldr_ctx {
|
||||
public Gdif_bldr_ctx() {}
|
||||
public Gdif_core Core;
|
||||
public Gdif_job_itm Cur_job;
|
||||
public Gdif_cmd_itm Cur_cmd;
|
||||
public Gdif_txn_itm Cur_txn;
|
||||
public int Cur_cmd_count;
|
||||
public int Cur_txn_count;
|
||||
public Gdif_bldr_ctx Init(Gdif_core core, Gdif_job_itm cur_job) {
|
||||
this.Core = core; this.Cur_job = cur_job;
|
||||
return this;
|
||||
}
|
||||
public void Clear() {
|
||||
Cur_cmd_count = 0; Cur_txn_count = 0;
|
||||
Cur_job = null;
|
||||
Cur_cmd = null;
|
||||
Cur_txn = null;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
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.diffs.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
|
||||
public class Gdif_cmd_tbl implements Rls_able {
|
||||
private String tbl_name = "gdif_cmd";
|
||||
private String fld_grp_id, fld_cmd_id, fld_tid, fld_data;
|
||||
private final Dbmeta_fld_list flds = Dbmeta_fld_list.new_();
|
||||
private final Db_conn conn; private Db_stmt stmt_insert;
|
||||
public Gdif_cmd_tbl(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
fld_grp_id = flds.Add_int("grp_id"); fld_cmd_id = flds.Add_int("cmd_id"); fld_tid = flds.Add_int("tid"); fld_data = flds.Add_text("data");
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_tbl(tbl_name, "main", fld_grp_id, fld_cmd_id)));}
|
||||
public Gdif_cmd_itm Insert(int grp_id, int cmd_id, int tid, String data) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear()
|
||||
.Val_int(fld_grp_id , grp_id)
|
||||
.Val_int(fld_cmd_id , cmd_id)
|
||||
.Val_int(fld_tid , tid)
|
||||
.Val_str(fld_data , data)
|
||||
.Exec_insert();
|
||||
return new Gdif_cmd_itm(grp_id, cmd_id, tid);
|
||||
}
|
||||
public void Rls() {
|
||||
stmt_insert = Db_stmt_.Rls(stmt_insert);
|
||||
}
|
||||
}
|
@ -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.diffs.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
|
||||
public class Gdif_job_itm {
|
||||
public Gdif_job_itm(int id, String name, String made_by, DateAdp made_on) {
|
||||
this.Id = id; this.Name = name; this.Made_by = made_by; this.Made_on = made_on;
|
||||
}
|
||||
public final int Id;
|
||||
public final String Name;
|
||||
public final String Made_by;
|
||||
public final DateAdp Made_on;
|
||||
public String Data;
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.diffs.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
|
||||
public class Gdif_job_tbl implements Rls_able {
|
||||
private String tbl_name = "gdif_job";
|
||||
private String fld_job_id, fld_name, fld_made_by, fld_made_on, fld_data;
|
||||
private final Dbmeta_fld_list flds = Dbmeta_fld_list.new_();
|
||||
private final Db_conn conn; private Db_stmt stmt_insert;
|
||||
public Gdif_job_tbl(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
fld_job_id = flds.Add_int_pkey("job_id"); fld_name = flds.Add_str("name", 255); fld_made_by = flds.Add_str("made_by", 255); fld_made_on = flds.Add_date("made_on"); fld_data = flds.Add_text("data");
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;}
|
||||
public String Fld_job_id() {return fld_job_id;}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds));}
|
||||
public Gdif_job_itm Insert(int id, String name, String made_by, DateAdp made_on, String data) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear()
|
||||
.Val_int(fld_job_id , id)
|
||||
.Val_str(fld_name , name)
|
||||
.Val_str(fld_made_by , made_by)
|
||||
.Val_date(fld_made_on , made_on)
|
||||
.Val_str(fld_data , data)
|
||||
.Exec_insert();
|
||||
return new Gdif_job_itm(id, name, made_by, made_on);
|
||||
}
|
||||
public void Rls() {
|
||||
stmt_insert = Db_stmt_.Rls(stmt_insert);
|
||||
}
|
||||
}
|
@ -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.diffs.itms; import gplx.*; import gplx.dbs.*; import gplx.dbs.diffs.*;
|
||||
public class Gdif_txn_itm {
|
||||
public Gdif_txn_itm(int job_id, int txn_id, int cmd_id, int owner_txn) {
|
||||
this.Job_id = job_id; this.Txn_id = txn_id; this.Cmd_id = cmd_id; this.Owner_txn = owner_txn;
|
||||
}
|
||||
public final int Job_id;
|
||||
public final int Txn_id;
|
||||
public final int Cmd_id;
|
||||
public final int Owner_txn;
|
||||
|
||||
public static final int Owner_txn__null = 0;
|
||||
}
|
@ -1,35 +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; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*;
|
||||
public class Sql_join_itm {
|
||||
public String SrcTbl() {return srcTbl;} public Sql_join_itm SrcTbl_(String v) {srcTbl = v; return this;} private String srcTbl;
|
||||
public String SrcFld() {return srcFld;} public Sql_join_itm SrcFld_(String v) {srcFld = v; return this;} private String srcFld;
|
||||
public String TrgFld() {return trgFld;} public Sql_join_itm TrgFld_(String v) {trgFld = v; return this;} private String trgFld;
|
||||
public String TrgFldOrSrcFld() {return trgFld == null ? srcFld : trgFld;}
|
||||
public static Sql_join_itm new_(String trgFld, String srcTbl, String srcFld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = trgFld; rv.srcTbl = srcTbl; rv.srcFld = srcFld;
|
||||
return rv;
|
||||
} Sql_join_itm() {}
|
||||
public static Sql_join_itm same_(String tbl, String fld) {
|
||||
Sql_join_itm rv = new Sql_join_itm();
|
||||
rv.trgFld = fld; rv.srcTbl = tbl; rv.srcFld = fld;
|
||||
return rv;
|
||||
}
|
||||
}
|
@ -1,31 +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; import gplx.*; import gplx.dbs.*;
|
||||
public class Sql_join_itmType {
|
||||
public int Val() {return val;} int val;
|
||||
public String Name() {return name;} private String name;
|
||||
Sql_join_itmType(int v, String name) {this.val = v; this.name = name;}
|
||||
public static final Sql_join_itmType
|
||||
From = new Sql_join_itmType(0, "FROM")
|
||||
, Inner = new Sql_join_itmType(1, "INNER JOIN")
|
||||
, Left = new Sql_join_itmType(2, "LEFT JOIN")
|
||||
, Right = new Sql_join_itmType(3, "RIGHT JOIN")
|
||||
, Outer = new Sql_join_itmType(4, "OUTER JOIN")
|
||||
, Cross = new Sql_join_itmType(5, "CROSS JOIN")
|
||||
;
|
||||
}
|
@ -1,300 +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; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*; import gplx.core.criterias.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
public class Sql_qry_wtr_ansi implements Sql_qry_wtr {
|
||||
private final String_bldr sb = String_bldr_.new_();
|
||||
public boolean prepare = false;
|
||||
public String Xto_str(Db_qry cmd, boolean prepare) {
|
||||
synchronized (sb) {
|
||||
this.prepare = prepare;
|
||||
switch (cmd.Tid()) {
|
||||
case Db_qry_.Tid_insert: return Bld_qry_insert((Db_qry_insert)cmd);
|
||||
case Db_qry_.Tid_delete: return Bld_qry_delete((Db_qry_delete)cmd);
|
||||
case Db_qry_.Tid_update: return Bld_qry_update((Db_qry_update)cmd);
|
||||
case Db_qry_.Tid_select_in_tbl:
|
||||
case Db_qry_.Tid_select: return Bld_qry_select((Db_qry__select_cmd)cmd);
|
||||
case Db_qry_.Tid_sql: return ((Db_qry_sql)cmd).Xto_sql();
|
||||
default: throw Err_.new_unhandled(cmd.Tid());
|
||||
}
|
||||
}
|
||||
}
|
||||
private String Bld_qry_delete(Db_qry_delete cmd) {
|
||||
sb.Add_many("DELETE FROM ", cmd.Base_table());
|
||||
Bld_where(sb, cmd.Where());
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_insert(Db_qry_insert cmd) {
|
||||
if (cmd.Select() != null) {
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < cmd.Cols().Count(); i++) {
|
||||
Sql_select_fld_base fld = cmd.Cols().Get_at(i);
|
||||
sb.Add(fld.Alias());
|
||||
sb.Add(i == cmd.Cols().Count() - 1 ? ") " : ", ");
|
||||
}
|
||||
sb.Add(Bld_qry_select(cmd.Select()));
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
int arg_count = cmd.Args().Count(); if (arg_count == 0) throw Err_.new_wo_type("Db_qry_insert has no columns", "base_table", cmd.Base_table());
|
||||
int last = arg_count - 1;
|
||||
sb.Add_many("INSERT INTO ", cmd.Base_table(), " (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().Get_at(i);
|
||||
this.Xto_sql_col(sb, pair.Key_as_obj());
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
sb.Add(" VALUES (");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().Get_at(i);
|
||||
Db_arg arg = (Db_arg)pair.Val();
|
||||
this.Bld_val(sb, arg);
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_update(Db_qry_update cmd) {
|
||||
int arg_count = cmd.Args().Count(); if (arg_count == 0) throw Err_.new_wo_type("Db_qry_update has no columns", "base_table", cmd.Base_table());
|
||||
sb.Add_many("UPDATE ", cmd.Base_table(), " SET ");
|
||||
for (int i = 0; i < arg_count; i++) {
|
||||
KeyVal pair = cmd.Args().Get_at(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.Xto_sql_col(sb, pair.Key_as_obj());
|
||||
sb.Add("=");
|
||||
this.Bld_val(sb, (Db_arg)pair.Val());
|
||||
}
|
||||
Bld_where(sb, cmd.Where());
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
private String Bld_qry_select(Db_qry__select_cmd cmd) {
|
||||
sb.Add("SELECT ");
|
||||
if (cmd.Cols().Distinct()) sb.Add("DISTINCT ");
|
||||
Sql_select_fld_list flds = cmd.Cols().Flds();
|
||||
if (flds.Count() == 0) sb.Add("*");
|
||||
for (int i = 0; i < flds.Count(); i++) {
|
||||
Sql_select_fld_base fld = (Sql_select_fld_base)flds.Get_at(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.Xto_sql_col(sb, fld.XtoSql());
|
||||
}
|
||||
Bld_clause_from(sb, cmd.From());
|
||||
Bld_indexed_by(sb, cmd.Indexed_by());
|
||||
Bld_where(sb, cmd.Where());
|
||||
Bld_select_group_by(sb, cmd.GroupBy());
|
||||
Bld_select_order_by(sb, cmd.OrderBy());
|
||||
Bld_select_limit(sb, cmd.Limit());
|
||||
return sb.To_str_and_clear();
|
||||
}
|
||||
private void Bld_select_group_by(String_bldr sb, Sql_group_by groupBy) {
|
||||
if (groupBy == null) return;
|
||||
sb.Add(" GROUP BY ");
|
||||
for (int i = 0; i < groupBy.Flds().Count(); i++) {
|
||||
String item = (String)groupBy.Flds().Get_at(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item);
|
||||
}
|
||||
}
|
||||
private void Bld_select_order_by(String_bldr sb, Sql_order_by orderBy) {
|
||||
if (orderBy == null) return;
|
||||
sb.Add(" ORDER BY ");
|
||||
for (int i = 0; i < orderBy.Flds().Count(); i++) {
|
||||
Sql_order_by_itm item = (Sql_order_by_itm)orderBy.Flds().Get_at(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item.XtoSql());
|
||||
}
|
||||
}
|
||||
private void Bld_select_limit(String_bldr sb, int limit) {
|
||||
if (limit == Db_qry__select_cmd.Limit_disabled) return;
|
||||
sb.Add(" LIMIT ").Add(limit);
|
||||
}
|
||||
private void Bld_clause_from(String_bldr sb, Sql_from from) {
|
||||
for (Object tblObj : from.Tbls()) {
|
||||
Sql_tbl_src tbl = (Sql_tbl_src)tblObj;
|
||||
sb.Add_many
|
||||
( " ", String_.Upper(tbl.JoinType().Name()), " ", tbl.TblName(), String_.FormatOrEmptyStrIfNull(" {0}", tbl.Alias())
|
||||
);
|
||||
String tblAliasForJoin = tbl.Alias() == null ? tbl.TblName() : tbl.Alias();
|
||||
for (int i = 0; i < tbl.JoinLinks().Count(); i++) {
|
||||
Sql_join_itm joinLink = (Sql_join_itm)tbl.JoinLinks().Get_at(i);
|
||||
String conjunction = i == 0 ? " ON " : " AND ";
|
||||
sb.Add_many(conjunction, joinLink.SrcTbl(), ".", joinLink.SrcFld(), "=", tblAliasForJoin, ".", joinLink.TrgFldOrSrcFld());
|
||||
}
|
||||
}
|
||||
}
|
||||
private void Bld_indexed_by(String_bldr sb, String idx_name) {
|
||||
if (idx_name == null) return;
|
||||
sb.Add(" INDEXED BY ").Add(idx_name);
|
||||
}
|
||||
private void Xto_sql_col(String_bldr sb, Object obj) {
|
||||
if (obj == null) throw Err_.new_null();
|
||||
sb.Add_obj(obj); // FIXME: options for bracketing; ex: [name]
|
||||
}
|
||||
public void Bld_val(String_bldr sb, Db_arg arg) {
|
||||
if (prepare) {
|
||||
sb.Add("?");
|
||||
return;
|
||||
}
|
||||
Object val = arg.Val();
|
||||
if (val == null) {
|
||||
sb.Add("NULL");
|
||||
return;
|
||||
}
|
||||
Class<?> val_type = val.getClass();
|
||||
if (val_type == Bool_.Cls_ref_type)
|
||||
sb.Add_obj(Bool_.To_int(Bool_.cast(val))); // NOTE: save boolean to 0 or 1, b/c (a) db may not support bit datatype (sqllite) and (b) avoid i18n issues with "true"/"false"
|
||||
else if
|
||||
( val_type == Byte_.Cls_ref_type || val_type == Short_.Cls_ref_type
|
||||
|| val_type == Int_.Cls_ref_type || val_type == Long_.Cls_ref_type
|
||||
|| val_type == Float_.Cls_ref_type || val_type == Double_.Cls_ref_type
|
||||
)
|
||||
sb.Add(Object_.Xto_str_strict_or_null(val));
|
||||
else if (val_type == DateAdp.class)
|
||||
Bld_val_date(sb, arg, (DateAdp)val);
|
||||
else if (val_type == Decimal_adp.class) {
|
||||
Decimal_adp valDecimal = (Decimal_adp)val;
|
||||
sb.Add(valDecimal.To_str());
|
||||
}
|
||||
else {
|
||||
String valString = Object_.Xto_str_strict_or_null(val);
|
||||
Bld_val_str(sb, arg, valString);
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_str(String_bldr sb, Db_arg arg, String s) {
|
||||
sb.Add_many("'", String_.Replace(s, "'", "''"), "'"); // stupid escaping of '
|
||||
}
|
||||
@gplx.Virtual public void Bld_val_date(String_bldr sb, Db_arg arg, DateAdp s) {
|
||||
sb.Add_many("'", s.XtoStr_gplx_long(), "'");
|
||||
}
|
||||
public void Bld_where(String_bldr sb, 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;
|
||||
sb.Add(" WHERE ");
|
||||
this.Bld_where_val(sb, crt);
|
||||
}
|
||||
public void Bld_where_val(String_bldr sb, 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) {
|
||||
sb.Add("(");
|
||||
Bld_where_val(sb, crt_bool.Lhs());
|
||||
sb.Add_many(" ", crt_bool.Op_literal(), " ");
|
||||
Bld_where_val(sb, crt_bool.Rhs());
|
||||
sb.Add(")");
|
||||
return;
|
||||
}
|
||||
if (crt.Tid() == Criteria_.Tid_db_obj_ary) {
|
||||
Append_db_obj_ary(sb, (Db_obj_ary_crt)crt);
|
||||
}
|
||||
else {
|
||||
Criteria_fld leaf = Criteria_fld.as_(crt); if (leaf == null) throw Err_.new_invalid_op(crt.To_str());
|
||||
sb.Add(leaf.Key());
|
||||
Bld_where_crt(sb, leaf.Crt());
|
||||
}
|
||||
}
|
||||
private void Bld_where_crt(String_bldr sb, Criteria crt) {
|
||||
switch (crt.Tid()) {
|
||||
case Criteria_.Tid_eq: Bld_where_eq(sb, Criteria_eq.as_(crt)); break;
|
||||
case Criteria_.Tid_comp: Bld_where_comp(sb, Criteria_comp.as_(crt)); break;
|
||||
case Criteria_.Tid_between: Bld_where_between(sb, Criteria_between.as_(crt)); break;
|
||||
case Criteria_.Tid_in: Bld_where_in(sb, Criteria_in.as_(crt)); break;
|
||||
case Criteria_.Tid_like: Bld_where_like(sb, Criteria_like.as_(crt)); break;
|
||||
case Criteria_.Tid_iomatch: Bld_where_iomatch(sb, Criteria_ioMatch.as_(crt)); break;
|
||||
default: throw Err_.new_unhandled(crt);
|
||||
}
|
||||
}
|
||||
private void Bld_where_eq(String_bldr sb, Criteria_eq crt) {
|
||||
sb.Add(crt.Negated() ? "!=" : "=");
|
||||
this.Bld_val(sb, Wrap(crt.Val()));
|
||||
}
|
||||
private void Bld_where_comp(String_bldr sb, Criteria_comp crt) {
|
||||
sb.Add_many(crt.XtoSymbol());
|
||||
this.Bld_val(sb, Wrap(crt.Val()));
|
||||
}
|
||||
private void Bld_where_between(String_bldr sb, Criteria_between crt) {
|
||||
sb.Add(crt.Negated() ? " NOT BETWEEN " : " BETWEEN ");
|
||||
this.Bld_val(sb, Wrap(crt.Lhs()));
|
||||
sb.Add(" AND ");
|
||||
this.Bld_val(sb, Wrap(crt.Rhs()));
|
||||
}
|
||||
private void Bld_where_like(String_bldr sb, Criteria_like crt) {
|
||||
sb.Add(crt.Negated() ? " NOT LIKE " : " LIKE ");
|
||||
this.Bld_val(sb, Wrap(crt.Pattern().Raw()));
|
||||
sb.Add_fmt(" ESCAPE '{0}'", crt.Pattern().Escape());
|
||||
}
|
||||
private void Bld_where_in(String_bldr sb, Criteria_in crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IN (" : " IN (");
|
||||
Object[] crt_vals = crt.Val_as_obj_ary();
|
||||
int len = crt_vals.length;
|
||||
int last = len - 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Object val = crt_vals[i];
|
||||
this.Bld_val(sb, Wrap(val));
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
}
|
||||
private void Bld_where_iomatch(String_bldr sb, Criteria_ioMatch crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IOMATCH " : " IOMATCH ");
|
||||
this.Bld_val(sb, Wrap(crt.Pattern().Raw()));
|
||||
}
|
||||
public void Append_db_obj_ary(String_bldr sb, Db_obj_ary_crt crt) {
|
||||
Object[][] ary = crt.Vals();
|
||||
int ary_len = ary.length;
|
||||
Db_fld[] flds = crt.Flds();
|
||||
for (int i = 0; i < ary_len; i++) {
|
||||
Object[] itm = (Object[])ary[i];
|
||||
int itm_len = itm.length;
|
||||
if (i != 0) sb.Add(" OR ");
|
||||
sb.Add("(");
|
||||
for (int j = 0; j < itm_len; j++) {
|
||||
if (j != 0) sb.Add(" AND ");
|
||||
Db_fld fld = flds[j];
|
||||
Object val = itm[j];
|
||||
boolean quote = false;
|
||||
switch (fld.Type_tid()) {
|
||||
case Type_adp_.Tid__str:
|
||||
case Type_adp_.Tid__char:
|
||||
case Type_adp_.Tid__date:
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
sb.Add(fld.Name());
|
||||
sb.Add("=");
|
||||
if (quote) sb.Add("'");
|
||||
sb.Add(Object_.Xto_str_strict_or_empty(val));
|
||||
if (quote) sb.Add("'");
|
||||
}
|
||||
sb.Add(")");
|
||||
}
|
||||
}
|
||||
private Db_arg Wrap(Object val) {return new Db_arg("unknown", val);}
|
||||
}
|
||||
class Sql_qry_wtr_ansi_escape_backslash extends Sql_qry_wtr_ansi { @Override public void Bld_val_str(String_bldr sb, Db_arg arg, String s) {
|
||||
if (String_.Has(s, "\\")) s = String_.Replace(s, "\\", "\\\\");
|
||||
super.Bld_val_str(sb, arg, s);
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs.sqls; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*; import gplx.core.strings.*; import gplx.core.criterias.*; import gplx.dbs.qrys.*;
|
||||
public class Sql_qry_wtr_tst {
|
||||
private final Sql_qry_wtr_fxt fxt = new Sql_qry_wtr_fxt();
|
||||
@Test public void Val() {
|
||||
fxt.Test_val(null , "NULL");
|
||||
fxt.Test_val(true , "1");
|
||||
fxt.Test_val(false , "0");
|
||||
fxt.Test_val(1 , "1");
|
||||
fxt.Test_val(1.1 , "1.1");
|
||||
fxt.Test_val("a" , "'a'");
|
||||
fxt.Test_val("a'b" , "'a''b'");
|
||||
}
|
||||
@Test public void Where_basic() {
|
||||
fxt.Test_where(Db_crt_.eq_("id", 1), "id=1");
|
||||
fxt.Test_where(Db_crt_.eqn_("id", 1), "id!=1");
|
||||
fxt.Test_where(Db_crt_.mt_("id", 1), "id>1");
|
||||
fxt.Test_where(Db_crt_.mte_("id", 1), "id>=1");
|
||||
fxt.Test_where(Db_crt_.lt_("id", 1), "id<1");
|
||||
fxt.Test_where(Db_crt_.lte_("id", 1), "id<=1");
|
||||
fxt.Test_where(Db_crt_.between_("id", 1, 2), "id BETWEEN 1 AND 2");
|
||||
fxt.Test_where(Db_crt_.in_("id", 1, 2, 3), "id IN (1, 2, 3)");
|
||||
}
|
||||
@Test public void Where_and() {
|
||||
fxt.Test_where(Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 AND name='me')");
|
||||
fxt.Test_where(Criteria_.Or(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 OR name='me')");
|
||||
fxt.Test_where(Criteria_.Or(Db_crt_.eq_("id", 1), Criteria_.And(Db_crt_.eq_("name", "me"), Db_crt_.eq_("id", 1))), "(id=1 OR (name='me' AND id=1))");
|
||||
}
|
||||
}
|
||||
class Sql_qry_wtr_fxt {
|
||||
private final Sql_qry_wtr_ansi sql_wtr = (Sql_qry_wtr_ansi)Sql_qry_wtr_.new_ansi();
|
||||
public void Test_val(Object val, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Db_arg arg = new Db_arg("not needed", val);
|
||||
sql_wtr.Bld_val(sb, arg);
|
||||
Tfds.Eq(expd, sb.To_str());
|
||||
}
|
||||
public void Test_where(Criteria crt, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sql_wtr.Bld_where_val(sb, crt);
|
||||
Tfds.Eq(expd, sb.To_str());
|
||||
}
|
||||
}
|
@ -1,30 +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; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*;
|
||||
import gplx.dbs.engines.tdbs.*;
|
||||
public class Sql_select {
|
||||
public Sql_select_fld_list Flds() {return flds;} Sql_select_fld_list flds = Sql_select_fld_list.new_();
|
||||
public boolean Distinct() {return distinct;} public void Distinct_set(boolean v) {distinct = v;} private boolean distinct;
|
||||
public void Add(String fldName) {flds.Add(Sql_select_fld_.new_fld(Sql_select_fld_base.Tbl_null, fldName, fldName));}
|
||||
public void Add(String fldName, String alias) {flds.Add(Sql_select_fld_.new_fld(Sql_select_fld_base.Tbl_null, fldName, alias));}
|
||||
public void Add(Sql_select_fld_base fld) {flds.Add(fld);}
|
||||
|
||||
public static final Sql_select All = all_(); static Sql_select all_() {Sql_select rv = new_(); rv.Add(Sql_select_fld_wild.Instance); return rv;}
|
||||
public static Sql_select new_() {return new Sql_select();} Sql_select() {}
|
||||
}
|
@ -1,70 +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; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.gfo_ndes.*; import gplx.core.type_xtns.*;
|
||||
public class Sql_select_fld_ {
|
||||
public static Sql_select_fld_base new_fld(String tbl, String fld, String alias) {return new Sql_select_fld_fld(tbl, fld, alias);}
|
||||
public static Sql_select_fld_base new_count(String tbl, String fld, String alias) {return new Sql_select_fld_count(tbl, fld, alias);}
|
||||
public static Sql_select_fld_base new_sum(String tbl, String fld, String alias) {return new Sql_select_fld_sum(tbl, fld, alias);}
|
||||
public static Sql_select_fld_base new_min(String tbl, String fld, String alias) {return new Sql_select_fld_minMax(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld_base new_max(String tbl, String fld, String alias) {return new Sql_select_fld_minMax(CompareAble_.More, tbl, fld, alias);}
|
||||
}
|
||||
class Sql_select_fld_fld extends Sql_select_fld_base {
|
||||
public Sql_select_fld_fld(String tbl, String fld, String alias) {this.ctor_(tbl, fld, alias);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {return curVal;}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@Override public String XtoSql() {
|
||||
String rv = Fld();
|
||||
if (Tbl() != Tbl_null)
|
||||
rv = Tbl() + "." + Fld();
|
||||
if (!String_.Eq(Alias(), Fld()))
|
||||
rv = rv + " AS " + Alias();
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_count extends Sql_select_fld_func_base {
|
||||
public Sql_select_fld_count(String tbl, String fld, String alias) {this.ctor_(tbl, fld, alias);}
|
||||
@Override public String XtoSql_functionName() {return "COUNT";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(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_base {
|
||||
public Sql_select_fld_sum(String tbl, String fld, String alias) {this.ctor_(tbl, fld, alias);}
|
||||
@Override public String XtoSql_functionName() {return "SUM";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(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_base {
|
||||
private final int compareType;
|
||||
public Sql_select_fld_minMax(int compareType, String tbl, String fld, String alias) {
|
||||
this.compareType = compareType;
|
||||
this.ctor_(tbl, fld, alias);
|
||||
}
|
||||
@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;
|
||||
}
|
||||
}
|
@ -1,46 +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; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.gfo_ndes.*; import gplx.core.type_xtns.*;
|
||||
public abstract class Sql_select_fld_base {
|
||||
public String Tbl() {return tbl;} public void Tbl_set(String val) {tbl = val;} private String tbl;
|
||||
public String Fld() {return fld;} public void Fld_set(String val) {fld = val;} private String fld;
|
||||
public String Alias() {return alias;} public void Alias_set(String val) {alias = val;} private String alias;
|
||||
public ClassXtn ValType() {return valType;} public void ValType_set(ClassXtn val) {valType = val;} ClassXtn valType = ObjectClassXtn.Instance;
|
||||
public abstract Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type);
|
||||
@gplx.Virtual public void GroupBy_type(GfoFld fld) {this.ValType_set(fld.Type());}
|
||||
@gplx.Virtual public boolean Type_fld() {return true;}
|
||||
public abstract String XtoSql();
|
||||
public static final String Tbl_null = null;
|
||||
@gplx.Internal protected void ctor_(String tbl, String fld, String alias) {
|
||||
Tbl_set(tbl); Fld_set(fld); Alias_set(alias);
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_wild extends Sql_select_fld_base {
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {throw Err_.new_wo_type("group by eval not allowed on *");}
|
||||
@Override public void GroupBy_type(GfoFld fld) {throw Err_.new_wo_type("group by type not allowed on *");}
|
||||
@Override public String XtoSql() {return "*";}
|
||||
public static final Sql_select_fld_wild Instance = new Sql_select_fld_wild(); Sql_select_fld_wild() {this.ctor_(Tbl_null, "*", "*");}
|
||||
}
|
||||
abstract class Sql_select_fld_func_base extends Sql_select_fld_base {
|
||||
public abstract String XtoSql_functionName();
|
||||
@Override public boolean Type_fld() {return false;}
|
||||
@Override public String XtoSql() {
|
||||
return String_.Format("{0}({1}) AS {2}", XtoSql_functionName(), Fld(), Alias());
|
||||
}
|
||||
}
|
@ -1,57 +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; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*; import gplx.core.gfo_ndes.*;
|
||||
import gplx.dbs.engines.tdbs.*;
|
||||
public class Sql_select_fld_list {
|
||||
public int Count() {return hash.Count();}
|
||||
public void Add(Sql_select_fld_base fld) {hash.Add(fld.Alias(), fld);}
|
||||
public Sql_select_fld_base Get_at(int i) {return (Sql_select_fld_base)hash.Get_at(i);}
|
||||
public Sql_select_fld_base FetchOrNull(String k) {return (Sql_select_fld_base)hash.Get_by(k);}
|
||||
public GfoFldList XtoGfoFldLst(TdbTable tbl) {
|
||||
GfoFldList rv = GfoFldList_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base selectFld = this.Get_at(i);
|
||||
GfoFld fld = tbl.Flds().FetchOrNull(selectFld.Fld());
|
||||
if (fld == null) throw Err_.new_wo_type("fld not found in tbl", "fldName", selectFld.Fld(), "tblName", tbl.Name(), "tblFlds", tbl.Flds().To_str());
|
||||
if (rv.Has(selectFld.Alias())) throw Err_.new_wo_type("alias is not unique", "fldName", selectFld.Fld(), "flds", rv.To_str());
|
||||
selectFld.GroupBy_type(fld);
|
||||
rv.Add(selectFld.Alias(), selectFld.ValType());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String[] To_str_ary() {
|
||||
int len = this.Count();
|
||||
String[] rv = new String[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
Sql_select_fld_base fld = this.Get_at(i);
|
||||
rv[i] = fld.Fld();
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String To_str() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base fld = this.Get_at(i);
|
||||
sb.Add_fmt("{0},{1}|", fld.Fld(), fld.Alias());
|
||||
}
|
||||
return sb.To_str();
|
||||
}
|
||||
Ordered_hash hash = Ordered_hash_.New();
|
||||
public static Sql_select_fld_list new_() {return new Sql_select_fld_list();} Sql_select_fld_list() {}
|
||||
}
|
@ -1,29 +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; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.strings.*;
|
||||
public class Sql_tbl_src {
|
||||
public Sql_join_itmType JoinType() {return type;} public Sql_tbl_src JoinType_(Sql_join_itmType v) {this.type = v; return this;} Sql_join_itmType type = Sql_join_itmType.Inner;
|
||||
public List_adp JoinLinks() {return joinLinks;} List_adp joinLinks = List_adp_.new_();
|
||||
public String TblName() {return tblName;} public Sql_tbl_src TblName_(String s) {tblName = s; return this;} private String tblName;
|
||||
public String Alias() {return alias;} public Sql_tbl_src Alias_(String s) {alias = s; return this;} private String alias;
|
||||
public void XtoSql(String_bldr sb) {
|
||||
sb.Add_many(tblName, alias == null ? "" : " " + alias);
|
||||
}
|
||||
public static Sql_tbl_src new_() {return new Sql_tbl_src();} Sql_tbl_src() {}
|
||||
}
|
@ -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;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue