/* 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 . */ package gplx.dbs.qrys; import gplx.*; import gplx.dbs.*; import gplx.core.strings.*; import gplx.core.criterias.*; public class Db_qry__select_in_tbl implements Db_qry { public Db_qry__select_in_tbl(String base_table, String[] select_flds, String[] where_flds, String group_by_sql, String having_sql, String order_by_sql, String limit_sql) { this.base_table = base_table; this.select_flds = select_flds; this.where_flds = where_flds; this.group_by_sql = group_by_sql; this.having_sql = having_sql; this.order_by_sql = order_by_sql; this.limit_sql = limit_sql; } public int Tid() {return Db_qry_.Tid_select_in_tbl;} public boolean Exec_is_rdr() {return true;} public String Base_table() {return base_table;} private final String base_table; public Criteria Where() {return where;} private Criteria where; public String[] Select_flds() {return select_flds;} private final String[] select_flds; private final String[] where_flds; public void Where_sql(String_bldr sb) { if (where_flds == null) return; int where_flds_len = where_flds.length; if (where_flds_len == 0) return; for (int i = 0; i < where_flds_len; ++i) { if (i != 0) sb.Add("AND "); sb.Add(where_flds[i]).Add(" = ? "); } } public String Group_by_sql() {return group_by_sql;} private final String group_by_sql; public String Having_sql() {return having_sql;} private final String having_sql; public String Order_by_sql() {return order_by_sql;} public Db_qry__select_in_tbl Order_by_sql_(String v) {order_by_sql = v; return this;} private String order_by_sql; public String Limit_sql() {return limit_sql;} private final String limit_sql; public String To_sql__exec(gplx.dbs.sqls.Sql_qry_wtr wtr) { synchronized (this) { String_bldr sb = String_bldr_.new_(); sb.Add("SELECT "); int select_flds_len = select_flds.length; for (int i = 0; i < select_flds_len; ++i) { if (i != 0) sb.Add(","); sb.Add(select_flds[i]); } sb.Add(" FROM ").Add(base_table); if (where_flds != null && where_flds.length != 0) {sb.Add(" WHERE "); Where_sql(sb);} if (group_by_sql != null) sb.Add(group_by_sql); if (having_sql != null) sb.Add(having_sql); if (order_by_sql != null) {sb.Add(" ORDER BY "); sb.Add(order_by_sql);} if (limit_sql != null) sb.Add(limit_sql); return sb.To_str(); } } public static Db_qry__select_in_tbl new_(String base_table, String[] where_flds, String[] select_flds, String[] order_flds) { String order_by_sql = null; if (order_flds != Order_by_null) { int len = order_flds.length; switch (len) { case 0: break; case 1: order_by_sql = order_flds[0]; break; default: Bry_bfr bfr = Bry_bfr.new_(); for (int i = 0; i < len; ++i) { String order_fld = order_flds[i]; if (i != 0) bfr.Add_byte_comma(); bfr.Add_str_a7(order_fld); } order_by_sql = bfr.To_str_and_clear(); break; } } Db_qry__select_in_tbl rv = new Db_qry__select_in_tbl(base_table, select_flds, where_flds, null, null, order_by_sql, null); rv.where = where_flds.length == 0 ? Db_crt_.Wildcard : Db_crt_.eq_many_(where_flds); return rv; } public static Db_qry__select_in_tbl as_(Object obj) {return obj instanceof Db_qry__select_in_tbl ? (Db_qry__select_in_tbl)obj : null;} public static final String[] Where_flds__all = String_.Ary_empty; public static final String[] Order_by_null = null; }