Db_conn_fxt.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012-2017 gnosygnu@gmail.com
  4. XOWA is licensed under the terms of the General Public License (GPL) Version 3,
  5. or alternatively under the terms of the Apache License Version 2.0.
  6. You may use XOWA according to either of these licenses as is most appropriate
  7. for your project on a case-by-case basis.
  8. The terms of each license can be found in the source code repository:
  9. GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
  10. Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
  11. */
  12. package gplx.dbs; import gplx.*;
  13. import gplx.core.stores.*;
  14. import gplx.dbs.engines.sqlite.*; import gplx.dbs.engines.mysql.*; import gplx.dbs.engines.postgres.*; import gplx.core.gfo_ndes.*;
  15. public class Db_conn_fxt implements Rls_able {
  16. public Db_conn Conn() {return conn;} public Db_conn_fxt Conn_(Db_conn v) {conn = v; return this;} Db_conn conn;
  17. public void DmlAffectedAvailable_(boolean v) {dmlAffectedAvailable = v;} private boolean dmlAffectedAvailable = true;
  18. public void ini_DeleteAll(String tbl) {conn.Exec_qry(Db_qry_.delete_tbl_(tbl));}
  19. public void tst_ExecDml(int expd, Db_qry qry) {
  20. int actl = conn.Exec_qry(qry);
  21. if (dmlAffectedAvailable)
  22. Tfds.Eq(expd, actl, "Exec_qry failed: sql={0}", qry.To_sql__exec(conn.Engine().Sql_wtr()));
  23. }
  24. public void tst_ExecRdrTbl(int expd, String tblName) {tst_ExecRdr(expd, Db_qry_.select_tbl_(tblName));}
  25. public void tst_ExecRdr(int expd, Db_qry qry) {
  26. DataRdr rdr = DataRdr_.Null;
  27. try {
  28. rdr = conn.Exec_qry_as_old_rdr(qry);
  29. tbl = GfoNde_.rdr_(rdr);
  30. }
  31. catch (Exception e) {Err_.Noop(e); rdr.Rls();}
  32. Tfds.Eq(expd, tbl.Subs().Count(), "Exec_qry_as_rdr failed: sql={0}", qry.To_sql__exec(conn.Engine().Sql_wtr()));
  33. } GfoNde tbl;
  34. public GfoNde tst_RowAry(int index, Object... expdValAry) {
  35. GfoNde record = tbl.Subs().FetchAt_asGfoNde(index);
  36. Object[] actlValAry = new Object[expdValAry.length];
  37. for (int i = 0; i < actlValAry.length; i++)
  38. actlValAry[i] = record.ReadAt(i);
  39. Tfds.Eq_ary(actlValAry, expdValAry);
  40. return record;
  41. }
  42. public void Rls() {conn.Rls_conn();}
  43. public static Db_conn Mysql() {return Db_conn_pool.Instance.Get_or_new(Mysql_conn_info.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
  44. public static Db_conn Tdb(String fileName) {return Db_conn_pool.Instance.Get_or_new(Db_conn_info_.tdb_(Tfds.RscDir.GenSubDir_nest("140_dbs", "tdbs").GenSubFil(fileName)));}
  45. public static Db_conn Postgres() {return Db_conn_pool.Instance.Get_or_new(Postgres_conn_info.new_("127.0.0.1", "unit_tests", "gplx_user", "gplx_password"));}
  46. public static Db_conn Sqlite() {return Db_conn_pool.Instance.Get_or_new(Sqlite_conn_info.load_(Tfds.RscDir.GenSubFil_nest("140_dbs", "sqlite", "unit_tests.db")));}
  47. public static final boolean SkipPostgres = Tfds.SkipDb || true;
  48. }