File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\constraint\ConstraintCheck.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.constraint;
8
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import org.h2.constant.ErrorCode;
12 import org.h2.engine.Session;
13 import org.h2.expression.Expression;
14 import org.h2.expression.ExpressionVisitor;
15 import org.h2.index.Index;
16 import org.h2.message.DbException;
17 import org.h2.result.ResultInterface;
18 import org.h2.result.Row;
19 import org.h2.schema.Schema;
20 import org.h2.table.Column;
21 import org.h2.table.Table;
22 import org.h2.table.TableFilter;
23 import org.h2.util.New;
24 import org.h2.util.StringUtils;
25
26 /**
27 * A check constraint.
28 */
29 public class ConstraintCheck extends Constraint {
30
31 private TableFilter filter;
32 private Expression expr;
33
34 public ConstraintCheck(Schema schema, int id, String name, Table table) {
35 super(schema, id, name, table);
36 }
37
38 public String getConstraintType() {
39 return Constraint.CHECK;
40 }
41
42 public void setTableFilter(TableFilter filter) {
43 this.filter = filter;
44 }
45
46 public void setExpression(Expression expr) {
47 this.expr = expr;
48 }
49
50 public String getCreateSQLForCopy(Table forTable, String quotedName) {
51 StringBuilder buff = new StringBuilder("ALTER TABLE ");
52 buff.append(forTable.getSQL()).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 ").append(StringUtils.quoteStringSQL(comment));
59 }
60 buff.append(" CHECK").append(StringUtils.enclose(expr.getSQL())).append(" NOCHECK");
61 return buff.toString();
62 }
63
64 private String getShortDescription() {
65 return getName() + ": " + expr.getSQL();
66 }
67
68 public String getCreateSQLWithoutIndexes() {
69 return getCreateSQL();
70 }
71
72 public String getCreateSQL() {
73 return getCreateSQLForCopy(table, getSQL());
74 }
75
76 public void removeChildrenAndResources(Session session) {
77 table.removeConstraint(this);
78 database.removeMeta(session, getId());
79 filter = null;
80 expr = null;
81 table = null;
82 invalidate();
83 }
84
85 public void checkRow(Session session, Table t, Row oldRow, Row newRow) {
86 if (newRow == null) {
87 return;
88 }
89 filter.set(newRow);
90 // Both TRUE and NULL are ok
91 if (Boolean.FALSE.equals(expr.getValue(session).getBoolean())) {
92 throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getShortDescription());
93 }
94 }
95
96 public boolean usesIndex(Index index) {
97 return false;
98 }
99
100 public void setIndexOwner(Index index) {
101 DbException.throwInternalError();
102 }
103
104 public HashSet<Column> getReferencedColumns(Table table) {
105 HashSet<Column> columns = New.hashSet();
106 expr.isEverything(ExpressionVisitor.getColumnsVisitor(columns));
107 for (Iterator<Column> it = columns.iterator(); it.hasNext();) {
108 if (it.next().getTable() != table) {
109 it.remove();
110 }
111 }
112 return columns;
113 }
114
115 public Expression getExpression() {
116 return expr;
117 }
118
119 public boolean isBefore() {
120 return true;
121 }
122
123 public void checkExistingData(Session session) {
124 if (session.getDatabase().isStarting()) {
125 // don't check at startup
126 return;
127 }
128 String sql = "SELECT 1 FROM " + filter.getTable().getSQL() + " WHERE NOT(" + expr.getSQL() + ")";
129 ResultInterface r = session.prepare(sql).query(1);
130 if (r.next()) {
131 throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getName());
132 }
133 }
134
135 public Index getUniqueIndex() {
136 return null;
137 }
138
139 public void rebuild() {
140 // nothing to do
141 }
142
143 public boolean isEverything(ExpressionVisitor visitor) {
144 return expr.isEverything(visitor);
145 }
146
147 }
148