This is an automated email from the ASF dual-hosted git repository.
doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git
The following commit(s) were added to refs/heads/master by this push:
new c6110166 EMPIREDB-402 Added on() functions on DBRowset in order to
create join expressions
c6110166 is described below
commit c61101665b3b278bcb7e150a9faab9aef83f0d8d
Author: Rainer Döbele <[email protected]>
AuthorDate: Thu Apr 13 10:40:03 2023 +0200
EMPIREDB-402
Added on() functions on DBRowset in order to create join expressions
---
.../main/java/org/apache/empire/db/DBCommand.java | 51 +++++++++++++---------
.../main/java/org/apache/empire/db/DBRowSet.java | 20 +++++++++
.../empire/db/expr/join/DBCrossJoinExpr.java | 10 +++++
.../empire/exceptions/ObjectNotValidException.java | 13 ++++--
4 files changed, 69 insertions(+), 25 deletions(-)
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 f2ba88c6..9d4aa9cb 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
@@ -673,9 +673,9 @@ public abstract class DBCommand extends DBCommandExpr
*/
public DBCommand join(DBJoinExpr join)
{
- // check tables
+ // Rowsets must be different
if (join.getLeftTable().equals(join.getRightTable()))
- throw new InvalidArgumentException("left|right",
join.getLeftTable());
+ throw new ObjectNotValidException(join, "The rowsets of a join
expression must not be the same!");
// create list
if (joins == null)
joins = new ArrayList<DBJoinExpr>();
@@ -696,55 +696,57 @@ public abstract class DBCommand extends DBCommandExpr
}
/**
- * Adds an inner join based on two columns to the list of join expressions.
- *
- * @param left the left join value
- * @param right the right join
+ * Adds a left join to the list of join expressions.
* @return itself (this)
*/
- public final DBCommand join(DBColumnExpr left, DBColumn right,
DBCompareExpr... addlConstraints)
+ public final DBCommand joinLeft(DBJoinExpr join)
{
- return join(left, right, DBJoinType.INNER, addlConstraints);
+ join.setType(DBJoinType.LEFT);
+ return join(join);
}
/**
* Adds a left join to the list of join expressions.
* @return itself (this)
*/
- public final DBCommand joinLeft(DBJoinExpr join)
+ public final DBCommand joinRight(DBJoinExpr join)
{
- join.setType(DBJoinType.LEFT);
+ join.setType(DBJoinType.RIGHT);
return join(join);
}
/**
- * Adds a left join based on two columns to the list of join expressions.
- * Added for convenience
- * Same as join(left, right, DBJoinType.LEFT);
+ * Adds an inner join based on two columns to the list of join expressions.
+ *
+ * New in release 3.1: Use join(left.on(right).and(addlConstraint)) instead
*
* @param left the left join value
* @param right the right join
* @return itself (this)
*/
- public final DBCommand joinLeft(DBColumnExpr left, DBColumn right,
DBCompareExpr... addlConstraints)
+ public final DBCommand join(DBColumnExpr left, DBColumn right,
DBCompareExpr... addlConstraints)
{
- return join(left, right, DBJoinType.LEFT, addlConstraints);
+ return join(left, right, DBJoinType.INNER, addlConstraints);
}
/**
- * Adds a left join to the list of join expressions.
+ * Adds a left join based on two columns to the list of join expressions.
+ *
+ * New in release 3.1: Use joinLeft(left.on(right).and(addlConstraint))
instead
+ *
+ * @param left the left join value
+ * @param right the right join
* @return itself (this)
*/
- public final DBCommand joinRight(DBJoinExpr join)
+ public final DBCommand joinLeft(DBColumnExpr left, DBColumn right,
DBCompareExpr... addlConstraints)
{
- join.setType(DBJoinType.RIGHT);
- return join(join);
+ return join(left, right, DBJoinType.LEFT, addlConstraints);
}
/**
* Adds a right join based on two columns to the list of join expressions.
- * Added for convenience
- * Same as join(left, right, DBJoinType.RIGHT);
+ *
+ * New in release 3.1: Use joinRight(left.on(right).and(addlConstraint))
instead
*
* @param left the left join value
* @param right the right join
@@ -831,6 +833,9 @@ public abstract class DBCommand extends DBCommandExpr
/**
* Adds a cross join for two tables or views
+ *
+ * New in release 3.1: Use left.on(right)) instead
+ *
* @param left the left RowSet
* @param right the right RowSet
* @return itself (this)
@@ -846,6 +851,8 @@ public abstract class DBCommand extends DBCommandExpr
/**
* Adds a join based on a compare expression to the command.
*
+ * New in release 3.1: Use joinLeft(rowset.on(cmp)) instead
+ *
* @param rowset table or view to join
* @param cmp the compare expression with which to join the table
* @param joinType type of join ({@link DBJoinType#INNER}, {@link
DBJoinType#LEFT}, {@link DBJoinType#RIGHT})
@@ -861,6 +868,8 @@ public abstract class DBCommand extends DBCommandExpr
/**
* Adds an inner join based on a compare expression to the command.
*
+ * New in release 3.1: Use rowset.on(cmp) instead
+ *
* @param rowset table of view which to join
* @param cmp the compare expression with wich to join the table
* @return itself (this)
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
index 76d06bce..66cf2396 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
@@ -49,6 +49,8 @@ import
org.apache.empire.db.exceptions.RecordUpdateFailedException;
import org.apache.empire.db.expr.column.DBCountExpr;
import org.apache.empire.db.expr.compare.DBCompareColExpr;
import org.apache.empire.db.expr.compare.DBCompareExpr;
+import org.apache.empire.db.expr.join.DBCompareJoinExpr;
+import org.apache.empire.db.expr.join.DBCrossJoinExpr;
import org.apache.empire.db.list.DBBeanFactoryCache;
import org.apache.empire.db.list.DBBeanListFactory;
import org.apache.empire.db.list.DBBeanListFactoryImpl;
@@ -603,6 +605,24 @@ public abstract class DBRowSet extends DBExpr implements
EntityType
{
return columns.get(index);
}
+
+ /**
+ * Creates a join expression based on a compare expression
+ */
+ public DBCompareJoinExpr on(DBCompareExpr cmp)
+ {
+ DBCompareJoinExpr join = new DBCompareJoinExpr(this, cmp,
DBJoinType.INNER);
+ return join;
+ }
+
+ /**
+ * Creates a cross join expression
+ */
+ public DBCrossJoinExpr on(DBRowSet right)
+ {
+ DBCrossJoinExpr join = new DBCrossJoinExpr(this, right);
+ return join;
+ }
/**
* Initializes a DBRecord for this RowSet and sets primary key values.
diff --git
a/empire-db/src/main/java/org/apache/empire/db/expr/join/DBCrossJoinExpr.java
b/empire-db/src/main/java/org/apache/empire/db/expr/join/DBCrossJoinExpr.java
index 25c3f4c8..3f48d63f 100644
---
a/empire-db/src/main/java/org/apache/empire/db/expr/join/DBCrossJoinExpr.java
+++
b/empire-db/src/main/java/org/apache/empire/db/expr/join/DBCrossJoinExpr.java
@@ -27,6 +27,7 @@ import org.apache.empire.db.DBDatabase;
import org.apache.empire.db.DBJoinType;
import org.apache.empire.db.DBRowSet;
import org.apache.empire.db.DBSQLBuilder;
+import org.apache.empire.exceptions.NotSupportedException;
/**
* This class is used for building a join expression of an SQL statement.
@@ -121,6 +122,15 @@ public class DBCrossJoinExpr extends DBJoinExpr
{
return null; // no outer table!
}
+
+ /**
+ * alters the join type for this join
+ */
+ @Override
+ public void setType(DBJoinType type)
+ {
+ throw new NotSupportedException(this, "joinType:"+type.name());
+ }
/**
* This function swaps the left and the right statements of the join
expression.
diff --git
a/empire-db/src/main/java/org/apache/empire/exceptions/ObjectNotValidException.java
b/empire-db/src/main/java/org/apache/empire/exceptions/ObjectNotValidException.java
index 2f503e97..7ff3e5e5 100644
---
a/empire-db/src/main/java/org/apache/empire/exceptions/ObjectNotValidException.java
+++
b/empire-db/src/main/java/org/apache/empire/exceptions/ObjectNotValidException.java
@@ -24,16 +24,21 @@ public class ObjectNotValidException extends EmpireException
{
private static final long serialVersionUID = 1L;
- public static final ErrorType errorType = new
ErrorType("error.objectNotValid", "The object of type {0} has not been
initialized.");
+ public static final ErrorType errorType = new
ErrorType("error.objectNotValid", "The object {0} is invalid: {1}");
- public ObjectNotValidException(Class<?> clazz)
+ public ObjectNotValidException(Class<?> clazz, String message)
{
- super(errorType, new String[] { (clazz!=null) ? clazz.getName() :
"{unknown}" } );
+ super(errorType, new String[] { (clazz!=null) ? clazz.getName() :
"{unknown}", message } );
+ }
+
+ public ObjectNotValidException(Object obj, String message)
+ {
+ this((obj!=null) ? obj.getClass() : null, message);
}
public ObjectNotValidException(Object obj)
{
- this((obj!=null) ? obj.getClass() : null);
+ this((obj!=null) ? obj.getClass() : null, "Not initialized");
}
}