use of com.wplatform.ddal.command.expression.Expression 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.command.expression.Expression in project jdbc-shards by wplatform.
the class UpdateExecutor method doTranslate.
@Override
protected List<Value> doTranslate(TableNode node, SearchRow row, StatementBuilder buff) {
ArrayList<Value> params = New.arrayList();
TableFilter tableFilter = prepared.getTableFilter();
String forTable = node.getCompositeObjectName();
List<Column> columns = prepared.getColumns();
Expression condition = prepared.getCondition();
Expression limitExpr = prepared.getLimitExpr();
buff.append("UPDATE ");
buff.append(identifier(forTable)).append(" SET ");
for (int i = 0, size = columns.size(); i < size; i++) {
Column c = columns.get(i);
buff.appendExceptFirst(", ");
buff.append(c.getSQL()).append(" = ");
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);
}
}
if (condition != null) {
condition.exportParameters(tableFilter, params);
buff.append(" WHERE ").append(StringUtils.unEnclose(condition.getSQL()));
}
if (limitExpr != null) {
limitExpr.exportParameters(tableFilter, params);
buff.append(" LIMIT ").append(StringUtils.unEnclose(limitExpr.getSQL()));
}
return params;
}
use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.
the class Table method getDefaultValue.
/**
* Get or generate a default value for the given column.
*
* @param session the session
* @param column the column
* @return the value
*/
public Value getDefaultValue(Session session, Column column) {
Expression defaultExpr = column.getDefaultExpression();
Value v;
if (defaultExpr == null) {
v = column.validateConvertUpdateSequence(session, null);
} else {
v = defaultExpr.getValue(session);
}
return column.convert(v);
}
use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.
the class SelectExecutor method queryGroupSorted.
private void queryGroupSorted(int columnCount, ResultTarget result) {
int rowNumber = 0;
prepared.setCurrentRowNumber(0);
prepared.setCurrentGroup(null);
Value[] previousKeyValues = null;
while (topTableFilter.next()) {
// for rownum expression
prepared.setCurrentRowNumber(rowNumber + 1);
if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
rowNumber++;
Value[] keyValues = new Value[groupIndex.length];
// update group
for (int i = 0; i < groupIndex.length; i++) {
int idx = groupIndex[i];
Expression expr = expressions.get(idx);
keyValues[i] = expr.getValue(session);
}
if (previousKeyValues == null) {
previousKeyValues = keyValues;
prepared.setCurrentGroup(New.<Expression, Object>hashMap());
} else if (!Arrays.equals(previousKeyValues, keyValues)) {
addGroupSortedRow(previousKeyValues, columnCount, result);
previousKeyValues = keyValues;
prepared.setCurrentGroup(New.<Expression, Object>hashMap());
}
prepared.increaseCurrentGroupRowId();
for (int i = 0; i < columnCount; i++) {
if (groupByExpression == null || !groupByExpression[i]) {
Expression expr = expressions.get(i);
expr.updateAggregate(session);
}
}
}
}
if (previousKeyValues != null) {
addGroupSortedRow(previousKeyValues, columnCount, result);
}
}
use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.
the class Select method prepare.
@Override
public void prepare() {
if (isPrepared) {
// sometimes a subquery is prepared twice (CREATE TABLE AS SELECT)
return;
}
if (SysProperties.CHECK && !checkInit) {
DbException.throwInternalError("not initialized");
}
if (orderList != null) {
sort = prepareOrder(orderList, expressions.size());
orderList = null;
}
for (int i = 0; i < expressions.size(); i++) {
Expression e = expressions.get(i);
expressions.set(i, e.optimize(session));
}
if (condition != null) {
condition = condition.optimize(session);
for (TableFilter f : filters) {
// left outer join child on p = pc where c is null;
if (!f.isJoinOuter() && !f.isJoinOuterIndirect()) {
condition.createIndexConditions(session, f);
}
}
}
if (isGroupQuery && groupIndex == null && havingIndex < 0) {
isQuickAggregateQuery = true;
}
cost = preparePlan();
TableMate last = null;
for (TableFilter filter : filters) {
if (!filter.isFromTableMate()) {
break;
}
if (filters.size() == 1) {
isAccordantQuery = true;
break;
}
TableMate table = (TableMate) filter.getTable();
if (last != null && !last.isRelationSymmetry(table)) {
break;
}
last = table;
}
if (distinct && session.getDatabase().getSettings().optimizeDistinct && !isGroupQuery && filters.size() == 1 && expressions.size() == 1 && condition == null) {
// 分布式的查询不合适
}
if (sort != null && !isGroupQuery) {
}
if (isGroupQuery && getGroupByExpressionCount() > 0) {
}
expressionArray = new Expression[expressions.size()];
expressions.toArray(expressionArray);
isPrepared = true;
}
Aggregations