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,24 +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.core.srls; import gplx.*; import gplx.core.*;
public class Dbmeta_dat_itm {
public Dbmeta_dat_itm(int tid, String key, Object val) {this.Tid = tid; this.Key = key; this.Val = val;}
public int Tid;
public String Key;
public Object Val;
}

View File

@@ -1,30 +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.core.srls; import gplx.*; import gplx.core.*;
import gplx.dbs.*; import gplx.dbs.metas.*;
public class Dbmeta_dat_mgr {
private final Ordered_hash hash = Ordered_hash_.New();
public Dbmeta_dat_mgr Clear() {hash.Clear(); return this;}
public int Len() {return hash.Count();}
public Dbmeta_dat_itm Get_at(int idx) {return (Dbmeta_dat_itm)hash.Get_at(idx);}
public Dbmeta_dat_mgr Add_int(String key, int val) {
Dbmeta_dat_itm itm = new Dbmeta_dat_itm(Dbmeta_fld_tid.Tid__int, key, val);
hash.Add(key, itm);
return this;
}
}

View File

@@ -1,23 +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.core.srls; import gplx.*; import gplx.core.*;
public interface Gfo_srl_ctx {
Gfo_srl_mgr_wtr Wtr_bgn(String key);
Gfo_srl_mgr_rdr Rdr_bgn(String key);
Dbmeta_dat_mgr Rdr_subs(String key);
}

View File

@@ -1,23 +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.core.srls; import gplx.*; import gplx.core.*;
public interface Gfo_srl_itm {
Gfo_srl_itm Make_new(Gfo_srl_ctx ctx);
void Load(Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_mgr_rdr rdr);
void Save(Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_mgr_wtr wtr);
}

View File

@@ -1,26 +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.core.srls; import gplx.*; import gplx.core.*;
public interface Gfo_srl_mgr_rdr {
void Itm_bgn(String key);
void Itm_end();
boolean Get_bool (String key);
int Get_int (String key);
String Get_str (String key);
Object Get_subs (Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_itm proto, Dbmeta_dat_mgr crt_mgr);
}

View File

@@ -1,27 +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.core.srls; import gplx.*; import gplx.core.*;
public interface Gfo_srl_mgr_wtr {
int Uid_next__as_int();
void Itm_bgn(String key);
void Itm_end();
void Set_bool (String key, boolean val);
void Set_int (String key, int val);
void Set_str (String key, String val);
Object Set_subs (Gfo_srl_ctx ctx, Gfo_srl_itm owner, Gfo_srl_itm proto, Object subs, Dbmeta_dat_mgr crt_mgr);
}

View File

@@ -1,27 +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.core.stores; import gplx.*; import gplx.core.*;
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;
}
}

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.core.stores; import gplx.*; import gplx.core.*;
public class DbMaprItm {
public String TableName() {return tableName;} public DbMaprItm TableName_(String val) {tableName = val; return this;} private String tableName;
public Ordered_hash Flds() {return flds;} Ordered_hash flds = Ordered_hash_.New();
public Hash_adp ContextFlds() {return contextFlds;} Hash_adp contextFlds = Hash_adp_.New();
public Hash_adp ConstantFlds() {return constantFlds;} Hash_adp constantFlds = Hash_adp_.New();
public List_adp 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.Get_by(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_.new_missing_key(find);
}
public DbMaprArg Flds_get(String key) {return (DbMaprArg)flds.Get_by(key);}
SrlObj proto; String key; List_adp subs = List_adp_.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() {}
}

View File

@@ -1,48 +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.core.stores; import gplx.*; import gplx.core.*;
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 List_adp OwnerStack() {return ownerStack;} List_adp ownerStack = List_adp_.New();
public Ordered_hash ContextVars() {return contextVars;} Ordered_hash contextVars = Ordered_hash_.New();
public List_adp MaprStack() {return maprStack;} List_adp maprStack = List_adp_.New();
public void EnvStack_add(DbMaprItm mapr, SrlObj gobj) {
for (Object argObj : mapr.ContextFlds()) {
DbMaprArg arg = (DbMaprArg)argObj;
Object contextVal = Gfo_invk_.Invk_by_key((Gfo_invk)gobj, arg.ObjProp());
this.ContextVars().Add_if_dupe_use_nth(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());
}
List_adp_.Del_at_last(this.OwnerStack());
List_adp_.Del_at_last(this.MaprStack());
}
public void Clear() {
ownerStack.Clear();
contextVars.Clear();
maprStack.Clear();
}
public static DbMaprMgr new_() {return new DbMaprMgr();} DbMaprMgr() {}
}

