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);
}
}
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();
}
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();
}
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);
}
}
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;
}
Aggregations