class ConstraintCheck extends Constraint {

    private IExpression.Evaluator exprEvaluator;
    private IExpression expr;

    public ConstraintCheck(Schema schema, int id, String name, Table table) {
        super(schema, id, name, table);
    }

    @Override
    public String getConstraintType() {
        return Constraint.CHECK;
    }

    public void setExpressionEvaluator(IExpression.Evaluator exprEvaluator) {
        this.exprEvaluator = exprEvaluator;
    }

    public void setExpression(IExpression expr) {
        this.expr = expr;
    }

    private String getShortDescription() {
        return getName() + ": " + expr.getSQL();
    }

    @Override
    public String getCreateSQLWithoutIndexes() {
        return getCreateSQL();
    }

    @Override
    public String getCreateSQL() {<FILL_FUNCTION_BODY>}

    @Override
    public void removeChildrenAndResources(ServerSession session, DbObjectLock lock) {
        table.removeConstraint(this);
    }

    @Override
    public void checkRow(ServerSession session, Table t, Row oldRow, Row newRow) {
        if (newRow == null) {
            return;
        }
        Value v;
        try {
            synchronized (this) {
                // 这里要同步，多个事务会把exprEvaluator的值修改
                v = exprEvaluator.getExpressionValue(session, expr, newRow);
            }
        } catch (DbException ex) {
            throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, ex, getShortDescription());
        }
        // Both TRUE and NULL are ok
        if (v.isFalse()) {
            throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getShortDescription());
        }
    }

    @Override
    public boolean usesIndex(Index index) {
        return false;
    }

    @Override
    public void setIndexOwner(Index index) {
        DbException.throwInternalError();
    }

    @Override
    public HashSet<Column> getReferencedColumns(Table table) {
        HashSet<Column> columns = new HashSet<>();
        expr.getColumns(columns);
        for (Iterator<Column> it = columns.iterator(); it.hasNext();) {
            if (it.next().getTable() != table) {
                it.remove();
            }
        }
        return columns;
    }

    public IExpression getExpression() {
        return expr;
    }

    @Override
    public boolean isBefore() {
        return true;
    }

    @Override
    public void checkExistingData(ServerSession session) {
        if (session.getDatabase().isStarting()) {
            // don't check at startup
            return;
        }
        String sql = "SELECT 1 FROM " + table.getSQL() + " WHERE NOT(" + expr.getSQL() + ")";
        Result r = session.executeNestedQueryLocal(sql);
        if (r.next()) {
            throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getName());
        }
    }

    @Override
    public Index getUniqueIndex() {
        return null;
    }

    @Override
    public void rebuild() {
        // nothing to do
    }

    @Override
    public void getDependencies(Set<DbObject> dependencies) {
        expr.getDependencies(dependencies);
    }
}

        StringBuilder buff = new StringBuilder("ALTER TABLE ");
        buff.append(table.getSQL()).append(" ADD CONSTRAINT ");
        if (table.isHidden()) {
            buff.append("IF NOT EXISTS ");
        }
        buff.append(getSQL());
        if (comment != null) {
            buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
        }
        buff.append(" CHECK").append(StringUtils.enclose(expr.getSQL())).append(" NOCHECK");
        return buff.toString();
    
<methods>public abstract void checkExistingData(com.lealone.db.session.ServerSession) ,public abstract void checkRow(com.lealone.db.session.ServerSession, com.lealone.db.table.Table, com.lealone.db.result.Row, com.lealone.db.result.Row) ,public int compareTo(com.lealone.db.constraint.Constraint) ,public abstract java.lang.String getConstraintType() ,public abstract java.lang.String getCreateSQLWithoutIndexes() ,public void getDependencies(Set<com.lealone.db.DbObject>) ,public com.lealone.db.table.Table getRefTable() ,public abstract HashSet<com.lealone.db.table.Column> getReferencedColumns(com.lealone.db.table.Table) ,public com.lealone.db.table.Table getTable() ,public com.lealone.db.DbObjectType getType() ,public abstract com.lealone.db.index.Index getUniqueIndex() ,public abstract boolean isBefore() ,public boolean isHidden() ,public abstract void rebuild() ,public abstract void setIndexOwner(com.lealone.db.index.Index) ,public abstract boolean usesIndex(com.lealone.db.index.Index) <variables>public static final java.lang.String CHECK,public static final java.lang.String PRIMARY_KEY,public static final java.lang.String REFERENTIAL,public static final java.lang.String UNIQUE,protected com.lealone.db.table.Table table