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

Source: Restore broken commit

This commit is contained in:
gnosygnu
2017-02-06 22:14:55 -05:00
parent 938beac9f9
commit 3bfeb94b43
4380 changed files with 328018 additions and 0 deletions

View 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.metas; import gplx.*; import gplx.dbs.*;
public class Dbmeta_fld_mgr {
private final Ordered_hash hash = Ordered_hash_.New();
public int Len() {return hash.Count();}
public void Clear() {hash.Clear();}
public void Add(Dbmeta_fld_itm itm) {hash.Add(itm.Name(), itm);}
public boolean Has(String name) {return hash.Has(name);}
public Dbmeta_fld_itm Get_at(int idx) {return (Dbmeta_fld_itm)hash.Get_at(idx);}
public Dbmeta_fld_itm Get_by(String name) {return (Dbmeta_fld_itm)hash.Get_by(name);}
public Dbmeta_fld_itm[] To_ary() {return hash.Count() == 0 ? Dbmeta_fld_itm.Ary_empty : (Dbmeta_fld_itm[])hash.To_ary(Dbmeta_fld_itm.class);}
public Dbmeta_fld_list To_fld_list() {
Dbmeta_fld_list rv = new Dbmeta_fld_list();
int len = hash.Count();
for (int i = 0; i < len; ++i)
rv.Add(Get_at(i));
return rv;
}
}

View 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.metas; import gplx.*; import gplx.dbs.*;
public class Dbmeta_idx_fld {
public Dbmeta_idx_fld(String name, int sort_tid) {this.Name = name; this.Sort_tid = sort_tid;}
public String Name;
public int Sort_tid;
public boolean Eq(Dbmeta_idx_fld comp) {
return String_.Eq(Name, comp.Name)
&& Sort_tid == comp.Sort_tid;
}
public static final Dbmeta_idx_fld[] Ary_empty = new Dbmeta_idx_fld[0];
public static final int Sort_tid__none = 0, Sort_tid__asc = 1, Sort_tid__desc = 2;
public static boolean Ary_eq(Dbmeta_idx_fld[] lhs_ary, Dbmeta_idx_fld[] rhs_ary) {
int lhs_len = lhs_ary.length, rhs_len = rhs_ary.length;
if (lhs_len != rhs_len) return false;
for (int i = 0; i < lhs_len; ++i)
if (!lhs_ary[i].Eq(rhs_ary[i])) return false;
return true;
}
public static Dbmeta_idx_fld Dsc(String name) {return new Dbmeta_idx_fld(name, Sort_tid__desc);}
}

View 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.metas; import gplx.*; import gplx.dbs.*;
public class Dbmeta_idx_mgr {
private final Ordered_hash hash = Ordered_hash_.New();
public int Len() {return hash.Count();}
public boolean Has(String name) {return hash.Has(name);}
public Dbmeta_idx_itm Get_at(int idx) {return (Dbmeta_idx_itm)hash.Get_at(idx);}
public Dbmeta_idx_itm Get_by(String name) {return (Dbmeta_idx_itm)hash.Get_by(name);}
public void Add(Dbmeta_idx_itm itm) {hash.Add(itm.Name(), itm);}
public void Clear() {hash.Clear();}
public Dbmeta_idx_itm[] To_ary() {return (Dbmeta_idx_itm[])hash.To_ary(Dbmeta_idx_itm.class);}
}

View 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.metas; import gplx.*; import gplx.dbs.*;
public class Dbmeta_itm_tid {
public static final int Tid_unknown = 0, Tid_table = 1, Tid_index = 2;
public static final String Key_table = "table", Key_index = "index";
public static int Xto_int(String s) {
s = String_.Lower(s);
if (String_.Eq(s, Key_table)) return Tid_table;
else if (String_.Eq(s, Key_index)) return Tid_index;
else return Tid_unknown;
}
}

View File

