empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r691196 - in /incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire: commons/ db/ db/expr/column/
Date Tue, 02 Sep 2008 10:56:23 GMT
Author: doebele
Date: Tue Sep  2 03:56:23 2008
New Revision: 691196

URL: http://svn.apache.org/viewvc?rev=691196&view=rev
Log:
EMPIREDB-7

Added:
    incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java
  (with props)
Modified:
    incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/ObjectUtils.java
    incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/Options.java
    incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/StringUtils.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/DBExpr.java
    incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBReader.java
    incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBRecord.java

Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/ObjectUtils.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/ObjectUtils.java?rev=691196&r1=691195&r2=691196&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/ObjectUtils.java
(original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/ObjectUtils.java
Tue Sep  2 03:56:23 2008
@@ -407,7 +407,6 @@
      * Generic conversion function that will convert a list to another list type.
      * @return the new list type
      */
-    @SuppressWarnings("unchecked")
     public static <T> List<T> convert(Class<T> t, Collection<? extends
T> source)
         throws ClassCastException
     {

Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/Options.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/Options.java?rev=691196&r1=691195&r2=691196&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/Options.java (original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/Options.java Tue
Sep  2 03:56:23 2008
@@ -36,7 +36,6 @@
  * where the entry value is used as the key for the set and thus must be unique.<BR>
  * <P> 
  */
-@SuppressWarnings("serial")
 public class Options extends AbstractSet<OptionEntry>
 {
     public enum InsertPos
@@ -68,7 +67,7 @@
     {
         // Find an Entry
         if (value instanceof Entry)
-            value = ((Entry) value).getKey();
+            value = ((Entry<?,?>) value).getKey();
         if (value instanceof OptionEntry)
             value = ((OptionEntry) value).getValue();
         // Find it now

Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/StringUtils.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/StringUtils.java?rev=691196&r1=691195&r2=691196&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/StringUtils.java
(original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/commons/StringUtils.java
Tue Sep  2 03:56:23 2008
@@ -215,7 +215,7 @@
         // Iterator
         StringBuilder buf = new StringBuilder();
         boolean addSep = false;
-        Iterator i = c.iterator();
+        Iterator<?> i = c.iterator();
         while (i.hasNext())
         {
             if (addSep)

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=691196&r1=691195&r2=691196&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 03:56:23 2008
@@ -32,10 +32,12 @@
 import org.apache.empire.data.DataType;
 import org.apache.empire.db.expr.column.DBAliasExpr;
 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.DBFuncExpr;
 import org.apache.empire.db.expr.column.DBValueExpr;
 import org.apache.empire.db.expr.compare.DBCompareColExpr;
+import org.apache.empire.db.expr.compare.DBCompareExpr;
 import org.w3c.dom.Element;
 
 
@@ -938,7 +940,8 @@
     }
 
     /**
-     * Creates and returns a sql-expression for the replace(...) function.
+     * Creates and returns a sql-expression that compares the current column expression with

+     * a list of values and returns the corresponding alternative value.<BR>
      * 
      * @param list 
      * @param otherwise
@@ -1018,6 +1021,30 @@
     {
         return decode(options, null);
     }
+    
+    /**
+     * Creates and returns a sql-expression for the SQL case-phrase.<BR/>
+     * The result will be in the form:<BR/> 
+     * "case when [compExpr] then [this] else [otherwise] end" 
+     * 
+     * @param compExpr the condition for which the current column expression is returned
+     * @param otherwise the value that is returned if the condition is false
+     * @return a DBCaseExpr representing the SQL case phrase.
+     */
+    public final DBCaseExpr when(DBCompareExpr compExpr, Object otherwise)
+    {
+        DBColumnExpr elseExpr = null;
+        if (otherwise instanceof DBColumnExpr)
+        {   // A column Expression
+            elseExpr = (DBColumnExpr)otherwise; 
+        }
+        else if (otherwise != null)
+        {   // A constant value   
+            elseExpr = new DBValueExpr(getDatabase(), otherwise, getDataType());
+        }
+        // Create DBCaseExpr
+        return new DBCaseExpr(compExpr, this, elseExpr);
+    }
 
     // ----------------------------------------------------------
     // --------------------- Conversion -------------------------

Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBExpr.java?rev=691196&r1=691195&r2=691196&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBExpr.java (original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBExpr.java Tue Sep
 2 03:56:23 2008
@@ -77,16 +77,16 @@
     {
         // it's an Object
         if (value instanceof DBExpr)
-        { // Wert ist Expression
+        { // it's an expression
             StringBuilder buf = new StringBuilder();
             ((DBExpr) value).addSQL(buf, context);
             return buf.toString();
         } 
         else if (value instanceof Collection)
         {
-        	value = ((Collection)value).toArray();
+        	value = ((Collection<?>)value).toArray();
         }
-        // Check wether it is an array
+        // Check whether it is an array
         if (value!=null && value.getClass().isArray())
         {
             StringBuilder buf = new StringBuilder();
@@ -119,6 +119,7 @@
      * @param type the data type
      * @return return the java class used for storing values of this dataType 
      */
+    @SuppressWarnings("unchecked")
     public static final Class getValueClass(DataType type)
     {
         switch(type)

Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBReader.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBReader.java?rev=691196&r1=691195&r2=691196&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBReader.java (original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBReader.java Tue Sep
 2 03:56:23 2008
@@ -846,6 +846,7 @@
     /**
      * copied from org.apache.commons.beanutils.ConstructorUtils since it's private there
      */
+    @SuppressWarnings("unchecked")
     private static Constructor findMatchingAccessibleConstructor(Class clazz, Class[] parameterTypes)
     {
         // See if we can find the method directly

Modified: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBRecord.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBRecord.java?rev=691196&r1=691195&r2=691196&view=diff
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBRecord.java (original)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/DBRecord.java Tue Sep
 2 03:56:23 2008
@@ -654,8 +654,8 @@
     public final boolean read(DBRowSet table, Object id, Connection conn)
     {
         if (id instanceof Collection)
-        {   // If it's a collectin then convert it to an array
-            return read(table, ((Collection)id).toArray(), conn);
+        {   // If it's a collection then convert it to an array
+            return read(table, ((Collection<?>)id).toArray(), conn);
         }
         // Simple One-Column key
         return read(table, new Object[] { id }, conn);

Added: 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=691196&view=auto
==============================================================================
--- incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java
(added)
+++ incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java
Tue Sep  2 03:56:23 2008
@@ -0,0 +1,133 @@
+/*
+ * 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.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.db.expr.compare.DBCompareExpr;
+import org.apache.empire.xml.XMLUtil;
+import org.w3c.dom.Element;
+
+/**
+ * This class is used to add the "case when ... then ... else ... end" statement to the SQL-Command.
+ * <P>
+ * There is no need to explicitly create instances of this class.<BR>
+ * Instead use {@link DBColumnExpr#when() }
+ * <P>
+ * @author doebele
+ */
+public class DBCaseExpr extends DBColumnExpr
+{
+    private final DBCompareExpr compExpr;
+    private final DBColumnExpr  trueExpr;
+    private final DBColumnExpr  elseExpr;
+    
+    /**
+     * Constructs a DBCaseExpr
+     * @param compExpr the condition to be evaluated
+     * @param trueExpr the expression returned if the condition is true
+     * @param elseExpr the expression returned if the condition is false (may be null)
+     */
+    public DBCaseExpr(DBCompareExpr compExpr, DBColumnExpr trueExpr, DBColumnExpr elseExpr)
+    {
+        this.compExpr = compExpr;
+        this.trueExpr = trueExpr;
+        this.elseExpr = elseExpr; 
+    }
+
+    @Override
+    public DBDatabase getDatabase()
+    {
+        return trueExpr.getDatabase();
+    }
+
+    @Override
+    public DataType getDataType()
+    {
+        return trueExpr.getDataType();
+    }
+
+    @Override
+    public String getName()
+    {
+        return trueExpr.getName();
+    }
+
+    @Override
+    public DBColumn getUpdateColumn()
+    {
+        return trueExpr.getUpdateColumn();
+    }
+
+    @Override
+    public boolean isAggregate()
+    {
+        return trueExpr.isAggregate();
+    }
+
+    @Override
+    public void addReferencedColumns(Set<DBColumn> list)
+    {
+        trueExpr.addReferencedColumns(list);
+        compExpr.addReferencedColumns(list);
+        if (elseExpr!=null)
+            elseExpr.addReferencedColumns(list);
+    }
+
+    @Override
+    public void addSQL(StringBuilder sql, long context)
+    {
+        // context = context & ~CTX_ALIAS;
+        sql.append("CASE WHEN ");
+        compExpr.addSQL(sql, context);
+        sql.append( " THEN ");
+        trueExpr.addSQL(sql, context);
+        sql.append(" ELSE ");
+        if (elseExpr!=null)
+        {   // Else
+            elseExpr.addSQL(sql, context);
+        }
+        else
+        {   // Append NULL
+            sql.append("NULL");
+        }
+        sql.append(" END");
+    }
+
+    @Override
+    public Element addXml(Element parent, long flags)
+    {
+        Element 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", "case");
+        return elem;
+    }
+
+}

Propchange: incubator/empire-db/trunk/core/Empire-db/src/org/apache/empire/db/expr/column/DBCaseExpr.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message