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,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.metas; import gplx.*; import gplx.dbs.*;
public class Meta_fld_itm {
public Meta_fld_itm(String name, Meta_type_itm type) {
this.name = name; this.type = type;
}
public String Name() {return name;} private final String name;
public Meta_type_itm Type() {return type;} private final Meta_type_itm type;
public int Nullable_tid() {return nullable_tid;} public void Nullable_tid_(int v) {nullable_tid = v;} private int nullable_tid;
public boolean Autonumber() {return autonumber;} public void Autonumber_y_() {autonumber = true;} private boolean autonumber;
public boolean Primary_key() {return primary_key;} public void Primary_key_y_() {primary_key = true;} private boolean primary_key;
public Object Default_val() {return default_val;} private Object default_val;
public void Default_val_(Object v) {this.default_val = v;}
public static final int Nullable_unknown = 0, Nullable_null = 1, Nullable_not_null = 2;
}

View File

@@ -0,0 +1,26 @@
/*
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 Meta_fld_mgr {
private final Ordered_hash hash = Ordered_hash_.new_();
public int Len() {return hash.Count();}
public void Add(Meta_fld_itm itm) {hash.Add(itm.Name(), itm);}
public boolean Has(String name) {return hash.Has(name);}
public Meta_fld_itm Get_by(String name) {return (Meta_fld_itm)hash.Get_by(name);}
public Meta_fld_itm Get_at(int i) {return (Meta_fld_itm)hash.Get_at(i);}
}

View File

@@ -0,0 +1,23 @@
/*
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 Meta_idx_itm {
public Meta_idx_itm(String name, String sql) {this.name = name; this.sql = sql;}
public String Name() {return name;} private final String name;
public String Sql() {return sql;} private final String sql;
}

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 Meta_idx_mgr {
private final Ordered_hash hash = Ordered_hash_.new_();
public void Add(Meta_idx_itm itm) {hash.Add(itm.Name(), itm);}
public boolean Has(String name) {return hash.Has(name);}
public Meta_idx_itm Get(String name) {return (Meta_idx_itm)hash.Get_by(name);}
}

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 Meta_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,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 Meta_tbl_itm {
public Meta_tbl_itm(String name, String sql) {this.name = name; this.sql = sql;}
public Meta_fld_mgr Flds() {return flds;} private final Meta_fld_mgr flds = new Meta_fld_mgr();
public String Name() {return name;} private final String name;
public String Sql() {return sql;} private final String sql;
}

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 Meta_tbl_mgr {
private final Ordered_hash hash = Ordered_hash_.new_();
public void Add(Meta_tbl_itm itm) {hash.Add(itm.Name(), itm);}
public boolean Has(String name) {return hash.Has(name);}
public Meta_tbl_itm Get_by(String name) {return (Meta_tbl_itm)hash.Get_by(name);}
}

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 Meta_type_itm {
public Meta_type_itm(int tid_ansi, int tid_sqlite, byte[] name, int len_1, int len_2) {
this.tid_ansi = tid_ansi; this.tid_sqlite = tid_sqlite; this.name = name; this.len_1 = len_1; this.len_2 = len_2;
}
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[] Name() {return name;} private final byte[] name;
public int Len_1() {return len_1;} private final int len_1;
public int Len_2() {return len_2;} private final int len_2;
}

View File

@@ -0,0 +1,117 @@
/*
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 Meta_fld_wkr__base {
private byte[] hook;
private byte[][] words_ary; 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_ary = words_ary;
this.words_len = words_ary.length;
}
public void Reg(Btrie_slim_mgr trie) {
trie.Add_obj(hook, this);
}
@gplx.Virtual public void Match(Bry_rdr rdr, Meta_fld_itm fld) {
for (int i = 0; i < words_len; ++i) {
rdr.Skip_ws();
byte[] word = words_ary[i];
rdr.Chk_bry_or_fail(word);
}
When_match(fld);
}
protected abstract void When_match(Meta_fld_itm fld);
public static final int Tid_end_comma = 1, Tid_end_paren = 2, Tid_other = 3;
}
class Meta_fld_wkr__end_comma extends Meta_fld_wkr__base {
public Meta_fld_wkr__end_comma() {this.Ctor(Hook);}
@Override public int Tid() {return Tid_end_comma;}
@Override protected void When_match(Meta_fld_itm fld) {}
private static final byte[] Hook = Bry_.new_a7(",");
public static final Meta_fld_wkr__end_comma I = new Meta_fld_wkr__end_comma();
}
class Meta_fld_wkr__end_paren extends Meta_fld_wkr__base {
public Meta_fld_wkr__end_paren() {this.Ctor(Hook);}
@Override public int Tid() {return Tid_end_paren;}
@Override protected void When_match(Meta_fld_itm fld) {}
private static final byte[] Hook = Bry_.new_a7(")");
public static final Meta_fld_wkr__end_paren I = new Meta_fld_wkr__end_paren();
}
class Meta_fld_wkr__nullable_null extends Meta_fld_wkr__base {
public Meta_fld_wkr__nullable_null() {this.Ctor(Hook);}
@Override protected void When_match(Meta_fld_itm fld) {
fld.Nullable_tid_(Meta_fld_itm.Nullable_null);
}
private static final byte[] Hook = Bry_.new_a7("null");
public static final Meta_fld_wkr__nullable_null I = new Meta_fld_wkr__nullable_null();
}
class Meta_fld_wkr__nullable_not extends Meta_fld_wkr__base {
public Meta_fld_wkr__nullable_not() {this.Ctor(Hook, Bry_null);}
@Override protected void When_match(Meta_fld_itm fld) {
fld.Nullable_tid_(Meta_fld_itm.Nullable_not_null);
}
private static final byte[] Hook = Bry_.new_a7("not"), Bry_null = Bry_.new_a7("null");
public static final Meta_fld_wkr__nullable_not I = new Meta_fld_wkr__nullable_not();
}
class Meta_fld_wkr__primary_key extends Meta_fld_wkr__base {
public Meta_fld_wkr__primary_key() {this.Ctor(Hook, Bry_key);}
@Override protected void When_match(Meta_fld_itm fld) {
fld.Primary_key_y_();
}
private static final byte[] Hook = Bry_.new_a7("primary"), Bry_key = Bry_.new_a7("key");
public static final Meta_fld_wkr__primary_key I = new Meta_fld_wkr__primary_key();
}
class Meta_fld_wkr__autonumber extends Meta_fld_wkr__base {
public Meta_fld_wkr__autonumber() {this.Ctor(Hook);}
@Override protected void When_match(Meta_fld_itm fld) {
fld.Autonumber_y_();
}
private static final byte[] Hook = Bry_.new_a7("autoincrement");
public static final Meta_fld_wkr__autonumber I = new Meta_fld_wkr__autonumber();
}
class Meta_fld_wkr__default extends Meta_fld_wkr__base {
public Meta_fld_wkr__default() {this.Ctor(Hook);}
@Override public void Match(Bry_rdr rdr, Meta_fld_itm fld) {
Object default_val = null;
rdr.Skip_ws();
byte[] src = rdr.Src();
byte b = src[rdr.Pos()];
switch (b) {
case Byte_ascii.Quote:
case Byte_ascii.Apos:
int bgn_pos = rdr.Pos() + 1;
int end_pos = Bry_finder.Find_fwd(src, b, bgn_pos); if (end_pos == Bry_finder.Not_found) throw Exc_.new_("unclosed quote", "snip", rdr.Mid_by_len_safe(40));
default_val = Bry_.Mid(src, bgn_pos, end_pos);
rdr.Pos_(end_pos + 1);
break;
case Byte_ascii.Dash:
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
default_val = rdr.Read_int_to_non_num();
break;
default:
throw Exc_.new_("invalid field_default", "snip", rdr.Mid_by_len_safe(40));
}
fld.Default_val_(default_val);
}
@Override protected void When_match(Meta_fld_itm fld) {}
private static final byte[] Hook = Bry_.new_a7("default");
public static final Meta_fld_wkr__default I = new Meta_fld_wkr__default();
}

View File

@@ -0,0 +1,118 @@
/*
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 Meta_parser__fld {
public Meta_type_itm Parse_type(Bry_rdr rdr) {
rdr.Skip_ws();
Object type_obj = type_trie.Match_bgn(rdr.Src(), rdr.Pos(), rdr.Src_len());
if (type_obj == null) throw Exc_.new_("invalid fld type", "snip", rdr.Mid_by_len_safe(40));
Meta_parser__fld_itm type_itm = (Meta_parser__fld_itm)type_obj;
rdr.Pos_add(type_itm.Word().length);
int paren_itms_count = type_itm.Paren_itms_count();
int len_1 = Int_.MinValue, len_2 = Int_.MinValue;
if (paren_itms_count > 0) {
rdr.Skip_ws().Chk_byte_or_fail(Byte_ascii.Paren_bgn);
len_1 = rdr.Skip_ws().Read_int_to_non_num(); if (len_1 == Int_.MinValue) throw Exc_.new_("invalid fld len_1", "snip", rdr.Mid_by_len_safe(40));
if (paren_itms_count == 2) {
rdr.Skip_ws().Chk_byte_or_fail(Byte_ascii.Comma);
len_2 = rdr.Skip_ws().Read_int_to_non_num(); if (len_2 == Int_.MinValue) throw Exc_.new_("invalid fld len_2", "snip", rdr.Mid_by_len_safe(40));
}
rdr.Skip_ws().Chk_byte_or_fail(Byte_ascii.Paren_end);
}
return new Meta_type_itm(type_itm.Tid_ansi(), type_itm.Tid_sqlite(), type_itm.Word(), len_1, len_2);
}
public Meta_fld_itm Parse_fld(Sql_bry_rdr rdr) { // starts after "(" or ","; EX: "(fld1 int", ", fld2 int"; ends at ")"
byte[] name = rdr.Read_sql_identifier();
Meta_type_itm type = this.Parse_type(rdr);
Meta_fld_itm fld = new Meta_fld_itm(String_.new_u8(name), type);
byte[] src = rdr.Src(); int src_len = rdr.Src_len();
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;
}
Object type_obj = fld_trie.Match_bgn(src, rdr.Pos(), src_len); if (type_obj == null) throw Exc_.new_("invalid", "snip", rdr.Mid_by_len_safe(40));
Meta_fld_wkr__base type_wkr = (Meta_fld_wkr__base)type_obj;
switch (type_wkr.Tid()) {
case Meta_fld_wkr__base.Tid_end_comma:
case Meta_fld_wkr__base.Tid_end_paren: return fld;
default:
rdr.Pos_(fld_trie.Match_pos());
type_wkr.Match(rdr, fld);
break;
}
}
// return fld; // NOTE: will happen for tests; EX: "fld_1 int" vs "fld_1 int,"
}
private static final Btrie_slim_mgr fld_trie = fld_trie_init
( Meta_fld_wkr__nullable_null.I
, Meta_fld_wkr__nullable_not.I
, Meta_fld_wkr__autonumber.I
, Meta_fld_wkr__primary_key.I
, Meta_fld_wkr__default.I
);
private static Btrie_slim_mgr fld_trie_init(Meta_fld_wkr__base... wkrs) {
Btrie_slim_mgr rv = Btrie_slim_mgr.ci_ascii_();
for (Meta_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_ascii_();
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_byte , Sqlite_tid.Tid_int , 0, "tinyint", "int2");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_short , Sqlite_tid.Tid_int , 0, "smallint");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_int , Sqlite_tid.Tid_int , 0, "int", "integer", "mediumint");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_long , Sqlite_tid.Tid_int , 0, "bigint", "int8"); // "UNSIGNED BIG INT"
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_str , Sqlite_tid.Tid_text , 1, "character", "varchar", "nchar"); // "varying character", "native character"
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_text , Sqlite_tid.Tid_text , 0, "text", "clob");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_bry , Sqlite_tid.Tid_none , 0, "blob");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_float , Sqlite_tid.Tid_real , 0, "float");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_double , Sqlite_tid.Tid_real , 0, "real", "double"); // "double precision"
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_decimal , Sqlite_tid.Tid_numeric , 0, "numeric");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_decimal , Sqlite_tid.Tid_numeric , 2, "decimal");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_decimal , Sqlite_tid.Tid_numeric , 2, "decimal");
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_bool , Sqlite_tid.Tid_numeric , 0, "boolean", "bit"); // "bit" is not SQLITE
Meta_parser__fld_itm.reg_many(rv, Db_meta_fld.Tid_date , Sqlite_tid.Tid_numeric , 0, "date", "datetime");
return rv;
}
}
class Meta_parser__fld_itm {
public Meta_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]);
Meta_parser__fld_itm itm = new Meta_parser__fld_itm(tid_ansi, tid_sqlite, name_bry, paren_itms_count);
trie.Add_obj(name_bry, itm);
}
}
}
class Sqlite_tid {
public static final int Tid_int = 1, Tid_text = 2, Tid_none = 3, Tid_real = 4, Tid_numeric = 5;
}

View File

@@ -0,0 +1,71 @@
/*
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 Meta_parser__fld_tst {
@Before public void init() {fxt.Clear();} private Meta_parser__fld_fxt fxt = new Meta_parser__fld_fxt();
@Test public void Parse_type() {
fxt.Test_parse_type("int" , fxt.Make_type(Db_meta_fld.Tid_int));
fxt.Test_parse_type("varchar(255)" , fxt.Make_type(Db_meta_fld.Tid_str, 255));
fxt.Test_parse_type("decimal(12,10)" , fxt.Make_type(Db_meta_fld.Tid_decimal, 12, 10));
fxt.Test_parse_type(" int" , fxt.Make_type(Db_meta_fld.Tid_int));
fxt.Test_parse_type(" decimal ( 12 , 10 )" , fxt.Make_type(Db_meta_fld.Tid_decimal, 12, 10));
}
@Test public void Parse_fld() {
fxt.Test_parse_fld("name_1 int" , fxt.Make_fld("name_1", Db_meta_fld.Tid_int, Meta_fld_itm.Nullable_unknown));
fxt.Test_parse_fld("name_1 int null" , fxt.Make_fld("name_1", Db_meta_fld.Tid_int, Meta_fld_itm.Nullable_null));
fxt.Test_parse_fld("name_1 int not null" , fxt.Make_fld("name_1", Db_meta_fld.Tid_int, Meta_fld_itm.Nullable_not_null));
fxt.Test_parse_fld("name_1 int not null autoincrement" , fxt.Make_fld("name_1", Db_meta_fld.Tid_int, Meta_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", Db_meta_fld.Tid_int, Meta_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", Db_meta_fld.Tid_int, Meta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N, -1));
fxt.Test_parse_fld("name_1 int not null default 'abc'" , fxt.Make_fld("name_1", Db_meta_fld.Tid_int, Meta_fld_itm.Nullable_not_null, Bool_.Y, Bool_.N, "abc"));
}
}
class Meta_parser__fld_fxt {
private final Meta_parser__fld fld_parser = new Meta_parser__fld();
private final Sql_bry_rdr rdr = new Sql_bry_rdr();
public void Clear() {}
public Meta_type_itm Make_type(int tid_ansi) {return new Meta_type_itm(tid_ansi, -1, null, Int_.MinValue, Int_.MinValue);}
public Meta_type_itm Make_type(int tid_ansi, int len_1) {return new Meta_type_itm(tid_ansi, -1, null, len_1, Int_.MinValue);}
public Meta_type_itm Make_type(int tid_ansi, int len_1, int len_2) {return new Meta_type_itm(tid_ansi, -1, null, len_1, len_2);}
public Meta_fld_itm Make_fld(String name, int tid_ansi, int nullable) {return Make_fld(name, tid_ansi, nullable, false, false, null);}
public Meta_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 Meta_fld_itm Make_fld(String name, int tid_ansi, int nullable, boolean autonumber, boolean primary_key, Object default_val) {
Meta_fld_itm rv = new Meta_fld_itm(name, Make_type(tid_ansi));
rv.Nullable_tid_(nullable);
if (autonumber) rv.Autonumber_y_();
if (primary_key) rv.Primary_key_y_();
rv.Default_val_(default_val);
return rv;
}
public void Test_parse_type(String src, Meta_type_itm expd_type) {
rdr.Init(Bry_.new_u8(src));
Meta_type_itm 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, Meta_fld_itm expd_fld) {
rdr.Init(Bry_.new_u8(src));
Meta_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_val()), Object_.Xto_str_strict_or_empty(actl_fld.Default_val()));
}
}

View File

@@ -0,0 +1,57 @@
/*
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 Meta_parser__tbl {
private final Sql_bry_rdr rdr = new Sql_bry_rdr();
private final Meta_parser__fld fld_parser = new Meta_parser__fld();
private Meta_tbl_itm tbl;
public Meta_tbl_itm Parse(byte[] src) {
src = Bry_.Lower_ascii(src);
rdr.Init(src);
tbl = null;
Parse_hdr();
Parse_flds();
return tbl;
}
private void Parse_hdr() {
rdr.Skip_ws().Chk_bry_or_fail(Tkn_create);
rdr.Skip_ws().Chk_bry_or_fail(Tkn_table);
byte[] tbl_name = rdr.Read_sql_identifier();
this.tbl = new Meta_tbl_itm(String_.new_u8(tbl_name), String_.new_u8(rdr.Src()));
rdr.Skip_ws().Chk_byte_or_fail(Byte_ascii.Paren_bgn);
}
private void Parse_flds() {
byte[] src = rdr.Src();
while (true) {
Meta_fld_itm fld = fld_parser.Parse_fld(rdr); if (fld == null) throw Exc_.new_("unknown field", "src", rdr.Src());
tbl.Flds().Add(fld);
int pos = rdr.Pos();
byte b = pos == rdr.Src_len() ? Byte_ascii.Nil : src[pos];
switch (b) {
case Byte_ascii.Comma: rdr.Pos_add_one(); break;
case Byte_ascii.Paren_end: rdr.Pos_add_one(); return;
default: throw Exc_.new_("premature end of flds", "src", rdr.Mid_by_len_safe(40));
}
}
}
private static final byte[]
Tkn_create = Bry_.new_a7("create")
, Tkn_table = Bry_.new_a7("table")
;
}

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.metas.parsers; import gplx.*; import gplx.dbs.*; import gplx.dbs.metas.*;
import org.junit.*;
public class Meta_parser__tbl_tst {
@Before public void init() {fxt.Clear();} private Meta_parser__tbl_fxt fxt = new Meta_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 Meta_parser__tbl_fxt {
private final Meta_parser__tbl tbl_parser = new Meta_parser__tbl();
public void Clear() {}
public Meta_tbl_itm Make_tbl(String tbl_name, String... fld_names) {
Meta_tbl_itm rv = new Meta_tbl_itm(tbl_name, "NONE");
int len = fld_names.length;
for (int i = 0; i < len; ++i) {
rv.Flds().Add(new Meta_fld_itm(fld_names[i], new Meta_type_itm(Db_meta_fld.Tid_int, Sqlite_tid.Tid_int, Bry_.new_a7("int"), Int_.MinValue, Int_.MinValue)));
}
return rv;
}
public void Test_parse(String src, Meta_tbl_itm expd_tbl) {
Meta_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(Meta_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,57 @@
/*
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_len) return null;
if (src[pos] == Byte_ascii.Brack_bgn) { // EX: [name with space]
bgn = ++pos; // set bgn after [
end = this.Find_fwd(Byte_ascii.Brack_end);
pos = end + 1; // set pos after ]
}
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_len ? src[pos] : Byte_ascii.Nil;
byte bgn_1 = Byte_ascii.Nil;
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_len ? src[pos + 1] : Byte_ascii.Nil;
if (b_1 != bgn_1) return this;
int end_pos = Bry_finder.Find_fwd(src, end_bry, pos + 2, src_len);
if (end_pos == Bry_finder.Not_found) return this;
pos = end_pos + end_bry.length;
return this.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(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(Bry_.new_u8(src));
Tfds.Eq(expd, String_.new_u8(rdr.Read_sql_identifier()));
}
}