Mercurial > hg > monetdb-java
diff src/main/java/org/monetdb/jdbc/MonetWrapper.java @ 425:95d15f1d750d
Restructured code (incl removal of duplicate code) for utility functions dq() and sq() which add double or double quotes and add escapes to string values.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Wed, 03 Feb 2021 17:22:03 +0100 (2021-02-03) |
parents | bf9f6b6ecf40 |
children | 3dfcd06fd8ba |
line wrap: on
line diff
--- a/src/main/java/org/monetdb/jdbc/MonetWrapper.java +++ b/src/main/java/org/monetdb/jdbc/MonetWrapper.java @@ -23,7 +23,7 @@ import java.sql.SQLFeatureNotSupportedEx * represented by their proxy, to permit direct access to the resource delegates. * * @author Fabian Groffen, Martin van Dinther - * @version 1.1 + * @version 1.2 */ public class MonetWrapper implements java.sql.Wrapper { /** @@ -77,6 +77,7 @@ public class MonetWrapper implements jav throw new SQLException("Cannot unwrap to interface: " + (iface != null ? iface.getName() : ""), "0A000"); } + /** * Small helper method that formats the "Method ... not implemented" message * and creates a new SQLFeatureNotSupportedException object @@ -88,4 +89,45 @@ public class MonetWrapper implements jav static final SQLFeatureNotSupportedException newSQLFeatureNotSupportedException(final String name) { return new SQLFeatureNotSupportedException("Method " + name + " not implemented", "0A000"); } + + /** + * General utility function to add double quotes around an SQL Indentifier + * such as column or table or schema name in SQL queries. + * It also adds escapes for special characters: double quotes and the escape character + * + * FYI: it is made public as it is also called from util/Exporter.java + * + * @param in the string to quote + * @return the double quoted string + */ + public static final String dq(final String in) { + String ret = in; + if (ret.contains("\\\\")) + // all double slashes in input need to be escaped. + ret = ret.replaceAll("\\\\", "\\\\\\\\"); + if (ret.contains("\"")) + // all double quotes in input need to be escaped. + ret = ret.replaceAll("\"", "\\\\\""); + return "\"" + ret + "\""; + } + + /** + * General utility function to add single quotes around string literals as used in SQL queries. + * It also adds escapes for special characters: single quotes and the escape character + * + * FYI: it is made public as it is also called from util/Exporter.java + * + * @param in the string to quote + * @return the single quoted string + */ + public static final String sq(final String in) { + String ret = in; + if (ret.contains("\\\\")) + // all double slashes in input need to be escaped. + ret = ret.replaceAll("\\\\", "\\\\\\\\"); + if (ret.contains("'")) + // all single quotes in input need to be escaped. + ret = ret.replaceAll("'", "\\\\'"); + return "'" + ret + "'"; + } }