cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject cayenne git commit: CAY-2186 rewrite DerbyPkGenerator to use sequences instead of AUTO_PK_SUPPORT table
Date Wed, 08 Feb 2017 14:26:34 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 27fb5716a -> 72aa17ac8


CAY-2186 rewrite DerbyPkGenerator to use sequences instead of AUTO_PK_SUPPORT table


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

Branch: refs/heads/master
Commit: 72aa17ac8f5381c18e94d3414eb2e19683857ebd
Parents: 27fb571
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Wed Feb 8 17:25:49 2017 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Wed Feb 8 17:25:49 2017 +0300

----------------------------------------------------------------------
 .../cayenne/dba/derby/DerbyPkGenerator.java     | 68 +++++++-------------
 1 file changed, 23 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/72aa17ac/cayenne-server/src/main/java/org/apache/cayenne/dba/derby/DerbyPkGenerator.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/dba/derby/DerbyPkGenerator.java
b/cayenne-server/src/main/java/org/apache/cayenne/dba/derby/DerbyPkGenerator.java
index e2595ce..00a7fbf 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/dba/derby/DerbyPkGenerator.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/dba/derby/DerbyPkGenerator.java
@@ -19,66 +19,44 @@
 
 package org.apache.cayenne.dba.derby;
 
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.util.Collections;
-
-import org.apache.cayenne.CayenneException;
-import org.apache.cayenne.access.DataNode;
 import org.apache.cayenne.dba.JdbcAdapter;
-import org.apache.cayenne.dba.JdbcPkGenerator;
-import org.apache.cayenne.log.JdbcEventLogger;
+import org.apache.cayenne.dba.oracle.OraclePkGenerator;
 import org.apache.cayenne.map.DbEntity;
 
 /**
- * Default PK generator for Derby that uses updateable ResultSet to get the next
- * id from the lookup table.
+ * PK generator for Derby that uses sequences.
  * 
- * @since 1.2
+ * @since 4.0 (old one used AUTO_PK_SUPPORT table)
  */
-public class DerbyPkGenerator extends JdbcPkGenerator {
+public class DerbyPkGenerator extends OraclePkGenerator {
 
 	DerbyPkGenerator(JdbcAdapter adapter) {
 		super(adapter);
 	}
 
-	static final String SELECT_QUERY = "SELECT NEXT_ID FROM AUTO_PK_SUPPORT WHERE TABLE_NAME
= ? FOR UPDATE";
-
-	/**
-	 * @since 3.0
-	 */
 	@Override
-	protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
-
-		JdbcEventLogger logger = adapter.getJdbcEventLogger();
-		if (logger.isLoggable()) {
-			logger.logQuery(SELECT_QUERY, Collections.singletonList(entity.getName()));
-		}
-
-		try (Connection c = node.getDataSource().getConnection()) {
-			try (PreparedStatement select =
-						 c.prepareStatement(SELECT_QUERY, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE))
{
-				select.setString(1, entity.getName());
-				try (ResultSet rs = select.executeQuery()) {
-					if (!rs.next()) {
-						throw new CayenneException("PK lookup failed for table: " + entity.getName());
-					}
-
-					long nextId = rs.getLong(1);
+	protected String sequenceName(DbEntity entity) {
+		return super.sequenceName(entity).toUpperCase();
+	}
 
-					rs.updateLong(1, nextId + pkCacheSize);
-					rs.updateRow();
+	@Override
+	protected String selectNextValQuery(String pkGeneratingSequenceName) {
+		return "VALUES (NEXT VALUE FOR " + pkGeneratingSequenceName + ")";
+	}
 
-					if (rs.next()) {
-						throw new CayenneException("More than one PK record for table: " + entity.getName());
-					}
+	@Override
+	protected String selectAllSequencesQuery() {
+		return "SELECT SEQUENCENAME FROM SYS.SYSSEQUENCES";
+	}
 
-					c.commit();
+	@Override
+	protected String dropSequenceString(DbEntity entity) {
+		return "DROP SEQUENCE " + sequenceName(entity) + " RESTRICT";
+	}
 
-					return nextId;
-				}
-			}
-		}
+	@Override
+	protected String createSequenceString(DbEntity entity) {
+		return "CREATE SEQUENCE " + sequenceName(entity) + " AS BIGINT START WITH " + pkStartValue
+
+				" INCREMENT BY " + getPkCacheSize() + " NO MAXVALUE NO CYCLE";
 	}
 }


Mime
View raw message