提交 d0ecd064 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add Value.getSQL(StringBuilder)

上级 07154efb
...@@ -628,7 +628,8 @@ public final class Interval { ...@@ -628,7 +628,8 @@ public final class Interval {
@Override @Override
public String toString() { public String toString() {
return IntervalUtils.intervalToString(qualifier, negative, leading, remaining); return IntervalUtils.appendInterval(new StringBuilder(), getQualifier(), negative, leading, remaining)
.toString();
} }
} }
...@@ -7,6 +7,7 @@ package org.h2.api; ...@@ -7,6 +7,7 @@ package org.h2.api;
import java.io.Serializable; import java.io.Serializable;
import org.h2.util.DateTimeUtils; import org.h2.util.DateTimeUtils;
import org.h2.value.ValueTimestampTimeZone;
/** /**
* How we expose "TIMESTAMP WITH TIME ZONE" in our ResultSets. * How we expose "TIMESTAMP WITH TIME ZONE" in our ResultSets.
...@@ -109,7 +110,9 @@ public class TimestampWithTimeZone implements Serializable, Cloneable { ...@@ -109,7 +110,9 @@ public class TimestampWithTimeZone implements Serializable, Cloneable {
@Override @Override
public String toString() { public String toString() {
return DateTimeUtils.timestampTimeZoneToString(dateValue, timeNanos, timeZoneOffsetMins); StringBuilder builder = new StringBuilder(ValueTimestampTimeZone.MAXIMUM_PRECISION);
DateTimeUtils.appendTimestampTimeZone(builder, dateValue, timeNanos, timeZoneOffsetMins);
return builder.toString();
} }
@Override @Override
......
...@@ -405,7 +405,7 @@ public abstract class Prepared { ...@@ -405,7 +405,7 @@ public abstract class Prepared {
for (Value v : values) { for (Value v : values) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
if (v != null) { if (v != null) {
buff.append(v.getSQL()); v.getSQL(buff.builder());
} }
} }
return buff.toString(); return buff.toString();
......
...@@ -422,10 +422,10 @@ public class ScriptCommand extends ScriptBase { ...@@ -422,10 +422,10 @@ public class ScriptCommand extends ScriptBase {
id = writeLobStream(v); id = writeLobStream(v);
buff.append("SYSTEM_COMBINE_BLOB(").append(id).append(')'); buff.append("SYSTEM_COMBINE_BLOB(").append(id).append(')');
} else { } else {
buff.append(v.getSQL()); v.getSQL(buff.builder());
} }
} else { } else {
buff.append(v.getSQL()); v.getSQL(buff.builder());
} }
} }
buff.append(')'); buff.append(')');
......
...@@ -111,9 +111,7 @@ public abstract class Expression { ...@@ -111,9 +111,7 @@ public abstract class Expression {
* @return the SQL statement * @return the SQL statement
*/ */
public String getSQL() { public String getSQL() {
StringBuilder builder = new StringBuilder(); return getSQL(new StringBuilder()).toString();
getSQL(builder);
return builder.toString();
} }
/** /**
......
...@@ -147,7 +147,7 @@ public class ValueExpression extends Expression { ...@@ -147,7 +147,7 @@ public class ValueExpression extends Expression {
if (this == DEFAULT) { if (this == DEFAULT) {
builder.append("DEFAULT"); builder.append("DEFAULT");
} else { } else {
builder.append(value.getSQL()); value.getSQL(builder);
} }
return builder; return builder;
} }
......
...@@ -38,7 +38,8 @@ public class Constant extends SchemaObjectBase { ...@@ -38,7 +38,8 @@ public class Constant extends SchemaObjectBase {
@Override @Override
public String getCreateSQL() { public String getCreateSQL() {
return "CREATE CONSTANT " + getSQL() + " VALUE " + value.getSQL(); StringBuilder builder = new StringBuilder().append("CREATE CONSTANT ").append(getSQL()).append(" VALUE ");
return value.getSQL(builder).toString();
} }
@Override @Override
......
...@@ -1896,11 +1896,12 @@ public class MetaTable extends Table { ...@@ -1896,11 +1896,12 @@ public class MetaTable extends Table {
case SESSION_STATE: { case SESSION_STATE: {
for (String name : session.getVariableNames()) { for (String name : session.getVariableNames()) {
Value v = session.getVariable(name); Value v = session.getVariable(name);
StringBuilder builder = new StringBuilder().append("SET @").append(name).append(' ');
v.getSQL(builder);
add(rows, add(rows,
// KEY // KEY
"@" + name, "@" + name,
// SQL builder.toString()
"SET @" + name + " " + v.getSQL()
); );
} }
for (Table table : session.getLocalTempTables()) { for (Table table : session.getLocalTempTables()) {
......
...@@ -510,7 +510,8 @@ public class TableLink extends Table { ...@@ -510,7 +510,8 @@ public class TableLink extends Table {
int i = 1; int i = 1;
for (Value v : params) { for (Value v : params) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(i++).append(": ").append(v.getSQL()); buff.append(i++).append(": ");
v.getSQL(buff.builder());
} }
buff.append('}'); buff.append('}');
} }
......
...@@ -991,7 +991,7 @@ public class Recover extends Tool implements DataHandler { ...@@ -991,7 +991,7 @@ public class Recover extends Tool implements DataHandler {
append(" VALUES("); append(" VALUES(");
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0; i < row.getColumnCount(); i++) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(row.getValue(i).getSQL()); row.getValue(i).getSQL(buff.builder());
} }
buff.append(");"); buff.append(");");
writer.println(buff.toString()); writer.println(buff.toString());
......
...@@ -1511,18 +1511,17 @@ public class DateTimeUtils { ...@@ -1511,18 +1511,17 @@ public class DateTimeUtils {
/** /**
* Formats timestamp with time zone as string. * Formats timestamp with time zone as string.
* *
* @param buff the target string builder
* @param dateValue the year-month-day bit field * @param dateValue the year-month-day bit field
* @param timeNanos nanoseconds since midnight * @param timeNanos nanoseconds since midnight
* @param timeZoneOffsetMins the time zone offset in minutes * @param timeZoneOffsetMins the time zone offset in minutes
* @return formatted string
*/ */
public static String timestampTimeZoneToString(long dateValue, long timeNanos, short timeZoneOffsetMins) { public static void appendTimestampTimeZone(StringBuilder buff, long dateValue, long timeNanos,
StringBuilder buff = new StringBuilder(ValueTimestampTimeZone.MAXIMUM_PRECISION); short timeZoneOffsetMins) {
appendDate(buff, dateValue); appendDate(buff, dateValue);
buff.append(' '); buff.append(' ');
appendTime(buff, timeNanos); appendTime(buff, timeNanos);
appendTimeZone(buff, timeZoneOffsetMins); appendTimeZone(buff, timeZoneOffsetMins);
return buff.toString();
} }
/** /**
......
...@@ -383,8 +383,11 @@ public class IntervalUtils { ...@@ -383,8 +383,11 @@ public class IntervalUtils {
} }
/** /**
* Formats interval as a string. * Formats interval as a string and appends it to a specified string
* builder.
* *
* @param buff
* string builder to append to
* @param qualifier * @param qualifier
* qualifier of the interval * qualifier of the interval
* @param negative * @param negative
...@@ -393,12 +396,11 @@ public class IntervalUtils { ...@@ -393,12 +396,11 @@ public class IntervalUtils {
* the value of leading field * the value of leading field
* @param remaining * @param remaining
* the value of all remaining fields * the value of all remaining fields
* @return string representation of the specified interval * @return the specified string builder
*/ */
public static String intervalToString(IntervalQualifier qualifier, boolean negative, long leading, long remaining) public static StringBuilder appendInterval(StringBuilder buff, IntervalQualifier qualifier, boolean negative,
{ long leading, long remaining) {
StringBuilder buff = new StringBuilder().append("INTERVAL "); buff.append("INTERVAL '");
buff.append('\'');
if (negative) { if (negative) {
buff.append('-'); buff.append('-');
} }
...@@ -453,8 +455,7 @@ public class IntervalUtils { ...@@ -453,8 +455,7 @@ public class IntervalUtils {
appendSecondsWithNanos(buff, remaining); appendSecondsWithNanos(buff, remaining);
break; break;
} }
buff.append("' ").append(qualifier); return buff.append("' ").append(qualifier);
return buff.toString();
} }
private static void appendSecondsWithNanos(StringBuilder buff, long nanos) { private static void appendSecondsWithNanos(StringBuilder buff, long nanos) {
......
...@@ -275,7 +275,18 @@ public abstract class Value { ...@@ -275,7 +275,18 @@ public abstract class Value {
* *
* @return the SQL expression * @return the SQL expression
*/ */
public abstract String getSQL(); public String getSQL() {
return getSQL(new StringBuilder()).toString();
}
/**
* Appends the SQL expression for this value to the specified builder.
*
* @param builder
* string builder
* @return the specified string builder
*/
public abstract StringBuilder getSQL(StringBuilder builder);
/** /**
* Get the value type. * Get the value type.
......
...@@ -155,16 +155,19 @@ public class ValueArray extends Value { ...@@ -155,16 +155,19 @@ public class ValueArray extends Value {
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
StatementBuilder buff = new StatementBuilder("("); builder.append('(');
for (Value v : values) { int length = values.length;
buff.appendExceptFirst(", "); for (int i = 0; i < length; i++) {
buff.append(v.getSQL()); if (i > 0) {
builder.append(", ");
}
values[i].getSQL(builder);
} }
if (values.length == 1) { if (length == 1) {
buff.append(','); builder.append(',');
} }
return buff.append(')').toString(); return builder.append(')');
} }
@Override @Override
......
...@@ -50,6 +50,11 @@ public class ValueBoolean extends Value { ...@@ -50,6 +50,11 @@ public class ValueBoolean extends Value {
return getString(); return getString();
} }
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(getString());
}
@Override @Override
public String getString() { public String getString() {
return value ? "TRUE" : "FALSE"; return value ? "TRUE" : "FALSE";
......
...@@ -89,7 +89,12 @@ public class ValueByte extends Value { ...@@ -89,7 +89,12 @@ public class ValueByte extends Value {
@Override @Override
public String getSQL() { public String getSQL() {
return getString(); return Integer.toString(value);
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(value);
} }
@Override @Override
......
...@@ -79,8 +79,8 @@ public class ValueBytes extends Value { ...@@ -79,8 +79,8 @@ public class ValueBytes extends Value {
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
return "X'" + StringUtils.convertBytesToHex(getBytesNoCopy()) + "'"; return builder.append("X'").append(StringUtils.convertBytesToHex(getBytesNoCopy())).append('\'');
} }
@Override @Override
......
...@@ -98,8 +98,10 @@ public class ValueDate extends Value { ...@@ -98,8 +98,10 @@ public class ValueDate extends Value {
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
return "DATE '" + getString() + "'"; builder.append("DATE '");
DateTimeUtils.appendDate(builder, dateValue);
return builder.append('\'');
} }
@Override @Override
......
...@@ -121,6 +121,11 @@ public class ValueDecimal extends Value { ...@@ -121,6 +121,11 @@ public class ValueDecimal extends Value {
return getString(); return getString();
} }
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(getString());
}
@Override @Override
public int getType() { public int getType() {
return Value.DECIMAL; return Value.DECIMAL;
......
...@@ -103,6 +103,20 @@ public class ValueDouble extends Value { ...@@ -103,6 +103,20 @@ public class ValueDouble extends Value {
return getString(); return getString();
} }
@Override
public StringBuilder getSQL(StringBuilder builder) {
if (value == Double.POSITIVE_INFINITY) {
builder.append("POWER(0, -1)");
} else if (value == Double.NEGATIVE_INFINITY) {
builder.append("(-POWER(0, -1))");
} else if (Double.isNaN(value)) {
builder.append("SQRT(-1)");
} else {
builder.append(value);
}
return builder;
}
@Override @Override
public int getType() { public int getType() {
return Value.DOUBLE; return Value.DOUBLE;
......
...@@ -98,6 +98,11 @@ public class ValueEnumBase extends Value { ...@@ -98,6 +98,11 @@ public class ValueEnumBase extends Value {
return StringUtils.quoteStringSQL(label); return StringUtils.quoteStringSQL(label);
} }
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(StringUtils.quoteStringSQL(label));
}
@Override @Override
public String getString() { public String getString() {
return label; return label;
......
...@@ -98,10 +98,23 @@ public class ValueFloat extends Value { ...@@ -98,10 +98,23 @@ public class ValueFloat extends Value {
} else if (value == Float.NEGATIVE_INFINITY) { } else if (value == Float.NEGATIVE_INFINITY) {
return "(-POWER(0, -1))"; return "(-POWER(0, -1))";
} else if (Float.isNaN(value)) { } else if (Float.isNaN(value)) {
// NaN
return "SQRT(-1)"; return "SQRT(-1)";
} }
return getString(); return Float.toString(value);
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
if (value == Float.POSITIVE_INFINITY) {
builder.append("POWER(0, -1)");
} else if (value == Float.NEGATIVE_INFINITY) {
builder.append("(-POWER(0, -1))");
} else if (Float.isNaN(value)) {
builder.append("SQRT(-1)");
} else {
builder.append(value);
}
return builder;
} }
@Override @Override
......
...@@ -268,9 +268,9 @@ public class ValueGeometry extends Value { ...@@ -268,9 +268,9 @@ public class ValueGeometry extends Value {
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
// Using bytes is faster than converting to EWKT. // Using bytes is faster than converting to EWKT.
return "X'" + StringUtils.convertBytesToHex(getBytesNoCopy()) + "'::Geometry"; return builder.append("X'").append(StringUtils.convertBytesToHex(getBytesNoCopy())).append("'::Geometry");
} }
@Override @Override
......
...@@ -122,7 +122,12 @@ public class ValueInt extends Value { ...@@ -122,7 +122,12 @@ public class ValueInt extends Value {
@Override @Override
public String getSQL() { public String getSQL() {
return getString(); return Integer.toString(value);
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(value);
} }
@Override @Override
......
...@@ -138,8 +138,8 @@ public class ValueInterval extends Value { ...@@ -138,8 +138,8 @@ public class ValueInterval extends Value {
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
return getString(); return IntervalUtils.appendInterval(builder, getQualifier(), negative, leading, remaining);
} }
@Override @Override
...@@ -211,7 +211,8 @@ public class ValueInterval extends Value { ...@@ -211,7 +211,8 @@ public class ValueInterval extends Value {
@Override @Override
public String getString() { public String getString() {
return IntervalUtils.intervalToString(getQualifier(), negative, leading, remaining); return IntervalUtils.appendInterval(new StringBuilder(), getQualifier(), negative, leading, remaining)
.toString();
} }
@Override @Override
......
...@@ -571,14 +571,20 @@ public class ValueLob extends Value { ...@@ -571,14 +571,20 @@ public class ValueLob extends Value {
@Override @Override
public String getSQL() { public String getSQL() {
String s;
if (valueType == Value.CLOB) { if (valueType == Value.CLOB) {
s = getString(); return StringUtils.quoteStringSQL(getString());
return StringUtils.quoteStringSQL(s);
} }
byte[] buff = getBytes(); return "X'" + StringUtils.convertBytesToHex(getBytes()) + '\'';
s = StringUtils.convertBytesToHex(buff); }
return "X'" + s + "'";
@Override
public StringBuilder getSQL(StringBuilder builder) {
if (valueType == Value.CLOB) {
builder.append(StringUtils.quoteStringSQL(getString()));
} else {
builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\'');
}
return builder;
} }
@Override @Override
......
...@@ -462,14 +462,20 @@ public class ValueLobDb extends Value { ...@@ -462,14 +462,20 @@ public class ValueLobDb extends Value {
@Override @Override
public String getSQL() { public String getSQL() {
String s;
if (valueType == Value.CLOB) { if (valueType == Value.CLOB) {
s = getString(); return StringUtils.quoteStringSQL(getString());
return StringUtils.quoteStringSQL(s);
} }
byte[] buff = getBytes(); return "X'" + StringUtils.convertBytesToHex(getBytes()) + '\'';
s = StringUtils.convertBytesToHex(buff); }
return "X'" + s + "'";
@Override
public StringBuilder getSQL(StringBuilder builder) {
if (valueType == Value.CLOB) {
builder.append(StringUtils.quoteStringSQL(getString()));
} else {
builder.append("X'").append(StringUtils.convertBytesToHex(getBytes())).append('\'');
}
return builder;
} }
@Override @Override
......
...@@ -147,7 +147,12 @@ public class ValueLong extends Value { ...@@ -147,7 +147,12 @@ public class ValueLong extends Value {
@Override @Override
public String getSQL() { public String getSQL() {
return getString(); return Long.toString(value);
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(value);
} }
@Override @Override
......
...@@ -53,6 +53,11 @@ public class ValueNull extends Value { ...@@ -53,6 +53,11 @@ public class ValueNull extends Value {
return "NULL"; return "NULL";
} }
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append("NULL");
}
@Override @Override
public int getType() { public int getType() {
return Value.NULL; return Value.NULL;
......
...@@ -170,6 +170,11 @@ public class ValueResultSet extends Value { ...@@ -170,6 +170,11 @@ public class ValueResultSet extends Value {
return ""; return "";
} }
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder;
}
@Override @Override
public Value convertPrecision(long precision, boolean force) { public Value convertPrecision(long precision, boolean force) {
if (!force) { if (!force) {
......
...@@ -89,7 +89,12 @@ public class ValueShort extends Value { ...@@ -89,7 +89,12 @@ public class ValueShort extends Value {
@Override @Override
public String getSQL() { public String getSQL() {
return getString(); return Integer.toString(value);
}
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(value);
} }
@Override @Override
......
...@@ -38,6 +38,11 @@ public class ValueString extends Value { ...@@ -38,6 +38,11 @@ public class ValueString extends Value {
return StringUtils.quoteStringSQL(value); return StringUtils.quoteStringSQL(value);
} }
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(StringUtils.quoteStringSQL(value));
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
return other instanceof ValueString return other instanceof ValueString
......
...@@ -138,8 +138,10 @@ public class ValueTime extends Value { ...@@ -138,8 +138,10 @@ public class ValueTime extends Value {
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
return "TIME '" + getString() + "'"; builder.append("TIME '");
DateTimeUtils.appendTime(builder, nanos);
return builder.append('\'');
} }
@Override @Override
......
...@@ -188,8 +188,12 @@ public class ValueTimestamp extends Value { ...@@ -188,8 +188,12 @@ public class ValueTimestamp extends Value {
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
return "TIMESTAMP '" + getString() + "'"; builder.append("TIMESTAMP '");
DateTimeUtils.appendDate(builder, dateValue);
builder.append(' ');
DateTimeUtils.appendTime(builder, timeNanos);
return builder.append('\'');
} }
@Override @Override
......
...@@ -175,12 +175,16 @@ public class ValueTimestampTimeZone extends Value { ...@@ -175,12 +175,16 @@ public class ValueTimestampTimeZone extends Value {
@Override @Override
public String getString() { public String getString() {
return DateTimeUtils.timestampTimeZoneToString(dateValue, timeNanos, timeZoneOffsetMins); StringBuilder builder = new StringBuilder(ValueTimestampTimeZone.MAXIMUM_PRECISION);
DateTimeUtils.appendTimestampTimeZone(builder, dateValue, timeNanos, timeZoneOffsetMins);
return builder.toString();
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
return "TIMESTAMP WITH TIME ZONE '" + getString() + "'"; builder.append("TIMESTAMP WITH TIME ZONE '");
DateTimeUtils.appendTimestampTimeZone(builder, dateValue, timeNanos, timeZoneOffsetMins);
return builder.append('\'');
} }
@Override @Override
......
...@@ -126,8 +126,9 @@ public class ValueUuid extends Value { ...@@ -126,8 +126,9 @@ public class ValueUuid extends Value {
} }
@Override @Override
public String getSQL() { public StringBuilder getSQL(StringBuilder builder) {
return StringUtils.quoteStringSQL(getString()); builder.append('\'');
return addString(builder).append('\'');
} }
@Override @Override
...@@ -149,17 +150,20 @@ public class ValueUuid extends Value { ...@@ -149,17 +150,20 @@ public class ValueUuid extends Value {
@Override @Override
public String getString() { public String getString() {
StringBuilder buff = new StringBuilder(36); return addString(new StringBuilder(36)).toString();
appendHex(buff, high >> 32, 4); }
buff.append('-');
appendHex(buff, high >> 16, 2); private StringBuilder addString(StringBuilder builder) {
buff.append('-'); appendHex(builder, high >> 32, 4);
appendHex(buff, high, 2); builder.append('-');
buff.append('-'); appendHex(builder, high >> 16, 2);
appendHex(buff, low >> 48, 2); builder.append('-');
buff.append('-'); appendHex(builder, high, 2);
appendHex(buff, low, 6); builder.append('-');
return buff.toString(); appendHex(builder, low >> 48, 2);
builder.append('-');
appendHex(builder, low, 6);
return builder;
} }
@Override @Override
......
...@@ -341,6 +341,11 @@ public class TestCustomDataTypesHandler extends TestDb { ...@@ -341,6 +341,11 @@ public class TestCustomDataTypesHandler extends TestDb {
return val.toString(); return val.toString();
} }
@Override
public StringBuilder getSQL(StringBuilder builder) {
return builder.append(val.toString());
}
@Override @Override
public int getType() { public int getType() {
return TestOnlyCustomDataTypesHandler.COMPLEX_DATA_TYPE_ID; return TestOnlyCustomDataTypesHandler.COMPLEX_DATA_TYPE_ID;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论