use of com.wplatform.ddal.dbobject.table.Column 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.dbobject.table.Column in project jdbc-shards by wplatform.
the class Function method exportParameters.
@Override
public String exportParameters(TableFilter filter, List<Value> container) {
StatementBuilder buff = new StatementBuilder(info.name);
if (info.type == CASE) {
if (args[0] != null) {
buff.append(" ").append(args[0].exportParameters(filter, container));
}
for (int i = 1, len = args.length - 1; i < len; i += 2) {
buff.append(" WHEN ").append(args[i].exportParameters(filter, container));
buff.append(" THEN ").append(args[i + 1].exportParameters(filter, container));
}
if (args.length % 2 == 0) {
buff.append(" ELSE ").append(args[args.length - 1].exportParameters(filter, container));
}
return buff.append(" END").toString();
}
buff.append('(');
switch(info.type) {
case CAST:
{
buff.append(args[0].exportParameters(filter, container)).append(" AS ").append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
break;
}
case CONVERT:
{
buff.append(args[0].exportParameters(filter, container)).append(',').append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
break;
}
case EXTRACT:
{
ValueString v = (ValueString) args[0].getValue(null);
buff.append(v.getString()).append(" FROM ").append(args[1].exportParameters(filter, container));
break;
}
default:
{
for (Expression e : args) {
buff.appendExceptFirst(", ");
buff.append(e.exportParameters(filter, container));
}
}
}
return buff.append(')').toString();
}
use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.
the class Function method getSQL.
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder(info.name);
if (info.type == CASE) {
if (args[0] != null) {
buff.append(" ").append(args[0].getSQL());
}
for (int i = 1, len = args.length - 1; i < len; i += 2) {
buff.append(" WHEN ").append(args[i].getSQL());
buff.append(" THEN ").append(args[i + 1].getSQL());
}
if (args.length % 2 == 0) {
buff.append(" ELSE ").append(args[args.length - 1].getSQL());
}
return buff.append(" END").toString();
}
buff.append('(');
switch(info.type) {
case CAST:
{
buff.append(args[0].getSQL()).append(" AS ").append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
break;
}
case CONVERT:
{
buff.append(args[0].getSQL()).append(',').append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
break;
}
case EXTRACT:
{
ValueString v = (ValueString) args[0].getValue(null);
buff.append(v.getString()).append(" FROM ").append(args[1].getSQL());
break;
}
default:
{
for (Expression e : args) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
}
}
}
return buff.append(')').toString();
}
use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.
the class Update method updateRows.
protected int updateRows() {
tableFilter.startQuery(session);
tableFilter.reset();
RowList rows = new RowList(session);
try {
Table table = tableFilter.getTable();
session.getUser().checkRight(table, Right.UPDATE);
//table.lock(session, true, false);
int columnCount = table.getColumns().length;
// get the old rows, compute the new rows
setCurrentRowNumber(0);
int count = 0;
Column[] columns = table.getColumns();
int limitRows = -1;
if (limitExpr != null) {
Value v = limitExpr.getValue(session);
if (v != ValueNull.INSTANCE) {
limitRows = v.getInt();
}
}
while (tableFilter.next()) {
setCurrentRowNumber(count + 1);
if (limitRows >= 0 && count >= limitRows) {
break;
}
if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
Row oldRow = tableFilter.get();
Row newRow = table.getTemplateRow();
for (int i = 0; i < columnCount; i++) {
Expression newExpr = expressionMap.get(columns[i]);
Value newValue;
if (newExpr == null) {
newValue = oldRow.getValue(i);
} else if (newExpr == ValueExpression.getDefault()) {
Column column = table.getColumn(i);
newValue = table.getDefaultValue(session, column);
} else {
Column column = table.getColumn(i);
newValue = column.convert(newExpr.getValue(session));
}
newRow.setValue(i, newValue);
}
table.validateConvertUpdateSequence(session, newRow);
rows.add(oldRow);
rows.add(newRow);
count++;
}
}
//table.updateRows(this, session, rows);
return count;
} finally {
rows.close();
}
}
use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.
the class Update method prepare.
@Override
public void prepare() {
if (condition != null) {
condition.mapColumns(tableFilter, 0);
condition = condition.optimize(session);
condition.createIndexConditions(session, tableFilter);
}
for (int i = 0, size = columns.size(); i < size; i++) {
Column c = columns.get(i);
Expression e = expressionMap.get(c);
e.mapColumns(tableFilter, 0);
expressionMap.put(c, e.optimize(session));
}
PlanItem item = tableFilter.getBestPlanItem(session, 1);
tableFilter.setPlanItem(item);
tableFilter.prepare();
}
Aggregations