View File

@@ -1,140 +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.core.stores; import gplx.*; import gplx.core.*;
import org.junit.*;
import gplx.dbs.*; /*Db_conn_info*/
public class DbMaprMgr_tst {
@Before public void setup() {
mgr = DbMaprMgr.new_().RootIndexFlds_(DbMaprArg.new_("id", "disc_id"))
.Root_
( DbMaprItm.proto_(MockDisc.Instance, "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.Instance, "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.Instance, "chapters", "mock_chapters")
. Flds_add(MockChapter.id_idk, "chapter_id").Flds_add(MockChapter.name_idk, "chapter_name")
, DbMaprItm.proto_(MockStream.Instance, "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.Instance, "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_conn_info_.Test);
wtr.EnvVars().Add(DbMaprWtr.Key_Mgr, mgr);
conn = Db_conn_pool.Instance.Get_or_new(Db_conn_info_.Test);
Db_qry_fxt.DeleteAll(conn, "mock_discs", "mock_titles", "mock_chapters", "mock_streams");
} DbMaprMgr mgr; DbMaprWtr wtr; Db_conn conn; 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(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1));
DbMaprWtrUtl.PurgeObjTree(disc, mgr, conn);
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(conn, "mock_discs"));
}
@Test public void PurgeObjTree_deep() {
disc = MockDisc.new_().Id_(1);
Db_qry_fxt.Insert_kvo(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1));
Db_qry_fxt.Insert_kvo(conn, "mock_titles", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1));
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 2).Add("chapter_id", 3));
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", Keyval_list.New_with_one("disc_id", 2).Add("title_id", 2).Add("chapter_id", 3));
DbMaprWtrUtl.PurgeObjTree(disc, mgr, conn);
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(conn, "mock_discs"));
Tfds.Eq(0, Db_qry_fxt.SelectAll_count(conn, "mock_titles"));
Tfds.Eq(1, Db_qry_fxt.SelectAll_count(conn, "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(conn, "mock_discs", Db_mock_row.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(conn, "mock_discs", Db_mock_row.vals_only_(1, "disc"));
Db_qry_fxt.tst_Select(conn, "mock_titles", Db_mock_row.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(conn, "mock_discs", Db_mock_row.vals_only_(1, "disc"));
Db_qry_fxt.tst_Select(conn, "mock_titles", Db_mock_row.vals_only_(1, 2, "title"));
Db_qry_fxt.tst_Select(conn, "mock_chapters", Db_mock_row.vals_only_(1, 2, 3, "chap"));
Db_qry_fxt.tst_Select(conn, "mock_streams"
, Db_mock_row.vals_only_(1, 2, null, 4, "audio")
, Db_mock_row.vals_only_(1, 2, null, 5, "subtitle")
);
}
@Test public void Load_root() {
rdr = rdr_();
Db_qry_fxt.Insert_kvo(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1).Add("disc_name", "name"));
disc = (MockDisc)rdr.StoreRoot(MockDisc.Instance, 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(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1).Add("disc_name", "name"));
Db_qry_fxt.Insert_kvo(conn, "mock_titles", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
Db_qry_fxt.Insert_kvo(conn, "mock_titles", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 2).Add("title_name", "title2"));
disc = (MockDisc)rdr.StoreRoot(MockDisc.Instance, null);
Tfds.Eq(1, disc.Id());
Tfds.Eq("name", disc.Name());
Tfds.Eq(2, disc.Titles().Count());
Tfds.Eq("title1", ((MockTitle)disc.Titles().Get_at(0)).Name());
Tfds.Eq("title2", ((MockTitle)disc.Titles().Get_at(1)).Name());
}
@Test public void Load_deep() {
rdr = rdr_();
Db_qry_fxt.Insert_kvo(conn, "mock_discs", Keyval_list.New_with_one("disc_id", 1).Add("disc_name", "name"));
Db_qry_fxt.Insert_kvo(conn, "mock_titles", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("title_name", "title1"));
Db_qry_fxt.Insert_kvo(conn, "mock_chapters", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("chapter_id", 3).Add("chapter_name", "chapter1"));
Db_qry_fxt.Insert_kvo(conn, "mock_streams", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("stream_id", 4).Add("stream_type", 0).Add("stream_name", "audio1"));
Db_qry_fxt.Insert_kvo(conn, "mock_streams", Keyval_list.New_with_one("disc_id", 1).Add("title_id", 1).Add("stream_id", 5).Add("stream_type", 1).Add("stream_name", "subtitle1"));
disc = (MockDisc)rdr.StoreRoot(MockDisc.Instance, null);
Tfds.Eq(1, disc.Id());
Tfds.Eq("name", disc.Name());
Tfds.Eq(1, disc.Titles().Count());
MockTitle t = ((MockTitle)disc.Titles().Get_at(0));
Tfds.Eq("title1", t.Name());
Tfds.Eq("chapter1", ((MockChapter)t.Chapters().Get_at(0)).Name());
Tfds.Eq(1, t.Audios().Count());
Tfds.Eq(1, t.Subtitles().Count());
Tfds.Eq("audio1", ((MockStream)t.Audios().Get_at(0)).Name());
Tfds.Eq("subtitle1", ((MockStream)t.Subtitles().Get_at(0)).Name());
}
DbMaprRdr rdr_() {
DbMaprRdr rv = DbMaprRdr.new_(Db_conn_info_.Test, Db_crt_.New_eq("disc_id", 1));
rv.EnvVars().Add(DbMaprWtr.Key_Mgr, mgr);
return rv;
}
}

