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 fr...@apache.org on 2009/02/26 22:03:53 UTC

svn commit: r748300 - in /incubator/empire-db/trunk/empire-db/src: main/java/org/apache/empire/db/DBCommand.java test/java/org/apache/empire/db/ test/java/org/apache/empire/db/DBCommandTest.java

Author: francisdb
Date: Thu Feb 26 21:03:52 2009
New Revision: 748300

URL: http://svn.apache.org/viewvc?rev=748300&view=rev
Log:
less select and group by functions + unit test for that part of DBCommand

Added:
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java
Modified:
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java?rev=748300&r1=748299&r2=748300&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java Thu Feb 26 21:03:52 2009
@@ -199,101 +199,29 @@
     }
 
     /**
-     * This helper function adds two DBColumnExpr objects
-     * to the Vector: 'select'
+     * Adds a list of columns to the select phrase of an sql statement.
      * 
-     * @param expr1 the first DBColumnExpr to select
-     * @param expr2 the second DBColumnExpr to select
+     * @param exprs an vararg of DBColumnExpr's to select
      */
-    public void select(DBColumnExpr expr1, DBColumnExpr expr2)
+    public void select(DBColumnExpr... exprs)
     {
-        select(expr1);
-        select(expr2);
-    }
-
-    /**
-     * This helper function adds three DBColumnExpr objects to the Vector: 'select'.
-     * 
-     * @param expr1 the first DBColumnExpr to select
-     * @param expr2 the second DBColumnExpr to select
-     * @param expr3 the third DBColumnExpr to select
-     */
-    public void select(DBColumnExpr expr1, DBColumnExpr expr2, DBColumnExpr expr3)
-    {
-        select(expr1);
-        select(expr2);
-        select(expr3);
-    }
-
-    /**
-     * This helper function adds four DBColumnExpr objects to the Vector: 'select'.
-     * 
-     * @param expr1 the first DBColumnExpr to select
-     * @param expr2 the second DBColumnExpr to select
-     * @param expr3 the third DBColumnExpr to select
-     * @param expr4 the fourth DBColumnExpr to select
-     */
-    public void select(DBColumnExpr expr1, DBColumnExpr expr2, DBColumnExpr expr3, DBColumnExpr expr4)
-    {
-        select(expr1);
-        select(expr2);
-        select(expr3);
-        select(expr4);
-    }
-
-    /**
-     * This helper function adds five DBColumnExpr objects
-     * to the Vector: 'select'.
-     * 
-     * @param expr1 the first DBColumnExpr to select
-     * @param expr2 the second DBColumnExpr to select
-     * @param expr3 the third DBColumnExpr to select
-     * @param expr4 the fourth DBColumnExpr to select
-     * @param expr5 the fifth DBColumnExpr to select
-     */
-    public void select(DBColumnExpr expr1, DBColumnExpr expr2, DBColumnExpr expr3, DBColumnExpr expr4, DBColumnExpr expr5)
-    {
-        select(expr1);
-        select(expr2);
-        select(expr3);
-        select(expr4);
-        select(expr5);
-    }
-
-    /**
-     * This helper function adds an array of DBColumnExpr
-     * objects to list of select-columns.
-     * 
-     * @param exprList an array of DBColumnExpr's to select
-     */
-    public void select(DBColumnExpr[] exprList)
-    {
-        for (int i=0; i<exprList.length; i++)
+        for (DBColumnExpr expr : exprs)
         {
-            select(exprList[i]);
+            select(expr);
         }
     }
 
     /**
-     * Adds a list of column expression to the select clause
+     * Adds a list of columns to the select phrase of an sql statement.
      * 
      * @param columns the column expressions to add
      */
-    public void select(Collection<DBColumnExpr> columns)
+    public void select(Collection<? extends DBColumnExpr> columns)
     {
         for (DBColumnExpr expr : columns)
+        {
             select(expr);
-    }
-
-    /**
-     * Adds a list of column expression to the select clause
-     * 
-     * @param columns the column expressions to add
-     */
-    public void select(List<DBColumn> columns)
-    {
-        for (int i = 0; i < columns.size(); i++)
-            select(columns.get(i));
+        }
     }
     
     private boolean useCmdParam(DBColumn col)
@@ -602,24 +530,6 @@
     }
 
     /**
-     * Adds a list columns to the group by phrase of an sql statement.
-     * 
-     * @param exprList array of columns by which to group the rows
-     */
-    public void groupBy(DBColumnExpr[] exprList)
-    {
-        if (groupBy == null)
-            groupBy = new ArrayList<DBColumnExpr>();
-        // group by
-        for (int i=0; i<exprList.length; i++)
-        {
-        	DBColumnExpr expr = exprList[i];
-        	if (expr.isAggregate()==false && groupBy.contains(expr)==false)
-                groupBy.add(expr);
-        }
-    }
-
-    /**
      * Adds a column to the group by phrase of an sql statement.
      * 
      * @param expr the DBCompareExpr object
@@ -636,24 +546,18 @@
     }
 
     /**
-     * Adds two columns to the group by phrase of an sql statement.
-     */
-    // groupBy
-    public void groupBy(DBColumnExpr expr1, DBColumnExpr expr2)
-    {
-        groupBy(expr1);
-        groupBy(expr2);
-    }
-
-    /**
-     * Adds three columns to the group by phrase of an sql statement.
+     * Adds a list of columns to the group by phrase of an sql statement.
+     * 
+     * @param exprs vararg of columns by which to group the rows
      */
