Search in sources :

Example 21 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder 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);
    }
}
Also used : AlterTableAlterColumn(com.wplatform.ddal.command.ddl.AlterTableAlterColumn) Column(com.wplatform.ddal.dbobject.table.Column) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) ArrayList(java.util.ArrayList)

Example 22 with StatementBuilder

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

the class TableView method getCreateSQL.

private String getCreateSQL(boolean orReplace, boolean force, String quotedName) {
    StatementBuilder buff = new StatementBuilder("CREATE ");
    if (orReplace) {
        buff.append("OR REPLACE ");
    }
    if (force) {
        buff.append("FORCE ");
    }
    buff.append("VIEW ");
    buff.append(quotedName);
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    if (columns != null && columns.length > 0) {
        buff.append('(');
        for (Column c : columns) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL());
        }
        buff.append(')');
    } else if (columnNames != null) {
        buff.append('(');
        for (String n : columnNames) {
            buff.appendExceptFirst(", ");
            buff.append(n);
        }
        buff.append(')');
    }
    return buff.append(" AS\n").append(querySQL).toString();
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 23 with StatementBuilder

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

the class SortOrder method getSQL.

/**
     * Create the SQL snippet that describes this sort order.
     * This is the SQL snippet that usually appears after the ORDER BY clause.
     *
     * @param list    the expression list
     * @param visible the number of columns in the select list
     * @return the SQL snippet
     */
public String getSQL(Expression[] list, int visible) {
    StatementBuilder buff = new StatementBuilder();
    int i = 0;
    for (int idx : queryColumnIndexes) {
        buff.appendExceptFirst(", ");
        if (idx < visible) {
            buff.append(idx + 1);
        } else {
            buff.append('=').append(StringUtils.unEnclose(list[idx].getSQL()));
        }
        int type = sortTypes[i++];
        if ((type & DESCENDING) != 0) {
            buff.append(" DESC");
        }
        if ((type & NULLS_FIRST) != 0) {
            buff.append(" NULLS FIRST");
        } else if ((type & NULLS_LAST) != 0) {
            buff.append(" NULLS LAST");
        }
    }
    return buff.toString();
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 24 with StatementBuilder

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

the class UpdatableRow method updateRow.

/**
     * Update a row in the database.
     *
     * @param current   the old row
     * @param updateRow the new row
     * @throws SQLException if the row has been deleted
     */
public void updateRow(Value[] current, Value[] updateRow) throws SQLException {
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    appendTableName(buff);
    buff.append(" SET ");
    appendColumnList(buff, true);
    // TODO updatable result set: we could add all current values to the
    // where clause
    // - like this optimistic ('no') locking is possible
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    int j = 1;
    for (int i = 0; i < columnCount; i++) {
        Value v = updateRow[i];
        if (v == null) {
            v = current[i];
        }
        v.set(prep, j++);
    }
    setKey(prep, j, current);
    int count = prep.executeUpdate();
    if (count != 1) {
        // the row has been deleted
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value) PreparedStatement(java.sql.PreparedStatement)

Example 25 with StatementBuilder

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

the class UpdatableRow method readRow.

/**
     * Re-reads a row from the database and updates the values in the array.
     *
     * @param row the values that contain the key
     * @return the row
     */
public Value[] readRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("SELECT ");
    appendColumnList(buff, false);
    buff.append(" FROM ");
    appendTableName(buff);
    appendKeyCondition(buff);
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    setKey(prep, 1, row);
    ResultSet rs = prep.executeQuery();
    if (!rs.next()) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
    Value[] newRow = new Value[columnCount];
    for (int i = 0; i < columnCount; i++) {
        int type = result.getColumnType(i);
        newRow[i] = DataType.readValue(conn.getSession(), rs, i + 1, type);
    }
    return newRow;
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) ResultSet(java.sql.ResultSet) Value(com.wplatform.ddal.value.Value) PreparedStatement(java.sql.PreparedStatement)

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