1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2024-10-27 20:34:16 +00:00
gnosygnu_xowa/140_dbs/src_130_misc/gplx/dbs/PoolIds.java

51 lines
2.0 KiB
Java
Raw Normal View History

2014-06-30 04:04:32 +00:00
/*
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; import gplx.*;
public class PoolIds {
2015-01-26 01:56:50 +00:00
public int FetchNext(Db_conn conn, String url) {
2014-06-30 04:04:32 +00:00
Db_qry_select cmd = Db_qry_.select_().From_(Tbl_Name).Where_(Db_crt_.eq_(Fld_id_path, url));
int rv = 0;//boolean isNew = true;
DataRdr rdr = DataRdr_.Null;
try {
2015-01-26 01:56:50 +00:00
rdr = cmd.Exec_qry_as_rdr(conn);
2014-06-30 04:04:32 +00:00
if (rdr.MoveNextPeer()) {
rv = rdr.ReadInt(Fld_id_next_id);
}
}
finally {rdr.Rls();}
return rv;
}
public int FetchNextAndCommit(String dbInfo, String url) {
2015-01-26 01:56:50 +00:00
Db_conn conn = Db_conn_pool_old._.Get_or_new(dbInfo);
int rv = PoolIds._.FetchNext(conn, url);
PoolIds._.Commit(conn, url, rv + 1);
2014-06-30 04:04:32 +00:00
return rv;
}
2015-01-26 01:56:50 +00:00
public void Commit(Db_conn conn, String url, int val) {
int rv = conn.Exec_qry(Db_qry_.update_(Tbl_Name, Db_crt_.eq_(Fld_id_path, url)).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
2014-06-30 04:04:32 +00:00
if (rv == 0) {
2015-01-26 01:56:50 +00:00
rv = conn.Exec_qry(Db_qry_.insert_(Tbl_Name).Arg_(Fld_id_path, url).Arg_(Fld_id_next_id, val));
2014-06-30 04:04:32 +00:00
}
2015-01-26 01:56:50 +00:00
if (rv != 1) throw Err_.new_("failed to update nextId").Add("url", url).Add("nextId", val);
2014-06-30 04:04:32 +00:00
}
public static final String Tbl_Name = "pool_ids";
@gplx.Internal protected static final String Fld_id_path = "id_path";
@gplx.Internal protected static final String Fld_id_next_id = "id_next_id";
public static final PoolIds _ = new PoolIds(); PoolIds() {}
}