提交 ea2c1c18 authored 作者: Noel Grandin's avatar Noel Grandin

Merge remote-tracking branch 'upstream/master' into objectid_in_prepared

...@@ -55,7 +55,9 @@ public abstract class Prepared { ...@@ -55,7 +55,9 @@ public abstract class Prepared {
private long modificationMetaId; private long modificationMetaId;
private Command command; private Command command;
/** /**
* Used when we restore metadata at startup, this is the metadata object ID as persisted in the database. * Used to preserve object identities on database startup. {@code 0} if
* object is not stored, {@code -1} if object is stored and its ID is
* already read, {@code >0} if object is stored and its id is not yet read.
*/ */
private int persistedObjectId; private int persistedObjectId;
private int currentRowNumber; private int currentRowNumber;
...@@ -248,12 +250,14 @@ public abstract class Prepared { ...@@ -248,12 +250,14 @@ public abstract class Prepared {
* @return the object id or 0 if not set * @return the object id or 0 if not set
*/ */
protected int getPersistedObjectId() { protected int getPersistedObjectId() {
return persistedObjectId; int id = persistedObjectId;
return id >= 0 ? id : 0;
} }
/** /**
* Get the current object id (ie. if it is already persisted), or get a new id from the database. The object * Get the current object id, or get a new id from the database. The object
* id is used when creating new database object (CREATE statement). * id is used when creating new database object (CREATE statement). This
* method may be called only once.
* *
* @return the object id * @return the object id
*/ */
...@@ -261,7 +265,10 @@ public abstract class Prepared { ...@@ -261,7 +265,10 @@ public abstract class Prepared {
int id = persistedObjectId; int id = persistedObjectId;
if (id == 0) { if (id == 0) {
id = session.getDatabase().allocateObjectId(); id = session.getDatabase().allocateObjectId();
} else if (id < 0) {
throw DbException.throwInternalError("Prepared.getObjectId() was called before");
} }
persistedObjectId = -1;
return id; return id;
} }
......
...@@ -137,25 +137,24 @@ public class AlterTableAddConstraint extends SchemaCommand { ...@@ -137,25 +137,24 @@ public class AlterTableAddConstraint extends SchemaCommand {
throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY); throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY);
} }
} }
} } else {
if (index == null) {
IndexType indexType = IndexType.createPrimaryKey( IndexType indexType = IndexType.createPrimaryKey(
table.isPersistIndexes(), primaryKeyHash); table.isPersistIndexes(), primaryKeyHash);
String indexName = table.getSchema().getUniqueIndexName( String indexName = table.getSchema().getUniqueIndexName(
session, table, Constants.PREFIX_PRIMARY_KEY); session, table, Constants.PREFIX_PRIMARY_KEY);
int id = getObjectId(); int indexId = session.getDatabase().allocateObjectId();
try { try {
index = table.addIndex(session, indexName, id, index = table.addIndex(session, indexName, indexId,
indexColumns, indexType, true, null); indexColumns, indexType, true, null);
} finally { } finally {
getSchema().freeUniqueName(indexName); getSchema().freeUniqueName(indexName);
} }
} }
index.getIndexType().setBelongsToConstraint(true); index.getIndexType().setBelongsToConstraint(true);
int constraintId = getObjectId(); int id = getObjectId();
String name = generateConstraintName(table); String name = generateConstraintName(table);
ConstraintUnique pk = new ConstraintUnique(getSchema(), ConstraintUnique pk = new ConstraintUnique(getSchema(),
constraintId, name, table, true); id, name, table, true);
pk.setColumns(indexColumns); pk.setColumns(indexColumns);
pk.setIndex(index, true); pk.setIndex(index, true);
constraint = pk; constraint = pk;
...@@ -277,7 +276,7 @@ public class AlterTableAddConstraint extends SchemaCommand { ...@@ -277,7 +276,7 @@ public class AlterTableAddConstraint extends SchemaCommand {
} }
private Index createIndex(Table t, IndexColumn[] cols, boolean unique) { private Index createIndex(Table t, IndexColumn[] cols, boolean unique) {
int indexId = getObjectId(); int indexId = session.getDatabase().allocateObjectId();
IndexType indexType; IndexType indexType;
if (unique) { if (unique) {
// for unique constraints // for unique constraints
......
...@@ -103,7 +103,7 @@ public abstract class CommandWithColumns extends SchemaCommand { ...@@ -103,7 +103,7 @@ public abstract class CommandWithColumns extends SchemaCommand {
if (columns != null) { if (columns != null) {
for (Column c : columns) { for (Column c : columns) {
if (c.isAutoIncrement()) { if (c.isAutoIncrement()) {
int objId = getObjectId(); int objId = session.getDatabase().allocateObjectId();
c.convertAutoIncrementToSequence(session, getSchema(), objId, temporary); c.convertAutoIncrementToSequence(session, getSchema(), objId, temporary);
if (!Constants.CLUSTERING_DISABLED.equals(session.getDatabase().getCluster())) { if (!Constants.CLUSTERING_DISABLED.equals(session.getDatabase().getCluster())) {
throw DbException.getUnsupportedException("CLUSTERING && auto-increment columns"); throw DbException.getUnsupportedException("CLUSTERING && auto-increment columns");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论