solomax commented on code in PR #276:
URL: https://github.com/apache/openjpa/pull/276#discussion_r4488514128


##########
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java:
##########
@@ -910,6 +1143,156 @@ private boolean hasConstantJoin(List<ForeignKey> joins) {
         return false;
     }
 
+    /**
+     * The fields and version columns that a bulk update sets, grouped by the
+     * tables that hold their columns, and the statements that update those
+     * tables by the primary keys of the matching rows.
+     */
+    private static class TableUpdates {
+        private final Map<Table, Map<Object,Object>> fields =
+            new LinkedHashMap<>();
+        private final Map<Table, Map<Column,Object>> versions =
+            new LinkedHashMap<>();
+        private final Set<Table> tables = new LinkedHashSet<>();
+        private final List<Column[]> keyColumns = new ArrayList<>();
+        private final List<SQLBuffer> statements = new ArrayList<>();
+        private SQLBuffer keySelect;
+        private int keyCount;
+
+        TableUpdates(ClassMapping mapping, Map updates) {
+            boolean version = false;
+            for (Object o : updates.entrySet()) {
+                Map.Entry e = (Map.Entry) o;
+                FieldMapping fm = (FieldMapping) ((Path) e.getKey()).last();
+                if (fm.isVersion()) {
+                    version = true;
+                }
+                // the SET clause only assigns the first column of a field
+                Column[] cols = fm.getColumns();
+                Table table = (cols.length == 0) ? mapping.getTable()
+                    : cols[0].getTable();
+                fields.computeIfAbsent(table, t -> new LinkedHashMap<>())
+                    .put(e.getKey(), e.getValue());
+                tables.add(table);
+            }
+
+            // the dictionary updates the version of the class of the first
+            // field unless the update sets a version field itself
+            if (!version) {
+                FieldMapping fm = (FieldMapping) ((Path) updates.keySet()
+                    .iterator().next()).last();
+                Map<Column,?> values = fm.getDefiningMapping().getVersion()
+                    .getBulkUpdateValues();
+                for (Map.Entry<Column,?> e : values.entrySet()) {
+                    Table table = e.getKey().getTable();
+                    versions.computeIfAbsent(table, t -> new LinkedHashMap<>())
+                        .put(e.getKey(), e.getValue());
+                    tables.add(table);
+                }
+            }
+        }
+
+        /**
+         * Whether all the columns to set are in the given table.
+         */
+        boolean isOnly(Table table) {
+            return tables.size() == 1 && tables.contains(table);
+        }
+
+        /**
+         * Whether the value of every field to set only reads columns of the
+         * table that holds the column of the field.
+         *
+         * @param alias the identification variable of the candidate, or null
+         * if unknown
+         */
+        boolean readsOnlyUpdatedTables(String alias) {
+            for (Map.Entry<Table, Map<Object,Object>> e : fields.entrySet()) {
+                for (Object val : e.getValue().values()) {

Review Comment:
   these `Object`s make me feel it need to be refactored and strong typing :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to