| 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.command.ddl; |
| 7 |
|
| 8 |
import org.h2.api.ErrorCode; |
| 9 |
import org.h2.command.CommandInterface; |
| 10 |
import org.h2.engine.Database; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.schema.Schema; |
| 14 |
import org.h2.table.TableLink; |
| 15 |
|
| 16 |
/** |
| 17 |
* This class represents the statement |
| 18 |
* CREATE LINKED TABLE |
| 19 |
*/ |
| 20 |
public class CreateLinkedTable extends SchemaCommand { |
| 21 |
|
| 22 |
private String tableName; |
| 23 |
private String driver, url, user, password, originalSchema, originalTable; |
| 24 |
private boolean ifNotExists; |
| 25 |
private String comment; |
| 26 |
private boolean emitUpdates; |
| 27 |
private boolean force; |
| 28 |
private boolean temporary; |
| 29 |
private boolean globalTemporary; |
| 30 |
private boolean readOnly; |
| 31 |
private int fetchSize; |
| 32 |
private boolean autocommit = true; |
| 33 |
|
| 34 |
public CreateLinkedTable(SessionLocal session, Schema schema) { |
| 35 |
super(session, schema); |
| 36 |
} |
| 37 |
|
| 38 |
public void setTableName(String tableName) { |
| 39 |
this.tableName = tableName; |
| 40 |
} |
| 41 |
|
| 42 |
public void setDriver(String driver) { |
| 43 |
this.driver = driver; |
| 44 |
} |
| 45 |
|
| 46 |
public void setOriginalTable(String originalTable) { |
| 47 |
this.originalTable = originalTable; |
| 48 |
} |
| 49 |
|
| 50 |
public void setPassword(String password) { |
| 51 |
this.password = password; |
| 52 |
} |
| 53 |
|
| 54 |
public void setUrl(String url) { |
| 55 |
this.url = url; |
| 56 |
} |
| 57 |
|
| 58 |
public void setUser(String user) { |
| 59 |
this.user = user; |
| 60 |
} |
| 61 |
|
| 62 |
public void setIfNotExists(boolean ifNotExists) { |
| 63 |
this.ifNotExists = ifNotExists; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Specify the number of rows fetched by the linked table command |
| 68 |
* |
| 69 |
* @param fetchSize to set |
| 70 |
*/ |
| 71 |
public void setFetchSize(int fetchSize) { |
| 72 |
this.fetchSize = fetchSize; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Specify if the autocommit mode is activated or not |
| 77 |
* |
| 78 |
* @param mode to set |
| 79 |
*/ |
| 80 |
public void setAutoCommit(boolean mode) { |
| 81 |
this.autocommit= mode; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public long update() { |
| 86 |
session.getUser().checkAdmin(); |
| 87 |
Database db = getDatabase(); |
| 88 |
if (getSchema().resolveTableOrView(session, tableName) != null) { |
| 89 |
if (ifNotExists) { |
| 90 |
return 0; |
| 91 |
} |
| 92 |
throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, |
| 93 |
tableName); |
| 94 |
} |
| 95 |
int id = getObjectId(); |
| 96 |
TableLink table = getSchema().createTableLink(id, tableName, driver, url, |
| 97 |
user, password, originalSchema, originalTable, emitUpdates, force); |
| 98 |
table.setTemporary(temporary); |
| 99 |
table.setGlobalTemporary(globalTemporary); |
| 100 |
table.setComment(comment); |
| 101 |
table.setReadOnly(readOnly); |
| 102 |
if (fetchSize > 0) { |
| 103 |
table.setFetchSize(fetchSize); |
| 104 |
} |
| 105 |
table.setAutoCommit(autocommit); |
| 106 |
if (temporary && !globalTemporary) { |
| 107 |
session.addLocalTempTable(table); |
| 108 |
} else { |
| 109 |
db.addSchemaObject(session, table); |
| 110 |
} |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
public void setEmitUpdates(boolean emitUpdates) { |
| 115 |
this.emitUpdates = emitUpdates; |
| 116 |
} |
| 117 |
|
| 118 |
public void setComment(String comment) { |
| 119 |
this.comment = comment; |
| 120 |
} |
| 121 |
|
| 122 |
public void setForce(boolean force) { |
| 123 |
this.force = force; |
| 124 |
} |
| 125 |
|
| 126 |
public void setTemporary(boolean temp) { |
| 127 |
this.temporary = temp; |
| 128 |
} |
| 129 |
|
| 130 |
public void setGlobalTemporary(boolean globalTemp) { |
| 131 |
this.globalTemporary = globalTemp; |
| 132 |
} |
| 133 |
|
| 134 |
public void setReadOnly(boolean readOnly) { |
| 135 |
this.readOnly = readOnly; |
| 136 |
} |
| 137 |
|
| 138 |
public void setOriginalSchema(String originalSchema) { |
| 139 |
this.originalSchema = originalSchema; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public int getType() { |
| 144 |
return CommandInterface.CREATE_LINKED_TABLE; |
| 145 |
} |
| 146 |
|
| 147 |
} |