User: dsundstrom
Date: 01/08/26 12:37:10
Modified: src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql
EJBQLParser.java SQLTarget.java
Log:
Added ejbSelect query support.
Revision Changes Path
1.4 +22 -20
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql/EJBQLParser.java
Index: EJBQLParser.java
===================================================================
RCS file:
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql/EJBQLParser.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- EJBQLParser.java 2001/08/19 22:50:33 1.3
+++ EJBQLParser.java 2001/08/26 19:37:10 1.4
@@ -18,6 +18,7 @@
import org.jboss.ejb.plugins.cmp.ejbql.Sequence;
import org.jboss.ejb.plugins.cmp.ejbql.StringLiteral;
import org.jboss.ejb.plugins.cmp.ejbql.Symbol;
+import org.jboss.ejb.plugins.cmp.ejbql.Token;
import org.jboss.ejb.plugins.cmp.ejbql.Word;
public class EJBQLParser {
@@ -47,6 +48,21 @@
s.add(new Repetition(commaList));
+ s.setAssembler(new Assembler() {
+ public void workOn(Assembly a) {
+ Token t = a.peekToken();
+ // did we totaly match the from clause?
+ // only true if there are no tokens left of the next
token is "WHERE"
+ if(t == null ||
t.toString().equalsIgnoreCase("WHERE")) {
+ SQLTarget target = (SQLTarget)a.getTarget();
+ List path = (List)a.pop();
+ target.setSelectPath(path);
+ } else {
+ a.setInvalid();
+ }
+ }
+ });
+
return s;
}
@@ -203,32 +219,18 @@
if(element.equalsIgnoreCase("SELECT"))
{
Collections.reverse(path);
- target.setSelectPath(path);
+ a.push(path);
return;
- }
-
if(element.equalsIgnoreCase("DISTINCT")) {
- a.pop(); // pop the word
'SELECT'
+ } else
if(element.equalsIgnoreCase("DISTINCT")) {
target.setSelectDistinct(true);
- Collections.reverse(path);
- target.setSelectPath(path);
- return;
+ } else
if(element.equalsIgnoreCase("OBJECT")) {
+ // ignore the object keyword,
it is just sugar to make parsing easy.
+ } else {
+ path.add(element);
}
- if(element.equalsIgnoreCase("OBJECT"))
{
- // pop the next word (will be
distinct or select)
- String word =
a.pop().toString();
-
if("DISTINCT".equalsIgnoreCase(word)) {
-
target.setSelectDistinct(true);
- a.pop(); // pop the
word 'SELECT'
- }
- Collections.reverse(path);
- target.setSelectPath(path);
- return;
- }
- path.add(element);
}
}
});
-
}
return selClau;
}
1.4 +135 -66
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql/SQLTarget.java
Index: SQLTarget.java
===================================================================
RCS file:
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/ejbql/SQLTarget.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SQLTarget.java 2001/08/19 22:50:33 1.3
+++ SQLTarget.java 2001/08/26 19:37:10 1.4
@@ -33,11 +33,17 @@
private final List inputParameters = new ArrayList();
private boolean isSelectDistinct;
- private String selectIdentifier;
- private List selectPath;
+ private String selectPath;
private String whereClause = "";
+ // cached generated sql
+ private String sql;
+
+ /**
+ * Constructs an a sql target for an EJB-QL query over the specified
application.
+ * @param application the application over which this query is defined
+ */
public SQLTarget(Application application) {
this.application = application;
@@ -58,6 +64,10 @@
}
}
+ /**
+ * Constructs a copy of the supplied sql target.
+ * @param target the SQLTarget to be coppied
+ */
public SQLTarget(SQLTarget target) {
application = target.application;
pathElements.putAll(target.pathElements);
@@ -65,20 +75,84 @@
inputParameters.addAll(target.inputParameters);
isSelectDistinct = target.isSelectDistinct;
- selectIdentifier = target.selectIdentifier;
selectPath = target.selectPath;
whereClause = target.whereClause;
+
+ sql = target.sql;
}
+ /**
+ * Set this target to generate a sql statement that returns distinct result
set.
+ * This means that the sql will begin with SELECT DISTINCT.
+ * @param isSelectDisctinct should this target generate a SELECT DISTINCT query
+ */
public void setSelectDistinct(boolean isSelectDistinct) {
this.isSelectDistinct = isSelectDistinct;
}
- public void setSelectPath(List selectPath) {
- this.selectPath = selectPath;
+ /**
+ * Set the path to the element to select. The path is a list of the string
names.
+ * @param selectPath list of strings that make up the path to select
+ */
+ public void setSelectPath(List selectPathList) {
+ //this.selectPath = selectPath;
+ if(selectPathList.isEmpty()) {
+ throw new IllegalArgumentException("SelectPathList is empty");
+ }
+
+ // is this a select object(o) style query?
+ if(selectPathList.size() == 1) {
+ String path = (String)selectPathList.get(0);
+ AbstractSchema schema = (AbstractSchema)pathElements.get(path);
+ if(schema == null) {
+ throw new IllegalStateException("Unknown identifier: "
+ path);
+ }
+ selectPath = path;
+ } else {
+ // select a.b.c.d style query
+ String path = (String)selectPathList.get(0);
+ for(int i=1; i < selectPathList.size(); i++) {
+ // are we done yet?
+ if(i<selectPathList.size()-1) {
+ // nope, assure that the next cmr field exists
and update path
+ path = getSingleValuedCMRField(path,
(String)selectPathList.get(i));
+ } else {
+ // get the final cmp field, if possible,
otherwise it is a single valued cmr field
+ String cmpFieldPath = getCMPField(path,
(String)selectPathList.get(i));
+ if(cmpFieldPath != null) {
+ path = cmpFieldPath;
+ } else {
+ // create the single valued cmr field
object
+ String cmrFieldPath =
this.getSingleValuedCMRField(path, (String)selectPathList.get(i));
+ if(cmrFieldPath == null) {
+ throw new
IllegalStateException("Unknown path: " + path + "." + selectPathList.get(i));
+ }
+ path = cmrFieldPath;
+ }
+ }
+ }
+ selectPath = path;
+ }
}
+ public String getSelectPath() {
+ return selectPath;
+ }
+
+ public Object getSelectBridgeObject() {
+ Object selectPathElement = pathElements.get(selectPath);
+ if(selectPathElement instanceof PathElement) {
+ PathElement pathElement = (PathElement)selectPathElement;
+ return pathElement.getEntityBridge();
+ } else if(selectPathElement instanceof CMPField) {
+ CMPField cmpField = (CMPField)selectPathElement;
+ return cmpField.getCMPFieldBridge();
+ }
+ throw new IllegalStateException("Select path element is instance of
unknown type: " +
+ "selectPath=" + selectPath + " selectPathElement=" +
selectPathElement);
+ }
+
public void setWhereClause(String whereClause) {
this.whereClause = whereClause;
}
@@ -102,7 +176,7 @@
throw new IllegalArgumentException("path must map to an
instance CollectionValuedCMRField: path="+path+", mappedPath="+o);
}
CollectionValuedCMRField cmrField = (CollectionValuedCMRField)o;
-
+
pathElements.put(identifier, cmrField);
}
@@ -217,73 +291,68 @@
}
public String toSQL() {
- Map identifiersByPathElement = getIdentifiersByPathElement();
- StringBuffer buf = new StringBuffer();
- buf.append("SELECT ");
- if(isSelectDistinct) {
- buf.append("DISTINCT ");
- }
-
- if(selectPath.size() == 1) {
- AbstractSchema schema =
(AbstractSchema)pathElements.get(selectPath.get(0));
- buf.append(schema.getSelectClause(identifiersByPathElement));
- } else {
-
- String path = (String)selectPath.get(0);
- PathElement selectPathElement = null;
- CMPField selectField = null;
- for(int i=1; i < selectPath.size(); i++) {
- if(i==selectPath.size()-1) {
- // will create the cmp field object, if
possible
- String cmpFieldPath = getCMPField(path,
(String)selectPath.get(i));
- if(cmpFieldPath != null) {
- CMPField cmpField =
(CMPField)pathElements.get(cmpFieldPath);
-
buf.append(cmpField.getColumnNamesClause(identifiersByPathElement));
- } else {
- // create the single valued cmr field
object
- path =
this.getSingleValuedCMRField(path, (String)selectPath.get(i));
- SingleValuedCMRField cmrField =
(SingleValuedCMRField)pathElements.get(path);
-
buf.append(cmrField.getSelectClause(identifiersByPathElement));
- }
- } else {
- path = this.getSingleValuedCMRField(path,
(String)selectPath.get(i));
- }
- }
- }
-
- buf.append(" FROM ");
-
- for(Iterator i = getUniquePathElements().iterator(); i.hasNext(); ) {
- PathElement pathElement = (PathElement)i.next();
-
buf.append(pathElement.getTableDeclarations(identifiersByPathElement));
- if(i.hasNext()) {
- buf.append(", ");
+ if(sql == null) {
+ Map identifiersByPathElement = getIdentifiersByPathElement();
+ StringBuffer buf = new StringBuffer();
+
+ // SELECT
+ buf.append("SELECT ");
+ if(isSelectDistinct) {
+ buf.append("DISTINCT ");
}
- }
-
- Set cmrFields = getUniqueCMRFields();
- if(whereClause.length() > 0 || cmrFields.size() > 0) {
- buf.append(" WHERE ");
- if(whereClause.length() > 0) {
- if(cmrFields.size() > 0) {
- buf.append("(");
- }
- buf.append(whereClause);
- if(cmrFields.size() > 0) {
- buf.append(") AND ");
+ Object selectPathElement = pathElements.get(selectPath);
+ if(selectPathElement instanceof AbstractSchema) {
+ AbstractSchema schema =
(AbstractSchema)selectPathElement;
+
buf.append(schema.getSelectClause(identifiersByPathElement));
+ } else if(selectPathElement instanceof SingleValuedCMRField) {
+ SingleValuedCMRField cmrField =
(SingleValuedCMRField)selectPathElement;
+
buf.append(cmrField.getSelectClause(identifiersByPathElement));
+ } else if(selectPathElement instanceof CMPField) {
+ CMPField cmpField = (CMPField)selectPathElement;
+
buf.append(cmpField.getColumnNamesClause(identifiersByPathElement));
+ } else {
+ throw new IllegalStateException("Path element is
instance of unknown type: " +
+ "selectPath=" + selectPath + "
selectPathElement=" + selectPathElement);
+ }
+
+ // FROM
+ buf.append(" FROM ");
+
+ for(Iterator i = getUniquePathElements().iterator();
i.hasNext(); ) {
+ PathElement pathElement = (PathElement)i.next();
+
buf.append(pathElement.getTableDeclarations(identifiersByPathElement));
+ if(i.hasNext()) {
+ buf.append(", ");
}
}
-
- for(Iterator i = getUniqueCMRFields().iterator(); i.hasNext();
) {
- CMRField pathElement = (CMRField)i.next();
-
buf.append(pathElement.getTableWhereClause(identifiersByPathElement));
- if(i.hasNext()) {
- buf.append(" AND ");
+
+ // [WHERE]
+ Set cmrFields = getUniqueCMRFields();
+ if(whereClause.length() > 0 || cmrFields.size() > 0) {
+ buf.append(" WHERE ");
+
+ if(whereClause.length() > 0) {
+ if(cmrFields.size() > 0) {
+ buf.append("(");
+ }
+ buf.append(whereClause);
+ if(cmrFields.size() > 0) {
+ buf.append(") AND ");
+ }
+ }
+
+ for(Iterator i = getUniqueCMRFields().iterator();
i.hasNext(); ) {
+ CMRField pathElement = (CMRField)i.next();
+
buf.append(pathElement.getTableWhereClause(identifiersByPathElement));
+ if(i.hasNext()) {
+ buf.append(" AND ");
+ }
}
}
+ sql = buf.toString();
}
- return buf.toString();
+ return sql;
}
public List getInputParameters() {
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development