View File

@@ -1,122 +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.core.stores; import gplx.*; import gplx.core.*;
import gplx.core.criterias.*; import gplx.dbs.*; import gplx.core.gfo_ndes.*;
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().Get_by_or_fail(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_wo_type("criteria returned > 1 row", "criteria", rootCrt.To_str(), "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, List_adp list, SrlObj subProto, String itmKey) {
DbMaprItm curMapr = (DbMaprItm)mgr.MaprStack().Get_at_last();
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); List_adp_.Del_at_last(rowStack);
}
}
Criteria MakeCrt(DbMaprMgr mgr, DbMaprItm mapr) {
Criteria rv = null, cur = null;
List_adp list = GetIdxFlds(mgr, mapr);
for (Object kvObj : list) {
Keyval kv = (Keyval)kvObj;
cur = Db_crt_.New_eq(kv.Key(), kv.Val());
rv = (rv == null) ? cur : Criteria_.And(rv, cur);
}
return rv;
}
List_adp GetIdxFlds(DbMaprMgr mgr, DbMaprItm curMapr) {
List_adp rv = List_adp_.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().Get_at(i);
SrlObj gobj = (SrlObj)mgr.OwnerStack().Get_at(i);
for (Object argObj : mapr.ContextFlds()) {
DbMaprArg arg = (DbMaprArg)argObj;
Object propVal = Gfo_invk_.Invk_by_key((Gfo_invk)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.Get_by(key));
if (tblByRootCrt == null) {
DataRdr dbRdr = null;
try {
dbRdr = conn.Exec_qry_as_old_rdr(Db_qry_.select_().From_(mapr.TableName()).Where_(rootCrt));
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_.new_missing_idx(i, ndeList.Count());
rowStack.Add(tbl.Subs().FetchAt_asGfoNde(i));
}
@Override public Object Read(String key) {
DbMaprItm mapr = (DbMaprItm)mgr.MaprStack().Get_at_last();
GfoNde row = (GfoNde)rowStack.Get_at_last();
DbMaprArg arg = mapr.Flds_get(key);
Object dbVal = null; try {dbVal = row.Read(arg.DbFld());} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to read dbVal from row", "key", key, "fld", arg.DbFld());}
return dbVal;
}
@Override public DataRdr Subs_byName_moveFirst(String name) {throw Err_.new_unimplemented();}
@Override public DataRdr Subs() {throw Err_.new_unimplemented();}
@Override public int FieldCount() {throw Err_.new_unimplemented();}
@Override public String KeyAt(int i) {throw Err_.new_unimplemented();}
@Override public Object ReadAt(int i) {throw Err_.new_unimplemented();}
@Override public Keyval KeyValAt(int i) {throw Err_.new_unimplemented();}
@Override public SrlMgr SrlMgr_new(Object o) {return new DbMaprRdr();}
Hash_adp tables = Hash_adp_.New();
Db_conn conn; Criteria rootCrt;
DbMaprMgr mgr; List_adp rowStack = List_adp_.New();
public static DbMaprRdr new_(Db_conn_info dbInfo, Criteria rootCrt) {
DbMaprRdr rv = new DbMaprRdr();
rv.conn = Db_conn_pool.Instance.Get_or_new(dbInfo); rv.rootCrt = rootCrt;
return rv;
} DbMaprRdr() {}
}

View File

@@ -1,115 +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.core.stores; import gplx.*; import gplx.core.*;
import gplx.core.criterias.*; import gplx.core.gfo_ndes.*;
import gplx.dbs.*; import gplx.dbs.qrys.*;
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().Get_by_or_fail(DbMaprWtr.Key_Mgr);
DbMaprWtrUtl.PurgeObjTree(root, mgr, conn);
WriteGfoObj(root, mgr.Root());
mgr.Clear();
return null;
}
@Override public void SrlList(String subPropKey, List_adp list, SrlObj subProto, String itmKey) {
DbMaprItm ownerMapr = (DbMaprItm)mgr.MaprStack().Get_at_last();
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().Get_at(i);
SrlObj gobj = (SrlObj)mgr.OwnerStack().Get_at(i);
for (Object argObj : mapr.ContextFlds()) {
DbMaprArg arg = (DbMaprArg)argObj;
Object argVal = Gfo_invk_.Invk_by_key((Gfo_invk)gobj, arg.ObjProp());
this.WriteDataVal(arg.DbFld(), argVal);
}
}
}
@Override public void WriteNodeBgn(String v) {
if (insertCmd != null) insertCmd.Exec_qry(conn); // 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().Get_at_last();
String fld = ""; try {fld = ownerMapr.Flds_get(name).DbFld();} catch (Exception e) {throw Err_.new_exc(e, "db", "failed to fetch fld from mapr", "key", name);}
WriteDataVal(fld, val);
}
void WriteDataVal(String fld, Object val) {
if (insertCmd == null) insertCmd = Db_qry_.insert_(curTableName);
if (Type_adp_.Eq_typeSafe(val, String.class))
insertCmd.Val_obj_type(fld, val, Db_val_type.Tid_varchar);
else
insertCmd.Val_obj(fld, val);
}
@Override public void WriteNodeEnd() {
if (insertCmd != null) insertCmd.Exec_qry(conn); // 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 To_str() {return "";}
@Override public SrlMgr SrlMgr_new(Object o) {return new DbMaprWtr();}
DbMaprMgr mgr; Db_conn conn; String curTableName; Db_qry_insert insertCmd;
public static DbMaprWtr new_by_url_(Db_conn_info url) {
DbMaprWtr rv = new DbMaprWtr();
rv.conn = Db_conn_pool.Instance.Get_or_new(url);
return rv;
} DbMaprWtr() {}
public static final String Key_Mgr = "DbMapr.mgr";
}
class DbMaprWtrUtl {
public static void PurgeObjTree(SrlObj root, DbMaprMgr mgr, Db_conn conn) {
Criteria crt = MakeCriteria(root, mgr.RootIndexFlds());
PurgeObj(mgr.Root(), crt, conn);
}
static Criteria MakeCriteria(SrlObj root, DbMaprArg[] objRootIdxFlds) {
Criteria rv = null;
for (DbMaprArg arg : objRootIdxFlds) {
Object argVal = Gfo_invk_.Invk_by_key((Gfo_invk)root, arg.ObjProp());
Criteria cur = Db_crt_.New_eq(arg.DbFld(), argVal);
rv = (rv == null) ? cur : Criteria_.And(rv, cur);
}
return rv;
}
static void PurgeObj(DbMaprItm mapr, Criteria crt, Db_conn conn) {
Db_qry_.delete_(mapr.TableName(), crt).Exec_qry(conn);
for (Object subObj : mapr.Subs()) {
DbMaprItm sub = (DbMaprItm)subObj;
PurgeObj(sub, crt, conn);
}
}
}

