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

'v3.9.2.1'

This commit is contained in:
gnosygnu
2016-09-11 21:49:20 -04:00
parent 232838c732
commit 35d78f6106
310 changed files with 4358 additions and 5116 deletions

View File

@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.dbs; import gplx.*;
import gplx.dbs.qrys.*;
public class Db_stmt_ {
public static final Db_stmt Null = new Db_stmt_sql();
public static final Db_stmt Null = new Db_stmt_sql();
public static Db_stmt new_insert_(Db_conn conn, String tbl, String... flds) {
Db_qry qry = Db_qry_insert.new_(tbl, flds);
return conn.Stmt_new(qry);

View File

@@ -127,4 +127,13 @@ public abstract class Db_engine_sql_base implements Db_engine {
try {return DriverManager.getConnection(url, uid, pwd);}
catch (SQLException e) {throw Err_.new_exc(e, "db", "connection open failed", "info", Conn_info().Raw());}
}
protected Connection Conn__new_by_url_and_props(String url, Keyval... props) {
try {
java.util.Properties properties = new java.util.Properties();
for (Keyval prop : props)
properties.setProperty(prop.Key(), prop.Val_to_str_or_empty());
return DriverManager.getConnection(url, properties);
}
catch (SQLException e) {throw Err_.new_exc(e, "db", "connection open failed", "info", Conn_info().Raw());}
}
}

View File

@@ -19,6 +19,7 @@ package gplx.dbs.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.d
import java.sql.*;
import gplx.core.stores.*; import gplx.dbs.engines.*; import gplx.dbs.engines.sqlite.*; import gplx.dbs.metas.*; import gplx.dbs.sqls.*;
import gplx.dbs.qrys.*;
import gplx.core.ios.IoItmFil;
import org.sqlite.SQLiteConnection;
public class Sqlite_engine extends Db_engine_sql_base {
private final Sqlite_txn_mgr txn_mgr; private final Sqlite_schema_mgr schema_mgr;
@@ -61,8 +62,22 @@ public class Sqlite_engine extends Db_engine_sql_base {
catch (ClassNotFoundException e) {throw Err_.new_exc(e, "db", "could not load sqlite jdbc driver");}
loaded = true;
}
// init vars for opening connection
Sqlite_conn_info conn_info_as_sqlite = (Sqlite_conn_info)conn_info;
Connection rv = Conn_make_by_url("jdbc:sqlite://" + String_.Replace(conn_info_as_sqlite.Url().Raw(), "\\", "/"), "", "");
Io_url sqlite_fs_url = conn_info_as_sqlite.Url();
String sqlite_db_url = "jdbc:sqlite://" + String_.Replace(sqlite_fs_url.Raw(), "\\", "/");
// set open_mode flag if conn is read-only; needed else all SELECT queries will be very slow; DATE:2016-09-03
IoItmFil sqlite_fs_itm = Io_mgr.Instance.QueryFil(sqlite_fs_url);
Keyval[] props = sqlite_fs_itm.Exists() && sqlite_fs_itm.ReadOnly() // NOTE: must check if it exists; else missing-file will be marked as readonly connection, and missing-file will sometimes be dynamically created as read-write; DATE:2016-09-04
? Keyval_.Ary(Keyval_.new_("open_mode", "1"))
: Keyval_.Ary_empty;
// open connection
Connection rv = Conn__new_by_url_and_props(sqlite_db_url, props);
// set busyTimeout; needed else multiple processes accessing same db can cause "database is locked" error; DATE:2016-09-03
SQLiteConnection rv_as_sqlite = (org.sqlite.SQLiteConnection)rv;
try {rv_as_sqlite.setBusyTimeout(10000);}
catch (SQLException e) {Gfo_usr_dlg_.Instance.Warn_many("", "", "failed to set busy timeout; err=~{0}", Err_.Message_gplx_log(e));}

View File

@@ -36,7 +36,7 @@ public class Db_stmt_arg_list {
}
list.Clear();
}
private static void Fill_crt(Db_stmt stmt, int tid, String key, Object val) {
public static void Fill_crt(Db_stmt stmt, int tid, String key, Object val) {
switch (tid) {
case Dbmeta_fld_tid.Tid__bool: stmt.Crt_bool_as_byte (key, Bool_.cast(val)); break;
case Dbmeta_fld_tid.Tid__byte: stmt.Crt_byte (key, Byte_.cast(val)); break;

View File

@@ -0,0 +1,80 @@
/*
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.utls; import gplx.*; import gplx.dbs.*;
import gplx.dbs.stmts.*;
public class Db_tbl__crud_ {
public static void Upsert(Db_conn conn, String tbl_name, Dbmeta_fld_list flds, String[] crt_cols, Object... objs) {
// init
int crt_cols_len = crt_cols.length;
String[] val_cols = Find_excepts(flds, crt_cols);
// check if exists
Db_stmt select_stmt = conn.Stmt_select(tbl_name, crt_cols, crt_cols);
Add_arg(select_stmt, flds, crt_cols, objs, Bool_.Y, 0);
Db_rdr rdr = select_stmt.Exec_select__rls_auto();
boolean exists = rdr.Move_next();
rdr.Rls();
// do update / insert; NOTE: 0-index and crt_cols_len assumes that objs starts with crts; EX: (id) -> (1, 'abc') x> ('abc', 1)
// update
if (exists) {
Db_stmt update_stmt = conn.Stmt_update(tbl_name, crt_cols, crt_cols);
Add_arg(update_stmt, flds, crt_cols, objs, Bool_.Y, 0);
Add_arg(update_stmt, flds, val_cols, objs, Bool_.N, crt_cols_len);
update_stmt.Exec_update();
update_stmt.Rls();
}
// insert
else {
Db_stmt insert_stmt = conn.Stmt_insert(tbl_name, flds);
Add_arg(insert_stmt, flds, crt_cols, objs, Bool_.N, 0);
Add_arg(insert_stmt, flds, val_cols, objs, Bool_.N, crt_cols_len);
insert_stmt.Exec_insert();
insert_stmt.Rls();
}
}
private static String[] Find_excepts(Dbmeta_fld_list flds, String[] cols) {
// hash cols
Hash_adp hash = Hash_adp_.New();
int cols_len = cols.length;
for (int i = 0; i < cols_len; ++i)
hash.Add_as_key_and_val(cols[i]);
// loop flds and get excepts
List_adp list = List_adp_.New();
int flds_len = flds.Len();
for (int i = 0; i < flds_len; ++i) {
Dbmeta_fld_itm fld = flds.Get_at(i);
if (!hash.Has(fld.Name()))
list.Add(fld.Name());
}
return list.To_str_ary_and_clear();
}
private static void Add_arg(Db_stmt stmt, Dbmeta_fld_list flds, String[] cols, Object[] objs, boolean mode_is_crt, int objs_bgn) {
int cols_len = cols.length;
for (int i = 0; i < cols_len; ++i) {
String col = cols[i];
Dbmeta_fld_itm fld = flds.Get_by(col);
Object obj = objs[i + objs_bgn];
if (mode_is_crt)
Db_stmt_arg_list.Fill_crt(stmt, fld.Type().Tid_ansi(), col, obj);
else
Db_stmt_arg_list.Fill_val(stmt, fld.Type().Tid_ansi(), col, obj);
}
}
}