提交 433056a5 authored 作者: Niklas Mehner's avatar Niklas Mehner

Update synonym implementation

* Close connections in test
* Call updateMeta in CreateSynonym to make sure changes get persisted
* Drop synonym when the backing is table is dropped to fix ticket #556
上级 5831e75e
...@@ -84,6 +84,7 @@ public class CreateSynonym extends SchemaCommand { ...@@ -84,6 +84,7 @@ public class CreateSynonym extends SchemaCommand {
table.updateData(data); table.updateData(data);
table.setComment(comment); table.setComment(comment);
table.setModified(); table.setModified();
db.updateMeta(session, table);
} else { } else {
data.id = getObjectId(); data.id = getObjectId();
table = getSchema().createSynonym(data); table = getSchema().createSynonym(data);
...@@ -91,6 +92,7 @@ public class CreateSynonym extends SchemaCommand { ...@@ -91,6 +92,7 @@ public class CreateSynonym extends SchemaCommand {
db.addSchemaObject(session, table); db.addSchemaObject(session, table);
} }
table.updateSynonymFor();
return 0; return 0;
} }
......
...@@ -379,6 +379,13 @@ public abstract class AbstractTable extends SchemaObjectBase { ...@@ -379,6 +379,13 @@ public abstract class AbstractTable extends SchemaObjectBase {
*/ */
public abstract void removeView(TableView view); public abstract void removeView(TableView view);
/**
* Remove the given synonym from the list.
*
* @param synonym the synonym to remove
*/
public abstract void removeSynonym(TableSynonym synonym);
/** /**
* Remove the given constraint from the list. * Remove the given constraint from the list.
* *
...@@ -407,6 +414,13 @@ public abstract class AbstractTable extends SchemaObjectBase { ...@@ -407,6 +414,13 @@ public abstract class AbstractTable extends SchemaObjectBase {
*/ */
public abstract void addView(TableView view); public abstract void addView(TableView view);
/**
* Add a synonym to this table.
*
* @param synonym the synonym to add
*/
public abstract void addSynonym(TableSynonym synonym);
/** /**
* Add a constraint to the table. * Add a constraint to the table.
* *
......
...@@ -72,6 +72,7 @@ public abstract class Table extends AbstractTable { ...@@ -72,6 +72,7 @@ public abstract class Table extends AbstractTable {
private ArrayList<Constraint> constraints; private ArrayList<Constraint> constraints;
private ArrayList<Sequence> sequences; private ArrayList<Sequence> sequences;
private ArrayList<TableView> views; private ArrayList<TableView> views;
private ArrayList<TableSynonym> synonyms;
private boolean checkForeignKeyConstraints = true; private boolean checkForeignKeyConstraints = true;
private boolean onCommitDrop, onCommitTruncate; private boolean onCommitDrop, onCommitTruncate;
private volatile Row nullRow; private volatile Row nullRow;
...@@ -249,6 +250,9 @@ public abstract class Table extends AbstractTable { ...@@ -249,6 +250,9 @@ public abstract class Table extends AbstractTable {
if (views != null) { if (views != null) {
children.addAll(views); children.addAll(views);
} }
if (synonyms != null) {
children.addAll(synonyms);
}
ArrayList<Right> rights = database.getAllRights(); ArrayList<Right> rights = database.getAllRights();
for (Right right : rights) { for (Right right : rights) {
if (right.getGrantedObject() == this) { if (right.getGrantedObject() == this) {
...@@ -379,6 +383,11 @@ public abstract class Table extends AbstractTable { ...@@ -379,6 +383,11 @@ public abstract class Table extends AbstractTable {
views.remove(0); views.remove(0);
database.removeSchemaObject(session, view); database.removeSchemaObject(session, view);
} }
while (synonyms != null && synonyms.size() > 0) {
TableSynonym synonym = synonyms.get(0);
synonyms.remove(0);
database.removeSchemaObject(session, synonym);
}
while (triggers != null && triggers.size() > 0) { while (triggers != null && triggers.size() > 0) {
TriggerObject trigger = triggers.get(0); TriggerObject trigger = triggers.get(0);
triggers.remove(0); triggers.remove(0);
...@@ -702,6 +711,16 @@ public abstract class Table extends AbstractTable { ...@@ -702,6 +711,16 @@ public abstract class Table extends AbstractTable {
remove(views, view); remove(views, view);
} }
/**
* Remove the given view from the list.
*
* @param synonym the synonym to remove
*/
@Override
public void removeSynonym(TableSynonym synonym) {
remove(synonyms, synonym);
}
/** /**
* Remove the given constraint from the list. * Remove the given constraint from the list.
* *
...@@ -742,6 +761,16 @@ public abstract class Table extends AbstractTable { ...@@ -742,6 +761,16 @@ public abstract class Table extends AbstractTable {
views = add(views, view); views = add(views, view);
} }
/**
* Add a synonym to this table.
*
* @param synonym the synonym to add
*/
@Override
public void addSynonym(TableSynonym synonym) {
synonyms = add(synonyms, synonym);
}
/** /**
* Add a constraint to the table. * Add a constraint to the table.
* *
......
...@@ -29,6 +29,8 @@ public class TableSynonym extends AbstractTable { ...@@ -29,6 +29,8 @@ public class TableSynonym extends AbstractTable {
private CreateSynonymData data; private CreateSynonymData data;
private AbstractTable synonymFor;
public TableSynonym(CreateSynonymData data) { public TableSynonym(CreateSynonymData data) {
initSchemaObjectBase(data.schema, data.id, data.synonymName, Trace.TABLE); initSchemaObjectBase(data.schema, data.id, data.synonymName, Trace.TABLE);
this.data = data; this.data = data;
...@@ -39,7 +41,7 @@ public class TableSynonym extends AbstractTable { ...@@ -39,7 +41,7 @@ public class TableSynonym extends AbstractTable {
} }
private AbstractTable getSynonymFor() { private AbstractTable getSynonymFor() {
return data.synonymForSchema.getTableOrView(data.session, data.synonymFor); return synonymFor;
} }
@Override @Override
...@@ -55,7 +57,7 @@ public class TableSynonym extends AbstractTable { ...@@ -55,7 +57,7 @@ public class TableSynonym extends AbstractTable {
@Override @Override
public int getType() { public int getType() {
return getSynonymFor().getType(); return TABLE_OR_VIEW;
} }
@Override @Override
...@@ -265,6 +267,7 @@ public class TableSynonym extends AbstractTable { ...@@ -265,6 +267,7 @@ public class TableSynonym extends AbstractTable {
@Override @Override
public void removeChildrenAndResources(Session session) { public void removeChildrenAndResources(Session session) {
synonymFor.removeSynonym(this);
database.removeMeta(session, getId()); database.removeMeta(session, getId());
} }
...@@ -411,6 +414,11 @@ public class TableSynonym extends AbstractTable { ...@@ -411,6 +414,11 @@ public class TableSynonym extends AbstractTable {
getSynonymFor().removeView(view); getSynonymFor().removeView(view);
} }
@Override
public void removeSynonym(TableSynonym synonym) {
throw DbException.getUnsupportedException("SYNONYM");
}
@Override @Override
public void removeConstraint(Constraint constraint) { public void removeConstraint(Constraint constraint) {
getSynonymFor().removeConstraint(constraint); getSynonymFor().removeConstraint(constraint);
...@@ -426,6 +434,11 @@ public class TableSynonym extends AbstractTable { ...@@ -426,6 +434,11 @@ public class TableSynonym extends AbstractTable {
getSynonymFor().addView(view); getSynonymFor().addView(view);
} }
@Override
public void addSynonym(TableSynonym synonym) {
throw DbException.getUnsupportedException("SYNONYM");
}
@Override @Override
public void addConstraint(Constraint constraint) { public void addConstraint(Constraint constraint) {
getSynonymFor().addConstraint(constraint); getSynonymFor().addConstraint(constraint);
...@@ -471,9 +484,18 @@ public class TableSynonym extends AbstractTable { ...@@ -471,9 +484,18 @@ public class TableSynonym extends AbstractTable {
getSynonymFor().fireAfterRow(session, oldRow, newRow, rollback); getSynonymFor().fireAfterRow(session, oldRow, newRow, rollback);
} }
public void updateSynonymFor() {
if (synonymFor != null) {
synonymFor.removeSynonym(this);
}
synonymFor = data.synonymForSchema.getTableOrView(data.session, data.synonymFor);
synonymFor.addSynonym(this);
}
@Override @Override
public boolean isGlobalTemporary() { public boolean isGlobalTemporary() {
return getSynonymFor().isGlobalTemporary(); return getSynonymFor().isGlobalTemporary();
} }
} }
...@@ -84,14 +84,12 @@ public class TestSynonymForTable extends TestBase { ...@@ -84,14 +84,12 @@ public class TestSynonymForTable extends TestBase {
// Backing table does not exist anymore. // Backing table does not exist anymore.
assertThrows(JdbcSQLException.class, stat).execute("SELECT id FROM testsynonym"); assertThrows(JdbcSQLException.class, stat).execute("SELECT id FROM testsynonym");
// Meta data should show INVALID // Synonym should be dropped as well
ResultSet synonyms = conn.createStatement().executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYNONYMS WHERE SYNONYM_NAME='TESTSYNONYM'"); ResultSet synonyms = conn.createStatement().executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYNONYMS WHERE SYNONYM_NAME='TESTSYNONYM'");
assertTrue(synonyms.next()); assertFalse(synonyms.next());
assertEquals("TESTSYNONYM", synonyms.getString("SYNONYM_NAME"));
assertEquals("INVALID", synonyms.getString("STATUS"));
conn.close(); conn.close();
// Reopening should work with invalid synonym // Reopening should work with dropped synonym
Connection conn2 = getConnection("synonym"); Connection conn2 = getConnection("synonym");
assertThrows(JdbcSQLException.class, stat).execute("SELECT id FROM testsynonym"); assertThrows(JdbcSQLException.class, stat).execute("SELECT id FROM testsynonym");
conn2.close(); conn2.close();
...@@ -112,6 +110,7 @@ public class TestSynonymForTable extends TestBase { ...@@ -112,6 +110,7 @@ public class TestSynonymForTable extends TestBase {
// Without "if exists" the command should fail if the synonym does not exist. // Without "if exists" the command should fail if the synonym does not exist.
assertThrows(JdbcSQLException.class, stat).execute("DROP SYNONYM testsynonym"); assertThrows(JdbcSQLException.class, stat).execute("DROP SYNONYM testsynonym");
conn.close();
} }
private void testSynonymInDifferentSchema() throws SQLException { private void testSynonymInDifferentSchema() throws SQLException {
...@@ -124,6 +123,7 @@ public class TestSynonymForTable extends TestBase { ...@@ -124,6 +123,7 @@ public class TestSynonymForTable extends TestBase {
stat.execute("CREATE OR REPLACE SYNONYM testsynonym FOR s1.backingtable"); stat.execute("CREATE OR REPLACE SYNONYM testsynonym FOR s1.backingtable");
stat.execute("INSERT INTO s1.backingtable VALUES(15)"); stat.execute("INSERT INTO s1.backingtable VALUES(15)");
assertSynonymContains(conn, 15); assertSynonymContains(conn, 15);
conn.close();
} }
private void testCreateOrReplaceExistingTable() throws SQLException { private void testCreateOrReplaceExistingTable() throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论