| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.constraint; |
| 7 |
|
| 8 |
import java.util.HashSet; |
| 9 |
import org.h2.api.ErrorCode; |
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.Expression; |
| 12 |
import org.h2.expression.ExpressionVisitor; |
| 13 |
import org.h2.index.Index; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.result.ResultInterface; |
| 16 |
import org.h2.result.Row; |
| 17 |
import org.h2.schema.Schema; |
| 18 |
import org.h2.table.Column; |
| 19 |
import org.h2.table.Table; |
| 20 |
import org.h2.table.TableFilter; |
| 21 |
import org.h2.util.StringUtils; |
| 22 |
import org.h2.value.Value; |
| 23 |
|
| 24 |
/** |
| 25 |
* A check constraint. |
| 26 |
*/ |
| 27 |
public class ConstraintCheck extends Constraint { |
| 28 |
|
| 29 |
private TableFilter filter; |
| 30 |
private Expression expr; |
| 31 |
|
| 32 |
public ConstraintCheck(Schema schema, int id, String name, Table table) { |
| 33 |
super(schema, id, name, table); |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public Type getConstraintType() { |
| 38 |
return Constraint.Type.CHECK; |
| 39 |
} |
| 40 |
|
| 41 |
public void setTableFilter(TableFilter filter) { |
| 42 |
this.filter = filter; |
| 43 |
} |
| 44 |
|
| 45 |
public void setExpression(Expression expr) { |
| 46 |
this.expr = expr; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public String getCreateSQLForCopy(Table forTable, String quotedName) { |
| 51 |
StringBuilder buff = new StringBuilder("ALTER TABLE "); |
| 52 |
forTable.getSQL(buff, DEFAULT_SQL_FLAGS).append(" ADD CONSTRAINT "); |
| 53 |
if (forTable.isHidden()) { |
| 54 |
buff.append("IF NOT EXISTS "); |
| 55 |
} |
| 56 |
buff.append(quotedName); |
| 57 |
if (comment != null) { |
| 58 |
buff.append(" COMMENT "); |
| 59 |
StringUtils.quoteStringSQL(buff, comment); |
| 60 |
} |
| 61 |
buff.append(" CHECK"); |
| 62 |
expr.getEnclosedSQL(buff, DEFAULT_SQL_FLAGS).append(" NOCHECK"); |
| 63 |
return buff.toString(); |
| 64 |
} |
| 65 |
|
| 66 |
private String getShortDescription() { |
| 67 |
StringBuilder builder = new StringBuilder().append(getName()).append(": "); |
| 68 |
expr.getTraceSQL(); |
| 69 |
return builder.toString(); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public String getCreateSQLWithoutIndexes() { |
| 74 |
return getCreateSQL(); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public String getCreateSQL() { |
| 79 |
return getCreateSQLForCopy(table, getSQL(DEFAULT_SQL_FLAGS)); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void removeChildrenAndResources(SessionLocal session) { |
| 84 |
table.removeConstraint(this); |
| 85 |
database.removeMeta(session, getId()); |
| 86 |
filter = null; |
| 87 |
expr = null; |
| 88 |
table = null; |
| 89 |
invalidate(); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void checkRow(SessionLocal session, Table t, Row oldRow, Row newRow) { |
| 94 |
if (newRow == null) { |
| 95 |
return; |
| 96 |
} |
| 97 |
boolean b; |
| 98 |
try { |
| 99 |
Value v; |
| 100 |
synchronized (this) { |
| 101 |
filter.set(newRow); |
| 102 |
v = expr.getValue(session); |
| 103 |
} |
| 104 |
// Both TRUE and NULL are ok |
| 105 |
b = v.isFalse(); |
| 106 |
} catch (DbException ex) { |
| 107 |
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, ex, getShortDescription()); |
| 108 |
} |
| 109 |
if (b) { |
| 110 |
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getShortDescription()); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public boolean usesIndex(Index index) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void setIndexOwner(Index index) { |
| 121 |
throw DbException.getInternalError(toString()); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public HashSet<Column> getReferencedColumns(Table table) { |
| 126 |
HashSet<Column> columns = new HashSet<>(); |
| 127 |
expr.isEverything(ExpressionVisitor.getColumnsVisitor(columns, table)); |
| 128 |
return columns; |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public Expression getExpression() { |
| 133 |
return expr; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public boolean isBefore() { |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void checkExistingData(SessionLocal session) { |
| 143 |
if (session.getDatabase().isStarting()) { |
| 144 |
// don't check at startup |
| 145 |
return; |
| 146 |
} |
| 147 |
StringBuilder builder = new StringBuilder().append("SELECT NULL FROM "); |
| 148 |
filter.getTable().getSQL(builder, DEFAULT_SQL_FLAGS).append(" WHERE NOT "); |
| 149 |
expr.getSQL(builder, DEFAULT_SQL_FLAGS, Expression.AUTO_PARENTHESES); |
| 150 |
String sql = builder.toString(); |
| 151 |
ResultInterface r = session.prepare(sql).query(1); |
| 152 |
if (r.next()) { |
| 153 |
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getName()); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void rebuild() { |
| 159 |
// nothing to do |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 164 |
return expr.isEverything(visitor); |
| 165 |
} |
| 166 |
|
| 167 |
} |