use of com.wplatform.ddal.dbobject.table.TableMate in project jdbc-shards by wplatform.
the class ReplaceExecutor method doTranslate.
@Override
protected List<Value> doTranslate(TableNode node, SearchRow row, StatementBuilder buff) {
String forTable = node.getCompositeObjectName();
TableMate table = castTableMate(prepared.getTable());
Column[] columns = table.getColumns();
return buildInsert(forTable, columns, row, buff);
}
use of com.wplatform.ddal.dbobject.table.TableMate 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;
}
use of com.wplatform.ddal.dbobject.table.TableMate in project jdbc-shards by wplatform.
the class AlterTableAddConstraintExecutor method doTranslate.
@Override
protected String doTranslate(TableNode tableNode) {
String tableName = prepared.getTableName();
TableMate table = getTableMate(tableName);
String forTable = tableNode.getCompositeObjectName();
IndexColumn.mapColumns(prepared.getIndexColumns(), table);
switch(prepared.getType()) {
case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY:
{
return doBuildUnique(forTable, AlterTableAddConstraint.PRIMARY_KEY);
}
case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE:
{
String uniqueType = AlterTableAddConstraint.UNIQUE + " KEY";
return doBuildUnique(forTable, uniqueType);
}
case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK:
{
/*
* MySQL. The CHECK clause is parsed but ignored by all storage
* engines.
*/
return doBuildCheck(forTable);
}
case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL:
{
/*
* MySQL. The FOREIGN KEY and REFERENCES clauses are supported by
* the InnoDB and NDB storage engines
*/
String refTableName = prepared.getRefTableName();
TableMate refTable = getTableMate(refTableName);
Map<TableNode, TableNode> symmetryRelation = getSymmetryRelation(table.getPartitionNode(), refTable.getPartitionNode());
TableNode relation = symmetryRelation.get(tableNode);
if (relation == null) {
throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, "The original table and reference table should be symmetrical.");
}
return doBuildReferences(forTable, relation.getCompositeObjectName());
}
default:
throw DbException.throwInternalError("type=" + prepared.getType());
}
}
use of com.wplatform.ddal.dbobject.table.TableMate in project jdbc-shards by wplatform.
the class AlterTableAlterColumnExecutor method executeUpdate.
@Override
public int executeUpdate() {
Table parseTable = prepared.getTable();
if (!(parseTable instanceof TableMate)) {
DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, parseTable.getSQL());
}
TableMate table = (TableMate) parseTable;
session.getUser().checkRight(table, Right.ALL);
TableNode[] tableNodes = table.getPartitionNode();
Column oldColumn = prepared.getOldColumn();
int type = prepared.getType();
switch(type) {
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL:
{
if (!oldColumn.isNullable()) {
break;
}
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL:
{
if (oldColumn.isNullable()) {
break;
}
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT:
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE:
case CommandInterface.ALTER_TABLE_ADD_COLUMN:
case CommandInterface.ALTER_TABLE_DROP_COLUMN:
{
execute(tableNodes);
table.loadMataData(session);
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY:
{
// not supported.
return 0;
}
default:
throw DbException.throwInternalError("type=" + type);
}
return 0;
}
use of com.wplatform.ddal.dbobject.table.TableMate 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());
}
Aggregations