01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.constant.ErrorCode;
11: import org.h2.engine.Database;
12: import org.h2.engine.Session;
13: import org.h2.message.Message;
14: import org.h2.schema.Schema;
15: import org.h2.table.TableLink;
16:
17: /**
18: * This class represents the statement
19: * CREATE LINKED TABLE
20: */
21: public class CreateLinkedTable extends SchemaCommand {
22:
23: private String tableName;
24: private String driver, url, user, password, originalTable;
25: private boolean ifNotExists;
26: private String comment;
27: private boolean emitUpdates;
28: private boolean force;
29:
30: public CreateLinkedTable(Session session, Schema schema) {
31: super (session, schema);
32: }
33:
34: public void setTableName(String tableName) {
35: this .tableName = tableName;
36: }
37:
38: public void setDriver(String driver) {
39: this .driver = driver;
40: }
41:
42: public void setOriginalTable(String originalTable) {
43: this .originalTable = originalTable;
44: }
45:
46: public void setPassword(String password) {
47: this .password = password;
48: }
49:
50: public void setUrl(String url) {
51: this .url = url;
52: }
53:
54: public void setUser(String user) {
55: this .user = user;
56: }
57:
58: public void setIfNotExists(boolean ifNotExists) {
59: this .ifNotExists = ifNotExists;
60: }
61:
62: public int update() throws SQLException {
63: session.commit(true);
64: Database db = session.getDatabase();
65: session.getUser().checkAdmin();
66: if (getSchema().findTableOrView(session, tableName) != null) {
67: if (ifNotExists) {
68: return 0;
69: }
70: throw Message
71: .getSQLException(
72: ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1,
73: tableName);
74: }
75: int id = getObjectId(false, true);
76: TableLink table = getSchema().createTableLink(id, tableName,
77: driver, url, user, password, originalTable,
78: emitUpdates, force);
79: table.setComment(comment);
80: db.addSchemaObject(session, table);
81: return 0;
82: }
83:
84: public void setEmitUpdates(boolean emitUpdates) {
85: this .emitUpdates = emitUpdates;
86: }
87:
88: public void setComment(String comment) {
89: this .comment = comment;
90: }
91:
92: public void setForce(boolean force) {
93: this.force = force;
94: }
95:
96: }
|