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.Constants; |
11 | import org.h2.engine.Database; |
12 | import org.h2.engine.Right; |
13 | import org.h2.engine.Session; |
14 | import org.h2.index.IndexType; |
15 | import org.h2.message.DbException; |
16 | import org.h2.schema.Schema; |
17 | import org.h2.table.IndexColumn; |
18 | import org.h2.table.Table; |
19 | |
20 | /** |
21 | * This class represents the statement |
22 | * CREATE INDEX |
23 | */ |
24 | public class CreateIndex extends SchemaCommand { |
25 | |
26 | private String tableName; |
27 | private String indexName; |
28 | private IndexColumn[] indexColumns; |
29 | private boolean primaryKey, unique, hash, spatial; |
30 | private boolean ifNotExists; |
31 | private String comment; |
32 | |
33 | public CreateIndex(Session session, Schema schema) { |
34 | super(session, schema); |
35 | } |
36 | |
37 | public void setIfNotExists(boolean ifNotExists) { |
38 | this.ifNotExists = ifNotExists; |
39 | } |
40 | |
41 | public void setTableName(String tableName) { |
42 | this.tableName = tableName; |
43 | } |
44 | |
45 | public void setIndexName(String indexName) { |
46 | this.indexName = indexName; |
47 | } |
48 | |
49 | public void setIndexColumns(IndexColumn[] columns) { |
50 | this.indexColumns = columns; |
51 | } |
52 | |
53 | @Override |
54 | public int update() { |
55 | if (!transactional) { |
56 | session.commit(true); |
57 | } |
58 | Database db = session.getDatabase(); |
59 | boolean persistent = db.isPersistent(); |
60 | Table table = getSchema().getTableOrView(session, tableName); |
61 | if (getSchema().findIndex(session, indexName) != null) { |
62 | if (ifNotExists) { |
63 | return 0; |
64 | } |
65 | throw DbException.get(ErrorCode.INDEX_ALREADY_EXISTS_1, indexName); |
66 | } |
67 | session.getUser().checkRight(table, Right.ALL); |
68 | table.lock(session, true, true); |
69 | if (!table.isPersistIndexes()) { |
70 | persistent = false; |
71 | } |
72 | int id = getObjectId(); |
73 | if (indexName == null) { |
74 | if (primaryKey) { |
75 | indexName = table.getSchema().getUniqueIndexName(session, |
76 | table, Constants.PREFIX_PRIMARY_KEY); |
77 | } else { |
78 | indexName = table.getSchema().getUniqueIndexName(session, |
79 | table, Constants.PREFIX_INDEX); |
80 | } |
81 | } |
82 | IndexType indexType; |
83 | if (primaryKey) { |
84 | if (table.findPrimaryKey() != null) { |
85 | throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY); |
86 | } |
87 | indexType = IndexType.createPrimaryKey(persistent, hash); |
88 | } else if (unique) { |
89 | indexType = IndexType.createUnique(persistent, hash); |
90 | } else { |
91 | indexType = IndexType.createNonUnique(persistent, hash, spatial); |
92 | } |
93 | IndexColumn.mapColumns(indexColumns, table); |
94 | table.addIndex(session, indexName, id, indexColumns, indexType, create, |
95 | comment); |
96 | return 0; |
97 | } |
98 | |
99 | public void setPrimaryKey(boolean b) { |
100 | this.primaryKey = b; |
101 | } |
102 | |
103 | public void setUnique(boolean b) { |
104 | this.unique = b; |
105 | } |
106 | |
107 | public void setHash(boolean b) { |
108 | this.hash = b; |
109 | } |
110 | |
111 | public void setSpatial(boolean b) { |
112 | this.spatial = b; |
113 | } |
114 | |
115 | public void setComment(String comment) { |
116 | this.comment = comment; |
117 | } |
118 | |
119 | @Override |
120 | public int getType() { |
121 | return CommandInterface.CREATE_INDEX; |
122 | } |
123 | |
124 | } |