@@ -0,0 +1,21 @@
/*
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.metas; import gplx.*; import gplx.dbs.*;
public interface Dbmeta_reload_cmd {
void Load_all();
}

View 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.metas; import gplx.*; import gplx.dbs.*;
public class Dbmeta_reload_cmd_ {
public static final Dbmeta_reload_cmd Noop = new Dbmeta_reload_cmd__noop();
}
class Dbmeta_reload_cmd__noop implements Dbmeta_reload_cmd {
public void Load_all() {}
}

View 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.metas; import gplx.*; import gplx.dbs.*;
public class Dbmeta_tbl_mgr {
private final Ordered_hash hash = Ordered_hash_.New();
private final Dbmeta_reload_cmd load_cmd;
public Dbmeta_tbl_mgr(Dbmeta_reload_cmd load_cmd) {this.load_cmd = load_cmd;}
public int Len() {return hash.Count();}
public boolean Has(String name) {return hash.Has(name);}
public Dbmeta_tbl_itm Get_at(int i) {return (Dbmeta_tbl_itm)hash.Get_at(i);}
public Dbmeta_tbl_itm Get_by(String name) {return (Dbmeta_tbl_itm)hash.Get_by(name);}
public void Add(Dbmeta_tbl_itm itm) {hash.Add_if_dupe_use_nth(itm.Name(), itm);}
public void Clear() {hash.Clear();}
public Dbmeta_tbl_mgr Load_all() {load_cmd.Load_all(); return this;}
}

View File

@@ -0,0 +1,148 @@
/*
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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import gplx.core.brys.*; import gplx.core.btries.*;
abstract class Dbmeta_fld_wkr__base {
private byte[] hook;
private final Btrie_slim_mgr words_trie = Btrie_slim_mgr.ci_a7();
private int words_len;
@gplx.Virtual public int Tid() {return Tid_other;}
public void Ctor(byte[] hook, byte[]... words_ary) {
this.hook = hook;
this.words_len = words_ary.length;
for (byte i = 0; i < words_len; ++i)
words_trie.Add_bry_byte(words_ary[i], i);
}
public void Reg(Btrie_slim_mgr trie) {
trie.Add_obj(hook, this);
}
@gplx.Virtual public void Match(Bry_rdr rdr, Dbmeta_fld_itm fld) {
int words_len = words_trie.Count();
for (int i = 0; i < words_len; ++i) {
rdr.Skip_ws();
rdr.Chk(words_trie);
}
When_match(fld);
}
protected abstract void When_match(Dbmeta_fld_itm fld);
public static final int Tid_end_comma = 1, Tid_end_paren = 2, Tid_other = 3;
}
class Dbmeta_fld_wkr__end_comma extends Dbmeta_fld_wkr__base {
public Dbmeta_fld_wkr__end_comma() {this.Ctor(Hook);}
@Override public int Tid() {return Tid_end_comma;}
@Override protected void When_match(Dbmeta_fld_itm fld) {}
private static final byte[] Hook = Bry_.new_a7(",");
public static final Dbmeta_fld_wkr__end_comma Instance = new Dbmeta_fld_wkr__end_comma();
}
class Dbmeta_fld_wkr__end_paren extends Dbmeta_fld_wkr__base {
public Dbmeta_fld_wkr__end_paren() {this.Ctor(Hook);}
@Override public int Tid() {return Tid_end_paren;}
@Override protected void When_match(Dbmeta_fld_itm fld) {}
private static final byte[] Hook = Bry_.new_a7(")");
public static final Dbmeta_fld_wkr__end_paren Instance = new Dbmeta_fld_wkr__end_paren();
}
class Dbmeta_fld_wkr__nullable_null extends Dbmeta_fld_wkr__base {
public Dbmeta_fld_wkr__nullable_null() {this.Ctor(Hook);}
@Override protected void When_match(Dbmeta_fld_itm fld) {
fld.Nullable_tid_(Dbmeta_fld_itm.Nullable_null);
}
private static final byte[] Hook = Bry_.new_a7("null");
public static final Dbmeta_fld_wkr__nullable_null Instance = new Dbmeta_fld_wkr__nullable_null();
}
class Dbmeta_fld_wkr__nullable_not extends Dbmeta_fld_wkr__base {
public Dbmeta_fld_wkr__nullable_not() {this.Ctor(Hook, Bry_null);}
@Override protected void When_match(Dbmeta_fld_itm fld) {
fld.Nullable_tid_(Dbmeta_fld_itm.Nullable_not_null);
}
private static final byte[] Hook = Bry_.new_a7("not"), Bry_null = Bry_.new_a7("null");
public static final Dbmeta_fld_wkr__nullable_not Instance = new Dbmeta_fld_wkr__nullable_not();
}
class Dbmeta_fld_wkr__primary_key extends Dbmeta_fld_wkr__base {
public Dbmeta_fld_wkr__primary_key() {this.Ctor(Hook, Bry_key);}
@Override protected void When_match(Dbmeta_fld_itm fld) {
fld.Primary_y_();
}
private static final byte[] Hook = Bry_.new_a7("primary"), Bry_key = Bry_.new_a7("key");
public static final Dbmeta_fld_wkr__primary_key Instance = new Dbmeta_fld_wkr__primary_key();
}
class Dbmeta_fld_wkr__autonumber extends Dbmeta_fld_wkr__base {
public Dbmeta_fld_wkr__autonumber() {this.Ctor(Hook);}
@Override protected void When_match(Dbmeta_fld_itm fld) {
fld.Autonum_y_();
}
private static final byte[] Hook = Bry_.new_a7("autoincrement");
public static final Dbmeta_fld_wkr__autonumber Instance = new Dbmeta_fld_wkr__autonumber();
}
class Dbmeta_fld_wkr__default extends Dbmeta_fld_wkr__base {
public Dbmeta_fld_wkr__default() {this.Ctor(Hook);}
@Override public void Match(Bry_rdr rdr, Dbmeta_fld_itm fld) {
Object default_val = null;
rdr.Skip_ws();
byte[] src = rdr.Src();
switch (fld.Type().Tid_ansi()) {
case Dbmeta_fld_tid.Tid__bool:
case Dbmeta_fld_tid.Tid__byte:
case Dbmeta_fld_tid.Tid__short:
case Dbmeta_fld_tid.Tid__int: default_val = Int_.parse(Read_str_to_end_of_num(rdr)); break;
case Dbmeta_fld_tid.Tid__long: default_val = Long_.parse(Read_str_to_end_of_num(rdr)); break;
case Dbmeta_fld_tid.Tid__float: default_val = Float_.parse(Read_str_to_end_of_num(rdr)); break;
case Dbmeta_fld_tid.Tid__double: default_val = Double_.parse(Read_str_to_end_of_num(rdr)); break;
case Dbmeta_fld_tid.Tid__decimal: default_val = Decimal_adp_.parse(Read_str_to_end_of_num(rdr)); break;
case Dbmeta_fld_tid.Tid__str:
case Dbmeta_fld_tid.Tid__text:
case Dbmeta_fld_tid.Tid__bry:
byte b = src[rdr.Pos()];
int bgn_pos = rdr.Pos() + 1;
int end_pos = Bry_find_.Find_fwd(src, b, bgn_pos); if (end_pos == Bry_find_.Not_found) rdr.Err_wkr().Fail("unclosed quote");
default_val = Bry_.Mid(src, bgn_pos, end_pos);
rdr.Move_to(end_pos + 1);
break;
case Dbmeta_fld_tid.Tid__date: throw Err_.new_unhandled_default(fld.Type().Tid_ansi());
}
fld.Default_(default_val);
}
public String Read_str_to_end_of_num(Bry_rdr rdr) {
int bgn = rdr.Pos();
int pos = bgn, end = bgn;
int src_end = rdr.Src_end();
byte[] src = rdr.Src();
boolean loop = true;
while (loop) {
if (pos >= src_end) {
end = src_end;
break;
}
byte b = src[pos]; ++pos;
switch (b) {
case Byte_ascii.Space: case Byte_ascii.Tab: case Byte_ascii.Nl: case Byte_ascii.Cr:
case Byte_ascii.Paren_end:
case Byte_ascii.Comma:
end = pos - 1;
loop = false;
break;
default:
break;
}
}
rdr.Move_to(end);
return String_.new_a7(src, bgn, end);
}
@Override protected void When_match(Dbmeta_fld_itm fld) {}
private static final byte[] Hook = Bry_.new_a7("default");
public static final Dbmeta_fld_wkr__default Instance = new Dbmeta_fld_wkr__default();
}

View File

@@ -0,0 +1,123 @@
/*
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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import gplx.core.brys.*; import gplx.core.btries.*;
import gplx.dbs.engines.sqlite.*;
public class Dbmeta_parser__fld {
private final Btrie_rv trv = new Btrie_rv();
public Dbmeta_fld_itm Parse_fld(Sql_bry_rdr rdr) { // starts after "(" or ","; EX: "(fld1 int", ", fld2 int"; ends at ")"
byte[] name = rdr.Read_sql_identifier();
Dbmeta_fld_tid type = this.Parse_type(rdr);
Dbmeta_fld_itm fld = new Dbmeta_fld_itm(String_.new_u8(name), type);
byte[] src = rdr.Src(); int src_len = rdr.Src_end();
while (true) {
rdr.Skip_ws();
if (rdr.Pos() == src_len) return fld; // eos
switch (src[rdr.Pos()]) {
case Byte_ascii.Comma: return fld;
case Byte_ascii.Paren_end: return fld;
case Byte_ascii.Dash:
int nxt_pos = rdr.Pos() + 1;
if (src[nxt_pos] == Byte_ascii.Dash) {
nxt_pos = Bry_find_.Find_fwd(src, Byte_ascii.Nl, nxt_pos);
rdr.Move_to(nxt_pos + 1);
}
else {
throw Err_.new_("sqls.dbs", "expected double dash for comment");
}
return fld;
}
Dbmeta_fld_wkr__base type_wkr = (Dbmeta_fld_wkr__base)rdr.Chk_trie_as_obj(trv, fld_trie);
switch (type_wkr.Tid()) {
case Dbmeta_fld_wkr__base.Tid_end_comma:
case Dbmeta_fld_wkr__base.Tid_end_paren: return fld;
default:
rdr.Move_to(trv.Pos());
type_wkr.Match(rdr, fld);
break;
}
}
}
@gplx.Internal protected Dbmeta_fld_tid Parse_type(Bry_rdr rdr) {
rdr.Skip_ws();
Dbmeta_parser__fld_itm type_itm = (Dbmeta_parser__fld_itm)rdr.Chk_trie_as_obj(trv, type_trie);
rdr.Move_by(type_itm.Word().length);
int paren_itms_count = type_itm.Paren_itms_count();
int len_1 = Int_.Min_value, len_2 = Int_.Min_value;
if (paren_itms_count > 0) {
rdr.Skip_ws().Chk(Byte_ascii.Paren_bgn);
len_1 = rdr.Skip_ws().Read_int_to_non_num(); if (len_1 == Int_.Min_value) rdr.Err_wkr().Fail("invalid fld len_1");
if (paren_itms_count == 2) {
rdr.Skip_ws().Chk(Byte_ascii.Comma);
len_2 = rdr.Skip_ws().Read_int_to_non_num(); if (len_2 == Int_.Min_value) rdr.Err_wkr().Fail("invalid fld len_2");
}
rdr.Skip_ws().Chk(Byte_ascii.Paren_end);
}
return new Dbmeta_fld_tid(type_itm.Tid_ansi(), type_itm.Tid_sqlite(), type_itm.Word(), len_1, len_2);
}
private static final Btrie_slim_mgr fld_trie = fld_trie_init
( Dbmeta_fld_wkr__nullable_null.Instance
, Dbmeta_fld_wkr__nullable_not.Instance
, Dbmeta_fld_wkr__autonumber.Instance
, Dbmeta_fld_wkr__primary_key.Instance
, Dbmeta_fld_wkr__default.Instance
);
private static Btrie_slim_mgr fld_trie_init(Dbmeta_fld_wkr__base... wkrs) {
Btrie_slim_mgr rv = Btrie_slim_mgr.ci_a7();
for (Dbmeta_fld_wkr__base wkr : wkrs)
wkr.Reg(rv);
return rv;
}
private static final Btrie_slim_mgr type_trie = type_trie_init();
private static Btrie_slim_mgr type_trie_init() {
Btrie_slim_mgr rv = Btrie_slim_mgr.ci_a7();
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__byte , Sqlite_tid.Tid_int , 0, "tinyint", "int2");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__short , Sqlite_tid.Tid_int , 0, "smallint");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__int , Sqlite_tid.Tid_int , 0, "int", "integer", "mediumint");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__long , Sqlite_tid.Tid_int , 0, "bigint", "int8"); // "UNSIGNED BIG INT"
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__str , Sqlite_tid.Tid_text , 1, "character", "varchar", "nchar"); // "varying character", "native character"
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__text , Sqlite_tid.Tid_text , 0, "text", "clob");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__bry , Sqlite_tid.Tid_none , 0, "blob", "mediumblob");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__float , Sqlite_tid.Tid_real , 0, "float");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__double , Sqlite_tid.Tid_real , 0, "real", "double"); // "double precision"
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__decimal , Sqlite_tid.Tid_numeric , 0, "numeric");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__decimal , Sqlite_tid.Tid_numeric , 2, "decimal");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__decimal , Sqlite_tid.Tid_numeric , 2, "decimal");
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__bool , Sqlite_tid.Tid_numeric , 0, "boolean", "bit"); // "bit" is not SQLITE
Dbmeta_parser__fld_itm.reg_many(rv, Dbmeta_fld_tid.Tid__date , Sqlite_tid.Tid_numeric , 0, "date", "datetime");
return rv;
}
}
class Dbmeta_parser__fld_itm {
public Dbmeta_parser__fld_itm(int tid_ansi, int tid_sqlite, byte[] word, int paren_itms_count) {
this.tid_ansi = tid_ansi; this.tid_sqlite = tid_sqlite;
this.word = word; this.paren_itms_count = paren_itms_count;
}
public int Tid_ansi() {return tid_ansi;} private final int tid_ansi;
public int Tid_sqlite() {return tid_sqlite;} private final int tid_sqlite;
public byte[] Word() {return word;} private final byte[] word;
public int Paren_itms_count() {return paren_itms_count;} private final int paren_itms_count;
public static void reg_many(Btrie_slim_mgr trie, int tid_ansi, int tid_sqlite, int paren_itms_count, String... names_str) {
int len = names_str.length;
for (int i = 0; i < len; ++i) {
byte[] name_bry = Bry_.new_a7(names_str[i]);
Dbmeta_parser__fld_itm itm = new Dbmeta_parser__fld_itm(tid_ansi, tid_sqlite, name_bry, paren_itms_count);
trie.Add_obj(name_bry, itm);
}
}
}

View File

@@ -0,0 +1,74 @@
/*
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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import org.junit.*;
public class Dbmeta_parser__fld_tst {
@Before public void init() {fxt.Clear();} private Dbmeta_parser__fld_fxt fxt = new Dbmeta_parser__fld_fxt();
@Test public void Parse_type() {
fxt.Test_parse_type("int" , fxt.Make_type(Dbmeta_fld_tid.Tid__int));
fxt.Test_parse_type("varchar(255)" , fxt.Make_type(Dbmeta_fld_tid.Tid__str, 255));
fxt.Test_parse_type("decimal(12,10)" , fxt.Make_type(Dbmeta_fld_tid.Tid__decimal, 12, 10));
fxt.Test_parse_type(" int" , fxt.Make_type(Dbmeta_fld_tid.Tid__int));
fxt.Test_parse_type(" decimal ( 12 , 10 )" , fxt.Make_type(Dbmeta_fld_tid.Tid__decimal, 12, 10));
}
@Test public void Parse_fld() {
fxt.Test_parse_fld("name_1 int" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_unknown));
fxt.Test_parse_fld("name_1 int null" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_null));
fxt.Test_parse_fld("name_1 int not null" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null));
fxt.Test_parse_fld("name_1 int not null autoincrement" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.N, Bool_.Y));
fxt.Test_parse_fld("name_1 int not null primary key" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N));
fxt.Test_parse_fld("name_1 int not null default -1" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N, -1));
fxt.Test_parse_fld("name_1 varchar(3) not null default 'abc'" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__str, Dbmeta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N, "abc"));
}
@Test public void Comment() {
fxt.Test_parse_fld("name_1 int --a\n" , fxt.Make_fld("name_1", Dbmeta_fld_tid.Tid__int, Dbmeta_fld_itm.Nullable_unknown));
}
}
class Dbmeta_parser__fld_fxt {
private final Dbmeta_parser__fld fld_parser = new Dbmeta_parser__fld();
private final Sql_bry_rdr rdr = new Sql_bry_rdr();
public void Clear() {}
public Dbmeta_fld_tid Make_type(int tid_ansi) {return new Dbmeta_fld_tid(tid_ansi, -1, null, Int_.Min_value, Int_.Min_value);}
public Dbmeta_fld_tid Make_type(int tid_ansi, int len_1) {return new Dbmeta_fld_tid(tid_ansi, -1, null, len_1, Int_.Min_value);}
public Dbmeta_fld_tid Make_type(int tid_ansi, int len_1, int len_2) {return new Dbmeta_fld_tid(tid_ansi, -1, null, len_1, len_2);}
public Dbmeta_fld_itm Make_fld(String name, int tid_ansi, int nullable) {return Make_fld(name, tid_ansi, nullable, false, false, null);}
public Dbmeta_fld_itm Make_fld(String name, int tid_ansi, int nullable, boolean autonumber, boolean primary_key) {return Make_fld(name, tid_ansi, nullable, false, false, null);}
public Dbmeta_fld_itm Make_fld(String name, int tid_ansi, int nullable, boolean autonumber, boolean primary_key, Object default_val) {
Dbmeta_fld_itm rv = new Dbmeta_fld_itm(name, Make_type(tid_ansi));
rv.Nullable_tid_(nullable);
if (autonumber) rv.Autonum_y_();
if (primary_key) rv.Primary_y_();
rv.Default_(default_val);
return rv;
}
public void Test_parse_type(String src, Dbmeta_fld_tid expd_type) {
rdr.Init_by_src(Bry_.new_u8(src));
Dbmeta_fld_tid actl_type = fld_parser.Parse_type(rdr);
Tfds.Eq(expd_type.Tid_ansi() , actl_type.Tid_ansi());
Tfds.Eq(expd_type.Len_1() , actl_type.Len_1());
Tfds.Eq(expd_type.Len_2() , actl_type.Len_2());
}
public void Test_parse_fld(String src, Dbmeta_fld_itm expd_fld) {
rdr.Init_by_src(Bry_.new_u8(src));
Dbmeta_fld_itm actl_fld = fld_parser.Parse_fld(rdr);
Tfds.Eq(expd_fld.Name() , actl_fld.Name());
Tfds.Eq(expd_fld.Type().Tid_ansi() , actl_fld.Type().Tid_ansi());
Tfds.Eq(expd_fld.Nullable_tid() , actl_fld.Nullable_tid());
Tfds.Eq(Object_.Xto_str_strict_or_empty(expd_fld.Default()), Object_.Xto_str_strict_or_empty(actl_fld.Default()));
}
}

View File

@@ -0,0 +1,58 @@
/*
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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import gplx.core.brys.*; import gplx.core.btries.*;
public class Dbmeta_parser__idx {
private final Sql_bry_rdr rdr = new Sql_bry_rdr();
private final List_adp tmp_list = List_adp_.New();
public Dbmeta_idx_itm Parse(byte[] src) {
rdr.Init_by_page(Bry_.Empty, src, src.length);
rdr.Skip_ws().Chk_trie_val(trie, Tid__create);
boolean unique = false;
byte tid = rdr.Skip_ws().Chk_or(trie, Byte_.Max_value_127);
switch (tid) {
case Tid__index: break;
case Tid__unique: rdr.Skip_ws().Chk_trie_val(trie, Tid__index); unique = true; break;
default: throw Err_.new_("db", "index parse failed; 'CREATE' should be followed by 'INDEX' or 'UNIQUE'", "src", src);
}
byte[] idx_name = rdr.Read_sql_identifier();
rdr.Skip_ws().Chk_trie_val(trie, Tid__on);
byte[] tbl_name = rdr.Read_sql_identifier();
rdr.Skip_ws().Chk(Byte_ascii.Paren_bgn);
while (true) {
byte[] fld_bry = rdr.Read_sql_identifier(); if (fld_bry == null) throw Err_.new_("db", "index parse failed; index field is not an identifier", "src", src);
// TODO_OLD: check for ASC / DESC
Dbmeta_idx_fld fld_itm = new Dbmeta_idx_fld(String_.new_u8(fld_bry), Dbmeta_idx_fld.Sort_tid__none);
tmp_list.Add(fld_itm);
byte sym = rdr.Skip_ws().Read_byte();
if (sym == Byte_ascii.Paren_end) break;
}
return new Dbmeta_idx_itm(unique, String_.new_u8(tbl_name), String_.new_u8(idx_name), (Dbmeta_idx_fld[])tmp_list.To_ary_and_clear(Dbmeta_idx_fld.class));
}
private static final byte Tid__create = 0, Tid__unique = 1, Tid__index = 2, Tid__on = 3;
private static final byte[]
Bry__create = Bry_.new_a7("create")
, Bry__unique = Bry_.new_a7("unique")
, Bry__index = Bry_.new_a7("index")
, Bry__on = Bry_.new_a7("on");
private static final Btrie_slim_mgr trie = Btrie_slim_mgr.ci_a7()
.Add_bry_byte(Bry__create , Tid__create)
.Add_bry_byte(Bry__unique , Tid__unique)
.Add_bry_byte(Bry__index , Tid__index)
.Add_bry_byte(Bry__on , Tid__on);
}

View 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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import org.junit.*;
public class Dbmeta_parser__idx_tst {
@Before public void init() {fxt.Clear();} private final Dbmeta_parser__idx_fxt fxt = new Dbmeta_parser__idx_fxt();
@Test public void Unique() {fxt.Test_parse("CREATE UNIQUE INDEX idx_1 ON tbl_1 (fld_1, fld_2, fld_3)" , fxt.Make_idx(Bool_.Y, "idx_1", "tbl_1", "fld_1", "fld_2", "fld_3"));}
@Test public void Normal() {fxt.Test_parse("CREATE INDEX idx_1 ON tbl_1 (fld_1, fld_2, fld_3)" , fxt.Make_idx(Bool_.N, "idx_1", "tbl_1", "fld_1", "fld_2", "fld_3"));}
@Test public void Fld_1() {fxt.Test_parse("CREATE INDEX idx_1 ON tbl_1 (fld_1)" , fxt.Make_idx(Bool_.N, "idx_1", "tbl_1", "fld_1"));}
}
class Dbmeta_parser__idx_fxt {
private final Dbmeta_parser__idx parser = new Dbmeta_parser__idx();
public void Clear() {}
public Dbmeta_idx_itm Make_idx(boolean unique, String idx_name, String tbl_name, String... fld_names) {return new Dbmeta_idx_itm(unique, tbl_name, idx_name, Dbmeta_idx_itm.To_fld_ary(fld_names));}
public void Test_parse(String src, Dbmeta_idx_itm expd) {
Dbmeta_idx_itm actl = parser.Parse(Bry_.new_u8(src));
Tfds.Eq_bool(expd.Unique(), actl.Unique());
Tfds.Eq_str(expd.Name(), actl.Name());
Tfds.Eq_str(expd.Tbl(), actl.Tbl());
Tfds.Eq_bool(Bool_.Y, Dbmeta_idx_fld.Ary_eq(expd.Flds, actl.Flds));
}
}

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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import gplx.core.brys.*; import gplx.core.btries.*;
public class Dbmeta_parser__tbl {
private final Sql_bry_rdr rdr = new Sql_bry_rdr();
private final Dbmeta_parser__fld fld_parser = new Dbmeta_parser__fld();
public Dbmeta_tbl_itm Parse(byte[] src) {
rdr.Init_by_page(Bry_.Empty, src, src.length);
rdr.Skip_ws().Chk_trie_val(trie, Tid__create);
rdr.Skip_ws().Chk_trie_val(trie, Tid__table);
byte[] tbl_name = rdr.Read_sql_identifier();
rdr.Skip_ws().Chk(Byte_ascii.Paren_bgn);
Dbmeta_tbl_itm rv = Dbmeta_tbl_itm.New(String_.new_u8(tbl_name));
boolean loop = true;
while (loop) {
Dbmeta_fld_itm fld = fld_parser.Parse_fld(rdr); if (fld == null) rdr.Err_wkr().Fail("unknown field", "src", src);
rv.Flds().Add(fld);
int pos = rdr.Pos();
byte b = pos == rdr.Src_end() ? Byte_ascii.Null : src[pos];
switch (b) {
case Byte_ascii.Comma: rdr.Move_by_one(); break;
case Byte_ascii.Paren_end: rdr.Move_by_one(); loop = false; break;
default: rdr.Err_wkr().Fail("premature end of flds"); break;
}
}
return rv;
}
private static final byte Tid__create = 0, Tid__table = 1;
private static final byte[]
Bry__create = Bry_.new_a7("create")
, Bry__table = Bry_.new_a7("table");
private static final Btrie_slim_mgr trie = Btrie_slim_mgr.ci_a7()
.Add_bry_byte(Bry__create , Tid__create)
.Add_bry_byte(Bry__table , Tid__table);
}

View 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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import org.junit.*; import gplx.dbs.engines.sqlite.*;
public class Dbmeta_parser__tbl_tst {
@Before public void init() {fxt.Clear();} private Dbmeta_parser__tbl_fxt fxt = new Dbmeta_parser__tbl_fxt();
@Test public void Test_parse() {
fxt.Test_parse("CREATE TABLE tbl_1 (fld_1 int, fld_2 int)", fxt.Make_tbl("tbl_1", "fld_1", "fld_2"));
}
@Test public void Test_smoke() {
fxt.Test_parse(String_.Concat_lines_nl_skip_last
( "CREATE TABLE page"
, "( page_id integer NOT NULL PRIMARY KEY"
, ", page_namespace integer NOT NULL"
, ", page_title varchar(255) NOT NULL"
, ", page_is_redirect integer NOT NULL"
, ", page_touched varchar(14) NOT NULL"
, ", page_len integer NOT NULL"
, ", page_random_int integer NOT NULL"
, ", page_text_db_id integer NOT NULL"
, ", page_html_db_id integer NOT NULL DEFAULT -1"
, ", page_redirect_id integer NOT NULL DEFAULT -1"
, ");"
), fxt.Make_tbl("page", "page_id", "page_namespace", "page_title", "page_is_redirect", "page_touched", "page_len", "page_random_int", "page_text_db_id", "page_html_db_id", "page_redirect_id"));
}
}
class Dbmeta_parser__tbl_fxt {
private final Dbmeta_parser__tbl tbl_parser = new Dbmeta_parser__tbl();
public void Clear() {}
public Dbmeta_tbl_itm Make_tbl(String tbl_name, String... fld_names) {
int len = fld_names.length;
Dbmeta_fld_itm[] flds = new Dbmeta_fld_itm[len];
for (int i = 0; i < len; ++i)
flds[i] = new Dbmeta_fld_itm(fld_names[i], new Dbmeta_fld_tid(Dbmeta_fld_tid.Tid__int, Sqlite_tid.Tid_int, Bry_.new_a7("int"), Int_.Min_value, Int_.Min_value));
return Dbmeta_tbl_itm.New(tbl_name, flds);
}
public void Test_parse(String src, Dbmeta_tbl_itm expd_tbl) {
Dbmeta_tbl_itm actl_tbl = tbl_parser.Parse(Bry_.new_u8(src));
Tfds.Eq(expd_tbl.Name(), actl_tbl.Name());
Tfds.Eq_ary_str(To_str_ary(expd_tbl.Flds()), To_str_ary(actl_tbl.Flds()));
}
private static String[] To_str_ary(Dbmeta_fld_mgr fld_mgr) {
int len = fld_mgr.Len();
String[] rv = new String[len];
for (int i = 0; i < len; ++i) {
rv[i] = fld_mgr.Get_at(i).Name();
}
return rv;
}
}

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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import gplx.core.brys.*;
public class Sql_bry_rdr extends Bry_rdr { public byte[] Read_sql_identifier() {
this.Skip_ws();
int bgn = pos, end = -1;
if (pos == src_end) return null;
if (src[pos] == Byte_ascii.Brack_bgn) { // EX: [name with space]
bgn = ++pos; // set bgn after [
end = this.Find_fwd_lr(Byte_ascii.Brack_end);
}
else {
this.Skip_alpha_num_under(); // ASSUME: identifier is ASCII and alpha / num / underscore
if (pos == bgn) return null; // String is not identifier; EX: "!@#"
end = pos;
}
return Bry_.Mid(src, bgn, end);
}
@Override public Bry_rdr Skip_ws() {
byte b_0 = pos < src_end ? src[pos] : Byte_ascii.Null;
byte bgn_1 = Byte_ascii.Null;
byte[] end_bry = null;
switch (b_0) {
case Byte_ascii.Dash: bgn_1 = Byte_ascii.Dash; end_bry = Comm_end_line; break;
case Byte_ascii.Slash: bgn_1 = Byte_ascii.Star; end_bry = Comm_end_multi; break;
case Byte_ascii.Tab: case Byte_ascii.Nl: case Byte_ascii.Cr: case Byte_ascii.Space:
++pos;
return super.Skip_ws();
default:
return this;
}
byte b_1 = pos + 1 < src_end ? src[pos + 1] : Byte_ascii.Null;
if (b_1 != bgn_1) return this;
int end_pos = Bry_find_.Find_fwd(src, end_bry, pos + 2, src_end);
if (end_pos == Bry_find_.Not_found) return this;
pos = end_pos + end_bry.length;
return super.Skip_ws();
}
private static final byte[] Comm_end_line = Byte_ascii.Nl_bry, Comm_end_multi = Bry_.new_a7("*/");
}

