mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v3.2.1.1
This commit is contained in:
39
140_dbs/src/gplx/dbs/sys/Db_sys_mgr.java
Normal file
39
140_dbs/src/gplx/dbs/sys/Db_sys_mgr.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.sys; import gplx.*; import gplx.dbs.*;
|
||||
import gplx.core.primitives.*;
|
||||
public class Db_sys_mgr {
|
||||
private final Db_conn conn;
|
||||
private final Db_sys_tbl sys_tbl;
|
||||
private boolean assert_exists = true;
|
||||
public Db_sys_mgr(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
sys_tbl = new Db_sys_tbl(conn);
|
||||
}
|
||||
public int Autonum_next(String tbl, String fld) {return Autonum_next(String_.Concat(tbl, ".", fld));}
|
||||
public int Autonum_next(String key) {
|
||||
if (assert_exists) Assert_exists();
|
||||
int rv = sys_tbl.Assert_int_or(key, 1);
|
||||
sys_tbl.Update_int(key, rv + 1);
|
||||
return rv;
|
||||
}
|
||||
private void Assert_exists() {
|
||||
assert_exists = false;
|
||||
if (!conn.Meta_tbl_exists(sys_tbl.Tbl_name())) sys_tbl.Create_tbl();
|
||||
}
|
||||
}
|
||||
34
140_dbs/src/gplx/dbs/sys/Db_sys_mgr_tst.java
Normal file
34
140_dbs/src/gplx/dbs/sys/Db_sys_mgr_tst.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.sys; import gplx.*; import gplx.dbs.*;
|
||||
import org.junit.*;
|
||||
public class Db_sys_mgr_tst {
|
||||
private final Db_sys_mgr_fxt fxt = new Db_sys_mgr_fxt();
|
||||
@Test public void FetchNextId() {
|
||||
fxt.Test__autonum_next("tbl_1.fld", 1); // default to "1" on first creation
|
||||
fxt.Test__autonum_next("tbl_1.fld", 2); // read "2" from db
|
||||
}
|
||||
}
|
||||
class Db_sys_mgr_fxt {
|
||||
private final Db_sys_mgr sys_mgr;
|
||||
public Db_sys_mgr_fxt() {
|
||||
Db_conn conn = Db_conn_pool.Instance.Get_or_new(Db_conn_info_.mem_("test"));
|
||||
sys_mgr = new Db_sys_mgr(conn);
|
||||
}
|
||||
public void Test__autonum_next(String key, int expd) {Tfds.Eq_int(expd, sys_mgr.Autonum_next(key));}
|
||||
}
|
||||
68
140_dbs/src/gplx/dbs/sys/Db_sys_tbl.java
Normal file
68
140_dbs/src/gplx/dbs/sys/Db_sys_tbl.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
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.sys; import gplx.*; import gplx.dbs.*;
|
||||
class Db_sys_tbl implements Rls_able {
|
||||
private final String tbl_name = "gfdb_sys";
|
||||
private String fld_key, fld_val;
|
||||
private final Dbmeta_fld_list flds = Dbmeta_fld_list.new_();
|
||||
private final Db_conn conn; private Db_stmt stmt_insert, stmt_update, stmt_select;
|
||||
public Db_sys_tbl(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
fld_key = flds.Add_str_pkey("sys_key", 255); fld_val = flds.Add_text("sys_val");
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public String Tbl_name() {return tbl_name;}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds));}
|
||||
public int Assert_int_or(String key, int or) {
|
||||
String rv = Assert_str_or(key, Int_.To_str(or));
|
||||
try {return Int_.parse(rv);}
|
||||
catch (Exception e) {Err_.Noop(e); return or;}
|
||||
}
|
||||
public String Assert_str_or(String key, String or) {
|
||||
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, flds, fld_key);
|
||||
Db_rdr rdr = stmt_select.Clear().Crt_str(fld_key, key).Exec_select__rls_manual();
|
||||
try {
|
||||
if (rdr.Move_next())
|
||||
return rdr.Read_str(fld_val);
|
||||
else {
|
||||
Insert_str(key, or);
|
||||
return or;
|
||||
}
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
public void Update_int(String key, int val) {Update_str(key, Int_.To_str(val));}
|
||||
private void Update_str(String key, String val) {
|
||||
if (stmt_update == null) stmt_update = conn.Stmt_update(tbl_name, String_.Ary(fld_key), fld_val);
|
||||
stmt_update.Clear()
|
||||
.Val_str(fld_val , val)
|
||||
.Crt_str(fld_key , key)
|
||||
.Exec_update();
|
||||
}
|
||||
private void Insert_str(String key, String val) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear()
|
||||
.Val_str(fld_key , key)
|
||||
.Val_str(fld_val , val)
|
||||
.Exec_insert();
|
||||
}
|
||||
public void Rls() {
|
||||
stmt_insert = Db_stmt_.Rls(stmt_insert);
|
||||
stmt_update = Db_stmt_.Rls(stmt_update);
|
||||
stmt_select = Db_stmt_.Rls(stmt_select);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user