1 5 package org.h2.constraint; 6 7 import java.sql.SQLException ; 8 9 import org.h2.command.Parser; 10 import org.h2.engine.Session; 11 import org.h2.index.Index; 12 import org.h2.result.Row; 13 import org.h2.schema.Schema; 14 import org.h2.table.Column; 15 import org.h2.table.Table; 16 import org.h2.util.StringUtils; 17 18 21 22 public class ConstraintUnique extends Constraint { 23 24 private Index index; 25 private boolean indexOwner; 26 private Column[] columns; 27 28 public ConstraintUnique(Schema schema, int id, String name, Table table) { 29 super(schema, id, name, table); 30 } 31 32 public String getConstraintType() { 33 return Constraint.UNIQUE; 34 } 35 36 public String getCreateSQLForCopy(Table table, String quotedName) { 37 return getCreateSQLForCopy(table, quotedName, true); 38 } 39 40 public String getCreateSQLForCopy(Table table, String quotedName, boolean internalIndex) { 41 StringBuffer buff = new StringBuffer (); 42 buff.append("ALTER TABLE "); 43 buff.append(table.getSQL()); 44 buff.append(" ADD CONSTRAINT "); 45 buff.append(quotedName); 46 if(comment != null) { 47 buff.append(" COMMENT "); 48 buff.append(StringUtils.quoteStringSQL(comment)); 49 } 50 buff.append(" UNIQUE("); 51 for (int i = 0; i < columns.length; i++) { 52 if (i > 0) { 53 buff.append(", "); 54 } 55 buff.append(Parser.quoteIdentifier(columns[i].getName())); 56 } 57 buff.append(")"); 58 if(internalIndex && indexOwner && table == this.table) { 59 buff.append(" INDEX "); 60 buff.append(index.getSQL()); 61 } 62 return buff.toString(); 63 } 64 65 public String getShortDescription() { 66 StringBuffer buff = new StringBuffer (); 67 buff.append(getName()); 68 buff.append(": "); 69 buff.append("UNIQUE("); 70 for (int i = 0; i < columns.length; i++) { 71 if (i > 0) { 72 buff.append(", "); 73 } 74 buff.append(Parser.quoteIdentifier(columns[i].getName())); 75 } 76 buff.append(")"); 77 return buff.toString(); 78 } 79 80 public String getCreateSQLWithoutIndexes() { 81 return getCreateSQLForCopy(table, getSQL(), false); 82 } 83 84 public String getCreateSQL() { 85 return getCreateSQLForCopy(table, getSQL()); 86 } 87 88 public void setColumns(Column[] columns) { 89 this.columns = columns; 90 } 91 92 public Column[] getColumns() { 93 return columns; 94 } 95 96 public void setIndex(Index index, boolean isOwner) { 97 this.index = index; 98 this.indexOwner = isOwner; 99 } 100 101 public void removeChildrenAndResources(Session session) throws SQLException { 102 table.removeConstraint(this); 103 if(indexOwner) { 104 database.removeSchemaObject(session, index); 105 } 106 index = null; 107 columns = null; 108 table = null; 109 invalidate(); 110 } 111 112 public void checkRow(Session session, Table t, Row oldRow, Row newRow) { 113 } 115 116 public boolean usesIndex(Index ind) { 117 return ind == index; 118 } 119 120 public boolean containsColumn(Column col) { 121 for(int i=0; i<columns.length; i++) { 122 if(columns[i] == col) { 123 return true; 124 } 125 } 126 return false; 127 } 128 129 public boolean isBefore() { 130 return true; 131 } 132 133 } 134 | Popular Tags |