mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v1.9.2.1
This commit is contained in:
@@ -16,30 +16,35 @@ 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.*;
|
||||
interface Db_engine extends RlsAble {
|
||||
String Key();
|
||||
Db_connect ConnectInfo();
|
||||
void Connect();
|
||||
Object Execute(Db_qry cmd);
|
||||
DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql);
|
||||
Db_stmt New_db_stmt(Db_provider provider, Db_qry sql);
|
||||
Object New_db_cmd(String sql);
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
Db_engine MakeEngine(Db_connect connectInfo);
|
||||
public interface Db_engine {
|
||||
String Key();
|
||||
Db_conn_info Conn_info();
|
||||
void Conn_open();
|
||||
void Conn_term();
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
Db_engine Make_new(Db_conn_info conn_info);
|
||||
|
||||
Db_rdr New_db_rdr(Object o, String sql); // ResultSet if desktop; Cursor if android
|
||||
Db_stmt New_db_stmt(Db_provider provider, Db_qry sql);
|
||||
Object Execute(Db_qry cmd);
|
||||
DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql);
|
||||
Object New_db_cmd(String sql);
|
||||
}
|
||||
class Db_engine_null implements Db_engine {
|
||||
public String Key() {return Db_connect_.Null.Key_of_db_connect();}
|
||||
public Db_connect ConnectInfo() {return Db_connect_.Null;}
|
||||
public void Connect() {}
|
||||
public String Key() {return Db_conn_info_.Null.Key();}
|
||||
public Db_conn_info Conn_info() {return Db_conn_info_.Null;}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {}
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public Db_engine Make_new(Db_conn_info conn_info) {return this;}
|
||||
|
||||
public Object Execute(Db_qry cmd) {return cmd.ExecRdrAble() ? (Object)DataRdr_.Null : -1;}
|
||||
public Object New_db_cmd(String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public Db_stmt New_db_stmt(Db_provider provider, Db_qry qry) {return Db_stmt_.Null;}
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public void Rls() {}
|
||||
public Db_engine MakeEngine(Db_connect connectInfo) {return this;}
|
||||
public Object New_db_cmd(String sql) {throw Err_.not_implemented_();}
|
||||
public DataRdr NewDataRdr(java.sql.ResultSet rdr, String sql) {return DataRdr_.Null;}
|
||||
public Db_rdr New_db_rdr(Object o, String sql) {return Db_rdr_.Null;}
|
||||
public Db_stmt New_db_stmt(Db_provider provider, Db_qry qry) {return Db_stmt_.Null;}
|
||||
public static final Db_engine_null _ = new Db_engine_null(); Db_engine_null() {}
|
||||
}
|
||||
class ExecSqlWkr implements Db_qryWkr {
|
||||
|
||||
32
140_dbs/src_200_engine/gplx/dbs/Db_engine_regy.java
Normal file
32
140_dbs/src_200_engine/gplx/dbs/Db_engine_regy.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
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.*;
|
||||
public class Db_engine_regy {
|
||||
private final HashAdp hash = HashAdp_.new_();
|
||||
Db_engine_regy() {
|
||||
this.Add(Db_engine_null._)
|
||||
.Add(TdbEngine._)
|
||||
.Add(Mysql_engine._)
|
||||
.Add(Postgres_engine._)
|
||||
.Add(Sqlite_engine._)
|
||||
;
|
||||
}
|
||||
public Db_engine_regy Add(Db_engine engine) {hash.Add(engine.Key(), engine); return this;}
|
||||
public Db_engine Get(String key) {return (Db_engine)hash.FetchOrFail(key);}
|
||||
public static final Db_engine_regy _ = new Db_engine_regy();
|
||||
}
|
||||
@@ -17,19 +17,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.dbs; import gplx.*;
|
||||
import java.sql.*;
|
||||
abstract class Db_engine_sql_base implements Db_engine, RlsAble {
|
||||
abstract class Db_engine_sql_base implements Db_engine {
|
||||
public abstract String Key();
|
||||
public Db_connect ConnectInfo() {return dbInfo;} protected Db_connect dbInfo;
|
||||
public abstract Db_engine MakeEngine(Db_connect dbInfo);
|
||||
public Db_conn_info Conn_info() {return conn_info;} protected Db_conn_info conn_info;
|
||||
@gplx.Virtual public void Txn_bgn() {Execute(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));}
|
||||
@gplx.Virtual public void Txn_end() {Execute(Db_qry_sql.xtn_("COMMIT TRANSACTION;"));}
|
||||
public abstract Db_engine Make_new(Db_conn_info conn_info);
|
||||
@gplx.Virtual public Sql_cmd_wtr SqlWtr() {return Sql_cmd_wtr_.Ansi;}
|
||||
public Object Execute(Db_qry cmd) {
|
||||
Db_qryWkr wkr = (Db_qryWkr)wkrs.FetchOrFail(cmd.KeyOfDb_qry());
|
||||
return wkr.Exec(this, cmd);
|
||||
}
|
||||
@gplx.Virtual public void Txn_bgn() {Execute(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));}
|
||||
@gplx.Virtual public void Txn_end() {Execute(Db_qry_sql.xtn_("COMMIT TRANSACTION;"));}
|
||||
@gplx.Internal protected void ctor_SqlEngineBase(Db_connect dbInfo) {
|
||||
this.dbInfo = dbInfo;
|
||||
@gplx.Internal protected void ctor_SqlEngineBase(Db_conn_info conn_info) {
|
||||
this.conn_info = conn_info;
|
||||
wkrs.Add(Db_qry_select.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_insert.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_update.KeyConst, new ExecSqlWkr());
|
||||
@@ -53,21 +53,22 @@ abstract class Db_engine_sql_base implements Db_engine, RlsAble {
|
||||
}
|
||||
catch (Exception exc) {throw Err_.err_(exc, "exec reader failed").Add("sql", sql).Add("err", Err_.Message_gplx_brief(exc));}
|
||||
}
|
||||
@gplx.Internal protected abstract Connection NewDbCon();
|
||||
@gplx.Internal protected abstract Connection Conn_new();
|
||||
@gplx.Virtual public DataRdr NewDataRdr(ResultSet rdr, String sql) {return gplx.stores.Db_data_rdr_.new_(rdr, sql);}
|
||||
public Db_stmt New_db_stmt(Db_provider provider, Db_qry qry) {return new Db_stmt_cmd(provider, qry);}
|
||||
public Db_rdr New_db_rdr(Object rdr, String sql) {return new Db_rdr__basic((ResultSet)rdr, sql);}
|
||||
public Db_stmt New_db_stmt(Db_provider provider, Db_qry qry) {return new Db_stmt_cmd(provider, qry);}
|
||||
public Object New_db_cmd(String sql) {
|
||||
try {return connection.prepareStatement(sql);}
|
||||
catch (Exception e) {
|
||||
throw Err_.err_(e, "failed to prepare sql; sql={0}", sql);}
|
||||
}
|
||||
public void Connect() {
|
||||
connection = NewDbCon();
|
||||
public void Conn_open() {
|
||||
connection = Conn_new();
|
||||
} private Connection connection;
|
||||
public void Rls() {
|
||||
public void Conn_term() {
|
||||
// if (Env_.Mode_testing()) return; // WORKAROUND:MYSQL:else errors randomly when running all tests. possible connection pooling issue (?); // commented out 2013-08-22
|
||||
try {connection.close();}
|
||||
catch (SQLException e) {throw Err_.err_(e, "close connection failed").Add("ConnectInfo", dbInfo.Raw_of_db_connect());}
|
||||
catch (SQLException e) {throw Err_.err_(e, "close connection failed").Add("ConnectInfo", conn_info.Str_raw());}
|
||||
}
|
||||
Statement NewDbCmd(String commandText) {
|
||||
Statement cmd = null;
|
||||
@@ -77,6 +78,6 @@ abstract class Db_engine_sql_base implements Db_engine, RlsAble {
|
||||
}
|
||||
protected Connection NewDbCon(String url, String uid, String pwd) {
|
||||
try {return DriverManager.getConnection(url, uid, pwd);}
|
||||
catch (SQLException e) {throw Err_.err_(e, "connection open failed").Add("ConnectInfo", ConnectInfo().Raw_of_db_connect());}
|
||||
catch (SQLException e) {throw Err_.err_(e, "connection open failed").Add("ConnectInfo", Conn_info().Str_raw());}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user