Support for version 1.2 of object-relational mapping format.

See http://helma.org/rfc/mapping12new/
This commit is contained in:
hns 2002-03-13 23:40:48 +00:00
parent 3f7180ae21
commit 63eb24bae6
2 changed files with 98 additions and 148 deletions

View file

@ -199,7 +199,6 @@ public class DbMapping implements Updatable {
* completed on all other mappings in this application * completed on all other mappings in this application
*/ */
public synchronized void rewire () { public synchronized void rewire () {
if (extendsProto != null) { if (extendsProto != null) {
parentMapping = app.getDbMapping (extendsProto); parentMapping = app.getDbMapping (extendsProto);
} }
@ -236,42 +235,61 @@ public class DbMapping implements Updatable {
prop2db = p2d; prop2db = p2d;
db2prop = d2p; db2prop = d2p;
String subnodeMapping = props.getProperty ("_subnodes"); if (version == 1) {
if (subnodeMapping != null) { String subnodeMapping = props.getProperty ("_children");
try { if (subnodeMapping != null) {
// check if subnode relation already exists. If so, reuse it try {
if (subnodesRel == null) // check if subnode relation already exists. If so, reuse it
subnodesRel = new Relation (subnodeMapping, "_subnodes", this, props); if (subnodesRel == null)
subnodesRel.update (subnodeMapping, props, version); subnodesRel = new Relation (subnodeMapping, "_children", this, props);
subnodesRel.update (subnodeMapping, props, version);
if (subnodesRel.accessor != null)
propertiesRel = subnodesRel;
} catch (Exception x) { } catch (Exception x) {
app.logEvent ("Error reading _subnodes relation for "+typename+": "+x.getMessage ()); app.logEvent ("Error reading _subnodes relation for "+typename+": "+x.getMessage ());
// subnodesRel = null; // subnodesRel = null;
}
} else
subnodesRel = null;
String propertiesMapping = props.getProperty ("_properties");
if (propertiesMapping != null) {
try {
// check if property relation already exists. If so, reuse it
if (propertiesRel == null)
propertiesRel = new Relation (propertiesMapping, "_properties", this, props);
propertiesRel.update (propertiesMapping, props, version);
// take over groupby flag from subnodes, if properties are subnodes
if (propertiesRel.subnodesAreProperties && subnodesRel != null) {
propertiesRel.groupby = subnodesRel.groupby;
propertiesRel.constraints = subnodesRel.constraints;
propertiesRel.filter = subnodesRel.filter;
} }
} else
subnodesRel = null;
} else {
String subnodeMapping = props.getProperty ("_subnodes");
if (subnodeMapping != null) {
try {
// check if subnode relation already exists. If so, reuse it
if (subnodesRel == null)
subnodesRel = new Relation (subnodeMapping, "_subnodes", this, props);
subnodesRel.update (subnodeMapping, props, version);
} catch (Exception x) { } catch (Exception x) {
app.logEvent ("Error reading _properties relation for "+typename+": "+x.getMessage ()); app.logEvent ("Error reading _subnodes relation for "+typename+": "+x.getMessage ());
// propertiesRel = null; // subnodesRel = null;
} }
} else } else
propertiesRel = null; subnodesRel = null;
String propertiesMapping = props.getProperty ("_properties");
if (propertiesMapping != null) {
try {
// check if property relation already exists. If so, reuse it
if (propertiesRel == null)
propertiesRel = new Relation (propertiesMapping, "_properties", this, props);
propertiesRel.update (propertiesMapping, props, version);
// take over groupby flag from subnodes, if properties are subnodes
if (propertiesRel.subnodesAreProperties && subnodesRel != null) {
propertiesRel.groupby = subnodesRel.groupby;
propertiesRel.constraints = subnodesRel.constraints;
propertiesRel.filter = subnodesRel.filter;
}
} catch (Exception x) {
app.logEvent ("Error reading _properties relation for "+typename+": "+x.getMessage ());
// propertiesRel = null;
}
} else
propertiesRel = null;
}
if (groupbyMapping != null) { if (groupbyMapping != null) {
initGroupbyMapping (); initGroupbyMapping ();
@ -358,7 +376,7 @@ public class DbMapping implements Updatable {
* Get the primary key column name for objects using this mapping. * Get the primary key column name for objects using this mapping.
*/ */
public String getIDField () { public String getIDField () {
if (idField == null && parentMapping != null) if (idField == null && parentMapping != null)
return parentMapping.getIDField (); return parentMapping.getIDField ();
return idField; return idField;
} }

View file

@ -300,8 +300,7 @@ public class Relation {
//////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////
private void update_v1 (String desc, Properties props) { private void update_v1 (String desc, Properties props) {
boolean mountpoint = false; Application app = ownType.getApplication ();
Vector cnst = null;
if (desc == null || "".equals (desc.trim ())) { if (desc == null || "".equals (desc.trim ())) {
if (propName != null) { if (propName != null) {
@ -313,127 +312,50 @@ public class Relation {
} }
} else { } else {
desc = desc.trim (); desc = desc.trim ();
String descLower = desc.toLowerCase (); int open = desc.indexOf ("(");
if (descLower.startsWith ("collection")) { int close = desc.indexOf (")");
desc = desc.substring (10).trim (); if (open > -1 && close > open) {
virtual = true; String ref = desc.substring (0, open).trim ();
} else if (descLower.startsWith ("[mountpoint]")) { String proto = desc.substring (open+1, close).trim ();
desc = desc.substring (12).trim (); if ("collection".equalsIgnoreCase (ref)) {
virtual = true; virtual = !"_children".equalsIgnoreCase (propName);
mountpoint = true; reftype = COLLECTION;
} else if ("mountpoint".equalsIgnoreCase (ref)) {
virtual = true;
reftype = COLLECTION;
prototype = proto;
} else if ("object".equalsIgnoreCase (ref)) {
virtual = false;
reftype = REFERENCE;
} else {
throw new RuntimeException ("Invalid property Mapping: "+desc);
}
otherType = app.getDbMapping (proto);
if (otherType == null)
throw new RuntimeException ("DbMapping for "+proto+" not found from "+ownType.typename);
} else { } else {
virtual = false; virtual = false;
columnName = desc;
reftype = PRIMITIVE;
} }
if (descLower.startsWith ("[readonly]")) { String rdonly = props.getProperty (desc+".readonly");
desc = desc.substring (10).trim (); if (rdonly != null && "true".equalsIgnoreCase (rdonly)) {
readonly = true; readonly = true;
} else { } else {
readonly = false; readonly = false;
} }
} }
// the following options only apply to object and collection relations
// parse the basic properties of this mapping
parseMapping_v1 (desc, mountpoint);
// the following options only apply to object relations
if (reftype != PRIMITIVE && reftype != INVALID) { if (reftype != PRIMITIVE && reftype != INVALID) {
cnst = new Vector (); Vector newConstraints = new Vector ();
parseOptions_v1 (newConstraints, props);
Constraint c = parseConstraint_v1 (desc); constraints = new Constraint[newConstraints.size()];
newConstraints.copyInto (constraints);
if (c != null)
cnst.add (c);
parseOptions_v1 (cnst, props);
constraints = new Constraint[cnst.size()];
cnst.copyInto (constraints);
// System.err.println ("PARSED RELATION "+this);
// if (accessor != null)
// System.err.println ("SET ACCESSOR: "+accessor);
} }
} }
/**
* Parse a line describing a mapping of a property field. If the mapping is a
* object reference of a collection of objects, put any constraints in the Vector.
*/
protected void parseMapping_v1 (String desc, boolean mountpoint) {
Application app = ownType.getApplication ();
if (desc.indexOf ("<") > -1) {
reftype = COLLECTION;
int lt = desc.indexOf ("<");
int dot = desc.indexOf (".");
String other = dot < 0 ? desc.substring (lt+1).trim () : desc.substring (lt+1, dot).trim ();
otherType = app.getDbMapping (other);
if (otherType == null)
throw new RuntimeException ("DbMapping for "+other+" not found from "+ownType.typename);
columnName = null;
if (mountpoint)
prototype = other;
} else if (desc.indexOf (">") > -1) {
reftype = REFERENCE;
int bt = desc.indexOf (">");
int dot = desc.indexOf (".");
String other = dot > -1 ? desc.substring (bt+1, dot).trim () : desc.substring (bt+1).trim ();
otherType = app.getDbMapping (other);
if (otherType == null)
throw new RuntimeException ("DbMapping for "+other+" not found from "+ownType.typename);
columnName = desc.substring (0, bt).trim ();
if (mountpoint)
prototype = other;
} else if (desc.indexOf (".") > -1) {
reftype = COLLECTION;
int dot = desc.indexOf (".");
String other = desc.substring (0, dot).trim ();
otherType = app.getDbMapping (other);
if (otherType == null)
throw new RuntimeException ("DbMapping for "+other+" not found from "+ownType.typename);
columnName = null;
// set accessor
accessor = desc.substring (dot+1).trim ();
if (mountpoint)
prototype = other;
} else {
if (virtual) {
reftype = COLLECTION;
otherType = app.getDbMapping (desc);
if (otherType == null)
throw new RuntimeException ("DbMapping for "+desc+" not found from "+ownType.typename);
if (mountpoint)
prototype = desc;
} else {
reftype = PRIMITIVE;
columnName = desc.trim ();
}
}
}
/**
* Parse a line describing a mapping of a property field. If the mapping is a
* object reference of a collection of objects, put any constraints in the Vector.
*/
protected Constraint parseConstraint_v1 (String desc) {
if (desc.indexOf ("<") > -1) {
int lt = desc.indexOf ("<");
int dot = desc.indexOf (".");
String remoteField = dot < 0 ? null : desc.substring (dot+1).trim ();
String localField = lt <= 0 ? null : desc.substring (0, lt).trim ();
return new Constraint (localField, otherType.getTableName (), remoteField, false);
} else if (desc.indexOf (">") > -1) {
int bt = desc.indexOf (">");
int dot = desc.indexOf (".");
String localField = desc.substring (0, bt).trim ();
String remoteField = dot < 0 ? null : desc.substring (dot+1).trim ();
return new Constraint (localField, otherType.getTableName (), remoteField, false);
}
return null;
}
protected void parseOptions_v1 (Vector cnst, Properties props) { protected void parseOptions_v1 (Vector cnst, Properties props) {
String loading = props.getProperty (propName+".loadmode"); String loading = props.getProperty (propName+".loadmode");
@ -470,13 +392,23 @@ public class Relation {
aggressiveLoading = aggressiveCaching = false; aggressiveLoading = aggressiveCaching = false;
} }
// check if subnode condition should be applied for property relations // check if subnode condition should be applied for property relations
if ("_properties".equalsIgnoreCase (propName) || virtual) { accessor = props.getProperty (propName+".accessname");
if (accessor != null)
subnodesAreProperties = true;
// parse contstraints
String local = props.getProperty (propName+".local");
String foreign = props.getProperty (propName+".foreign");
if (local != null && foreign != null) {
cnst.addElement (new Constraint (local, otherType.getTableName (), foreign, false));
columnName = local;
}
/* if ("_properties".equalsIgnoreCase (propName) || virtual) {
String subnodes2props = props.getProperty (propName+".aresubnodes"); String subnodes2props = props.getProperty (propName+".aresubnodes");
subnodesAreProperties = "true".equalsIgnoreCase (subnodes2props); subnodesAreProperties = "true".equalsIgnoreCase (subnodes2props);
if (virtual) { if (virtual) {
String subnodefilter = props.getProperty (propName+".subnoderelation"); String subnodefilter = props.getProperty (propName+".subnoderelation");
if (subnodefilter != null) { if (subnodefilter != null) {
Constraint c = parseConstraint_v1 (subnodefilter); Constraint c = null; // parseConstraint_v1 (subnodefilter);
if (c != null) { if (c != null) {
cnst.add (c); cnst.add (c);
} }
@ -488,7 +420,7 @@ public class Relation {
virtualMapping.propertiesRel = getVirtualPropertyRelation (); virtualMapping.propertiesRel = getVirtualPropertyRelation ();
virtualMapping.lastTypeChange = ownType.lastTypeChange; virtualMapping.lastTypeChange = ownType.lastTypeChange;
} }
} } */
} }
@ -819,7 +751,7 @@ public class Relation {
public void addToQuery (StringBuffer q, INode home, INode nonvirtual) throws SQLException { public void addToQuery (StringBuffer q, INode home, INode nonvirtual) throws SQLException {
String local = null; String local = null;
INode ref = isGroupby ? home : nonvirtual; INode ref = isGroupby ? home : nonvirtual;
if (localName == null) if (localName == null || localName.equals (ref.getDbMapping ().getIDField ()))
local = ref.getID (); local = ref.getID ();
else { else {
String homeprop = ownType.columnNameToProperty (localName); String homeprop = ownType.columnNameToProperty (localName);