use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class UpdateExecutor method executeUpdate.
@Override
public int executeUpdate() {
TableFilter tableFilter = prepared.getTableFilter();
TableMate table = castTableMate(tableFilter.getTable());
List<Column> columns = prepared.getColumns();
Map<Column, Expression> valueMap = prepared.getExpressionMap();
table.check();
session.getUser().checkRight(table, Right.UPDATE);
Row updateRow = table.getTemplateRow();
for (int i = 0, size = columns.size(); i < size; i++) {
Column c = columns.get(i);
Expression e = valueMap.get(c);
int index = c.getColumnId();
if (e != null) {
// e can be null (DEFAULT)
e = e.optimize(session);
try {
Value v = c.convert(e.getValue(session));
updateRow.setValue(index, v);
} catch (DbException ex) {
ex.addSQL("evaluate expression " + e.getSQL());
throw ex;
}
}
}
return updateRow(table, updateRow, tableFilter.getIndexConditions());
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class TableFilter method getValue.
@Override
public Value getValue(Column column) {
if (currentSearchRow == null) {
return null;
}
int columnId = column.getColumnId();
if (columnId == -1) {
return ValueLong.get(currentSearchRow.getKey());
}
if (current == null) {
Value v = currentSearchRow.getValue(columnId);
if (v != null) {
return v;
}
current = cursor.get();
if (current == null) {
return ValueNull.INSTANCE;
}
}
return current.getValue(columnId);
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class RoutingHandlerImpl method enumRange.
private List<Value> enumRange(Value firstV, Value listV) {
if (firstV.getType() != listV.getType()) {
return null;
}
int type = firstV.getType();
switch(type) {
case Value.BYTE:
case Value.INT:
case Value.LONG:
case Value.SHORT:
if (listV.subtract(firstV).getLong() > 200) {
return null;
}
List<Value> enumValues = New.arrayList(10);
Value enumValue = firstV;
while (database.compare(enumValue, listV) <= 0) {
enumValues.add(enumValue);
Value increase = ValueLong.get(1).convertTo(enumValue.getType());
enumValue = enumValue.add(increase);
}
return enumValues;
default:
return null;
}
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class RoutingHandlerImpl method doRoute.
@Override
public RoutingResult doRoute(TableMate table, Session session, List<IndexCondition> indexConditions) {
TableRouter tr = table.getTableRouter();
if (tr == null) {
return fixedRoutingResult(table.getShards());
} else {
Map<String, List<Value>> routingArgs = New.hashMap();
List<RuleColumn> ruleCols = tr.getRuleColumns();
SearchRow start = null, end = null;
for (IndexCondition condition : indexConditions) {
Column column = condition.getColumn();
String colName = column.getName();
RuleColumn matched = null;
for (RuleColumn ruleColumn : ruleCols) {
if (colName.equalsIgnoreCase(ruleColumn.getName())) {
matched = ruleColumn;
}
}
if (matched == null) {
continue;
}
List<Value> values = routingArgs.get(matched.getName());
if (values == null) {
values = New.arrayList();
routingArgs.put(matched.getName(), values);
}
if (condition.getCompareType() == Comparison.IN_LIST) {
Value[] inList = condition.getCurrentValueList(session);
for (Value value : inList) {
values.add(value);
}
} else if (condition.getCompareType() == Comparison.IN_QUERY) {
ResultInterface result = condition.getCurrentResult();
while (result.next()) {
Value v = result.currentRow()[0];
if (v != ValueNull.INSTANCE) {
v = column.convert(v);
values.add(v);
}
}
} else {
int columnId = column.getColumnId();
Value v = condition.getCurrentValue(session);
boolean isStart = condition.isStart();
boolean isEnd = condition.isEnd();
if (isStart) {
start = getSearchRow(table, session, start, columnId, v, true);
}
if (isEnd) {
end = getSearchRow(table, session, end, columnId, v, false);
}
}
}
exportRangeArg(table, start, end, routingArgs);
RoutingResult rr = trc.calculate(tr, routingArgs);
return rr;
}
}
use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.
the class BatchUpdateWorker method doWork.
@Override
public Integer[] doWork() {
Connection conn = null;
PreparedStatement stmt = null;
try {
if (array == null || array.size() < 1) {
throw new IllegalArgumentException();
}
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: {};", shardName, sql);
}
stmt = conn.prepareStatement(sql);
attach(stmt);
applyQueryTimeout(stmt);
for (List<Value> params : array) {
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());
}
}
stmt.addBatch();
if (trace.isDebugEnabled()) {
trace.debug("{0} addBatch.", shardName);
}
}
}
int[] affected = stmt.executeBatch();
Integer[] rows = new Integer[affected.length];
for (int i = 0; i < rows.length; i++) {
rows[i] = affected[i];
}
if (trace.isDebugEnabled()) {
trace.debug("{0} executeUpdate: {1} affected.", shardName, Arrays.toString(affected));
}
return rows;
} catch (SQLException e) {
error(e);
throw wrapException(sql, e);
} catch (Throwable e) {
error(e);
throw DbException.convert(e);
}
}
Aggregations