001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.command.ddl;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.constant.ErrorCode;
011: import org.h2.engine.Constants;
012: import org.h2.engine.Database;
013: import org.h2.engine.Right;
014: import org.h2.engine.Session;
015: import org.h2.index.IndexType;
016: import org.h2.message.Message;
017: import org.h2.schema.Schema;
018: import org.h2.table.IndexColumn;
019: import org.h2.table.Table;
020:
021: /**
022: * This class represents the statement
023: * CREATE INDEX
024: */
025: public class CreateIndex extends SchemaCommand {
026:
027: private String tableName;
028: private String indexName;
029: private IndexColumn[] indexColumns;
030: private boolean primaryKey, unique, hash;
031: private boolean ifNotExists;
032: private String comment;
033:
034: public CreateIndex(Session session, Schema schema) {
035: super (session, schema);
036: }
037:
038: public void setIfNotExists(boolean ifNotExists) {
039: this .ifNotExists = ifNotExists;
040: }
041:
042: public void setTableName(String tableName) {
043: this .tableName = tableName;
044: }
045:
046: public void setIndexName(String indexName) {
047: this .indexName = indexName;
048: }
049:
050: public void setIndexColumns(IndexColumn[] columns) {
051: this .indexColumns = columns;
052: }
053:
054: public boolean getPrimaryKey() {
055: return primaryKey;
056: }
057:
058: public IndexColumn[] getIndexColumns() {
059: return indexColumns;
060: }
061:
062: public int update() throws SQLException {
063: // TODO cancel: may support for index creation
064: session.commit(true);
065: Database db = session.getDatabase();
066: boolean persistent = db.isPersistent();
067: Table table = getSchema().getTableOrView(session, tableName);
068: session.getUser().checkRight(table, Right.ALL);
069: table.lock(session, true, true);
070: if (!table.isPersistent()) {
071: persistent = false;
072: }
073: int id = getObjectId(true, false);
074: if (primaryKey) {
075: indexName = table.getSchema().getUniqueIndexName(table,
076: Constants.PREFIX_PRIMARY_KEY);
077: } else if (indexName == null) {
078: indexName = table.getSchema().getUniqueIndexName(table,
079: Constants.PREFIX_INDEX);
080: }
081: if (getSchema().findIndex(indexName) != null) {
082: if (ifNotExists) {
083: return 0;
084: }
085: throw Message.getSQLException(
086: ErrorCode.INDEX_ALREADY_EXISTS_1, indexName);
087: }
088: IndexType indexType;
089: if (primaryKey) {
090: if (table.findPrimaryKey() != null) {
091: throw Message
092: .getSQLException(ErrorCode.SECOND_PRIMARY_KEY);
093: }
094: indexType = IndexType.createPrimaryKey(persistent, hash);
095: } else if (unique) {
096: indexType = IndexType.createUnique(persistent, hash);
097: } else {
098: indexType = IndexType.createNonUnique(persistent);
099: }
100: IndexColumn.mapColumns(indexColumns, table);
101: table.addIndex(session, indexName, id, indexColumns, indexType,
102: headPos, comment);
103: return 0;
104: }
105:
106: public void setPrimaryKey(boolean b) {
107: this .primaryKey = b;
108: }
109:
110: public void setUnique(boolean b) {
111: this .unique = b;
112: }
113:
114: public void setHash(boolean b) {
115: this .hash = b;
116: }
117:
118: public boolean getHash() {
119: return hash;
120: }
121:
122: public void setComment(String comment) {
123: this.comment = comment;
124: }
125:
126: }
|