You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2005/01/14 22:11:31 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/util SqlHelper.java
brj 2005/01/14 13:11:31
Modified: src/test/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
QueryTest.java
src/java/org/apache/ojb/broker/util Tag: OJB_1_0_RELEASE
SqlHelper.java
Log:
support simple expressions in attributes (ie: price + 10)
Revision Changes Path
No revision
No revision
1.61.2.9 +3 -3 db-ojb/src/test/org/apache/ojb/broker/QueryTest.java
Index: QueryTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/QueryTest.java,v
retrieving revision 1.61.2.8
retrieving revision 1.61.2.9
diff -u -r1.61.2.8 -r1.61.2.9
--- QueryTest.java 14 Jan 2005 20:49:41 -0000 1.61.2.8
+++ QueryTest.java 14 Jan 2005 21:11:31 -0000 1.61.2.9
@@ -1549,18 +1549,18 @@
**/
public void testReportQueryExpressionInStatement()
{
- // TODO: make path expressions extent aware
+ // TODO: Resolve attributes of expressions
if(ojbSkipKnownIssueProblem("Resolve attributes of expressions")) return;
Criteria crit = new Criteria();
ReportQueryByCriteria q = QueryFactory.newReportQuery(Article.class, crit);
- q.setAttributes(new String[]{"articleId", "price + 0"});
+ q.setAttributes(new String[]{"articleId", "price+10"});
ClassDescriptor cd = broker.getClassDescriptor(q.getBaseClass());
SqlGenerator sqlg = broker.serviceSqlGenerator();
String sql = sqlg.getPreparedSelectStatement(q, cd);
assertTrue("Bad query generated. the 'price' field has not table prefix. SQL Output: " + sql, sql
- .equalsIgnoreCase("SELECT A0.Artikel_Nr,A0.PRICE + 0 FROM Artikel A0"));
+ .equalsIgnoreCase("SELECT A0.Artikel_Nr,A0.Einzelpreis+10 FROM Artikel A0"));
}
No revision
No revision
1.24.2.1 +56 -44 db-ojb/src/java/org/apache/ojb/broker/util/SqlHelper.java
Index: SqlHelper.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/SqlHelper.java,v
retrieving revision 1.24
retrieving revision 1.24.2.1
diff -u -r1.24 -r1.24.2.1
--- SqlHelper.java 4 Apr 2004 23:53:37 -0000 1.24
+++ SqlHelper.java 14 Jan 2005 21:11:31 -0000 1.24.2.1
@@ -1,5 +1,7 @@
package org.apache.ojb.broker.util;
+import org.apache.commons.lang.StringUtils;
+
/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -61,49 +63,59 @@
return splitPath(aPath).column;
}
- /**
- * Split a path into column , prefix and suffix, the prefix contains all
- * info up to the column <br>
- * ie: avg(amount) -> amount , avg( , )<br>
- * ie: sum (accounts.amount) as theSum -> accounts.amount , sum( , ) as
- * theSum <br>
- * ie: count( distinct id ) as bla -> id , count(distinct , ) as bla <br>
- *
- * @param aPath
- * @return PathInfo
- */
- public static PathInfo splitPath(String aPath)
- {
- String prefix = null;
- String suffix = null;
- String colName = aPath;
-
- if (aPath == null)
- {
- return new PathInfo(null, null, null);
- }
+ /**
+ * Split a path into column , prefix and suffix, the prefix contains all
+ * info up to the column <br>
+ * ie: avg(amount) -> amount , avg( , )<br>
+ * ie: sum (accounts.amount) as theSum -> accounts.amount , sum( , ) as
+ * theSum <br>
+ * ie: count( distinct id ) as bla -> id , count(distinct , ) as bla <br>
+ * Supports simple expressions ie: price * 1.05
+ *
+ * TODO: cannot resolve multiple attributes in expression
+ * ie: price - bonus
+ *
+ * @param aPath
+ * @return PathInfo
+ */
+ public static PathInfo splitPath(String aPath)
+ {
+ String prefix = null;
+ String suffix = null;
+ String colName = aPath;
- // ignore leading ( and trailing ) ie: sum(avg(col1))
- int braceBegin = aPath.lastIndexOf("(");
- int braceEnd = aPath.indexOf(")");
-
- if (braceBegin >= 0 && braceEnd >= 0 && braceEnd > braceBegin)
- {
- int colBegin;
- int colEnd;
- String betweenBraces;
-
- betweenBraces = aPath.substring(braceBegin + 1, braceEnd).trim();
- // look for ie 'distinct name'
- colBegin = betweenBraces.indexOf(" ");
- // look for multiarg function like to_char(col,'format_mask')
- colEnd = betweenBraces.indexOf(",");
- colEnd = colEnd > 0 ? colEnd : betweenBraces.length();
- prefix = aPath.substring(0, braceBegin + 1) + betweenBraces.substring(0, colBegin + 1);
- colName = betweenBraces.substring(colBegin + 1, colEnd);
- suffix = betweenBraces.substring(colEnd) + aPath.substring(braceEnd);
- }
+ if (aPath == null)
+ {
+ return new PathInfo(null, null, null);
+ }
+
+ // ignore leading ( and trailing ) ie: sum(avg(col1))
+ int braceBegin = aPath.lastIndexOf("(");
+ int braceEnd = aPath.indexOf(")");
+ int opPos = StringUtils.indexOfAny(aPath, "+-/*");
+
+ if (braceBegin >= 0 && braceEnd >= 0 && braceEnd > braceBegin)
+ {
+ int colBegin;
+ int colEnd;
+ String betweenBraces;
+
+ betweenBraces = aPath.substring(braceBegin + 1, braceEnd).trim();
+ // look for ie 'distinct name'
+ colBegin = betweenBraces.indexOf(" ");
+ // look for multiarg function like to_char(col,'format_mask')
+ colEnd = betweenBraces.indexOf(",");
+ colEnd = colEnd > 0 ? colEnd : betweenBraces.length();
+ prefix = aPath.substring(0, braceBegin + 1) + betweenBraces.substring(0, colBegin + 1);
+ colName = betweenBraces.substring(colBegin + 1, colEnd);
+ suffix = betweenBraces.substring(colEnd) + aPath.substring(braceEnd);
+ }
+ else if (opPos >= 0)
+ {
+ colName = aPath.substring(0, opPos).trim();
+ suffix = aPath.substring(opPos);
+ }
- return new PathInfo(prefix, colName.trim(), suffix);
- }
+ return new PathInfo(prefix, colName.trim(), suffix);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org