1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-07-12 21:10:02 -04:00
commit 794b5a232f
3099 changed files with 238212 additions and 0 deletions

View 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.utls; import gplx.*; import gplx.dbs.*;
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.I.SaveFilStr_args(bkpCmdFil, cmdText).Exec(); // explicitly state utf8;
ProcessAdp.run_wait_(bkpCmdFil);
Io_mgr.I.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() {}
}

View 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.utls; import gplx.*; import gplx.dbs.*;
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());
}
}

View 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.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_.I;
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

@@ -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.utls; import gplx.*; import gplx.dbs.*;
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_.eq_(Fld_id_path, url));
int rv = 0;//boolean isNew = true;
DataRdr rdr = DataRdr_.Null;
try {
rdr = cmd.Exec_qry_as_rdr(conn);
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.I.Get_or_new(dbInfo);
int rv = PoolIds._.FetchNext(conn, url);
PoolIds._.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_.eq_(Fld_id_path, url)).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
if (rv == 0) {
rv = conn.Exec_qry(Db_qry_.insert_(Tbl_Name).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
}
if (rv != 1) throw Exc_.new_("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 _ = new PoolIds(); PoolIds() {}
}

View 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.utls; import gplx.*; import gplx.dbs.*;
import org.junit.*;
public class PoolIds_tst {
@Before public void setup() {
conn = Db_conn_pool.I.Get_or_new(Db_conn_info_.Test);
Db_qry_fxt.DeleteAll(conn, 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(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;
}