| 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 |
|
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.command.Parser; |
| 12 |
import org.h2.command.ddl.AlterDomain; |
| 13 |
import org.h2.command.query.AllColumnsForPlan; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.expression.Expression; |
| 16 |
import org.h2.expression.ExpressionVisitor; |
| 17 |
import org.h2.index.Index; |
| 18 |
import org.h2.message.DbException; |
| 19 |
import org.h2.result.Row; |
| 20 |
import org.h2.schema.Domain; |
| 21 |
import org.h2.schema.Schema; |
| 22 |
import org.h2.table.Column; |
| 23 |
import org.h2.table.PlanItem; |
| 24 |
import org.h2.table.Table; |
| 25 |
import org.h2.table.TableFilter; |
| 26 |
import org.h2.util.StringUtils; |
| 27 |
import org.h2.value.Value; |
| 28 |
import org.h2.value.ValueNull; |
| 29 |
|
| 30 |
/** |
| 31 |
* A domain constraint. |
| 32 |
*/ |
| 33 |
public class ConstraintDomain extends Constraint { |
| 34 |
|
| 35 |
private Domain domain; |
| 36 |
|
| 37 |
private Expression expr; |
| 38 |
|
| 39 |
private DomainColumnResolver resolver; |
| 40 |
|
| 41 |
public ConstraintDomain(Schema schema, int id, String name, Domain domain) { |
| 42 |
super(schema, id, name, null); |
| 43 |
this.domain = domain; |
| 44 |
resolver = new DomainColumnResolver(domain.getDataType()); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public Type getConstraintType() { |
| 49 |
return Constraint.Type.DOMAIN; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns the domain of this constraint. |
| 54 |
* |
| 55 |
* @return the domain |
| 56 |
*/ |
| 57 |
public Domain getDomain() { |
| 58 |
return domain; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Set the expression. |
| 63 |
* |
| 64 |
* @param session the session |
| 65 |
* @param expr the expression |
| 66 |
*/ |
| 67 |
public void setExpression(SessionLocal session, Expression expr) { |
| 68 |
expr.mapColumns(resolver, 0, Expression.MAP_INITIAL); |
| 69 |
expr = expr.optimize(session); |
| 70 |
// check if the column is mapped |
| 71 |
synchronized (this) { |
| 72 |
resolver.setValue(ValueNull.INSTANCE); |
| 73 |
expr.getValue(session); |
| 74 |
} |
| 75 |
this.expr = expr; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public String getCreateSQLWithoutIndexes() { |
| 80 |
return getCreateSQL(); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public String getCreateSQL() { |
| 85 |
StringBuilder builder = new StringBuilder("ALTER DOMAIN "); |
| 86 |
domain.getSQL(builder, DEFAULT_SQL_FLAGS).append(" ADD CONSTRAINT "); |
| 87 |
getSQL(builder, DEFAULT_SQL_FLAGS); |
| 88 |
if (comment != null) { |
| 89 |
builder.append(" COMMENT "); |
| 90 |
StringUtils.quoteStringSQL(builder, comment); |
| 91 |
} |
| 92 |
builder.append(" CHECK"); |
| 93 |
expr.getEnclosedSQL(builder, DEFAULT_SQL_FLAGS).append(" NOCHECK"); |
| 94 |
return builder.toString(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void removeChildrenAndResources(SessionLocal session) { |
| 99 |
domain.removeConstraint(this); |
| 100 |
database.removeMeta(session, getId()); |
| 101 |
domain = null; |
| 102 |
expr = null; |
| 103 |
invalidate(); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void checkRow(SessionLocal session, Table t, Row oldRow, Row newRow) { |
| 108 |
throw DbException.getInternalError(toString()); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Check the specified value. |
| 113 |
* |
| 114 |
* @param session |
| 115 |
* the session |
| 116 |
* @param value |
| 117 |
* the value to check |
| 118 |
*/ |
| 119 |
public void check(SessionLocal session, Value value) { |
| 120 |
Value v; |
| 121 |
synchronized (this) { |
| 122 |
resolver.setValue(value); |
| 123 |
v = expr.getValue(session); |
| 124 |
} |
| 125 |
// Both TRUE and NULL are OK |
| 126 |
if (v.isFalse()) { |
| 127 |
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, expr.getTraceSQL()); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get the check constraint expression for this column. |
| 133 |
* |
| 134 |
* @param session the session |
| 135 |
* @param columnName the column name |
| 136 |
* @return the expression |
| 137 |
*/ |
| 138 |
public Expression getCheckConstraint(SessionLocal session, String columnName) { |
| 139 |
String sql; |
| 140 |
if (columnName != null) { |
| 141 |
synchronized (this) { |
| 142 |
try { |
| 143 |
resolver.setColumnName(columnName); |
| 144 |
sql = expr.getSQL(DEFAULT_SQL_FLAGS); |
| 145 |
} finally { |
| 146 |
resolver.resetColumnName(); |
| 147 |
} |
| 148 |
} |
| 149 |
return new Parser(session).parseExpression(sql); |
| 150 |
} else { |
| 151 |
synchronized (this) { |
| 152 |
sql = expr.getSQL(DEFAULT_SQL_FLAGS); |
| 153 |
} |
| 154 |
return new Parser(session).parseDomainConstraintExpression(sql); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public boolean usesIndex(Index index) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public void setIndexOwner(Index index) { |
| 165 |
throw DbException.getInternalError(toString()); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public HashSet<Column> getReferencedColumns(Table table) { |
| 170 |
HashSet<Column> columns = new HashSet<>(); |
| 171 |
expr.isEverything(ExpressionVisitor.getColumnsVisitor(columns, table)); |
| 172 |
return columns; |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public Expression getExpression() { |
| 177 |
return expr; |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public boolean isBefore() { |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
public void checkExistingData(SessionLocal session) { |
| 187 |
if (session.getDatabase().isStarting()) { |
| 188 |
// don't check at startup |
| 189 |
return; |
| 190 |
} |
| 191 |
new CheckExistingData(session, domain); |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void rebuild() { |
| 196 |
// nothing to do |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 201 |
return expr.isEverything(visitor); |
| 202 |
} |
| 203 |
|
| 204 |
private class CheckExistingData { |
| 205 |
|
| 206 |
private final SessionLocal session; |
| 207 |
|
| 208 |
CheckExistingData(SessionLocal session, Domain domain) { |
| 209 |
this.session = session; |
| 210 |
checkDomain(null, domain); |
| 211 |
} |
| 212 |
|
| 213 |
private boolean checkColumn(Domain domain, Column targetColumn) { |
| 214 |
Table table = targetColumn.getTable(); |
| 215 |
TableFilter filter = new TableFilter(session, table, null, true, null, 0, null); |
| 216 |
TableFilter[] filters = { filter }; |
| 217 |
PlanItem item = filter.getBestPlanItem(session, filters, 0, new AllColumnsForPlan(filters)); |
| 218 |
filter.setPlanItem(item); |
| 219 |
filter.prepare(); |
| 220 |
filter.startQuery(session); |
| 221 |
filter.reset(); |
| 222 |
while (filter.next()) { |
| 223 |
check(session, filter.getValue(targetColumn)); |
| 224 |
} |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
private boolean checkDomain(Domain domain, Domain targetDomain) { |
| 229 |
AlterDomain.forAllDependencies(session, targetDomain, this::checkColumn, this::checkDomain, false); |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
} |