You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2008/09/02 22:18:55 UTC
svn commit: r691366 - in
/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db: ./
expr/column/
Author: doebele
Date: Tue Sep 2 13:18:55 2008
New Revision: 691366
URL: http://svn.apache.org/viewvc?rev=691366&view=rev
Log:
EMPIREDB-8
Added:
incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAbstractFuncExpr.java (with props)
incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConvertExpr.java (with props)
incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBDecodeExpr.java (with props)
Removed:
incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExprEx.java
Modified:
incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumn.java
incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumnExpr.java
incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java
incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java
Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumn.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumn.java?rev=691366&r1=691365&r2=691366&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumn.java (original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumn.java Tue Sep 2 13:18:55 2008
@@ -125,7 +125,7 @@
buf.append(".");
}
// Append the name
- DBDatabaseDriver driver = getDatabaseDriver();
+ DBDatabaseDriver driver = getDatabase().getDriver();
driver.appendElementName(buf, name);
}
Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumnExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumnExpr.java?rev=691366&r1=691365&r2=691366&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumnExpr.java (original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBColumnExpr.java Tue Sep 2 13:18:55 2008
@@ -20,7 +20,6 @@
// java
import java.util.HashMap;
-import java.util.Iterator;
import java.util.Map;
import org.apache.empire.commons.Attributes;
@@ -34,6 +33,8 @@
import org.apache.empire.db.expr.column.DBCalcExpr;
import org.apache.empire.db.expr.column.DBCaseExpr;
import org.apache.empire.db.expr.column.DBConcatExpr;
+import org.apache.empire.db.expr.column.DBConvertExpr;
+import org.apache.empire.db.expr.column.DBDecodeExpr;
import org.apache.empire.db.expr.column.DBFuncExpr;
import org.apache.empire.db.expr.column.DBValueExpr;
import org.apache.empire.db.expr.compare.DBCompareColExpr;
@@ -42,10 +43,8 @@
/**
- * This class is the base class for building the SQL-Commands
- * <P>
- *
- *
+ * This class is the base class for all expressions that represent a single value.
+ * It provides a set of factory functions for SQL functions.
*/
public abstract class DBColumnExpr extends DBExpr
implements ColumnExpr
@@ -58,16 +57,6 @@
protected Attributes attributes = null;
protected Options options = null;
protected String beanPropertyName = null;
-
- /**
- * returns the Database driver or null if the Expression is not attached to an open database<BR/>
- * This function is intended for convenience only.
- */
- protected final DBDatabaseDriver getDatabaseDriver()
- {
- DBDatabase db = getDatabase();
- return (db!=null) ? db.getDriver() : null;
- }
/**
* Returns the data type of this column expression.
@@ -364,9 +353,9 @@
*/
public DBCompareColExpr like(String value, char escape)
{
- DBValueExpr textExpr = new DBValueExpr(getDatabase(), value, DataType.TEXT);
- return cmp(DBCmpType.LIKE, textExpr.getExprFromPhrase(DBDatabaseDriver.SQL_FUNC_ESCAPE,
- new Object[] { String.valueOf(escape) }, getUpdateColumn(), false));
+ DBValueExpr textExpr = new DBValueExpr(getDatabase(), value, DataType.TEXT);
+ DBFuncExpr escapeExpr = new DBFuncExpr(textExpr, DBDatabaseDriver.SQL_FUNC_ESCAPE, new Object[] { String.valueOf(escape) }, getUpdateColumn(), false, DataType.TEXT );
+ return cmp(DBCmpType.LIKE, escapeExpr);
}
/**
@@ -585,30 +574,18 @@
* Creates a new DBFuncExpr from a given SQL-PRHASE and
* optional additional parameters.
*
- * @param phrase the enum-id of the phrase (see DBDatabaseDriver.SQL_...)
+ * @param phrase the id of the SQL-phrase
* @param params the params to replace in the template
* @param isAggregate indicates whether the Function creates an aggregate
* @param dataType the resulting data Type
* @return the new DBCalcExpr object
*/
- public DBColumnExpr getExprFromPhrase(int phrase, Object[] params, DBColumn updateColumn, boolean isAggregate, DataType dataType)
+ private DBColumnExpr getExprFromPhrase(int phrase, Object[] params, DBColumn updateColumn, boolean isAggregate, DataType dataType)
{
- DBDatabaseDriver driver = getDatabase().getDriver();
- String template = driver.getSQLPhrase(phrase);
- if (params != null)
- {
- for (int i = 0; i < params.length; i++)
- { // Replace Params
- // String test =(params[i] != null) ? params[i].toString() : "";
- String value = getObjectValue(this, params[i], DBExpr.CTX_DEFAULT, ",");
- // template = template.replaceAll("\\{" + String.valueOf(i) + "\\}", value);
- template = StringUtils.replaceAll(template, "{"+ String.valueOf(i) + "}", value);
- }
- }
- return new DBFuncExpr(this, template, updateColumn, isAggregate, dataType);
+ return new DBFuncExpr(this, phrase, params, updateColumn, isAggregate, dataType);
}
- public DBColumnExpr getExprFromPhrase(int phrase, Object[] params, DBColumn updateColumn, boolean isAggregate)
+ private DBColumnExpr getExprFromPhrase(int phrase, Object[] params, DBColumn updateColumn, boolean isAggregate)
{
return getExprFromPhrase(phrase, params, updateColumn, isAggregate, getDataType());
}
@@ -621,7 +598,7 @@
*/
public DBColumnExpr parenthesis()
{
- return new DBFuncExpr(this, "(?)", getUpdateColumn(), false, getDataType());
+ return new DBFuncExpr(this, "(?)", null, getUpdateColumn(), false, getDataType());
}
/**
@@ -948,39 +925,10 @@
* @return the new DBFuncExpr object
*/
@SuppressWarnings("unchecked")
- public DBColumnExpr decode(Map list, Object otherwise)
+ public DBColumnExpr decode(Map valueMap, Object otherwise)
{
- DBDatabaseDriver driver = getDatabase().getDriver();
- StringBuilder inner = new StringBuilder();
- // Generate parts
- for (Iterator i = list.keySet().iterator(); i.hasNext();)
- {
- Object key = i.next();
- Object val = list.get(key);
-
- String part = driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE_PART);
- // part = part.replaceAll("\\{0\\}", getObjectValue(this, key, DBExpr.CTX_DEFAULT, ""));
- part = StringUtils.replaceAll(part, "{0}", getObjectValue(this, key, DBExpr.CTX_DEFAULT, ""));
-
- // part = part.replaceAll("\\{1\\}", getObjectValue(this, val, DBExpr.CTX_DEFAULT, ""));
- part = StringUtils.replaceAll(part, "{1}", getObjectValue(this, val, DBExpr.CTX_DEFAULT, ""));
-
- inner.append(driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE_SEP));
- inner.append(part);
- }
- // Generate other
- if (otherwise != null)
- { // Else
- String other = driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE_ELSE);
-
- // other = other.replaceAll("\\{0\\}", getObjectValue(this, otherwise, DBExpr.CTX_DEFAULT, ""));
- other = StringUtils.replaceAll(other, "{0}", getObjectValue(this, otherwise, DBExpr.CTX_DEFAULT, ""));
-
- inner.append(driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE_SEP));
- inner.append(other);
- }
- DBValueExpr param = new DBValueExpr(getDatabase(), inner, DataType.UNKNOWN);
- return getExprFromPhrase(DBDatabaseDriver.SQL_FUNC_DECODE, new Object[] { param }, null, false);
+ DataType dataType = DataType.UNKNOWN;
+ return new DBDecodeExpr(this, valueMap, otherwise, dataType);
}
public final DBColumnExpr decode(Object key1, Object value1, Object otherwise)
@@ -1084,8 +1032,7 @@
*/
public DBColumnExpr convertTo(DataType dataType, Object format)
{
- DBDatabaseDriver driver = getDatabase().getDriver();
- return new DBFuncExpr(this, driver.getConvertPhrase(dataType, getDataType(), format), getUpdateColumn(), false, dataType);
+ return new DBConvertExpr(this, dataType, format);
}
/**
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAbstractFuncExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAbstractFuncExpr.java?rev=691366&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAbstractFuncExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAbstractFuncExpr.java Tue Sep 2 13:18:55 2008
@@ -0,0 +1,202 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.empire.db.expr.column;
+
+// Java
+import java.util.Set;
+
+import org.apache.empire.commons.StringUtils;
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBDatabaseDriver;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+
+/**
+ * This implements some basic functionality for SQL functions based on a column expression
+ */
+public abstract class DBAbstractFuncExpr extends DBColumnExpr
+{
+ protected final DBColumnExpr expr;
+ protected final DBColumn updateColumn; // optional
+ protected final boolean isAggregate;
+ protected final DataType dataType;
+
+ /**
+ * Constructs a new DBFuncExpr object set the specified parameters to this object.
+ * Do not use directly - use any of the DBColumnExpr.??? factory functions instead!
+ *
+ * The sql function string is built from a string template.
+ * The template string must contain a ? which is a placeholder for the column expression.
+ *
+ * @param expr the DBColumnExpr object
+ * @param template specifies a template for the expression. The template must contain a ? placeholder for the column expression
+ * @param updateColumn optional update column if any. This parameter may be null
+ * @param isAggregate indicates whether the function is an aggregate function (sum, min, max, avg, ...)
+ * @param dataType indicates the data type of the function result
+ */
+ public DBAbstractFuncExpr(DBColumnExpr expr, DBColumn updateColumn, boolean isAggregate, DataType dataType)
+ {
+ this.expr = expr;
+ this.updateColumn = updateColumn;
+ this.isAggregate = isAggregate;
+ this.dataType = dataType;
+ }
+
+ /**
+ * returns the name of the function
+ * @return
+ */
+ protected abstract String getFunctionName();
+
+ /**
+ * returns the Database driver or null if the Expression is not attached to an open database<BR/>
+ * This function is intended for convenience only.
+ */
+ protected final DBDatabaseDriver getDatabaseDriver()
+ {
+ DBDatabase db = getDatabase();
+ return (db!=null) ? db.getDriver() : null;
+ }
+
+ /**
+ * Returns the current DBDatabase object.
+ *
+ * @return the current DBDatabase object
+ */
+ @Override
+ public DBDatabase getDatabase()
+ {
+ return expr.getDatabase();
+ }
+
+ /**
+ * Returns the data type of the DBColumnExpr object.
+ *
+ * @return the data type
+ */
+ @Override
+ public DataType getDataType()
+ {
+ return this.dataType;
+ }
+
+ /**
+ * Returns the column name.
+ *
+ * @return the column name
+ */
+ @Override
+ public String getName()
+ {
+ return expr.getName();
+ }
+
+ /**
+ * Returns the DBColunm object.
+ *
+ * @return the DBColunm object
+ */
+ @Override
+ public DBColumn getUpdateColumn()
+ {
+ return updateColumn;
+ }
+
+ /**
+ * Returns whether the function is an aggegation function<br>
+ * that combines multiple rows to one result row.
+ *
+ * @return true if the function is an aggregate or false otherwise
+ */
+ @Override
+ public boolean isAggregate()
+ {
+ return isAggregate || expr.isAggregate();
+ }
+
+ /**
+ * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
+ */
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ expr.addReferencedColumns(list);
+ }
+
+ /**
+ * Creates the SQL-Command adds a function to the SQL-Command.
+ *
+ * @param buf the SQL-Command
+ * @param context the current SQL-Command context
+ */
+ public final void addSQL(StringBuilder sql, String template, Object[] params, long context)
+ {
+ // Get Template
+ if (params != null)
+ { // Replace Params
+ for (int i = 0; i < params.length; i++)
+ { // String test =(params[i] != null) ? params[i].toString() : "";
+ String value = getObjectValue(expr, params[i], CTX_DEFAULT, ",");
+ // template = template.replaceAll("\\{" + String.valueOf(i) + "\\}", value);
+ template = StringUtils.replaceAll(template, "{"+ String.valueOf(i) + "}", value);
+ }
+ }
+ // Get Prefix and Postfix
+ String prefix = template;
+ String postfix = "";
+ int sep = template.indexOf("?");
+ if (sep >= 0)
+ {
+ prefix = template.substring(0, sep);
+ postfix = template.substring(sep + 1);
+ }
+ // append
+ sql.append(prefix);
+ expr.addSQL(sql, context & ~CTX_ALIAS );
+ sql.append(postfix);
+ }
+
+ @Override
+ public Element addXml(Element parent, long flags)
+ {
+ Element elem;
+ if (updateColumn!=null)
+ { // Update Column
+ elem = updateColumn.addXml(parent, flags);
+ }
+ else
+ { // Add a column expression for this function
+ elem = XMLUtil.addElement(parent, "column");
+ elem.setAttribute("name", getName());
+ // Add Other Attributes
+ if (attributes!=null)
+ attributes.addXml(elem, flags);
+ // add All Options
+ if (options!=null)
+ options.addXml(elem, flags);
+ }
+ // Done
+ elem.setAttribute("function", getFunctionName());
+ return elem;
+ }
+}
\ No newline at end of file
Propchange: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBAbstractFuncExpr.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java?rev=691366&r1=691365&r2=691366&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java (original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java Tue Sep 2 13:18:55 2008
@@ -29,7 +29,7 @@
import org.w3c.dom.Element;
/**
- * This class is used to add the "case when ... then ... else ... end" statement to the SQL-Command.
+ * This class is used to add the "case when ?=A then X else Y end" statement to the SQL-Command.
* <P>
* There is no need to explicitly create instances of this class.<BR>
* Instead use {@link DBColumnExpr#when() }
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConvertExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConvertExpr.java?rev=691366&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConvertExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConvertExpr.java Tue Sep 2 13:18:55 2008
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.empire.db.expr.column;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabaseDriver;
+
+/**
+ * This class is used to convert a value to a different data type.
+ * This function uses the DBDatabaseDriver.getConvertPhrase function to obtain a conversion template.
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBColumnExpr#convertTo() }
+ * <P>
+ * @author doebele
+ */
+public class DBConvertExpr extends DBAbstractFuncExpr
+{
+ private final Object format;
+
+ /**
+ * Constructs a DBDecodeExpr
+ * @param compExpr the condition to be evaluated
+ * @param expr the expression returned if the condition is true
+ * @param elseExpr the expression returned if the condition is false (may be null)
+ */
+ public DBConvertExpr(DBColumnExpr expr, DataType dataType, Object format)
+ {
+ super(expr, expr.getUpdateColumn(), false, dataType);
+ // Save Info
+ this.format = format;
+ }
+
+ @Override
+ protected String getFunctionName()
+ {
+ return "convert";
+ }
+
+ @Override
+ public void addSQL(StringBuilder sql, long context)
+ {
+ DBDatabaseDriver driver = getDatabaseDriver();
+ // Set Params
+ String template = driver.getConvertPhrase(dataType, expr.getDataType(), format);
+ super.addSQL(sql, template, null, context);
+ }
+
+}
Propchange: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBConvertExpr.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBDecodeExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBDecodeExpr.java?rev=691366&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBDecodeExpr.java (added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBDecodeExpr.java Tue Sep 2 13:18:55 2008
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.empire.db.expr.column;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.empire.commons.StringUtils;
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBColumnExpr;
+import org.apache.empire.db.DBDatabaseDriver;
+import org.apache.empire.db.DBExpr;
+
+/**
+ * This class is used to decode a set of keys to the corresponding target values.
+ * For most drivers this will be performed by the "case ? when A then X else Y end" statement.
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBColumnExpr#when() }
+ * <P>
+ * @author doebele
+ */
+public class DBDecodeExpr extends DBAbstractFuncExpr
+{
+ private final Map<?,?> valueMap;
+ private final Object elseExpr;
+
+ /**
+ * Constructs a DBDecodeExpr
+ * @param compExpr the condition to be evaluated
+ * @param expr the expression returned if the condition is true
+ * @param elseExpr the expression returned if the condition is false (may be null)
+ */
+ public DBDecodeExpr(DBColumnExpr expr, Map<?,?> valueMap, Object elseExpr, DataType dataType)
+ {
+ super(expr, expr.getUpdateColumn(), false, dataType);
+ // Save Info
+ this.valueMap = valueMap;
+ this.elseExpr = elseExpr;
+ }
+
+ @Override
+ protected String getFunctionName()
+ {
+ return "decode";
+ }
+
+ @Override
+ public void addReferencedColumns(Set<DBColumn> list)
+ {
+ expr.addReferencedColumns(list);
+ for (Map.Entry<?,?> e : valueMap.entrySet())
+ { // Check Key of Value for Expressions
+ if (e.getKey() instanceof DBExpr)
+ ((DBExpr)e.getKey()).addReferencedColumns(list);
+ if (e.getValue() instanceof DBExpr)
+ ((DBExpr)e.getValue()).addReferencedColumns(list);
+ }
+ if (elseExpr instanceof DBExpr)
+ ((DBExpr)elseExpr).addReferencedColumns(list);
+ }
+
+ @Override
+ public void addSQL(StringBuilder sql, long context)
+ {
+ DBDatabaseDriver driver = getDatabase().getDriver();
+ StringBuilder inner = new StringBuilder();
+ // Generate parts
+ for (Iterator<?> i = valueMap.keySet().iterator(); i.hasNext();)
+ {
+ Object key = i.next();
+ Object val = valueMap.get(key);
+
+ String part = driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE_PART);
+ part = StringUtils.replaceAll(part, "{0}", getObjectValue(expr, key, DBExpr.CTX_DEFAULT, ""));
+ part = StringUtils.replaceAll(part, "{1}", getObjectValue(expr, val, DBExpr.CTX_DEFAULT, ""));
+
+ inner.append(driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE_SEP));
+ inner.append(part);
+ }
+ // Generate other
+ if (elseExpr != null)
+ { // Else
+ String other = driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE_ELSE);
+ other = StringUtils.replaceAll(other, "{0}", getObjectValue(expr, elseExpr, DBExpr.CTX_DEFAULT, ""));
+
+ inner.append(driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE_SEP));
+ inner.append(other);
+ }
+ DBValueExpr param = new DBValueExpr(getDatabase(), inner, DataType.UNKNOWN);
+ // Set Params
+ String template = driver.getSQLPhrase(DBDatabaseDriver.SQL_FUNC_DECODE);
+ super.addSQL(sql, template, new Object[] { param }, context);
+ }
+
+}
Propchange: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBDecodeExpr.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java?rev=691366&r1=691365&r2=691366&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java (original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBFuncExpr.java Tue Sep 2 13:18:55 2008
@@ -19,14 +19,9 @@
package org.apache.empire.db.expr.column;
// Java
-import java.util.Set;
-
import org.apache.empire.data.DataType;
import org.apache.empire.db.DBColumn;
import org.apache.empire.db.DBColumnExpr;
-import org.apache.empire.db.DBDatabase;
-import org.apache.empire.xml.XMLUtil;
-import org.w3c.dom.Element;
/**
@@ -41,164 +36,77 @@
* <P>
*
*/
-public class DBFuncExpr extends DBColumnExpr
+public class DBFuncExpr extends DBAbstractFuncExpr
{
- protected final DBColumnExpr expr;
- protected final DBColumn updateColumn; // optional
-
- protected final String prefix;
- protected final String postfix;
- protected final boolean isAggregate;
- protected final DataType dataType;
+ protected final int phrase;
+ protected final Object[] params;
+ protected String template;
/**
* Constructs a new DBFuncExpr object set the specified parameters to this object.
* Do not use directly - use any of the DBColumnExpr.??? factory functions instead!
*
* The sql function string is built from a string template.
- * The template string must contain a ? which is a placeholder for the column expression.
+ * The template string is identified by the phrase param and obtained from the driver.
*
* @param expr the DBColumnExpr object
- * @param template specifies a template for the expression. The template must contain a ? placeholder for the column expression
+ * @param phrase the SQL-phrase
+ * @param params an array of params which will be replaced in the template
* @param updateColumn optional update column if any. This parameter may be null
* @param isAggregate indicates whether the function is an aggregate function (sum, min, max, avg, ...)
* @param dataType indicates the data type of the function result
*/
- public DBFuncExpr(DBColumnExpr expr, String template, DBColumn updateColumn, boolean isAggregate, DataType dataType)
+ public DBFuncExpr(DBColumnExpr expr, int phrase, Object[] params, DBColumn updateColumn, boolean isAggregate, DataType dataType)
{
- this.expr = expr;
- this.dataType = dataType;
- this.isAggregate = isAggregate;
- this.updateColumn = updateColumn;
-
- int sep = template.indexOf("?");
- if (sep >= 0)
- {
- prefix = template.substring(0, sep);
- postfix = template.substring(sep + 1);
- }
- else
- {
- prefix = template;
- postfix = "";
- }
+ super(expr, updateColumn, isAggregate, dataType);
+ // Set Phrase and Params
+ this.phrase = phrase;
+ this.params = params;
+ this.template = null;
}
/**
* Constructs a new DBFuncExpr object set the specified parameters to this object.
- * Do not use directly - use any of the DBColumnExpr.??? factory functions instead!
*
- * The function sql string is built from a template string.
+ * The sql function string is built from a string template.
* The template string must contain a ? which is a placeholder for the column expression.
*
* @param expr the DBColumnExpr object
* @param template specifies a template for the expression. The template must contain a ? placeholder for the column expression
+ * @param params an array of params which will be replaced in the template
* @param updateColumn optional update column if any. This parameter may be null
* @param isAggregate indicates whether the function is an aggregate function (sum, min, max, avg, ...)
+ * @param dataType indicates the data type of the function result
*/
- public DBFuncExpr(DBColumnExpr expr, String template, DBColumn updateColumn, boolean isAggregate)
- {
- this(expr, template, updateColumn, isAggregate, expr.getDataType());
- }
-
- /**
- * Returns the current DBDatabase object.
- *
- * @return the current DBDatabase object
- */
- @Override
- public DBDatabase getDatabase()
+ public DBFuncExpr(DBColumnExpr expr, String template, Object[] params, DBColumn updateColumn, boolean isAggregate, DataType dataType)
{
- return expr.getDatabase();
- }
-
- /**
- * Returns the data type of the DBColumnExpr object.
- *
- * @return the data type
- */
- @Override
- public DataType getDataType()
- {
- return this.dataType;
- }
-
- /**
- * Returns the column name.
- *
- * @return the column name
- */
- @Override
- public String getName()
- {
- return expr.getName();
- }
-
- @Override
- public Element addXml(Element parent, long flags)
- {
- Element elem;
- if (updateColumn!=null)
- { // Update Column
- elem = updateColumn.addXml(parent, flags);
- }
- else
- { // Add a column expression for this function
- elem = XMLUtil.addElement(parent, "column");
- elem.setAttribute("name", getName());
- // Add Other Attributes
- if (attributes!=null)
- attributes.addXml(elem, flags);
- // add All Options
- if (options!=null)
- options.addXml(elem, flags);
- }
- // Done
- elem.setAttribute("function", getFunctionName());
- return elem;
+ super(expr, updateColumn, isAggregate, dataType);
+ // Set Phrase and Params
+ this.phrase = 0;
+ this.params = params;
+ this.template = template;
}
- private String getFunctionName()
- {
- String s = prefix.trim();
- int i=0;
- for (; i<s.length(); i++)
- if (s.charAt(i)<'A')
- break;
- // return name
- return (i>0) ? s.substring(0,i) : postfix.trim();
- }
-
- /**
- * Returns the DBColunm object.
- *
- * @return the DBColunm object
- */
- @Override
- public DBColumn getUpdateColumn()
- {
- return updateColumn;
- }
-
- /**
- * Returns whether the function is an aggegation function<br>
- * that combines multiple rows to one result row.
- *
- * @return true if the function is an aggregate or false otherwise
- */
@Override
- public boolean isAggregate()
+ protected String getFunctionName()
{
- return isAggregate || expr.isAggregate();
- }
-
- /**
- * @see org.apache.empire.db.DBExpr#addReferencedColumns(Set)
- */
- @Override
- public void addReferencedColumns(Set<DBColumn> list)
- {
- expr.addReferencedColumns(list);
+ // Get the template
+ if (template==null && getDatabaseDriver()!=null)
+ template = getDatabaseDriver().getSQLPhrase(phrase);
+ // Get the first word
+ if (template!=null)
+ {
+ String s = template.trim();
+ int i=0;
+ for (; i<s.length(); i++)
+ if (s.charAt(i)<'A')
+ break;
+ // return name
+ if (i>0)
+ return s.substring(0,i);
+ }
+ // default
+ return "func_" + String.valueOf(phrase);
}
/**
@@ -208,10 +116,13 @@
* @param context the current SQL-Command context
*/
@Override
- public void addSQL(StringBuilder buf, long context)
- { // Expression
- buf.append(prefix);
- expr.addSQL(buf, context & ~CTX_ALIAS );
- buf.append(postfix);
+ public void addSQL(StringBuilder sql, long context)
+ {
+ // Get the template
+ if (template==null)
+ template = getDatabaseDriver().getSQLPhrase(phrase);
+ // Add SQL
+ super.addSQL(sql, template, params, context);
}
+
}
\ No newline at end of file