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.constraint;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.command.Parser;
011: import org.h2.engine.Session;
012: import org.h2.index.Index;
013: import org.h2.result.Row;
014: import org.h2.schema.Schema;
015: import org.h2.table.Column;
016: import org.h2.table.IndexColumn;
017: import org.h2.table.Table;
018: import org.h2.util.StringUtils;
019:
020: /**
021: * A unique constraint. This object always backed by a unique index.
022: */
023: public class ConstraintUnique extends Constraint {
024:
025: private Index index;
026: private boolean indexOwner;
027: private IndexColumn[] columns;
028: private boolean primaryKey;
029:
030: public ConstraintUnique(Schema schema, int id, String name,
031: Table table, boolean primaryKey) {
032: super (schema, id, name, table);
033: this .primaryKey = primaryKey;
034: }
035:
036: public String getConstraintType() {
037: return primaryKey ? Constraint.PRIMARY_KEY : Constraint.UNIQUE;
038: }
039:
040: public String getCreateSQLForCopy(Table table, String quotedName) {
041: return getCreateSQLForCopy(table, quotedName, true);
042: }
043:
044: private String getCreateSQLForCopy(Table table, String quotedName,
045: boolean internalIndex) {
046: StringBuffer buff = new StringBuffer();
047: buff.append("ALTER TABLE ");
048: buff.append(table.getSQL());
049: buff.append(" ADD CONSTRAINT ");
050: buff.append(quotedName);
051: if (comment != null) {
052: buff.append(" COMMENT ");
053: buff.append(StringUtils.quoteStringSQL(comment));
054: }
055: buff.append(' ');
056: buff.append(getTypeName());
057: buff.append('(');
058: for (int i = 0; i < columns.length; i++) {
059: if (i > 0) {
060: buff.append(", ");
061: }
062: buff.append(Parser.quoteIdentifier(columns[i].column
063: .getName()));
064: }
065: buff.append(')');
066: if (internalIndex && indexOwner && table == this .table) {
067: buff.append(" INDEX ");
068: buff.append(index.getSQL());
069: }
070: return buff.toString();
071: }
072:
073: private String getTypeName() {
074: if (primaryKey) {
075: return "PRIMARY KEY";
076: } else {
077: return "UNIQUE";
078: }
079: }
080:
081: public String getShortDescription() {
082: StringBuffer buff = new StringBuffer();
083: buff.append(getName());
084: buff.append(": ");
085: buff.append(getTypeName());
086: buff.append('(');
087: for (int i = 0; i < columns.length; i++) {
088: if (i > 0) {
089: buff.append(", ");
090: }
091: buff.append(Parser.quoteIdentifier(columns[i].column
092: .getName()));
093: }
094: buff.append(")");
095: return buff.toString();
096: }
097:
098: public String getCreateSQLWithoutIndexes() {
099: return getCreateSQLForCopy(table, getSQL(), false);
100: }
101:
102: public String getCreateSQL() {
103: return getCreateSQLForCopy(table, getSQL());
104: }
105:
106: public void setColumns(IndexColumn[] columns) {
107: this .columns = columns;
108: }
109:
110: public IndexColumn[] getColumns() {
111: return columns;
112: }
113:
114: public void setIndex(Index index, boolean isOwner) {
115: this .index = index;
116: this .indexOwner = isOwner;
117: }
118:
119: public void removeChildrenAndResources(Session session)
120: throws SQLException {
121: table.removeConstraint(this );
122: if (indexOwner) {
123: table.removeIndexOrTransferOwnership(session, index);
124: }
125: database.removeMeta(session, getId());
126: index = null;
127: columns = null;
128: table = null;
129: invalidate();
130: }
131:
132: public void checkRow(Session session, Table t, Row oldRow,
133: Row newRow) {
134: // unique index check is enough
135: }
136:
137: public boolean usesIndex(Index idx) {
138: return idx == index;
139: }
140:
141: public void setIndexOwner(Index index) {
142: indexOwner = true;
143: }
144:
145: public boolean containsColumn(Column col) {
146: for (int i = 0; i < columns.length; i++) {
147: if (columns[i].column == col) {
148: return true;
149: }
150: }
151: return false;
152: }
153:
154: public boolean isBefore() {
155: return true;
156: }
157:
158: public void checkExistingData(Session session) throws SQLException {
159: // no need to check: when creating the unique index any problems are found
160: }
161:
162: }
|