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 1d59c8db EMPIREDB-456 DBCommand new method addKeyConstraints(...)
1d59c8db is described below
commit 1d59c8db69b599d8ef42620d4c85e8a3d21ed077
Author: Rainer Döbele <[email protected]>
AuthorDate: Fri Mar 21 11:41:14 2025 +0100
EMPIREDB-456
DBCommand new method addKeyConstraints(...)
---
.../main/java/org/apache/empire/db/DBCommand.java | 99 +++++++++++++---------
.../main/java/org/apache/empire/db/DBRowSet.java | 16 ----
2 files changed, 58 insertions(+), 57 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 b9c23fe1..e04fd808 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
@@ -29,6 +29,8 @@ import java.util.Set;
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.StringUtils;
import org.apache.empire.data.DataType;
+import org.apache.empire.data.RecordData;
+import org.apache.empire.db.exceptions.NoPrimaryKeyException;
import org.apache.empire.db.expr.column.DBAliasExpr;
import org.apache.empire.db.expr.column.DBValueExpr;
import org.apache.empire.db.expr.compare.DBCompareAndOrExpr;
@@ -931,6 +933,16 @@ public abstract class DBCommand extends DBCommandExpr
this.joins.addAll(joinExprList);
}
+ /**
+ * Returns a copy of the defined joins.
+ *
+ * @return the list of joins
+ */
+ public List<DBJoinExpr> getJoins()
+ {
+ return (this.joins!=null ? Collections.unmodifiableList(this.joins) :
null);
+ }
+
/**
* Returns true if the command has a join on the given table or false
otherwise.
*
@@ -952,37 +964,6 @@ public abstract class DBCommand extends DBCommandExpr
return false;
}
- /**
- * Returns true if the command has a constraint on the given table or
false otherwise.
- *
- * @param rowset rowset table or view to join
- *
- * @return true if the command has a join on the given table or false
otherwise
- */
- public boolean hasConstraintOn(DBRowSet rowset)
- {
- if (where==null && having==null)
- return false;
- // Examine all constraints
- int i = 0;
- Set<DBColumn> columns = new HashSet<DBColumn>();
- for (i = 0; where != null && i < where.size(); i++)
- ((DBExpr) where.get(i)).addReferencedColumns(columns);
- for (i = 0; having != null && i < having.size(); i++)
- ((DBExpr) having.get(i)).addReferencedColumns(columns);
- // now we have all columns
- Iterator<DBColumn> iterator = columns.iterator();
- while (iterator.hasNext())
- { // get the table
- DBColumn col = iterator.next();
- DBRowSet table = col.getRowSet();
- if (table.equals(rowset))
- return true;
- }
- // not found
- return false;
- }
-
/**
* Returns true if the command has a join on the given column or false
otherwise.
*
@@ -1124,16 +1105,6 @@ public abstract class DBCommand extends DBCommandExpr
{
return (findConstraintOn(where, col)!=null);
}
-
- /**
- * Returns a copy of the defined joins.
- *
- * @return the list of joins
- */
- public List<DBJoinExpr> getJoins()
- {
- return (this.joins!=null ? Collections.unmodifiableList(this.joins) :
null);
- }
/**
* Adds a list of constraints to the command.
@@ -1147,6 +1118,21 @@ public abstract class DBCommand extends DBCommandExpr
// add
this.where.addAll(constraints);
}
+
+ /**
+ * Adds key constraints the command
+ * @param rowset the rowset for which to add constraints
+ * @param data the record data from which to take the key values
+ */
+ public void addKeyConstraints(DBRowSet rowset, RecordData data)
+ {
+ DBColumn[] keyColumns = rowset.getKeyColumns();
+ if (keyColumns==null || keyColumns.length==0)
+ throw new NoPrimaryKeyException(rowset);
+ // Collect key
+ for (int i=0; i<keyColumns.length; i++)
+ where(keyColumns[i].is(data.get(keyColumns[i])));
+ }
/**
* adds a constraint to the having clause.
@@ -1211,6 +1197,37 @@ public abstract class DBCommand extends DBCommandExpr
return (findConstraintOn(having, col)!=null);
}
+ /**
+ * Returns true if the command has a constraint on the given table or
false otherwise.
+ *
+ * @param rowset rowset table or view to join
+ *
+ * @return true if the command has a join on the given table or false
otherwise
+ */
+ public boolean hasConstraintOn(DBRowSet rowset)
+ {
+ if (where==null && having==null)
+ return false;
+ // Examine all constraints
+ int i = 0;
+ Set<DBColumn> columns = new HashSet<DBColumn>();
+ for (i = 0; where != null && i < where.size(); i++)
+ ((DBExpr) where.get(i)).addReferencedColumns(columns);
+ for (i = 0; having != null && i < having.size(); i++)
+ ((DBExpr) having.get(i)).addReferencedColumns(columns);
+ // now we have all columns
+ Iterator<DBColumn> iterator = columns.iterator();
+ while (iterator.hasNext())
+ { // get the table
+ DBColumn col = iterator.next();
+ DBRowSet table = col.getRowSet();
+ if (table.equals(rowset))
+ return true;
+ }
+ // not found
+ return false;
+ }
+
/**
* Returns whether or not the command has group by set
* @return true if a group by expression exists
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 2765d473..44f0dcc6 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
@@ -37,7 +37,6 @@ import org.apache.empire.data.ColumnExpr;
import org.apache.empire.data.DataType;
import org.apache.empire.data.EntityType;
import org.apache.empire.data.Record;
-import org.apache.empire.data.RecordData;
import org.apache.empire.db.DBRelation.DBCascadeAction;
import org.apache.empire.db.DBRelation.DBReference;
import org.apache.empire.db.context.DBContextBase;
@@ -1293,21 +1292,6 @@ public abstract class DBRowSet extends DBExpr implements
EntityType
// just use the context
return context.createCommand();
}
-
- /**
- * Adds key constraints to a DBCommand object
- * @param cmd the command to add the constraints to
- * @param data the record from which to take the values
- */
- public void addKeyConstraints(DBCommand cmd, RecordData data)
- {
- DBColumn[] keyColumns = getKeyColumns();
- if (keyColumns==null || keyColumns.length==0)
- throw new NoPrimaryKeyException(this);
- // Collect key
- for (int i=0; i<keyColumns.length; i++)
- cmd.where(keyColumns[i].is(data.get(keyColumns[i])));
- }
/**
* Returns additional data stored on a record by the RowSet