-    // groupBy
-    public void groupBy(DBColumnExpr expr1, DBColumnExpr expr2, DBColumnExpr expr3)
+    public void groupBy(DBColumnExpr...exprs)
     {
-        groupBy(expr1);
-        groupBy(expr2);
-        groupBy(expr3);
+        if (groupBy == null)
+            groupBy = new ArrayList<DBColumnExpr>();
+        for(DBColumnExpr expr:exprs){
+            if (expr.isAggregate()==false && groupBy.contains(expr)==false)
+                groupBy.add(expr);
+        }
     }
 
     /**
@@ -686,9 +590,9 @@
     }
     
     /**
-     * Returns a array of all DBColumnExpr object of the Vector: 'select'.
+     * Returns a array of all select DBColumnExpr for this command 
      * 
-     * @return a array of all DBColumnExpr object of the Vector: 'select'
+     * @return a array of all DBColumnExpr objects or <code>null</code> if there are no selects
      */
     @Override
     public DBColumnExpr[] getSelectExprList()

Added: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java?rev=748300&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java (added)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/DBCommandTest.java Thu Feb 26 21:03:52 2009
@@ -0,0 +1,158 @@
+/*
+ * 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;
+
+import static org.junit.Assert.*;
+
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.empire.data.DataType;
+import org.junit.Test;
+import org.w3c.dom.Element;
+
+/**
+ * @author francisdb
+ *
+ */
+public class DBCommandTest
+{
+
+	@Test
+	public void testDBCommand()
+	{
+		MockDB mock = new MockDB();
+		mock.open(new MockDriver(), null);
+        DBColumn col = new MockDBColumn(null, "test");
+        DBColumn col2 = new MockDBColumn(null, "test2");
+        DBColumn col3 = new MockDBColumn(null, "test3");
+		
+		List<DBColumn> list = new ArrayList<DBColumn>();
+		DBCommand command = mock.createCommand();
+	    command.select(list);
+	    assertNull(command.getSelectExprList());
+	    
+		List<DBColumnExpr> list2 = new ArrayList<DBColumnExpr>();
+		list2.add(col);
+		list2.add(col2);
+		DBCommand command2 = mock.createCommand();
+		command2.select(list2);
+		assertEquals(2, command2.getSelectExprList().length);
+		
+        DBCommand command3 = mock.createCommand();
+        command3.select(col, col2, col3);
+        command3.groupBy(col, col2, col2);
+        assertEquals(3, command3.getSelectExprList().length);
+        assertEquals(2, command3.groupBy.size());
+	}
+	
+	private class MockDB extends DBDatabase{
+		
+	}
+	
+	private class MockDriver extends DBDatabaseDriver{
+
+        @Override
+        public DBCommand createCommand(DBDatabase db)
+        {
+            return new MockCommand(db);
+        }
+
+        @Override
+        public String getConvertPhrase(DataType destType, DataType srcType, Object format)
+        {
+            return null;
+        }
+
+        @Override
+        public Object getNextSequenceValue(DBDatabase db, String SeqName, int minValue, Connection conn)
+        {
+            return null;
+        }
+
+        @Override
+        public String getSQLPhrase(int phrase)
+        {
+            return null;
+        }
+
+        @Override
+        public boolean isSupported(DBDriverFeature type)
+        {
+            return false;
+        }
+	    
+	}
+	
+	private class MockCommand extends DBCommand{
+
+        protected MockCommand(DBDatabase db)
+        {
+            super(db);
+        }
+	    
+	}
+	
+	private class MockDBColumn extends DBColumn{
+
+	    public MockDBColumn(DBRowSet rowSet, String name)
+        {
+            super(rowSet, name);
+        }
+	    
+        @Override
+        public Element addXml(Element parent, long flags)
+        {
+            return null;
+        }
+
+        @Override
+        public boolean checkValue(Object value)
+        {
+            return false;
+        }
+
+        @Override
+        public double getSize()
+        {
+            return 0;
+        }
+
+        @Override
+        public boolean isReadOnly()
+        {
+            return false;
+        }
+
+        @Override
+        public boolean isRequired()
+        {
+            return false;
+        }
+
+        @Override
+        public DataType getDataType()
+        {
+            return null;
+        }
+	    
+	}
+
+}