cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1429989 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/dba/JdbcPkGenerator.java test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
Date Mon, 07 Jan 2013 20:16:36 GMT
Author: aadamchik
Date: Mon Jan  7 20:16:35 2013
New Revision: 1429989

URL: http://svn.apache.org/viewvc?rev=1429989&view=rev
Log:
CAY-1783 JdbcPkGenerator.longPkFromDatabase would throw an exception if the PK value exceeds
a range of Java int

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java?rev=1429989&r1=1429988&r2=1429989&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java
Mon Jan  7 20:16:35 2013
@@ -26,10 +26,7 @@ import java.sql.SQLException;
 import java.sql.Statement;
 import java.sql.Types;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentHashMap;
@@ -54,10 +51,12 @@ import org.apache.cayenne.util.IDUtil;
 public class JdbcPkGenerator implements PkGenerator {
 
     public static final int DEFAULT_PK_CACHE_SIZE = 20;
+    private static final long DEFAULT_PK_START_VALUE = 200;
 
     protected JdbcAdapter adapter;
     protected ConcurrentHashMap<String, Queue<Long>> pkCache = new ConcurrentHashMap<String,
Queue<Long>>();
     protected int pkCacheSize = DEFAULT_PK_CACHE_SIZE;
+    protected long pkStartValue = DEFAULT_PK_START_VALUE;
 
     public JdbcPkGenerator(JdbcAdapter adapter) {
         this.adapter = adapter;
@@ -146,7 +145,7 @@ public class JdbcPkGenerator implements 
                 .append(" (TABLE_NAME, NEXT_ID)")
                 .append(" VALUES ('")
                 .append(entName)
-                .append("', 200)");
+                .append("', ").append(pkStartValue).append(")");
         return buf.toString();
     }
 
@@ -337,6 +336,14 @@ public class JdbcPkGenerator implements 
     public void setPkCacheSize(int pkCacheSize) {
         this.pkCacheSize = (pkCacheSize < 1) ? 1 : pkCacheSize;
     }
+    
+    long getPkStartValue() {
+        return pkStartValue;
+    }
+    
+    void setPkStartValue(long startValue) {
+        this.pkStartValue = startValue;
+    }
 
     public void reset() {
         pkCache.clear();
@@ -358,13 +365,13 @@ public class JdbcPkGenerator implements 
             return false;
         }
 
-        public int getId() {
+        public long getId() {
             if (id == null) {
                 throw new CayenneRuntimeException("No key was retrieved for entity "
                         + entityName);
             }
 
-            return id.intValue();
+            return id.longValue();
         }
 
         public void nextRows(Query query, List<?> dataRows) {

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java?rev=1429989&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/dba/JdbcPkGeneratorTest.java
Mon Jan  7 20:16:35 2013
@@ -0,0 +1,60 @@
+/*****************************************************************
+ *   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;
+
+import java.util.Collections;
+
+import org.apache.cayenne.access.DataNode;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.testdo.testmap.Artist;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
+
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class JdbcPkGeneratorTest extends ServerCase {
+
+    @Inject
+    private DbAdapter adapter;
+
+    @Inject
+    private DataNode node;
+
+    public void testLongPk() throws Exception {
+
+        if (!JdbcPkGenerator.class.isAssignableFrom(adapter.getPkGenerator().getClass()))
{
+            return;
+        }
+
+        DbEntity artistEntity = node.getEntityResolver().lookupObjEntity(Artist.class).getDbEntity();
+
+        DbAttribute pkAttribute = (DbAttribute) artistEntity.getAttribute(Artist.ARTIST_ID_PK_COLUMN);
+
+        JdbcPkGenerator pkGenerator = (JdbcPkGenerator) adapter.getPkGenerator();
+
+        pkGenerator.setPkStartValue(Integer.MAX_VALUE * 2l);
+        pkGenerator.createAutoPk(node, Collections.singletonList(artistEntity));
+        pkGenerator.reset();
+
+        Object pk = pkGenerator.generatePk(node, pkAttribute);
+        assertTrue(pk instanceof Long);
+        assertTrue("PK is too small: " + pk, ((Long) pk).longValue() > Integer.MAX_VALUE);
+    }
+}



Mime
View raw message