mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.6.4.1
This commit is contained in:
@@ -44,7 +44,7 @@ public class Db_conn {
|
||||
public void Ddl_create_idx(Gfo_usr_dlg usr_dlg, Db_meta_idx... idxs) {engine.Ddl_create_idx(usr_dlg, idxs);}
|
||||
public void Ddl_append_fld(String tbl, Db_meta_fld fld) {engine.Ddl_append_fld(tbl, fld);}
|
||||
public void Ddl_delete_tbl(String tbl) {engine.Ddl_delete_tbl(tbl);}
|
||||
public boolean Schema_tbl_exists(String tbl) {return engine.Schema_tbl_exists(tbl);}
|
||||
public boolean Meta_tbl_exists(String tbl) {return engine.Meta_tbl_exists(tbl);}
|
||||
public void Rls_reg(RlsAble rls) {rls_list.Add(rls);}
|
||||
public void Rls_conn() {
|
||||
int len = rls_list.Count();
|
||||
|
||||
@@ -19,7 +19,7 @@ package gplx.dbs; import gplx.*;
|
||||
import gplx.dbs.engines.nulls.*; import gplx.dbs.engines.mems.*; import gplx.dbs.engines.sqlite.*; import gplx.dbs.engines.tdbs.*;
|
||||
import gplx.dbs.engines.mysql.*; import gplx.dbs.engines.postgres.*;
|
||||
public class Db_conn_info_ {
|
||||
public static final Db_conn_info Null = Null_conn_info._;
|
||||
public static final Db_conn_info Null = Noop_conn_info.I;
|
||||
public static final Db_conn_info Test = Mysql_conn_info.new_("127.0.0.1", "unit_tests", "root", "mysql7760");
|
||||
public static Db_conn_info parse_(String raw) {return Db_conn_info_pool._.Parse(raw);}
|
||||
public static Db_conn_info sqlite_(Io_url url) {return Sqlite_conn_info.load_(url);}
|
||||
@@ -30,7 +30,7 @@ public class Db_conn_info_ {
|
||||
class Db_conn_info_pool {
|
||||
private Ordered_hash regy = Ordered_hash_.new_();
|
||||
public Db_conn_info_pool() {
|
||||
this.Add(Null_conn_info._).Add(Tdb_conn_info._).Add(Mysql_conn_info._).Add(Postgres_conn_info._).Add(Sqlite_conn_info._);
|
||||
this.Add(Noop_conn_info.I).Add(Tdb_conn_info._).Add(Mysql_conn_info._).Add(Postgres_conn_info._).Add(Sqlite_conn_info._);
|
||||
this.Add(Db_conn_info__mem.I);
|
||||
}
|
||||
public Db_conn_info_pool Add(Db_conn_info itm) {regy.Add_if_dupe_use_nth(itm.Tid(), itm); return this;}
|
||||
|
||||
@@ -41,6 +41,6 @@ public class Db_conn_pool {
|
||||
}
|
||||
public static final Db_conn_pool I = new Db_conn_pool(); Db_conn_pool() {this.Init();}
|
||||
private void Init() {
|
||||
this.Engines__add(Null_engine._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
this.Engines__add(Noop_engine._, TdbEngine._, Mysql_engine._, Postgres_engine._, Sqlite_engine._, Db_engine__mem._);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public class Db_meta_fld {
|
||||
public boolean Primary() {return primary;} private final boolean primary;
|
||||
public boolean Autonum() {return autonum;} private final boolean autonum;
|
||||
public Object Default_value() {return default_value;} private final Object default_value;
|
||||
public static final int Tid_bool = 0, Tid_byte = 1, Tid_short = 2, Tid_int = 3, Tid_long = 4, Tid_float = 5, Tid_double = 6, Tid_str = 7, Tid_text = 8, Tid_bry = 9;
|
||||
public static final int Tid_bool = 0, Tid_byte = 1, Tid_short = 2, Tid_int = 3, Tid_long = 4, Tid_float = 5, Tid_double = 6, Tid_str = 7, Tid_text = 8, Tid_bry = 9, Tid_decimal = 10, Tid_date = 11;
|
||||
public static final String Key_null = null;
|
||||
public static final int Len_null = -1;
|
||||
public static final String[] Ary_empy = String_.Ary_empty;
|
||||
|
||||
@@ -24,6 +24,17 @@ public class Db_meta_tbl {
|
||||
}
|
||||
public String Name() {return name;} private final String name;
|
||||
public Db_meta_fld[] Flds() {return flds;} private final Db_meta_fld[] flds;
|
||||
public boolean Flds_has(String fld) {
|
||||
if (flds_hash == null) {
|
||||
flds_hash = Ordered_hash_.new_();
|
||||
int len = flds.length;
|
||||
for (int i = 0; i < len; ++i) {
|
||||
Db_meta_fld fld_itm = flds[i];
|
||||
flds_hash.Add(fld_itm.Name(), fld_itm);
|
||||
}
|
||||
}
|
||||
return flds_hash.Has(fld);
|
||||
} private Ordered_hash flds_hash;
|
||||
public Db_meta_idx[] Idxs() {return idxs;} private final Db_meta_idx[] idxs;
|
||||
public String To_sql_create() {return Db_sqlbldr__sqlite.I.Bld_create_tbl(this);}
|
||||
public static Db_meta_tbl new_(String name, Db_meta_fld_list flds, Db_meta_idx... idxs) {return new Db_meta_tbl(name, flds.To_fld_ary(), idxs);}
|
||||
|
||||
@@ -18,25 +18,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.dbs; import gplx.*;
|
||||
public interface Db_rdr {
|
||||
boolean Move_next();
|
||||
byte[] Read_bry(int i);
|
||||
byte[] Read_bry(String k);
|
||||
byte[] Read_bry_by_str(int i);
|
||||
byte[] Read_bry_in_parts(String tbl, String fld, String crt_key, Object crt_val);
|
||||
byte[] Read_bry_by_str(String k);
|
||||
String Read_str(int i);
|
||||
String Read_str(String k);
|
||||
byte Read_byte(int i);
|
||||
byte Read_byte(String k);
|
||||
int Read_int(int i);
|
||||
int Read_int(String k);
|
||||
long Read_long(int i);
|
||||
long Read_long(String k);
|
||||
float Read_float(int i);
|
||||
float Read_float(String k);
|
||||
double Read_double(int i);
|
||||
double Read_double(String k);
|
||||
DateAdp Read_date_by_str(int i);
|
||||
DateAdp Read_date_by_str(String k);
|
||||
boolean Read_bool_by_byte(int i);
|
||||
boolean Read_bool_by_byte(String k);
|
||||
void Rls();
|
||||
}
|
||||
|
||||
@@ -21,25 +21,16 @@ public class Db_rdr_ {
|
||||
}
|
||||
class Db_rdr__empty implements Db_rdr {
|
||||
public boolean Move_next() {return false;}
|
||||
public byte[] Read_bry(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry(String k) {return Bry_.Empty;}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.Empty;}
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.Empty;}
|
||||
public byte Read_byte(int i) {return Byte_.Max_value_127;}
|
||||
public byte[] Read_bry_in_parts(String tbl, String fld, String crt_key, Object crt_val) {return Bry_.Empty;}
|
||||
public byte Read_byte(String k) {return Byte_.Max_value_127;}
|
||||
public String Read_str(int i) {return String_.Empty;}
|
||||
public String Read_str(String k) {return String_.Empty;}
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.MinValue;}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.MinValue;}
|
||||
public int Read_int(int i) {return Int_.MinValue;}
|
||||
public int Read_int(String k) {return Int_.MinValue;}
|
||||
public long Read_long(int i) {return Long_.MinValue;}
|
||||
public long Read_long(String k) {return Long_.MinValue;}
|
||||
public float Read_float(int i) {return Float_.NaN;}
|
||||
public float Read_float(String k) {return Float_.NaN;}
|
||||
public double Read_double(int i) {return Double_.NaN;}
|
||||
public double Read_double(String k) {return Double_.NaN;}
|
||||
public boolean Read_bool_by_byte(int i) {return false;}
|
||||
public boolean Read_bool_by_byte(String k) {return false;}
|
||||
public void Rls() {}
|
||||
}
|
||||
|
||||
@@ -26,25 +26,16 @@ public class Db_rdr__basic implements Db_rdr {
|
||||
try {return rdr.next();}
|
||||
catch (Exception e) {throw Err_.new_fmt_("move_next failed; check column casting error in SQL: err={0} sql={1}", Err_.Message_lang(e), sql);}
|
||||
}
|
||||
@gplx.Virtual public byte[] Read_bry(int i) {try {return rdr.getBytes(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte[] Read_bry(String k) {try {return (byte[])rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bry_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte[] Read_bry_by_str(int i) {try {return Bry_.new_u8(rdr.getString(i + 1));} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte[] Read_bry_by_str(String k) {try {return Bry_.new_u8((String)rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public String Read_str(int i) {try {return rdr.getString(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte[] Read_bry_in_parts(String tbl, String fld, String crt_key, Object crt_val) {throw Err_.not_implemented_();}
|
||||
@gplx.Virtual public String Read_str(String k) {try {return (String)rdr.getObject(k);} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, String_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561(Read_str(i));}
|
||||
@gplx.Virtual public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561(Read_str(k));}
|
||||
@gplx.Virtual public int Read_int(int i) {try {return rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public int Read_int(String k) {try {return Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Int_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public long Read_long(int i) {try {return rdr.getLong(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public long Read_long(String k) {try {return Long_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Long_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public float Read_float(int i) {try {return rdr.getFloat(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public float Read_float(String k) {try {return Float_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Float_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public double Read_double(int i) {try {return rdr.getDouble(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public double Read_double(String k) {try {return Double_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Double_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(int i) {try {return rdr.getByte(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public byte Read_byte(String k) {try {return Byte_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public boolean Read_bool_by_byte(int i) {try {return rdr.getByte(i + 1) == 1;} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public boolean Read_bool_by_byte(String k) {try {return Byte_.cast_(rdr.getObject(k)) == 1;} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Bool_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@gplx.Virtual public void Rls() {
|
||||
try {rdr.close();}
|
||||
|
||||
@@ -38,5 +38,6 @@ public interface Db_engine {
|
||||
void Ddl_delete_tbl(String tbl);
|
||||
void Env_db_attach(String alias, Io_url db_url);
|
||||
void Env_db_detach(String alias);
|
||||
boolean Schema_tbl_exists(String name);
|
||||
boolean Meta_tbl_exists(String tbl);
|
||||
boolean Meta_fld_exists(String tbl, String fld);
|
||||
}
|
||||
|
||||
@@ -74,7 +74,8 @@ public abstract class Db_engine_sql_base implements Db_engine {
|
||||
public void Ddl_delete_tbl(String tbl) {Exec_as_int(Db_sqlbldr__sqlite.I.Bld_drop_tbl(tbl));}
|
||||
@gplx.Virtual public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
@gplx.Virtual public void Env_db_detach(String alias) {}
|
||||
@gplx.Virtual public boolean Schema_tbl_exists(String name) {return false;}
|
||||
@gplx.Virtual public boolean Meta_tbl_exists(String tbl) {return false;}
|
||||
@gplx.Virtual public boolean Meta_fld_exists(String tbl, String fld) {return false;}
|
||||
@gplx.Virtual public DataRdr New_rdr(ResultSet rdr, String sql) {return gplx.stores.Db_data_rdr_.new_(rdr, sql);}
|
||||
@gplx.Virtual public Sql_qry_wtr SqlWtr() {return Sql_qry_wtr_.new_ansi();}
|
||||
private Db_rdr New_rdr(Db_stmt stmt, Object rdr, String sql) {
|
||||
|
||||
@@ -46,6 +46,11 @@ public class Db_engine__mem implements Db_engine {
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
public boolean Schema_tbl_exists(String name) {return tbl_hash.Has(name);}
|
||||
public boolean Meta_tbl_exists(String tbl) {return tbl_hash.Has(tbl);}
|
||||
public boolean Meta_fld_exists(String tbl, String fld) {
|
||||
Mem_tbl mem_tbl = (Mem_tbl)tbl_hash.Get_by(tbl); if (mem_tbl == null) return false;
|
||||
return mem_tbl.Meta().Flds_has(fld);
|
||||
}
|
||||
// public boolean Meta_fld_exists(String name) {return tbl_hash.Has(name);}
|
||||
public static final Db_engine__mem _ = new Db_engine__mem(); Db_engine__mem() {}
|
||||
}
|
||||
|
||||
@@ -28,25 +28,16 @@ public class Db_rdr__mem implements Db_rdr {
|
||||
row = rows[row_idx];
|
||||
return rv;
|
||||
}
|
||||
public byte[] Read_bry(int i) {return (byte[])row.Get_at(i);}
|
||||
public byte[] Read_bry(String k) {return (byte[])row.Get_by(k);}
|
||||
public String Read_str(int i) {return (String)row.Get_at(i);}
|
||||
public String Read_str(String k) {return (String)row.Get_by(k);}
|
||||
public byte[] Read_bry_by_str(int i) {return Bry_.new_u8_safe((String)row.Get_at(i));} // NOTE: null b/c db can have NULL
|
||||
public byte[] Read_bry_by_str(String k) {return Bry_.new_u8_safe((String)row.Get_by(k));} // NOTE: null b/c db can have NULL
|
||||
public DateAdp Read_date_by_str(int i) {return DateAdp_.parse_iso8561((String)row.Get_at(i));}
|
||||
@gplx.Virtual public byte[] Read_bry_in_parts(String tbl, String fld, String crt_key, Object crt_val) {throw Err_.not_implemented_();}
|
||||
public DateAdp Read_date_by_str(String k) {return DateAdp_.parse_iso8561((String)row.Get_by(k));}
|
||||
public byte Read_byte(int i) {return Byte_.cast_(row.Get_at(i));}
|
||||
public byte Read_byte(String k) {return Byte_.cast_(row.Get_by(k));}
|
||||
public int Read_int(int i) {return Int_.cast_(row.Get_at(i));}
|
||||
public int Read_int(String k) {return Int_.cast_(row.Get_by(k));}
|
||||
public long Read_long(int i) {return Long_.cast_(row.Get_at(i));}
|
||||
public long Read_long(String k) {return Long_.cast_(row.Get_by(k));}
|
||||
public float Read_float(int i) {return Float_.cast_(row.Get_at(i));}
|
||||
public float Read_float(String k) {return Float_.cast_(row.Get_by(k));}
|
||||
public double Read_double(int i) {return Double_.cast_(row.Get_at(i));}
|
||||
public double Read_double(String k) {return Double_.cast_(row.Get_by(k));}
|
||||
public boolean Read_bool_by_byte(int i) {return Byte_.cast_(row.Get_at(i)) == 1;}
|
||||
public boolean Read_bool_by_byte(String k) {return Byte_.cast_(row.Get_by(k)) == 1;}
|
||||
public void Rls() {}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ package gplx.dbs.engines.mems; import gplx.*; import gplx.dbs.*; import gplx.dbs
|
||||
import gplx.core.primitives.*; import gplx.core.criterias.*; import gplx.dbs.qrys.*;
|
||||
public class Mem_tbl {
|
||||
private final List_adp rows = List_adp_.new_(); private final List_adp where_rows = List_adp_.new_();
|
||||
private final Hash_adp autonum_hash = Hash_adp_.new_();
|
||||
private final Db_meta_tbl meta;
|
||||
private final Hash_adp autonum_hash = Hash_adp_.new_();
|
||||
public Mem_tbl(Db_meta_tbl meta) {this.meta = meta;}
|
||||
public Db_meta_tbl Meta() {return meta;} private final Db_meta_tbl meta;
|
||||
public int Insert(Db_stmt__mem stmt) {
|
||||
Mem_itm itm = new Mem_itm();
|
||||
Db_meta_fld[] flds = meta.Flds();
|
||||
|
||||
@@ -16,8 +16,8 @@ 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.engines.nulls; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Null_conn_info extends Db_conn_info__base {
|
||||
public class Noop_conn_info extends Db_conn_info__base {
|
||||
@Override public String Tid() {return Tid_const;} public static final String Tid_const = "null_db";
|
||||
@Override public Db_conn_info New_self(String raw, GfoMsg m) {return this;}
|
||||
public static final Null_conn_info _ = new Null_conn_info(); Null_conn_info() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
public static final Noop_conn_info I = new Noop_conn_info(); Noop_conn_info() {this.Ctor("", "", "gplx_key=null_db", "");}
|
||||
}
|
||||
@@ -16,8 +16,8 @@ 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.engines.nulls; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
public class Null_engine implements Db_engine {
|
||||
public String Tid() {return Null_conn_info.Tid_const;}
|
||||
public class Noop_engine implements Db_engine {
|
||||
public String Tid() {return Noop_conn_info.Tid_const;}
|
||||
public Db_conn_info Conn_info() {return Db_conn_info_.Null;}
|
||||
public void Conn_open() {}
|
||||
public void Conn_term() {}
|
||||
@@ -38,6 +38,7 @@ public class Null_engine implements Db_engine {
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
public boolean Schema_tbl_exists(String name) {return false;}
|
||||
public static final Null_engine _ = new Null_engine(); Null_engine() {}
|
||||
public boolean Meta_tbl_exists(String tbl) {return false;}
|
||||
public boolean Meta_fld_exists(String tbl, String fld) {return false;}
|
||||
public static final Noop_engine _ = new Noop_engine(); Noop_engine() {}
|
||||
}
|
||||
@@ -39,7 +39,8 @@ public class Sqlite_engine extends Db_engine_sql_base {
|
||||
@Override public void Txn_end() {txn_mgr.Txn_end();}
|
||||
@Override public void Txn_cxl() {txn_mgr.Txn_cxl();}
|
||||
@Override public void Txn_sav() {txn_mgr.Txn_sav();}
|
||||
@Override public boolean Schema_tbl_exists(String name) {return schema_mgr.Tbl_exists(name);}
|
||||
@Override public boolean Meta_tbl_exists(String tbl) {return schema_mgr.Tbl_exists(tbl);}
|
||||
@Override public boolean Meta_fld_exists(String tbl, String fld) {return schema_mgr.Fld_exists(tbl, fld);}
|
||||
static boolean loaded = false;
|
||||
@gplx.Internal @Override protected Connection Conn_new() {
|
||||
if (!loaded) {
|
||||
@@ -55,8 +56,7 @@ public class Sqlite_engine extends Db_engine_sql_base {
|
||||
}
|
||||
public static final Sqlite_engine _ = new Sqlite_engine();
|
||||
}
|
||||
class Db_rdr__sqlite extends Db_rdr__basic { @Override public byte Read_byte(int i) {try {return (byte)rdr.getInt(i + 1);} catch (Exception e) {throw Err_.new_("read failed: i={0} type={1} err={2}", i, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@Override public byte Read_byte(String k) {try {return (byte)Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
class Db_rdr__sqlite extends Db_rdr__basic { @Override public byte Read_byte(String k) {try {return (byte)Int_.cast_(rdr.getObject(k));} catch (Exception e) {throw Err_.new_("read failed: k={0} type={1} err={2}", k, Byte_.Cls_val_name, Err_.Message_lang(e));}}
|
||||
@Override public boolean Read_bool_by_byte(String k) {
|
||||
try {
|
||||
int val = rdr.getInt(k);
|
||||
|
||||
@@ -16,36 +16,47 @@ 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.engines.sqlite; import gplx.*; import gplx.dbs.*; import gplx.dbs.engines.*;
|
||||
import gplx.dbs.schemas.*; import gplx.dbs.qrys.*;
|
||||
import gplx.dbs.qrys.*;
|
||||
import gplx.dbs.metas.*; import gplx.dbs.metas.parsers.*;
|
||||
public class Sqlite_schema_mgr {
|
||||
private final Db_engine engine; private boolean init = true;
|
||||
public Sqlite_schema_mgr(Db_engine engine) {this.engine = engine;}
|
||||
public Schema_tbl_mgr Tbl_mgr() {return tbl_mgr;} private final Schema_tbl_mgr tbl_mgr = new Schema_tbl_mgr();
|
||||
public Schema_idx_mgr Idx_mgr() {return idx_mgr;} private final Schema_idx_mgr idx_mgr = new Schema_idx_mgr();
|
||||
public Meta_tbl_mgr Tbl_mgr() {return tbl_mgr;} private final Meta_tbl_mgr tbl_mgr = new Meta_tbl_mgr();
|
||||
public Meta_idx_mgr Idx_mgr() {return idx_mgr;} private final Meta_idx_mgr idx_mgr = new Meta_idx_mgr();
|
||||
public boolean Tbl_exists(String name) {
|
||||
if (init) Init(engine);
|
||||
return tbl_mgr.Has(name);
|
||||
}
|
||||
public boolean Fld_exists(String tbl, String fld) {
|
||||
if (init) Init(engine);
|
||||
Meta_tbl_itm tbl_itm = tbl_mgr.Get_by(tbl);
|
||||
return (tbl_itm == null) ? false : tbl_itm.Flds().Has(fld);
|
||||
}
|
||||
private void Init(Db_engine engine) {
|
||||
init = false;
|
||||
Gfo_usr_dlg_.I.Log_many("", "", "db.schema.load.bgn: conn=~{0}", engine.Conn_info().Xto_api());
|
||||
Meta_parser__tbl tbl_parser = new Meta_parser__tbl();
|
||||
Db_qry__select_in_tbl qry = Db_qry__select_in_tbl.new_("sqlite_master", String_.Ary_empty, String_.Ary("type", "name", "sql"), Db_qry__select_in_tbl.Order_by_null);
|
||||
Db_rdr rdr = engine.New_stmt_prep(qry).Exec_select__rls_auto();
|
||||
try {
|
||||
while (rdr.Move_next()) {
|
||||
String type_str = rdr.Read_str(0);
|
||||
int type_int = Schema_itm_tid.Xto_int(type_str);
|
||||
String type_str = rdr.Read_str("type");
|
||||
String name = rdr.Read_str("name");
|
||||
String sql = rdr.Read_str("sql");
|
||||
int type_int = Meta_itm_tid.Xto_int(type_str);
|
||||
switch (type_int) {
|
||||
case Schema_itm_tid.Tid_table:
|
||||
Schema_tbl_itm tbl_itm = new Schema_tbl_itm(rdr.Read_str(1), rdr.Read_str(2));
|
||||
case Meta_itm_tid.Tid_table:
|
||||
if (String_.Eq(name, "sqlite_sequence")) continue; // ignore b/c of non-orthodox syntax; EX: "CREATE TABLE sqlite_sequence(name, seq)";
|
||||
// Meta_tbl_itm tbl_itm = new Meta_tbl_itm(tbl_name, tbl_sql);
|
||||
Meta_tbl_itm tbl_itm = tbl_parser.Parse(Bry_.new_u8(sql));
|
||||
tbl_mgr.Add(tbl_itm);
|
||||
break;
|
||||
case Schema_itm_tid.Tid_index:
|
||||
Schema_idx_itm idx_itm = new Schema_idx_itm(rdr.Read_str(1), rdr.Read_str(2));
|
||||
case Meta_itm_tid.Tid_index:
|
||||
Meta_idx_itm idx_itm = new Meta_idx_itm(name, sql);
|
||||
idx_mgr.Add(idx_itm);
|
||||
break;
|
||||
default:
|
||||
Gfo_usr_dlg_.I.Log_many("", "", "db.schema.unknown type: conn=~{0} type=~{1} name=~{2} sql=~{3}", engine.Conn_info().Xto_api(), type_str, rdr.Read_str(1), rdr.Read_str(2));
|
||||
Gfo_usr_dlg_.I.Log_many("", "", "db.schema.unknown type: conn=~{0} type=~{1} name=~{2} sql=~{3}", engine.Conn_info().Xto_api(), type_str, name, sql);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,8 @@ public class TdbEngine implements Db_engine {
|
||||
public void Ddl_delete_tbl(String tbl) {}
|
||||
public void Env_db_attach(String alias, Io_url db_url) {}
|
||||
public void Env_db_detach(String alias) {}
|
||||
public boolean Schema_tbl_exists(String name) {return false;}
|
||||
public boolean Meta_tbl_exists(String name) {return false;}
|
||||
public boolean Meta_fld_exists(String tbl, String fld) {return false;}
|
||||
|
||||
Hash_adp wkrs = Hash_adp_.new_(); TdbDbLoadMgr loadMgr = TdbDbLoadMgr.new_(); TdbDbSaveMgr saveMgr = TdbDbSaveMgr.new_();
|
||||
public static final TdbEngine _ = new TdbEngine();
|
||||
|
||||
31
140_dbs/src/gplx/dbs/metas/Meta_fld_itm.java
Normal file
31
140_dbs/src/gplx/dbs/metas/Meta_fld_itm.java
Normal 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;
|
||||
}
|
||||
26
140_dbs/src/gplx/dbs/metas/Meta_fld_mgr.java
Normal file
26
140_dbs/src/gplx/dbs/metas/Meta_fld_mgr.java
Normal 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);}
|
||||
}
|
||||
@@ -15,9 +15,9 @@ 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.schemas; import gplx.*; import gplx.dbs.*;
|
||||
public class Schema_tbl_itm {
|
||||
public Schema_tbl_itm(String name, String sql) {this.name = name; this.sql = sql;}
|
||||
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;
|
||||
}
|
||||
@@ -15,10 +15,10 @@ 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.schemas; import gplx.*; import gplx.dbs.*;
|
||||
public class Schema_tbl_mgr {
|
||||
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(Schema_tbl_itm itm) {hash.Add(itm.Name(), itm);}
|
||||
public void Add(Meta_idx_itm itm) {hash.Add(itm.Name(), itm);}
|
||||
public boolean Has(String name) {return hash.Has(name);}
|
||||
public Schema_tbl_itm Get(String name) {return (Schema_tbl_itm)hash.Get_by(name);}
|
||||
public Meta_idx_itm Get(String name) {return (Meta_idx_itm)hash.Get_by(name);}
|
||||
}
|
||||
@@ -15,8 +15,8 @@ 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.schemas; import gplx.*; import gplx.dbs.*;
|
||||
public class Schema_itm_tid {
|
||||
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) {
|
||||
@@ -15,9 +15,10 @@ 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.schemas; import gplx.*; import gplx.dbs.*;
|
||||
public class Schema_idx_itm {
|
||||
public Schema_idx_itm(String name, String sql) {this.name = name; this.sql = sql;}
|
||||
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;
|
||||
}
|
||||
@@ -15,10 +15,10 @@ 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.schemas; import gplx.*; import gplx.dbs.*;
|
||||
public class Schema_idx_mgr {
|
||||
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(Schema_idx_itm itm) {hash.Add(itm.Name(), itm);}
|
||||
public void Add(Meta_tbl_itm itm) {hash.Add(itm.Name(), itm);}
|
||||
public boolean Has(String name) {return hash.Has(name);}
|
||||
public Schema_idx_itm Get(String name) {return (Schema_idx_itm)hash.Get_by(name);}
|
||||
public Meta_tbl_itm Get_by(String name) {return (Meta_tbl_itm)hash.Get_by(name);}
|
||||
}
|
||||
28
140_dbs/src/gplx/dbs/metas/Meta_type_itm.java
Normal file
28
140_dbs/src/gplx/dbs/metas/Meta_type_itm.java
Normal 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;
|
||||
}
|
||||
117
140_dbs/src/gplx/dbs/metas/parsers/Meta_fld_wkr__base.java
Normal file
117
140_dbs/src/gplx/dbs/metas/parsers/Meta_fld_wkr__base.java
Normal 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 Err_.new_("unclosed quote; {0}", 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 Err_.new_("invalid field_default; {0}", 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();
|
||||
}
|
||||
118
140_dbs/src/gplx/dbs/metas/parsers/Meta_parser__fld.java
Normal file
118
140_dbs/src/gplx/dbs/metas/parsers/Meta_parser__fld.java
Normal 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 Err_.new_("invalid fld type; excerpt={0}", 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 Err_.new_("invalid fld len_1; excerpt={0}", 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 Err_.new_("invalid fld len_2; excerpt={0}", 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 Err_.new_("invalid; excerpt={0}", 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;
|
||||
}
|
||||
71
140_dbs/src/gplx/dbs/metas/parsers/Meta_parser__fld_tst.java
Normal file
71
140_dbs/src/gplx/dbs/metas/parsers/Meta_parser__fld_tst.java
Normal 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()));
|
||||
}
|
||||
}
|
||||
57
140_dbs/src/gplx/dbs/metas/parsers/Meta_parser__tbl.java
Normal file
57
140_dbs/src/gplx/dbs/metas/parsers/Meta_parser__tbl.java
Normal 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 Err_.new_("unknown field; src={0}", 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 Err_.new_("premature end of flds; src={0}", rdr.Mid_by_len_safe(40));
|
||||
}
|
||||
}
|
||||
}
|
||||
private static final byte[]
|
||||
Tkn_create = Bry_.new_a7("create")
|
||||
, Tkn_table = Bry_.new_a7("table")
|
||||
;
|
||||
}
|
||||
66
140_dbs/src/gplx/dbs/metas/parsers/Meta_parser__tbl_tst.java
Normal file
66
140_dbs/src/gplx/dbs/metas/parsers/Meta_parser__tbl_tst.java
Normal 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;
|
||||
}
|
||||
}
|
||||
57
140_dbs/src/gplx/dbs/metas/parsers/Sql_bry_rdr.java
Normal file
57
140_dbs/src/gplx/dbs/metas/parsers/Sql_bry_rdr.java
Normal 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.Asterisk; end_bry = Comm_end_multi; break;
|
||||
case Byte_ascii.Tab: case Byte_ascii.NewLine: case Byte_ascii.CarriageReturn: 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.NewLine_bry, Comm_end_multi = Bry_.new_a7("*/");
|
||||
}
|
||||
52
140_dbs/src/gplx/dbs/metas/parsers/Sql_bry_rdr_tst.java
Normal file
52
140_dbs/src/gplx/dbs/metas/parsers/Sql_bry_rdr_tst.java
Normal 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()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user