File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\CreateIndex.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import org.h2.command.CommandInterface;
10 import org.h2.constant.ErrorCode;
11 import org.h2.engine.Constants;
12 import org.h2.engine.Database;
13 import org.h2.engine.Right;
14 import org.h2.engine.Session;
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 boolean primaryKey, unique, hash;
31 private boolean ifNotExists;
32 private String comment;
33
34 public CreateIndex(Session session, Schema schema) {
35 super(session, schema);
36 }
37
38 public void setIfNotExists(boolean ifNotExists) {
39 this.ifNotExists = ifNotExists;
40 }
41
42 public void setTableName(String tableName) {
43 this.tableName = tableName;
44 }
45
46 public void setIndexName(String indexName) {
47 this.indexName = indexName;
48 }
49
50 public void setIndexColumns(IndexColumn[] columns) {
51 this.indexColumns = columns;
52 }
53
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, table, Constants.PREFIX_PRIMARY_KEY);
76 } else {
77 indexName = table.getSchema().getUniqueIndexName(session, table, Constants.PREFIX_INDEX);
78 }
79 }
80 IndexType indexType;
81 if (primaryKey) {
82 if (table.findPrimaryKey() != null) {
83 throw DbException.get(ErrorCode.SECOND_PRIMARY_KEY);
84 }
85 indexType = IndexType.createPrimaryKey(persistent, hash);
86 } else if (unique) {
87 indexType = IndexType.createUnique(persistent, hash);
88 } else {
89 indexType = IndexType.createNonUnique(persistent, hash);
90 }
91 IndexColumn.mapColumns(indexColumns, table);
92 table.addIndex(session, indexName, id, indexColumns, indexType, create, comment);
93 return 0;
94 }
95
96 public void setPrimaryKey(boolean b) {
97 this.primaryKey = b;
98 }
99
100 public void setUnique(boolean b) {
101 this.unique = b;
102 }
103
104 public void setHash(boolean b) {
105 this.hash = b;
106 }
107
108 public void setComment(String comment) {
109 this.comment = comment;
110 }
111
112 public int getType() {
113 return CommandInterface.CREATE_INDEX;
114 }
115
116 }
117