cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skolbac...@apache.org
Subject cayenne git commit: refactor CAY-2100: add PostgresBatchAction;
Date Thu, 11 Aug 2016 08:57:30 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master c208ae6a1 -> 6a9e43d91


refactor CAY-2100: add PostgresBatchAction;


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/6a9e43d9
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/6a9e43d9
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/6a9e43d9

Branch: refs/heads/master
Commit: 6a9e43d9112ccab2de998b639db2d7942b61b4b2
Parents: c208ae6
Author: Savva Kolbachev <s.kolbachev@gmail.com>
Authored: Thu Aug 11 11:57:04 2016 +0300
Committer: Savva Kolbachev <s.kolbachev@gmail.com>
Committed: Thu Aug 11 11:57:04 2016 +0300

----------------------------------------------------------------------
 .../apache/cayenne/access/jdbc/BatchAction.java | 33 ++--------
 .../dba/postgres/PostgresActionBuilder.java     | 12 ++++
 .../dba/postgres/PostgresBatchAction.java       | 63 ++++++++++++++++++++
 3 files changed, 80 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/6a9e43d9/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/BatchAction.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/BatchAction.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/BatchAction.java
index 0f6d7c8..09e0c96 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/BatchAction.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/jdbc/BatchAction.java
@@ -29,7 +29,6 @@ import org.apache.cayenne.access.translator.DbAttributeBinding;
 import org.apache.cayenne.access.translator.batch.BatchTranslator;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.TypesMapping;
-import org.apache.cayenne.dba.postgres.PostgresAdapter;
 import org.apache.cayenne.log.JdbcEventLogger;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.ObjAttribute;
@@ -183,33 +182,11 @@ public class BatchAction extends BaseSQLAction {
 		}
 	}
 
-	private PreparedStatement prepareStatement(
-			Connection connection,
-			String queryStr,
-			DbAdapter adapter,
-			boolean generatedKeys)
-			throws SQLException {
-
-		if (generatedKeys) {
-
-			if (adapter.unwrap().getClass().equals(PostgresAdapter.class)) {
-				Collection<DbAttribute> generatedAttributes = query.getDbEntity().getGeneratedAttributes();
-				String[] generatedPKColumns = new String[generatedAttributes.size()];
-
-				int i = 0;
-				for (DbAttribute generatedAttribute : generatedAttributes) {
-					if (generatedAttribute.isPrimaryKey()) {
-						generatedPKColumns[i++] = generatedAttribute.getName().toLowerCase();
-					}
-				}
-
-				return connection.prepareStatement(queryStr, generatedPKColumns);
-			}
-
-			return connection.prepareStatement(queryStr, Statement.RETURN_GENERATED_KEYS);
-		}
-
-		return connection.prepareStatement(queryStr);
+	protected PreparedStatement prepareStatement(Connection connection,	String queryStr,
+												 DbAdapter adapter,	boolean generatedKeys) throws SQLException {
+		return (generatedKeys)
+				? connection.prepareStatement(queryStr, Statement.RETURN_GENERATED_KEYS)
+				: connection.prepareStatement(queryStr);
 	}
 
 	/**

http://git-wip-us.apache.org/repos/asf/cayenne/blob/6a9e43d9/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresActionBuilder.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresActionBuilder.java
b/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresActionBuilder.java
index b3f77ee..c583eca 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresActionBuilder.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresActionBuilder.java
@@ -21,6 +21,7 @@ package org.apache.cayenne.dba.postgres;
 
 import org.apache.cayenne.access.DataNode;
 import org.apache.cayenne.dba.JdbcActionBuilder;
+import org.apache.cayenne.query.BatchQuery;
 import org.apache.cayenne.query.ProcedureQuery;
 import org.apache.cayenne.query.SQLAction;
 import org.apache.cayenne.query.SelectQuery;
@@ -35,6 +36,17 @@ class PostgresActionBuilder extends JdbcActionBuilder {
     }
 
     @Override
+    public SQLAction batchAction(BatchQuery query) {
+        // check run strategy...
+
+        // optimistic locking is not supported in batches due to JDBC driver limitations
+        boolean useOptimisticLock = query.isUsingOptimisticLocking();
+
+        boolean runningAsBatch = !useOptimisticLock && dataNode.getAdapter().supportsBatchUpdates();
+        return new PostgresBatchAction(query, dataNode, runningAsBatch);
+    }
+
+    @Override
     public SQLAction procedureAction(ProcedureQuery query) {
         return new PostgresProcedureAction(query, dataNode);
     }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/6a9e43d9/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresBatchAction.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresBatchAction.java
b/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresBatchAction.java
new file mode 100644
index 0000000..28f5b37
--- /dev/null
+++ b/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresBatchAction.java
@@ -0,0 +1,63 @@
+/*****************************************************************
+ *   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.cayenne.dba.postgres;
+
+import org.apache.cayenne.access.DataNode;
+import org.apache.cayenne.access.jdbc.BatchAction;
+import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.query.BatchQuery;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collection;
+
+public class PostgresBatchAction extends BatchAction {
+
+    /**
+     * @since 4.0
+     */
+    public PostgresBatchAction(BatchQuery query, DataNode dataNode, boolean runningAsBatch)
{
+        super(query, dataNode, runningAsBatch);
+    }
+
+    @Override
+    protected PreparedStatement prepareStatement(Connection connection, String queryStr,
+                                                 DbAdapter adapter, boolean generatedKeys)
throws SQLException {
+        if (generatedKeys) {
+            Collection<DbAttribute> generatedAttributes = query.getDbEntity().getGeneratedAttributes();
+            String[] generatedPKColumns = new String[generatedAttributes.size()];
+
+            int i = 0;
+            for (DbAttribute generatedAttribute : generatedAttributes) {
+                if (generatedAttribute.isPrimaryKey()) {
+                    generatedPKColumns[i++] = generatedAttribute.getName().toLowerCase();
+                }
+            }
+
+            return connection.prepareStatement(queryStr, Arrays.copyOf(generatedPKColumns,
i));
+        }
+
+        return connection.prepareStatement(queryStr);
+    }
+
+}


Mime
View raw message