cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skolbac...@apache.org
Subject [2/2] cayenne git commit: CAY-2100 Add supporting generated keys for PostgreSQL
Date Wed, 10 Aug 2016 14:41:08 GMT
CAY-2100 Add supporting generated keys for PostgreSQL


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

Branch: refs/heads/master
Commit: c208ae6a170dabe1a4a9cf3578aa61e5de03e744
Parents: 3f7dd72
Author: Savva Kolbachev <s.kolbachev@gmail.com>
Authored: Wed Aug 10 17:17:18 2016 +0300
Committer: Savva Kolbachev <s.kolbachev@gmail.com>
Committed: Wed Aug 10 17:17:18 2016 +0300

----------------------------------------------------------------------
 .../apache/cayenne/access/jdbc/BatchAction.java | 45 +++++++++++++++-----
 .../cayenne/dba/postgres/PostgresAdapter.java   |  7 ++-
 .../org/apache/cayenne/dba/postgres/types.xml   |  3 ++
 .../org/apache/cayenne/access/DbLoaderIT.java   |  2 +-
 docs/doc/src/main/resources/RELEASE-NOTES.txt   |  1 +
 pom.xml                                         |  4 +-
 6 files changed, 48 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/c208ae6a/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 a97bc78..0f6d7c8 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
@@ -19,14 +19,6 @@
 
 package org.apache.cayenne.access.jdbc;
 
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Collection;
-import java.util.Collections;
-
 import org.apache.cayenne.CayenneException;
 import org.apache.cayenne.ResultIterator;
 import org.apache.cayenne.access.DataNode;
@@ -37,6 +29,7 @@ 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;
@@ -44,6 +37,10 @@ import org.apache.cayenne.query.BatchQuery;
 import org.apache.cayenne.query.BatchQueryRow;
 import org.apache.cayenne.query.InsertBatchQuery;
 
+import java.sql.*;
+import java.util.Collection;
+import java.util.Collections;
+
 /**
  * @since 1.2
  */
@@ -161,8 +158,7 @@ public class BatchAction extends BaseSQLAction {
 
 		DbAdapter adapter = dataNode.getAdapter();
 
-		try (PreparedStatement statement = (generatesKeys) ? connection.prepareStatement(queryStr,
-				Statement.RETURN_GENERATED_KEYS) : connection.prepareStatement(queryStr);) {
+		try (PreparedStatement statement = prepareStatement(connection, queryStr, adapter, generatesKeys))
{
 			for (BatchQueryRow row : query.getRows()) {
 
 				DbAttributeBinding[] bindings = translator.updateBindings(row);
@@ -187,6 +183,35 @@ 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);
+	}
+
 	/**
 	 * Returns whether BatchQuery generates any keys.
 	 */

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c208ae6a/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresAdapter.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresAdapter.java
b/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresAdapter.java
index e4c7eff..7df4354 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresAdapter.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/dba/postgres/PostgresAdapter.java
@@ -76,6 +76,7 @@ public class PostgresAdapter extends JdbcAdapter {
 			@Inject(Constants.SERVER_RESOURCE_LOCATOR) ResourceLocator resourceLocator) {
 		super(runtimeProperties, defaultExtendedTypes, userExtendedTypes, extendedTypeFactories,
resourceLocator);
 		setSupportsBatchUpdates(true);
+		setSupportsGeneratedKeys(true);
 	}
 
 	/**
@@ -220,7 +221,11 @@ public class PostgresAdapter extends JdbcAdapter {
 					+ at.getName() + "': " + at.getType());
 		}
 
-		buf.append(context.quotedName(at)).append(' ').append(types[0]).append(sizeAndPrecision(this,
at))
+		// Checking that attribute is generated and we have alternative types in types.xml.
+		// If so, use those autoincremented types. For example serial, bigserial, smallserial.
+		String type = (at.isGenerated() && types.length > 1) ? types[1] : types[0];
+
+		buf.append(context.quotedName(at)).append(' ').append(type).append(sizeAndPrecision(this,
at))
 				.append(at.isMandatory() ? " NOT" : "").append(" NULL");
 	}
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c208ae6a/cayenne-server/src/main/resources/org/apache/cayenne/dba/postgres/types.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/resources/org/apache/cayenne/dba/postgres/types.xml b/cayenne-server/src/main/resources/org/apache/cayenne/dba/postgres/types.xml
index 8799f6d..0ea28cf 100644
--- a/cayenne-server/src/main/resources/org/apache/cayenne/dba/postgres/types.xml
+++ b/cayenne-server/src/main/resources/org/apache/cayenne/dba/postgres/types.xml
@@ -27,6 +27,7 @@ Broken PostgreSQL types mapping.
    <jdbc-type name="ARRAY"/>
    <jdbc-type name="BIGINT">
        <db-type name="bigint"/>
+       <db-type name="bigserial"/>
    </jdbc-type>
    <jdbc-type name="BINARY">
        <db-type name="bytea"/>
@@ -68,6 +69,7 @@ Broken PostgreSQL types mapping.
    </jdbc-type>
    <jdbc-type name="INTEGER">
        <db-type name="integer"/>
+       <db-type name="serial"/>
    </jdbc-type>
    <jdbc-type name="JAVA_OBJECT"/>
    <jdbc-type name="LONGVARBINARY">
@@ -89,6 +91,7 @@ Broken PostgreSQL types mapping.
    <jdbc-type name="REF"/>
    <jdbc-type name="SMALLINT">
        <db-type name="smallint"/>
+       <db-type name="smallserial"/>
    </jdbc-type>
    <jdbc-type name="STRUCT"/>
    <jdbc-type name="TIME">

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c208ae6a/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java b/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java
index 18c5d99..6d9a682 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/DbLoaderIT.java
@@ -262,7 +262,7 @@ public class DbLoaderIT extends ServerCase {
 
         if (adapter.supportsGeneratedKeys()) {
             DbEntity bag = getDbEntity(map, "GENERATED_COLUMN_TEST");
-            DbAttribute id = bag.getAttribute("GENERATED_COLUMN");
+            DbAttribute id = getDbAttribute(bag, "GENERATED_COLUMN");
             assertTrue(id.isPrimaryKey());
             assertTrue(id.isGenerated());
         }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c208ae6a/docs/doc/src/main/resources/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/RELEASE-NOTES.txt b/docs/doc/src/main/resources/RELEASE-NOTES.txt
index bbff246..aef5cd7 100644
--- a/docs/doc/src/main/resources/RELEASE-NOTES.txt
+++ b/docs/doc/src/main/resources/RELEASE-NOTES.txt
@@ -24,6 +24,7 @@ CAY-2074 Support for catalogs in stored procedures
 CAY-2076 Implement Jetty HTTP/1.1 and HTTP/2 Client support for ROP Client
 CAY-2083 Implement Protostuff as serialization service for Cayenne ROP
 CAY-2090 Untangle HttpRemoteService from ServiceContext thread local setup
+CAY-2100 Add supporting generated keys for PostgreSQL
 
 Bug Fixes:
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c208ae6a/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 16bd539..76363f2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1335,9 +1335,9 @@
 			</activation>
 			<dependencies>
 				<dependency>
-					<groupId>postgresql</groupId>
+					<groupId>org.postgresql</groupId>
 					<artifactId>postgresql</artifactId>
-					<version>9.1-901-1.jdbc4</version>
+					<version>9.4.1209</version>
 					<scope>test</scope>
 				</dependency>
 			</dependencies>


Mime
View raw message