1 package com.quadcap.sql; 2 3 40 41 import java.util.Vector ; 42 43 import java.io.IOException ; 44 45 import java.sql.SQLException ; 46 47 import com.quadcap.util.Debug; 48 49 54 55 public class StmtCreateIndex implements Stmt { 56 String indexName; 57 String tableName; 58 Vector columns; 59 boolean unique; 60 boolean text; 61 62 public StmtCreateIndex() {} 63 64 public StmtCreateIndex(String indexName, String tableName, Vector columns, 65 boolean unique, boolean text) { 66 this.indexName = indexName; 67 this.tableName = tableName; 68 this.columns = columns; 69 this.unique = unique; 70 this.text = text; 71 } 72 73 public void execute(Session session) throws IOException , SQLException { 74 session.getTableWriteLock("#Schema"); 75 session.getTableWriteLock(tableName); 76 Database db = session.getDatabase(); 77 Relation r = db.getRelation(tableName); 78 if (r == null) { 79 throw new SQLException ("No such table: " + tableName, "42000"); 80 } 81 if (!(r instanceof Table)) { 82 throw new SQLException ("Not a base table: " + tableName, "42000"); 83 } 84 if (db.getTableForIndex(indexName) != null) { 85 throw new SQLException ("Index: " + indexName + " exists", "42000"); 86 } 87 Table t = (Table)r; 88 Constraint c = null; 89 if (unique) { 90 c = new UniqueConstraint(indexName, columns); 91 } else { 94 c = new NonUniqueIndexConstraint(indexName, columns); 95 } 96 c.setGlobal(true); 97 c.setTable(t); 98 t.nameConstraint(c); 99 session.doStep(new AddConstraint(session, t, c, true)); 100 } 101 } 102 103 | Popular Tags |