Search in sources :

Example 71 with Value

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

the class Session method setVariable.

/**
 * Set the value of the given variable for this session.
 *
 * @param name the name of the variable (may not be null)
 * @param value the new value (may not be null)
 */
public void setVariable(String name, Value value) {
    initVariables();
    modificationId++;
    Value old;
    if (value == ValueNull.INSTANCE) {
        old = variables.remove(name);
    } else {
        old = variables.put(name, value);
    }
    if (old != null) {
        // close the old value (in case it is a lob)
        old.close();
    }
}
Also used : Value(com.wplatform.ddal.value.Value)

Example 72 with Value

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

the class Session method commit.

/**
 * Commit the current transaction. If the statement was not a data
 * definition statement, and if there are temporary tables that should be
 * dropped or truncated at commit, this is done as well.
 *
 * @param ddl if the statement was a data definition statement
 */
public void commit(boolean ddl) {
    currentTransactionName = null;
    transactionStart = 0;
    if (containsUncommitted()) {
        // need to commit even if rollback is not possible
        // (create/drop table and so on)
        database.commit(this);
    }
    if (temporaryLobs != null) {
        for (Value v : temporaryLobs) {
            v.close();
        }
        temporaryLobs.clear();
    }
    if (!ddl) {
        // do not clean the temp tables if the last command was a
        // create/drop
        cleanTempTables(false);
        if (autoCommitAtTransactionEnd) {
            autoCommit = true;
            autoCommitAtTransactionEnd = false;
        }
    }
    endTransaction();
    boolean commit = true;
    List<SQLException> commitExceptions = New.arrayList();
    StringBuilder buf = new StringBuilder();
    for (Map.Entry<String, Connection> entry : connectionHolder.entrySet()) {
        if (commit) {
            try {
                entry.getValue().commit();
                buf.append("\ncommit shard " + entry.getKey() + " transaction succeed.");
            } catch (SQLException ex) {
                commit = false;
                commitExceptions.add(ex);
                buf.append("\ncommit shard " + entry.getKey() + " transaction failure.");
            }
        } else {
            // remaining connections
            try {
                entry.getValue().rollback();
                buf.append("\nrollback shard " + entry.getKey() + " transaction succeed.");
            } catch (SQLException ex) {
                buf.append("\nrollback shard " + entry.getKey() + " transaction failure.");
            }
        }
    }
    if (commitExceptions.isEmpty()) {
        trace.debug("commit multiple group transaction succeed. commit track list:{0}", buf);
    } else {
        trace.error(commitExceptions.get(0), "fail to commit multiple group transaction. commit track list:{0}", buf);
        DbException.convert(commitExceptions.get(0));
    }
}
Also used : SQLException(java.sql.SQLException) Value(com.wplatform.ddal.value.Value) Connection(java.sql.Connection) JdbcConnection(com.wplatform.ddal.jdbc.JdbcConnection) ValueString(com.wplatform.ddal.value.ValueString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 73 with Value

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

the class BatchUpdateWorker method error.

/**
 * @param e
 */
protected void error(Throwable e) {
    StatementBuilder buff = new StatementBuilder();
    buff.append(shardName).append(" executing batchUpdate error:").append(sql);
    for (List<Value> params : array) {
        if (params != null) {
            if (params != null && params.size() > 0) {
                buff.appendExceptFirst(", ");
                buff.append("\n{");
                int i = 1;
                for (Value v : params) {
                    buff.appendExceptFirst(", ");
                    buff.append(i++).append(": ").append(v.getSQL());
                }
                buff.append('}');
            }
        }
    }
    buff.append(';');
    trace.error(e, buff.toString());
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value)

Example 74 with Value

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

the class PreparedRoutingExecutor method buildInsert.

/**
 * build insert statement
 * @param forTable
 * @param columns
 * @param row
 * @param buff
 * @return
 */
protected List<Value> buildInsert(String forTable, Column[] columns, SearchRow row, StatementBuilder buff) {
    ArrayList<Value> params = New.arrayList();
    buff.append("INSERT INTO ");
    buff.append(identifier(forTable)).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(") ");
    buff.resetCount();
    buff.append("VALUES( ");
    for (int i = 0; i < columns.length; i++) {
        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);
        }
    }
    buff.append(")");
    return params;
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Value(com.wplatform.ddal.value.Value)

Example 75 with Value

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

the class PreparedRoutingExecutor method invokeUpdateRow.

/**
 * @param result
 * @param row
 * @return
 */
private int invokeUpdateRow(RoutingResult result, Row row) {
    List<JdbcWorker<Integer>> workers = New.arrayList(result.tableNodeCount());
    TableNode[] selectNodes = result.getSelectNodes();
    for (TableNode node : selectNodes) {
        StatementBuilder sqlBuff = new StatementBuilder();
        List<Value> params = doTranslate(node, row, sqlBuff);
        workers.add(createUpdateWorker(node.getShardName(), sqlBuff.toString(), params));
    }
    try {
        addRuningJdbcWorkers(workers);
        int affectRows = 0;
        if (workers.size() > 1) {
            // MILLISECONDS
            int queryTimeout = getQueryTimeout();
            List<Future<Integer>> invokeAll;
            if (queryTimeout > 0) {
                invokeAll = jdbcExecutor.invokeAll(workers, queryTimeout, TimeUnit.MILLISECONDS);
            } else {
                invokeAll = jdbcExecutor.invokeAll(workers);
            }
            for (Future<Integer> future : invokeAll) {
                affectRows += future.get();
            }
        } else if (workers.size() == 1) {
            affectRows = workers.get(0).doWork();
        }
        return affectRows;
    } catch (InterruptedException e) {
        throw DbException.convert(e);
    } catch (ExecutionException e) {
        throw DbException.convert(e.getCause());
    } finally {
        removeRuningJdbcWorkers(workers);
        for (JdbcWorker<Integer> jdbcWorker : workers) {
            jdbcWorker.closeResource();
        }
    }
}
Also used : JdbcWorker(com.wplatform.ddal.excutor.JdbcWorker) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) Value(com.wplatform.ddal.value.Value) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

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