Search in sources :

Example 41 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class JavaFunction method exportParameters.

@Override
public String exportParameters(TableFilter filter, List<Value> container) {
    StatementBuilder buff = new StatementBuilder();
    // TODO always append the schema once FUNCTIONS_IN_SCHEMA is enabled
    if (functionAlias.getDatabase().getSettings().functionsInSchema || !functionAlias.getSchema().getName().equals(Constants.SCHEMA_MAIN)) {
        buff.append(Parser.quoteIdentifier(functionAlias.getSchema().getName())).append('.');
    }
    buff.append(Parser.quoteIdentifier(functionAlias.getName())).append('(');
    for (Expression e : args) {
        buff.appendExceptFirst(", ");
        buff.append(e.exportParameters(filter, container));
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 42 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class TableFunction method getSQL.

@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder(getName());
    buff.append('(');
    int i = 0;
    for (Expression e : args) {
        buff.appendExceptFirst(", ");
        buff.append(columnList[i++].getCreateSQL()).append('=').append(e.getSQL());
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 43 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class JdbcDatabaseMetaData method getFunctions.

private String getFunctions(String section) throws SQLException {
    try {
        checkClosed();
        PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT TOPIC " + "FROM INFORMATION_SCHEMA.HELP WHERE SECTION = ?");
        prep.setString(1, section);
        ResultSet rs = prep.executeQuery();
        StatementBuilder buff = new StatementBuilder();
        while (rs.next()) {
            String s = rs.getString(1).trim();
            String[] array = StringUtils.arraySplit(s, ',', true);
            for (String a : array) {
                buff.appendExceptFirst(",");
                String f = a.trim();
                if (f.indexOf(' ') >= 0) {
                    // remove 'Function' from 'INSERT Function'
                    f = f.substring(0, f.indexOf(' ')).trim();
                }
                buff.append(f);
            }
        }
        rs.close();
        prep.close();
        return buff.toString();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) SimpleResultSet(com.wplatform.ddal.result.SimpleResultSet) DbException(com.wplatform.ddal.message.DbException)

Example 44 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class BatchUpdateWorker method error.

/**
 * @param e
 */
protected void error(Throwable e) {
    StatementBuilder buff = new StatementBuilder();
    buff.append(shardName).append(" executing batchUpdate error:").append(sql);
    for (List<Value> params : array) {
        if (params != null) {
            if (params != null && params.size() > 0) {
                buff.appendExceptFirst(", ");
                buff.append("\n{");
                int i = 1;
                for (Value v : params) {
                    buff.appendExceptFirst(", ");
                    buff.append(i++).append(": ").append(v.getSQL());
                }
                buff.append('}');
            }
        }
    }
    buff.append(';');
    trace.error(e, buff.toString());
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value)

Example 45 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder 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

StatementBuilder (com.wplatform.ddal.util.StatementBuilder)49 Value (com.wplatform.ddal.value.Value)13 Expression (com.wplatform.ddal.command.expression.Expression)9 Column (com.wplatform.ddal.dbobject.table.Column)8 PreparedStatement (java.sql.PreparedStatement)7 SQLException (java.sql.SQLException)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Connection (java.sql.Connection)3 DataSource (javax.sql.DataSource)3 Index (com.wplatform.ddal.dbobject.index.Index)2 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)2 DbException (com.wplatform.ddal.message.DbException)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 AlterTableAlterColumn (com.wplatform.ddal.command.ddl.AlterTableAlterColumn)1