use of com.wplatform.ddal.result.Row 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;
}
use of com.wplatform.ddal.result.Row in project jdbc-shards by wplatform.
the class InsertExecutor method addRow.
@Override
public void addRow(Value[] values) {
TableMate table = castTableMate(prepared.getTable());
Row newRow = table.getTemplateRow();
Column[] columns = prepared.getColumns();
prepared.setCurrentRowNumber(++rowNumber);
for (int j = 0, len = columns.length; j < len; j++) {
Column c = columns[j];
int index = c.getColumnId();
try {
Value v = c.convert(values[j]);
newRow.setValue(index, v);
} catch (DbException ex) {
throw prepared.setRow(ex, rowNumber, Prepared.getSQL(values));
}
}
table.validateConvertUpdateSequence(session, newRow);
addNewRow(newRow);
}
use of com.wplatform.ddal.result.Row 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.result.Row 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;
}
use of com.wplatform.ddal.result.Row in project jdbc-shards by wplatform.
the class ReplaceExecutor 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));
}
}
}
replace(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));
}
}
replace(newRow);
}
rows.close();
}
return count;
}
Aggregations