Search in sources :

Example 31 with Column

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

Example 32 with Column

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

Example 33 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class MergeExecutor 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 34 with Column

use of com.wplatform.ddal.dbobject.table.Column 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 35 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class CreateTableExecutor method doTranslate.

@Override
protected String doTranslate(TableNode tableNode) {
    StatementBuilder buff = new StatementBuilder("CREATE ");
    if (prepared.isTemporary()) {
        buff.append("TEMPORARY ");
    }
    buff.append("TABLE ");
    if (prepared.isIfNotExists()) {
        buff.append("IF NOT EXISTS ");
    }
    buff.append(identifier(tableNode.getCompositeObjectName()));
    if (prepared.getComment() != null) {
    // buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(prepared.getComment()));
    }
    buff.append("(");
    for (Column column : prepared.getColumns()) {
        buff.appendExceptFirst(", ");
        buff.append(column.getCreateSQL());
    }
    for (DefineCommand command : prepared.getConstraintCommands()) {
        buff.appendExceptFirst(", ");
        int type = command.getType();
        switch(type) {
            case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY:
                {
                    AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                    buff.append(" CONSTRAINT PRIMARY KEY");
                    if (stmt.isPrimaryKeyHash()) {
                        buff.append(" USING HASH");
                    }
                    buff.resetCount();
                    buff.append("(");
                    for (IndexColumn c : stmt.getIndexColumns()) {
                        buff.appendExceptFirst(", ");
                        buff.append(identifier(c.columnName));
                    }
                    buff.append(")");
                    break;
                }
            case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE:
                {
                    AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                    buff.append(" CONSTRAINT UNIQUE KEY");
                    buff.resetCount();
                    buff.append("(");
                    for (IndexColumn c : stmt.getIndexColumns()) {
                        buff.appendExceptFirst(", ");
                        buff.append(identifier(c.columnName));
                    }
                    buff.append(")");
                    break;
                }
            case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK:
                {
                    AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                    String enclose = StringUtils.enclose(stmt.getCheckExpression().getSQL());
                    buff.append(" CHECK").append(enclose);
                    break;
                }
            case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL:
                {
                    AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                    String refTableName = stmt.getRefTableName();
                    TableMate table = getTableMate(stmt.getTableName());
                    TableMate refTable = getTableMate(refTableName);
                    if (refTable != null) {
                        TableNode[] partitionNode = refTable.getPartitionNode();
                        if (partitionNode.length > 1) {
                            TableNode[] tableNodes = table.getPartitionNode();
                            TableNode[] refTableNodes = partitionNode;
                            Map<TableNode, TableNode> symmetryRelation = getSymmetryRelation(tableNodes, refTableNodes);
                            TableNode relation = symmetryRelation.get(tableNode);
                            if (relation == null) {
                                throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, "The original table and reference table should be symmetrical.");
                            }
                            refTableName = relation.getCompositeObjectName();
                        } else if (partitionNode.length == 1) {
                            refTableName = partitionNode[0].getCompositeObjectName();
                        }
                    }
                    IndexColumn[] cols = stmt.getIndexColumns();
                    IndexColumn[] refCols = stmt.getRefIndexColumns();
                    buff.resetCount();
                    buff.append(" CONSTRAINT FOREIGN KEY(");
                    for (IndexColumn c : cols) {
                        buff.appendExceptFirst(", ");
                        buff.append(c.columnName);
                    }
                    buff.append(")");
                    buff.append(" REFERENCES ");
                    buff.append(identifier(refTableName)).append("(");
                    buff.resetCount();
                    for (IndexColumn r : refCols) {
                        buff.appendExceptFirst(", ");
                        buff.append(r.columnName);
                    }
                    buff.append(")");
                    break;
                }
            case CommandInterface.CREATE_INDEX:
                {
                    CreateIndex stmt = (CreateIndex) command;
                    if (stmt.isSpatial()) {
                        buff.append(" SPATIAL INDEX");
                    } else {
                        buff.append(" INDEX");
                        if (stmt.isHash()) {
                            buff.append(" USING HASH");
                        }
                    }
                    buff.resetCount();
                    buff.append("(");
                    for (IndexColumn c : stmt.getIndexColumns()) {
                        buff.appendExceptFirst(", ");
                        buff.append(identifier(c.columnName));
                    }
                    buff.append(")");
                    break;
                }
            default:
                throw DbException.throwInternalError("type=" + type);
        }
    }
    buff.append(")");
    if (prepared.getTableEngine() != null) {
        buff.append(" ENGINE = ");
        buff.append(prepared.getTableEngine());
    }
    ArrayList<String> tableEngineParams = prepared.getTableEngineParams();
    if (tableEngineParams != null && tableEngineParams.isEmpty()) {
        buff.append("WITH ");
        buff.resetCount();
        for (String parameter : tableEngineParams) {
            buff.appendExceptFirst(", ");
            buff.append(StringUtils.quoteIdentifier(parameter));
        }
    }
    if (prepared.getCharset() != null) {
        buff.append(" DEFAULT CHARACTER SET = ");
        buff.append(prepared.getCharset());
    }
    return buff.toString();
}
Also used : AlterTableAddConstraint(com.wplatform.ddal.command.ddl.AlterTableAddConstraint) CreateIndex(com.wplatform.ddal.command.ddl.CreateIndex) IndexColumn(com.wplatform.ddal.dbobject.table.IndexColumn) Column(com.wplatform.ddal.dbobject.table.Column) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Map(java.util.Map) AlterTableAddConstraint(com.wplatform.ddal.command.ddl.AlterTableAddConstraint) DefineCommand(com.wplatform.ddal.command.ddl.DefineCommand) IndexColumn(com.wplatform.ddal.dbobject.table.IndexColumn)

Aggregations

Column (com.wplatform.ddal.dbobject.table.Column)38 Expression (com.wplatform.ddal.command.expression.Expression)15 Value (com.wplatform.ddal.value.Value)14 TableMate (com.wplatform.ddal.dbobject.table.TableMate)12 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)8 DbException (com.wplatform.ddal.message.DbException)7 Row (com.wplatform.ddal.result.Row)6 SearchRow (com.wplatform.ddal.result.SearchRow)6 Query (com.wplatform.ddal.command.dml.Query)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)4 ResultInterface (com.wplatform.ddal.result.ResultInterface)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 ValueExpression (com.wplatform.ddal.command.expression.ValueExpression)3 Index (com.wplatform.ddal.dbobject.index.Index)3 Table (com.wplatform.ddal.dbobject.table.Table)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Database (com.wplatform.ddal.engine.Database)3 List (java.util.List)3 Prepared (com.wplatform.ddal.command.Prepared)2