Added Daniel's changes to generate id from table without sequence

This commit is contained in:
hns 2001-02-20 21:10:17 +00:00
parent fbcfbc32ba
commit cf7bdee73b
3 changed files with 45 additions and 3 deletions

View file

@ -39,7 +39,12 @@ public class DbMapping {
String idField;
String nameField;
// id generator via sequence
private String idgen;
// id generator for dbs that don't support sequences
private String sqlidgen;
Schema schema = null;
KeyDef keydef = null;
@ -90,6 +95,7 @@ public class DbMapping {
this.table = props.getProperty ("_tablename");
this.idgen = props.getProperty ("_idgen");
this.sqlidgen = props.getProperty ("_sqlidgen");
String sourceName = props.getProperty ("_datasource");
if (sourceName != null)
source = (DbSource) IServer.dbSources.get (sourceName.toLowerCase ());
@ -324,6 +330,11 @@ public class DbMapping {
return idgen;
}
public String getSQLIDGen () {
return sqlidgen;
}
public WrappedNodeManager getWrappedNodeManager () {
if (app == null)
throw new RuntimeException ("Can't get node manager from internal db mapping");

View file

@ -475,6 +475,33 @@ public final class NodeManager {
// tx.timer.endEvent ("deleteNode "+node);
}
/**
* Generates an ID for the table by finding out the maximum current value
*/
public String generateSQLID (DbMapping map) throws Exception {
Transactor tx = (Transactor) Thread.currentThread ();
// tx.timer.beginEvent ("generateID "+map);
QueryDataSet qds = null;
String retval = null;
try {
Connection con = map.getConnection ();
String q = "SELECT "+map.getIDField()+" FROM "+map.getTableName()+" ORDER BY "+map.getIDField()+" DESC LIMIT 1";
qds = new QueryDataSet (con, q);
qds.fetchRecords ();
long currMax = qds.getRecord (0).getValue (1).asLong ();
retval = Long.toString (currMax+1);
} finally {
// tx.timer.endEvent ("generateID "+map);
if (qds != null) {
qds.close ();
}
}
return retval;
}
public String generateID (DbMapping map) throws Exception {
Transactor tx = (Transactor) Thread.currentThread ();

View file

@ -116,10 +116,14 @@ import java.util.Vector;
public String generateID (DbMapping map) {
try {
if (map == null || map.getIDgen() == null)
if (map == null || (map.getIDgen() == null && map.getSQLIDgen() == null))
return nmgr.idgen.newID ();
else
return nmgr.generateID (map);
else {
if ("true".equalsIgnoreCase (map.getSQLIDgen()))
return nmgr.generateSQLID (map);
else
return nmgr.generateID (map);
}
} catch (Exception x) {
throw new RuntimeException (x.toString ());
}