Search in sources :

Example 11 with Index

use of com.wplatform.ddal.dbobject.index.Index in project jdbc-shards by wplatform.

the class Merge method prepare.

@Override
public void prepare() {
    if (columns == null) {
        if (list.size() > 0 && list.get(0).length == 0) {
            // special case where table is used as a sequence
            columns = new Column[0];
        } else {
            columns = table.getColumns();
        }
    }
    if (list.size() > 0) {
        for (Expression[] expr : list) {
            if (expr.length != columns.length) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            for (int i = 0; i < expr.length; i++) {
                Expression e = expr[i];
                if (e != null) {
                    expr[i] = e.optimize(session);
                }
            }
        }
    } else {
        query.prepare();
        if (query.getColumnCount() != columns.length) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    if (keys == null) {
        Index idx = table.getPrimaryKey();
        if (idx == null) {
            throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, "PRIMARY KEY");
        }
        keys = idx.getColumns();
    }
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(table.getSQL()).append(" SET ");
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL()).append("=?");
    }
    buff.append(" WHERE ");
    buff.resetCount();
    for (Column c : keys) {
        buff.appendExceptFirst(" AND ");
        buff.append(c.getSQL()).append("=?");
    }
    String sql = buff.toString();
    update = session.prepare(sql);
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Column(com.wplatform.ddal.dbobject.table.Column) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Index(com.wplatform.ddal.dbobject.index.Index)

Example 12 with Index

use of com.wplatform.ddal.dbobject.index.Index in project jdbc-shards by wplatform.

the class Replace method prepare.

@Override
public void prepare() {
    if (columns == null) {
        if (list.size() > 0 && list.get(0).length == 0) {
            // special case where table is used as a sequence
            columns = new Column[0];
        } else {
            columns = table.getColumns();
        }
    }
    if (list.size() > 0) {
        for (Expression[] expr : list) {
            if (expr.length != columns.length) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            for (int i = 0; i < expr.length; i++) {
                Expression e = expr[i];
                if (e != null) {
                    expr[i] = e.optimize(session);
                }
            }
        }
    } else {
        query.prepare();
        if (query.getColumnCount() != columns.length) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    if (keys == null) {
        Index idx = table.getPrimaryKey();
        if (idx == null) {
            throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, "PRIMARY KEY");
        }
        keys = idx.getColumns();
    }
    // INSERT
    for (Column key : keys) {
        boolean found = false;
        for (Column column : columns) {
            if (column.getColumnId() == key.getColumnId()) {
                found = true;
                break;
            }
        }
        if (!found) {
            return;
        }
    }
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(table.getSQL()).append(" SET ");
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL()).append("=?");
    }
    buff.append(" WHERE ");
    buff.resetCount();
    for (Column c : keys) {
        buff.appendExceptFirst(" AND ");
        buff.append(c.getSQL()).append("=?");
    }
    String sql = buff.toString();
    update = session.prepare(sql);
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Column(com.wplatform.ddal.dbobject.table.Column) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Index(com.wplatform.ddal.dbobject.index.Index)

Aggregations

Index (com.wplatform.ddal.dbobject.index.Index)12 Table (com.wplatform.ddal.dbobject.table.Table)4 Column (com.wplatform.ddal.dbobject.table.Column)3 Expression (com.wplatform.ddal.command.expression.Expression)2 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)2 Comment (com.wplatform.ddal.dbobject.Comment)1 DbObject (com.wplatform.ddal.dbobject.DbObject)1 FunctionAlias (com.wplatform.ddal.dbobject.FunctionAlias)1 Right (com.wplatform.ddal.dbobject.Right)1 IndexMate (com.wplatform.ddal.dbobject.index.IndexMate)1 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)1 TableMate (com.wplatform.ddal.dbobject.table.TableMate)1 RuleColumn (com.wplatform.ddal.dispatch.rule.RuleColumn)1