| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.table; |
| 7 |
|
| 8 |
import org.h2.command.ddl.CreateSynonymData; |
| 9 |
import org.h2.engine.SessionLocal; |
| 10 |
import org.h2.message.DbException; |
| 11 |
import org.h2.message.Trace; |
| 12 |
import org.h2.schema.Schema; |
| 13 |
import org.h2.schema.SchemaObject; |
| 14 |
import org.h2.util.ParserUtil; |
| 15 |
|
| 16 |
/** |
| 17 |
* Synonym for an existing table or view. All DML requests are forwarded to the backing table. |
| 18 |
* Adding indices to a synonym or altering the table is not supported. |
| 19 |
*/ |
| 20 |
public class TableSynonym extends SchemaObject { |
| 21 |
|
| 22 |
private CreateSynonymData data; |
| 23 |
|
| 24 |
/** |
| 25 |
* The table the synonym is created for. |
| 26 |
*/ |
| 27 |
private Table synonymFor; |
| 28 |
|
| 29 |
public TableSynonym(CreateSynonymData data) { |
| 30 |
super(data.schema, data.id, data.synonymName, Trace.TABLE); |
| 31 |
this.data = data; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @return the table this is a synonym for |
| 36 |
*/ |
| 37 |
public Table getSynonymFor() { |
| 38 |
return synonymFor; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Set (update) the data. |
| 43 |
* |
| 44 |
* @param data the new data |
| 45 |
*/ |
| 46 |
public void updateData(CreateSynonymData data) { |
| 47 |
this.data = data; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public int getType() { |
| 52 |
return SYNONYM; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public String getCreateSQLForCopy(Table table, String quotedName) { |
| 57 |
return synonymFor.getCreateSQLForCopy(table, quotedName); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void rename(String newName) { throw DbException.getUnsupportedException("SYNONYM"); } |
| 62 |
|
| 63 |
@Override |
| 64 |
public void removeChildrenAndResources(SessionLocal session) { |
| 65 |
synonymFor.removeSynonym(this); |
| 66 |
database.removeMeta(session, getId()); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public String getCreateSQL() { |
| 71 |
StringBuilder builder = new StringBuilder("CREATE SYNONYM "); |
| 72 |
getSQL(builder, DEFAULT_SQL_FLAGS).append(" FOR "); |
| 73 |
ParserUtil.quoteIdentifier(builder, data.synonymForSchema.getName(), DEFAULT_SQL_FLAGS).append('.'); |
| 74 |
ParserUtil.quoteIdentifier(builder, data.synonymFor, DEFAULT_SQL_FLAGS); |
| 75 |
return builder.toString(); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public String getDropSQL() { |
| 80 |
return getSQL(new StringBuilder("DROP SYNONYM "), DEFAULT_SQL_FLAGS).toString(); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void checkRename() { |
| 85 |
throw DbException.getUnsupportedException("SYNONYM"); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @return the table this synonym is for |
| 90 |
*/ |
| 91 |
public String getSynonymForName() { |
| 92 |
return data.synonymFor; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return the schema this synonym is for |
| 97 |
*/ |
| 98 |
public Schema getSynonymForSchema() { |
| 99 |
return data.synonymForSchema; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return true if this synonym currently points to a real table |
| 104 |
*/ |
| 105 |
public boolean isInvalid() { |
| 106 |
return synonymFor.isValid(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Update the table that this is a synonym for, to know about this synonym. |
| 111 |
*/ |
| 112 |
public void updateSynonymFor() { |
| 113 |
if (synonymFor != null) { |
| 114 |
synonymFor.removeSynonym(this); |
| 115 |
} |
| 116 |
synonymFor = data.synonymForSchema.getTableOrView(data.session, data.synonymFor); |
| 117 |
synonymFor.addSynonym(this); |
| 118 |
} |
| 119 |
|
| 120 |
} |