mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v1.6.5.1
This commit is contained in:
21
140_dbs/.classpath
Normal file
21
140_dbs/.classpath
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src_100_core"/>
|
||||
<classpathentry kind="src" path="src_110_dbQry"/>
|
||||
<classpathentry kind="src" path="src_120_sql"/>
|
||||
<classpathentry kind="src" path="src_130_misc"/>
|
||||
<classpathentry kind="src" path="src_200_engine"/>
|
||||
<classpathentry kind="src" path="src_300_sqlDbs"/>
|
||||
<classpathentry kind="src" path="src_400_tdbs"/>
|
||||
<classpathentry kind="src" path="src_410_stores"/>
|
||||
<classpathentry kind="src" path="tst"/>
|
||||
<classpathentry kind="src" path="xtn"/>
|
||||
<classpathentry kind="src" path="/100_core"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/110_gfml"/>
|
||||
<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.12-bin.jar"/>
|
||||
<classpathentry kind="lib" path="lib/postgresql-8.4-701.jdbc4.jar"/>
|
||||
<classpathentry kind="lib" path="lib/sqlite-jdbc-3.7.15-M1.jar"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
140_dbs/.project
Normal file
17
140_dbs/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>140_dbs</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
BIN
140_dbs/lib/mysql-connector-java-5.1.12-bin.jar
Normal file
BIN
140_dbs/lib/mysql-connector-java-5.1.12-bin.jar
Normal file
Binary file not shown.
BIN
140_dbs/lib/postgresql-8.4-701.jdbc4.jar
Normal file
BIN
140_dbs/lib/postgresql-8.4-701.jdbc4.jar
Normal file
Binary file not shown.
BIN
140_dbs/lib/sqlite-jdbc-3.7.15-M1.jar
Normal file
BIN
140_dbs/lib/sqlite-jdbc-3.7.15-M1.jar
Normal file
Binary file not shown.
97
140_dbs/src_100_core/gplx/dbs/Db_connect.java
Normal file
97
140_dbs/src_100_core/gplx/dbs/Db_connect.java
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
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 interface Db_connect {
|
||||
String Key_of_db_connect();
|
||||
String Server();
|
||||
String Database();
|
||||
String Raw_of_db_connect();
|
||||
String Api_of_db_connect();
|
||||
Db_connect Clone_of_db_connect(String raw, GfoMsg m);
|
||||
}
|
||||
class Db_connect_null extends Db_connect_base {
|
||||
@Override public String Key_of_db_connect() {return KeyDef;} public static final String KeyDef = "null_db";
|
||||
@Override public Db_connect Clone_of_db_connect(String raw, GfoMsg m) {return this;}
|
||||
public static final Db_connect_null _ = new Db_connect_null(); Db_connect_null() {this.Ctor_of_db_connect("", "", "gplx_key=null_db", "");}
|
||||
}
|
||||
class Db_connect_mysql extends Db_connect_base {
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
@Override public String Key_of_db_connect() {return KeyDef;} public static final String KeyDef = "mysql";
|
||||
public static Db_connect new_(String server, String database, String uid, String pwd) {
|
||||
return Db_connect_.parse_(BldRaw(GfoMsg_.new_cast_("Db_connect")
|
||||
.Add("gplx_key", KeyDef)
|
||||
.Add("server", server)
|
||||
.Add("database", database)
|
||||
.Add("uid", uid)
|
||||
.Add("pwd", pwd)
|
||||
.Add("charset", "utf8")
|
||||
));
|
||||
}
|
||||
@Override public Db_connect Clone_of_db_connect(String raw, GfoMsg m) {
|
||||
Db_connect_mysql rv = new Db_connect_mysql();
|
||||
rv.Ctor_of_db_connect(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("charset", "utf8")));
|
||||
rv.uid = m.ReadStr("uid");
|
||||
rv.pwd = m.ReadStr("pwd");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_connect_mysql _ = new Db_connect_mysql(); Db_connect_mysql() {}
|
||||
}
|
||||
class Db_connect_postgres extends Db_connect_base {
|
||||
public String Uid() {return uid;} private String uid;
|
||||
public String Pwd() {return pwd;} private String pwd;
|
||||
@Override public String Key_of_db_connect() {return KeyDef;} public static final String KeyDef = "postgresql";
|
||||
public static Db_connect new_(String server, String database, String uid, String pwd) {
|
||||
return Db_connect_.parse_(BldRaw(GfoMsg_.new_cast_("Db_connect")
|
||||
.Add("gplx_key", KeyDef)
|
||||
.Add("server", server)
|
||||
.Add("database", database)
|
||||
.Add("port", 5432)
|
||||
.Add("user id", uid)
|
||||
.Add("password", pwd)
|
||||
.Add("encoding", "unicode") // needed for 1.1 provider; otherwise, ascii
|
||||
));
|
||||
}
|
||||
@Override public Db_connect Clone_of_db_connect(String raw, GfoMsg m) {
|
||||
Db_connect_postgres rv = new Db_connect_postgres();
|
||||
rv.Ctor_of_db_connect(m.ReadStr("server"), m.ReadStr("database"), raw, BldApi(m, KeyVal_.new_("encoding", "unicode")));
|
||||
rv.uid = m.ReadStr("user id");
|
||||
rv.pwd = m.ReadStr("password");
|
||||
return rv;
|
||||
}
|
||||
public static final Db_connect_postgres _ = new Db_connect_postgres(); Db_connect_postgres() {}
|
||||
}
|
||||
class Db_connect_tdb extends Db_connect_base {
|
||||
public Io_url Url() {return url;} Io_url url;
|
||||
@Override public String Key_of_db_connect() {return KeyDef;} public static final String KeyDef = "tdb";
|
||||
public static Db_connect new_(Io_url url) {
|
||||
return Db_connect_.parse_(BldRaw(GfoMsg_.new_cast_("Db_connect")
|
||||
.Add("gplx_key", KeyDef)
|
||||
.Add("url", url.Raw())
|
||||
));
|
||||
} Db_connect_tdb() {}
|
||||
@Override public Db_connect Clone_of_db_connect(String raw, GfoMsg m) {
|
||||
Db_connect_tdb rv = new Db_connect_tdb();
|
||||
String urlStr = m.ReadStr("url");
|
||||
Io_url url = Io_url_.new_any_(urlStr);
|
||||
rv.Ctor_of_db_connect(urlStr, url.NameOnly(), raw, BldApi(m));
|
||||
rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final Db_connect_tdb _ = new Db_connect_tdb();
|
||||
}
|
||||
52
140_dbs/src_100_core/gplx/dbs/Db_connect_.java
Normal file
52
140_dbs/src_100_core/gplx/dbs/Db_connect_.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
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_connect_ {
|
||||
public static final Db_connect Null = Db_connect_null._;
|
||||
public static final Db_connect Test = Db_connect_mysql.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static Db_connect parse_(String raw) {return Db_connect_pool._.Parse(raw);}
|
||||
public static Db_connect tdb_(Io_url url) {return Db_connect_tdb.new_(url);}
|
||||
public static Db_connect sqlite_(Io_url url) {return Db_connect_sqlite.load_(url);}
|
||||
public static final String TdbKey = TdbEngine.KeyDef;
|
||||
}
|
||||
class Db_connect_pool {
|
||||
public Db_connect_pool() {
|
||||
this.Add(Db_connect_null._).Add(Db_connect_tdb._).Add(Db_connect_mysql._).Add(Db_connect_postgres._).Add(Db_connect_sqlite._);
|
||||
}
|
||||
public Db_connect_pool Add(Db_connect prototype) {regy.AddReplace(prototype.Key_of_db_connect(), prototype); return this;}
|
||||
public Db_connect Parse(String raw) {// assume each pair has format of: name=val;
|
||||
try {
|
||||
GfoMsg m = GfoMsg_.new_parse_("dbConnectionString");
|
||||
String[] terms = String_.Split(raw, ";");
|
||||
String gplxKey = "";
|
||||
for (String term : terms) {
|
||||
if (String_.Len(term) == 0) continue;
|
||||
String[] kv = String_.Split(term, "=");
|
||||
if (String_.Eq(kv[0], "gplx_key"))
|
||||
gplxKey = kv[1]; // NOTE: do not add to GfoMsg; will not be part of ApiStr
|
||||
else
|
||||
m.Add(kv[0], kv[1]);
|
||||
}
|
||||
Db_connect prototype = (Db_connect)regy.Fetch(gplxKey);
|
||||
return prototype.Clone_of_db_connect(raw, m);
|
||||
}
|
||||
catch(Exception exc) {throw Err_.parse_type_exc_(exc, Db_connect.class, raw);}
|
||||
}
|
||||
OrderedHash regy = OrderedHash_.new_();
|
||||
public static final Db_connect_pool _ = new Db_connect_pool();
|
||||
}
|
||||
49
140_dbs/src_100_core/gplx/dbs/Db_connect_base.java
Normal file
49
140_dbs/src_100_core/gplx/dbs/Db_connect_base.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
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 abstract class Db_connect_base implements Db_connect {
|
||||
public abstract String Key_of_db_connect();
|
||||
public String Database() {return database;} public Db_connect_base Database_(String v) {database = v; return this;} private String database = "";
|
||||
public String Server() {return server;} public Db_connect_base Server_(String v) {server = v; return this;} private String server = "";
|
||||
public String Raw_of_db_connect() {return raw;} public Db_connect XtoStr_raw_(String v) {raw = v; return this;} private String raw = "";
|
||||
public String Api_of_db_connect() {return api;} public Db_connect XtoStr_std_(String v) {api = v; return this;} private String api = "";
|
||||
public abstract Db_connect Clone_of_db_connect(String raw, GfoMsg m);
|
||||
protected void Ctor_of_db_connect(String server, String database, String raw, String api) {this.server = server; this.database = database; this.raw = raw; this.api = api;}
|
||||
protected static String BldApi(GfoMsg m, KeyVal... xtnAry) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
HashAdp hash = HashAdp_.new_();
|
||||
for (int i = 0; i < m.Args_count(); i++) {
|
||||
KeyVal kv = m.Args_getAt(i);
|
||||
sb.Add_fmt("{0}={1};", kv.Key(), kv.Val_to_str_or_empty());
|
||||
hash.AddKeyVal(kv.Key());
|
||||
}
|
||||
for (KeyVal xtn : xtnAry) {
|
||||
if (hash.Has(xtn.Key())) continue;
|
||||
sb.Add_fmt("{0}={1};", xtn.Key(), xtn.Val_to_str_or_empty());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
protected static String BldRaw(GfoMsg m) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < m.Args_count(); i++) {
|
||||
KeyVal itm = m.Args_getAt(i);
|
||||
sb.Add_fmt("{0}={1};", itm.Key(), itm.Val_to_str_or_empty());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
}
|
||||
45
140_dbs/src_100_core/gplx/dbs/Db_connect_sqlite.java
Normal file
45
140_dbs/src_100_core/gplx/dbs/Db_connect_sqlite.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
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_connect_sqlite extends Db_connect_base {
|
||||
@Override public String Key_of_db_connect() {return KeyDef;} public static final String KeyDef = "sqlite";
|
||||
public Io_url Url() {return url;} Io_url url;
|
||||
public static Db_connect load_(Io_url url) {
|
||||
return Db_connect_.parse_(BldRaw(GfoMsg_.new_cast_("Db_connect")
|
||||
.Add("gplx_key", KeyDef)
|
||||
.Add("data source", url.Xto_api())
|
||||
.Add("version", 3)
|
||||
));
|
||||
}
|
||||
public static Db_connect make_(Io_url url) {
|
||||
Io_mgr._.CreateDirIfAbsent(url.OwnerDir());
|
||||
return Db_connect_.parse_(BldRaw(GfoMsg_.new_cast_("Db_connect")
|
||||
.Add("gplx_key", KeyDef)
|
||||
.Add("data source", url.Xto_api())
|
||||
.Add("version", 3)
|
||||
));
|
||||
}
|
||||
@Override public Db_connect Clone_of_db_connect(String raw, GfoMsg m) {
|
||||
Db_connect_sqlite rv = new Db_connect_sqlite();
|
||||
String dataSourceUrl = m.ReadStr("data source");
|
||||
rv.url = Io_url_.new_any_(dataSourceUrl);
|
||||
rv.Ctor_of_db_connect("", dataSourceUrl, raw, BldApi(m, KeyVal_.new_("version", "3")));
|
||||
return rv;
|
||||
}
|
||||
public static final Db_connect_sqlite _ = new Db_connect_sqlite(); Db_connect_sqlite() {}
|
||||
}
|
||||
47
140_dbs/src_100_core/gplx/dbs/Db_connect_tst.java
Normal file
47
140_dbs/src_100_core/gplx/dbs/Db_connect_tst.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
public class Db_connect_tst {
|
||||
@Before public void setup() {
|
||||
regy = new Db_connect_pool();
|
||||
regy.Add(Db_connect_mock._);
|
||||
} Db_connect_pool regy;
|
||||
@Test public void Parse() {
|
||||
tst_Parse("gplx_key=mock;id=1;", kv_("id", "1")); // one; gplx_key removed
|
||||
tst_Parse("gplx_key=mock;id=1;name=me;", kv_("id", "1"), kv_("name", "me")); // many
|
||||
tst_Parse("gplx_key=mock;id=1;name=me" , kv_("id", "1"), kv_("name", "me")); // no semi-colon at end
|
||||
}
|
||||
KeyVal kv_(String key, Object val) {return KeyVal_.new_(key, val);}
|
||||
void tst_Parse(String raw, KeyVal... expd) {
|
||||
Db_connect_mock mock = (Db_connect_mock)regy.Parse(raw);
|
||||
Tfds.Eq_ary_str(expd, mock.Kvs());
|
||||
}
|
||||
}
|
||||
class Db_connect_mock extends Db_connect_base {
|
||||
public KeyVal[] Kvs() {return kvs;} KeyVal[] kvs;
|
||||
@Override public String Key_of_db_connect() {return KeyDef;} public static final String KeyDef = "mock";
|
||||
@Override public Db_connect Clone_of_db_connect(String raw, GfoMsg m) {
|
||||
Db_connect_mock rv = new Db_connect_mock();
|
||||
rv.kvs = new KeyVal[m.Args_count()];
|
||||
for (int i = 0; i < m.Args_count(); i++)
|
||||
rv.kvs[i] = m.Args_getAt(i);
|
||||
return rv;
|
||||
}
|
||||
public static final Db_connect_mock _ = new Db_connect_mock(); Db_connect_mock() {}
|
||||
}
|
||||
36
140_dbs/src_100_core/gplx/dbs/Db_provider.java
Normal file
36
140_dbs/src_100_core/gplx/dbs/Db_provider.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
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_provider implements RlsAble {
|
||||
@gplx.Internal protected Db_provider(Db_engine engine) {
|
||||
this.engine = engine;
|
||||
txn_mgr = new Db_txn_mgr_base(engine);
|
||||
}
|
||||
@gplx.Internal protected Db_engine Engine() {return engine;} Db_engine engine;
|
||||
public Db_connect ConnectInfo() {return engine.ConnectInfo();}
|
||||
public Db_stmt Prepare(Db_qry qry) {return engine.New_db_stmt(this, qry);}
|
||||
public int Exec_qry(Db_qry qry) {txn_mgr.Txn_count_(txn_mgr.Txn_count() + 1); return Int_.cast_(engine.Execute(qry));}
|
||||
public DataRdr Exec_qry_as_rdr(Db_qry qry) {return DataRdr_.cast_(engine.Execute(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));}
|
||||
public Db_txn_mgr Txn_mgr() {return txn_mgr;} Db_txn_mgr txn_mgr;
|
||||
public void Rls() {
|
||||
engine.Rls();
|
||||
Db_provider_pool._.Del(this.ConnectInfo()); // remove from pool, else rls'd instance will be cached and fail upon next use
|
||||
}
|
||||
}
|
||||
52
140_dbs/src_100_core/gplx/dbs/Db_provider_.java
Normal file
52
140_dbs/src_100_core/gplx/dbs/Db_provider_.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
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_provider_ {
|
||||
public static final Db_provider Null = new_(Db_connect_.Null);
|
||||
public static Db_provider new_(Db_connect dbInfo) {
|
||||
Db_engine prototype = Db_engineRegy._.Get(dbInfo.Key_of_db_connect());
|
||||
Db_engine engine = prototype.MakeEngine(dbInfo);
|
||||
engine.Connect();
|
||||
Db_provider rv = new Db_provider(engine);
|
||||
// Env_.Dispose_reg(rv); // NOTE: need to dispose or else mysql error: Unable to release semaphore
|
||||
return rv;
|
||||
}
|
||||
public static int Select_fld0_as_int_or(Db_provider p, String sql, int or) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = p.Exec_qry_as_rdr(Db_qry_sql.rdr_(sql));
|
||||
int rv = or;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
Object rv_obj = rdr.ReadAt(0);
|
||||
if (rv_obj != null) // Max(fil_id) will be NULL if tbl is empty
|
||||
rv = Int_.cast_or_(rv_obj, or);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
finally {
|
||||
rdr.Rls();
|
||||
}
|
||||
}
|
||||
}
|
||||
class Db_engineRegy {
|
||||
public Db_engine Get(String key) {return (Db_engine)hash.FetchOrFail(key);}
|
||||
HashAdp hash = HashAdp_.new_();
|
||||
Db_engineRegy Add(Db_engine engine) {hash.Add(engine.Key(), engine); return this;}
|
||||
Db_engineRegy() {this.Add(Db_engine_null._).Add(TdbEngine._).Add(Mysql_engine._).Add(Postgres_engine._).Add(Sqlite_engine._);}
|
||||
public static final Db_engineRegy _ = new Db_engineRegy();
|
||||
}
|
||||
34
140_dbs/src_100_core/gplx/dbs/Db_provider_mkr.java
Normal file
34
140_dbs/src_100_core/gplx/dbs/Db_provider_mkr.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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 interface Db_provider_mkr {
|
||||
Db_provider Load_or_make_(Io_url db_url, Bool_obj_ref created_ref);
|
||||
}
|
||||
class Db_provider_mkr_sqlite implements Db_provider_mkr {
|
||||
public Db_provider Load_or_make_(Io_url db_url, Bool_obj_ref created_ref) {return Sqlite_engine_.Provider_load_or_make_(db_url, created_ref);}
|
||||
public static final Db_provider_mkr_sqlite _ = new Db_provider_mkr_sqlite(); Db_provider_mkr_sqlite() {}
|
||||
}
|
||||
class Db_provider_mkr_mem implements Db_provider_mkr {
|
||||
private boolean create;
|
||||
public Db_provider Load_or_make_(Io_url db_url, Bool_obj_ref create_ref) {create_ref.Val_(create); return null;}
|
||||
public static Db_provider_mkr_mem create_(boolean create) {
|
||||
Db_provider_mkr_mem rv = new Db_provider_mkr_mem();
|
||||
rv.create = create;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
25
140_dbs/src_100_core/gplx/dbs/Db_provider_mkr_.java
Normal file
25
140_dbs/src_100_core/gplx/dbs/Db_provider_mkr_.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
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_provider_mkr_ {
|
||||
public static final Db_provider_mkr
|
||||
Sqlite = Db_provider_mkr_sqlite._
|
||||
, Mem_create_y = Db_provider_mkr_mem.create_(Bool_.Y)
|
||||
, Mem_create_n = Db_provider_mkr_mem.create_(Bool_.N)
|
||||
;
|
||||
}
|
||||
40
140_dbs/src_100_core/gplx/dbs/Db_provider_pool.java
Normal file
40
140_dbs/src_100_core/gplx/dbs/Db_provider_pool.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
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_provider_pool {
|
||||
public void Del(Db_connect connectInfo) {hash.Del(connectInfo.Raw_of_db_connect());}
|
||||
public Db_provider FetchOrNew(Db_connect connectInfo) {return FetchOrNew(connectInfo.Raw_of_db_connect());}
|
||||
public Db_provider FetchOrNew(String raw) {
|
||||
Db_provider rv = (Db_provider)hash.Fetch(raw);
|
||||
if (rv == null) {
|
||||
rv = Db_provider_.new_(Db_connect_.parse_(raw));
|
||||
hash.Add(raw, rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public void Clear() {
|
||||
for (int i = 0; i < hash.Count(); i++) {
|
||||
Db_provider provider = (Db_provider)hash.FetchAt(i);
|
||||
provider.Rls();
|
||||
}
|
||||
hash.Clear();
|
||||
}
|
||||
OrderedHash hash = OrderedHash_.new_();
|
||||
@gplx.Internal protected static Db_provider_pool new_() {return new Db_provider_pool();} Db_provider_pool() {}
|
||||
public static final Db_provider_pool _ = new Db_provider_pool();
|
||||
}
|
||||
23
140_dbs/src_110_dbQry/gplx/dbs/Db_qry.java
Normal file
23
140_dbs/src_110_dbQry/gplx/dbs/Db_qry.java
Normal file
@@ -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; import gplx.*;
|
||||
public interface Db_qry {
|
||||
String KeyOfDb_qry();
|
||||
boolean ExecRdrAble();
|
||||
String XtoSql();
|
||||
}
|
||||
59
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_.java
Normal file
59
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_ {
|
||||
public static Db_qry_select select_cols_(String tbl, Criteria crt, String... cols){return select_().From_(tbl).Where_(crt).Cols_(cols);}
|
||||
public static Db_qry_select select_val_(String tbl, String col, Criteria crt) {return select_().From_(tbl).Where_(crt).Cols_(col);}
|
||||
public static Db_qry_select select_tbl_(String tbl) {return select_().From_(tbl);}
|
||||
public static Db_qry_select select_() {return Db_qry_select.new_();}
|
||||
public static Db_qry_delete delete_(String tbl, Criteria crt) {return Db_qry_delete.new_().BaseTable_(tbl).Where_(crt);}
|
||||
public static Db_qry_delete delete_tbl_(String tbl) {return Db_qry_delete.new_().BaseTable_(tbl);}
|
||||
public static Db_qry_insert insert_(String tbl) {return Db_qry_insert.new_().BaseTable_(tbl);}
|
||||
public static Db_qry_insert insert_common_(String tbl, KeyVal... pairs) {
|
||||
Db_qry_insert cmd = Db_qry_insert.new_().BaseTable_(tbl);
|
||||
for (KeyVal pair : pairs)
|
||||
cmd.Arg_obj_(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public static Db_qry_update update_(String tbl, Criteria crt) {
|
||||
Db_qry_update update = Db_qry_update.new_();
|
||||
update.From_(tbl);
|
||||
update.Where_(crt);
|
||||
return update;
|
||||
}
|
||||
public static Db_qry_update update_common_(String tbl, Criteria crt, KeyVal... pairs) {
|
||||
Db_qry_update cmd = Db_qry_update.new_();
|
||||
cmd.From_(tbl); cmd.Where_(crt);
|
||||
for (KeyVal pair : pairs)
|
||||
cmd.Arg_obj_(pair.Key(), pair.Val());
|
||||
return cmd;
|
||||
}
|
||||
public static final Object WhereAll = null;
|
||||
public static Db_qry as_(Object obj) {return obj instanceof Db_qry ? (Db_qry)obj : null;}
|
||||
}
|
||||
interface Db_qryWkr {
|
||||
Object Exec(Db_engine engine, Db_qry cmd);
|
||||
}
|
||||
class Db_qryWkr_ {
|
||||
public static final Db_qryWkr Null = new Db_qryWrk_null();
|
||||
}
|
||||
class Db_qryWrk_null implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engine, Db_qry cmd) {return null;}
|
||||
}
|
||||
40
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_arg_owner.java
Normal file
40
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_arg_owner.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
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 interface Db_qry_arg_owner extends Db_qry {
|
||||
Db_qry_arg_owner From_(String tbl);
|
||||
|
||||
Db_qry_arg_owner Key_arg_(String k, int v);
|
||||
Db_qry_arg_owner Key_arg_(String k, String v);
|
||||
Db_qry_arg_owner Arg_(String k, int v);
|
||||
Db_qry_arg_owner Arg_(String k, long v);
|
||||
Db_qry_arg_owner Arg_(String k, String v);
|
||||
Db_qry_arg_owner Arg_(String k, byte[] v);
|
||||
Db_qry_arg_owner Arg_(String k, DateAdp v);
|
||||
Db_qry_arg_owner Arg_(String k, DecimalAdp v);
|
||||
Db_qry_arg_owner Arg_byte_(String k, byte v);
|
||||
Db_qry_arg_owner Arg_bry_(String k, byte[] v);
|
||||
Db_qry_arg_owner Arg_obj_(String key, Object val);
|
||||
Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid);
|
||||
}
|
||||
class Db_arg {
|
||||
public String Key() {return key;} private String key;
|
||||
public Object Val() {return val;} public Db_arg Val_(Object v) {val = v; return this;} Object val;
|
||||
public byte Val_tid() {return val_tid;} public Db_arg Val_tid_(byte v) {val_tid = v; return this;} private byte val_tid = Db_val_type.Tid_null;
|
||||
@gplx.Internal protected Db_arg(String key, Object val) {this.key = key; this.val = val;}
|
||||
}
|
||||
39
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_delete.java
Normal file
39
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_delete.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_delete implements Db_qry {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "DELETE";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
|
||||
public Db_qry_delete Where_add_(String key, int val) {
|
||||
Criteria crt = Db_crt_.eq_(key, val);
|
||||
where = Sql_where.merge_or_new_(where, crt);
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} public Db_qry_delete BaseTable_(String baseTable_) {baseTable = baseTable_; return this;} private String baseTable;
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_delete Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
|
||||
public static Db_qry_delete new_() {return new Db_qry_delete();} Db_qry_delete() {}
|
||||
public static Db_qry_delete new_all_(String tbl) {
|
||||
Db_qry_delete rv = new Db_qry_delete();
|
||||
rv.baseTable = tbl;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
44
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_dml_tst.java
Normal file
44
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_dml_tst.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_dml_tst {
|
||||
@Test public void Delete_basic() {
|
||||
tst_XtoSql(Db_qry_delete.new_().BaseTable_("tbl0").Where_(Db_crt_.eq_("fld0", "val0"))
|
||||
, "DELETE FROM tbl0 WHERE fld0='val0'");
|
||||
}
|
||||
@Test public void Insert_basic() {
|
||||
tst_XtoSql(Db_qry_insert.new_().BaseTable_("tbl0").Arg_("id", 0).Arg_("name", "me").Arg_("time", DateAdp_.parse_gplx("2007-12-23"))
|
||||
, "INSERT INTO tbl0 (id, name, time) VALUES (0, 'me', '2007-12-23 00:00:00.000')");
|
||||
}
|
||||
@Test public void Update_basic() {
|
||||
Db_qry_update qry = Db_qry_update.new_();
|
||||
qry.From_("tbl0");
|
||||
qry.Where_(Db_crt_.eq_("id", 0)).Arg_("name", "me");
|
||||
tst_XtoSql(qry, "UPDATE tbl0 SET name='me' WHERE id=0");
|
||||
}
|
||||
@Test public void Update_all() {
|
||||
Db_qry_update qry = Db_qry_update.new_();
|
||||
qry.From_("tbl0");
|
||||
qry.Arg_("id", 1).Arg_("name", "me").Arg_("startTime", DateAdp_.parse_gplx("2007-12-23"));
|
||||
qry.Where_(Criteria_.And(Db_crt_.eq_("id", 0), Db_crt_.mt_("startTime", DateAdp_.parse_gplx("2005-01-01"))));
|
||||
tst_XtoSql(qry, "UPDATE tbl0 SET id=1, name='me', startTime='2007-12-23 00:00:00.000' WHERE (id=0 AND startTime>'2005-01-01 00:00:00.000')");
|
||||
}
|
||||
void tst_XtoSql(Db_qry qry, String expd) {Tfds.Eq(expd, qry.XtoSql());}
|
||||
}
|
||||
34
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_flush.java
Normal file
34
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_flush.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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_qry_flush implements Db_qry {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "FLUSH";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
|
||||
@gplx.Internal protected String[] TableNames() {return tableNames;} private String[] tableNames;
|
||||
|
||||
public static Db_qry_flush as_(Object obj) {return obj instanceof Db_qry_flush ? (Db_qry_flush)obj : null;}
|
||||
public static Db_qry_flush cast_(Object obj) {try {return (Db_qry_flush)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, Db_qry_flush.class, obj);}}
|
||||
public static Db_qry_flush new_(String... ary) {
|
||||
Db_qry_flush rv = new Db_qry_flush();
|
||||
rv.tableNames = ary;
|
||||
return rv;
|
||||
} Db_qry_flush() {}
|
||||
}
|
||||
54
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_insert.java
Normal file
54
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_insert.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
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_qry_insert implements Db_qry_arg_owner {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "INSERT";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public Db_qry_arg_owner From_(String tbl) {baseTable = tbl; return this;}
|
||||
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.XtoDecimal(), Db_val_type.Tid_decimal);}
|
||||
public Db_qry_arg_owner Arg_(String k, DateAdp v) {return Arg_obj_type_(k, v, Db_val_type.Tid_date);}
|
||||
public Db_qry_arg_owner Arg_byte_(String k, byte v) {return Arg_obj_type_(k, v, Db_val_type.Tid_byte);}
|
||||
public Db_qry_arg_owner Arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_qry_arg_owner Arg_(String k, long v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int64);}
|
||||
public Db_qry_arg_owner Arg_(String k, String v) {return Arg_obj_type_(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_bry_(String k, byte[] v) {return Arg_obj_type_(k, v, Db_val_type.Tid_bry);}
|
||||
public Db_qry_arg_owner Arg_(String k, byte[] v) {return Arg_obj_type_(k, String_.new_utf8_(v), Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_obj_(String k, Object v) {return Arg_obj_type_(k, v, Db_val_type.Tid_null);}
|
||||
public Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid) {
|
||||
Db_arg arg = new Db_arg(key, val).Val_tid_(val_tid);
|
||||
args.Add(arg.Key(), arg);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_arg_owner Key_arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_qry_arg_owner Key_arg_(String k, String v) {return Arg_obj_type_(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_qry_select Select() {return select;} Db_qry_select select;
|
||||
public Db_qry_insert Select_(Db_qry_select qry) {this.select = qry; return this;}
|
||||
public Db_qry_insert Cols_(String... ary) {
|
||||
if (cols == null) cols = Sql_select_fld_list.new_();
|
||||
for (String fld : ary)
|
||||
cols.Add(Sql_select_fld_fld.new_(Sql_select_fld_base.Tbl_null, fld, fld));
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} public Db_qry_insert BaseTable_(String val) {baseTable = val; return this;} private String baseTable;
|
||||
@gplx.Internal protected KeyValHash Args() {return args;} KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected Sql_select_fld_list Cols() {return cols;} Sql_select_fld_list cols;
|
||||
|
||||
public static Db_qry_insert new_() {return new Db_qry_insert();} Db_qry_insert() {}
|
||||
}
|
||||
128
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_select.java
Normal file
128
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_select.java
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_select implements Db_qry {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "SELECT";
|
||||
public boolean ExecRdrAble() {return true;}
|
||||
public DataRdr Exec_qry_as_rdr(Db_provider provider) {return provider.Exec_qry_as_rdr(this);}
|
||||
public GfoNde ExecRdr_nde(Db_provider provider) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {return GfoNde_.rdr_(Exec_qry_as_rdr(provider));} finally {rdr.Rls();}
|
||||
}
|
||||
public Object ExecRdr_val(Db_provider provider) {
|
||||
DataRdr rdr = Exec_qry_as_rdr(provider);
|
||||
try {
|
||||
Object rv = null;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
rv = rdr.Read(cols.Flds().FetchAt(0).Fld()); // NOTE: need to access from flds for tdb
|
||||
}
|
||||
return rv;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
public static Object Rdr_to_val(DataRdr rdr) {
|
||||
try {
|
||||
Object rv = null;
|
||||
if (rdr.MoveNextPeer()) {
|
||||
rv = rdr.ReadAt(0);
|
||||
}
|
||||
return rv;
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
|
||||
@gplx.Internal protected Sql_from From() {return from;} Sql_from from;
|
||||
public Db_qry_select From_(String tblName) {return From_(tblName, null);}
|
||||
public Db_qry_select From_(String tblName, String alias) {
|
||||
if (from != null) throw Err_.new_("super table already defined").Add("from", from.Tbls().Count());
|
||||
from = Sql_from.new_(Sql_tbl_src.new_().JoinType_(Sql_join_itmType.From).TblName_(tblName).Alias_(alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Join_(String name, String alias, Sql_join_itm... ary) {
|
||||
if (from == null) throw Err_.new_("super table is not defined");
|
||||
Sql_tbl_src tbl = Sql_tbl_src.new_().JoinType_(Sql_join_itmType.Inner).TblName_(name).Alias_(alias);
|
||||
for (Sql_join_itm itm : ary)
|
||||
tbl.JoinLinks().Add(itm);
|
||||
from.Tbls().Add(tbl);
|
||||
return this;
|
||||
}
|
||||
|
||||
@gplx.Internal protected Sql_select Cols() {return cols;} Sql_select cols = Sql_select.All;
|
||||
public Db_qry_select Cols_all_() {return this;}
|
||||
public Db_qry_select Cols_alias_(String expr, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(expr, alias);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_(String... ary) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
for (String itm : ary)
|
||||
cols.Add(itm);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_max(String fld) {return Cols_groupBy_max(fld, fld);}
|
||||
public Db_qry_select Cols_groupBy_max(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_minMax.max_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_min(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_minMax.min_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_count(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_count.new_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Cols_groupBy_sum(String fld) {return Cols_groupBy_sum(fld, fld);}
|
||||
public Db_qry_select Cols_groupBy_sum(String fld, String alias) {
|
||||
if (cols == Sql_select.All) cols = Sql_select.new_();
|
||||
cols.Add(Sql_select_fld_sum.new_(Sql_select_fld_base.Tbl_null, fld, alias));
|
||||
return this;
|
||||
}
|
||||
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_select Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
|
||||
@gplx.Internal protected Sql_order_by OrderBy() {return orderBy;} Sql_order_by orderBy = null;
|
||||
public Db_qry_select OrderBy_(String fieldName, boolean ascending) {
|
||||
Sql_order_by_itm item = Sql_order_by_itm.new_(fieldName, ascending);
|
||||
orderBy = Sql_order_by.new_(item);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select OrderBy_asc_(String fieldName) {return OrderBy_(fieldName, true);}
|
||||
public Db_qry_select OrderBy_many_(String... fldNames) {
|
||||
Sql_order_by_itm[] ary = new Sql_order_by_itm[fldNames.length];
|
||||
for (int i = 0; i < fldNames.length; i++)
|
||||
ary[i] = Sql_order_by_itm.new_(fldNames[i], true);
|
||||
orderBy = Sql_order_by.new_(ary);
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected Sql_group_by GroupBy() {return groupBy;} Sql_group_by groupBy = null;
|
||||
public Db_qry_select GroupBy_(String... flds) {
|
||||
if (groupBy != null) throw Err_.new_("group by already defined").Add("group", groupBy);
|
||||
groupBy = Sql_group_by.new_(flds);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_select Distinct_() {cols.Distinct_set(true); return this;}
|
||||
@gplx.Internal protected int Limit() {return limit;} int limit = -1; public static final int Limit_disabled = -1;
|
||||
public Db_qry_select Limit_(int v) {this.limit = v; return this;}
|
||||
|
||||
public static Db_qry_select new_() {return new Db_qry_select();} Db_qry_select() {}
|
||||
}
|
||||
89
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_select_tst.java
Normal file
89
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_select_tst.java
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
public class Db_qry_select_tst {
|
||||
@Before public void setup() {
|
||||
cmd = Db_qry_select.new_();
|
||||
} Db_qry_select cmd; String expd;
|
||||
@Test public void Basic() {
|
||||
cmd.Cols_("fld0", "fld1").From_("tbl0");
|
||||
expd = "SELECT fld0, fld1 FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderDoesNotMatter() {
|
||||
cmd.From_("tbl0").Cols_("fld0", "fld1");
|
||||
expd = "SELECT fld0, fld1 FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void DefaultAllFields() {
|
||||
cmd.From_("tbl0");
|
||||
expd = "SELECT * FROM tbl0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Where() {
|
||||
cmd.From_("tbl0").Where_(Db_crt_.eq_("fld0", 0));
|
||||
expd = "SELECT * FROM tbl0 WHERE fld0=0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Join() {
|
||||
cmd.From_("tbl0").Join_("tbl1", "t1", Sql_join_itm.new_("fld1", "tbl0", "fld0"));
|
||||
expd = "SELECT * FROM tbl0 INNER JOIN tbl1 t1 ON tbl0.fld0=t1.fld1";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderBy() {
|
||||
cmd.From_("tbl0").OrderBy_("fld0", true);
|
||||
expd = "SELECT * FROM tbl0 ORDER BY fld0";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void OrderByMany() {
|
||||
cmd.From_("tbl0").OrderBy_many_("fld0", "fld1");
|
||||
expd = "SELECT * FROM tbl0 ORDER BY fld0, fld1";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
@Test public void Limit() {
|
||||
cmd.From_("tbl0").Limit_(10);
|
||||
expd = "SELECT * FROM tbl0 LIMIT 10";
|
||||
|
||||
tst_XtoStr(cmd, expd);
|
||||
}
|
||||
// @Test public void GroupBy() {
|
||||
// cmd.From_("tbl0").groupBy_("fld0", "fld1");
|
||||
// expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1";
|
||||
// Tfds.Eq(cmd.XtoStr(), expd);
|
||||
// }
|
||||
// @Test public void Union() {
|
||||
// cmd.From_("tbl0").select("fld0").union_(qry2.from("tbl1").select("fld0"));
|
||||
// cmd.From_("tbl0").select("fld0").union_().from("tbl1").select("fld0"); // feasible, but will be bad later when trying to access Db_qry_select props
|
||||
// expd = "SELECT fld0 FROM tbl0 UNION SELECT fld0 FROM tbl1";
|
||||
// Tfds.Eq(cmd.XtoStr(), expd);
|
||||
// }
|
||||
// @Test public void Having() {
|
||||
// cmd.From_("tbl0").groupBy_("fld0", "fld1");
|
||||
// expd = "SELECT fld0, fld1 FROM tbl0 GROUP BY fld0, fld1 HAVING Count(fld0) > 1";
|
||||
// Tfds.Eq(cmd.XtoStr(), expd);
|
||||
// }
|
||||
void tst_XtoStr(Db_qry qry, String expd) {Tfds.Eq(expd, cmd.XtoSql());}
|
||||
}
|
||||
39
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_sql.java
Normal file
39
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_sql.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
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_qry_sql implements Db_qry {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "SQL";
|
||||
public boolean ExecRdrAble() {return isReader;} private boolean isReader;
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return sql;} private String sql;
|
||||
public static Db_qry_sql dml_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql ddl_(String sql) {return sql_(sql);}
|
||||
public static Db_qry_sql xtn_(String sql) {return sql_(sql);}
|
||||
static Db_qry_sql sql_(String sql) {
|
||||
Db_qry_sql rv = new Db_qry_sql();
|
||||
rv.sql = sql; rv.isReader = false;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_sql rdr_(String sql) {
|
||||
Db_qry_sql rv = new Db_qry_sql();
|
||||
rv.sql = sql; rv.isReader = true;
|
||||
return rv;
|
||||
}
|
||||
public static Db_qry_sql as_(Object obj) {return obj instanceof Db_qry_sql ? (Db_qry_sql)obj : null;}
|
||||
public static Db_qry_sql cast_(Object obj) {try {return (Db_qry_sql)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, Db_qry_sql.class, obj);}}
|
||||
}
|
||||
53
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_update.java
Normal file
53
140_dbs/src_110_dbQry/gplx/dbs/Db_qry_update.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_qry_update implements Db_qry_arg_owner {
|
||||
public String KeyOfDb_qry() {return KeyConst;} public static final String KeyConst = "UPDATE";
|
||||
public boolean ExecRdrAble() {return false;}
|
||||
public int Exec_qry(Db_provider provider) {return provider.Exec_qry(this);}
|
||||
public String XtoSql() {return Sql_cmd_wtr_.Ansi.XtoSqlQry(this, false);}
|
||||
|
||||
public Db_qry_arg_owner From_(String tbl) {baseTable = tbl; return this;}
|
||||
public Db_qry_arg_owner Arg_(String k, DecimalAdp v) {return Arg_obj_type_(k, v.XtoDecimal(), Db_val_type.Tid_decimal);}
|
||||
public Db_qry_arg_owner Arg_(String k, DateAdp v) {return Arg_obj_type_(k, v, Db_val_type.Tid_date);}
|
||||
public Db_qry_arg_owner Arg_byte_(String k, byte v) {return Arg_obj_type_(k, v, Db_val_type.Tid_byte);}
|
||||
public Db_qry_arg_owner Arg_(String k, int v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int32);}
|
||||
public Db_qry_arg_owner Arg_(String k, long v) {return Arg_obj_type_(k, v, Db_val_type.Tid_int64);}
|
||||
public Db_qry_arg_owner Arg_(String k, String v) {return Arg_obj_type_(k, v, Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_bry_(String k, byte[] v) {return Arg_obj_type_(k, v, Db_val_type.Tid_bry);}
|
||||
public Db_qry_arg_owner Arg_(String k, byte[] v) {return Arg_obj_type_(k, String_.new_utf8_(v), Db_val_type.Tid_varchar);}
|
||||
public Db_qry_arg_owner Arg_obj_(String k, Object v) {return Arg_obj_type_(k, v, Db_val_type.Tid_null);}
|
||||
public Db_qry_arg_owner Arg_obj_type_(String key, Object val, byte val_tid) {
|
||||
Db_arg arg = new Db_arg(key, val).Val_tid_(val_tid);
|
||||
args.Add(arg.Key(), arg);
|
||||
return this;
|
||||
}
|
||||
public Db_qry_arg_owner Key_arg_(String k, int v) {return Key_arg_obj_(k, v);}
|
||||
public Db_qry_arg_owner Key_arg_(String k, String v) {return Key_arg_obj_(k, v);}
|
||||
Db_qry_arg_owner Key_arg_obj_(String k, Object v) {
|
||||
Criteria crt = Db_crt_.eq_(k, v);
|
||||
where = Sql_where.merge_or_new_(where, crt);
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected String BaseTable() {return baseTable;} private String baseTable;
|
||||
@gplx.Internal protected KeyValHash Args() {return args;} final KeyValHash args = KeyValHash.new_();
|
||||
@gplx.Internal protected Sql_where Where() {return where;} public Db_qry_update Where_(Criteria crt) {where = Sql_where.new_(crt); return this;} Sql_where where;
|
||||
|
||||
public static Db_qry_update new_() {return new Db_qry_update();} Db_qry_update() {}
|
||||
}
|
||||
41
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt.java
Normal file
41
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
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 interface Db_stmt extends RlsAble {
|
||||
Db_provider Provider();
|
||||
Db_stmt Val_bool_(boolean v);
|
||||
Db_stmt Val_byte_(byte v);
|
||||
Db_stmt Val_byte_by_bool_(boolean v);
|
||||
Db_stmt Val_int_(int v);
|
||||
Db_stmt Val_long_(long v);
|
||||
Db_stmt Val_float_(float v);
|
||||
Db_stmt Val_double_(double v);
|
||||
Db_stmt Val_decimal_(DecimalAdp v);
|
||||
Db_stmt Val_bry_(byte[] v);
|
||||
Db_stmt Val_bry_by_str_(String v);
|
||||
Db_stmt Val_str_by_bry_(byte[] v);
|
||||
Db_stmt Val_str_(String v);
|
||||
Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr rdr, long rdr_len);
|
||||
boolean Exec_insert();
|
||||
int Exec_update();
|
||||
int Exec_delete();
|
||||
DataRdr Exec_select();
|
||||
Object Exec_select_val();
|
||||
Db_stmt Clear();
|
||||
Db_stmt New();
|
||||
}
|
||||
50
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_.java
Normal file
50
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
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_stmt_ {
|
||||
public static final Db_stmt Null = new Db_stmt_sql();
|
||||
public static Db_stmt new_insert_(Db_provider provider, String tbl, String... flds) {
|
||||
Db_qry_insert qry = Db_qry_insert.new_().BaseTable_(tbl);
|
||||
int len = flds.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
qry.Arg_obj_(flds[i], null);
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_update_(Db_provider provider, String tbl, String[] where, String... flds) {
|
||||
Db_qry_update qry = Db_qry_.update_(tbl, Db_crt_.eq_many_(where));
|
||||
int len = flds.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
qry.Arg_obj_(flds[i], null);
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_delete_(Db_provider provider, String tbl, String... where) {
|
||||
Db_qry_delete qry = Db_qry_.delete_(tbl, Db_crt_.eq_many_(where));
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_select_(Db_provider provider, String tbl, String[] where, String... flds) {
|
||||
Db_qry_select qry = Db_qry_.select_cols_(tbl, Db_crt_.eq_many_(where), flds);
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_select_in_(Db_provider provider, String tbl, String in_fld, Object[] in_vals, String... flds) {
|
||||
Db_qry_select qry = Db_qry_.select_cols_(tbl, Db_crt_.in_(in_fld, in_vals), flds).OrderBy_asc_(in_fld);
|
||||
return provider.Prepare(qry);
|
||||
}
|
||||
public static Db_stmt new_select_all_(Db_provider provider, String tbl) {
|
||||
return provider.Prepare(Db_qry_.select_tbl_(tbl));
|
||||
}
|
||||
}
|
||||
54
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_bldr.java
Normal file
54
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_bldr.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
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_stmt_bldr implements RlsAble {
|
||||
private Db_provider provider;
|
||||
private Db_stmt create, update, delete;
|
||||
private String tbl_name; private String[] flds_keys, flds_vals, flds_all;
|
||||
public Db_stmt_bldr(String tbl_name, String[] flds_keys, String... flds_vals) {
|
||||
this.tbl_name = tbl_name; this.flds_keys = flds_keys; this.flds_vals = flds_vals;
|
||||
flds_all = String_.Ary_add(flds_keys, flds_vals);
|
||||
}
|
||||
public Db_stmt_bldr Init(Db_provider v) {
|
||||
this.provider = v;
|
||||
provider.Txn_mgr().Txn_bgn_if_none();
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Get(byte cmd_mode) {
|
||||
switch (cmd_mode) {
|
||||
case Db_cmd_mode.Create: if (create == null) create = Db_stmt_.new_insert_(provider, tbl_name, flds_all); return create;
|
||||
case Db_cmd_mode.Update: if (update == null) update = Db_stmt_.new_update_(provider, tbl_name, flds_keys, flds_vals); return update;
|
||||
case Db_cmd_mode.Delete: if (delete == null) delete = Db_stmt_.new_delete_(provider, tbl_name, flds_keys); return delete;
|
||||
case Db_cmd_mode.Ignore: return Db_stmt_.Null;
|
||||
default: throw Err_.unhandled(cmd_mode);
|
||||
}
|
||||
}
|
||||
public void Commit() {
|
||||
provider.Txn_mgr().Txn_end_all();
|
||||
}
|
||||
public void Rls() {
|
||||
provider = null;
|
||||
create = Rls(create);
|
||||
update = Rls(update);
|
||||
delete = Rls(delete);
|
||||
}
|
||||
private Db_stmt Rls(Db_stmt stmt) {
|
||||
if (stmt != null) stmt.Rls();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
104
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_cmd.java
Normal file
104
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_cmd.java
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
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.*;
|
||||
import java.sql.*;
|
||||
class Db_stmt_cmd implements Db_stmt {
|
||||
Db_engine engine;
|
||||
PreparedStatement stmt = null;
|
||||
String sql;
|
||||
int val_idx = 0;
|
||||
public Db_stmt_cmd(Db_provider provider, Db_qry qry) {
|
||||
this.provider = provider; this.engine = provider.Engine();
|
||||
sql = Sql_cmd_wtr_.Ansi.XtoSqlQry(qry, true);
|
||||
New();
|
||||
}
|
||||
public Db_stmt New() {
|
||||
stmt = (PreparedStatement)engine.New_db_cmd(sql);
|
||||
return this;
|
||||
}
|
||||
public Db_provider Provider() {return provider;} Db_provider provider;
|
||||
public Db_stmt Val_bool_(boolean v) {
|
||||
try {stmt.setBoolean(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_byte_by_bool_(boolean v) {return Val_byte_(v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Val_byte_(byte v) {
|
||||
try {stmt.setByte(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_int_(int v) {
|
||||
try {stmt.setInt(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_long_(long v) {
|
||||
try {stmt.setLong(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_float_(float v) {
|
||||
try {stmt.setFloat(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_double_(double v) {
|
||||
try {stmt.setDouble(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_decimal_(DecimalAdp v) {
|
||||
try {stmt.setBigDecimal(++val_idx, v.XtoDecimal());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_bry_by_str_(String v) {return Val_bry_(Bry_.new_utf8_(v));}
|
||||
public Db_stmt Val_bry_(byte[] v) {
|
||||
try {stmt.setBytes(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_str_by_bry_(byte[] v) {return Val_str_(String_.new_utf8_(v));}
|
||||
public Db_stmt Val_str_(String v) {
|
||||
try {stmt.setString(++val_idx, v);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {stmt.setBinaryStream(++val_idx, (java.io.InputStream)v.Under(), (int)rdr_len);} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = stmt.execute(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0} err={1}", sql, Err_.Message_gplx_brief(e));}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = stmt.executeUpdate(); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = engine.NewDataRdr(stmt.executeQuery(), sql); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(engine.NewDataRdr(stmt.executeQuery(), sql)); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
try {stmt.clearBatch();}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to clear parameters;", sql);}
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
if (stmt == null) return; // Null instance
|
||||
try {stmt.close();}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to close command: {0}", sql);}
|
||||
}
|
||||
}
|
||||
124
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_sql.java
Normal file
124
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_sql.java
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
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.*;
|
||||
class Db_stmt_sql implements Db_stmt {
|
||||
Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
Bry_fmtr tmp_fmtr = Bry_fmtr.new_();
|
||||
int val_idx = 0;
|
||||
public Db_provider Provider() {return provider;} public void Provider_(Db_provider v) {this.provider = v;} Db_provider provider;
|
||||
public Db_stmt New() {return this;}
|
||||
public Db_stmt Val_bool_(boolean v) {
|
||||
try {Add(++val_idx, v ? "true" : "false");} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_byte_by_bool_(boolean v) {return Val_byte_(v ? Bool_.Y_byte : Bool_.N_byte);}
|
||||
public Db_stmt Val_byte_(byte v) {
|
||||
try {Add(++val_idx, Byte_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_int_(int v) {
|
||||
try {Add(++val_idx, Int_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "int", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_long_(long v) {
|
||||
try {Add(++val_idx, Long_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "long", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_float_(float v) {
|
||||
try {Add(++val_idx, Float_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "float", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_double_(double v) {
|
||||
try {Add(++val_idx, Double_.XtoStr(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "double", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_decimal_(DecimalAdp v) {
|
||||
try {Add(++val_idx, v.XtoStr());} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "decimal", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_bry_by_str_(String v) {return Val_bry_(Bry_.new_utf8_(v));}
|
||||
public Db_stmt Val_bry_(byte[] v) { // HACK: convert to String b/c tdb does not support byte[]
|
||||
try {Add(++val_idx, Val_str_wrap(String_.new_utf8_(v)));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "byte[]", v.length);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_str_by_bry_(byte[] v) {return Val_str_(String_.new_utf8_(v));}
|
||||
public Db_stmt Val_str_(String v) {
|
||||
try {Add(++val_idx, Val_str_wrap(v));} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "String", v);}
|
||||
return this;
|
||||
}
|
||||
public Db_stmt Val_rdr_(gplx.ios.Io_stream_rdr v, long rdr_len) {
|
||||
try {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
gplx.ios.Io_stream_rdr_.Load_all_to_bfr(bfr, v);
|
||||
Add(++val_idx, bfr.XtoStrAndClear());
|
||||
} catch (Exception e) {throw Err_.err_(e, "failed to add value: type={0} val={1}", "rdr", v);}
|
||||
return this;
|
||||
}
|
||||
String Val_str_wrap(String v) {
|
||||
return "'" + String_.Replace(v, "'", "\\'") + "'";
|
||||
}
|
||||
public boolean Exec_insert() {
|
||||
try {boolean rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())) != 0; return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public int Exec_update() {
|
||||
try {int rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public int Exec_delete() {
|
||||
try {int rv = provider.Exec_qry(Db_qry_sql.dml_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public DataRdr Exec_select() {
|
||||
try {DataRdr rv = provider.Exec_qry_as_rdr(Db_qry_sql.rdr_(this.Xto_sql())); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Object Exec_select_val() {
|
||||
try {Object rv = Db_qry_select.Rdr_to_val(this.Exec_select()); return rv;} catch (Exception e) {throw Err_.err_(e, "failed to exec prepared statement: sql={0}", sql_orig);}
|
||||
}
|
||||
public Db_stmt Clear() {
|
||||
val_idx = 0;
|
||||
args.Clear();
|
||||
return this;
|
||||
}
|
||||
public void Rls() {
|
||||
this.Clear();
|
||||
}
|
||||
public void Add(String v) {Add(-1, v);}
|
||||
public void Add(int idx, String v) {args.Add(v);} ListAdp args = ListAdp_.new_();
|
||||
public String Xto_sql() {
|
||||
tmp_fmtr.Bld_bfr_many(tmp_bfr, (Object[])args.XtoAryAndClear(Object.class));
|
||||
return tmp_bfr.XtoStrAndClear();
|
||||
}
|
||||
String sql_orig;
|
||||
public Db_stmt Parse(String sql_str) {
|
||||
this.sql_orig = sql_str;
|
||||
int arg_idx = 0;
|
||||
byte[] src = Bry_.new_utf8_(sql_str);
|
||||
int pos_prv = 0;
|
||||
tmp_bfr.Clear();
|
||||
while (true) {
|
||||
int pos_cur = Bry_finder.Find_fwd(src, Byte_ascii.Question, pos_prv);
|
||||
if (pos_cur == Bry_.NotFound) break;
|
||||
tmp_bfr.Add_mid(src, pos_prv, pos_cur);
|
||||
tmp_bfr.Add_byte(Byte_ascii.Tilde).Add_byte(Byte_ascii.Curly_bgn);
|
||||
tmp_bfr.Add_int_variable(arg_idx++);
|
||||
tmp_bfr.Add_byte(Byte_ascii.Curly_end);
|
||||
pos_prv = pos_cur + 1;
|
||||
}
|
||||
tmp_bfr.Add_mid(src, pos_prv, src.length);
|
||||
tmp_fmtr.Fmt_(tmp_bfr.XtoAryAndClear());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
29
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_sql_tst.java
Normal file
29
140_dbs/src_110_dbQry/gplx/dbs/Db_stmt_sql_tst.java
Normal file
@@ -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; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_stmt_sql_tst {
|
||||
@Before public void init() {}
|
||||
@Test public void Basic() {
|
||||
Db_stmt_sql stmt = new Db_stmt_sql();
|
||||
stmt.Parse("UPDATE tbl_0 SET col_0 = ? WHERE col_1 = ?");
|
||||
stmt.Add("1");
|
||||
stmt.Add("2");
|
||||
Tfds.Eq("UPDATE tbl_0 SET col_0 = 1 WHERE col_1 = 2", stmt.Xto_sql());
|
||||
}
|
||||
}
|
||||
48
140_dbs/src_110_dbQry/gplx/dbs/Db_txn_mgr.java
Normal file
48
140_dbs/src_110_dbQry/gplx/dbs/Db_txn_mgr.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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 interface Db_txn_mgr {
|
||||
int Txn_depth();
|
||||
void Txn_bgn_if_none();
|
||||
void Txn_bgn();
|
||||
void Txn_end();
|
||||
void Txn_end_all();
|
||||
void Txn_end_all_bgn_if_none();
|
||||
int Txn_count(); void Txn_count_(int v);
|
||||
}
|
||||
class Db_txn_mgr_base implements Db_txn_mgr {
|
||||
public Db_txn_mgr_base(Db_engine engine) {this.engine = engine;} Db_engine engine;
|
||||
public int Txn_depth() {return txn_depth;} int txn_depth; // NOTE: only support 1 level for now;
|
||||
public void Txn_bgn_if_none() {if (txn_depth == 0) this.Txn_bgn();}
|
||||
public void Txn_bgn() {
|
||||
engine.Txn_bgn();
|
||||
++txn_depth;
|
||||
}
|
||||
public void Txn_end_all() {this.Txn_end();}
|
||||
public void Txn_end() {
|
||||
if (txn_depth == 0) return;
|
||||
engine.Txn_end();
|
||||
--txn_depth;
|
||||
txn_count = 0;
|
||||
}
|
||||
public void Txn_end_all_bgn_if_none() {
|
||||
this.Txn_end_all();
|
||||
this.Txn_bgn_if_none();
|
||||
}
|
||||
public int Txn_count() {return txn_count;} public void Txn_count_(int v) {txn_count = v;} int txn_count;
|
||||
}
|
||||
35
140_dbs/src_110_dbQry/gplx/dbs/Db_val_type.java
Normal file
35
140_dbs/src_110_dbQry/gplx/dbs/Db_val_type.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
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_val_type {
|
||||
public static final byte // not serialized
|
||||
Tid_null = 0
|
||||
, Tid_bool = 1
|
||||
, Tid_byte = 2
|
||||
, Tid_int32 = 3
|
||||
, Tid_int64 = 4
|
||||
, Tid_date = 5
|
||||
, Tid_decimal = 6
|
||||
, Tid_float = 7
|
||||
, Tid_double = 8
|
||||
, Tid_bry = 9
|
||||
, Tid_varchar = 10
|
||||
, Tid_nvarchar = 11
|
||||
, Tid_rdr = 12
|
||||
;
|
||||
}
|
||||
23
140_dbs/src_120_sql/gplx/dbs/Db_fld.java
Normal file
23
140_dbs/src_120_sql/gplx/dbs/Db_fld.java
Normal file
@@ -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; import gplx.*;
|
||||
public class Db_fld {
|
||||
public Db_fld(String name, byte type_tid) {this.name = name; this.type_tid = type_tid;}
|
||||
public String Name() {return name;} public Db_fld Name_(String v) {name = v; return this;} private String name;
|
||||
public byte Type_tid() {return type_tid;} public Db_fld Type_tid_(byte v) {type_tid = v; return this;} private byte type_tid;
|
||||
}
|
||||
34
140_dbs/src_120_sql/gplx/dbs/Db_obj_ary_crt.java
Normal file
34
140_dbs/src_120_sql/gplx/dbs/Db_obj_ary_crt.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_obj_ary_crt implements gplx.criterias.Criteria {
|
||||
public byte Crt_tid() {return Criteria_.Tid_db_obj_ary;}
|
||||
public Db_fld[] Flds() {return flds;} public Db_obj_ary_crt Flds_(Db_fld[] v) {this.flds = v; return this;} private Db_fld[] flds;
|
||||
public Object[][] Vals() {return vals;} public void Vals_(Object[][] v) {this.vals = v;} private Object[][] vals;
|
||||
public boolean Matches(Object obj) {return false;}
|
||||
public String XtoStr() {return "";}
|
||||
public static Db_obj_ary_crt new_(Db_fld... flds) {return new Db_obj_ary_crt().Flds_(flds);}
|
||||
public static Db_obj_ary_crt new_by_type(byte type_tid, String... names) {
|
||||
int len = names.length;
|
||||
Db_fld[] flds = new Db_fld[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
flds[i] = new Db_fld(names[i], type_tid);
|
||||
return new Db_obj_ary_crt().Flds_(flds);
|
||||
}
|
||||
}
|
||||
42
140_dbs/src_120_sql/gplx/dbs/Db_obj_ary_tst.java
Normal file
42
140_dbs/src_120_sql/gplx/dbs/Db_obj_ary_tst.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
public class Db_obj_ary_tst {
|
||||
@Before public void init() {} private Db_obj_ary_fxt fxt = new Db_obj_ary_fxt();
|
||||
@Test public void Int() {
|
||||
fxt.Init_fld("fld_0", ClassAdp_.Tid_int).Init_fld("fld_1", ClassAdp_.Tid_int).Init_vals(1, 10).Init_vals(2, 20).Test_sql("(fld_0=1 AND fld_1=10) OR (fld_0=2 AND fld_1=20)");
|
||||
}
|
||||
@Test public void Str() {
|
||||
fxt.Init_fld("fld_0", ClassAdp_.Tid_int).Init_fld("fld_1", ClassAdp_.Tid_str).Init_vals(1, "a").Init_vals(2, "b").Test_sql("(fld_0=1 AND fld_1='a') OR (fld_0=2 AND fld_1='b')");
|
||||
}
|
||||
}
|
||||
class Db_obj_ary_fxt {
|
||||
private Db_obj_ary_crt crt = new Db_obj_ary_crt();
|
||||
public Db_obj_ary_fxt Init_fld(String name, byte tid) {flds_list.Add(new Db_fld(name, tid)); return this;} private ListAdp flds_list = ListAdp_.new_();
|
||||
public Db_obj_ary_fxt Init_vals(Object... ary) {vals_list.Add(ary); return this;} private ListAdp vals_list = ListAdp_.new_();
|
||||
public Db_obj_ary_fxt Test_sql(String expd) {
|
||||
Sql_cmd_wtr_ansi cmd_wtr = (Sql_cmd_wtr_ansi)Sql_cmd_wtr_ansi_.default_();
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
crt.Flds_((Db_fld[])flds_list.XtoAryAndClear(Db_fld.class));
|
||||
crt.Vals_((Object[][])vals_list.XtoAryAndClear(Object[].class));
|
||||
cmd_wtr.Append_db_obj_ary(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStrAndClear());
|
||||
return this;
|
||||
}
|
||||
}
|
||||
32
140_dbs/src_120_sql/gplx/dbs/Sql_cmd_wtr.java
Normal file
32
140_dbs/src_120_sql/gplx/dbs/Sql_cmd_wtr.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.*;
|
||||
import gplx.criterias.*;
|
||||
interface Sql_cmd_wtr {
|
||||
String XtoSqlQry(Db_qry qry, boolean prepare);
|
||||
String XtoSql_insert(Db_qry_insert qry);
|
||||
String XtoSql_delete(Db_qry_delete qry);
|
||||
String XtoSql_update(Db_qry_update qry);
|
||||
String XtoSql_select(Db_qry_select qry);
|
||||
void BldWhere(String_bldr sb, Criteria crt);
|
||||
void BldValStr(String_bldr sb, Db_arg prm);
|
||||
}
|
||||
class Sql_cmd_wtr_ {
|
||||
public static final Sql_cmd_wtr Ansi = Sql_cmd_wtr_ansi_.default_();
|
||||
public static final Sql_cmd_wtr BackslashSensitive = Sql_cmd_wtr_ansi_.backslash_sensitive_();
|
||||
}
|
||||
288
140_dbs/src_120_sql/gplx/dbs/Sql_cmd_wtr_ansi.java
Normal file
288
140_dbs/src_120_sql/gplx/dbs/Sql_cmd_wtr_ansi.java
Normal file
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*; /*Criteria_bool_base*/
|
||||
class Sql_cmd_wtr_ansi implements Sql_cmd_wtr {
|
||||
boolean prepare = false;
|
||||
public String XtoSqlQry(Db_qry cmd, boolean prepare) {
|
||||
String key = cmd.KeyOfDb_qry(); this.prepare = prepare;
|
||||
if (String_.Eq(key, Db_qry_insert.KeyConst)) return XtoSql_insert((Db_qry_insert)cmd);
|
||||
else if (String_.Eq(key, Db_qry_delete.KeyConst)) return XtoSql_delete((Db_qry_delete)cmd);
|
||||
else if (String_.Eq(key, Db_qry_update.KeyConst)) return XtoSql_update((Db_qry_update)cmd);
|
||||
else if (String_.Eq(key, Db_qry_select.KeyConst)) return XtoSql_select((Db_qry_select)cmd);
|
||||
else if (String_.Eq(key, Db_qry_sql.KeyConst)) return ((Db_qry_sql)cmd).XtoSql();
|
||||
else throw Err_.unhandled(cmd.KeyOfDb_qry());
|
||||
}
|
||||
public String XtoSql_delete(Db_qry_delete cmd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add_many("DELETE FROM ", cmd.BaseTable());
|
||||
BldWhere(sb, cmd.Where());
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public String XtoSql_insert(Db_qry_insert cmd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
if (cmd.Select() != null) {
|
||||
sb.Add_many("INSERT INTO ", cmd.BaseTable(), " (");
|
||||
for (int i = 0; i < cmd.Cols().Count(); i++) {
|
||||
Sql_select_fld_base fld = cmd.Cols().FetchAt(i);
|
||||
sb.Add(fld.Alias());
|
||||
sb.Add(i == cmd.Cols().Count() - 1 ? ") " : ", ");
|
||||
}
|
||||
sb.Add(XtoSql_select(cmd.Select()));
|
||||
return sb.XtoStr();
|
||||
}
|
||||
int argCount = cmd.Args().Count();
|
||||
if (argCount == 0) throw Err_.new_("Db_qry_insert has no columns").Add("baseTable", cmd.BaseTable());
|
||||
int last = argCount - 1;
|
||||
sb.Add_many("INSERT INTO ", cmd.BaseTable(), " (");
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
sb.Add(" VALUES (");
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
Db_arg prm = (Db_arg)pair.Val();
|
||||
this.BldValStr(sb, prm);
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public String XtoSql_update(Db_qry_update cmd) {
|
||||
int argCount = cmd.Args().Count();
|
||||
if (argCount == 0) throw Err_.new_("Db_qry_update has no columns").Add("baseTable", cmd.BaseTable());
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
sb.Add_many("UPDATE ", cmd.BaseTable(), " SET ");
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
KeyVal pair = cmd.Args().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, pair.Key_as_obj());
|
||||
sb.Add("=");
|
||||
this.BldValStr(sb, (Db_arg)pair.Val());
|
||||
}
|
||||
BldWhere(sb, cmd.Where());
|
||||
return sb.XtoStr();
|
||||
}
|
||||
public String XtoSql_select(Db_qry_select cmd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
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.FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
this.XtoSqlCol(sb, fld.XtoSql());
|
||||
}
|
||||
XtoSql_from(sb, cmd.From());
|
||||
BldWhere(sb, cmd.Where());
|
||||
XtoSql_group_by(sb, cmd.GroupBy());
|
||||
XtoSql_order_by(sb, cmd.OrderBy());
|
||||
XtoSql_limit(sb, cmd.Limit());
|
||||
return sb.XtoStr();
|
||||
}
|
||||
void XtoSql_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().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item);
|
||||
}
|
||||
}
|
||||
void XtoSql_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().FetchAt(i);
|
||||
if (i > 0) sb.Add(", ");
|
||||
sb.Add(item.XtoSql());
|
||||
}
|
||||
}
|
||||
void XtoSql_limit(String_bldr sb, int limit) {
|
||||
if (limit == Db_qry_select.Limit_disabled) return;
|
||||
sb.Add(" LIMIT ").Add(limit);
|
||||
}
|
||||
void XtoSql_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().FetchAt(i);
|
||||
String conjunction = i == 0 ? " ON " : " AND ";
|
||||
sb.Add_many
|
||||
( conjunction, joinLink.SrcTbl(), ".", joinLink.SrcFld(), "=", tblAliasForJoin, ".", joinLink.TrgFldOrSrcFld()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void XtoSqlCol(String_bldr sb, Object obj) {
|
||||
if (obj == null) throw Err_.null_("ColName");
|
||||
sb.Add_obj(obj); // FIXME: options for bracketing; ex: [name]
|
||||
}
|
||||
public void BldValStr(String_bldr sb, Db_arg prm) {
|
||||
if (prepare) {
|
||||
sb.Add("?");
|
||||
return;
|
||||
}
|
||||
Object val = prm.Val();
|
||||
if (val == null) {
|
||||
sb.Add("NULL");
|
||||
return;
|
||||
}
|
||||
Class<?> valType = val.getClass();
|
||||
if (valType == Boolean.class)
|
||||
sb.Add_obj(Bool_.XtoInt(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
|
||||
( valType == Byte.class || valType == Short.class
|
||||
|| valType == Integer.class || valType == Long.class
|
||||
|| valType == Float.class || valType == Double.class
|
||||
)
|
||||
sb.Add(Object_.XtoStr_OrNull(val));
|
||||
else if (valType == DateAdp.class)
|
||||
XtoSqlVal_DateAdp(sb, prm, (DateAdp)val);
|
||||
else if (valType == DecimalAdp.class) {
|
||||
DecimalAdp valDecimal = (DecimalAdp)val;
|
||||
sb.Add(valDecimal.XtoStr());
|
||||
}
|
||||
// else if (valType == System.Enum.class)
|
||||
// sb.Add_any(Enm_.XtoInt(val)); // save enum as 0 or 1, since (a) no db supports enum datatype; (b) names are fungible; (c) int is less space than name
|
||||
else {
|
||||
String valString = Object_.XtoStr_OrNull(val);
|
||||
XtoSqlVal_Str(sb, prm, valString);
|
||||
}
|
||||
}
|
||||
@gplx.Virtual public void XtoSqlVal_Str(String_bldr sb, Db_arg prm, String s) {
|
||||
sb.Add_many("'", String_.Replace(s, "'", "''"), "'"); // stupid escaping of '
|
||||
}
|
||||
@gplx.Virtual public void XtoSqlVal_DateAdp(String_bldr sb, Db_arg prm, DateAdp s) {
|
||||
sb.Add_many("'", s.XtoStr_gplx_long(), "'"); // stupid escaping of '
|
||||
}
|
||||
void BldWhere(String_bldr sb, Sql_where where) {
|
||||
if (where == null || where.Crt() == Db_crt_.Wildcard) return;
|
||||
sb.Add(" WHERE ");
|
||||
this.BldWhere(sb, where.Crt());
|
||||
}
|
||||
public void BldWhere(String_bldr sb, Criteria crt) {
|
||||
Criteria_bool_base boolOp = Criteria_bool_base.as_(crt);
|
||||
if (boolOp != null) {
|
||||
sb.Add("(");
|
||||
BldWhere(sb, boolOp.Lhs());
|
||||
sb.Add_many(" ", boolOp.OpLiteral(), " ");
|
||||
BldWhere(sb, boolOp.Rhs());
|
||||
sb.Add(")");
|
||||
return;
|
||||
}
|
||||
if (crt.Crt_tid() == Criteria_.Tid_db_obj_ary) {
|
||||
Append_db_obj_ary(sb, (Db_obj_ary_crt)crt);
|
||||
}
|
||||
else {
|
||||
Criteria_wrapper leaf = Criteria_wrapper.as_(crt); if (leaf == null) throw Err_.invalid_op_(crt.XtoStr());
|
||||
sb.Add(leaf.Name_of_GfoFldCrt());
|
||||
AppendWhereItem(sb, leaf.Crt_of_GfoFldCrt());
|
||||
}
|
||||
}
|
||||
void AppendWhereItem(String_bldr sb, Criteria crt) {
|
||||
switch (crt.Crt_tid()) {
|
||||
case Criteria_.Tid_eq: AppendEqual(sb, Criteria_eq.as_(crt)); break;
|
||||
case Criteria_.Tid_comp: AppendCompare(sb, Criteria_comp.as_(crt)); break;
|
||||
case Criteria_.Tid_between: AppendBetween(sb, Criteria_between.as_(crt)); break;
|
||||
case Criteria_.Tid_in: AppendIn(sb, Criteria_in.as_(crt)); break;
|
||||
case Criteria_.Tid_like: AppendLike(sb, Criteria_like.as_(crt)); break;
|
||||
case Criteria_.Tid_iomatch: AppendIoMatch(sb, Criteria_ioMatch.as_(crt)); break;
|
||||
default: throw Err_.unhandled(crt);
|
||||
}
|
||||
}
|
||||
void AppendEqual(String_bldr sb, Criteria_eq crt) {
|
||||
sb.Add(crt.Negated() ? "!=" : "=");
|
||||
this.BldValStr(sb, Wrap(crt.Value()));
|
||||
}
|
||||
void AppendCompare(String_bldr sb, Criteria_comp crt) {
|
||||
sb.Add_many(crt.XtoSymbol());
|
||||
this.BldValStr(sb, Wrap(crt.Value()));
|
||||
}
|
||||
void AppendBetween(String_bldr sb, Criteria_between crt) {
|
||||
sb.Add(crt.Negated() ? " NOT BETWEEN " : " BETWEEN ");
|
||||
this.BldValStr(sb, Wrap(crt.Lhs()));
|
||||
sb.Add(" AND ");
|
||||
this.BldValStr(sb, Wrap(crt.Rhs()));
|
||||
}
|
||||
void AppendLike(String_bldr sb, Criteria_like crt) {
|
||||
sb.Add(crt.Negated() ? " NOT LIKE " : " LIKE ");
|
||||
this.BldValStr(sb, Wrap(crt.Pattern().Raw()));
|
||||
sb.Add_fmt(" ESCAPE '{0}'", crt.Pattern().Escape());
|
||||
}
|
||||
void AppendIn(String_bldr sb, Criteria_in crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IN (" : " IN (");
|
||||
int last = crt.Values().length - 1;
|
||||
for (int i = 0; i < crt.Values().length; i++) {
|
||||
Object val = crt.Values()[i];
|
||||
this.BldValStr(sb, Wrap(val));
|
||||
sb.Add(i == last ? ")" : ", ");
|
||||
}
|
||||
}
|
||||
void AppendIoMatch(String_bldr sb, Criteria_ioMatch crt) {
|
||||
sb.Add(crt.Negated() ? " NOT IOMATCH " : " IOMATCH ");
|
||||
this.BldValStr(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 ClassAdp_.Tid_str:
|
||||
case ClassAdp_.Tid_char:
|
||||
case ClassAdp_.Tid_date:
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
sb.Add(fld.Name());
|
||||
sb.Add("=");
|
||||
if (quote) sb.Add("'");
|
||||
sb.Add(Object_.XtoStr_OrEmpty(val));
|
||||
if (quote) sb.Add("'");
|
||||
}
|
||||
sb.Add(")");
|
||||
}
|
||||
}
|
||||
Db_arg Wrap(Object val) {return new Db_arg("unknown", val);}
|
||||
}
|
||||
class Sql_cmd_wtr_ansi_ {
|
||||
public static Sql_cmd_wtr default_() {return new Sql_cmd_wtr_ansi();}
|
||||
public static Sql_cmd_wtr backslash_sensitive_() {return Sql_cmd_wtr_ansi_backslashSensitive.new_();}
|
||||
}
|
||||
class Sql_cmd_wtr_ansi_backslashSensitive extends Sql_cmd_wtr_ansi { @Override public void XtoSqlVal_Str(String_bldr sb, Db_arg prm, String s) {
|
||||
if (String_.Has(s, "\\")) s = String_.Replace(s, "\\", "\\\\");
|
||||
super.XtoSqlVal_Str(sb, prm, s);
|
||||
}
|
||||
public static Sql_cmd_wtr_ansi_backslashSensitive new_() {return new Sql_cmd_wtr_ansi_backslashSensitive();} Sql_cmd_wtr_ansi_backslashSensitive() {}
|
||||
}
|
||||
67
140_dbs/src_120_sql/gplx/dbs/Sql_join_itm.java
Normal file
67
140_dbs/src_120_sql/gplx/dbs/Sql_join_itm.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
class Sql_from {
|
||||
public ListAdp Tbls() {return tbls;} ListAdp tbls = ListAdp_.new_();
|
||||
public Sql_tbl_src BaseTable() {return (Sql_tbl_src)tbls.FetchAt(0);}
|
||||
|
||||
public static Sql_from new_(Sql_tbl_src baseTable) {
|
||||
Sql_from rv = new Sql_from();
|
||||
rv.tbls.Add(baseTable);
|
||||
return rv;
|
||||
} Sql_from() {}
|
||||
}
|
||||
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 ListAdp JoinLinks() {return joinLinks;} ListAdp joinLinks = ListAdp_.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() {}
|
||||
}
|
||||
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")
|
||||
;
|
||||
}
|
||||
73
140_dbs/src_120_sql/gplx/dbs/Sql_order_by.java
Normal file
73
140_dbs/src_120_sql/gplx/dbs/Sql_order_by.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.lists.*; /*ComparerAble*/
|
||||
class Sql_order_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_order_by new_(Sql_order_by_itm... ary) {
|
||||
Sql_order_by rv = new Sql_order_by();
|
||||
for (Sql_order_by_itm itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_order_by() {}
|
||||
}
|
||||
class Sql_group_by {
|
||||
public ListAdp Flds() {return flds;} ListAdp flds = ListAdp_.new_();
|
||||
|
||||
public static Sql_group_by new_(String... ary) {
|
||||
Sql_group_by rv = new Sql_group_by();
|
||||
for (String itm : ary)
|
||||
rv.flds.Add(itm);
|
||||
return rv;
|
||||
} Sql_group_by() {}
|
||||
}
|
||||
class Sql_order_by_itm {
|
||||
public String Name() {return name;} private String name;
|
||||
public boolean Ascending() {return ascending;} private boolean ascending;
|
||||
public String XtoSql() {
|
||||
String ascString = ascending ? "" : " DESC";
|
||||
return name + ascString;
|
||||
}
|
||||
public static Sql_order_by_itm new_(String name, boolean ascending) {
|
||||
Sql_order_by_itm rv = new Sql_order_by_itm();
|
||||
rv.name = name; rv.ascending = ascending;
|
||||
return rv;
|
||||
} Sql_order_by_itm() {}
|
||||
}
|
||||
class Sql_order_by_sorter implements ComparerAble {
|
||||
public int compare(Object lhsObj, Object rhsObj) {
|
||||
GfoNde lhs = (GfoNde)lhsObj; GfoNde rhs = (GfoNde)rhsObj;
|
||||
Sql_order_by_itm item = null; Object lhsData = null, rhsData = null;
|
||||
for (int i = 0; i < items.length; i++) {
|
||||
item = items[i];
|
||||
lhsData = lhs.Read(item.Name()); rhsData = rhs.Read(item.Name());
|
||||
int compare = CompareAble_.Compare_obj(lhsData, rhsData);
|
||||
if (compare == CompareAble_.Same) continue;
|
||||
int ascendingVal = item.Ascending() ? 1 : -1;
|
||||
return compare * ascendingVal;
|
||||
}
|
||||
return CompareAble_.Same;
|
||||
}
|
||||
Sql_order_by_itm[] items;
|
||||
public static ComparerAble new_(Sql_order_by_itm[] items) {
|
||||
Sql_order_by_sorter rv = new Sql_order_by_sorter();
|
||||
rv.items = items;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
143
140_dbs/src_120_sql/gplx/dbs/Sql_select.java
Normal file
143
140_dbs/src_120_sql/gplx/dbs/Sql_select.java
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
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.*;
|
||||
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_fld.new_(Sql_select_fld_base.Tbl_null, fldName, fldName));}
|
||||
public void Add(String fldName, String alias) {flds.Add(Sql_select_fld_fld.new_(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._); return rv;}
|
||||
public static Sql_select new_() {return new Sql_select();} Sql_select() {}
|
||||
}
|
||||
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 FetchAt(int i) {return (Sql_select_fld_base)hash.FetchAt(i);}
|
||||
public Sql_select_fld_base FetchOrNull(String k) {return (Sql_select_fld_base)hash.Fetch(k);}
|
||||
public GfoFldList XtoGfoFldLst(TdbTable tbl) {
|
||||
GfoFldList rv = GfoFldList_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base selectFld = this.FetchAt(i);
|
||||
GfoFld fld = tbl.Flds().FetchOrNull(selectFld.Fld());
|
||||
if (fld == null) throw Err_.new_("fld not found in tbl").Add("fldName", selectFld.Fld()).Add("tblName", tbl.Name()).Add("tblFlds", tbl.Flds().XtoStr());
|
||||
if (rv.Has(selectFld.Alias())) throw Err_.new_("alias is not unique").Add("fldName", selectFld.Fld()).Add("flds", rv.XtoStr());
|
||||
selectFld.GroupBy_type(fld);
|
||||
rv.Add(selectFld.Alias(), selectFld.ValType());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public String XtoStr() {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
for (int i = 0; i < this.Count(); i++) {
|
||||
Sql_select_fld_base fld = this.FetchAt(i);
|
||||
sb.Add_fmt("{0},{1}|", fld.Fld(), fld.Alias());
|
||||
}
|
||||
return sb.XtoStr();
|
||||
}
|
||||
OrderedHash hash = OrderedHash_.new_();
|
||||
public static Sql_select_fld_list new_() {return new Sql_select_fld_list();} Sql_select_fld_list() {}
|
||||
}
|
||||
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._;
|
||||
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_("group by eval not allowed on *");}
|
||||
@Override public void GroupBy_type(GfoFld fld) {throw Err_.new_("group by type not allowed on *");}
|
||||
@Override public String XtoSql() {return "*";}
|
||||
public static final Sql_select_fld_wild _ = new Sql_select_fld_wild(); Sql_select_fld_wild() {this.ctor_(Tbl_null, "*", "*");}
|
||||
}
|
||||
class Sql_select_fld_fld extends Sql_select_fld_base {
|
||||
@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;
|
||||
}
|
||||
public static Sql_select_fld_fld new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_fld rv = new Sql_select_fld_fld();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_fld() {}
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
class Sql_select_fld_count extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "COUNT";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@Override public Object GroupBy_eval(Object groupByVal, Object curVal, ClassXtn type) {
|
||||
if (groupByVal == null) return 1;
|
||||
return Int_.cast_(groupByVal) + 1;
|
||||
}
|
||||
public static Sql_select_fld_count new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_count rv = new Sql_select_fld_count();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_count() {}
|
||||
}
|
||||
class Sql_select_fld_sum extends Sql_select_fld_func_base {
|
||||
@Override public String XtoSql_functionName() {return "SUM";}
|
||||
@Override public void GroupBy_type(GfoFld fld) {this.ValType_set(IntClassXtn._);}
|
||||
@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);
|
||||
}
|
||||
public static Sql_select_fld_sum new_(String tbl, String fld, String alias) {
|
||||
Sql_select_fld_sum rv = new Sql_select_fld_sum();
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_sum() {}
|
||||
}
|
||||
class Sql_select_fld_minMax extends Sql_select_fld_func_base {
|
||||
int compareType = CompareAble_.Less;
|
||||
@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;
|
||||
}
|
||||
public static Sql_select_fld_minMax min_(String tbl, String fld, String alias) {return new_(CompareAble_.Less, tbl, fld, alias);}
|
||||
public static Sql_select_fld_minMax max_(String tbl, String fld, String alias) {return new_(CompareAble_.More, tbl, fld, alias);}
|
||||
static Sql_select_fld_minMax new_(int compareType, String tbl, String fld, String alias) {
|
||||
Sql_select_fld_minMax rv = new Sql_select_fld_minMax();
|
||||
rv.compareType = compareType;
|
||||
rv.ctor_(tbl, fld, alias);
|
||||
return rv;
|
||||
} Sql_select_fld_minMax() {}
|
||||
}
|
||||
32
140_dbs/src_120_sql/gplx/dbs/Sql_where.java
Normal file
32
140_dbs/src_120_sql/gplx/dbs/Sql_where.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.*;
|
||||
import gplx.criterias.*;
|
||||
class Sql_where {
|
||||
public Criteria Crt() {return crt;} Criteria crt;
|
||||
public static Sql_where merge_or_new_(Sql_where where, Criteria crt) {
|
||||
return where == null
|
||||
? Sql_where.new_(crt)
|
||||
: Sql_where.new_(Criteria_.And(where.Crt(), crt));
|
||||
}
|
||||
public static Sql_where new_(Criteria crt) {
|
||||
Sql_where rv = new Sql_where();
|
||||
rv.crt = crt;
|
||||
return rv;
|
||||
} Sql_where() {}
|
||||
}
|
||||
66
140_dbs/src_130_misc/gplx/dbs/Db_cmd_backup.java
Normal file
66
140_dbs/src_130_misc/gplx/dbs/Db_cmd_backup.java
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
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_cmd_backup implements GfoInvkAble {
|
||||
public String DbName() {return dbName;} public Db_cmd_backup DbName_(String val) {dbName = val; return this;} private String dbName = "db";
|
||||
public Io_url ExeUrl() {return exeUrl;} public Db_cmd_backup ExeUrl_(Io_url val) {exeUrl = val; return this;} Io_url exeUrl;
|
||||
public Io_url BkpDir() {return bkpDir;} public Db_cmd_backup BkpDir_(Io_url val) {bkpDir = val; return this;} Io_url bkpDir;
|
||||
public String Usr() {return usr;} public Db_cmd_backup Usr_(String val) {usr = val; return this;} private String usr;
|
||||
public String Pwd() {return pwd;} public Db_cmd_backup Pwd_(String val) {pwd = val; return this;} private String pwd;
|
||||
public String DteFmt() {return dteFmt;} public Db_cmd_backup DteFmt_(String val) {dteFmt = val; return this;} private String dteFmt = "yyyyMMdd_HHmm";
|
||||
public String BkpFilNameFmt() {return bkpFilNameFmt;} public Db_cmd_backup BkpFilNameFmt_(String val) {bkpFilNameFmt = val; return this;} private String bkpFilNameFmt = "{0}_{1}.sql";
|
||||
public String BkpFilName() {return bkpFilName;} private String bkpFilName;
|
||||
public Io_url BkpFil() {return bkpFil;} Io_url bkpFil;
|
||||
public String CmdText() {return cmdText;} private String cmdText;
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_ExeUrl)) return exeUrl;
|
||||
else if (ctx.Match(k, Invk_BkpDir)) return bkpDir;
|
||||
else if (ctx.Match(k, Invk_Usr)) return usr;
|
||||
else if (ctx.Match(k, Invk_Pwd)) return pwd;
|
||||
else if (ctx.Match(k, Invk_DteFmt)) return dteFmt;
|
||||
else if (ctx.Match(k, Invk_BkpFilNameFmt)) return bkpFilNameFmt;
|
||||
else if (ctx.Match(k, Invk_ExeUrl_)) exeUrl = GfoMsgUtl.SetIoUrl(ctx, m, exeUrl);
|
||||
else if (ctx.Match(k, Invk_BkpDir_)) bkpDir = GfoMsgUtl.SetIoUrl(ctx, m, exeUrl);
|
||||
else if (ctx.Match(k, Invk_Usr_)) usr = GfoMsgUtl.SetStr(ctx, m, usr);
|
||||
else if (ctx.Match(k, Invk_Pwd_)) pwd = GfoMsgUtl.SetStr(ctx, m, pwd);
|
||||
else if (ctx.Match(k, Invk_DteFmt_)) dteFmt = GfoMsgUtl.SetStr(ctx, m, dteFmt);
|
||||
else if (ctx.Match(k, Invk_BkpFilNameFmt_)) bkpFilNameFmt = GfoMsgUtl.SetStr(ctx, m, bkpFilNameFmt);
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
return this;
|
||||
} public static final String
|
||||
Invk_ExeUrl = "ExeUrl", Invk_BkpDir = "BkpDir", Invk_Usr = "Usr", Invk_Pwd = "Pwd", Invk_DteFmt = "DteFmt", Invk_BkpFilNameFmt = "BkpFilNameFmt"
|
||||
, Invk_ExeUrl_ = "ExeUrl_", Invk_BkpDir_ = "BkpDir_", Invk_Usr_ = "Usr_", Invk_Pwd_ = "Pwd_", Invk_DteFmt_ = "DteFmt_", Invk_BkpFilNameFmt_ = "BkpFilNameFmt_"
|
||||
;
|
||||
public Db_cmd_backup Exec() {
|
||||
this.InitVars();
|
||||
Io_url bkpCmdFil = bkpDir.GenSubFil_ary("backup_", dbName, ".cmd");
|
||||
// Io_url bkpCmdFil = Io_url_.new_dir_("/home/").GenSubFil_ary("backup_", dbName, ".cmd"); // LNX: uncomment
|
||||
Io_mgr._.SaveFilStr_args(bkpCmdFil, cmdText).Exec(); // explicitly state utf8;
|
||||
ProcessAdp.run_wait_(bkpCmdFil);
|
||||
Io_mgr._.DeleteFil(bkpCmdFil);
|
||||
return this;
|
||||
}
|
||||
@gplx.Internal protected Db_cmd_backup InitVars() {
|
||||
String dteStr = DateAdp_.Now().XtoStr_fmt(dteFmt);
|
||||
bkpFilName = String_.Format(bkpFilNameFmt, dbName, dteStr);
|
||||
bkpFil = bkpDir.GenSubFil(bkpFilName);
|
||||
cmdText = String_.Format("\"{0}\" -u {1} -p{2} {3} > {4}", exeUrl.Xto_api(), usr, pwd, dbName, bkpFil.Xto_api());
|
||||
return this;
|
||||
}
|
||||
public static Db_cmd_backup new_() {return new Db_cmd_backup();} Db_cmd_backup() {}
|
||||
}
|
||||
31
140_dbs/src_130_misc/gplx/dbs/Db_cmd_backup_tst.java
Normal file
31
140_dbs/src_130_misc/gplx/dbs/Db_cmd_backup_tst.java
Normal file
@@ -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; import gplx.*;
|
||||
import org.junit.*;
|
||||
public class Db_cmd_backup_tst {
|
||||
@Test public void Basic() {
|
||||
Tfds.Now_enabled_y_();
|
||||
Db_cmd_backup bkpWkr = Db_cmd_backup.new_()
|
||||
.ExeUrl_(Io_url_.new_any_("C:\\mysql\\mysqldump.exe"))
|
||||
.BkpDir_(Io_url_.new_any_("C:\\bkp\\"))
|
||||
.Usr_("username")
|
||||
.Pwd_("password")
|
||||
.DbName_("dbname").InitVars();
|
||||
Tfds.Eq("\"C:\\mysql\\mysqldump.exe\" -u username -ppassword dbname > C:\\bkp\\dbname_20010101_0000.sql", bkpWkr.CmdText());
|
||||
}
|
||||
}
|
||||
32
140_dbs/src_130_misc/gplx/dbs/Db_cmd_mode.java
Normal file
32
140_dbs/src_130_misc/gplx/dbs/Db_cmd_mode.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_cmd_mode {
|
||||
public static final byte Create = 1, Update = 2, Delete = 3, Ignore = 4;
|
||||
public static byte X_to_update(byte cur) {
|
||||
switch (cur) {
|
||||
case Create: // ignore update if item is already marked for create
|
||||
case Delete: // ignore update if item is already marked for delete (might want to throw error)
|
||||
return cur;
|
||||
case Ignore: // must mark for update
|
||||
case Update: // return self
|
||||
return Update;
|
||||
default: throw Err_.unhandled(cur);
|
||||
}
|
||||
}
|
||||
}
|
||||
52
140_dbs/src_130_misc/gplx/dbs/Db_crt_.java
Normal file
52
140_dbs/src_130_misc/gplx/dbs/Db_crt_.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_crt_ {
|
||||
public static final Criteria Wildcard = Criteria_.All;
|
||||
public static Criteria eq_(String name, Object val) {return Criteria_wrapper.new_(name, Criteria_.eq_(val));}
|
||||
public static Criteria eqn_(String name, Object val) {return Criteria_wrapper.new_(name, Criteria_.eqn_(val));}
|
||||
public static Criteria lt_(String name, Comparable val) {return Criteria_wrapper.new_(name, Criteria_.lt_(val));}
|
||||
public static Criteria lte_(String name, Comparable val) {return Criteria_wrapper.new_(name, Criteria_.lte_(val));}
|
||||
public static Criteria mt_(String name, Comparable val) {return Criteria_wrapper.new_(name, Criteria_.mt_(val));}
|
||||
public static Criteria mte_(String name, Comparable val) {return Criteria_wrapper.new_(name, Criteria_.mte_(val));}
|
||||
public static Criteria between_(String name, Comparable lhs, Comparable rhs) {return Criteria_wrapper.new_(name, Criteria_.between_(lhs, rhs));}
|
||||
public static Criteria in_(String name, Object... vals) {return Criteria_wrapper.new_(name, Criteria_.in_(vals));}
|
||||
public static Criteria like_(String name, String pattern) {return Criteria_wrapper.new_(name, Criteria_.like_(pattern));}
|
||||
public static Criteria liken_(String name, String pattern) {return Criteria_wrapper.new_(name, Criteria_.liken_(pattern));}
|
||||
public static Criteria eqMany_(KeyVal... array) {
|
||||
Criteria rv = null;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
KeyVal pair = array[i];
|
||||
Criteria crt = Db_crt_.eq_(pair.Key(), pair.Val());
|
||||
rv = (i == 0)? crt : Criteria_.And(rv, crt);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Criteria eq_(String name) {return Criteria_wrapper.new_(name, Criteria_.eq_(null));}
|
||||
public static Criteria eq_many_(String... ary) {
|
||||
Criteria rv = null;
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Criteria crt = Db_crt_.eq_(ary[i], null);
|
||||
rv = (i == 0)? crt : Criteria_.And(rv, crt);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static Criteria wrap_(String name, Criteria crt) {return Criteria_wrapper.new_(name, crt);}
|
||||
}
|
||||
34
140_dbs/src_130_misc/gplx/dbs/Db_idx_itm.java
Normal file
34
140_dbs/src_130_misc/gplx/dbs/Db_idx_itm.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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_idx_itm {
|
||||
public String Xto_sql() {return sql;} private String sql;
|
||||
public static Db_idx_itm sql_(String sql) {
|
||||
Db_idx_itm rv = new Db_idx_itm();
|
||||
rv.sql = sql;
|
||||
return rv;
|
||||
}
|
||||
public static Db_idx_itm[] ary_sql_(String... ary) {
|
||||
int len = ary.length;
|
||||
Db_idx_itm[] rv = new Db_idx_itm[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
rv[i] = sql_(ary[i]);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
30
140_dbs/src_130_misc/gplx/dbs/Db_obj_state.java
Normal file
30
140_dbs/src_130_misc/gplx/dbs/Db_obj_state.java
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
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_obj_state {
|
||||
public int Val() {return val;} int val;
|
||||
public Db_obj_state MarkUpdated() {return this == Retrieved ? Updated : this;} // Created/Deleted noops
|
||||
public boolean Modified() {return this == Created || this == Updated;}
|
||||
Db_obj_state(int val) {this.val = val;}
|
||||
public static final Db_obj_state
|
||||
Created = new Db_obj_state(1)
|
||||
, Retrieved = new Db_obj_state(2)
|
||||
, Updated = new Db_obj_state(3)
|
||||
, Deleted = new Db_obj_state(4)
|
||||
;
|
||||
}
|
||||
50
140_dbs/src_130_misc/gplx/dbs/PoolIds.java
Normal file
50
140_dbs/src_130_misc/gplx/dbs/PoolIds.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
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 PoolIds {
|
||||
public int FetchNext(Db_provider provider, String url) {
|
||||
Db_qry_select cmd = Db_qry_.select_().From_(Tbl_Name).Where_(Db_crt_.eq_(Fld_id_path, url));
|
||||
int rv = 0;//boolean isNew = true;
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = cmd.Exec_qry_as_rdr(provider);
|
||||
if (rdr.MoveNextPeer()) {
|
||||
rv = rdr.ReadInt(Fld_id_next_id);
|
||||
}
|
||||
}
|
||||
finally {rdr.Rls();}
|
||||
return rv;
|
||||
}
|
||||
public int FetchNextAndCommit(String dbInfo, String url) {
|
||||
Db_provider provider = Db_provider_pool._.FetchOrNew(dbInfo);
|
||||
int rv = PoolIds._.FetchNext(provider, url);
|
||||
PoolIds._.Commit(provider, url, rv + 1);
|
||||
return rv;
|
||||
}
|
||||
public void Commit(Db_provider provider, String url, int val) {
|
||||
int rv = provider.Exec_qry(Db_qry_.update_(Tbl_Name, Db_crt_.eq_(Fld_id_path, url)).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
if (rv == 0) {
|
||||
rv = provider.Exec_qry(Db_qry_.insert_(Tbl_Name).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
|
||||
}
|
||||
if (rv != 1) throw Err_.new_("failed to update nextId").Add("provider", provider.ConnectInfo().Raw_of_db_connect()).Add("url", url).Add("nextId", val);
|
||||
}
|
||||
public static final String Tbl_Name = "pool_ids";
|
||||
@gplx.Internal protected static final String Fld_id_path = "id_path";
|
||||
@gplx.Internal protected static final String Fld_id_next_id = "id_next_id";
|
||||
public static final PoolIds _ = new PoolIds(); PoolIds() {}
|
||||
}
|
||||
51
140_dbs/src_200_engine/gplx/dbs/Db_engine.java
Normal file
51
140_dbs/src_200_engine/gplx/dbs/Db_engine.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
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.*;
|
||||
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);
|
||||
}
|
||||
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 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 static final Db_engine_null _ = new Db_engine_null(); Db_engine_null() {}
|
||||
}
|
||||
class ExecSqlWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmd) {
|
||||
Db_engine_sql_base engine = (Db_engine_sql_base)engineObj;
|
||||
String sql = engine.SqlWtr().XtoSqlQry(cmd, false); // Tfds.Write(sql);
|
||||
return cmd.ExecRdrAble() ? (Object)engine.ExecuteReader(sql) : engine.ExecuteNonQuery(sql);
|
||||
}
|
||||
}
|
||||
82
140_dbs/src_200_engine/gplx/dbs/Db_engine_sql_base.java
Normal file
82
140_dbs/src_200_engine/gplx/dbs/Db_engine_sql_base.java
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
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.*;
|
||||
import java.sql.*;
|
||||
abstract class Db_engine_sql_base implements Db_engine, RlsAble {
|
||||
public abstract String Key();
|
||||
public Db_connect ConnectInfo() {return dbInfo;} protected Db_connect dbInfo;
|
||||
public abstract Db_engine MakeEngine(Db_connect dbInfo);
|
||||
@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;
|
||||
wkrs.Add(Db_qry_select.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_insert.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_update.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_delete.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_sql.KeyConst, new ExecSqlWkr());
|
||||
wkrs.Add(Db_qry_flush.KeyConst, Db_qryWkr_.Null);
|
||||
} HashAdp wkrs = HashAdp_.new_();
|
||||
@gplx.Internal @gplx.Virtual protected int ExecuteNonQuery(String sql) {
|
||||
try {
|
||||
Statement cmd = NewDbCmd(sql);
|
||||
return cmd.executeUpdate(sql);
|
||||
}
|
||||
catch (Exception exc) {throw Err_.err_(exc, "exec nonQuery failed").Add("sql", sql).Add("err", Err_.Message_gplx_brief(exc));}
|
||||
}
|
||||
@gplx.Internal @gplx.Virtual protected DataRdr ExecuteReader(String sql) {
|
||||
try {
|
||||
Statement cmd = NewDbCmd(sql);
|
||||
cmd.execute(sql);
|
||||
ResultSet rdr = cmd.getResultSet();
|
||||
return NewDataRdr(rdr, sql);
|
||||
}
|
||||
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.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 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();
|
||||
} private Connection connection;
|
||||
public void Rls() {
|
||||
// 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());}
|
||||
}
|
||||
Statement NewDbCmd(String commandText) {
|
||||
Statement cmd = null;
|
||||
try {cmd = connection.createStatement();}
|
||||
catch (SQLException e) {throw Err_.err_(e, "could not create statement").Add("commandText", commandText).Add("e", Err_.Message_lang(e));}
|
||||
return cmd;
|
||||
}
|
||||
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());}
|
||||
}
|
||||
}
|
||||
48
140_dbs/src_300_sqlDbs/gplx/dbs/Mysql_engine.java
Normal file
48
140_dbs/src_300_sqlDbs/gplx/dbs/Mysql_engine.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.stores.*;
|
||||
import java.sql.*;
|
||||
class Mysql_engine extends Db_engine_sql_base {
|
||||
@Override public String Key() {return Db_connect_mysql.KeyDef;}
|
||||
@Override public Sql_cmd_wtr SqlWtr() {return Sql_cmd_wtr_.BackslashSensitive;}
|
||||
@Override public Db_engine MakeEngine(Db_connect connectInfo) {
|
||||
Mysql_engine rv = new Mysql_engine();
|
||||
rv.ctor_SqlEngineBase(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr NewDataRdr(ResultSet rdr, String commandText) {return Mysql_rdr.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection NewDbCon() {
|
||||
Db_connect_mysql connUrl = (Db_connect_mysql)dbInfo;
|
||||
return NewDbCon("jdbc:mysql://localhost/" + connUrl.Database() + "?characterEncoding=UTF8", connUrl.Uid(), connUrl.Pwd());
|
||||
}
|
||||
@gplx.Internal protected static final Mysql_engine _ = new Mysql_engine(); Mysql_engine() {}
|
||||
}
|
||||
class Mysql_rdr extends Db_data_rdr {
|
||||
//PATCH:MYSQL:byte actually returned as int by Jdbc ResultSet (or MYSQL impmentation); convert to byte
|
||||
@Override public byte ReadByte(String key) {return ReadByteOr(key, Byte.MAX_VALUE);}
|
||||
@Override public byte ReadByteOr(String key, byte or) {
|
||||
Object val = Read(key);
|
||||
return val == null ? or : ((Integer)val).byteValue();
|
||||
}
|
||||
public static Mysql_rdr new_(ResultSet rdr, String commandText) {
|
||||
Mysql_rdr rv = new Mysql_rdr();
|
||||
rv.ctor_db_data_rdr(rdr, commandText);
|
||||
return rv;
|
||||
} Mysql_rdr() {}
|
||||
}
|
||||
35
140_dbs/src_300_sqlDbs/gplx/dbs/Postgres_engine.java
Normal file
35
140_dbs/src_300_sqlDbs/gplx/dbs/Postgres_engine.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.stores.*;
|
||||
import java.sql.*;
|
||||
class Postgres_engine extends Db_engine_sql_base {
|
||||
@Override public String Key() {return Db_connect_postgres.KeyDef;}
|
||||
@Override public Sql_cmd_wtr SqlWtr() {return Sql_cmd_wtr_.BackslashSensitive;}
|
||||
@Override public Db_engine MakeEngine(Db_connect connectInfo) {
|
||||
Postgres_engine rv = new Postgres_engine();
|
||||
rv.ctor_SqlEngineBase(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr NewDataRdr(ResultSet rdr, String commandText) {return Db_data_rdr_.new_(rdr, commandText);}
|
||||
@gplx.Internal @Override protected Connection NewDbCon() {
|
||||
Db_connect_postgres connUrl = (Db_connect_postgres)dbInfo;
|
||||
return NewDbCon("jdbc:" + connUrl.Key_of_db_connect() + "://localhost/" + connUrl.Database(), connUrl.Uid(), connUrl.Pwd());
|
||||
}
|
||||
@gplx.Internal protected static final Postgres_engine _ = new Postgres_engine(); Postgres_engine() {}
|
||||
}
|
||||
96
140_dbs/src_300_sqlDbs/gplx/dbs/Sqlite_engine.java
Normal file
96
140_dbs/src_300_sqlDbs/gplx/dbs/Sqlite_engine.java
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.stores.*;
|
||||
import java.sql.*;
|
||||
class Sqlite_engine extends Db_engine_sql_base {
|
||||
@Override public String Key() {return Db_connect_sqlite.KeyDef;}
|
||||
@Override public Db_engine MakeEngine(Db_connect connectInfo) {
|
||||
Sqlite_engine rv = new Sqlite_engine();
|
||||
rv.ctor_SqlEngineBase(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
@Override public DataRdr NewDataRdr(ResultSet rdr, String commandText) {return Sqlite_rdr.new_(rdr, commandText);}
|
||||
static boolean loaded = false;
|
||||
@gplx.Internal @Override protected Connection NewDbCon() {
|
||||
if (!loaded) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
}
|
||||
catch (ClassNotFoundException e) {throw Err_.new_("could not load sqlite jdbc driver");}
|
||||
loaded = true;
|
||||
}
|
||||
Db_connect_sqlite connUrl = (Db_connect_sqlite)dbInfo;
|
||||
return NewDbCon("jdbc:sqlite://" + String_.Replace(connUrl.Database(), "\\", "/"), "", "");
|
||||
}
|
||||
private boolean pragma_needed = true;
|
||||
@Override public void Txn_bgn() {
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA ENCODING=\"UTF-8\";"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA journal_mode = OFF;")); // will cause out of memory
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA journal_mode = MEMORY;"));
|
||||
if (pragma_needed) {
|
||||
Execute(Db_qry_sql.xtn_("PRAGMA synchronous = OFF;"));
|
||||
pragma_needed = false;
|
||||
}
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA temp_store = MEMORY;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA locking_mode = EXCLUSIVE;"));
|
||||
// Execute(Db_qry_sql.xtn_("PRAGMA cache_size=4000;")); // too many will also cause out of memory
|
||||
Execute(Db_qry_sql.xtn_("BEGIN TRANSACTION;"));
|
||||
}
|
||||
@gplx.Internal protected static final Sqlite_engine _ = new Sqlite_engine(); Sqlite_engine() {}
|
||||
}
|
||||
class Sqlite_rdr extends Db_data_rdr { @Override public float ReadFloat(String key) {return ReadFloatOr(key, Float.NaN);}
|
||||
@Override public float ReadFloatOr(String key, float or) {
|
||||
Object val = Read(key);
|
||||
Double d = ((Double)val);
|
||||
return val == null ? or : d.floatValue();
|
||||
}
|
||||
@Override public DecimalAdp ReadDecimal(String key) {return ReadDecimalOr(key, null);}
|
||||
@Override public DecimalAdp ReadDecimalOr(String key, DecimalAdp or) {
|
||||
Object val = Read(key);
|
||||
Double d = ((Double)val);
|
||||
return val == null ? or : DecimalAdp_.double_(d);
|
||||
}
|
||||
@Override public DateAdp ReadDate(String key) {return ReadDateOr(key, null);}
|
||||
@Override public DateAdp ReadDateOr(String key, DateAdp or) {
|
||||
Object val = Read(key);
|
||||
return val == null ? or : DateAdp_.parse_fmt((String)val, "M/dd/yyyy hh:mm tt");
|
||||
}
|
||||
@Override public boolean ReadBool(String key) {return ReadBoolOr(key, false);}
|
||||
@Override public boolean ReadBoolOr(String key, boolean or) {
|
||||
Object val = Read(key);
|
||||
return val == null ? or : Int_.cast_(val) == 1;
|
||||
}
|
||||
@Override public byte ReadByte(String key) {return ReadByteOr(key, Byte_.Zero);}
|
||||
@Override public byte ReadByteOr(String key, byte or) {
|
||||
Object val = Read(key);
|
||||
return val == null ? or : (byte)Int_.cast_(val);
|
||||
}
|
||||
@Override public long ReadLong(String key) {return ReadLongOr(key, Long_.MinValue);}
|
||||
@Override public long ReadLongOr(String key, long or) {
|
||||
Object val = Read(key);
|
||||
if (val == null) return or;
|
||||
Number n = (Number)val;
|
||||
return n.longValue();
|
||||
}
|
||||
public static Sqlite_rdr new_(ResultSet rdr, String commandText) {
|
||||
Sqlite_rdr rv = new Sqlite_rdr();
|
||||
rv.ctor_db_data_rdr(rdr, commandText);
|
||||
return rv;
|
||||
} Sqlite_rdr() {}
|
||||
}
|
||||
91
140_dbs/src_300_sqlDbs/gplx/dbs/Sqlite_engine_.java
Normal file
91
140_dbs/src_300_sqlDbs/gplx/dbs/Sqlite_engine_.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
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 Sqlite_engine_ {
|
||||
public static void Db_attach(Db_provider p, String alias, String url) {
|
||||
String s = String_.Format("ATTACH '{0}' AS {1};", url, alias);
|
||||
Db_qry qry = Db_qry_sql.xtn_(s);
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Db_detach(Db_provider p, String alias) {
|
||||
String s = String_.Format("DETACH '{0}';", alias);
|
||||
Db_qry qry = Db_qry_sql.xtn_(s);
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Tbl_create_and_delete(Db_provider p, String tbl_name, String tbl_sql) {
|
||||
Tbl_delete(p, tbl_name);
|
||||
Db_qry qry = Db_qry_sql.ddl_(tbl_sql);
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Tbl_create(Db_provider p, String tbl_name, String tbl_sql) {
|
||||
Db_qry qry = Db_qry_sql.ddl_(tbl_sql);
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Tbl_delete_many(Db_provider p, String... tbls) {
|
||||
int len = tbls.length;
|
||||
for (int i = 0; i < len; i++)
|
||||
Tbl_delete(p, tbls[i]);
|
||||
}
|
||||
public static void Tbl_delete(Db_provider p, String tbl) {
|
||||
Db_qry qry = Db_qry_sql.ddl_("DROP TABLE IF EXISTS " + tbl + ";");
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Tbl_rename(Db_provider p, String src, String trg) {
|
||||
Db_qry qry = Db_qry_sql.ddl_(String_.Format("ALTER TABLE {0} RENAME TO {1};", src, trg));
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Pragma_page_size_4096(Db_provider p) {Pragma_page_size(p, 4096);}
|
||||
public static void Pragma_page_size(Db_provider p, int val) {
|
||||
Db_qry qry = Db_qry_sql.ddl_("PRAGMA page_size = " + Int_.XtoStr(val) + ";");
|
||||
p.Exec_qry(qry);
|
||||
}
|
||||
public static void Idx_create(Db_provider p, Db_idx_itm... idxs) {Idx_create(Gfo_usr_dlg_.Null, p, "", idxs);}
|
||||
public static void Idx_create(Gfo_usr_dlg usr_dlg, Db_provider p, String file_id, Db_idx_itm... idxs) {
|
||||
int len = idxs.length;
|
||||
p.Txn_mgr().Txn_end_all(); // commit any pending transactions
|
||||
for (int i = 0; i < len; i++) {
|
||||
p.Txn_mgr().Txn_bgn_if_none();
|
||||
String index = idxs[i].Xto_sql();
|
||||
usr_dlg.Prog_many("", "", "creating index: ~{0} ~{1}", file_id, index);
|
||||
p.Exec_qry(Db_qry_sql.ddl_(index));
|
||||
p.Txn_mgr().Txn_end_all();
|
||||
}
|
||||
}
|
||||
public static Db_provider Provider_load_or_fail_(Io_url url) {
|
||||
boolean exists = Io_mgr._.ExistsFil(url);
|
||||
if (!exists) throw Err_.new_fmt_("db does not exist; url=~{0}", url.Raw());
|
||||
Db_connect connect = Db_connect_sqlite.load_(url);
|
||||
return Db_provider_.new_(connect);
|
||||
}
|
||||
public static Db_provider Provider_load_or_make_(Io_url url) {return Provider_load_or_make_(url, Bool_obj_ref.n_());}
|
||||
public static Db_provider Provider_load_or_make_(Io_url url, Bool_obj_ref created) {
|
||||
boolean exists = Io_mgr._.ExistsFil(url);
|
||||
created.Val_(!exists);
|
||||
Db_connect connect = exists ? Db_connect_sqlite.load_(url) : Db_connect_sqlite.make_(url);
|
||||
Db_provider p = Db_provider_.new_(connect);
|
||||
if (!exists)
|
||||
Pragma_page_size(p, 4096);
|
||||
return p;
|
||||
}
|
||||
public static final int Stmt_arg_max = 999; // 999 is max number of variables allowed by sqlite
|
||||
public static final boolean Supports_read_binary_stream = false;
|
||||
public static final boolean Supports_indexed_by = true;
|
||||
public static String X_date_to_str(DateAdp v) {return v == Date_null ? "" : v.XtoStr_fmt_iso_8561();}
|
||||
public static final DateAdp Date_null = null;
|
||||
public static final byte Wildcard_byte = Byte_ascii.Hash;
|
||||
}
|
||||
129
140_dbs/src_300_sqlDbs/gplx/dbs/db_CrudOps_tst.java
Normal file
129
140_dbs/src_300_sqlDbs/gplx/dbs/db_CrudOps_tst.java
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
public class db_CrudOps_tst {
|
||||
CrudOpsFxt fx = new CrudOpsFxt();
|
||||
@Test public void Mysql() {if (Tfds.SkipDb) return;
|
||||
fx.RunAll(Db_provider_fxt.Mysql());
|
||||
}
|
||||
@Test public void Tdb() {if (Tfds.SkipDb) return;
|
||||
fx.RunAll(Db_provider_fxt.Tdb("100_dbs_crud_ops.dsv"));
|
||||
}
|
||||
@Test public void Postgres() {if (Db_provider_fxt.SkipPostgres) return;
|
||||
fx.RunAll(Db_provider_fxt.Postgres());
|
||||
}
|
||||
@Test public void Sqlite() {if (Tfds.SkipDb) return;
|
||||
fx.Fx().DmlAffectedAvailable_(false);
|
||||
fx.RunAll(Db_provider_fxt.Sqlite());
|
||||
}
|
||||
}
|
||||
class CrudOpsFxt {
|
||||
public Db_provider_fxt Fx() {return fx;} Db_provider_fxt fx = new Db_provider_fxt();
|
||||
void Init() {fx.ini_DeleteAll("dbs_crud_ops");}
|
||||
public void RunAll(Db_provider provider) {
|
||||
fx.Provider_(provider);
|
||||
Insert_hook();
|
||||
UpdateOne_hook();
|
||||
UpdateMany_hook();
|
||||
DeleteOne_hook();
|
||||
DeleteMany_hook();
|
||||
SelectLike_hook();
|
||||
SelectLikeForFileName_hook();
|
||||
InsertUnicode_hook();
|
||||
Backslash_hook();
|
||||
fx.Rls();
|
||||
}
|
||||
public void Insert_hook() {
|
||||
this.Init();
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "John Doe"));
|
||||
fx.tst_ExecRdrTbl(1, "dbs_crud_ops");
|
||||
fx.tst_RowAry(0, 1, "John Doe");
|
||||
}
|
||||
public void UpdateOne_hook() {
|
||||
this.Init();
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "John Doe"));
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 2).Arg_("name", "John Doe"));
|
||||
|
||||
fx.tst_ExecDml(1, Db_qry_.update_common_("dbs_crud_ops", Db_crt_.eq_("id", 2), KeyVal_.new_("name", "Jane Smith")));
|
||||
fx.tst_ExecRdrTbl(2, "dbs_crud_ops");
|
||||
fx.tst_RowAry(0, 1, "John Doe");
|
||||
fx.tst_RowAry(1, 2, "Jane Smith");
|
||||
}
|
||||
public void UpdateMany_hook() {
|
||||
this.Init();
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "John Doe"));
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 2).Arg_("name", "John Doe"));
|
||||
|
||||
fx.tst_ExecDml(2, Db_qry_.update_common_("dbs_crud_ops", Db_crt_.eq_("name", "John Doe"), KeyVal_.new_("name", "Jane Smith")));
|
||||
fx.tst_ExecRdrTbl(2, "dbs_crud_ops");
|
||||
fx.tst_RowAry(0, 1, "Jane Smith");
|
||||
fx.tst_RowAry(1, 2, "Jane Smith");
|
||||
}
|
||||
public void DeleteOne_hook() {
|
||||
this.Init();
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "John Doe"));
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 2).Arg_("name", "Jane Smith"));
|
||||
|
||||
fx.tst_ExecDml(1, Db_qry_.delete_("dbs_crud_ops", Db_crt_.eq_("id", 2)));
|
||||
fx.tst_ExecRdrTbl(1, "dbs_crud_ops");
|
||||
fx.tst_RowAry(0, 1, "John Doe");
|
||||
}
|
||||
public void DeleteMany_hook() {
|
||||
this.Init();
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "John Doe"));
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 2).Arg_("name", "Jane Smith"));
|
||||
|
||||
fx.tst_ExecDml(2, Db_qry_.delete_tbl_("dbs_crud_ops"));
|
||||
fx.tst_ExecRdrTbl(0, "dbs_crud_ops");
|
||||
}
|
||||
public void SelectLike_hook() {
|
||||
this.Init();
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "John Doe"));
|
||||
|
||||
fx.tst_ExecRdr(1, Db_qry_.select_cols_("dbs_crud_ops", Db_crt_.like_("name", "John%")));
|
||||
fx.tst_RowAry(0, 1, "John Doe");
|
||||
}
|
||||
public void SelectLikeForFileName_hook() {
|
||||
this.Init();
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "file%"));
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "file|%"));
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 1).Arg_("name", "file test"));
|
||||
|
||||
fx.tst_ExecRdr(1, Db_qry_.select_cols_("dbs_crud_ops", Db_crt_.like_("name", "file|%")));
|
||||
fx.tst_RowAry(0, 1, "file%");
|
||||
}
|
||||
public void InsertUnicode_hook() {
|
||||
this.Init();
|
||||
String val = "Ω";
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 3).Arg_obj_type_("name", val, Db_val_type.Tid_nvarchar));
|
||||
Db_qry_select select = Db_qry_.select_val_("dbs_crud_ops", "name", Db_crt_.eq_("id", 3));
|
||||
Tfds.Eq(val, ExecRdr_val(select));
|
||||
|
||||
fx.tst_ExecDml(1, Db_qry_.update_("dbs_crud_ops", Db_crt_.Wildcard).Arg_obj_type_("name", val + "a", Db_val_type.Tid_nvarchar));
|
||||
Tfds.Eq(val + "a", ExecRdr_val(select));
|
||||
}
|
||||
public void Backslash_hook() {
|
||||
this.Init();
|
||||
String val = "\\";
|
||||
fx.tst_ExecDml(1, Db_qry_.insert_("dbs_crud_ops").Arg_("id", 3).Arg_("name", val));
|
||||
Tfds.Eq(val, ExecRdr_val(Db_qry_.select_val_("dbs_crud_ops", "name", Db_crt_.eq_("id", 3))));
|
||||
Tfds.Eq(val, ExecRdr_val(Db_qry_.select_val_("dbs_crud_ops", "name", Db_crt_.eq_("name", "\\"))));
|
||||
}
|
||||
String ExecRdr_val(Db_qry_select select) {return (String)select.ExecRdr_val(fx.Provider());}
|
||||
}
|
||||
79
140_dbs/src_300_sqlDbs/gplx/dbs/db_DataTypes_tst.java
Normal file
79
140_dbs/src_300_sqlDbs/gplx/dbs/db_DataTypes_tst.java
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
public class db_DataTypes_tst {
|
||||
DataTypes_base_fxt fx = new DataTypes_base_fxt();
|
||||
@Test public void Mysql() {if (Tfds.SkipDb) return;
|
||||
fx.Select_FloatStr_("0.333333");
|
||||
fx.RunAll(Db_provider_fxt.Mysql());
|
||||
}
|
||||
@Test public void Tdb() {if (Tfds.SkipDb) return;
|
||||
fx.Select_FloatStr_(Float_.XtoStr(Float_.Div(1, 3)));
|
||||
fx.RunAll(Db_provider_fxt.Tdb("110_dbs_multiple_data_types.dsv"));
|
||||
}
|
||||
@Test public void Postgres() {if (Db_provider_fxt.SkipPostgres) return;
|
||||
fx.Select_FloatStr_("0.33333334");
|
||||
fx.RunAll(Db_provider_fxt.Postgres());
|
||||
}
|
||||
@Test public void Sqlite() {if (Tfds.SkipDb) return;
|
||||
fx.Select_FloatStr_("0.33333334");
|
||||
fx.RunAll(Db_provider_fxt.Sqlite());
|
||||
}
|
||||
/*
|
||||
DROP TABLE dbs_multiple_data_types;
|
||||
CREATE TABLE dbs_multiple_data_types (
|
||||
unique_id int
|
||||
, full_name varchar(255)
|
||||
, is_active bit
|
||||
, last_update timestamp
|
||||
, quantity float(24)
|
||||
, amount decimal(12, 3)
|
||||
);
|
||||
INSERT INTO dbs_multiple_data_types VALUES (1, 'John Doe', B'1', '3/30/2006 10:22 PM', 0.333333343, 12.345);
|
||||
*/
|
||||
}
|
||||
class DataTypes_base_fxt {
|
||||
public Db_provider Provider() {return provider;} Db_provider provider;
|
||||
public DataTypes_base_fxt() {}
|
||||
public void Rls() {provider.Rls();}
|
||||
public String Select_FloatStr() {return select_FloatStr;} public DataTypes_base_fxt Select_FloatStr_(String v) {select_FloatStr = v; return this;} private String select_FloatStr;
|
||||
public void RunAll(Db_provider provider) {
|
||||
this.provider = provider;
|
||||
this.Select_hook(select_FloatStr);
|
||||
provider.Rls();
|
||||
}
|
||||
public void Select_hook(String floatStr) {
|
||||
DataRdr rdr = Db_qry_.select_tbl_("dbs_multiple_data_types").Exec_qry_as_rdr(provider);
|
||||
|
||||
rdr.MoveNextPeer();
|
||||
Tfds.Eq(rdr.ReadInt("unique_id"), 1);
|
||||
Tfds.Eq(rdr.ReadStr("full_name"), "John Doe");
|
||||
Tfds.Eq(rdr.ReadBool("is_active"), true);
|
||||
Tfds.Eq_date(rdr.ReadDate("last_update"), DateAdp_.parse_gplx("2006-03-30 22:22:00.000"));
|
||||
Tfds.Eq(floatStr, Object_.XtoStr_OrEmpty(rdr.ReadFloat("quantity")));
|
||||
Tfds.Eq_decimal(rdr.ReadDecimal("amount"), DecimalAdp_.parts_(12, 345));
|
||||
}
|
||||
public void UpdateDate_hook() {
|
||||
provider.Exec_qry(Db_qry_.update_("dbs_multiple_data_types", Db_crt_.eq_("unique_id", 1)).Arg_obj_("last_update", DateAdpClassXtn._.XtoDb(DateAdp_.parse_gplx("20091115 220000.000"))));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_tbl_("dbs_multiple_data_types").Exec_qry_as_rdr(provider);
|
||||
rdr.MoveNextPeer();
|
||||
Tfds.Eq_date(rdr.ReadDate("last_update"), DateAdp_.parse_gplx("20091115 220000.000"));
|
||||
}
|
||||
}
|
||||
32
140_dbs/src_400_tdbs/gplx/dbs/TdbConnectInfo_tst.java
Normal file
32
140_dbs/src_400_tdbs/gplx/dbs/TdbConnectInfo_tst.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.*;
|
||||
import org.junit.*;
|
||||
public class TdbConnectInfo_tst {
|
||||
@Test public void Full() {
|
||||
Db_connect connectInfo = Db_connect_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb;format=dsv;");
|
||||
tst_Parse(connectInfo, Io_url_.new_any_("C:\\dir\\xmpl.tdb"), "dsv");
|
||||
}
|
||||
@Test public void DefaultFormat() {
|
||||
Db_connect connectInfo = Db_connect_.parse_("gplx_key=tdb;url=C:\\dir\\xmpl.tdb"); // dsv Format inferred
|
||||
tst_Parse(connectInfo, Io_url_.new_any_("C:\\dir\\xmpl.tdb"), "dsv");
|
||||
}
|
||||
void tst_Parse(Db_connect connectInfo, Io_url url, String format) {
|
||||
Tfds.Eq(((Db_connect_tdb)connectInfo).Url(), url);
|
||||
}
|
||||
}
|
||||
47
140_dbs/src_400_tdbs/gplx/dbs/TdbDatabase.java
Normal file
47
140_dbs/src_400_tdbs/gplx/dbs/TdbDatabase.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
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 TdbDatabase {
|
||||
public String Name() {return name;} public void Name_set(String v) {name = v;} private String name = "xmpl";
|
||||
public Io_url DbUrl() {return dbInfo;} Io_url dbInfo;
|
||||
public TdbFileList Files() {return files;} TdbFileList files;
|
||||
public TdbTableList Tables() {return tables;} TdbTableList tables;
|
||||
@gplx.Internal protected boolean IsNew() {return isNew;} @gplx.Internal protected void IsNew_set(boolean v) {isNew = v;} private boolean isNew;
|
||||
@gplx.Internal protected TdbFile MakeFile(Io_url url) {
|
||||
TdbFile rv = TdbFile.new_(FileId_next++, url);
|
||||
files.Add(rv);
|
||||
return rv;
|
||||
}
|
||||
@gplx.Internal protected TdbTable MakeTbl(String name, int fileId) {
|
||||
TdbFile file = files.FetchOrFail(fileId);
|
||||
TdbTable rv = TdbTable.new_(TableId_next++, name, file);
|
||||
tables.Add(rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static TdbDatabase new_(Io_url dbInfo) {TdbDatabase rv = new TdbDatabase(); rv.ctor(dbInfo); return rv;}
|
||||
void ctor(Io_url dbInfo) {
|
||||
this.dbInfo = dbInfo;
|
||||
TdbFile mainFile = TdbFile.new_(TdbFile.MainFileId, dbInfo);
|
||||
files = TdbFileList.new_(dbInfo, mainFile);
|
||||
tables = TdbTableList.new_(dbInfo);
|
||||
}
|
||||
int FileId_next = TdbFile.MainFileId + 1;
|
||||
int TableId_next = 1;
|
||||
// public static Io_url UrlOf(Db_connect url) {return Io_url_.new_any_(url.ServerName());}
|
||||
}
|
||||
49
140_dbs/src_400_tdbs/gplx/dbs/TdbDbLoadMgr.java
Normal file
49
140_dbs/src_400_tdbs/gplx/dbs/TdbDbLoadMgr.java
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
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.*;
|
||||
class TdbDbLoadMgr {
|
||||
public TdbDatabase LoadTbls(Io_url dbInfo) {
|
||||
TdbDatabase db = TdbDatabase.new_(dbInfo);
|
||||
if (!Io_mgr._.ExistsFil(dbInfo)) {
|
||||
db.IsNew_set(true);
|
||||
return db;
|
||||
}
|
||||
DataRdr rdr = MakeDataRdr(dbInfo);
|
||||
LoadTblsByRdr(db, rdr);
|
||||
return db;
|
||||
}
|
||||
public void LoadTbl(TdbDatabase db, TdbTable tbl) {
|
||||
DataRdr rootRdr = MakeDataRdr(tbl.File().Path());
|
||||
LoadTblsByRdr(db, rootRdr);
|
||||
}
|
||||
void LoadTblsByRdr(TdbDatabase db, DataRdr rootRdr) {
|
||||
DataRdr rdr = rootRdr.Subs();
|
||||
while (rdr.MoveNextPeer()) {
|
||||
String name = rdr.NameOfNode();
|
||||
if (String_.Eq(name, TdbFileList.StoreTblName)) db.Files().DataObj_Rdr(rdr);
|
||||
else if (String_.Eq(name, TdbTableList.StoreTableName)) db.Tables().DataObj_Rdr(rdr, db.Files());
|
||||
else db.Tables().FetchOrFail(rdr.NameOfNode()).DataObj_Rdr(rdr);
|
||||
}
|
||||
if (db.Files().Count() == 0) throw Err_.new_("fatal error: db has no files").Add("connectInfo", db.DbUrl());
|
||||
}
|
||||
DataRdr MakeDataRdr(Io_url fil) {
|
||||
String text = Io_mgr._.LoadFilStr(fil);
|
||||
return TdbStores.rdr_(text);
|
||||
}
|
||||
public static TdbDbLoadMgr new_() {return new TdbDbLoadMgr();} TdbDbLoadMgr() {}
|
||||
}
|
||||
101
140_dbs/src_400_tdbs/gplx/dbs/TdbDbLoadMgr_tst.java
Normal file
101
140_dbs/src_400_tdbs/gplx/dbs/TdbDbLoadMgr_tst.java
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
import gplx.stores.*; /*DsvDataRdr*/ import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
||||
public class TdbDbLoadMgr_tst {
|
||||
@Before public void setup() {
|
||||
Io_url dbInfo = Io_url_.mem_fil_("mem/dir/db0.dsv");
|
||||
db = TdbDatabase.new_(dbInfo);
|
||||
wtr = DsvDataWtr_.new_();
|
||||
}
|
||||
TdbDatabase db; TdbDbLoadMgr loadMgr = TdbDbLoadMgr.new_(); TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_();
|
||||
DataRdr rdr; DataWtr wtr;
|
||||
@Test public void ReadDbFiles() {
|
||||
String raw = String_.Concat_lines_crlf
|
||||
( "=======DIF======================, ,\" \",//"
|
||||
, "_files, ,\" \",#"
|
||||
, "==DEF==DIF======================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + "," + StringClassXtn.Key_const + ", ,\" \",$"
|
||||
, "id,url,format, ,\" \",@"
|
||||
, "==DATA=DIF======================, ,\" \",//"
|
||||
, "1,mem/dir/db0.dsv,dsv"
|
||||
, "2,C:\\file.dsv,dsv"
|
||||
);
|
||||
rdr = rdr_(raw);
|
||||
|
||||
db.Files().DataObj_Rdr(rdr);
|
||||
Tfds.Eq(db.Files().Count(), 2);
|
||||
TdbFile file2 = db.Files().FetchOrFail(2);
|
||||
Tfds.Eq(file2.Path().Raw(), "C:\\file.dsv");
|
||||
|
||||
db.Files().DataObj_Wtr(wtr);
|
||||
Tfds.Eq(wtr.XtoStr(), raw);
|
||||
}
|
||||
@Test public void ReadDbTbls() {
|
||||
String raw = String_.Concat_lines_crlf
|
||||
( "=======DIF======================, ,\" \",//"
|
||||
, "_tables, ,\" \",#"
|
||||
, "==DEF==DIF======================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + ",int, ,\" \",$"
|
||||
, "id,name,file_id, ,\" \",@"
|
||||
, "==DATA=DIF======================, ,\" \",//"
|
||||
, "1,tbl1,1"
|
||||
);
|
||||
rdr = rdr_(raw);
|
||||
|
||||
db.Tables().DataObj_Rdr(rdr, db.Files());
|
||||
Tfds.Eq(db.Tables().Count(), 1);
|
||||
TdbTable table = db.Tables().FetchOrFail("tbl1");
|
||||
Tfds.Eq(table.Name(), "tbl1");
|
||||
Tfds.Eq(table.File().Id(), 1);
|
||||
|
||||
db.Tables().DataObj_Wtr(wtr);
|
||||
Tfds.Eq(wtr.XtoStr(), raw);
|
||||
}
|
||||
@Test public void ReadTbl() {
|
||||
String raw = String_.Concat_lines_crlf
|
||||
( "=======DIF======================, ,\" \",//"
|
||||
, "tbl0, ,\" \",#"
|
||||
, "==DEF==DIF======================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + ", ,\" \",$"
|
||||
, "id,name, ,\" \",@"
|
||||
, "==DATA=DIF======================, ,\" \",//"
|
||||
, "0,me"
|
||||
);
|
||||
rdr = rdr_(raw);
|
||||
|
||||
db.MakeTbl("tbl0", TdbFile.MainFileId);
|
||||
db.Tables().FetchOrFail(rdr.NameOfNode()).DataObj_Rdr(rdr);
|
||||
Tfds.Eq(db.Tables().Count(), 1);
|
||||
TdbTable tbl = db.Tables().FetchOrFail("tbl0");
|
||||
Tfds.Eq(tbl.Rows().Count(), 1);
|
||||
|
||||
GfoNde row = tbl.Rows().FetchAt_asGfoNde(0);
|
||||
Tfds.Eq(row.Read("id"), 0);
|
||||
Tfds.Eq(row.Read("name"), "me");
|
||||
|
||||
tbl.DataObj_Wtr(wtr);
|
||||
Tfds.Eq(wtr.XtoStr(), raw);
|
||||
}
|
||||
DataRdr rdr_(String raw) {
|
||||
DataRdr rdr = DsvDataRdr_.dsv_(raw);
|
||||
rdr.MoveNextPeer(); // must move next as cur is not set and ReadProcs assume cur is set
|
||||
return rdr;
|
||||
}
|
||||
}
|
||||
63
140_dbs/src_400_tdbs/gplx/dbs/TdbDbSaveMgr.java
Normal file
63
140_dbs/src_400_tdbs/gplx/dbs/TdbDbSaveMgr.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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.*;
|
||||
class TdbDbSaveMgr {
|
||||
public void SaveDb(TdbDatabase db) {
|
||||
for (Object filObj : db.Files()) {
|
||||
TdbFile fil = (TdbFile)filObj;
|
||||
SaveFile(db, fil);
|
||||
}
|
||||
}
|
||||
public void SaveFile(TdbDatabase db, TdbFile fil) {
|
||||
ListAdp tbls = FetchTablesWithSamePath(db, fil.Path());
|
||||
boolean isSaveNeeded = db.IsNew();
|
||||
for (Object tblObj : tbls) {
|
||||
TdbTable tbl = (TdbTable)tblObj;
|
||||
if (tbl.IsDirty()) {
|
||||
isSaveNeeded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isSaveNeeded) {
|
||||
SaveTblsToFile(db, fil, tbls);
|
||||
db.IsNew_set(false);
|
||||
}
|
||||
}
|
||||
void SaveTblsToFile(TdbDatabase db, TdbFile fil, ListAdp tbls) {
|
||||
DataWtr wtr = TdbStores.wtr_();
|
||||
if (fil.Id() == TdbFile.MainFileId) { // if MainFile, save critical Files and Tables data
|
||||
db.Files().DataObj_Wtr(wtr);
|
||||
db.Tables().DataObj_Wtr(wtr);
|
||||
}
|
||||
for (Object tblObj : tbls) {
|
||||
TdbTable tbl = (TdbTable)tblObj;
|
||||
tbl.DataObj_Wtr(wtr);
|
||||
}
|
||||
Io_mgr._.SaveFilStr(fil.Path(), wtr.XtoStr());
|
||||
}
|
||||
ListAdp FetchTablesWithSamePath(TdbDatabase db, Io_url filPath) {
|
||||
ListAdp list = ListAdp_.new_();
|
||||
for (Object tblObj : db.Tables()) {
|
||||
TdbTable tbl = (TdbTable)tblObj;
|
||||
if (tbl.File().Path().Eq (filPath))
|
||||
list.Add(tbl);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
public static TdbDbSaveMgr new_() {return new TdbDbSaveMgr();} TdbDbSaveMgr() {}
|
||||
}
|
||||
77
140_dbs/src_400_tdbs/gplx/dbs/TdbDbSaveMgr_tst.java
Normal file
77
140_dbs/src_400_tdbs/gplx/dbs/TdbDbSaveMgr_tst.java
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
||||
public class TdbDbSaveMgr_tst {
|
||||
@Before public void setup() {
|
||||
Io_url dbInfo = Io_url_.mem_fil_("mem/dir/db0.dsv");
|
||||
db = TdbDatabase.new_(dbInfo);
|
||||
wtr.Clear();
|
||||
} TdbDatabase db; TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_(); DataWtr wtr = DsvDataWtr_.new_();
|
||||
@Test public void WriteDbFils() {
|
||||
String expd = String_.Concat_lines_crlf
|
||||
( ""
|
||||
, ""
|
||||
, "================================, ,\" \",//"
|
||||
, "_files, ,\" \",#"
|
||||
, "================================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + "," + StringClassXtn.Key_const + ", ,\" \",$"
|
||||
, "id,url,format, ,\" \",@"
|
||||
, "================================, ,\" \",//"
|
||||
, "1,mem/dir/db0.dsv,dsv"
|
||||
);
|
||||
db.Files().DataObj_Wtr(wtr);
|
||||
String actl = wtr.XtoStr();
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
@Test public void WriteDbTbls() {
|
||||
String expd = String_.Concat_lines_crlf
|
||||
( ""
|
||||
, ""
|
||||
, "================================, ,\" \",//"
|
||||
, "_tables, ,\" \",#"
|
||||
, "================================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + ",int, ,\" \",$"
|
||||
, "id,name,file_id, ,\" \",@"
|
||||
, "================================, ,\" \",//"
|
||||
);
|
||||
db.Tables().DataObj_Wtr(wtr);
|
||||
String actl = wtr.XtoStr();
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
@Test public void WriteTbl() {
|
||||
String expd = String_.Concat_lines_crlf
|
||||
( ""
|
||||
, ""
|
||||
, "================================, ,\" \",//"
|
||||
, "tbl, ,\" \",#"
|
||||
, "================================, ,\" \",//"
|
||||
, "int," + StringClassXtn.Key_const + ", ,\" \",$"
|
||||
, "id,name, ,\" \",@"
|
||||
, "================================, ,\" \",//"
|
||||
);
|
||||
TdbTable tbl = db.MakeTbl("tbl", TdbFile.MainFileId);
|
||||
tbl.Flds().Add("id", IntClassXtn._);
|
||||
tbl.Flds().Add("name", StringClassXtn._);
|
||||
|
||||
tbl.DataObj_Wtr(wtr);
|
||||
String actl = wtr.XtoStr();
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
}
|
||||
48
140_dbs/src_400_tdbs/gplx/dbs/TdbDelete.java
Normal file
48
140_dbs/src_400_tdbs/gplx/dbs/TdbDelete.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.lists.*; /*GfoNde*/
|
||||
class TdbDeleteWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_delete cmd = (Db_qry_delete)cmdObj;
|
||||
TdbTable tbl = engine.FetchTbl(cmd.BaseTable());
|
||||
ListAdp deleted = ListAdp_.new_();
|
||||
int rv = 0;
|
||||
if (cmd.Where() == Db_qry_.WhereAll) {
|
||||
rv = tbl.Rows().Count();
|
||||
tbl.Rows().Clear();
|
||||
}
|
||||
else {
|
||||
Criteria crt = cmd.Where().Crt();
|
||||
for (int i = 0; i < tbl.Rows().Count(); i++) {
|
||||
GfoNde row = tbl.Rows().FetchAt_asGfoNde(i);
|
||||
if (crt.Matches(row))
|
||||
deleted.Add(row);
|
||||
}
|
||||
for (int i = 0; i < deleted.Count(); i++) {
|
||||
GfoNde row = (GfoNde)deleted.FetchAt(i);
|
||||
tbl.Rows().Del(row);
|
||||
rv++;
|
||||
}
|
||||
}
|
||||
if (rv > 0) tbl.IsDirty_set(true);
|
||||
return rv;
|
||||
}
|
||||
public static TdbDeleteWkr new_() {TdbDeleteWkr rv = new TdbDeleteWkr(); return rv;}
|
||||
}
|
||||
67
140_dbs/src_400_tdbs/gplx/dbs/TdbEngine.java
Normal file
67
140_dbs/src_400_tdbs/gplx/dbs/TdbEngine.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
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.*;
|
||||
class TdbEngine implements Db_engine {
|
||||
public String Key() {return KeyDef;} public static final String KeyDef = "tdb";
|
||||
public Db_connect ConnectInfo() {return connectInfo;} Db_connect connectInfo;
|
||||
public TdbDatabase Db() {return db;} TdbDatabase db;
|
||||
public Db_engine MakeEngine(Db_connect connectInfo) {
|
||||
TdbEngine rv = new TdbEngine();
|
||||
rv.CtorTdbEngine(connectInfo);
|
||||
return rv;
|
||||
}
|
||||
public void Connect() {
|
||||
String urlStr = (String)connectInfo.Server();
|
||||
db = loadMgr.LoadTbls(Io_url_.new_any_(urlStr));
|
||||
}
|
||||
public Object Execute(Db_qry cmd) {
|
||||
Db_qryWkr wkr = (Db_qryWkr)wkrs.FetchOrFail(cmd.KeyOfDb_qry());
|
||||
return wkr.Exec(this, cmd);
|
||||
}
|
||||
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 new Db_stmt_sql().Parse(Sql_cmd_wtr_.Ansi.XtoSqlQry(qry, true));}
|
||||
public Object New_db_cmd(String sql) {throw Err_.not_implemented_();}
|
||||
public void Txn_bgn() {}
|
||||
public void Txn_end() {}
|
||||
public TdbTable FetchTbl(String name) {
|
||||
TdbTable tbl = db.Tables().FetchOrFail(name);
|
||||
if (!tbl.IsLoaded()) loadMgr.LoadTbl(db, tbl);
|
||||
return tbl;
|
||||
}
|
||||
public void FlushAll() {
|
||||
saveMgr.SaveDb(db);
|
||||
}
|
||||
public void FlushTbl(TdbTable tbl) {
|
||||
saveMgr.SaveFile(db, tbl.File());
|
||||
}
|
||||
public void Rls() {}
|
||||
|
||||
HashAdp wkrs = HashAdp_.new_(); TdbDbLoadMgr loadMgr = TdbDbLoadMgr.new_(); TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_();
|
||||
@gplx.Internal protected static final TdbEngine _ = new TdbEngine();
|
||||
//TdbEngine() {this.connectInfo = TdbConnectUrl._;}
|
||||
void CtorTdbEngine(Db_connect connectInfo) {
|
||||
this.connectInfo = connectInfo;
|
||||
wkrs.Add(Db_qry_select.KeyConst, TdbSelectWkr._);
|
||||
wkrs.Add(Db_qry_insert.KeyConst, TdbInsertWkr.new_());
|
||||
wkrs.Add(Db_qry_update.KeyConst, TdbUpdateWkr.new_());
|
||||
wkrs.Add(Db_qry_delete.KeyConst, TdbDeleteWkr.new_());
|
||||
wkrs.Add(Db_qry_flush.KeyConst, TdbFlushWkr.new_());
|
||||
}
|
||||
public static TdbEngine as_(Object obj) {return obj instanceof TdbEngine ? (TdbEngine)obj : null;}
|
||||
public static TdbEngine cast_(Object obj) {try {return (TdbEngine)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, TdbEngine.class, obj);}}
|
||||
}
|
||||
31
140_dbs/src_400_tdbs/gplx/dbs/TdbFile.java
Normal file
31
140_dbs/src_400_tdbs/gplx/dbs/TdbFile.java
Normal file
@@ -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; import gplx.*;
|
||||
public class TdbFile {
|
||||
public int Id() {return id;} int id;
|
||||
public Io_url Path() {return url;} Io_url url;
|
||||
|
||||
public static TdbFile new_(int id, Io_url url) {
|
||||
TdbFile rv = new TdbFile();
|
||||
rv.id = id; rv.url = url;
|
||||
return rv;
|
||||
}
|
||||
public static final int MainFileId = 1;
|
||||
public static TdbFile as_(Object obj) {return obj instanceof TdbFile ? (TdbFile)obj : null;}
|
||||
public static TdbFile cast_(Object obj) {try {return (TdbFile)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, TdbFile.class, obj);}}
|
||||
}
|
||||
63
140_dbs/src_400_tdbs/gplx/dbs/TdbFileList.java
Normal file
63
140_dbs/src_400_tdbs/gplx/dbs/TdbFileList.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.lists.*; /*OrderedHash_base*/ import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbFileList extends OrderedHash_base {
|
||||
public TdbFile FetchOrFail(int id) {return TdbFile.as_(FetchOrFail_base(id));}
|
||||
public void Add(TdbFile src) {Add_base(src.Id(), src);}
|
||||
|
||||
Io_url dbInfo;
|
||||
public static TdbFileList new_(Io_url dbInfo, TdbFile mainFile) {
|
||||
TdbFileList rv = new TdbFileList();
|
||||
rv.dbInfo = dbInfo;
|
||||
rv.Add(mainFile);
|
||||
rv.layout = DsvStoreLayout.dsv_full_();
|
||||
return rv;
|
||||
} TdbFileList() {}
|
||||
|
||||
@gplx.Internal protected void DataObj_Wtr(DataWtr wtr) {
|
||||
wtr.InitWtr(DsvStoreLayout.Key_const, layout);
|
||||
wtr.WriteTableBgn(StoreTblName, FldList);
|
||||
for (Object filObj : this) {
|
||||
TdbFile fil = (TdbFile)filObj;
|
||||
wtr.WriteLeafBgn("fil");
|
||||
wtr.WriteData(Fld_id, fil.Id());
|
||||
wtr.WriteData(Fld_path, fil.Path());
|
||||
wtr.WriteData("format", "dsv");
|
||||
wtr.WriteLeafEnd();
|
||||
}
|
||||
wtr.WriteNodeEnd();
|
||||
}
|
||||
@gplx.Internal protected void DataObj_Rdr(DataRdr rdr) {
|
||||
layout = TdbStores.FetchLayout(rdr);
|
||||
this.Clear();
|
||||
DataRdr subRdr = rdr.Subs();
|
||||
while (subRdr.MoveNextPeer()) {
|
||||
int id = subRdr.ReadInt(Fld_id);
|
||||
String url = subRdr.ReadStrOr(Fld_path, dbInfo.Raw());
|
||||
TdbFile file = (id == TdbFile.MainFileId)
|
||||
? TdbFile.new_(TdbFile.MainFileId, dbInfo) // not redundant; prevents error of MainFile in different url/format than connectInfo
|
||||
: TdbFile.new_(id, Io_url_.new_any_(url));
|
||||
this.Add(file);
|
||||
}
|
||||
}
|
||||
DsvStoreLayout layout;
|
||||
public static final String StoreTblName = "_files";
|
||||
static final String Fld_id = "id"; static final String Fld_path = "url";
|
||||
static final GfoFldList FldList = GfoFldList_.new_().Add(Fld_id, IntClassXtn._).Add(Fld_path, StringClassXtn._).Add("format", StringClassXtn._);
|
||||
}
|
||||
34
140_dbs/src_400_tdbs/gplx/dbs/TdbFlush.java
Normal file
34
140_dbs/src_400_tdbs/gplx/dbs/TdbFlush.java
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.lists.*; /*GfoNde*/
|
||||
class TdbFlushWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_flush cmd = Db_qry_flush.cast_(cmdObj);
|
||||
if (Array_.Len(cmd.TableNames()) == 0)
|
||||
engine.FlushAll();
|
||||
else {
|
||||
for (String tblName : cmd.TableNames()) {
|
||||
TdbTable tbl = engine.FetchTbl(tblName);
|
||||
if (tbl.IsDirty()) engine.FlushTbl(tbl);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
public static TdbFlushWkr new_() {TdbFlushWkr rv = new TdbFlushWkr(); return rv;}
|
||||
}
|
||||
119
140_dbs/src_400_tdbs/gplx/dbs/TdbFlush_tst.java
Normal file
119
140_dbs/src_400_tdbs/gplx/dbs/TdbFlush_tst.java
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
import gplx.ios.*; /*IoMgrFxt*/
|
||||
public class TdbFlush_tst {
|
||||
@Before public void setup() {
|
||||
Io_mgr._.InitEngine_mem();
|
||||
engine = fx_engine.run_MakeEngine(dbPath);
|
||||
}
|
||||
TdbEngine engine; Io_url dbPath = Io_url_.mem_fil_("mem/dir/db0.dsv"); DateAdp time = DateAdp_.parse_gplx("2001-01-01");
|
||||
TdbEngineFxt fx_engine = TdbEngineFxt.new_(); IoMgrFxt fx_io = IoMgrFxt.new_();
|
||||
@Test public void FlushNewDb() {
|
||||
fx_engine.tst_FilesCount(engine, 1);
|
||||
fx_engine.tst_File(engine, 0, TdbFile.MainFileId, Io_url_.mem_fil_("mem/dir/db0.dsv"), "dsv");
|
||||
fx_io.tst_Exists(false, dbPath);
|
||||
|
||||
engine.FlushAll();
|
||||
fx_io.tst_Exists(true, dbPath);
|
||||
}
|
||||
@Test public void IgnoreFlushedDb() {
|
||||
engine.FlushAll();
|
||||
fx_io.tst_Exists(true, dbPath);
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time);
|
||||
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(true, dbPath, time);
|
||||
}
|
||||
@Test public void FlushNewTbl() {
|
||||
engine.FlushAll();
|
||||
fx_engine.run_MakeTbl(engine, "tbl0", TdbFile.MainFileId);
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time);
|
||||
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(false, dbPath, time);
|
||||
}
|
||||
@Test public void IgnoreFlushedTbl() {
|
||||
fx_engine.run_MakeTbl(engine, "tbl0", TdbFile.MainFileId);
|
||||
engine.FlushAll();
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time);
|
||||
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(true, dbPath, time);
|
||||
}
|
||||
@Test public void FlushDirtyTbl() {
|
||||
fx_engine.run_MakeTbl(engine, "tbl0", TdbFile.MainFileId);
|
||||
engine.FlushAll();
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time);
|
||||
|
||||
fx_engine.run_InsertRow(engine, "tbl0", 1);
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(false, dbPath, time);
|
||||
}
|
||||
@Test public void FlushDirtyFilOnly() {
|
||||
Io_url dbPathOther = Io_url_.mem_fil_("mem/dir/db1.dsv");
|
||||
TdbFile filOther = fx_engine.run_MakeFile(engine, dbPathOther); Tfds.Eq(false, Object_.Eq(filOther.Id(), TdbFile.MainFileId));
|
||||
fx_engine.run_MakeTbl(engine, "tbl0", TdbFile.MainFileId); fx_engine.run_MakeTbl(engine, "tbl1", filOther.Id());
|
||||
engine.FlushAll();
|
||||
fx_io.run_UpdateFilModifiedTime(dbPath, time); fx_io.run_UpdateFilModifiedTime(dbPathOther, time);
|
||||
|
||||
fx_engine.run_InsertRow(engine, "tbl1", 1);
|
||||
engine.FlushAll();
|
||||
fx_io.tst_QueryFilModified(true, dbPath, time);
|
||||
fx_io.tst_QueryFilModified(false, dbPathOther, time);
|
||||
}
|
||||
}
|
||||
class TdbEngineFxt {
|
||||
public TdbEngine run_MakeEngine(Io_url url) {
|
||||
Db_connect connectInfo = Db_connect_.tdb_(url);
|
||||
TdbEngine engine = (TdbEngine)TdbEngine._.MakeEngine(connectInfo);
|
||||
engine.Connect();
|
||||
return engine;
|
||||
}
|
||||
public TdbFile run_MakeFile(TdbEngine engine, Io_url url) {return engine.Db().MakeFile(url);}
|
||||
public TdbTable run_MakeTbl(TdbEngine engine, String tblName, int srcId) {
|
||||
TdbTable rv = engine.Db().MakeTbl(tblName, srcId);
|
||||
rv.Flds().Add("id", IntClassXtn._);
|
||||
return rv;
|
||||
}
|
||||
public void run_InsertRow(TdbEngine engine, String tblName, int idVal) {
|
||||
Db_qry_insert cmd = Db_qry_insert.new_().BaseTable_(tblName);
|
||||
cmd.Arg_("id", idVal);
|
||||
engine.Execute(cmd);
|
||||
}
|
||||
|
||||
public void tst_FilesCount(TdbEngine engine, int count) {Tfds.Eq(engine.Db().Files().Count(), count);}
|
||||
public void tst_File(TdbEngine engine, int index, int id, Io_url url, String format) {
|
||||
TdbFile src = engine.Db().Files().FetchOrFail(id);
|
||||
Tfds.Eq(src.Path().Raw(), url.Raw());
|
||||
}
|
||||
public static TdbEngineFxt new_() {return new TdbEngineFxt();} TdbEngineFxt() {}
|
||||
}
|
||||
class IoMgrFxt {
|
||||
public void run_UpdateFilModifiedTime(Io_url url, DateAdp val) {Io_mgr._.UpdateFilModifiedTime(url, val);}
|
||||
public void tst_QueryFilModified(boolean expdMatch, Io_url url, DateAdp expt) {
|
||||
IoItmFil filItem = Io_mgr._.QueryFil(url);
|
||||
DateAdp actl = filItem.ModifiedTime();
|
||||
boolean actlMatch = String_.Eq(expt.XtoStr_gplx(), actl.XtoStr_gplx());
|
||||
Tfds.Eq(expdMatch, actlMatch, expt.XtoStr_gplx() + (expdMatch ? "!=" : "==") + actl.XtoStr_gplx());
|
||||
}
|
||||
public void tst_Exists(boolean expd, Io_url url) {Tfds.Eq(expd, Io_mgr._.ExistsFil(url));}
|
||||
|
||||
public static IoMgrFxt new_() {return new IoMgrFxt();} IoMgrFxt() {}
|
||||
}
|
||||
59
140_dbs/src_400_tdbs/gplx/dbs/TdbInsert.java
Normal file
59
140_dbs/src_400_tdbs/gplx/dbs/TdbInsert.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.lists.*; /*GfoNde*/
|
||||
class TdbInsertWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_insert cmd = (Db_qry_insert)cmdObj;
|
||||
|
||||
TdbTable tbl = engine.FetchTbl(cmd.BaseTable());
|
||||
tbl.IsDirty_set(true);
|
||||
return cmd.Select() == null
|
||||
? InsertRowsByVals(engine, tbl, cmd)
|
||||
: InsertRowsBySelect(engine, tbl, cmd);
|
||||
}
|
||||
int InsertRowsBySelect(TdbEngine engine, TdbTable tbl, Db_qry_insert insert) {
|
||||
int count = 0;
|
||||
DataRdr rdr = (DataRdr)TdbSelectWkr._.Exec(engine, insert.Select());
|
||||
Sql_select_fld_list insertFlds = insert.Cols(); int insertFldsCount = insertFlds.Count();
|
||||
GfoFldList selectFldsForNewRow = null;
|
||||
try {selectFldsForNewRow = insertFlds.XtoGfoFldLst(tbl);}
|
||||
catch (Exception e) {throw Err_.err_(e, "failed to generate flds for new row");}
|
||||
if (insertFldsCount > selectFldsForNewRow.Count()) throw Err_.new_("insert flds cannot exceed selectFlds").Add("insertFlds", insertFlds.XtoStr()).Add("selectFlds", selectFldsForNewRow.XtoStr());
|
||||
while (rdr.MoveNextPeer()) {
|
||||
count++;
|
||||
GfoNde row = GfoNde_.vals_(selectFldsForNewRow, new Object[insertFldsCount]);
|
||||
for (int i = 0; i < insertFldsCount; i++) {
|
||||
row.WriteAt(i, rdr.ReadAt(i)); // NOTE: SELECT and INSERT flds are in same order; ex: INSERT INTO (a, b) SELECT (b, a)
|
||||
}
|
||||
tbl.Rows().Add(row);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
int InsertRowsByVals(TdbEngine engine, TdbTable tbl, Db_qry_insert insert) {
|
||||
GfoNde row = GfoNde_.vals_(tbl.Flds(), new Object[tbl.Flds().Count()]);
|
||||
for (int i = 0; i < insert.Args().Count(); i++) {
|
||||
KeyVal kv = insert.Args().FetchAt(i);
|
||||
Db_arg arg = (Db_arg)kv.Val();
|
||||
row.Write(kv.Key(), arg.Val());
|
||||
}
|
||||
tbl.Rows().Add(row);
|
||||
return 1;
|
||||
}
|
||||
public static TdbInsertWkr new_() {TdbInsertWkr rv = new TdbInsertWkr(); return rv;}
|
||||
}
|
||||
99
140_dbs/src_400_tdbs/gplx/dbs/TdbSelect.java
Normal file
99
140_dbs/src_400_tdbs/gplx/dbs/TdbSelect.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.lists.*; /*ComparerAble*/ import gplx.stores.*; /*GfoNdeRdr*/
|
||||
class TdbSelectWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_select cmd = (Db_qry_select)cmdObj;
|
||||
if (cmd.From().Tbls().Count() > 1) throw Err_.new_key_("gplx.tdbs", "joins not supported for tdbs").Add("sql", cmd.XtoSql());
|
||||
|
||||
TdbTable tbl = engine.FetchTbl(cmd.From().BaseTable().TblName());
|
||||
GfoNdeList rv = (cmd.Where() == Db_qry_.WhereAll && cmd.Limit() == Db_qry_select.Limit_disabled) ? rv = tbl.Rows() : FilterRecords(tbl, cmd.Where().Crt(), cmd.Limit());
|
||||
if (cmd.GroupBy() != null)
|
||||
rv = TdbGroupByWkr.GroupByExec(cmd, rv, tbl);
|
||||
if (cmd.OrderBy() != null) { // don't use null pattern here; if null ORDER BY, then don't call .Sort on GfoNdeList
|
||||
ComparerAble comparer = Sql_order_by_sorter.new_((Sql_order_by_itm[])cmd.OrderBy().Flds().XtoAry(Sql_order_by_itm.class));
|
||||
rv.SortBy(comparer);
|
||||
}
|
||||
return GfoNdeRdr_.peers_(rv, false);
|
||||
}
|
||||
GfoNdeList FilterRecords(TdbTable tbl, Criteria crt, int limit) {
|
||||
GfoNdeList rv = GfoNdeList_.new_();
|
||||
int count = 0;
|
||||
for (int i = 0; i < tbl.Rows().Count(); i++) {
|
||||
GfoNde row = (GfoNde)tbl.Rows().FetchAt_asGfoNde(i);
|
||||
if (crt.Matches(row)) rv.Add(row);
|
||||
++count;
|
||||
if (count == limit) break;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
public static final TdbSelectWkr _ = new TdbSelectWkr(); TdbSelectWkr() {}
|
||||
}
|
||||
class TdbGroupByWkr {
|
||||
public static GfoNdeList GroupByExec(Db_qry_select select, GfoNdeList selectRows, TdbTable tbl) {
|
||||
GfoNdeList rv = GfoNdeList_.new_();
|
||||
OrderedHash groupByHash = OrderedHash_.new_();
|
||||
ListAdp groupByFlds = select.GroupBy().Flds();
|
||||
GfoFldList selectFldsForNewRow = select.Cols().Flds().XtoGfoFldLst(tbl);
|
||||
Sql_select_fld_list selectFlds = select.Cols().Flds();
|
||||
for (int rowIdx = 0; rowIdx < selectRows.Count(); rowIdx++) {
|
||||
GfoNde selectRow = selectRows.FetchAt_asGfoNde(rowIdx);
|
||||
GfoNde groupByRow = FindOrNew(selectFldsForNewRow, groupByFlds, selectRow, groupByHash, rv);
|
||||
for (int i = 0; i < selectFlds.Count(); i++) {
|
||||
Sql_select_fld_base selectFld = selectFlds.FetchAt(i);
|
||||
Object val = groupByRow.Read(selectFld.Alias()); // groupByRow is keyed by Alias; EX: Count(Id) AS CountOf
|
||||
groupByRow.WriteAt(i, selectFld.GroupBy_eval(val, selectRow.Read(selectFld.Fld()), selectFld.ValType()));
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
static GfoNde FindOrNew(GfoFldList selectFldsForNewRow, ListAdp groupByFlds, GfoNde selectRow, OrderedHash groupByRows, GfoNdeList rslt) {
|
||||
int len = groupByFlds.Count();
|
||||
OrderedHash curHash = groupByRows;
|
||||
GfoNde rv = null;
|
||||
for (int i = 0; i < len; i++) {
|
||||
String fld = (String)groupByFlds.FetchAt(i);
|
||||
boolean last = i == len - 1;
|
||||
Object val = selectRow.Read(fld);
|
||||
Object o = curHash.Fetch(val);
|
||||
if (last) {
|
||||
if (o == null) {
|
||||
Object[] valAry = new Object[selectFldsForNewRow.Count()];
|
||||
rv = GfoNde_.vals_(selectFldsForNewRow, valAry);
|
||||
curHash.Add(val, rv);
|
||||
rslt.Add(rv);
|
||||
}
|
||||
else
|
||||
rv = GfoNde_.as_(o);
|
||||
}
|
||||
else {
|
||||
if (o == null) {
|
||||
OrderedHash nextHash = OrderedHash_.new_();
|
||||
curHash.Add(val, nextHash);
|
||||
curHash = nextHash;
|
||||
}
|
||||
else {
|
||||
curHash = (OrderedHash)o;
|
||||
}
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
32
140_dbs/src_400_tdbs/gplx/dbs/TdbStores.java
Normal file
32
140_dbs/src_400_tdbs/gplx/dbs/TdbStores.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.*;
|
||||
import gplx.stores.*;
|
||||
import gplx.stores.xmls.*; /*XmlDataRdr*/
|
||||
import gplx.stores.dsvs.*; /*DsvDataWtr*/
|
||||
import gplx.lists.*; /*GfoNdeRdr*/
|
||||
class TdbStores {
|
||||
public static final String Dsv = "dsv";
|
||||
public static final String Xml = "xml";
|
||||
public static DataRdr rdr_(String text) {return DsvDataRdr_.dsv_(text);}
|
||||
public static DataWtr wtr_() {return DsvDataWtr_.new_();}
|
||||
@gplx.Internal protected static DsvStoreLayout FetchLayout(DataRdr rdr) {
|
||||
GfoNdeRdr ndeRdr = GfoNdeRdr_.as_(rdr); if (ndeRdr == null) return null; // can happen for non-Dsv Rdrs (ex: Xml)
|
||||
return DsvStoreLayout.as_(ndeRdr.UnderNde().EnvVars().Fetch(DsvStoreLayout.Key_const));
|
||||
}
|
||||
}
|
||||
65
140_dbs/src_400_tdbs/gplx/dbs/TdbTable.java
Normal file
65
140_dbs/src_400_tdbs/gplx/dbs/TdbTable.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.*; /*GfoNdeList*/ import gplx.stores.*; import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbTable {
|
||||
public int Id() {return id;} int id;
|
||||
public String Name() {return name;} private String name;
|
||||
public GfoFldList Flds() {return flds;} GfoFldList flds = GfoFldList_.new_();
|
||||
public GfoNdeList Rows() {return rows;} GfoNdeList rows = GfoNdeList_.new_();
|
||||
@gplx.Internal protected TdbFile File() {return file;} TdbFile file;
|
||||
@gplx.Internal protected boolean IsLoaded() {return isLoaded;} private boolean isLoaded = true;
|
||||
@gplx.Internal protected boolean IsDirty() {return isDirty;} public void IsDirty_set(boolean v) {isDirty = v;} private boolean isDirty = false;
|
||||
|
||||
public static TdbTable new_(int id, String name, TdbFile file) {TdbTable rv = new TdbTable(); rv.ctor(id, name, file); rv.isDirty = true; return rv;} TdbTable() {}
|
||||
public static TdbTable load_(int id, String name, TdbFile file) {TdbTable rv = new TdbTable(); rv.ctor(id, name, file); rv.isLoaded = false; return rv;}
|
||||
void ctor(int id, String name, TdbFile file) {
|
||||
this.id = id; this.name = name; this.file = file;
|
||||
layout = DsvStoreLayout.dsv_full_();
|
||||
}
|
||||
|
||||
@gplx.Internal protected void DataObj_Wtr(DataWtr wtr) {
|
||||
wtr.InitWtr(DsvStoreLayout.Key_const, layout);
|
||||
wtr.WriteTableBgn(name, flds);
|
||||
for (int rowIdx = 0; rowIdx < rows.Count(); rowIdx++) {
|
||||
GfoNde drow = rows.FetchAt_asGfoNde(rowIdx);
|
||||
wtr.WriteLeafBgn("row");
|
||||
for (int i = 0; i < drow.Flds().Count(); i++)
|
||||
wtr.WriteData(drow.Flds().FetchAt(i).Key(), drow.ReadAt(i));
|
||||
wtr.WriteLeafEnd();
|
||||
}
|
||||
wtr.WriteNodeEnd();
|
||||
isDirty = false;
|
||||
}
|
||||
@gplx.Internal protected void DataObj_Rdr(DataRdr rdr) {
|
||||
layout = TdbStores.FetchLayout(rdr);
|
||||
GfoNdeRdr ndeRdr = GfoNdeRdr_.as_(rdr );
|
||||
if (ndeRdr != null) {
|
||||
if (ndeRdr.UnderNde() == null) throw Err_.new_("ndeRdr.UnderNde is null").Add("name", rdr.NameOfNode());
|
||||
rows = ndeRdr.UnderNde().Subs();
|
||||
flds = ndeRdr.UnderNde().SubFlds();
|
||||
}
|
||||
else { // XmlRdr needs to load each row again...
|
||||
throw Err_.invalid_op_("TableLoad not implemented").Add("rdrType", ClassAdp_.NameOf_obj(rdr)).Add("rdrName", rdr.NameOfNode());
|
||||
}
|
||||
isLoaded = true;
|
||||
}
|
||||
DsvStoreLayout layout;
|
||||
public static TdbTable as_(Object obj) {return obj instanceof TdbTable ? (TdbTable)obj : null;}
|
||||
public static TdbTable cast_(Object obj) {try {return (TdbTable)obj;} catch(Exception exc) {throw Err_.type_mismatch_exc_(exc, TdbTable.class, obj);}}
|
||||
}
|
||||
63
140_dbs/src_400_tdbs/gplx/dbs/TdbTableList.java
Normal file
63
140_dbs/src_400_tdbs/gplx/dbs/TdbTableList.java
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.lists.*; /*OrderedHash_base*/ import gplx.stores.dsvs.*; /*DsvStoreLayout*/
|
||||
public class TdbTableList extends OrderedHash_base {
|
||||
public TdbTable Fetch(String name) {return TdbTable.as_(Fetch_base(name));}
|
||||
public TdbTable FetchOrFail(String name) {
|
||||
TdbTable rv = TdbTable.as_(Fetch(name)); if (rv == null) throw Err_.new_("could not find table; database file may not exist").Add("table", name);
|
||||
return rv;
|
||||
}
|
||||
public void Add(TdbTable dataTable) {Add_base(dataTable.Name(), dataTable);}
|
||||
|
||||
public static TdbTableList new_(Io_url dbInfo) {
|
||||
TdbTableList rv = new TdbTableList();
|
||||
rv.layout = DsvStoreLayout.dsv_full_();
|
||||
return rv;
|
||||
}
|
||||
@gplx.Internal protected void DataObj_Wtr(DataWtr wtr) {
|
||||
wtr.InitWtr(DsvStoreLayout.Key_const, layout);
|
||||
wtr.WriteTableBgn(StoreTableName, FldList);
|
||||
for (Object tblObj : this) {
|
||||
TdbTable tbl = (TdbTable)tblObj;
|
||||
wtr.WriteLeafBgn("tbl");
|
||||
wtr.WriteData(Fld_id, tbl.Id());
|
||||
wtr.WriteData(Fld_name, tbl.Name());
|
||||
wtr.WriteData(Fld_file_id, tbl.File().Id());
|
||||
wtr.WriteLeafEnd();
|
||||
}
|
||||
wtr.WriteNodeEnd();
|
||||
}
|
||||
@gplx.Internal protected void DataObj_Rdr(DataRdr rdr, TdbFileList files) {
|
||||
layout = TdbStores.FetchLayout(rdr);
|
||||
DataRdr subRdr = rdr.Subs();
|
||||
this.Clear();
|
||||
while (subRdr.MoveNextPeer()) {
|
||||
int id = subRdr.ReadInt(Fld_id);
|
||||
String name = subRdr.ReadStr(Fld_name);
|
||||
int file_id = subRdr.ReadInt(Fld_file_id);
|
||||
TdbFile file = files.FetchOrFail(file_id);
|
||||
TdbTable table = TdbTable.load_(id, name, file);
|
||||
this.Add(table);
|
||||
}
|
||||
}
|
||||
DsvStoreLayout layout;
|
||||
public static final String StoreTableName = "_tables";
|
||||
static final String Fld_id = "id"; static final String Fld_name = "name"; static final String Fld_file_id = "file_id";
|
||||
static final GfoFldList FldList = GfoFldList_.new_().Add(Fld_id, IntClassXtn._).Add(Fld_name, StringClassXtn._).Add(Fld_file_id, IntClassXtn._);
|
||||
}
|
||||
46
140_dbs/src_400_tdbs/gplx/dbs/TdbUpdate.java
Normal file
46
140_dbs/src_400_tdbs/gplx/dbs/TdbUpdate.java
Normal file
@@ -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; import gplx.*;
|
||||
import gplx.criterias.*;
|
||||
import gplx.lists.*; /*GfoNde*/
|
||||
class TdbUpdateWkr implements Db_qryWkr {
|
||||
public Object Exec(Db_engine engineObj, Db_qry cmdObj) {
|
||||
TdbEngine engine = TdbEngine.cast_(engineObj); Db_qry_update cmd = (Db_qry_update)cmdObj;
|
||||
|
||||
int rv = 0;
|
||||
TdbTable tbl = engine.FetchTbl(cmd.BaseTable());
|
||||
Criteria crt = cmd.Where().Crt();
|
||||
for (int i = 0; i < tbl.Rows().Count(); i++) {
|
||||
GfoNde row = (GfoNde)tbl.Rows().FetchAt_asGfoNde(i);
|
||||
if (crt.Matches(row)) {
|
||||
UpdateRow(cmd, row);
|
||||
rv++;
|
||||
}
|
||||
}
|
||||
if (rv > 0) tbl.IsDirty_set(true);
|
||||
return rv;
|
||||
}
|
||||
void UpdateRow(Db_qry_update cmd, GfoNde row) {
|
||||
for (int i = 0; i < cmd.Args().Count(); i++) {
|
||||
KeyVal p = (KeyVal)cmd.Args().FetchAt(i);
|
||||
Db_arg prm = (Db_arg)p.Val();
|
||||
row.Write(p.Key(), prm.Val());
|
||||
}
|
||||
}
|
||||
public static TdbUpdateWkr new_() {TdbUpdateWkr rv = new TdbUpdateWkr(); return rv;}
|
||||
}
|
||||
27
140_dbs/src_410_stores/gplx/stores/DbMaprArg.java
Normal file
27
140_dbs/src_410_stores/gplx/stores/DbMaprArg.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
public class DbMaprArg {
|
||||
public String ObjProp() {return objProp;} private String objProp;
|
||||
public String DbFld() {return dbFld;} private String dbFld;
|
||||
public static DbMaprArg new_(String objProp, String dbFld) {
|
||||
DbMaprArg rv = new DbMaprArg();
|
||||
rv.objProp = objProp; rv.dbFld = dbFld;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
52
140_dbs/src_410_stores/gplx/stores/DbMaprItm.java
Normal file
52
140_dbs/src_410_stores/gplx/stores/DbMaprItm.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
public class DbMaprItm {
|
||||
public String TableName() {return tableName;} public DbMaprItm TableName_(String val) {tableName = val; return this;} private String tableName;
|
||||
public OrderedHash Flds() {return flds;} OrderedHash flds = OrderedHash_.new_();
|
||||
public HashAdp ContextFlds() {return contextFlds;} HashAdp contextFlds = HashAdp_.new_();
|
||||
public HashAdp ConstantFlds() {return constantFlds;} HashAdp constantFlds = HashAdp_.new_();
|
||||
public ListAdp Subs() {return subs;}
|
||||
|
||||
public DbMaprItm Flds_add(String objProp, String dbFld) {flds.Add(objProp, DbMaprArg.new_(objProp, dbFld)); return this;}
|
||||
public DbMaprItm ContextFlds_add(String s) {
|
||||
DbMaprArg arg = (DbMaprArg)flds.Fetch(s);
|
||||
contextFlds.Add(arg.ObjProp(), arg);
|
||||
return this;
|
||||
}
|
||||
public DbMaprItm ConstantFlds_add(String dbFld, Object dbVal) {constantFlds.Add(dbFld, KeyVal_.new_(dbFld, dbVal)); return this;}
|
||||
public DbMaprItm Subs_add(DbMaprItm... ary) {
|
||||
for (DbMaprItm itm : ary)
|
||||
subs.Add(itm);
|
||||
return this;
|
||||
}
|
||||
public DbMaprItm Subs_get(String find) {
|
||||
for (Object itmObj : subs) {
|
||||
DbMaprItm itm = (DbMaprItm)itmObj;
|
||||
if (String_.Eq(find, itm.key)) return itm;
|
||||
}
|
||||
throw Err_arg.notFound_key_("find", find);
|
||||
}
|
||||
public DbMaprArg Flds_get(String key) {return (DbMaprArg)flds.Fetch(key);}
|
||||
SrlObj proto; String key; ListAdp subs = ListAdp_.new_();
|
||||
public static DbMaprItm proto_(SrlObj proto, String key, String tableName) {
|
||||
DbMaprItm rv = new DbMaprItm();
|
||||
rv.proto = proto; rv.key = key; rv.tableName = tableName;
|
||||
return rv;
|
||||
} DbMaprItm() {}
|
||||
}
|
||||
48
140_dbs/src_410_stores/gplx/stores/DbMaprMgr.java
Normal file
48
140_dbs/src_410_stores/gplx/stores/DbMaprMgr.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
public class DbMaprMgr {
|
||||
public DbMaprArg[] RootIndexFlds() {return rootIndexFlds;} public DbMaprMgr RootIndexFlds_(DbMaprArg... val) {rootIndexFlds = val; return this;} DbMaprArg[] rootIndexFlds;
|
||||
public DbMaprItm Root() {return root;} public DbMaprMgr Root_(DbMaprItm v) {root = v; return this;} DbMaprItm root;
|
||||
public ListAdp OwnerStack() {return ownerStack;} ListAdp ownerStack = ListAdp_.new_();
|
||||
public OrderedHash ContextVars() {return contextVars;} OrderedHash contextVars = OrderedHash_.new_();
|
||||
public ListAdp MaprStack() {return maprStack;} ListAdp maprStack = ListAdp_.new_();
|
||||
public void EnvStack_add(DbMaprItm mapr, SrlObj gobj) {
|
||||
for (Object argObj : mapr.ContextFlds()) {
|
||||
DbMaprArg arg = (DbMaprArg)argObj;
|
||||
Object contextVal = GfoInvkAble_.InvkCmd((GfoInvkAble)gobj, arg.ObjProp());
|
||||
this.ContextVars().AddReplace(arg.DbFld(), contextVal);
|
||||
}
|
||||
this.OwnerStack().Add(gobj);
|
||||
this.MaprStack().Add(mapr);
|
||||
}
|
||||
public void EnvStack_del(DbMaprItm mapr, SrlObj gobj) {
|
||||
for (Object argObj : mapr.ContextFlds()) {
|
||||
DbMaprArg arg = (DbMaprArg)argObj;
|
||||
this.ContextVars().Del(arg.DbFld());
|
||||
}
|
||||
ListAdp_.DelAt_last(this.OwnerStack());
|
||||
ListAdp_.DelAt_last(this.MaprStack());
|
||||
}
|
||||
public void Clear() {
|
||||
ownerStack.Clear();
|
||||
contextVars.Clear();
|
||||
maprStack.Clear();
|
||||
}
|
||||
public static DbMaprMgr new_() {return new DbMaprMgr();} DbMaprMgr() {}
|
||||
}
|
||||
140
140_dbs/src_410_stores/gplx/stores/DbMaprMgr_tst.java
Normal file
140
140_dbs/src_410_stores/gplx/stores/DbMaprMgr_tst.java
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.dbs.*; /*Db_connect*/
|
||||
public class DbMaprMgr_tst {
|
||||
@Before public void setup() {
|
||||
mgr = DbMaprMgr.new_().RootIndexFlds_(DbMaprArg.new_("id", "disc_id"))
|
||||
.Root_
|
||||
( DbMaprItm.proto_(MockDisc._, "discs", "mock_discs")
|
||||
. Flds_add(MockDisc.id_idk, "disc_id").Flds_add(MockDisc.name_idk, "disc_name")
|
||||
. ContextFlds_add(MockDisc.id_idk).Subs_add
|
||||
( DbMaprItm.proto_(MockTitle._, "titles", "mock_titles")
|
||||
. Flds_add(MockTitle.id_idk, "title_id").Flds_add(MockTitle.name_idk, "title_name")
|
||||
. ContextFlds_add(MockTitle.id_idk).Subs_add
|
||||
( DbMaprItm.proto_(MockChapter._, "chapters", "mock_chapters")
|
||||
. Flds_add(MockChapter.id_idk, "chapter_id").Flds_add(MockChapter.name_idk, "chapter_name")
|
||||
, DbMaprItm.proto_(MockStream._, "audios", "mock_streams")
|
||||
. Flds_add(MockStream.id_idk, "stream_id").Flds_add(MockStream.name_idk, "stream_name")
|
||||
. ConstantFlds_add("stream_type", 0)
|
||||
, DbMaprItm.proto_(MockStream._, "subtitles", "mock_streams")
|
||||
. Flds_add(MockStream.id_idk, "stream_id").Flds_add(MockStream.name_idk, "stream_name")
|
||||
. ConstantFlds_add("stream_type", 1)
|
||||
)));
|
||||
wtr = DbMaprWtr.new_by_url_(Db_connect_.Test);
|
||||
wtr.EnvVars().Add(DbMaprWtr.Key_Mgr, mgr);
|
||||
provider = Db_provider_pool._.FetchOrNew(Db_connect_.Test);
|
||||
Db_qry_fxt.DeleteAll(provider, "mock_discs", "mock_titles", "mock_chapters", "mock_streams");
|
||||
} DbMaprMgr mgr; DbMaprWtr wtr; Db_provider provider; MockDisc disc; MockTitle title; MockChapter chapter; MockStream audio, subtitle; SrlMgr rdr;
|
||||
@Test public void PurgeObjTree() {
|
||||
disc = MockDisc.new_().Id_(1);
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1));
|
||||
DbMaprWtrUtl.PurgeObjTree(disc, mgr, provider);
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(provider, "mock_discs"));
|
||||
}
|
||||
@Test public void PurgeObjTree_deep() {
|
||||
disc = MockDisc.new_().Id_(1);
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_chapters", KeyValList.args_("disc_id", 1).Add("title_id", 2).Add("chapter_id", 3));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_chapters", KeyValList.args_("disc_id", 2).Add("title_id", 2).Add("chapter_id", 3));
|
||||
DbMaprWtrUtl.PurgeObjTree(disc, mgr, provider);
|
||||
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(provider, "mock_discs"));
|
||||
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(provider, "mock_titles"));
|
||||
Tfds.Eq(1, Db_qry_fxt.SelectAll_count(provider, "mock_chapters")); // ignore chapter with disc_id=2
|
||||
}
|
||||
@Test public void Save_root() {
|
||||
disc = MockDisc.new_().Id_(1).Name_("disc");
|
||||
|
||||
wtr.StoreRoot(disc, "mock_discs");
|
||||
Db_qry_fxt.tst_Select(provider, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
}
|
||||
@Test public void Save_subs() {
|
||||
disc = MockDisc.new_().Id_(1).Name_("disc");
|
||||
title = MockTitle.new_().Id_(2).Name_("title").Disc_(disc);
|
||||
|
||||
wtr.StoreRoot(disc, "mock_discs");
|
||||
Db_qry_fxt.tst_Select(provider, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
Db_qry_fxt.tst_Select(provider, "mock_titles", DbTstRow.vals_only_(1, 2, "title"));
|
||||
}
|
||||
@Test public void Save_deep() {
|
||||
disc = MockDisc.new_().Id_(1).Name_("disc");
|
||||
title = MockTitle.new_().Id_(2).Name_("title").Disc_(disc);
|
||||
chapter = MockChapter.new_().Id_(3).Name_("chap").Title_(title);
|
||||
audio = MockStream.new_().Id_(4).Name_("audio").Title_(title.Audios());
|
||||
subtitle = MockStream.new_().Id_(5).Name_("subtitle").Title_(title.Subtitles());
|
||||
|
||||
wtr.StoreRoot(disc, "mock_discs");
|
||||
Db_qry_fxt.tst_Select(provider, "mock_discs", DbTstRow.vals_only_(1, "disc"));
|
||||
Db_qry_fxt.tst_Select(provider, "mock_titles", DbTstRow.vals_only_(1, 2, "title"));
|
||||
Db_qry_fxt.tst_Select(provider, "mock_chapters", DbTstRow.vals_only_(1, 2, 3, "chap"));
|
||||
Db_qry_fxt.tst_Select(provider, "mock_streams"
|
||||
, DbTstRow.vals_only_(1, 2, null, 4, "audio")
|
||||
, DbTstRow.vals_only_(1, 2, null, 5, "subtitle")
|
||||
);
|
||||
}
|
||||
@Test public void Load_root() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc._, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
Tfds.Eq("name", disc.Name());
|
||||
Tfds.Eq(0, disc.Titles().Count());
|
||||
}
|
||||
@Test public void Load_subs() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 2).Add("title_name", "title2"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc._, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
Tfds.Eq("name", disc.Name());
|
||||
Tfds.Eq(2, disc.Titles().Count());
|
||||
Tfds.Eq("title1", ((MockTitle)disc.Titles().FetchAt(0)).Name());
|
||||
Tfds.Eq("title2", ((MockTitle)disc.Titles().FetchAt(1)).Name());
|
||||
}
|
||||
@Test public void Load_deep() {
|
||||
rdr = rdr_();
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_discs", KeyValList.args_("disc_id", 1).Add("disc_name", "name"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_titles", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_chapters", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("chapter_id", 3).Add("chapter_name", "chapter1"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_streams", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("stream_id", 4).Add("stream_type", 0).Add("stream_name", "audio1"));
|
||||
Db_qry_fxt.Insert_kvo(provider, "mock_streams", KeyValList.args_("disc_id", 1).Add("title_id", 1).Add("stream_id", 5).Add("stream_type", 1).Add("stream_name", "subtitle1"));
|
||||
disc = (MockDisc)rdr.StoreRoot(MockDisc._, null);
|
||||
|
||||
Tfds.Eq(1, disc.Id());
|
||||
Tfds.Eq("name", disc.Name());
|
||||
Tfds.Eq(1, disc.Titles().Count());
|
||||
MockTitle t = ((MockTitle)disc.Titles().FetchAt(0));
|
||||
Tfds.Eq("title1", t.Name());
|
||||
Tfds.Eq("chapter1", ((MockChapter)t.Chapters().FetchAt(0)).Name());
|
||||
Tfds.Eq(1, t.Audios().Count());
|
||||
Tfds.Eq(1, t.Subtitles().Count());
|
||||
Tfds.Eq("audio1", ((MockStream)t.Audios().FetchAt(0)).Name());
|
||||
Tfds.Eq("subtitle1", ((MockStream)t.Subtitles().FetchAt(0)).Name());
|
||||
}
|
||||
DbMaprRdr rdr_() {
|
||||
DbMaprRdr rv = DbMaprRdr.new_(Db_connect_.Test, Db_crt_.eq_("disc_id", 1));
|
||||
rv.EnvVars().Add(DbMaprWtr.Key_Mgr, mgr);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
122
140_dbs/src_410_stores/gplx/stores/DbMaprRdr.java
Normal file
122
140_dbs/src_410_stores/gplx/stores/DbMaprRdr.java
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
import gplx.criterias.*; import gplx.dbs.*;
|
||||
public class DbMaprRdr extends DataRdr_base implements SrlMgr {
|
||||
@Override public String NameOfNode() {return "DbMaprRdr";}
|
||||
@Override public Object StoreRoot(SrlObj subProto, String key) {
|
||||
mgr = (DbMaprMgr)this.EnvVars().FetchOrFail(DbMaprWtr.Key_Mgr);
|
||||
DbMaprItm rootMapr = mgr.Root();
|
||||
|
||||
GfoNde tbl = GetTbl(rootMapr, rootCrt); int subsCount = tbl.Subs().Count(); if (subsCount == 0) return null; if (subsCount > 1) throw Err_.new_("criteria returned > 1 row").Add("criteria", rootCrt.XtoStr()).Add("subsCount", subsCount);
|
||||
SrlObj root = subProto.SrlObj_New(null);
|
||||
mgr.EnvStack_add(rootMapr, root); RowStack_add(tbl, 0);
|
||||
root.SrlObj_Srl(this);
|
||||
mgr.Clear(); rowStack.Clear();
|
||||
return root;
|
||||
}
|
||||
@Override public void SrlList(String subPropKey, ListAdp list, SrlObj subProto, String itmKey) {
|
||||
DbMaprItm curMapr = (DbMaprItm)mgr.MaprStack().FetchAtLast();
|
||||
DbMaprItm subMapr = curMapr.Subs_get(subPropKey);
|
||||
list.Clear();
|
||||
|
||||
Criteria crit = MakeCrt(mgr, subMapr);
|
||||
GfoNde tbl = GetTbl(subMapr, crit);
|
||||
int tblLen = tbl.Subs().Count();
|
||||
for (int i = 0; i < tblLen; i++) {
|
||||
SrlObj sub = (SrlObj)subProto.SrlObj_New(null);
|
||||
GfoNde subRow = tbl.Subs().FetchAt_asGfoNde(i);
|
||||
mgr.EnvStack_add(subMapr, sub); rowStack.Add(subRow);
|
||||
sub.SrlObj_Srl(this); list.Add(sub);
|
||||
mgr.EnvStack_del(subMapr, sub); ListAdp_.DelAt_last(rowStack);
|
||||
}
|
||||
}
|
||||
Criteria MakeCrt(DbMaprMgr mgr, DbMaprItm mapr) {
|
||||
Criteria rv = null, cur = null;
|
||||
ListAdp list = GetIdxFlds(mgr, mapr);
|
||||
for (Object kvObj : list) {
|
||||
KeyVal kv = (KeyVal)kvObj;
|
||||
cur = Db_crt_.eq_(kv.Key(), kv.Val());
|
||||
rv = (rv == null) ? cur : Criteria_.And(rv, cur);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
ListAdp GetIdxFlds(DbMaprMgr mgr, DbMaprItm curMapr) {
|
||||
ListAdp rv = ListAdp_.new_();
|
||||
int maprStackCount = mgr.MaprStack().Count() - 0; // -1 b/c current is added to stack
|
||||
for (int i = 0; i < maprStackCount; i ++) {
|
||||
DbMaprItm mapr = (DbMaprItm)mgr.MaprStack().FetchAt(i);
|
||||
SrlObj gobj = (SrlObj)mgr.OwnerStack().FetchAt(i);
|
||||
for (Object argObj : mapr.ContextFlds()) {
|
||||
DbMaprArg arg = (DbMaprArg)argObj;
|
||||
Object propVal = GfoInvkAble_.InvkCmd((GfoInvkAble)gobj, arg.ObjProp());
|
||||
rv.Add(KeyVal_.new_(arg.DbFld(), propVal));
|
||||
}
|
||||
}
|
||||
for (Object argObj : curMapr.ConstantFlds()) {
|
||||
KeyVal arg = (KeyVal)argObj;
|
||||
rv.Add(arg);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
GfoNde GetTbl(DbMaprItm mapr, Criteria crit) {
|
||||
String key = mapr.TableName();
|
||||
GfoNde tblByRootCrt = GfoNde_.as_(tables.Fetch(key));
|
||||
if (tblByRootCrt == null) {
|
||||
DataRdr dbRdr = null;
|
||||
try {
|
||||
dbRdr = Db_qry_.select_().From_(mapr.TableName()).Where_(rootCrt).Exec_qry_as_rdr(provider);
|
||||
tblByRootCrt = GfoNde_.rdr_(dbRdr);
|
||||
}
|
||||
finally {dbRdr.Rls();}
|
||||
tables.Add(key, tblByRootCrt);
|
||||
}
|
||||
GfoNde rv = GfoNde_.tbl_(mapr.TableName(), tblByRootCrt.Flds());
|
||||
for (int i = 0; i < tblByRootCrt.Subs().Count(); i++) {
|
||||
GfoNde row = tblByRootCrt.Subs().FetchAt_asGfoNde(i);
|
||||
if (crit.Matches(row)) rv.Subs().Add(row);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
void RowStack_add(GfoNde tbl, int i) {
|
||||
GfoNdeList ndeList = tbl.Subs(); if (i >= ndeList.Count()) throw Err_arg.outOfBounds_("rowIdx", i, ndeList.Count());
|
||||
rowStack.Add(tbl.Subs().FetchAt_asGfoNde(i));
|
||||
}
|
||||
@Override public Object Read(String key) {
|
||||
DbMaprItm mapr = (DbMaprItm)mgr.MaprStack().FetchAtLast();
|
||||
GfoNde row = (GfoNde)rowStack.FetchAtLast();
|
||||
DbMaprArg arg = mapr.Flds_get(key);
|
||||
Object dbVal = null; try {dbVal = row.Read(arg.DbFld());} catch (Exception exc) {throw Err_.err_(exc, "failed to read dbVal from row").Add("key", key).Add("fld", arg.DbFld());}
|
||||
return dbVal;
|
||||
}
|
||||
@Override public DataRdr Subs_byName_moveFirst(String name) {throw Err_.not_implemented_();}
|
||||
@Override public DataRdr Subs() {throw Err_.not_implemented_();}
|
||||
@Override public int FieldCount() {throw Err_.not_implemented_();}
|
||||
@Override public String KeyAt(int i) {throw Err_.not_implemented_();}
|
||||
@Override public Object ReadAt(int i) {throw Err_.not_implemented_();}
|
||||
@Override public KeyVal KeyValAt(int i) {throw Err_.not_implemented_();}
|
||||
@Override public SrlMgr SrlMgr_new(Object o) {return new DbMaprRdr();}
|
||||
HashAdp tables = HashAdp_.new_();
|
||||
Db_provider provider; Criteria rootCrt;
|
||||
DbMaprMgr mgr; ListAdp rowStack = ListAdp_.new_();
|
||||
public static DbMaprRdr new_(Db_connect dbInfo, Criteria rootCrt) {
|
||||
DbMaprRdr rv = new DbMaprRdr();
|
||||
rv.provider = Db_provider_pool._.FetchOrNew(dbInfo); rv.rootCrt = rootCrt;
|
||||
return rv;
|
||||
} DbMaprRdr() {}
|
||||
}
|
||||
114
140_dbs/src_410_stores/gplx/stores/DbMaprWtr.java
Normal file
114
140_dbs/src_410_stores/gplx/stores/DbMaprWtr.java
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
import gplx.criterias.*; import gplx.dbs.*;
|
||||
public class DbMaprWtr extends DataWtr_base implements DataWtr {
|
||||
public void InitWtr(String key, Object val) {}
|
||||
@Override public Object StoreRoot(SrlObj root, String key) {
|
||||
mgr = (DbMaprMgr)this.EnvVars().FetchOrFail(DbMaprWtr.Key_Mgr);
|
||||
DbMaprWtrUtl.PurgeObjTree(root, mgr, provider);
|
||||
WriteGfoObj(root, mgr.Root());
|
||||
mgr.Clear();
|
||||
return null;
|
||||
}
|
||||
@Override public void SrlList(String subPropKey, ListAdp list, SrlObj subProto, String itmKey) {
|
||||
DbMaprItm ownerMapr = (DbMaprItm)mgr.MaprStack().FetchAtLast();
|
||||
DbMaprItm subMapr = ownerMapr.Subs_get(subPropKey);
|
||||
|
||||
for (Object subObj : list) {
|
||||
SrlObj sub = (SrlObj)subObj;
|
||||
WriteGfoObj(sub, subMapr);
|
||||
}
|
||||
}
|
||||
void WriteGfoObj(SrlObj gobj, DbMaprItm mapr) {
|
||||
mgr.EnvStack_add(mapr, gobj);
|
||||
this.WriteNodeBgn(mapr.TableName());
|
||||
this.WriteContextFlds();
|
||||
gobj.SrlObj_Srl(this);
|
||||
this.WriteNodeEnd();
|
||||
mgr.EnvStack_del(mapr, gobj);
|
||||
}
|
||||
void WriteContextFlds() {
|
||||
int maprStackCount = mgr.MaprStack().Count() - 1; // -1 b/c current is added to stack
|
||||
for (int i = 0; i < maprStackCount; i ++) {
|
||||
DbMaprItm mapr = (DbMaprItm)mgr.MaprStack().FetchAt(i);
|
||||
SrlObj gobj = (SrlObj)mgr.OwnerStack().FetchAt(i);
|
||||
for (Object argObj : mapr.ContextFlds()) {
|
||||
DbMaprArg arg = (DbMaprArg)argObj;
|
||||
Object argVal = GfoInvkAble_.InvkCmd((GfoInvkAble)gobj, arg.ObjProp());
|
||||
this.WriteDataVal(arg.DbFld(), argVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override public void WriteNodeBgn(String v) {
|
||||
if (insertCmd != null) insertCmd.Exec_qry(provider); // occurs for nodes; ex: new title starts; commit changes for own disc
|
||||
curTableName = v;
|
||||
insertCmd = null;
|
||||
}
|
||||
@Override public void WriteData(String name, Object val) {
|
||||
DbMaprItm ownerMapr = (DbMaprItm)mgr.MaprStack().FetchAtLast();
|
||||
String fld = ""; try {fld = ownerMapr.Flds_get(name).DbFld();} catch (Exception exc) {throw Err_.err_(exc, "failed to fetch fld from mapr").Add("key", name);}
|
||||
WriteDataVal(fld, val);
|
||||
}
|
||||
void WriteDataVal(String fld, Object val) {
|
||||
if (insertCmd == null) insertCmd = Db_qry_.insert_(curTableName);
|
||||
if (ClassAdp_.Eq_typeSafe(val, String.class))
|
||||
insertCmd.Arg_obj_type_(fld, val, Db_val_type.Tid_varchar);
|
||||
else
|
||||
insertCmd.Arg_obj_(fld, val);
|
||||
}
|
||||
@Override public void WriteNodeEnd() {
|
||||
if (insertCmd != null) insertCmd.Exec_qry(provider); // occurs for nodes and leaves; for nodes, insertCmd will be null (committed by last leaf)
|
||||
insertCmd = null;
|
||||
}
|
||||
public void WriteTableBgn(String name, GfoFldList fields) {}
|
||||
public void WriteLeafBgn(String leafName) {}
|
||||
public void WriteLeafEnd() {}
|
||||
public void Clear() {}
|
||||
public String XtoStr() {return "";}
|
||||
@Override public SrlMgr SrlMgr_new(Object o) {return new DbMaprWtr();}
|
||||
DbMaprMgr mgr; Db_provider provider; String curTableName; Db_qry_insert insertCmd;
|
||||
public static DbMaprWtr new_by_url_(Db_connect url) {
|
||||
DbMaprWtr rv = new DbMaprWtr();
|
||||
rv.provider = Db_provider_pool._.FetchOrNew(url);
|
||||
return rv;
|
||||
} DbMaprWtr() {}
|
||||
public static final String Key_Mgr = "DbMapr.mgr";
|
||||
}
|
||||
class DbMaprWtrUtl {
|
||||
public static void PurgeObjTree(SrlObj root, DbMaprMgr mgr, Db_provider provider) {
|
||||
Criteria crt = MakeCriteria(root, mgr.RootIndexFlds());
|
||||
PurgeObj(mgr.Root(), crt, provider);
|
||||
}
|
||||
static Criteria MakeCriteria(SrlObj root, DbMaprArg[] objRootIdxFlds) {
|
||||
Criteria rv = null;
|
||||
for (DbMaprArg arg : objRootIdxFlds) {
|
||||
Object argVal = GfoInvkAble_.InvkCmd((GfoInvkAble)root, arg.ObjProp());
|
||||
Criteria cur = Db_crt_.eq_(arg.DbFld(), argVal);
|
||||
rv = (rv == null) ? cur : Criteria_.And(rv, cur);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
static void PurgeObj(DbMaprItm mapr, Criteria crt, Db_provider provider) {
|
||||
Db_qry_.delete_(mapr.TableName(), crt).Exec_qry(provider);
|
||||
for (Object subObj : mapr.Subs()) {
|
||||
DbMaprItm sub = (DbMaprItm)subObj;
|
||||
PurgeObj(sub, crt, provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
81
140_dbs/src_410_stores/gplx/stores/Db_data_rdr.java
Normal file
81
140_dbs/src_410_stores/gplx/stores/Db_data_rdr.java
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
import java.sql.Date;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.GregorianCalendar;
|
||||
import gplx.dbs.*;
|
||||
public class Db_data_rdr extends DataRdr_base implements DataRdr {
|
||||
@Override public String NameOfNode() {return commandText;} public String XtoStr() {return commandText;} private String commandText;
|
||||
private ResultSet rdr;
|
||||
private int fieldCount;
|
||||
@Override public int FieldCount() {return fieldCount;}
|
||||
@Override public String KeyAt(int i) {
|
||||
String rv = null;
|
||||
try {rv = rdr.getMetaData().getColumnLabel(i + ListAdp_.Base1);}
|
||||
catch (SQLException e) {Err_.err_(e, "get columnName failed").Add("i", i).Add("commandText", commandText);}
|
||||
return rv;
|
||||
}
|
||||
@Override public Object ReadAt(int i) {
|
||||
Object rv;
|
||||
try {rv = rdr.getObject(i + ListAdp_.Base1);} catch(Exception exc) {throw Err_.new_("could not read val from dataReader; idx not found or rdr not open").Add("idx", i).Add("sql", commandText);}
|
||||
return rv;
|
||||
}
|
||||
@Override public Object Read(String key) {
|
||||
Object rv;
|
||||
try {rv = rdr.getObject(key);} catch(Exception exc) {throw Err_.new_("could not read val from dataReader; key not found or rdr may not be open").Add("key", key).Add("sql", commandText);}
|
||||
return rv;
|
||||
}
|
||||
@Override public DateAdp ReadDate(String key) {
|
||||
Object o = this.Read(key);
|
||||
Timestamp ts = (Timestamp)o;
|
||||
GregorianCalendar g = new GregorianCalendar();
|
||||
g.setTime(ts);
|
||||
return DateAdp_.dateTime_(g);
|
||||
}
|
||||
@Override public DecimalAdp ReadDecimal(String key) {return DecimalAdp_.db_(this.Read(key));}
|
||||
@Override public gplx.ios.Io_stream_rdr ReadRdr(String key) {
|
||||
try {
|
||||
java.io.InputStream input_stream = rdr.getBinaryStream(key);
|
||||
return gplx.ios.Io_stream_rdr_.file_(input_stream);
|
||||
}
|
||||
catch (SQLException e) {return gplx.ios.Io_stream_rdr_.Null;}
|
||||
}
|
||||
|
||||
public boolean MoveNextPeer() {
|
||||
try {return rdr.next();}
|
||||
catch (Exception exc) {throw Err_.new_("could not move next; check column casting error in SQL").Add("exc", Err_.Message_gplx(exc)).Add("sql", commandText);}
|
||||
}
|
||||
@Override public DataRdr Subs() {throw Err_.not_implemented_();}
|
||||
public DataRdr Subs_byName(String fld) {throw Err_.not_implemented_();}
|
||||
@Override public DataRdr Subs_byName_moveFirst(String fld) {throw Err_.not_implemented_();}
|
||||
public void Rls() {
|
||||
try {rdr.close();}
|
||||
catch (SQLException e) {Err_.err_(e, "reader dispose failed").Add("commandText", commandText);}
|
||||
this.EnvVars().Clear();
|
||||
}
|
||||
@gplx.Internal protected Db_data_rdr ctor_db_data_rdr(ResultSet rdr, String commandText) {
|
||||
this.rdr = rdr; this.commandText = commandText; this.Parse_set(false);
|
||||
try {fieldCount = this.rdr.getMetaData().getColumnCount();}
|
||||
catch (SQLException e) {Err_.err_(e, "get columnCount failed").Add("commandText", commandText);}
|
||||
return this;
|
||||
}
|
||||
@Override public SrlMgr SrlMgr_new(Object o) {return new Db_data_rdr();}
|
||||
}
|
||||
22
140_dbs/src_410_stores/gplx/stores/Db_data_rdr_.java
Normal file
22
140_dbs/src_410_stores/gplx/stores/Db_data_rdr_.java
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
import java.sql.ResultSet;
|
||||
public class Db_data_rdr_ {
|
||||
public static Db_data_rdr new_(ResultSet rdr, String commandText) {return new Db_data_rdr().ctor_db_data_rdr(rdr, commandText);}
|
||||
}
|
||||
130
140_dbs/src_410_stores/gplx/stores/MockDiscObj.java
Normal file
130
140_dbs/src_410_stores/gplx/stores/MockDiscObj.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
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.stores; import gplx.*;
|
||||
class MockDisc implements SrlObj, GfoInvkAble {
|
||||
public int Id() {return id;} public MockDisc Id_(int val) {id = val; return this;} int id; public static final String id_idk = "id";
|
||||
public String Name() {return name;} public MockDisc Name_(String val) {name = val; return this;} private String name; public static final String name_idk = "name";
|
||||
public ListAdp Titles() {return titles;} ListAdp titles = ListAdp_.new_(); public static final String titles_idk = "titles";
|
||||
public static final MockDisc _ = new MockDisc(); MockDisc() {}
|
||||
public SrlObj SrlObj_New(Object o) {return new MockDisc();}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, id_idk)) return Id();
|
||||
else if (ctx.Match(k, name_idk)) return Name();
|
||||
else if (ctx.Match(k, titles_idk)) return Titles();
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
}
|
||||
// public Object Srl_new(GfsCtx ctx) {return new MockDisc();}
|
||||
// public void Srl(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
// id = m.ReadIntOr(id_idk, id);
|
||||
// name = m.ReadStrOr(name_idk, name);
|
||||
// for (int i = 0; i < m.Subs_count(); i++) {
|
||||
// GfoMsg subMsg = m.Subs_getAt(i);
|
||||
// if (String_.Eq(subMsg.Key(), titles_idk)) DoIt(ctx, ikey, k, subMsg, titles, MockTitle._, "title");
|
||||
// }
|
||||
// }
|
||||
// public static void DoIt(GfsCtx ctx, int ikey, String k, GfoMsg m, ListAdp list, Object o, String subKey) {
|
||||
// }
|
||||
public void SrlObj_Srl(SrlMgr mgr) {
|
||||
id = mgr.SrlIntOr(id_idk, id);
|
||||
name = mgr.SrlStrOr(name_idk, name);
|
||||
mgr.SrlList(titles_idk, titles, MockTitle._, "title");
|
||||
}
|
||||
public static MockDisc new_() {
|
||||
MockDisc rv = new MockDisc();
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
class MockTitle implements SrlObj, GfoInvkAble {
|
||||
public int Id() {return id;} public MockTitle Id_(int val) {id = val; return this;} int id; public static final String id_idk = "id";
|
||||
public String Name() {return name;} public MockTitle Name_(String val) {name = val; return this;} private String name; public static final String name_idk = "name";
|
||||
public ListAdp Chapters() {return chapters;} ListAdp chapters = ListAdp_.new_(); public static final String chapters_idk = "chapters";
|
||||
public ListAdp Audios() {return audios;} ListAdp audios = ListAdp_.new_(); public static final String audios_idk = "audios";
|
||||
public ListAdp Subtitles() {return subtitles;} ListAdp subtitles = ListAdp_.new_(); public static final String subtitles_idk = "subtitles";
|
||||
public MockTitle Disc_(MockDisc disc) {disc.Titles().Add(this); return this;}
|
||||
public static MockTitle new_() {
|
||||
MockTitle rv = new MockTitle();
|
||||
return rv;
|
||||
}
|
||||
public static final MockTitle _ = new MockTitle(); MockTitle() {}
|
||||
public SrlObj SrlObj_New(Object o) {return new MockTitle();}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, id_idk)) return Id();
|
||||
else if (ctx.Match(k, name_idk)) return Name();
|
||||
else if (ctx.Match(k, chapters_idk)) return Chapters();
|
||||
else if (ctx.Match(k, audios_idk)) return Audios();
|
||||
else if (ctx.Match(k, subtitles_idk)) return Subtitles();
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
}
|
||||
// public void Srl(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
// id = m.ReadIntOr(id_idk, id);
|
||||
// name = m.ReadStrOr(name_idk, name);
|
||||
// for (int i = 0; i < m.Subs_count(); i++) {
|
||||
// GfoMsg subMsg = m.Subs_getAt(i);
|
||||
// if (String_.Eq(subMsg.Key(), chapters_idk)) MockDisc.DoIt(ctx, ikey, k, subMsg, chapters, MockChapter._, "chapter");
|
||||
// else if (String_.Eq(subMsg.Key(), audios_idk)) MockDisc.DoIt(ctx, ikey, k, subMsg, audios, MockStream._, "audio");
|
||||
// else if (String_.Eq(subMsg.Key(), subtitles_idk)) MockDisc.DoIt(ctx, ikey, k, subMsg, subtitles, MockStream._, "subtitle");
|
||||
// }
|
||||
// }
|
||||
public void SrlObj_Srl(SrlMgr mgr) {
|
||||
id = mgr.SrlIntOr(id_idk, id);
|
||||
name = mgr.SrlStrOr(name_idk, name);
|
||||
mgr.SrlList(chapters_idk, chapters, MockChapter._, "chapter");
|
||||
mgr.SrlList(audios_idk, audios, MockStream._, "audio");
|
||||
mgr.SrlList(subtitles_idk, subtitles, MockStream._, "subtitle");
|
||||
}
|
||||
}
|
||||
class MockChapter implements SrlObj, GfoInvkAble {
|
||||
public int Id() {return id;} public MockChapter Id_(int val) {id = val; return this;} int id; public static final String id_idk = "id";
|
||||
public String Name() {return name;} public MockChapter Name_(String val) {name = val; return this;} private String name; public static final String name_idk = "name";
|
||||
public MockChapter Title_(MockTitle title) {title.Chapters().Add(this); return this;}
|
||||
public static final MockChapter _ = new MockChapter(); MockChapter() {}
|
||||
public static MockChapter new_() {
|
||||
MockChapter rv = new MockChapter();
|
||||
return rv;
|
||||
}
|
||||
public SrlObj SrlObj_New(Object o) {return new MockChapter();}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, id_idk)) return Id();
|
||||
else if (ctx.Match(k, name_idk)) return Name();
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
}
|
||||
public void SrlObj_Srl(SrlMgr mgr) {
|
||||
id = mgr.SrlIntOr(id_idk, id);
|
||||
name = mgr.SrlStrOr(name_idk, name);
|
||||
}
|
||||
}
|
||||
class MockStream implements SrlObj, GfoInvkAble {
|
||||
public int Id() {return id;} public MockStream Id_(int val) {id = val; return this;} int id; public static final String id_idk = "id";
|
||||
public String Name() {return name;} public MockStream Name_(String val) {name = val; return this;} private String name; public static final String name_idk = "name";
|
||||
public MockStream Title_(ListAdp list) {list.Add(this); return this;}
|
||||
public static final MockStream _ = new MockStream(); MockStream() {}
|
||||
public static MockStream new_() {
|
||||
MockStream rv = new MockStream();
|
||||
return rv;
|
||||
}
|
||||
public SrlObj SrlObj_New(Object o) {return new MockStream();}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, id_idk)) return Id();
|
||||
else if (ctx.Match(k, name_idk)) return Name();
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
}
|
||||
public void SrlObj_Srl(SrlMgr mgr) {
|
||||
id = mgr.SrlIntOr(id_idk, id);
|
||||
name = mgr.SrlStrOr(name_idk, name);
|
||||
}
|
||||
}
|
||||
99
140_dbs/tst/gplx/dbs/AnsiSqlWtr_tst.java
Normal file
99
140_dbs/tst/gplx/dbs/AnsiSqlWtr_tst.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class AnsiSqlWtr_tst {
|
||||
Sql_cmd_wtr sqlWtr = Sql_cmd_wtr_ansi_.default_();
|
||||
@Test public void Insert() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.insert_("people").Arg_("id", 1).Arg_("name", "me")
|
||||
, "INSERT INTO people (id, name) VALUES (1, 'me')"
|
||||
);
|
||||
}
|
||||
@Test public void Delete() {
|
||||
Criteria crt = Db_crt_.eq_("id", 1);
|
||||
tst_XtoSql
|
||||
( Db_qry_.delete_("people", crt)
|
||||
, "DELETE FROM people WHERE id=1"
|
||||
);
|
||||
}
|
||||
@Test public void Update() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.update_("people", Db_crt_.eq_("id", 1)).Arg_("name", "me")
|
||||
, "UPDATE people SET name='me' WHERE id=1"
|
||||
);
|
||||
}
|
||||
@Test public void SelectAll() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people")
|
||||
, "SELECT * FROM people"
|
||||
);
|
||||
}
|
||||
@Test public void SelectFlds() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().Cols_("id", "name").From_("people")
|
||||
, "SELECT id, name FROM people"
|
||||
);
|
||||
}
|
||||
@Test public void SelectOrderBy() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people").OrderBy_("name", false)
|
||||
, "SELECT * FROM people ORDER BY name DESC"
|
||||
);
|
||||
}
|
||||
@Test public void SelectWhere() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people").Where_(Db_crt_.eq_("id", 1))
|
||||
, "SELECT * FROM people WHERE id=1"
|
||||
);
|
||||
}
|
||||
@Test public void Select_From_Alias() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people", "p")
|
||||
, "SELECT * FROM people p"
|
||||
);
|
||||
}
|
||||
@Test public void Select_Join_Alias() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.select_().From_("people", "p").Join_("roles", "r", Sql_join_itm.same_("p", "id"))
|
||||
, "SELECT * FROM people p INNER JOIN roles r ON p.id=r.id"
|
||||
);
|
||||
}
|
||||
@Test public void Prepare() {
|
||||
tst_XtoSql
|
||||
( Db_qry_.insert_("people").Arg_("id", 1).Arg_("name", "me")
|
||||
, "INSERT INTO people (id, name) VALUES (?, ?)"
|
||||
, true
|
||||
);
|
||||
|
||||
tst_XtoSql
|
||||
( Db_qry_.delete_("people", Db_crt_.eq_("id", 1))
|
||||
, "DELETE FROM people WHERE id=?"
|
||||
, true
|
||||
);
|
||||
|
||||
tst_XtoSql
|
||||
( Db_qry_.update_("people", Db_crt_.eq_("id", 1)).Arg_("name", "me")
|
||||
, "UPDATE people SET name=? WHERE id=?"
|
||||
, true
|
||||
);
|
||||
}
|
||||
void tst_XtoSql(Db_qry cmd, String expd) {tst_XtoSql(cmd, expd, false);}
|
||||
void tst_XtoSql(Db_qry cmd, String expd, boolean prepare) {Tfds.Eq(expd, sqlWtr.XtoSqlQry(cmd, prepare));}
|
||||
}
|
||||
37
140_dbs/tst/gplx/dbs/AnsiValWtr_tst.java
Normal file
37
140_dbs/tst/gplx/dbs/AnsiValWtr_tst.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
public class AnsiValWtr_tst {
|
||||
@Test public void BldValStr() {
|
||||
tst_XtoSqlVal(null, "NULL");
|
||||
tst_XtoSqlVal(true, "1");
|
||||
tst_XtoSqlVal(false, "0");
|
||||
tst_XtoSqlVal(1, "1");
|
||||
tst_XtoSqlVal(1.1, "1.1");
|
||||
tst_XtoSqlVal("a", "'a'");
|
||||
tst_XtoSqlVal("a'b", "'a''b'");
|
||||
}
|
||||
void tst_XtoSqlVal(Object val, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Db_arg prm = new Db_arg("not needed", val);
|
||||
valWtr.BldValStr(sb, prm);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
Sql_cmd_wtr valWtr = Sql_cmd_wtr_ansi_.default_();
|
||||
}
|
||||
24
140_dbs/tst/gplx/dbs/DbTstDat.java
Normal file
24
140_dbs/tst/gplx/dbs/DbTstDat.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
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 DbTstDat {
|
||||
public int Idx() {return idx;} public DbTstDat Idx_(int val) {idx = val; return this;} int idx = -1;
|
||||
public String Fld() {return fld;} public DbTstDat Fld_(String v) {fld = v; return this;} private String fld = null;
|
||||
public Object Val() {return val;} public DbTstDat Val_(Object v) {val = v; return this;} Object val = null;
|
||||
public static DbTstDat new_() {return new DbTstDat();} DbTstDat() {}
|
||||
}
|
||||
31
140_dbs/tst/gplx/dbs/DbTstRow.java
Normal file
31
140_dbs/tst/gplx/dbs/DbTstRow.java
Normal file
@@ -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; import gplx.*;
|
||||
public class DbTstRow {
|
||||
public int Idx() {return idx;} public DbTstRow Idx_(int val) {idx = val; return this;} int idx = -1;
|
||||
public DbTstDat[] Dat() {return dat;} DbTstDat[] dat = null;
|
||||
public static DbTstRow vals_only_(Object... ary) {
|
||||
DbTstRow rv = new DbTstRow();
|
||||
int len = Array_.Len(ary);
|
||||
rv.dat = new DbTstDat[len];
|
||||
for (int i = 0; i < len; i++)
|
||||
rv.dat[i] = DbTstDat.new_().Val_(ary[i]);
|
||||
return rv;
|
||||
}
|
||||
public static DbTstRow new_() {return new DbTstRow();} DbTstRow() {}
|
||||
}
|
||||
53
140_dbs/tst/gplx/dbs/Db_crt_tst.java
Normal file
53
140_dbs/tst/gplx/dbs/Db_crt_tst.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class Db_crt_tst {
|
||||
@Before public void setup() {
|
||||
row = GfoNde_.vals_(GfoFldList_.new_().Add("id", IntClassXtn._).Add("name", StringClassXtn._), Object_.Ary(1, "me"));
|
||||
}
|
||||
@Test public void EqualTest() {
|
||||
crt = Db_crt_.eq_("id", 1);
|
||||
tst_Match(true, row, crt);
|
||||
}
|
||||
@Test public void EqualFalseTest() {
|
||||
crt = Db_crt_.eq_("id", 2);
|
||||
tst_Match(false, row, crt);
|
||||
}
|
||||
@Test public void AndCompositeTest() {
|
||||
crt = Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me"));
|
||||
tst_Match(true, row, crt);
|
||||
|
||||
crt = Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "you"));
|
||||
tst_Match(false, row, crt);
|
||||
}
|
||||
@Test public void OrCompositeTest() {
|
||||
crt = Criteria_.Or(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "you"));
|
||||
tst_Match(true, row, crt);
|
||||
|
||||
crt = Criteria_.Or(Db_crt_.eq_("id", 2), Db_crt_.eq_("name", "you"));
|
||||
tst_Match(false, row, crt);
|
||||
}
|
||||
|
||||
void tst_Match(boolean epxd, GfoNde row, Criteria crt) {
|
||||
boolean actl = crt.Matches(row);
|
||||
Tfds.Eq(epxd, actl);
|
||||
}
|
||||
GfoNde row; Criteria crt;
|
||||
}
|
||||
54
140_dbs/tst/gplx/dbs/Db_provider_fxt.java
Normal file
54
140_dbs/tst/gplx/dbs/Db_provider_fxt.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
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_provider_fxt implements RlsAble {
|
||||
public Db_provider Provider() {return provider;} public Db_provider_fxt Provider_(Db_provider v) {provider = v; return this;} Db_provider provider;
|
||||
public void DmlAffectedAvailable_(boolean v) {dmlAffectedAvailable = v;} private boolean dmlAffectedAvailable = true;
|
||||
public void ini_DeleteAll(String tbl) {provider.Exec_qry(Db_qry_.delete_tbl_(tbl));}
|
||||
public void tst_ExecDml(int expd, Db_qry qry) {
|
||||
int actl = provider.Exec_qry(qry);
|
||||
if (dmlAffectedAvailable)
|
||||
Tfds.Eq(expd, actl, "Exec_qry failed: sql={0}", qry.XtoSql());
|
||||
}
|
||||
public void tst_ExecRdrTbl(int expd, String tblName) {tst_ExecRdr(expd, Db_qry_.select_tbl_(tblName));}
|
||||
public void tst_ExecRdr(int expd, Db_qry qry) {
|
||||
DataRdr rdr = DataRdr_.Null;
|
||||
try {
|
||||
rdr = provider.Exec_qry_as_rdr(qry);
|
||||
tbl = GfoNde_.rdr_(rdr);
|
||||
}
|
||||
catch (Exception e) {Err_.Noop(e); rdr.Rls();}
|
||||
Tfds.Eq(expd, tbl.Subs().Count(), "Exec_qry_as_rdr failed: sql={0}", qry.XtoSql());
|
||||
} GfoNde tbl;
|
||||
public GfoNde tst_RowAry(int index, Object... expdValAry) {
|
||||
GfoNde record = tbl.Subs().FetchAt_asGfoNde(index);
|
||||
Object[] actlValAry = new Object[expdValAry.length];
|
||||
for (int i = 0; i < actlValAry.length; i++)
|
||||
actlValAry[i] = record.ReadAt(i);
|
||||
Tfds.Eq_ary(actlValAry, expdValAry);
|
||||
return record;
|
||||
}
|
||||
public void Rls() {provider.Rls();}
|
||||
|
||||
public static Db_provider Mysql() {return Db_provider_.new_(Db_connect_mysql.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
|
||||
public static Db_provider Tdb(String fileName) {return Db_provider_.new_(Db_connect_.tdb_(Tfds.RscDir.GenSubDir_nest("140_dbs", "tdbs").GenSubFil(fileName)));}
|
||||
public static Db_provider Postgres() {return Db_provider_.new_(Db_connect_postgres.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
|
||||
public static Db_provider Sqlite() {return Db_provider_.new_(Db_connect_sqlite.load_(Tfds.RscDir.GenSubFil_nest("140_dbs", "sqlite", "unit_tests.db")));}
|
||||
// public static Db_provider Mssql() {return MssqlConnectUrl.WindowsAuth(".", "unit_tests");
|
||||
public static final boolean SkipPostgres = Tfds.SkipDb || true;
|
||||
}
|
||||
54
140_dbs/tst/gplx/dbs/Db_qry_fxt.java
Normal file
54
140_dbs/tst/gplx/dbs/Db_qry_fxt.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
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_qry_fxt {
|
||||
public static void Insert_kvo(Db_provider provider, String tblName, KeyValList kvList) {
|
||||
Db_qry_insert qry = Db_qry_.insert_(tblName);
|
||||
for (int i = 0; i < kvList.Count(); i++) {
|
||||
KeyVal kv = kvList.GetAt(i);
|
||||
qry.Arg_obj_(kv.Key(), kv.Val());
|
||||
}
|
||||
qry.Exec_qry(provider);
|
||||
}
|
||||
public static GfoNde SelectAll(Db_provider provider, String tblName) {
|
||||
return Db_qry_.select_tbl_(tblName).ExecRdr_nde(provider);
|
||||
}
|
||||
public static int SelectAll_count(Db_provider provider, String tblName) {
|
||||
GfoNde nde = Db_qry_fxt.SelectAll(provider, tblName);
|
||||
return nde.Subs().Count();
|
||||
}
|
||||
public static void DeleteAll(Db_provider provider, String... ary) {
|
||||
for (String s : ary)
|
||||
Db_qry_.delete_tbl_(s).Exec_qry(provider);
|
||||
}
|
||||
public static void tst_Select(Db_provider provider, String tblName, DbTstRow... expdAry) {
|
||||
GfoNde nde = Db_qry_fxt.SelectAll(provider, tblName);
|
||||
int len = Array_.Len(expdAry);
|
||||
for (int i = 0; i < len; i++) {
|
||||
DbTstRow expdRow = expdAry[i];
|
||||
int actlIdx = (expdRow.Idx() == -1) ? i : expdRow.Idx();
|
||||
GfoNde actlNde = nde.Subs().FetchAt_asGfoNde(actlIdx);
|
||||
int fldLen = Array_.Len(expdRow.Dat());
|
||||
for (int j = 0; j < fldLen; j++) {
|
||||
DbTstDat expdDat = expdRow.Dat()[j];
|
||||
Object actlVal = expdDat.Fld() == null ? actlNde.ReadAt(j) : actlNde.Read(expdDat.Fld());
|
||||
Tfds.Eq(expdDat.Val(), actlVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
140_dbs/tst/gplx/dbs/GfoNdeTstr.java
Normal file
32
140_dbs/tst/gplx/dbs/GfoNdeTstr.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 GfoNdeTstr {
|
||||
public static void tst_ValsByCol(GfoNde nde, String fld, Object... expdAry) {
|
||||
ListAdp expd = ListAdp_.new_();
|
||||
for (int i = 0; i < expdAry.length; i++) {
|
||||
expd.Add(Object_.XtoStr_OrEmpty(expdAry[i]));
|
||||
}
|
||||
ListAdp actl = ListAdp_.new_();
|
||||
for (int i = 0; i < nde.Subs().Count(); i++) {
|
||||
GfoNde sub = nde.Subs().FetchAt_asGfoNde(i);
|
||||
actl.Add(Object_.XtoStr_OrEmpty(sub.Read(fld)));
|
||||
}
|
||||
Tfds.Eq_ary(expd.XtoStrAry(), actl.XtoStrAry());
|
||||
}
|
||||
}
|
||||
60
140_dbs/tst/gplx/dbs/IoSqlCriteriaWriter_tst.java
Normal file
60
140_dbs/tst/gplx/dbs/IoSqlCriteriaWriter_tst.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*; /*Criteria_base*/
|
||||
import gplx.ios.*;
|
||||
public class IoSqlCriteriaWriter_tst {
|
||||
@Test public void Type() {
|
||||
fld = IoItm_base_.Prop_Type;
|
||||
tst_Write("type=1", ioCrt_(fld, Criteria_.eq_(IoItmDir.Type_Dir)));
|
||||
tst_Write("type=2", ioCrt_(fld, Criteria_.eq_(IoItmFil.Type_Fil)));
|
||||
}
|
||||
@Test public void Ext() {
|
||||
fld = IoItm_base_.Prop_Ext;
|
||||
tst_Write("ext='.txt'", ioCrt_(fld, Criteria_.eq_(".txt")));
|
||||
tst_Write("ext IN ('.txt', '.xml', '.html')", ioCrt_(fld, Criteria_.in_(".txt", ".xml", ".html")));
|
||||
tst_Write("ext NOT IN ('.dll', '.exe')", ioCrt_(fld, Criteria_.inn_(".dll", ".exe")));
|
||||
}
|
||||
@Test public void Title() {
|
||||
fld = IoItm_base_.Prop_Title;
|
||||
tst_Write("title='bin'", ioCrt_(fld, Criteria_.eq_("bin")));
|
||||
tst_Write("title NOT IN ('bin', 'obj')", ioCrt_(fld, Criteria_.inn_("bin", "obj")));
|
||||
}
|
||||
@Test public void Url() {
|
||||
fld = IoItm_base_.Prop_Path;
|
||||
tst_Write("url='C:\\fil.txt'", ioCrt_(fld, Criteria_.eq_("C:\\fil.txt")));
|
||||
tst_Write("url IOMATCH '*.txt'", ioCrt_(fld, Criteria_ioMatch.parse_(true, "*.txt", false)));
|
||||
}
|
||||
@Test public void Binary() {
|
||||
// parentheses around lhs and rhs
|
||||
tst_Write(
|
||||
"(type=1 OR type=2)"
|
||||
, Criteria_.Or
|
||||
( ioCrt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmDir.Type_Dir)), ioCrt_(IoItm_base_.Prop_Type, Criteria_.eq_(IoItmFil.Type_Fil))
|
||||
));
|
||||
}
|
||||
Criteria ioCrt_(String fld, Criteria crt) {return Criteria_wrapper.new_(fld, crt);}
|
||||
String fld;
|
||||
void tst_Write(String expd, Criteria crt) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
Sql_cmd_wtr whereWtr = Sql_cmd_wtr_ansi_.default_();
|
||||
whereWtr.BldWhere(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
}
|
||||
56
140_dbs/tst/gplx/dbs/PoolIds_tst.java
Normal file
56
140_dbs/tst/gplx/dbs/PoolIds_tst.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
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.*;
|
||||
import org.junit.*;
|
||||
public class PoolIds_tst {
|
||||
@Before public void setup() {
|
||||
provider = Db_provider_pool._.FetchOrNew(Db_connect_.Test);
|
||||
Db_qry_fxt.DeleteAll(provider, PoolIds.Tbl_Name);
|
||||
mgr = PoolIds._;
|
||||
}
|
||||
@Test public void FetchNextId() {
|
||||
tst_Fetch("/test0", 0);
|
||||
}
|
||||
@Test public void ChangeNextId_Insert() {
|
||||
run_Change("/test0", 1);
|
||||
|
||||
tst_Fetch("/test0", 1);
|
||||
}
|
||||
@Test public void ChangeNextId_Update() {
|
||||
run_Change("/test0", 0);
|
||||
run_Change("/test0", 1);
|
||||
|
||||
tst_Fetch("/test0", 1);
|
||||
}
|
||||
@Test public void FetchNextId_Multiple() {
|
||||
run_Change("/test0", 0);
|
||||
run_Change("/test1", 1);
|
||||
|
||||
tst_Fetch("/test0", 0);
|
||||
tst_Fetch("/test1", 1);
|
||||
}
|
||||
void run_Change(String url, int expd) {
|
||||
mgr.Commit(provider, url, expd);
|
||||
}
|
||||
void tst_Fetch(String url, int expd) {
|
||||
int actl = mgr.FetchNext(provider, url);
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
Db_provider provider;
|
||||
PoolIds mgr;
|
||||
}
|
||||
43
140_dbs/tst/gplx/dbs/Sql_cmd_wtr_ansi_tst.java
Normal file
43
140_dbs/tst/gplx/dbs/Sql_cmd_wtr_ansi_tst.java
Normal file
@@ -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; import gplx.*;
|
||||
import org.junit.*;
|
||||
import gplx.criterias.*;
|
||||
public class Sql_cmd_wtr_ansi_tst {
|
||||
Sql_cmd_wtr whereWtr = Sql_cmd_wtr_ansi_.default_();
|
||||
@Test public void Basic() {
|
||||
tst_XtoSql_where(Db_crt_.eq_("id", 1), "id=1");
|
||||
tst_XtoSql_where(Db_crt_.eqn_("id", 1), "id!=1");
|
||||
tst_XtoSql_where(Db_crt_.mt_("id", 1), "id>1");
|
||||
tst_XtoSql_where(Db_crt_.mte_("id", 1), "id>=1");
|
||||
tst_XtoSql_where(Db_crt_.lt_("id", 1), "id<1");
|
||||
tst_XtoSql_where(Db_crt_.lte_("id", 1), "id<=1");
|
||||
tst_XtoSql_where(Db_crt_.between_("id", 1, 2), "id BETWEEN 1 AND 2");
|
||||
tst_XtoSql_where(Db_crt_.in_("id", 1, 2, 3), "id IN (1, 2, 3)");
|
||||
}
|
||||
@Test public void AndOr() {
|
||||
tst_XtoSql_where(Criteria_.And(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 AND name='me')");
|
||||
tst_XtoSql_where(Criteria_.Or(Db_crt_.eq_("id", 1), Db_crt_.eq_("name", "me")), "(id=1 OR name='me')");
|
||||
tst_XtoSql_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))");
|
||||
}
|
||||
void tst_XtoSql_where(Criteria crt, String expd) {
|
||||
String_bldr sb = String_bldr_.new_();
|
||||
whereWtr.BldWhere(sb, crt);
|
||||
Tfds.Eq(expd, sb.XtoStr());
|
||||
}
|
||||
}
|
||||
95
140_dbs/tst/gplx/dbs/groupBys/GroupBys_base_tst.java
Normal file
95
140_dbs/tst/gplx/dbs/groupBys/GroupBys_base_tst.java
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
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.groupBys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public abstract class GroupBys_base_tst {
|
||||
protected abstract Db_provider provider_();
|
||||
protected Db_provider provider;
|
||||
@Before public void setup() {
|
||||
provider = provider_();
|
||||
Db_qry_.delete_tbl_("dbs_group_bys").Exec_qry(provider);
|
||||
}
|
||||
@After public void teardown() {
|
||||
provider.Rls();
|
||||
}
|
||||
protected void GroupBy_1fld_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1")
|
||||
.GroupBy_("key1")
|
||||
.Exec_qry_as_rdr(provider);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
}
|
||||
protected void GroupBy_2fld_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("key2", "b").Arg_("val_int", 1));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("key2", "b").Arg_("val_int", 2));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1", "key2")
|
||||
.GroupBy_("key1", "key2")
|
||||
.Exec_qry_as_rdr(provider);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key2", "b");
|
||||
}
|
||||
protected void MinMax_hook(boolean min) {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
|
||||
Db_qry_select qry = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1")
|
||||
.GroupBy_("key1");
|
||||
int expd = min ? 1 : 2;
|
||||
if (min)
|
||||
qry.Cols_groupBy_min("val_int", "val_int_func");
|
||||
else
|
||||
qry.Cols_groupBy_max("val_int", "val_int_func");
|
||||
|
||||
DataRdr rdr = qry.Exec_qry_as_rdr(provider);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "val_int_func", expd);
|
||||
}
|
||||
protected void Count_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 10));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 20));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1").Cols_groupBy_count("val_int", "val_int_func")
|
||||
.GroupBy_("key1")
|
||||
.Exec_qry_as_rdr(provider);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "val_int_func", 2);
|
||||
}
|
||||
protected void Sum_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 10));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 20));
|
||||
|
||||
DataRdr rdr = Db_qry_.select_().From_("dbs_group_bys")
|
||||
.Cols_("key1").Cols_groupBy_sum("val_int", "val_int_func")
|
||||
.GroupBy_("key1")
|
||||
.Exec_qry_as_rdr(provider);
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "val_int_func", 30);
|
||||
}
|
||||
}
|
||||
28
140_dbs/tst/gplx/dbs/groupBys/GroupBys_mysql_tst.java
Normal file
28
140_dbs/tst/gplx/dbs/groupBys/GroupBys_mysql_tst.java
Normal file
@@ -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.groupBys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class GroupBys_mysql_tst extends GroupBys_base_tst {
|
||||
@Override protected Db_provider provider_() {return Db_provider_fxt.Mysql();}
|
||||
@Test public void GroupBy_1fld() {super.GroupBy_1fld_hook();}
|
||||
@Test public void GroupBy_2fld() {super.GroupBy_2fld_hook();}
|
||||
@Test public void Min() {super.MinMax_hook(true);}
|
||||
@Test public void Max() {super.MinMax_hook(false);}
|
||||
@Test public void Count() {super.Count_hook();}
|
||||
@Test public void Sum() {super.Sum_hook();}
|
||||
}
|
||||
29
140_dbs/tst/gplx/dbs/groupBys/GroupBys_tdb_tst.java
Normal file
29
140_dbs/tst/gplx/dbs/groupBys/GroupBys_tdb_tst.java
Normal file
@@ -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.groupBys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class GroupBys_tdb_tst extends GroupBys_base_tst {
|
||||
@Override protected Db_provider provider_() {return Db_provider_fxt.Tdb("130_dbs_group_bys.dsv");}
|
||||
@Test public void GroupBy_1fld() {super.GroupBy_1fld_hook();}
|
||||
@Test public void GroupBy_2fld() {super.GroupBy_2fld_hook();}
|
||||
@Test public void Min() {super.MinMax_hook(true);}
|
||||
@Test public void Max() {super.MinMax_hook(false);}
|
||||
@Test public void Count() {super.Count_hook();}
|
||||
@Test public void Sum() {super.Sum_hook();}
|
||||
// Avg, CountDistinct
|
||||
}
|
||||
60
140_dbs/tst/gplx/dbs/insertIntos/InsertIntos_base_tst.java
Normal file
60
140_dbs/tst/gplx/dbs/insertIntos/InsertIntos_base_tst.java
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
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.insertIntos; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public abstract class InsertIntos_base_tst {
|
||||
protected abstract Db_provider provider_();
|
||||
protected Db_provider provider;
|
||||
@Before public void setup() {
|
||||
provider = provider_();
|
||||
provider.Exec_qry(Db_qry_delete.new_().BaseTable_("dbs_group_bys"));
|
||||
provider.Exec_qry(Db_qry_delete.new_().BaseTable_("dbs_insert_intos"));
|
||||
}
|
||||
@After public void teardown() {
|
||||
provider.Rls();
|
||||
}
|
||||
protected void Select_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
|
||||
provider.Exec_qry
|
||||
(Db_qry_.insert_("dbs_insert_intos")
|
||||
.Cols_("key1", "val_int")
|
||||
.Select_
|
||||
( Db_qry_select.new_().Cols_("key1", "val_int").From_("dbs_group_bys")
|
||||
)
|
||||
);
|
||||
DataRdr rdr = provider.Exec_qry_as_rdr(Db_qry_select.new_().Cols_("key1", "val_int").From_("dbs_insert_intos"));
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "key1", "a");
|
||||
}
|
||||
protected void GroupBy_hook() {
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 1));
|
||||
provider.Exec_qry(Db_qry_.insert_("dbs_group_bys").Arg_("key1", "a").Arg_("val_int", 2));
|
||||
|
||||
provider.Exec_qry
|
||||
(Db_qry_.insert_("dbs_insert_intos")
|
||||
.Cols_("key1", "val_int")
|
||||
.Select_
|
||||
( Db_qry_select.new_().Cols_("key1").Cols_groupBy_sum("val_int", "val_int_func")
|
||||
.From_("dbs_group_bys").GroupBy_("key1")
|
||||
));
|
||||
DataRdr rdr = provider.Exec_qry_as_rdr(Db_qry_select.new_().Cols_("key1", "val_int").From_("dbs_insert_intos"));
|
||||
GfoNde nde = GfoNde_.rdr_(rdr);
|
||||
GfoNdeTstr.tst_ValsByCol(nde, "val_int", 3);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user