use of com.wplatform.ddal.dbobject.table.Column 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.dbobject.table.Column in project jdbc-shards by wplatform.
the class MergeExecutor method merge.
private void merge(Row row) {
TableMate table = castTableMate(prepared.getTable());
Prepared update = prepared.getUpdate();
Column[] columns = prepared.getColumns();
Column[] keys = prepared.getKeys();
ArrayList<Parameter> k = update.getParameters();
for (int i = 0; i < columns.length; i++) {
Column col = columns[i];
Value v = row.getValue(col.getColumnId());
Parameter p = k.get(i);
p.setValue(v);
}
for (int i = 0; i < keys.length; i++) {
Column col = keys[i];
Value v = row.getValue(col.getColumnId());
if (v == null) {
throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, col.getSQL());
}
Parameter p = k.get(columns.length + i);
p.setValue(v);
}
int count = update.update();
if (count == 0) {
try {
table.validateConvertUpdateSequence(session, row);
updateRow(table, row);
} catch (DbException e) {
throw e;
}
} else if (count != 1) {
throw DbException.get(ErrorCode.DUPLICATE_KEY_1, table.getSQL());
}
}
use of com.wplatform.ddal.dbobject.table.Column 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.dbobject.table.Column 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.dbobject.table.Column in project jdbc-shards by wplatform.
the class AlterTableAlterColumnExecutor method doTranslate.
@Override
protected String doTranslate(TableNode node) {
Column oldColumn = prepared.getOldColumn();
String forTable = node.getCompositeObjectName();
int type = prepared.getType();
switch(type) {
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL:
{
StringBuilder buff = new StringBuilder("ALTER TABLE ");
buff.append(identifier(forTable));
buff.append(" CHANGE COLUMN ");
buff.append(oldColumn.getSQL()).append(' ');
buff.append(oldColumn.getCreateSQL());
return buff.toString();
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL:
{
StringBuilder buff = new StringBuilder("ALTER TABLE ");
buff.append(identifier(forTable));
buff.append(" CHANGE COLUMN ");
buff.append(oldColumn.getSQL()).append(' ');
buff.append(oldColumn.getCreateSQL());
return buff.toString();
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT:
{
StringBuilder buff = new StringBuilder("ALTER TABLE");
buff.append(identifier(forTable));
buff.append(" ALTER COLUMN ");
buff.append(prepared.getOldColumn().getSQL());
buff.append(" SET DEFAULT ");
buff.append(prepared.getDefaultExpression().getSQL());
return buff.toString();
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE:
{
StringBuilder buff = new StringBuilder("ALTER TABLE");
buff.append(identifier(forTable));
buff.append(" ALTER COLUMN ");
buff.append(prepared.getOldColumn().getSQL());
buff.append(" SET DEFAULT ");
buff.append(prepared.getDefaultExpression().getSQL());
return buff.toString();
}
case CommandInterface.ALTER_TABLE_ADD_COLUMN:
{
StatementBuilder buff = new StatementBuilder("ALTER TABLE");
buff.append(identifier(forTable));
ArrayList<Column> columnsToAdd = getPrepared().getColumnsToAdd();
for (Column column : columnsToAdd) {
buff.appendExceptFirst(", ");
buff.append(" ADD COLUMN ");
buff.append(column.getCreateSQL());
}
return buff.toString();
}
case CommandInterface.ALTER_TABLE_DROP_COLUMN:
{
StatementBuilder buff = new StatementBuilder("ALTER TABLE");
buff.append(identifier(forTable));
buff.append(" DROP COLUMN ");
buff.append(oldColumn.getSQL());
return buff.toString();
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY:
default:
throw DbException.throwInternalError("type=" + type);
}
}
Aggregations