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

Embeddable: Fix if_exists

This commit is contained in:
gnosygnu
2016-11-24 08:53:07 -05:00
parent dae3be97ec
commit fcbdf8bbd8
1352 changed files with 7 additions and 88517 deletions

View File

@@ -1,67 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.utls; import gplx.*; import gplx.dbs.*;
import gplx.core.envs.*;
public class Db_cmd_backup implements Gfo_invk {
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 Gfo_invk_.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.Instance.SaveFilStr_args(bkpCmdFil, cmdText).Exec(); // explicitly state utf8;
Process_adp.run_wait_(bkpCmdFil);
Io_mgr.Instance.DeleteFil(bkpCmdFil);
return this;
}
@gplx.Internal protected Db_cmd_backup InitVars() {
String dteStr = Datetime_now.Get().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() {}
}

View File

@@ -1,31 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.utls; import gplx.*; import gplx.dbs.*;
import org.junit.*;
public class Db_cmd_backup_tst {
@Test public void Basic() {
Datetime_now.Manual_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());
}
}

View File

@@ -1,49 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.utls; import gplx.*; import gplx.dbs.*;
public abstract class Db_in_wkr__base {
@gplx.Virtual protected int Interval() {return gplx.dbs.engines.sqlite.Sqlite_engine_.Stmt_arg_max - 10;} // -10 for safety's sake
protected abstract Db_qry Make_qry (int bgn, int end);
protected abstract void Fill_stmt (Db_stmt stmt, int bgn, int end);
protected abstract void Read_data (Cancelable cancelable, Db_rdr rdr);
@gplx.Virtual protected boolean Show_progress() {return false;}
public void Select_in(Cancelable cancelable, Db_conn conn, int full_bgn, int full_end) {
int part_len = Interval();
Gfo_usr_dlg usr_dlg = Gfo_usr_dlg_.Instance;
boolean show_progress = this.Show_progress();;
for (int part_bgn = full_bgn; part_bgn < full_end; part_bgn += part_len) {
int part_end = part_bgn + part_len;
if (part_end > full_end) part_end = full_end;
Db_stmt stmt = Db_stmt_.Null; Db_rdr rdr = Db_rdr_.Empty;
try {
if (show_progress) usr_dlg.Prog_many("", "", "reading: count=~{0}", part_end);
stmt = conn.Stmt_new(Make_qry(part_bgn, part_end));
Fill_stmt(stmt, part_bgn, part_end);
rdr = stmt.Exec_select__rls_manual();
Read_data(cancelable, rdr);
}
finally {rdr.Rls(); stmt.Rls();}
}
}
public static Object[] In_ary(int len) {
Object[] rv = new Object[len];
for (int i = 0; i < len; i++)
rv[i] = "";
return rv;
}
}

View File

