Added Daniel's changes to generate id from table without sequence
This commit is contained in:
parent
fbcfbc32ba
commit
cf7bdee73b
3 changed files with 45 additions and 3 deletions
|
@ -39,7 +39,12 @@ public class DbMapping {
|
||||||
|
|
||||||
String idField;
|
String idField;
|
||||||
String nameField;
|
String nameField;
|
||||||
|
|
||||||
|
// id generator via sequence
|
||||||
private String idgen;
|
private String idgen;
|
||||||
|
// id generator for dbs that don't support sequences
|
||||||
|
private String sqlidgen;
|
||||||
|
|
||||||
|
|
||||||
Schema schema = null;
|
Schema schema = null;
|
||||||
KeyDef keydef = null;
|
KeyDef keydef = null;
|
||||||
|
@ -90,6 +95,7 @@ public class DbMapping {
|
||||||
|
|
||||||
this.table = props.getProperty ("_tablename");
|
this.table = props.getProperty ("_tablename");
|
||||||
this.idgen = props.getProperty ("_idgen");
|
this.idgen = props.getProperty ("_idgen");
|
||||||
|
this.sqlidgen = props.getProperty ("_sqlidgen");
|
||||||
String sourceName = props.getProperty ("_datasource");
|
String sourceName = props.getProperty ("_datasource");
|
||||||
if (sourceName != null)
|
if (sourceName != null)
|
||||||
source = (DbSource) IServer.dbSources.get (sourceName.toLowerCase ());
|
source = (DbSource) IServer.dbSources.get (sourceName.toLowerCase ());
|
||||||
|
@ -324,6 +330,11 @@ public class DbMapping {
|
||||||
return idgen;
|
return idgen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSQLIDGen () {
|
||||||
|
return sqlidgen;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public WrappedNodeManager getWrappedNodeManager () {
|
public WrappedNodeManager getWrappedNodeManager () {
|
||||||
if (app == null)
|
if (app == null)
|
||||||
throw new RuntimeException ("Can't get node manager from internal db mapping");
|
throw new RuntimeException ("Can't get node manager from internal db mapping");
|
||||||
|
|
|
@ -475,6 +475,33 @@ public final class NodeManager {
|
||||||
// tx.timer.endEvent ("deleteNode "+node);
|
// 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 {
|
public String generateID (DbMapping map) throws Exception {
|
||||||
|
|
||||||
Transactor tx = (Transactor) Thread.currentThread ();
|
Transactor tx = (Transactor) Thread.currentThread ();
|
||||||
|
|
|
@ -116,10 +116,14 @@ import java.util.Vector;
|
||||||
|
|
||||||
public String generateID (DbMapping map) {
|
public String generateID (DbMapping map) {
|
||||||
try {
|
try {
|
||||||
if (map == null || map.getIDgen() == null)
|
if (map == null || (map.getIDgen() == null && map.getSQLIDgen() == null))
|
||||||
return nmgr.idgen.newID ();
|
return nmgr.idgen.newID ();
|
||||||
|
else {
|
||||||
|
if ("true".equalsIgnoreCase (map.getSQLIDgen()))
|
||||||
|
return nmgr.generateSQLID (map);
|
||||||
else
|
else
|
||||||
return nmgr.generateID (map);
|
return nmgr.generateID (map);
|
||||||
|
}
|
||||||
} catch (Exception x) {
|
} catch (Exception x) {
|
||||||
throw new RuntimeException (x.toString ());
|
throw new RuntimeException (x.toString ());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue