Search in sources :

Example 1 with Parameter

use of com.wplatform.ddal.command.expression.Parameter in project jdbc-shards by wplatform.

the class ExecuteProcedure method setParameters.

private void setParameters() {
    Prepared prepared = procedure.getPrepared();
    ArrayList<Parameter> params = prepared.getParameters();
    for (int i = 0; params != null && i < params.size() && i < expressions.size(); i++) {
        Expression expr = expressions.get(i);
        Parameter p = params.get(i);
        p.setValue(expr.getValue(session));
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Prepared(com.wplatform.ddal.command.Prepared) Parameter(com.wplatform.ddal.command.expression.Parameter)

Example 2 with Parameter

use of com.wplatform.ddal.command.expression.Parameter in project jdbc-shards by wplatform.

the class Insert 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, len = expr.length; i < len; i++) {
                Expression e = expr[i];
                if (e != null) {
                    e = e.optimize(session);
                    if (e instanceof Parameter) {
                        Parameter p = (Parameter) e;
                        p.setColumn(columns[i]);
                    }
                    expr[i] = e;
                }
            }
        }
    } else {
        query.prepare();
        if (query.getColumnCount() != columns.length) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Parameter(com.wplatform.ddal.command.expression.Parameter)

Example 3 with Parameter

use of com.wplatform.ddal.command.expression.Parameter in project jdbc-shards by wplatform.

the class CommandContainer method recompileIfRequired.

private void recompileIfRequired() {
    if (prepared.needRecompile()) {
        // TODO test with 'always recompile'
        String sql = prepared.getSQL();
        ArrayList<Parameter> oldParams = prepared.getParameters();
        Parser parser = new Parser(session);
        prepared = parser.parse(sql);
        ArrayList<Parameter> newParams = prepared.getParameters();
        for (int i = 0, size = newParams.size(); i < size; i++) {
            Parameter old = oldParams.get(i);
            if (old.isValueSet()) {
                Value v = old.getValue(session);
                Parameter p = newParams.get(i);
                p.setValue(v);
            }
        }
        prepared.prepare();
    }
}
Also used : Value(com.wplatform.ddal.value.Value) Parameter(com.wplatform.ddal.command.expression.Parameter)

Example 4 with Parameter

use of com.wplatform.ddal.command.expression.Parameter in project jdbc-shards by wplatform.

the class MergeExecutor method merge.

private void merge(Row row) {
    TableMate table = castTableMate(prepared.getTable());
    Prepared update = prepared.getUpdate();
    Column[] columns = prepared.getColumns();
    Column[] keys = prepared.getKeys();
    ArrayList<Parameter> k = update.getParameters();
    for (int i = 0; i < columns.length; i++) {
        Column col = columns[i];
        Value v = row.getValue(col.getColumnId());
        Parameter p = k.get(i);
        p.setValue(v);
    }
    for (int i = 0; i < keys.length; i++) {
        Column col = keys[i];
        Value v = row.getValue(col.getColumnId());
        if (v == null) {
            throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, col.getSQL());
        }
        Parameter p = k.get(columns.length + i);
        p.setValue(v);
    }
    int count = update.update();
    if (count == 0) {
        try {
            table.validateConvertUpdateSequence(session, row);
            updateRow(table, row);
        } catch (DbException e) {
            throw e;
        }
    } else if (count != 1) {
        throw DbException.get(ErrorCode.DUPLICATE_KEY_1, table.getSQL());
    }
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Prepared(com.wplatform.ddal.command.Prepared) Value(com.wplatform.ddal.value.Value) Parameter(com.wplatform.ddal.command.expression.Parameter) TableMate(com.wplatform.ddal.dbobject.table.TableMate) DbException(com.wplatform.ddal.message.DbException)

Example 5 with Parameter

use of com.wplatform.ddal.command.expression.Parameter in project jdbc-shards by wplatform.

the class DefineCommandExecutor method execute.

/**
 * execute DDL use default sql translator
 *
 * @param nodes
 */
public void execute(TableNode[] nodes) {
    session.checkCanceled();
    List<JdbcWorker<Integer>> workers = New.arrayList(nodes.length);
    for (TableNode node : nodes) {
        String sql = doTranslate(node);
        List<Parameter> items = getPrepared().getParameters();
        List<Value> params = New.arrayList(items.size());
        for (Parameter parameter : items) {
            params.add(parameter.getParamValue());
        }
        workers.add(createUpdateWorker(node.getShardName(), sql, params));
    }
    addRuningJdbcWorkers(workers);
    try {
        // DDL statement returns nothing.
        if (workers.size() > 1) {
            // MILLISECONDS
            int queryTimeout = getQueryTimeout();
            if (queryTimeout > 0) {
                jdbcExecutor.invokeAll(workers, queryTimeout, TimeUnit.MILLISECONDS);
            } else {
                jdbcExecutor.invokeAll(workers);
            }
        } else if (workers.size() == 1) {
            workers.get(0).doWork();
        }
    } catch (InterruptedException e) {
        throw DbException.convert(e);
    } finally {
        removeRuningJdbcWorkers(workers);
        for (JdbcWorker<Integer> jdbcWorker : workers) {
            jdbcWorker.closeResource();
        }
    }
}
Also used : JdbcWorker(com.wplatform.ddal.excutor.JdbcWorker) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) Value(com.wplatform.ddal.value.Value) Parameter(com.wplatform.ddal.command.expression.Parameter)

Aggregations

Parameter (com.wplatform.ddal.command.expression.Parameter)8 Value (com.wplatform.ddal.value.Value)4 Prepared (com.wplatform.ddal.command.Prepared)3 Expression (com.wplatform.ddal.command.expression.Expression)2 Column (com.wplatform.ddal.dbobject.table.Column)2 TableMate (com.wplatform.ddal.dbobject.table.TableMate)1 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)1 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)1 DbException (com.wplatform.ddal.message.DbException)1