summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2010-06-01 13:40:33 -0700
committerShawn O. Pearce <sop@google.com>2010-06-01 13:40:36 -0700
commitc57426c28c2a2d419cf5bae10469a8a225fa0efa (patch)
treedd1cea60e77041cd993b02b53f525bd251210e76
parent3b8d5560d64e0cf3e74f38733350aedc52eb648e (diff)
downloadgwtorm-c57426c28c2a2d419cf5bae10469a8a225fa0efa.tar.gz
Refactor sequence increment to be in dialects
By changing the nextLong method to generate the SQL on the fly within the increment rather than embedding it into the generated schema implementation we can later refactor the schema code generator to be more generic for the NoSQL type of backend where we don't necessarily have a query statement. This also cleans up the corner case of the MySQL dialect where we don't have a single query statement. Change-Id: Id29923f742c847e16e48655b928ab40f7c827c0f Signed-off-by: Shawn O. Pearce <sop@google.com>
-rw-r--r--src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java8
-rw-r--r--src/main/java/com/google/gwtorm/jdbc/gen/SchemaGen.java2
-rw-r--r--src/main/java/com/google/gwtorm/schema/sql/DialectH2.java2
-rw-r--r--src/main/java/com/google/gwtorm/schema/sql/DialectMySQL.java2
-rw-r--r--src/main/java/com/google/gwtorm/schema/sql/DialectPostgreSQL.java2
-rw-r--r--src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java5
-rw-r--r--src/main/java/com/google/gwtorm/server/AbstractSchema.java36
7 files changed, 48 insertions, 9 deletions
diff --git a/src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java b/src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java
index 9603edd..13ce9f9 100644
--- a/src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java
+++ b/src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java
@@ -22,6 +22,7 @@ import com.google.gwtorm.schema.RelationModel;
import com.google.gwtorm.schema.SchemaModel;
import com.google.gwtorm.schema.SequenceModel;
import com.google.gwtorm.schema.sql.SqlDialect;
+import com.google.gwtorm.server.AbstractSchema;
import java.sql.Connection;
import java.sql.SQLException;
@@ -30,7 +31,7 @@ import java.util.HashSet;
import java.util.Set;
/** Internal base class for implementations of {@link Schema}. */
-public abstract class JdbcSchema implements Schema {
+public abstract class JdbcSchema extends AbstractSchema {
private final Database<?> dbDef;
private Connection conn;
@@ -189,8 +190,9 @@ public abstract class JdbcSchema implements Schema {
}
}
- protected long nextLong(final String query) throws OrmException {
- return getDialect().nextLong(getConnection(), query);
+ @Override
+ protected long nextLong(final String poolName) throws OrmException {
+ return getDialect().nextLong(getConnection(), poolName);
}
public void close() {
diff --git a/src/main/java/com/google/gwtorm/jdbc/gen/SchemaGen.java b/src/main/java/com/google/gwtorm/jdbc/gen/SchemaGen.java
index f5d32e7..fc46f9a 100644
--- a/src/main/java/com/google/gwtorm/jdbc/gen/SchemaGen.java
+++ b/src/main/java/com/google/gwtorm/jdbc/gen/SchemaGen.java
@@ -153,7 +153,7 @@ public class SchemaGen implements Opcodes {
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
- mv.visitLdcInsn(dialect.getNextSequenceValueSql(seq.getSequenceName()));
+ mv.visitLdcInsn(seq.getSequenceName());
mv.visitMethodInsn(INVOKEVIRTUAL, superTypeName, "nextLong", Type
.getMethodDescriptor(Type.getType(Long.TYPE), new Type[] {Type
.getType(String.class)}));
diff --git a/src/main/java/com/google/gwtorm/schema/sql/DialectH2.java b/src/main/java/com/google/gwtorm/schema/sql/DialectH2.java
index 971118b..dab8535 100644
--- a/src/main/java/com/google/gwtorm/schema/sql/DialectH2.java
+++ b/src/main/java/com/google/gwtorm/schema/sql/DialectH2.java
@@ -42,7 +42,7 @@ public class DialectH2 extends SqlDialect {
}
@Override
- public String getNextSequenceValueSql(final String seqname) {
+ protected String getNextSequenceValueSql(final String seqname) {
return "SELECT NEXT VALUE FOR " + seqname;
}
diff --git a/src/main/java/com/google/gwtorm/schema/sql/DialectMySQL.java b/src/main/java/com/google/gwtorm/schema/sql/DialectMySQL.java
index 98cf63e..c0e1025 100644
--- a/src/main/java/com/google/gwtorm/schema/sql/DialectMySQL.java
+++ b/src/main/java/com/google/gwtorm/schema/sql/DialectMySQL.java
@@ -91,7 +91,7 @@ public class DialectMySQL extends SqlDialect {
}
@Override
- public String getNextSequenceValueSql(final String seqname) {
+ protected String getNextSequenceValueSql(final String seqname) {
return seqname;
}
diff --git a/src/main/java/com/google/gwtorm/schema/sql/DialectPostgreSQL.java b/src/main/java/com/google/gwtorm/schema/sql/DialectPostgreSQL.java
index e9b323d..b413dd1 100644
--- a/src/main/java/com/google/gwtorm/schema/sql/DialectPostgreSQL.java
+++ b/src/main/java/com/google/gwtorm/schema/sql/DialectPostgreSQL.java
@@ -62,7 +62,7 @@ public class DialectPostgreSQL extends SqlDialect {
}
@Override
- public String getNextSequenceValueSql(final String seqname) {
+ protected String getNextSequenceValueSql(final String seqname) {
return "SELECT nextval('" + seqname + "')";
}
diff --git a/src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java b/src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java
index 0e51db3..7b1fb65 100644
--- a/src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java
+++ b/src/main/java/com/google/gwtorm/schema/sql/SqlDialect.java
@@ -122,8 +122,9 @@ public abstract class SqlDialect {
return new OrmException(op + " failure on " + entity, err);
}
- public long nextLong(final Connection conn, final String query)
+ public long nextLong(final Connection conn, final String poolName)
throws OrmException {
+ final String query = getNextSequenceValueSql(poolName);
try {
final Statement st = conn.createStatement();
try {
@@ -297,5 +298,5 @@ public abstract class SqlDialect {
public abstract void renameColumn(StatementExecutor e, String tableName,
String fromColumn, ColumnModel col) throws OrmException;
- public abstract String getNextSequenceValueSql(String seqname);
+ protected abstract String getNextSequenceValueSql(String seqname);
}
diff --git a/src/main/java/com/google/gwtorm/server/AbstractSchema.java b/src/main/java/com/google/gwtorm/server/AbstractSchema.java
new file mode 100644
index 0000000..11a934c
--- /dev/null
+++ b/src/main/java/com/google/gwtorm/server/AbstractSchema.java
@@ -0,0 +1,36 @@
+// Copyright 2010 Google Inc.
+//
+// Licensed 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 com.google.gwtorm.server;
+
+import com.google.gwtorm.client.OrmException;
+import com.google.gwtorm.client.Schema;
+
+/** Base implementation any generated schema must implement. */
+public abstract class AbstractSchema implements Schema {
+ /**
+ * Obtain the next unique value from a pool of available numbers.
+ * <p>
+ * Frequently the next number will be just an increment of a global counter,
+ * but may be spread across multiple counter ranges to increase concurrency.
+ *
+ * @param poolName unique name of the counter within the schema. The
+ * underlying storage system should use this to identify the counter
+ * pool to obtain the next value from.
+ * @return a new unique value.
+ * @throws OrmException a value cannot be reserved for the caller, or the pool
+ * has been exhausted and no new values are available.
+ */
+ protected abstract long nextLong(String poolName) throws OrmException;
+}