Search in sources :

Example 21 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class InsertExecutor method executeUpdate.

@Override
public int executeUpdate() {
    TableMate table = castTableMate(prepared.getTable());
    table.check();
    session.getUser().checkRight(table, Right.INSERT);
    prepared.setCurrentRowNumber(0);
    rowNumber = 0;
    affectRows = 0;
    ArrayList<Expression[]> list = prepared.getList();
    Column[] columns = prepared.getColumns();
    int listSize = list.size();
    if (listSize > 0) {
        int columnLen = columns.length;
        for (int x = 0; x < listSize; x++) {
            Row newRow = table.getTemplateRow();
            Expression[] expr = list.get(x);
            prepared.setCurrentRowNumber(x + 1);
            for (int i = 0; i < columnLen; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    e = e.optimize(session);
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw prepared.setRow(ex, x, Prepared.getSQL(expr));
                    }
                }
            }
            rowNumber++;
            table.validateConvertUpdateSequence(session, newRow);
            addNewRow(newRow);
        }
    } else {
        Query query = prepared.getQuery();
        if (prepared.isInsertFromSelect()) {
            query.query(0, this);
        } else {
            ResultInterface rows = query.query(0);
            while (rows.next()) {
                Value[] r = rows.currentRow();
                addRow(r);
            }
            rows.close();
        }
    }
    flushNewRows();
    return affectRows;
}
Also used : ResultInterface(com.wplatform.ddal.result.ResultInterface) Query(com.wplatform.ddal.command.dml.Query) DbException(com.wplatform.ddal.message.DbException) Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow)

Example 22 with Value

use of com.wplatform.ddal.value.Value 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 23 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class MergeExecutor method executeUpdate.

@Override
public int executeUpdate() {
    Column[] columns = prepared.getColumns();
    TableMate table = castTableMate(prepared.getTable());
    ArrayList<Expression[]> list = prepared.getList();
    table.check();
    int count;
    session.getUser().checkRight(table, Right.INSERT);
    session.getUser().checkRight(table, Right.UPDATE);
    prepared.setCurrentRowNumber(0);
    if (list.size() > 0) {
        count = 0;
        for (int x = 0, size = list.size(); x < size; x++) {
            prepared.setCurrentRowNumber(x + 1);
            Expression[] expr = list.get(x);
            Row newRow = table.getTemplateRow();
            for (int i = 0, len = columns.length; i < len; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw prepared.setRow(ex, count, Prepared.getSQL(expr));
                    }
                }
            }
            merge(newRow);
            count++;
        }
    } else {
        Query query = prepared.getQuery();
        ResultInterface rows = query.query(0);
        count = 0;
        while (rows.next()) {
            count++;
            Value[] r = rows.currentRow();
            Row newRow = table.getTemplateRow();
            prepared.setCurrentRowNumber(count);
            for (int j = 0; j < columns.length; j++) {
                Column c = columns[j];
                int index = c.getColumnId();
                try {
                    Value v = c.convert(r[j]);
                    newRow.setValue(index, v);
                } catch (DbException ex) {
                    throw prepared.setRow(ex, count, Prepared.getSQL(r));
                }
            }
            merge(newRow);
        }
        rows.close();
    }
    return count;
}
Also used : ResultInterface(com.wplatform.ddal.result.ResultInterface) Query(com.wplatform.ddal.command.dml.Query) DbException(com.wplatform.ddal.message.DbException) Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow)

Example 24 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class UpdateExecutor method doTranslate.

@Override
protected List<Value> doTranslate(TableNode node, SearchRow row, StatementBuilder buff) {
    ArrayList<Value> params = New.arrayList();
    TableFilter tableFilter = prepared.getTableFilter();
    String forTable = node.getCompositeObjectName();
    List<Column> columns = prepared.getColumns();
    Expression condition = prepared.getCondition();
    Expression limitExpr = prepared.getLimitExpr();
    buff.append("UPDATE ");
    buff.append(identifier(forTable)).append(" SET ");
    for (int i = 0, size = columns.size(); i < size; i++) {
        Column c = columns.get(i);
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL()).append(" = ");
        Value v = row.getValue(i);
        buff.appendExceptFirst(", ");
        if (v == null) {
            buff.append("DEFAULT");
        } else if (isNull(v)) {
            buff.append("NULL");
        } else {
            buff.append('?');
            params.add(v);
        }
    }
    if (condition != null) {
        condition.exportParameters(tableFilter, params);
        buff.append(" WHERE ").append(StringUtils.unEnclose(condition.getSQL()));
    }
    if (limitExpr != null) {
        limitExpr.exportParameters(tableFilter, params);
        buff.append(" LIMIT ").append(StringUtils.unEnclose(limitExpr.getSQL()));
    }
    return params;
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value)

Example 25 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class JdbcConnection method createClob.

/**
     * Create a new empty Clob object.
     *
     * @return the object
     */
@Override
public Clob createClob() throws SQLException {
    try {
        int id = getNextId(TraceObject.CLOB);
        debugCodeAssign("Clob", TraceObject.CLOB, id, "createClob()");
        checkClosedForWrite();
        try {
            Value v = ValueLobDb.createTempClob(new InputStreamReader(new ByteArrayInputStream(Utils.EMPTY_BYTES)), 0);
            session.addTemporaryLob(v);
            return new JdbcClob(this, v, id);
        } finally {
            afterWriting();
        }
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Value(com.wplatform.ddal.value.Value) Savepoint(java.sql.Savepoint) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) DbException(com.wplatform.ddal.message.DbException)

Aggregations

Value (com.wplatform.ddal.value.Value)84 Expression (com.wplatform.ddal.command.expression.Expression)14 Column (com.wplatform.ddal.dbobject.table.Column)14 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)13 DbException (com.wplatform.ddal.message.DbException)9 SQLException (java.sql.SQLException)8 Row (com.wplatform.ddal.result.Row)7 SearchRow (com.wplatform.ddal.result.SearchRow)7 PreparedStatement (java.sql.PreparedStatement)7 TableMate (com.wplatform.ddal.dbobject.table.TableMate)6 LocalResult (com.wplatform.ddal.result.LocalResult)6 ResultInterface (com.wplatform.ddal.result.ResultInterface)5 Connection (java.sql.Connection)5 Parameter (com.wplatform.ddal.command.expression.Parameter)4 List (java.util.List)4 DataSource (javax.sql.DataSource)4 Query (com.wplatform.ddal.command.dml.Query)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)3