EMPIREDB-362 API cleanup and beautification
diff --git a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
index 68589ab..8e13af1 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
@@ -993,6 +993,33 @@
}
return strArray;
}
+
+ /**
+ * Checks whether a object implements the Unwrappable interface and is also a wrapper
+ * If the object does not Implement the Interface or is not a wrapper then false is returned
+ * @param object the object to check
+ * @return true if the object is a wrapper or false otherwise
+ */
+ public static boolean isWrapper(Object object)
+ {
+ return ((object instanceof Unwrappable<?>)) && ((Unwrappable<?>)object).isWrapper();
+ }
+
+ /**
+ * Unwraps an object implementing the Unwrappable interface
+ * If the object does not Implement the Interface or is not a wrapper then the object itself is returned
+ * @param object the object to unwrap
+ * @return the unwrapped object or the object itself
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T unwrap(T object)
+ {
+ if ((object instanceof Unwrappable<?>) && ((Unwrappable<?>)object).isWrapper())
+ { // recursive
+ return unwrap(((Unwrappable<T>)object).unwrap());
+ }
+ return object;
+ }
/**
* returns whether or not a array contains a certain item
@@ -1052,5 +1079,4 @@
{
return (indexOf(array, item)>=0);
}
-
}
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
index a8927db..71c6080 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
@@ -28,8 +28,8 @@
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.StringUtils;
-import org.apache.empire.commons.Unwrappable;
import org.apache.empire.data.DataType;
+import org.apache.empire.db.expr.column.DBAliasExpr;
import org.apache.empire.db.expr.compare.DBCompareAndOrExpr;
import org.apache.empire.db.expr.compare.DBCompareColExpr;
import org.apache.empire.db.expr.compare.DBCompareExpr;
@@ -255,9 +255,9 @@
{ // DBCompareNotExpr
removeCommandParams(((DBCompareNotExpr)cmpExpr).getExpr());
}
- else if ((cmpExpr instanceof Unwrappable<?>) && ((Unwrappable<?>)cmpExpr).isWrapper())
+ else if (ObjectUtils.isWrapper(cmpExpr))
{ // unwrap
- removeCommandParams((DBCompareExpr)((Unwrappable<?>)cmpExpr).unwrap());
+ removeCommandParams(ObjectUtils.unwrap(cmpExpr));
}
}
@@ -409,6 +409,28 @@
}
return this;
}
+
+ /**
+ * Makes sure all selected columns are identified by their proper names (qualified)
+ * @return itself (this)
+ */
+ public DBCommand qualifyAll()
+ {
+ if (select == null)
+ return this;
+ // check select expression
+ for (int i=0; i<select.size(); i++)
+ {
+ DBColumnExpr expr = select.get(i);
+ if (expr instanceof DBColumn)
+ continue; // No need to qualify
+ if (expr instanceof DBAliasExpr)
+ continue; // Already qualified
+ // qualify now
+ select.set(i, expr.qualified());
+ }
+ return this;
+ }
/**
* Returns an array of all select expressions
@@ -885,10 +907,6 @@
Set<DBColumn> columns = new HashSet<DBColumn>();
for (i = 0; where != null && i < where.size(); i++)
((DBExpr) where.get(i)).addReferencedColumns(columns);
- /*
- for (i = 0; groupBy != null && i < groupBy.size(); i++)
- ((DBExpr) groupBy.get(i)).addReferencedColumns(columns);
- */
for (i = 0; having != null && i < having.size(); i++)
((DBExpr) having.get(i)).addReferencedColumns(columns);
// now we have all columns
@@ -1039,6 +1057,17 @@
}
/**
+ * Checks whether the command has a constraint on a particular column expression
+ * @param col the column expression which to check
+ */
+ public boolean hasWhereConstraintOn(DBColumnExpr col)
+ {
+ if (where == null)
+ return false;
+ return hasConstraintOn(where, col);
+ }
+
+ /**
* Returns a copy of the defined joins.
*
* @return the list of joins
@@ -1117,6 +1146,17 @@
}
/**
+ * Checks whether the command has a constraint on a particular column expression
+ * @param col the column expression which to check
+ */
+ public boolean hasHavingConstraintOn(DBColumnExpr col)
+ {
+ if (where == null)
+ return false;
+ return hasConstraintOn(having, col);
+ }
+
+ /**
* Returns whether or not the command has group by set
*/
public boolean hasGroupBy()
@@ -1133,28 +1173,49 @@
{
return (this.groupBy!=null ? Collections.unmodifiableList(this.groupBy) : null);
}
-
+
/**
- * Adds a list of columns to the group by phrase of an sql statement.
+ * Adds a column expression to the Group By clause of an sql statement.
*
- * @param exprs vararg of columns by which to group the rows
+ * @param columnExpr the column expression
* @return itself (this)
*/
- public DBCommand groupBy(DBColumnExpr...exprs)
+ public DBCommand groupBy(DBColumnExpr columnExpr)
{
if (groupBy == null)
groupBy = new ArrayList<DBColumnExpr>();
// Add all
+ if (columnExpr.isAggregate())
+ return this;
+ // Unwrap DBAliasExpr only
+ if (columnExpr instanceof DBAliasExpr)
+ columnExpr = ((DBAliasExpr)columnExpr).unwrap();
+ // Already present?
+ if (groupBy.contains(columnExpr))
+ return this;
+ // add
+ groupBy.add(columnExpr);
+ // done
+ return this;
+ }
+
+ /**
+ * Adds a list of columns to the Group By clause of an sql statement.
+ *
+ * @param exprs vararg of columns by which to group the rows
+ * @return itself (this)
+ */
+ public final DBCommand groupBy(DBColumnExpr...exprs)
+ {
for(DBColumnExpr expr : exprs)
{
- if (expr.isAggregate()==false && groupBy.contains(expr)==false)
- groupBy.add(expr);
+ groupBy(expr);
}
return this;
}
/**
- * Adds a collection of columns to the group by phrase of an sql statement.
+ * Adds a collection of columns to the Group By clause of an sql statement.
*
* @param columns the column expressions to add
* @return itself (this)
@@ -1169,6 +1230,27 @@
}
/**
+ * Adds all select expressions which are not aggregates to the Group By clause
+ * @return itself (this)
+ */
+ public final DBCommand groupAll()
+ {
+ clearGroupBy();
+ // check select expression
+ if (select == null)
+ return this;
+ // make a group by array
+ for (DBColumnExpr expr : select)
+ {
+ if (expr.isAggregate())
+ continue; // ignore aggregates
+ // append
+ groupBy(expr);
+ }
+ return this;
+ }
+
+ /**
* Clears the select distinct option.
*/
public void clearSelectDistinct()
@@ -1185,7 +1267,7 @@
}
/**
- * Clears the list of set expressions.
+ * Clears the Set clause
*/
public void clearSet()
{
@@ -1202,7 +1284,7 @@
}
/**
- * Clears the list of join expressions.
+ * Clears the From / Join clause
*/
public void clearJoin()
{
@@ -1210,7 +1292,7 @@
}
/**
- * Clears the list of where constraints.
+ * Removes all constraints from the Where clause
*/
public void clearWhere()
{
@@ -1219,7 +1301,7 @@
}
/**
- * Clears the list of having constraints.
+ * Removes all constraints from the Having clause
*/
public void clearHaving()
{
@@ -1228,7 +1310,7 @@
}
/**
- * Clears the list of group by constraints.
+ * Clears the Group By clause
*/
public void clearGroupBy()
{
@@ -1376,6 +1458,30 @@
* @param list the 'where' or 'having' list
* @param col the column expression for which to remove the constraint
*/
+ protected boolean hasConstraintOn(List<DBCompareExpr> list, DBColumnExpr colExpr)
+ {
+ if (list == null)
+ return false;
+ for (DBCompareExpr cmp : list)
+ { // Check whether it is a compare column expr.
+ if (!(cmp instanceof DBCompareColExpr))
+ continue;
+ // Compare columns
+ DBColumnExpr cmpCol = ((DBCompareColExpr)cmp).getColumnExpr();
+ if (ObjectUtils.compareEqual(cmpCol, colExpr))
+ return true;
+ // Update column
+ if ((colExpr instanceof DBColumn) && !(cmpCol instanceof DBColumn) && colExpr.equals(colExpr.getUpdateColumn()))
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * removes a constraint on a particular column to the 'where' or 'having' collections
+ * @param list the 'where' or 'having' list
+ * @param col the column expression for which to remove the constraint
+ */
protected void removeConstraintOn(List<DBCompareExpr> list, DBColumnExpr colExpr)
{
if (list == null)
@@ -1560,7 +1666,12 @@
return buf.toString();
}
- @SuppressWarnings("unchecked")
+ /**
+ * Appends all nested DBCompareColExpr for a particular RowSet to a list
+ * @param table the rowset for which to collect the DBCompareColExpr
+ * @param expr a compare expression
+ * @param list
+ */
protected void appendCompareColExprs(DBRowSet table, DBCompareExpr expr, List<DBCompareColExpr> list)
{
if (expr instanceof DBCompareColExpr)
@@ -1578,9 +1689,9 @@
{ // DBCompareNotExpr
appendCompareColExprs(table, ((DBCompareNotExpr)expr).getExpr(), list);
}
- else if ((expr instanceof Unwrappable<?>) && ((Unwrappable<?>)expr).isWrapper())
+ else if (ObjectUtils.isWrapper(expr))
{ // unwrap
- appendCompareColExprs(table, ((Unwrappable<DBCompareExpr>)expr).unwrap(), list);
+ appendCompareColExprs(table, ObjectUtils.unwrap(expr), list);
}
}
@@ -1811,7 +1922,7 @@
protected void addGrouping(StringBuilder buf)
{
if (groupBy!=null && !groupBy.isEmpty())
- { // Having
+ { // Group by
buf.append("\r\nGROUP BY ");
addListExpr(buf, groupBy, CTX_DEFAULT, ", ");
}
diff --git a/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareAndOrExpr.java b/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareAndOrExpr.java
index e052054..937103a 100644
--- a/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareAndOrExpr.java
+++ b/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareAndOrExpr.java
@@ -20,7 +20,7 @@
import java.util.Set;
-import org.apache.empire.commons.Unwrappable;
+import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.db.DBColumn;
import org.apache.empire.db.DBCommand;
import org.apache.empire.db.DBDatabase;
@@ -165,13 +165,12 @@
*
* @return true it the constraints are mutually exclusive or false otherwise
*/
- @SuppressWarnings("unchecked")
@Override
public boolean isMutuallyExclusive(DBCompareExpr other)
{
- if ((other instanceof Unwrappable<?>) && ((Unwrappable<?>)other).isWrapper())
+ if (ObjectUtils.isWrapper(other))
{ // unwrap
- other = ((Unwrappable<DBCompareExpr>)other).unwrap();
+ other = ObjectUtils.unwrap(other);
}
if (other instanceof DBCompareAndOrExpr)
{ // check other
diff --git a/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java b/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java
index 39c5ccf..6d38d99 100644
--- a/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java
+++ b/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java
@@ -28,6 +28,7 @@
import org.apache.empire.db.DBCommand;
import org.apache.empire.db.DBDatabase;
import org.apache.empire.db.DBExpr;
+import org.apache.empire.db.expr.column.DBAliasExpr;
/**
@@ -59,6 +60,10 @@
*/
public DBCompareColExpr(DBColumnExpr expr, DBCmpType op, Object value)
{
+ // unwrap DBAliasExpr only
+ if (expr instanceof DBAliasExpr)
+ expr = ((DBAliasExpr)expr).unwrap();
+ // set
this.expr = expr;
this.cmpop = op;
this.value = value;
diff --git a/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareNotExpr.java b/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareNotExpr.java
index 04ddfed..f2fd63d 100644
--- a/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareNotExpr.java
+++ b/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareNotExpr.java
@@ -20,7 +20,7 @@
import java.util.Set;
-import org.apache.empire.commons.Unwrappable;
+import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.db.DBColumn;
import org.apache.empire.db.DBCommand;
import org.apache.empire.db.DBDatabase;
@@ -128,13 +128,12 @@
*
* @return true it the constraints are mutually exclusive or false otherwise
*/
- @SuppressWarnings("unchecked")
@Override
public boolean isMutuallyExclusive(DBCompareExpr other)
{
- if ((other instanceof Unwrappable<?>) && ((Unwrappable<?>)other).isWrapper())
+ if (ObjectUtils.isWrapper(expr))
{ // unwrap
- other = ((Unwrappable<DBCompareExpr>)other).unwrap();
+ other = ObjectUtils.unwrap(other);
}
if (other instanceof DBCompareNotExpr)
{ // compare
diff --git a/empire-db/src/main/java/org/apache/empire/dbms/postgresql/PostgresAtAt.java b/empire-db/src/main/java/org/apache/empire/dbms/postgresql/PostgresAtAt.java
index b61317f..abd5276 100644
--- a/empire-db/src/main/java/org/apache/empire/dbms/postgresql/PostgresAtAt.java
+++ b/empire-db/src/main/java/org/apache/empire/dbms/postgresql/PostgresAtAt.java
@@ -20,7 +20,7 @@
import java.util.Set;
-import org.apache.empire.commons.Unwrappable;
+import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.db.DBColumn;
import org.apache.empire.db.DBColumnExpr;
import org.apache.empire.db.DBCommand;
@@ -61,13 +61,12 @@
return new PostgresAtAt(this.left, this.right);
}
- @SuppressWarnings("unchecked")
@Override
public boolean isMutuallyExclusive(DBCompareExpr other)
{
- if ((other instanceof Unwrappable<?>) && ((Unwrappable<?>)other).isWrapper())
+ if (ObjectUtils.isWrapper(other))
{ // unwrap
- other = ((Unwrappable<DBCompareExpr>)other).unwrap();
+ other = ObjectUtils.unwrap(other);
}
if (other instanceof PostgresAtAt)
{ // compare
diff --git a/empire-db/src/main/java/org/apache/empire/dbms/postgresql/PostgresBoolAndOrExpr.java b/empire-db/src/main/java/org/apache/empire/dbms/postgresql/PostgresBoolAndOrExpr.java
index db59da9..6b79033 100644
--- a/empire-db/src/main/java/org/apache/empire/dbms/postgresql/PostgresBoolAndOrExpr.java
+++ b/empire-db/src/main/java/org/apache/empire/dbms/postgresql/PostgresBoolAndOrExpr.java
@@ -20,7 +20,7 @@
import java.util.Set;
-import org.apache.empire.commons.Unwrappable;
+import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.data.DataType;
import org.apache.empire.db.DBColumn;
import org.apache.empire.db.DBColumnExpr;
@@ -126,11 +126,10 @@
return elem;
}
- @SuppressWarnings("unchecked")
protected void appendName(StringBuilder buf, DBCompareExpr expr)
{
- if ((expr instanceof Unwrappable<?>) && ((Unwrappable<?>)expr).isWrapper())
- appendName(buf, ((Unwrappable<DBCompareExpr>)expr).unwrap());
+ if (ObjectUtils.isWrapper(expr))
+ appendName(buf, ObjectUtils.unwrap(expr));
else if (expr instanceof DBCompareNotExpr)
appendName(buf, ((DBCompareNotExpr)expr).getExpr());
else if (expr instanceof DBCompareAndOrExpr) {