View 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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import org.junit.*;
public class Sql_bry_rdr_tst {
@Before public void init() {fxt.Clear();} private Sql_bry_rdr_fxt fxt = new Sql_bry_rdr_fxt();
@Test public void Skip_ws() {
fxt.Test_skip_ws("a", 0); // char
fxt.Test_skip_ws("\ta", 1); // tab
fxt.Test_skip_ws("\na", 1); // \n
fxt.Test_skip_ws("\ra", 1); // \r
fxt.Test_skip_ws(" a", 1); // space
fxt.Test_skip_ws("\t\n\r a", 4); // multi
fxt.Test_skip_ws("", 0); // eos
}
@Test public void Read_sql_identifier() {
fxt.Test_read_sql_identifier("a", "a"); // one
fxt.Test_read_sql_identifier("abc_1", "abc_1"); // many
fxt.Test_read_sql_identifier("[abc_1]", "abc_1"); // bracket
fxt.Test_read_sql_identifier(" a ", "a"); // ws
fxt.Test_read_sql_identifier("", null); // eos
fxt.Test_read_sql_identifier("!@#", null); // sym
}
}
class Sql_bry_rdr_fxt {
private final Sql_bry_rdr rdr = new Sql_bry_rdr();
public void Clear() {}
public void Test_skip_ws(String src, int expd_pos) {
rdr.Init_by_src(Bry_.new_u8(src));
rdr.Skip_ws();
Tfds.Eq(expd_pos, rdr.Pos());
}
public void Test_read_sql_identifier(String src, String expd) {
rdr.Init_by_src(Bry_.new_u8(src));
Tfds.Eq(expd, String_.new_u8(rdr.Read_sql_identifier()));
}
}