Search in sources :

Example 16 with TableMate

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);
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 17 with TableMate

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;
}
Also used : ResultInterface(com.wplatform.ddal.result.ResultInterface) Query(com.wplatform.ddal.command.dml.Query) DbException(com.wplatform.ddal.message.DbException) Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow)

Example 18 with TableMate

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());
    }
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Map(java.util.Map)

Example 19 with TableMate

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;
}
Also used : Table(com.wplatform.ddal.dbobject.table.Table) AlterTableAlterColumn(com.wplatform.ddal.command.ddl.AlterTableAlterColumn) Column(com.wplatform.ddal.dbobject.table.Column) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 20 with TableMate

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());
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow) DbException(com.wplatform.ddal.message.DbException)

Aggregations

TableMate (com.wplatform.ddal.dbobject.table.TableMate)22 Column (com.wplatform.ddal.dbobject.table.Column)12 DbException (com.wplatform.ddal.message.DbException)8 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)7 Value (com.wplatform.ddal.value.Value)6 Row (com.wplatform.ddal.result.Row)5 SearchRow (com.wplatform.ddal.result.SearchRow)5 Query (com.wplatform.ddal.command.dml.Query)4 Expression (com.wplatform.ddal.command.expression.Expression)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 Table (com.wplatform.ddal.dbobject.table.Table)3 ResultInterface (com.wplatform.ddal.result.ResultInterface)3 Map (java.util.Map)3 DefineCommand (com.wplatform.ddal.command.ddl.DefineCommand)2 Schema (com.wplatform.ddal.dbobject.schema.Schema)2 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)2 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)2 Prepared (com.wplatform.ddal.command.Prepared)1 AlterTableAlterColumn (com.wplatform.ddal.command.ddl.AlterTableAlterColumn)1 CreateIndex (com.wplatform.ddal.command.ddl.CreateIndex)1