Skip to content
Open
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
28 changes: 20 additions & 8 deletions java/com/google/zetasql/ZetaSQLStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package com.google.zetasql;

import com.google.common.base.Preconditions;
import com.google.common.escape.Escaper;
import com.google.common.escape.Escapers;
import com.google.common.io.BaseEncoding;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.joda.time.DateTime;
Expand All @@ -28,6 +31,15 @@
public class ZetaSQLStrings {
private static final DateTime EPOCH = new DateTime(1970, 1, 1, 0, 0);
private static final Charset UTF_8 = StandardCharsets.UTF_8;
private static final Escaper BACKTICK_ESCAPER = Escapers.builder()
.addEscape('`', "\\`")
.build();
private static final Escaper DOUBLE_QUOTE_ESCAPER = Escapers.builder()
.addEscape('"', "\\\"")
.build();
private static final Escaper SINGLE_QUOTE_ESCAPER = Escapers.builder()
.addEscape('\'', "\\'")
.build();

/**
* Convert a string to a ZetaSQL identifier literal.
Expand All @@ -36,7 +48,7 @@ public class ZetaSQLStrings {
* @return Legal ZetaSQL identifier converted from the string.
*/
public static String toIdentifierLiteral(String str) {
throw new UnsupportedOperationException();
return '`' + BACKTICK_ESCAPER.escape(str) + '`';
}

/**
Expand All @@ -48,7 +60,7 @@ public static String toIdentifierLiteral(String str) {
* @return Quoted and escaped ZetaSQL bytes literal.
*/
public static String toBytesLiteral(byte[] bytes) {
throw new UnsupportedOperationException();
return toSingleQuotedBytesLiteral(bytes);
}

/**
Expand All @@ -71,7 +83,7 @@ public static String toBytesLiteral(String str) {
* @return Quoted and escaped ZetaSQL bytes literal.
*/
public static String toSingleQuotedBytesLiteral(byte[] bytes) {
throw new UnsupportedOperationException();
return "b'" + BaseEncoding.base16().encode(bytes) + '\'';
}

/**
Expand All @@ -93,7 +105,7 @@ public static String toSingleQuotedBytesLiteral(String str) {
* @return Quoted and escaped ZetaSQL bytes literal.
*/
public static String toDoubleQuotedBytesLiteral(byte[] bytes) {
throw new UnsupportedOperationException();
return "b\"" + BaseEncoding.base16().encode(bytes) + '"';
}

/**
Expand All @@ -115,7 +127,7 @@ public static String toDoubleQuotedBytesLiteral(String str) {
* @return Quoted and escaped ZetaSQL string literal.
*/
public static String toStringLiteral(String str) {
throw new UnsupportedOperationException();
return toSingleQuotedStringLiteral(str);
}

/**
Expand All @@ -126,7 +138,7 @@ public static String toStringLiteral(String str) {
* @return Quoted and escaped ZetaSQL string literal.
*/
public static String toSingleQuotedStringLiteral(String str) {
throw new UnsupportedOperationException();
return '\'' + SINGLE_QUOTE_ESCAPER.escape(str) + '\'';
}

/**
Expand All @@ -137,7 +149,7 @@ public static String toSingleQuotedStringLiteral(String str) {
* @return Quoted and escaped ZetaSQL string literal.
*/
public static String toDoubleQuotedStringLiteral(String str) {
throw new UnsupportedOperationException();
return '"' + DOUBLE_QUOTE_ESCAPER.escape(str) + '"';
}

/**
Expand All @@ -153,7 +165,7 @@ public static String convertDateToString(int date) {
public static String convertSimpleValueToString(Value value, boolean verbose) {
Type type = value.getType();
Preconditions.checkArgument(type.isSimpleType());
throw new UnsupportedOperationException();
return value.getProto().toString().trim();
}

/**
Expand Down