1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://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.Session; |
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 | |
32 | public CreateLinkedTable(Session session, Schema schema) { |
33 | super(session, schema); |
34 | } |
35 | |
36 | public void setTableName(String tableName) { |
37 | this.tableName = tableName; |
38 | } |
39 | |
40 | public void setDriver(String driver) { |
41 | this.driver = driver; |
42 | } |
43 | |
44 | public void setOriginalTable(String originalTable) { |
45 | this.originalTable = originalTable; |
46 | } |
47 | |
48 | public void setPassword(String password) { |
49 | this.password = password; |
50 | } |
51 | |
52 | public void setUrl(String url) { |
53 | this.url = url; |
54 | } |
55 | |
56 | public void setUser(String user) { |
57 | this.user = user; |
58 | } |
59 | |
60 | public void setIfNotExists(boolean ifNotExists) { |
61 | this.ifNotExists = ifNotExists; |
62 | } |
63 | |
64 | @Override |
65 | public int update() { |
66 | session.commit(true); |
67 | Database db = session.getDatabase(); |
68 | session.getUser().checkAdmin(); |
69 | if (getSchema().findTableOrView(session, tableName) != null) { |
70 | if (ifNotExists) { |
71 | return 0; |
72 | } |
73 | throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, |
74 | tableName); |
75 | } |
76 | int id = getObjectId(); |
77 | TableLink table = getSchema().createTableLink(id, tableName, driver, url, |
78 | user, password, originalSchema, originalTable, emitUpdates, force); |
79 | table.setTemporary(temporary); |
80 | table.setGlobalTemporary(globalTemporary); |
81 | table.setComment(comment); |
82 | table.setReadOnly(readOnly); |
83 | if (temporary && !globalTemporary) { |
84 | session.addLocalTempTable(table); |
85 | } else { |
86 | db.addSchemaObject(session, table); |
87 | } |
88 | return 0; |
89 | } |
90 | |
91 | public void setEmitUpdates(boolean emitUpdates) { |
92 | this.emitUpdates = emitUpdates; |
93 | } |
94 | |
95 | public void setComment(String comment) { |
96 | this.comment = comment; |
97 | } |
98 | |
99 | public void setForce(boolean force) { |
100 | this.force = force; |
101 | } |
102 | |
103 | public void setTemporary(boolean temp) { |
104 | this.temporary = temp; |
105 | } |
106 | |
107 | public void setGlobalTemporary(boolean globalTemp) { |
108 | this.globalTemporary = globalTemp; |
109 | } |
110 | |
111 | public void setReadOnly(boolean readOnly) { |
112 | this.readOnly = readOnly; |
113 | } |
114 | |
115 | public void setOriginalSchema(String originalSchema) { |
116 | this.originalSchema = originalSchema; |
117 | } |
118 | |
119 | @Override |
120 | public int getType() { |
121 | return CommandInterface.CREATE_LINKED_TABLE; |
122 | } |
123 | |
124 | } |