View File

@@ -1,81 +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.core.stores; import gplx.*; import gplx.core.*;
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 To_str() {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 + List_adp_.Base1);}
catch (SQLException e) {throw Err_.new_exc(e, "db", "get columnName failed", "i", i, "commandText", commandText);}
return rv;
}
@Override public Object ReadAt(int i) {
Object rv;
try {rv = rdr.getObject(i + List_adp_.Base1);} catch(Exception exc) {throw Err_.new_wo_type("could not read val from dataReader; idx not found or rdr not open", "idx", i, "sql", commandText);}
return rv;
}
@Override public Object Read(String key) {
Object rv;
try {rv = rdr.getObject(key);} catch(Exception exc) {throw Err_.new_wo_type("could not read val from dataReader; key not found or rdr may not be open", "key", key, "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 Decimal_adp ReadDecimal(String key) {return Decimal_adp_.db_(this.Read(key));}
@Override public gplx.core.ios.streams.Io_stream_rdr ReadRdr(String key) {
try {
java.io.InputStream input_stream = rdr.getBinaryStream(key);
return gplx.core.ios.streams.Io_stream_rdr_.New__raw(input_stream);
}
catch (SQLException e) {return gplx.core.ios.streams.Io_stream_rdr_.Noop;}
}
public boolean MoveNextPeer() {
try {return rdr.next();}
catch (Exception e) {throw Err_.new_exc(e, "db", "could not move next; check column casting error in SQL", "sql", commandText);}
}
@Override public DataRdr Subs() {throw Err_.new_unimplemented();}
public DataRdr Subs_byName(String fld) {throw Err_.new_unimplemented();}
@Override public DataRdr Subs_byName_moveFirst(String fld) {throw Err_.new_unimplemented();}
public void Rls() {
try {rdr.close();}
catch (SQLException e) {throw Err_.new_exc(e, "db", "reader dispose failed", "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_.new_exc(e, "xo", "get columnCount failed", "commandText", commandText);}
return this;
}
@Override public SrlMgr SrlMgr_new(Object o) {return new Db_data_rdr();}
}

View File

@@ -1,22 +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.core.stores; import gplx.*; import gplx.core.*;
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);}
}

View File

@@ -1,130 +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.core.stores; import gplx.*; import gplx.core.*;
class MockDisc implements SrlObj, Gfo_invk {
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 List_adp Titles() {return titles;} List_adp titles = List_adp_.New(); public static final String titles_idk = "titles";
public static final MockDisc Instance = 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 Gfo_invk_.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.Instance, "title");
// }
// }
// public static void DoIt(GfsCtx ctx, int ikey, String k, GfoMsg m, List_adp 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.Instance, "title");
}
public static MockDisc new_() {
MockDisc rv = new MockDisc();
return rv;
}
}
class MockTitle implements SrlObj, Gfo_invk {
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 List_adp Chapters() {return chapters;} List_adp chapters = List_adp_.New(); public static final String chapters_idk = "chapters";
public List_adp Audios() {return audios;} List_adp audios = List_adp_.New(); public static final String audios_idk = "audios";
public List_adp Subtitles() {return subtitles;} List_adp subtitles = List_adp_.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 Instance = 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 Gfo_invk_.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.Instance, "chapter");
// else if (String_.Eq(subMsg.Key(), audios_idk)) MockDisc.DoIt(ctx, ikey, k, subMsg, audios, MockStream.Instance, "audio");
// else if (String_.Eq(subMsg.Key(), subtitles_idk)) MockDisc.DoIt(ctx, ikey, k, subMsg, subtitles, MockStream.Instance, "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.Instance, "chapter");
mgr.SrlList(audios_idk, audios, MockStream.Instance, "audio");
mgr.SrlList(subtitles_idk, subtitles, MockStream.Instance, "subtitle");
}
}
class MockChapter implements SrlObj, Gfo_invk {
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 Instance = 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 Gfo_invk_.Rv_unhandled;
}
public void SrlObj_Srl(SrlMgr mgr) {
id = mgr.SrlIntOr(id_idk, id);
name = mgr.SrlStrOr(name_idk, name);
}
}
class MockStream implements SrlObj, Gfo_invk {
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_(List_adp list) {list.Add(this); return this;}
public static final MockStream Instance = 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 Gfo_invk_.Rv_unhandled;
}
public void SrlObj_Srl(SrlMgr mgr) {
id = mgr.SrlIntOr(id_idk, id);
name = mgr.SrlStrOr(name_idk, name);
}
}