提交 4656131a authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add and use Column.getSQL(StringBuilder)

上级 a6c94ddf
...@@ -409,9 +409,13 @@ public class AlterTableAlterColumn extends CommandWithColumns { ...@@ -409,9 +409,13 @@ public class AlterTableAlterColumn extends CommandWithColumns {
if (type == CommandInterface.ALTER_TABLE_ADD_COLUMN && if (type == CommandInterface.ALTER_TABLE_ADD_COLUMN &&
columnsToAdd != null && columnsToAdd.contains(nc)) { columnsToAdd != null && columnsToAdd.contains(nc)) {
Expression def = nc.getDefaultExpression(); Expression def = nc.getDefaultExpression();
columnList.append(def == null ? "NULL" : def.getSQL()); if (def == null) {
columnList.append("NULL");
} else {
def.getSQL(columnList);
}
} else { } else {
columnList.append(nc.getSQL()); nc.getSQL(columnList);
} }
} }
String newTableName = newTable.getName(); String newTableName = newTable.getName();
...@@ -589,7 +593,7 @@ public class AlterTableAlterColumn extends CommandWithColumns { ...@@ -589,7 +593,7 @@ public class AlterTableAlterColumn extends CommandWithColumns {
private void checkNoNullValues(Table table) { private void checkNoNullValues(Table table) {
StringBuilder builder = new StringBuilder("SELECT COUNT(*) FROM "); StringBuilder builder = new StringBuilder("SELECT COUNT(*) FROM ");
table.getSQL(builder).append(" WHERE "); table.getSQL(builder).append(" WHERE ");
builder.append(oldColumn.getSQL()).append(" IS NULL"); oldColumn.getSQL(builder).append(" IS NULL");
String sql = builder.toString(); String sql = builder.toString();
Prepared command = session.prepare(sql); Prepared command = session.prepare(sql);
ResultInterface result = command.query(0); ResultInterface result = command.query(0);
......
...@@ -110,7 +110,8 @@ public class Analyze extends DefineCommand { ...@@ -110,7 +110,8 @@ public class Analyze extends DefineCommand {
// the selectivity is not required // the selectivity is not required
buff.append("MAX(NULL)"); buff.append("MAX(NULL)");
} else { } else {
buff.append("SELECTIVITY(").append(col.getSQL()).append(')'); buff.append("SELECTIVITY(");
col.getSQL(buff.builder()).append(')');
} }
} }
buff.append(" FROM "); buff.append(" FROM ");
......
...@@ -273,7 +273,7 @@ public class Insert extends CommandWithValues implements ResultTarget { ...@@ -273,7 +273,7 @@ public class Insert extends CommandWithValues implements ResultTarget {
table.getSQL(buff.builder()).append('('); table.getSQL(buff.builder()).append('(');
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); c.getSQL(buff.builder());
} }
buff.append(")\n"); buff.append(")\n");
if (insertFromSelect) { if (insertFromSelect) {
...@@ -409,7 +409,7 @@ public class Insert extends CommandWithValues implements ResultTarget { ...@@ -409,7 +409,7 @@ public class Insert extends CommandWithValues implements ResultTarget {
for (Column column : duplicateKeyAssignmentMap.keySet()) { for (Column column : duplicateKeyAssignmentMap.keySet()) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
Expression ex = duplicateKeyAssignmentMap.get(column); Expression ex = duplicateKeyAssignmentMap.get(column);
buff.append(column.getSQL()).append('='); column.getSQL(buff.builder()).append('=');
ex.getSQL(buff.builder()); ex.getSQL(buff.builder());
} }
buff.append(" WHERE "); buff.append(" WHERE ");
......
...@@ -210,7 +210,7 @@ public class Merge extends CommandWithValues { ...@@ -210,7 +210,7 @@ public class Merge extends CommandWithValues {
targetTable.getSQL(buff.builder()).append('('); targetTable.getSQL(buff.builder()).append('(');
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); c.getSQL(buff.builder());
} }
buff.append(')'); buff.append(')');
if (keys != null) { if (keys != null) {
...@@ -218,7 +218,7 @@ public class Merge extends CommandWithValues { ...@@ -218,7 +218,7 @@ public class Merge extends CommandWithValues {
buff.resetCount(); buff.resetCount();
for (Column c : keys) { for (Column c : keys) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); c.getSQL(buff.builder());
} }
buff.append(')'); buff.append(')');
} }
...@@ -279,13 +279,13 @@ public class Merge extends CommandWithValues { ...@@ -279,13 +279,13 @@ public class Merge extends CommandWithValues {
targetTable.getSQL(buff.builder()).append(" SET "); targetTable.getSQL(buff.builder()).append(" SET ");
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()).append("=?"); c.getSQL(buff.builder()).append("=?");
} }
buff.append(" WHERE "); buff.append(" WHERE ");
buff.resetCount(); buff.resetCount();
for (Column c : keys) { for (Column c : keys) {
buff.appendExceptFirst(" AND "); buff.appendExceptFirst(" AND ");
buff.append(c.getSQL()).append("=?"); c.getSQL(buff.builder()).append("=?");
} }
String sql = buff.toString(); String sql = buff.toString();
update = session.prepare(sql); update = session.prepare(sql);
......
...@@ -199,7 +199,7 @@ public class Replace extends CommandWithValues { ...@@ -199,7 +199,7 @@ public class Replace extends CommandWithValues {
table.getSQL(buff.builder()).append('('); table.getSQL(buff.builder()).append('(');
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); c.getSQL(buff.builder());
} }
buff.append(')'); buff.append(')');
buff.append('\n'); buff.append('\n');
...@@ -273,13 +273,13 @@ public class Replace extends CommandWithValues { ...@@ -273,13 +273,13 @@ public class Replace extends CommandWithValues {
table.getSQL(buff.builder()).append(" SET "); table.getSQL(buff.builder()).append(" SET ");
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()).append("=?"); c.getSQL(buff.builder()).append("=?");
} }
buff.append(" WHERE "); buff.append(" WHERE ");
buff.resetCount(); buff.resetCount();
for (Column c : keys) { for (Column c : keys) {
buff.appendExceptFirst(" AND "); buff.appendExceptFirst(" AND ");
buff.append(c.getSQL()).append("=?"); c.getSQL(buff.builder()).append("=?");
} }
String sql = buff.toString(); String sql = buff.toString();
update = session.prepare(sql); update = session.prepare(sql);
......
...@@ -218,7 +218,7 @@ public class Update extends Prepared { ...@@ -218,7 +218,7 @@ public class Update extends Prepared {
builder.append(",\n "); builder.append(",\n ");
} }
Column c = columns.get(i); Column c = columns.get(i);
builder.append(c.getSQL()).append(" = "); c.getSQL(builder).append(" = ");
expressionMap.get(c).getSQL(builder); expressionMap.get(c).getSQL(builder);
} }
if (condition != null) { if (condition != null) {
......
...@@ -69,7 +69,7 @@ public class ExpressionColumn extends Expression { ...@@ -69,7 +69,7 @@ public class ExpressionColumn extends Expression {
if (derivedName != null) { if (derivedName != null) {
Parser.quoteIdentifier(builder, derivedName); Parser.quoteIdentifier(builder, derivedName);
} else { } else {
builder.append(column.getSQL()); column.getSQL(builder);
} }
} else { } else {
Parser.quoteIdentifier(builder, columnName); Parser.quoteIdentifier(builder, columnName);
......
...@@ -178,7 +178,7 @@ public class IndexCondition { ...@@ -178,7 +178,7 @@ public class IndexCondition {
return "FALSE"; return "FALSE";
} }
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append(column.getSQL()); column.getSQL(builder);
switch (compareType) { switch (compareType) {
case Comparison.EQUAL: case Comparison.EQUAL:
builder.append(" = "); builder.append(" = ");
......
...@@ -95,7 +95,7 @@ public class LinkedIndex extends BaseIndex { ...@@ -95,7 +95,7 @@ public class LinkedIndex extends BaseIndex {
buff.appendOnlyFirst(" WHERE "); buff.appendOnlyFirst(" WHERE ");
buff.appendExceptFirst(" AND "); buff.appendExceptFirst(" AND ");
Column col = table.getColumn(i); Column col = table.getColumn(i);
buff.append(col.getSQL()); col.getSQL(buff.builder());
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
buff.append(" IS NULL"); buff.append(" IS NULL");
} else { } else {
...@@ -111,7 +111,7 @@ public class LinkedIndex extends BaseIndex { ...@@ -111,7 +111,7 @@ public class LinkedIndex extends BaseIndex {
buff.appendOnlyFirst(" WHERE "); buff.appendOnlyFirst(" WHERE ");
buff.appendExceptFirst(" AND "); buff.appendExceptFirst(" AND ");
Column col = table.getColumn(i); Column col = table.getColumn(i);
buff.append(col.getSQL()); col.getSQL(buff.builder());
if (v == ValueNull.INSTANCE) { if (v == ValueNull.INSTANCE) {
buff.append(" IS NULL"); buff.append(" IS NULL");
} else { } else {
...@@ -192,7 +192,7 @@ public class LinkedIndex extends BaseIndex { ...@@ -192,7 +192,7 @@ public class LinkedIndex extends BaseIndex {
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0; i < row.getColumnCount(); i++) {
buff.appendExceptFirst("AND "); buff.appendExceptFirst("AND ");
Column col = table.getColumn(i); Column col = table.getColumn(i);
buff.append(col.getSQL()); col.getSQL(buff.builder());
Value v = row.getValue(i); Value v = row.getValue(i);
if (isNull(v)) { if (isNull(v)) {
buff.append(" IS NULL "); buff.append(" IS NULL ");
...@@ -227,7 +227,7 @@ public class LinkedIndex extends BaseIndex { ...@@ -227,7 +227,7 @@ public class LinkedIndex extends BaseIndex {
buff.append(targetTableName).append(" SET "); buff.append(targetTableName).append(" SET ");
for (int i = 0; i < newRow.getColumnCount(); i++) { for (int i = 0; i < newRow.getColumnCount(); i++) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(table.getColumn(i).getSQL()).append('='); table.getColumn(i).getSQL(buff.builder()).append('=');
Value v = newRow.getValue(i); Value v = newRow.getValue(i);
if (v == null) { if (v == null) {
buff.append("DEFAULT"); buff.append("DEFAULT");
...@@ -241,7 +241,7 @@ public class LinkedIndex extends BaseIndex { ...@@ -241,7 +241,7 @@ public class LinkedIndex extends BaseIndex {
for (int i = 0; i < oldRow.getColumnCount(); i++) { for (int i = 0; i < oldRow.getColumnCount(); i++) {
Column col = table.getColumn(i); Column col = table.getColumn(i);
buff.appendExceptFirst(" AND "); buff.appendExceptFirst(" AND ");
buff.append(col.getSQL()); col.getSQL(buff.builder());
Value v = oldRow.getValue(i); Value v = oldRow.getValue(i);
if (isNull(v)) { if (isNull(v)) {
buff.append(" IS NULL"); buff.append(" IS NULL");
......
...@@ -248,6 +248,10 @@ public class Column { ...@@ -248,6 +248,10 @@ public class Column {
return Parser.quoteIdentifier(name); return Parser.quoteIdentifier(name);
} }
public StringBuilder getSQL(StringBuilder builder) {
return Parser.quoteIdentifier(builder, name);
}
public String getName() { public String getName() {
return name; return name;
} }
......
...@@ -35,9 +35,10 @@ public class IndexColumn { ...@@ -35,9 +35,10 @@ public class IndexColumn {
* @return the SQL snippet * @return the SQL snippet
*/ */
public String getSQL() { public String getSQL() {
StringBuilder buff = new StringBuilder(column.getSQL()); StringBuilder builder = new StringBuilder();
SortOrder.typeToString(buff, sortType); column.getSQL(builder);
return buff.toString(); SortOrder.typeToString(builder, sortType);
return builder.toString();
} }
/** /**
......
...@@ -345,7 +345,7 @@ public class TableView extends Table { ...@@ -345,7 +345,7 @@ public class TableView extends Table {
buff.append('('); buff.append('(');
for (Column c : columns) { for (Column c : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(c.getSQL()); c.getSQL(buff.builder());
} }
buff.append(')'); buff.append(')');
} else if (columnTemplates != null) { } else if (columnTemplates != null) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论