You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ha...@apache.org on 2001/12/18 14:55:05 UTC
cvs commit: jakarta-avalon-cornerstone/apps/db/src/scripts/lxsql Test2.script
hammant 01/12/18 05:55:05
Modified: apps/db build.xml
apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql
BasicLXSQLSelectSimpleHandler.java
Added: apps/db/src/scripts/lxsql Test2.script
Log:
trim, concat, const (nested too) now work in LXSQL queries
Revision Changes Path
1.26 +9 -0 jakarta-avalon-cornerstone/apps/db/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/build.xml,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- build.xml 2001/12/17 09:22:01 1.25
+++ build.xml 2001/12/18 13:55:05 1.26
@@ -352,6 +352,15 @@
</java>
</target>
+ <target name="testl2" depends="main">
+ <property name="script" location="src/scripts/lxsql/Test2.script"/>
+ <java classname="org.apache.avalon.db.test.Tester" fork="true">
+ <arg value="lxsql"/>
+ <arg value="${script}"/>
+ <classpath refid="test.path"/>
+ </java>
+ </target>
+
<target name="tests1" depends="main">
<property name="script" location="src/scripts/sql/Test1.script"/>
<java classname="org.apache.avalon.db.test.Tester" fork="true">
1.9 +119 -33 jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLSelectSimpleHandler.java
Index: BasicLXSQLSelectSimpleHandler.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-cornerstone/apps/db/src/java/org/apache/avalon/db/basic/handlers/lxsql/BasicLXSQLSelectSimpleHandler.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- BasicLXSQLSelectSimpleHandler.java 2001/12/03 11:18:35 1.8
+++ BasicLXSQLSelectSimpleHandler.java 2001/12/18 13:55:05 1.9
@@ -21,10 +21,15 @@
import org.apache.avalon.db.data.Column;
import org.apache.avalon.db.data.Queryable;
import org.apache.avalon.db.data.impl.AbstractQueryable;
+import org.apache.avalon.db.data.impl.DefaultContrivedStringFunctionColumn;
+import org.apache.avalon.db.data.impl.DefaultStringConstantColumn;
+import org.apache.avalon.db.functions.impl.ConcatFunction;
+import org.apache.avalon.db.functions.impl.TrimStringFunction;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
+import org.w3c.dom.Node;
import java.util.Vector;
@@ -35,7 +40,7 @@
*
* @author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">g-froehlich@gmx.de</a>
* @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
- * @version $Revision: 1.8 $
+ * @version $Revision: 1.9 $
*/
public class BasicLXSQLSelectSimpleHandler extends BasicLXSQLHandler {
@@ -74,19 +79,10 @@
AbstractQueryable queryable = (AbstractQueryable) mDatabasePersistor.getQueryable(tables[0]);
- String[] cols = getColumns(rootElement);
- Column[] columns = null;
+ Column[] cols = getColumns(queryable, rootElement);
- // TODO - not perfect.
- if (cols.length != 0 && cols[0].equals("*")) {
- columns = queryable.getColumns();
- } else {
- columns = convertColumns(queryable, cols);
- }
-
- Select action = new BasicSelectSimple(tables,
- convertColumns(queryable, getColumns(rootElement)),
- new BasicCriteria(criteriaHelper.getCriteria()));
+ Select action = new BasicSelectSimple(tables, cols,
+ new BasicCriteria(criteriaHelper.getCriteria()));
mParser.initializeAction(action);
action.execute();
@@ -122,20 +118,9 @@
return tablenames;
}
- protected Column[] convertColumns(AbstractQueryable queryable, String[] columnNames) {
-
- //TODO * could be with other contrived columns
-
- Column[] columns = new Column[columnNames.length];
- for ( int i = 0; i < columnNames.length; i++ ) {
- columns[i] = queryable.getColumn(columnNames[i], false);
- }
- return columns;
- }
-
- private String[] getColumns(Element rootElement) throws ActionException {
+ private Column[] getColumns(AbstractQueryable queryable, Element rootElement) throws ActionException {
NodeList subRootNodes = rootElement.getChildNodes();
- String[] cols = null;
+ Column[] cols = null;
for (int i=0; i < subRootNodes.getLength(); i++) {
if(subRootNodes.item(i).getNodeName().equals("columns")) {
@@ -146,17 +131,21 @@
Vector colVec = new Vector();
for (int j=0; j < subColumnsNodes.getLength(); j++) {
String nodeName = subColumnsNodes.item(j).getNodeName();
- if (nodeName.equals("column")) {
- //TODO checking columns, function...maybe extern syntax checking!?
- Element columnElement = (Element)subColumnsNodes.item(j);
- colVec.add(columnElement.getAttribute("name"));
- } else if (nodeName.equals("#text")) {
+ if (nodeName.equals("#text")) {
// do nothing, whitespace between elems
} else {
- throw new ActionException("Unknow node type '"+nodeName+"' under 'columns'");
+ //TODO checking columns, function...maybe extern syntax checking!?
+ if (isAllColumns((Element) subColumnsNodes.item(j))) {
+ Column[] cs = queryable.getColumns();
+ for (int f = 0; f< cs.length; f++) {
+ colVec.add(cs[f]);
+ }
+ } else {
+ colVec.add(getSelectColumn(queryable ,(Element) subColumnsNodes.item(j)));
+ }
}
}
- cols = new String[colVec.size()];
+ cols = new Column[colVec.size()];
colVec.copyInto(cols);
} else {
throw new ActionException(columnsElement.getFirstChild().getNodeValue()
@@ -166,4 +155,101 @@
}
return cols;
}
+
+ private boolean isAllColumns(Element elem) {
+ String nodeName = elem.getNodeName();
+ if (nodeName.equals("column")) {
+ String name = elem.getAttribute("name");
+ if (name.equals("*")) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private Column getSelectColumn(AbstractQueryable queryable, Element elem) throws ActionException {
+
+ String nodeName = elem.getNodeName();
+
+ if (nodeName.equals("column")) {
+ return getSelectColumnColumn(queryable, elem);
+ } else if (nodeName.equals("concat")) {
+ return getSelectColumnConcat(queryable, elem);
+ } else if (nodeName.equals("const")) {
+ return getSelectColumnConst(queryable, elem);
+ } else if (nodeName.equals("trim")) {
+ return getSelectColumnTrim(queryable, elem);
+ } else {
+ throw new ActionException("Unknown node type '"+nodeName+"' under 'columns'");
+ }
+
+
+ }
+
+ private Column getSelectColumnColumn(AbstractQueryable queryable, Element elem) throws ActionException {
+ String name = elem.getAttribute("name");
+ return queryable.getColumn(name, true);
+ }
+
+ private Column getSelectColumnConcat(AbstractQueryable queryable, Element concatElem) throws ActionException {
+ String as = concatElem.getAttribute("as");
+ // TODO as might be missing
+ //return queryable.getColumn(name, true);
+ if (concatElem.hasChildNodes()) {
+ NodeList subConcatNodes = concatElem.getChildNodes();
+ Vector colVec = new Vector();
+ for (int j=0; j < subConcatNodes.getLength(); j++) {
+ Node node = subConcatNodes.item(j);
+ String nodeName = node.getNodeName();
+ if (nodeName.equals("#text")) {
+ // do nothing
+ } else {
+ colVec.add(getSelectColumn(queryable,(Element) node));
+ }
+ }
+ Column[] cols = new Column[colVec.size()];
+ colVec.copyInto(cols);
+ ConcatFunction cf = new ConcatFunction();
+ cf.initialize(cols);
+ return new DefaultContrivedStringFunctionColumn(as,cf);
+ } else {
+ throw new ActionException("concat must have child elements");
+ }
+ }
+
+ private Column getSelectColumnConst(AbstractQueryable queryable, Element elem) throws ActionException {
+ String value = elem.getAttribute("value");
+ return new DefaultStringConstantColumn("TODO",value);
+ }
+
+ private Column getSelectColumnTrim(AbstractQueryable queryable, Element trimElem) throws ActionException {
+ String as = trimElem.getAttribute("as");
+ // TODO as might be missing
+ //return queryable.getColumn(name, true);
+ int count = 0;
+ if (trimElem.hasChildNodes()) {
+ NodeList subConcatNodes = trimElem.getChildNodes();
+ Vector colVec = new Vector();
+ for (int j=0; j < subConcatNodes.getLength(); j++) {
+ Node node = subConcatNodes.item(j);
+ String nodeName = node.getNodeName();
+ if (nodeName.equals("#text")) {
+ // do nothing
+ } else {
+ count++;
+ colVec.add(getSelectColumn(queryable,(Element) node));
+ }
+ }
+ if (colVec.size() > 1) {
+ throw new ActionException("trim can only have one child column");
+ }
+ TrimStringFunction tsf = new TrimStringFunction();
+ tsf.initialize(new Column[] { (Column) colVec.elementAt(0) });
+ return new DefaultContrivedStringFunctionColumn(as,tsf);
+ } else {
+ throw new ActionException("trim must have child column");
+ }
+ }
+
+
}
1.1 jakarta-avalon-cornerstone/apps/db/src/scripts/lxsql/Test2.script
Index: Test2.script
===================================================================
### SELECT 0 (function concat cols) ##!
<simple-select>
<columns>
<column name="forename"/>
<trim as="fs">
<concat as="tmp1">
<concat as="tmp2">
<column name="forename"/>
<const value=" "/>
<column name="surname"/>
</concat>
<const value=" "/>
</concat>
</trim>
<column name="surname"/>
</columns>
<from>
<table name="Flintstones"/>
</from>
</simple-select>
###
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>