Search in sources :

Example 26 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class Explain method query.

@Override
public ResultInterface query(int maxrows) {
    Column column = new Column("PLAN", Value.STRING);
    Database db = session.getDatabase();
    ExpressionColumn expr = new ExpressionColumn(db, column);
    Expression[] expressions = { expr };
    result = new LocalResult(session, expressions, 1);
    if (maxrows >= 0) {
        String plan;
        if (executeCommand) {
            if (command.isQuery()) {
                command.query(maxrows);
            } else {
                command.update();
            }
            plan = command.getPlanSQL();
        } else {
            plan = command.getPlanSQL();
        }
        add(plan);
    }
    result.done();
    return result;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Column(com.wplatform.ddal.dbobject.table.Column) ExpressionColumn(com.wplatform.ddal.command.expression.ExpressionColumn) Expression(com.wplatform.ddal.command.expression.Expression) Database(com.wplatform.ddal.engine.Database) ValueString(com.wplatform.ddal.value.ValueString) ExpressionColumn(com.wplatform.ddal.command.expression.ExpressionColumn)

Example 27 with Column

use of com.wplatform.ddal.dbobject.table.Column 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)

Example 28 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class Expression method getExpressionColumns.

/**
     * Extracts expression columns from ValueArray
     *
     * @param session the current session
     * @param value   the value to extract columns from
     * @return array of expression columns
     */
static Expression[] getExpressionColumns(Session session, ValueArray value) {
    Value[] list = value.getList();
    ExpressionColumn[] expr = new ExpressionColumn[list.length];
    for (int i = 0, len = list.length; i < len; i++) {
        Value v = list[i];
        Column col = new Column("C" + (i + 1), v.getType(), v.getPrecision(), v.getScale(), v.getDisplaySize());
        expr[i] = new ExpressionColumn(session.getDatabase(), col);
    }
    return expr;
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Value(com.wplatform.ddal.value.Value)

Example 29 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class Merge method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("MERGE INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(')');
    if (keys != null) {
        buff.append(" KEY(");
        buff.resetCount();
        for (Column c : keys) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL());
        }
        buff.append(')');
    }
    buff.append('\n');
    if (list.size() > 0) {
        buff.append("VALUES ");
        int row = 0;
        for (Expression[] expr : list) {
            if (row++ > 0) {
                buff.append(", ");
            }
            buff.append('(');
            buff.resetCount();
            for (Expression e : expr) {
                buff.appendExceptFirst(", ");
                if (e == null) {
                    buff.append("DEFAULT");
                } else {
                    buff.append(e.getSQL());
                }
            }
            buff.append(')');
        }
    } else {
        buff.append(query.getPlanSQL());
    }
    return buff.toString();
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 30 with Column

use of com.wplatform.ddal.dbobject.table.Column 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)

Aggregations

Column (com.wplatform.ddal.dbobject.table.Column)38 Expression (com.wplatform.ddal.command.expression.Expression)15 Value (com.wplatform.ddal.value.Value)14 TableMate (com.wplatform.ddal.dbobject.table.TableMate)12 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)8 DbException (com.wplatform.ddal.message.DbException)7 Row (com.wplatform.ddal.result.Row)6 SearchRow (com.wplatform.ddal.result.SearchRow)6 Query (com.wplatform.ddal.command.dml.Query)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)4 ResultInterface (com.wplatform.ddal.result.ResultInterface)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 ValueExpression (com.wplatform.ddal.command.expression.ValueExpression)3 Index (com.wplatform.ddal.dbobject.index.Index)3 Table (com.wplatform.ddal.dbobject.table.Table)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Database (com.wplatform.ddal.engine.Database)3 List (java.util.List)3 Prepared (com.wplatform.ddal.command.Prepared)2