use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.
the class PreparedRoutingExecutor method updateRows.
protected int updateRows(TableMate table, List<Row> rows) {
Map<BatchKey, List<List<Value>>> batches = New.hashMap();
session.checkCanceled();
for (Row row : rows) {
RoutingResult result = routingHandler.doRoute(table, row);
TableNode[] selectNodes = result.getSelectNodes();
for (TableNode node : selectNodes) {
StatementBuilder sqlBuff = new StatementBuilder();
List<Value> params = doTranslate(node, row, sqlBuff);
BatchKey batchKey = new BatchKey(node.getShardName(), sqlBuff.toString());
List<List<Value>> batchArgs = batches.get(batchKey);
if (batchArgs == null) {
batchArgs = New.arrayList(10);
batches.put(batchKey, batchArgs);
}
batchArgs.add(params);
}
}
List<JdbcWorker<Integer[]>> workers = New.arrayList(batches.size());
for (Map.Entry<BatchKey, List<List<Value>>> entry : batches.entrySet()) {
String shardName = entry.getKey().shardName;
String sql = entry.getKey().sql;
List<List<Value>> array = entry.getValue();
workers.add(createBatchUpdateWorker(shardName, sql, array));
}
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) {
Integer[] integers = future.get();
for (Integer integer : integers) {
affectRows += integer;
}
}
} else if (workers.size() == 1) {
Integer[] integers = workers.get(0).doWork();
for (Integer integer : integers) {
affectRows += integer;
}
}
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();
}
}
}
use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.
the class SelectExecutor method getPlanSQL.
public String getPlanSQL() {
// can not use the field sqlStatement because the parameter
// indexes may be incorrect: ? may be in fact ?2 for a subquery
// but indexes may be set manually as well
Expression[] exprList = expressions.toArray(new Expression[expressions.size()]);
StatementBuilder buff = new StatementBuilder("SELECT");
if (distinct) {
buff.append(" DISTINCT");
}
for (int i = 0; i < visibleColumnCount; i++) {
buff.appendExceptFirst(",");
buff.append('\n');
buff.append(StringUtils.indent(exprList[i].getSQL(), 4, false));
}
buff.append("\nFROM ");
TableFilter filter = topTableFilter;
if (filter != null) {
buff.resetCount();
int i = 0;
do {
buff.appendExceptFirst("\n");
buff.append(filter.getPlanSQL(i++ > 0));
filter = filter.getJoin();
} while (filter != null);
} else {
buff.resetCount();
int i = 0;
for (TableFilter f : topFilters) {
do {
buff.appendExceptFirst("\n");
buff.append(f.getPlanSQL(i++ > 0));
f = f.getJoin();
} while (f != null);
}
}
if (condition != null) {
buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL()));
}
if (groupIndex != null) {
buff.append("\nGROUP BY ");
buff.resetCount();
for (int gi : groupIndex) {
Expression g = exprList[gi];
g = g.getNonAliasExpression();
buff.appendExceptFirst(", ");
buff.append(StringUtils.unEnclose(g.getSQL()));
}
}
if (group != null) {
buff.append("\nGROUP BY ");
buff.resetCount();
for (Expression g : group) {
buff.appendExceptFirst(", ");
buff.append(StringUtils.unEnclose(g.getSQL()));
}
}
if (having != null) {
// could be set in addGlobalCondition
// in this case the query is not run directly, just getPlanSQL is
// called
Expression h = having;
buff.append("\nHAVING ").append(StringUtils.unEnclose(h.getSQL()));
} else if (havingIndex >= 0) {
Expression h = exprList[havingIndex];
buff.append("\nHAVING ").append(StringUtils.unEnclose(h.getSQL()));
}
if (sort != null) {
buff.append("\nORDER BY ").append(sort.getSQL(exprList, visibleColumnCount));
}
if (limitExpr != null) {
buff.append("\nLIMIT ").append(StringUtils.unEnclose(limitExpr.getSQL()));
if (offsetExpr != null) {
buff.append(" OFFSET ").append(StringUtils.unEnclose(offsetExpr.getSQL()));
}
}
if (sampleSizeExpr != null) {
buff.append("\nSAMPLE_SIZE ").append(StringUtils.unEnclose(sampleSizeExpr.getSQL()));
}
if (isForUpdate) {
buff.append("\nFOR UPDATE");
}
return buff.toString();
}
use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.
the class TableFilter method getPlanSQL.
/**
* Get the query execution plan text to use for this table filter.
*
* @param isJoin if this is a joined table
* @return the SQL statement snippet
*/
public String getPlanSQL(boolean isJoin) {
StringBuilder buff = new StringBuilder();
if (isJoin) {
if (joinOuter) {
buff.append("LEFT OUTER JOIN ");
} else {
buff.append("INNER JOIN ");
}
}
if (nestedJoin != null) {
StringBuffer buffNested = new StringBuffer();
TableFilter n = nestedJoin;
do {
buffNested.append(n.getPlanSQL(n != nestedJoin));
buffNested.append('\n');
n = n.getJoin();
} while (n != null);
String nested = buffNested.toString();
boolean enclose = !nested.startsWith("(");
if (enclose) {
buff.append("(\n");
}
buff.append(StringUtils.indent(nested, 4, false));
if (enclose) {
buff.append(')');
}
if (isJoin) {
buff.append(" ON ");
if (joinCondition == null) {
// need to have a ON expression,
// otherwise the nesting is unclear
buff.append("1=1");
} else {
buff.append(StringUtils.unEnclose(joinCondition.getSQL()));
}
}
return buff.toString();
}
buff.append(table.getSQL());
if (alias != null) {
buff.append(' ').append(Parser.quoteIdentifier(alias));
}
if (index != null) {
buff.append('\n');
StatementBuilder planBuff = new StatementBuilder();
planBuff.append(index.getSQL());
if (indexConditions.size() > 0) {
planBuff.append(": ");
for (IndexCondition condition : indexConditions) {
planBuff.appendExceptFirst("\n AND ");
planBuff.append(condition.getSQL());
}
}
String plan = StringUtils.quoteRemarkSQL(planBuff.toString());
if (plan.indexOf('\n') >= 0) {
plan += "\n";
}
buff.append(StringUtils.indent("/* " + plan + " */", 4, false));
}
if (isJoin) {
buff.append("\n ON ");
if (joinCondition == null) {
// need to have a ON expression, otherwise the nesting is
// unclear
buff.append("1=1");
} else {
buff.append(StringUtils.unEnclose(joinCondition.getSQL()));
}
}
if (filterCondition != null) {
buff.append('\n');
String condition = StringUtils.unEnclose(filterCondition.getSQL());
condition = "/* WHERE " + StringUtils.quoteRemarkSQL(condition) + "\n*/";
buff.append(StringUtils.indent(condition, 4, false));
}
if (scanCount > 0) {
buff.append("\n /* scanCount: ").append(scanCount).append(" */");
}
return buff.toString();
}
use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.
the class JdbcCallWorker method doWork.
@Override
public Integer doWork() {
Connection conn = null;
PreparedStatement stmt = null;
try {
DataSource dataSource = getDataSource();
Optional optional = Optional.build().shardName(shardName).readOnly(false);
if (trace.isDebugEnabled()) {
trace.debug("{0} Fetching connection from DataSource.", shardName);
}
conn = session.applyConnection(dataSource, optional);
attach(conn);
if (trace.isDebugEnabled()) {
trace.debug("{0} Preparing: {1};", shardName, sql);
}
stmt = conn.prepareStatement(sql);
attach(stmt);
applyQueryTimeout(stmt);
if (params != null) {
for (int i = 0, size = params.size(); i < size; i++) {
Value v = params.get(i);
v.set(stmt, i + 1);
if (trace.isDebugEnabled()) {
trace.debug("{0} setParameter: {1} -> {2};", shardName, i + 1, v.getSQL());
}
}
}
int rows = stmt.executeUpdate();
if (trace.isDebugEnabled()) {
trace.debug("{0} executeUpdate: {1} affected.", shardName, rows);
}
return rows;
} catch (SQLException e) {
StatementBuilder buff = new StatementBuilder();
buff.append(shardName).append(" executing executeUpdate error:").append(sql);
if (params != null && params.size() > 0) {
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());
throw wrapException(sql, e);
}
}
use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.
the class JdbcUpdateWorker method doWork.
@Override
public Integer doWork() {
Connection conn = null;
PreparedStatement stmt = null;
try {
DataSource dataSource = getDataSource();
Optional optional = Optional.build().shardName(shardName).readOnly(false);
if (trace.isDebugEnabled()) {
trace.debug("{0} Fetching connection from DataSource.", shardName);
}
conn = session.applyConnection(dataSource, optional);
attach(conn);
if (trace.isDebugEnabled()) {
trace.debug("{0} Preparing call: {1};", shardName, sql);
}
stmt = conn.prepareCall(sql);
attach(stmt);
applyQueryTimeout(stmt);
if (params != null) {
for (int i = 0, size = params.size(); i < size; i++) {
Value v = params.get(i);
v.set(stmt, i + 1);
if (trace.isDebugEnabled()) {
trace.debug("{0} setParameter: {1} -> {2};", shardName, i + 1, v.getSQL());
}
}
}
int rows = stmt.executeUpdate();
if (trace.isDebugEnabled()) {
trace.debug("{0} executeUpdate: {1} affected.", shardName, rows);
}
return rows;
} catch (SQLException e) {
StatementBuilder buff = new StatementBuilder();
buff.append(shardName).append(" executing executeUpdate error:").append(sql);
if (params != null && 0 < params.size()) {
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());
throw wrapException(sql, e);
}
}
Aggregations