Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/org/apache/calcite/rex/RexLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ private static void appendAsJava(@Nullable Comparable value, StringBuilder sb,
(nlsString.getCharsetName() != null)
&& !nlsString.getCharsetName().equals(
CalciteSystemProperty.DEFAULT_CHARSET.value());
sb.append(nlsString.asSql(includeCharset, false));
nlsString.asSql(sb, includeCharset, false);
}
break;
case BOOLEAN:
Expand Down
30 changes: 26 additions & 4 deletions core/src/main/java/org/apache/calcite/util/NlsString.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ public String asSql(boolean prefix, boolean suffix) {
return asSql(prefix, suffix, AnsiSqlDialect.DEFAULT);
}

/** As {@link #asSql(StringBuilder, boolean, boolean, SqlDialect)} but with
* SQL standard dialect. */
public void asSql(StringBuilder buf, boolean prefix, boolean suffix) {
asSql(buf, prefix, suffix, AnsiSqlDialect.DEFAULT);
}

/**
* Returns the string quoted for SQL, for example <code>_ISO-8859-1'is it a
* plane? no it''s superman!'</code>.
Expand All @@ -329,16 +335,32 @@ public String asSql(
boolean suffix,
SqlDialect dialect) {
StringBuilder ret = new StringBuilder();
dialect.quoteStringLiteral(ret, prefix ? charsetName : null, getValue());
asSql(ret, prefix, suffix, dialect);
return ret.toString();
}

/**
* Appends this string quoted for SQL.
*
* @param buf Buffer to append to
* @param prefix if true, prefix the character set name
* @param suffix if true, suffix the collation clause
* @param dialect Dialect
*/
public void asSql(
StringBuilder buf,
boolean prefix,
boolean suffix,
SqlDialect dialect) {
dialect.quoteStringLiteral(buf, prefix ? charsetName : null, getValue());

// NOTE jvs 3-Feb-2005: see FRG-78 for why this should go away
if (false) {
if (suffix && (null != collation)) {
ret.append(" ");
ret.append(collation.toString());
buf.append(" ");
buf.append(collation.toString());
}
}
return ret.toString();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/java/org/apache/calcite/util/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,13 @@ private void checkNameMultimap(String s, NameMultimap<Integer> map) {
assertThat(s2, hasToString(s.toString()));
}

@Test void testNlsStringAsSqlStringBuilder() {
final NlsString s = new NlsString("isn't", "LATIN1", SqlCollation.IMPLICIT);
final StringBuilder buf = new StringBuilder("literal: ");
s.asSql(buf, true, false);
assertThat(buf, hasToString("literal: _LATIN1'isn''t'"));
}

/** Tests that {@link NlsString#compareTo} is consistent with
* {@link NlsString#equals}: {@code x.compareTo(y) == 0} iff
* {@code x.equals(y)} for values that differ in charset or collation. */
Expand Down
Loading