Debian Codegraph @

src/main/org/h2/command/ddl/CreateIndex.java

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