You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ddlutils-dev@db.apache.org by to...@apache.org on 2006/06/18 11:08:29 UTC
svn commit: r415113 - in /db/ddlutils/trunk/src:
java/org/apache/ddlutils/platform/mckoi/ test/org/apache/ddlutils/platform/
Author: tomdz
Date: Sun Jun 18 02:08:28 2006
New Revision: 415113
URL: http://svn.apache.org/viewvc?rev=415113&view=rev
Log:
Changed auto-increment support in McKoi to use sequences rather than UNIQUEKEY
Modified:
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java
db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java
db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMcKoiPlatform.java
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java?rev=415113&r1=415112&r2=415113&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiBuilder.java Sun Jun 18 02:08:28 2006
@@ -17,10 +17,15 @@
*/
import java.io.IOException;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.alteration.AddColumnChange;
+import org.apache.ddlutils.alteration.ColumnAutoIncrementChange;
+import org.apache.ddlutils.alteration.RemoveColumnChange;
+import org.apache.ddlutils.alteration.TableChange;
import org.apache.ddlutils.model.Column;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.model.Table;
@@ -52,11 +57,69 @@
/**
* {@inheritDoc}
*/
+ public void createTable(Database database, Table table, Map parameters) throws IOException
+ {
+ // we use sequences instead of the UNIQUEKEY function because this way
+ // we can read their values back
+ Column[] columns = table.getAutoIncrementColumns();
+
+ for (int idx = 0; idx < columns.length; idx++)
+ {
+ createAutoIncrementSequence(table, columns[idx]);
+ }
+
+ super.createTable(database, table, parameters);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public void dropTable(Table table) throws IOException
{
print("DROP TABLE IF EXISTS ");
printIdentifier(getTableName(table));
printEndOfStatement();
+
+ Column[] columns = table.getAutoIncrementColumns();
+
+ for (int idx = 0; idx < columns.length; idx++)
+ {
+ dropAutoIncrementSequence(table, columns[idx]);
+ }
+ }
+
+ /**
+ * Creates the sequence necessary for the auto-increment of the given column.
+ *
+ * @param table The table
+ * @param column The column
+ */
+ protected void createAutoIncrementSequence(Table table,
+ Column column) throws IOException
+ {
+ print("CREATE SEQUENCE ");
+ printIdentifier(getConstraintName("seq",
+ table,
+ column.getName(),
+ null));
+ printEndOfStatement();
+ }
+
+ /**
+ * Drops the sequence used for the auto-increment of the given column.
+ *
+ * @param table The table
+ * @param column The column
+ */
+ protected void dropAutoIncrementSequence(Table table,
+ Column column) throws IOException
+ {
+ print("DROP SEQUENCE ");
+ printIdentifier(getConstraintName("seq",
+ table,
+ column.getName(),
+ null));
+ printEndOfStatement();
}
/**
@@ -67,8 +130,8 @@
if (column.isAutoIncrement())
{
// we start at value 1 to avoid issues with jdbc
- print("UNIQUEKEY('");
- print(getTableName(table));
+ print("NEXTVAL('");
+ print(getConstraintName("seq", table, column.getName(), null));
print("')");
}
else
@@ -80,6 +143,36 @@
/**
* {@inheritDoc}
*/
+ public String getSelectLastIdentityValues(Table table)
+ {
+ Column[] columns = table.getAutoIncrementColumns();
+
+ if (columns.length > 0)
+ {
+ StringBuffer result = new StringBuffer();
+
+ result.append("SELECT ");
+ for (int idx = 0; idx < columns.length; idx++)
+ {
+ if (idx > 0)
+ {
+ result.append(",");
+ }
+ result.append("CURRVAL('");
+ result.append(getConstraintName("seq", table, columns[idx].getName(), null));
+ result.append("')");
+ }
+ return result.toString();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
protected void processTableStructureChanges(Database currentModel,
Database desiredModel,
Table sourceTable,
@@ -88,8 +181,67 @@
List changes) throws IOException
{
// McKoi has this nice ALTER CREATE TABLE statement which saves us a lot of work
+ // We only have to handle auto-increment changes manually
+ for (Iterator it = changes.iterator(); it.hasNext();)
+ {
+ TableChange change = (TableChange)it.next();
+
+ if (change instanceof ColumnAutoIncrementChange)
+ {
+ Column column = ((ColumnAutoIncrementChange)change).getColumn();
+
+ // we have to defer removal of the sequences until they are no longer used
+ if (!column.isAutoIncrement())
+ {
+ ColumnAutoIncrementChange autoIncrChange = (ColumnAutoIncrementChange)change;
+
+ createAutoIncrementSequence(autoIncrChange.getChangedTable(),
+ autoIncrChange.getColumn());
+ }
+ }
+ else if (change instanceof AddColumnChange)
+ {
+ AddColumnChange addColumnChange = (AddColumnChange)change;
+
+ if (addColumnChange.getNewColumn().isAutoIncrement())
+ {
+ createAutoIncrementSequence(addColumnChange.getChangedTable(),
+ addColumnChange.getNewColumn());
+ }
+ }
+ }
+
print("ALTER ");
- createTable(desiredModel, targetTable, parameters);
- }
+ super.createTable(desiredModel, targetTable, parameters);
+ for (Iterator it = changes.iterator(); it.hasNext();)
+ {
+ TableChange change = (TableChange)it.next();
+
+ if (change instanceof ColumnAutoIncrementChange)
+ {
+ Column column = ((ColumnAutoIncrementChange)change).getColumn();
+
+ if (column.isAutoIncrement())
+ {
+ ColumnAutoIncrementChange autoIncrChange = (ColumnAutoIncrementChange)change;
+
+ dropAutoIncrementSequence(autoIncrChange.getChangedTable(),
+ autoIncrChange.getColumn());
+ }
+ }
+ else if (change instanceof RemoveColumnChange)
+ {
+ RemoveColumnChange removeColumnChange = (RemoveColumnChange)change;
+
+ if (removeColumnChange.getColumn().isAutoIncrement())
+ {
+ dropAutoIncrementSequence(removeColumnChange.getChangedTable(),
+ removeColumnChange.getColumn());
+ }
+ }
+ }
+ changes.clear();
+ }
}
+
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java?rev=415113&r1=415112&r2=415113&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiModelReader.java Sun Jun 18 02:08:28 2006
@@ -26,6 +26,7 @@
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.model.Column;
import org.apache.ddlutils.model.Table;
+import org.apache.ddlutils.model.TypeMap;
import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
import org.apache.ddlutils.platform.JdbcModelReader;
@@ -112,10 +113,18 @@
String defaultValue = column.getDefaultValue();
- if ((defaultValue != null) && defaultValue.startsWith("UNIQUEKEY("))
+ if (defaultValue != null)
{
- column.setDefaultValue(null);
- column.setAutoIncrement(true);
+ if (defaultValue.toLowerCase().startsWith("nextval('") ||
+ defaultValue.toLowerCase().startsWith("uniquekey('"))
+ {
+ column.setDefaultValue(null);
+ column.setAutoIncrement(true);
+ }
+ else if (TypeMap.isTextType(column.getTypeCode()))
+ {
+ column.setDefaultValue(unescape(column.getDefaultValue(), "'", "\\'"));
+ }
}
return column;
}
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java?rev=415113&r1=415112&r2=415113&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/mckoi/MckoiPlatform.java Sun Jun 18 02:08:28 2006
@@ -55,6 +55,7 @@
info.setIndicesSupported(false);
info.setIndicesEmbedded(true);
info.setDefaultValueUsedForIdentitySpec(true);
+ info.setAutoCommitModeForLastIdentityValueReading(false);
info.addNativeTypeMapping(Types.ARRAY, "BLOB", Types.BLOB);
info.addNativeTypeMapping(Types.DISTINCT, "BLOB", Types.BLOB);
Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMcKoiPlatform.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMcKoiPlatform.java?rev=415113&r1=415112&r2=415113&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMcKoiPlatform.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/platform/TestMcKoiPlatform.java Sun Jun 18 02:08:28 2006
@@ -133,15 +133,19 @@
// note that this is not valid SQL as obviously only one auto increment field
// can be defined for each table
assertEqualsIgnoringWhitespaces(
- "DROP TABLE IF EXISTS \"constraints\";\n" +
+ "DROP TABLE IF EXISTS \"constraints\";\n"+
+ "DROP SEQUENCE \"seq_constraints_COL_PK_AUTO_INCR\";\n"+
+ "DROP SEQUENCE \"seq_constraints_COL_AUTO_INCR\";\n"+
+ "CREATE SEQUENCE \"seq_constraints_COL_PK_AUTO_INCR\";\n"+
+ "CREATE SEQUENCE \"seq_constraints_COL_AUTO_INCR\";\n"+
"CREATE TABLE \"constraints\"\n"+
"(\n"+
" \"COL_PK\" VARCHAR(32),\n"+
- " \"COL_PK_AUTO_INCR\" INTEGER DEFAULT UNIQUEKEY('constraints'),\n"+
+ " \"COL_PK_AUTO_INCR\" INTEGER DEFAULT NEXTVAL('seq_constraints_COL_PK_AUTO_INCR'),\n"+
" \"COL_NOT_NULL\" BINARY(100) NOT NULL,\n"+
" \"COL_NOT_NULL_DEFAULT\" DOUBLE DEFAULT -2.0 NOT NULL,\n"+
" \"COL_DEFAULT\" CHAR(4) DEFAULT 'test',\n"+
- " \"COL_AUTO_INCR\" BIGINT DEFAULT UNIQUEKEY('constraints'),\n"+
+ " \"COL_AUTO_INCR\" BIGINT DEFAULT NEXTVAL('seq_constraints_COL_AUTO_INCR'),\n"+
" PRIMARY KEY (\"COL_PK\", \"COL_PK_AUTO_INCR\")\n"+
");\n",
createTestDatabase(COLUMN_CONSTRAINT_TEST_SCHEMA));