@@ -1,82 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.utls; import gplx.*; import gplx.dbs.*;
import gplx.dbs.stmts.*;
public class Db_tbl__crud_ {
public static boolean Upsert(Db_conn conn, String tbl_name, Dbmeta_fld_list flds, String[] crt_cols, Object... objs) {
// init
int crt_cols_len = crt_cols.length;
String[] val_cols = Find_excepts(flds, crt_cols);
// check if exists
Db_stmt select_stmt = conn.Stmt_select(tbl_name, crt_cols, crt_cols);
Add_arg(select_stmt, flds, crt_cols, objs, Bool_.Y, 0);
Db_rdr rdr = select_stmt.Exec_select__rls_auto();
boolean exists = rdr.Move_next();
rdr.Rls();
// do update / insert; NOTE: 0-index and crt_cols_len assumes that objs starts with crts; EX: (id) -> (1, 'abc') x> ('abc', 1)
// update
if (exists) {
Db_stmt update_stmt = conn.Stmt_update(tbl_name, crt_cols, val_cols);
Add_arg(update_stmt, flds, val_cols, objs, Bool_.N, crt_cols_len);
Add_arg(update_stmt, flds, crt_cols, objs, Bool_.Y, 0);
update_stmt.Exec_update();
update_stmt.Rls();
return false;
}
// insert
else {
Db_stmt insert_stmt = conn.Stmt_insert(tbl_name, flds);
Add_arg(insert_stmt, flds, crt_cols, objs, Bool_.N, 0);
Add_arg(insert_stmt, flds, val_cols, objs, Bool_.N, crt_cols_len);
insert_stmt.Exec_insert();
insert_stmt.Rls();
return true;
}
}
private static String[] Find_excepts(Dbmeta_fld_list flds, String[] cols) {
// hash cols
Hash_adp hash = Hash_adp_.New();
int cols_len = cols.length;
for (int i = 0; i < cols_len; ++i)
hash.Add_as_key_and_val(cols[i]);
// loop flds and get excepts
List_adp list = List_adp_.New();
int flds_len = flds.Len();
for (int i = 0; i < flds_len; ++i) {
Dbmeta_fld_itm fld = flds.Get_at(i);
if (!hash.Has(fld.Name()))
list.Add(fld.Name());
}
return list.To_str_ary_and_clear();
}
private static void Add_arg(Db_stmt stmt, Dbmeta_fld_list flds, String[] cols, Object[] objs, boolean mode_is_crt, int objs_bgn) {
int cols_len = cols.length;
for (int i = 0; i < cols_len; ++i) {
String col = cols[i];
Dbmeta_fld_itm fld = flds.Get_by(col);
Object obj = objs[i + objs_bgn];
if (mode_is_crt)
Db_stmt_arg_list.Fill_crt(stmt, fld.Type().Tid_ansi(), col, obj);
else
Db_stmt_arg_list.Fill_val(stmt, fld.Type().Tid_ansi(), col, obj);
}
}
}

View File

@@ -1,52 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.utls; import gplx.*; import gplx.dbs.*;
import gplx.core.stores.*;
import gplx.dbs.qrys.*;
public class PoolIds {
public int FetchNext(Db_conn conn, String url) {
Db_qry__select_cmd cmd = Db_qry_.select_().From_(Tbl_Name).Where_(Db_crt_.New_eq(Fld_id_path, url));
int rv = 0;//boolean isNew = true;
DataRdr rdr = DataRdr_.Null;
try {
rdr = conn.Exec_qry_as_old_rdr(cmd);
if (rdr.MoveNextPeer()) {
rv = rdr.ReadInt(Fld_id_next_id);
}
}
finally {rdr.Rls();}
return rv;
}
public int FetchNextAndCommit(String dbInfo, String url) {
Db_conn conn = Db_conn_pool.Instance.Get_or_new(dbInfo);
int rv = PoolIds.Instance.FetchNext(conn, url);
PoolIds.Instance.Commit(conn, url, rv + 1);
return rv;
}
public void Commit(Db_conn conn, String url, int val) {
int rv = conn.Exec_qry(Db_qry_.update_(Tbl_Name, Db_crt_.New_eq(Fld_id_path, url)).Val_str(Fld_id_path, url).Val_int(Fld_id_next_id, val));
if (rv == 0) {
rv = conn.Exec_qry(Db_qry_.insert_(Tbl_Name).Val_str(Fld_id_path, url).Val_int(Fld_id_next_id, val));
}
if (rv != 1) throw Err_.new_wo_type("failed to update nextId", "url", url, "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 Instance = new PoolIds(); PoolIds() {}
}

View File

@@ -1,56 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.dbs.utls; import gplx.*; import gplx.dbs.*;
import org.junit.*;
public class PoolIds_tst {
@Before public void setup() {
conn = Db_conn_pool.Instance.Get_or_new(Db_conn_info_.Test);
Db_qry_fxt.DeleteAll(conn, PoolIds.Tbl_Name);
mgr = PoolIds.Instance;
}
@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(conn, url, expd);
}
void tst_Fetch(String url, int expd) {
int actl = mgr.FetchNext(conn, url);
Tfds.Eq(expd, actl);
}
Db_conn conn;
PoolIds mgr;
}