KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > CreateIndex


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Database;
10 import org.h2.engine.Right;
11 import org.h2.engine.Session;
12 import org.h2.index.IndexType;
13 import org.h2.message.Message;
14 import org.h2.schema.Schema;
15 import org.h2.table.Column;
16 import org.h2.table.Table;
17
18 /**
19  * @author Thomas
20  */

21 public class CreateIndex extends SchemaCommand {
22
23     private String JavaDoc tableName;
24     private String JavaDoc indexName;
25     private String JavaDoc[] columnNames;
26     private boolean primaryKey, unique, hash;
27     private boolean ifNotExists;
28     private String JavaDoc comment;
29
30     public CreateIndex(Session session, Schema schema) {
31         super(session, schema);
32     }
33
34     public void setIfNotExists(boolean ifNotExists) {
35         this.ifNotExists = ifNotExists;
36     }
37
38     public void setTableName(String JavaDoc tableName) {
39         this.tableName = tableName;
40     }
41
42     public void setIndexName(String JavaDoc indexName) {
43         this.indexName = indexName;
44     }
45
46     public void setColumnNames(String JavaDoc[] columnNames) {
47         this.columnNames = columnNames;
48     }
49
50     public boolean getPrimaryKey() {
51         return primaryKey;
52     }
53
54     public String JavaDoc[] getColumnNames() {
55         return columnNames;
56     }
57
58     public int update() throws SQLException JavaDoc {
59         // TODO cancel: may support for index creation
60
session.commit();
61         Database db = session.getDatabase();
62         boolean persistent = db.isPersistent();
63         Table table = getSchema().getTableOrView(session, tableName);
64         session.getUser().checkRight(table, Right.ALL);
65         table.lock(session, true);
66         if(!table.isPersistent()) {
67             persistent = false;
68         }
69         int id = getObjectId(true, false);
70         if(indexName == null) {
71             indexName = getSchema().getUniqueIndexName("INDEX_");
72         }
73         if(getSchema().findIndex(indexName) != null) {
74             if (ifNotExists) {
75                 return 0;
76             }
77             throw Message.getSQLException(Message.INDEX_ALREADY_EXISTS_1, indexName);
78         }
79         IndexType indexType;
80         if(primaryKey) {
81             if(table.findPrimaryKey() != null) {
82                 throw Message.getSQLException(Message.SECOND_PRIMARY_KEY);
83             }
84             indexType = IndexType.createPrimaryKey(persistent, hash);
85         } else if(unique) {
86             indexType = IndexType.createUnique(persistent, hash);
87         } else {
88             indexType = IndexType.createNonUnique(persistent);
89         }
90         Column[] columns = table.getColumns(columnNames);
91         table.addIndex(session, indexName, id, columns, indexType, headPos, comment);
92         return 0;
93     }
94
95     public void setPrimaryKey(boolean b) {
96         this.primaryKey = b;
97     }
98
99     public void setUnique(boolean b) {
100         this.unique = b;
101     }
102
103     public void setHash(boolean b) {
104         this.hash = b;
105     }
106
107     public boolean getHash() {
108         return hash;
109     }
110
111     public void setComment(String JavaDoc comment) {
112         this.comment = comment;
113     }
114
115 }
116
Popular Tags