1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-10-27 20:34:16 +00:00
gnosygnu_xowa/140_dbs/src/gplx/dbs/Db_conn.java

83 lines
4.9 KiB
Java
Raw Normal View History

2015-01-26 01:56:50 +00:00
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs; import gplx.*;
2015-02-23 02:03:49 +00:00
import gplx.dbs.engines.*; import gplx.dbs.qrys.*;
2015-01-26 01:56:50 +00:00
public class Db_conn {
private final Db_engine engine;
2015-02-23 02:03:49 +00:00
private final ListAdp rls_list = ListAdp_.new_();
2015-01-26 01:56:50 +00:00
public Db_conn(Db_engine engine) {
this.engine = engine;
2015-02-23 02:03:49 +00:00
this.txn_mgr = new Db_txn_mgr(engine);
2015-01-26 01:56:50 +00:00
}
public Db_url Url() {return engine.Url();}
public Db_txn_mgr Txn_mgr() {return txn_mgr;} private final Db_txn_mgr txn_mgr;
2015-03-09 01:27:59 +00:00
public void Txn_bgn() {txn_mgr.Txn_bgn();}
public void Txn_commit() {txn_mgr.Txn_end(); txn_mgr.Txn_bgn();}
public void Txn_end() {txn_mgr.Txn_end();}
2015-02-23 02:03:49 +00:00
public Db_stmt Stmt_insert(String tbl, Db_meta_fld_list flds) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, flds.To_str_ary()));}
public Db_stmt Stmt_insert(String tbl, String... cols) {return engine.New_stmt_prep(Db_qry_insert.new_(tbl, cols));}
public Db_stmt Stmt_update(String tbl, String[] where, String... cols) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, cols));}
public Db_stmt Stmt_update_exclude(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry_update.new_(tbl, where, flds.To_str_ary_exclude(where)));}
public Db_stmt Stmt_delete(String tbl, String... where) {return engine.New_stmt_prep(Db_qry_delete.new_(tbl, where));}
2015-03-09 01:27:59 +00:00
public Db_stmt Stmt_select(String tbl, String[] cols, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, cols, null));}
public Db_stmt Stmt_select(String tbl, Db_meta_fld_list flds, String... where) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary(), null));}
public Db_stmt Stmt_select_order(String tbl, Db_meta_fld_list flds, String[] where, String... orderbys) {return engine.New_stmt_prep(Db_qry__select_in_tbl.new_(tbl, where, flds.To_str_ary(), orderbys));}
2015-02-23 02:03:49 +00:00
public void Exec_create_tbl_and_idx(Db_meta_tbl meta) {
2015-03-02 02:59:12 +00:00
engine.Exec_ddl_create_tbl(meta);
engine.Exec_ddl_create_idx(Gfo_usr_dlg_.Null, meta.Idxs());
2015-02-01 20:11:16 +00:00
}
2015-03-02 02:59:12 +00:00
public void Exec_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... idxs) {engine.Exec_ddl_create_idx(usr_dlg, idxs);}
2015-03-09 01:27:59 +00:00
public void Exec_env_db_attach(String alias, Io_url db_url) {engine.Exec_env_db_attach(alias, db_url);}
public void Exec_env_db_detach(String alias) {engine.Exec_env_db_detach(alias);}
2015-03-02 02:59:12 +00:00
public void Exec_ddl_append_fld(String tbl, Db_meta_fld fld) {engine.Exec_ddl_append_fld(tbl, fld);}
2015-02-23 02:03:49 +00:00
public Db_stmt Rls_reg(Db_stmt stmt) {rls_list.Add(stmt); return stmt;}
2015-01-26 01:56:50 +00:00
public void Conn_term() {
2015-02-23 02:03:49 +00:00
int len = rls_list.Count();
2015-01-26 01:56:50 +00:00
for (int i = 0; i < len; ++i) {
2015-02-23 02:03:49 +00:00
RlsAble itm = (RlsAble)rls_list.FetchAt(i);
itm.Rls();
2015-01-26 01:56:50 +00:00
}
engine.Conn_term();
2015-02-23 02:03:49 +00:00
// Db_conn_pool.I.Del(this.Url()); // remove from pool, else rls'd instance will be cached and fail upon next use
2015-01-26 01:56:50 +00:00
}
2015-02-23 02:03:49 +00:00
public Db_stmt Stmt_new(Db_qry qry) {return engine.New_stmt_prep(qry);}
2015-01-26 01:56:50 +00:00
public int Exec_qry(Db_qry qry) {txn_mgr.Txn_count_(txn_mgr.Txn_count() + 1); return Int_.cast_(engine.Exec_as_obj(qry));}
public DataRdr Exec_qry_as_rdr(Db_qry qry) {return DataRdr_.cast_(engine.Exec_as_obj(qry));}
public int Exec_sql(String sql) {return this.Exec_qry(Db_qry_sql.dml_(sql));}
public DataRdr Exec_sql_as_rdr(String sql) {return this.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));}
2015-03-09 01:27:59 +00:00
public void Exec_sql_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... idxs) {engine.Exec_ddl_create_idx(usr_dlg, idxs);}
public void Exec_sql(Db_batch_wkr... wkrs) {
int len = wkrs.length;
for (int i = 0; i < len; ++i) {
Db_batch_wkr wkr = wkrs[i];
wkr.Batch_bgn();
wkr.Batch_end();
}
}
public void Exec_sql__vacuum(Db_batch_wkr__msg msg) {
msg.Msg_("vaccuum");
Exec_sql(msg, Batch_sql("VACCUUM;"));
}
public void Exec_sql__idx(Db_batch_wkr__msg msg, Db_meta_idx... idxs) {
engine.Exec_ddl_create_idx(msg.Usr_dlg(), idxs);
}
public Db_batch_wkr__msg Batch_msg(Gfo_usr_dlg usr_dlg, String msg_pre) {return new Db_batch_wkr__msg(usr_dlg, msg_pre);}
public Db_batch_wkr__attach Batch_attach(String alias, Io_url url) {return new Db_batch_wkr__attach(this).Add(alias, url);}
public Db_batch_wkr__sql Batch_sql(String... lines) {return new Db_batch_wkr__sql(this, lines);}
2015-01-26 01:56:50 +00:00
}