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

Add Value.getSQL(StringBuilder)

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