annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
1 /*
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
2 * This Source Code Form is subject to the terms of the Mozilla Public
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
5 *
406
bf9f6b6ecf40 Update Copyright year.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 391
diff changeset
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
7 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
8
391
f523727db392 Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 350
diff changeset
9 package org.monetdb.jdbc;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
10
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
11 import java.sql.SQLException;
262
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
12 import java.sql.SQLFeatureNotSupportedException;
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
13
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
14 /**
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
15 * A Wrapper class which provide the ability to retrieve the delegate instance
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
16 * when the instance in question is in fact a proxy class.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
17 *
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
18 * The wrapper pattern is employed by many JDBC driver implementations to provide
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
19 * extensions beyond the traditional JDBC API that are specific to a data source.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
20 * Developers may wish to gain access to these resources that are wrapped (the delegates)
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
21 * as proxy class instances representing the the actual resources.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
22 * This class contains a standard mechanism to access these wrapped resources
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
23 * represented by their proxy, to permit direct access to the resource delegates.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
24 *
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
25 * @author Fabian Groffen, Martin van Dinther
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
26 * @version 1.2
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
27 */
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
28 public class MonetWrapper implements java.sql.Wrapper {
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
29 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
30 * Returns true if this either implements the interface argument or
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
31 * is directly or indirectly a wrapper for an object that does.
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
32 * Returns false otherwise. If this implements the interface then
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
33 * return true, else if this is a wrapper then return the result of
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
34 * recursively calling <code>isWrapperFor</code> on the wrapped object.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
35 * If this does not implement the interface and is not a wrapper, return
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
36 * false. This method should be implemented as a low-cost operation
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
37 * compared to <code>unwrap</code> so that callers can use this method to avoid
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
38 * expensive <code>unwrap</code> calls that may fail.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
39 * If this method returns true then calling <code>unwrap</code> with the same argument should succeed.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
40 *
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
41 * @param iface a Class defining an interface.
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
42 * @return true if this implements the interface or directly or indirectly wraps an object that does.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
43 * @throws SQLException if an error occurs while determining whether this is a wrapper
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
44 * for an object with the given interface.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
45 * @since 1.6
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
46 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
47 @Override
295
003ae6d881db Add "final" keyword to method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 262
diff changeset
48 public boolean isWrapperFor(final Class<?> iface) throws SQLException {
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
49 return iface != null && iface.isAssignableFrom(getClass());
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
50 }
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
51
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
52 /**
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
53 * Returns an object that implements the given interface to allow
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
54 * access to non-standard methods, or standard methods not exposed by the proxy.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
55 * The result may be either the object found to implement the interface
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
56 * or a proxy for that object.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
57 * If the receiver implements the interface then the result is the receiver
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
58 * or a proxy for the receiver.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
59 * If the receiver is a wrapper and the wrapped object implements the interface
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
60 * then the result is the wrapped object or a proxy for the wrapped object.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
61 * Otherwise return the result of calling <code>unwrap</code> recursively on
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
62 * the wrapped object or a proxy for that result.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
63 * If the receiver is not a wrapper and does not implement the interface,
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
64 * then an <code>SQLException</code> is thrown.
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
65 *
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
66 * @param iface A Class defining an interface that the result must implement.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
67 * @return an object that implements the interface. May be a proxy for the actual implementing object.
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
68 * @throws SQLException If no object found that implements the interface
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
69 * @since 1.6
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
70 */
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
71 @Override
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
72 @SuppressWarnings("unchecked")
295
003ae6d881db Add "final" keyword to method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 262
diff changeset
73 public <T> T unwrap(final Class<T> iface) throws SQLException {
18
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
74 if (isWrapperFor(iface)) {
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
75 return (T) this;
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
76 }
8e57d20b5e80 Corrected implementation of java.sql.Wrapper methods isWrapperFor() and unwrap().
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 0
diff changeset
77 throw new SQLException("Cannot unwrap to interface: " + (iface != null ? iface.getName() : ""), "0A000");
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
78 }
262
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
79
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
80
262
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
81 /**
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
82 * Small helper method that formats the "Method ... not implemented" message
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
83 * and creates a new SQLFeatureNotSupportedException object
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
84 * whose SQLState is set to "0A000": feature not supported.
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
85 *
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
86 * @param name the method name
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
87 * @return a new created SQLFeatureNotSupportedException object with SQLState 0A000
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
88 */
295
003ae6d881db Add "final" keyword to method arguments and local variables where possible.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 262
diff changeset
89 static final SQLFeatureNotSupportedException newSQLFeatureNotSupportedException(final String name) {
262
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
90 return new SQLFeatureNotSupportedException("Method " + name + " not implemented", "0A000");
2d62ca1f758b Moved utility method newSQLFeatureNotSupportedException(String name) from MonetPreparedStatement.java and MonetResultSet.java
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 261
diff changeset
91 }
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
92
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
93 /**
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
94 * General utility function to add double quotes around an SQL Indentifier
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
95 * such as column or table or schema name in SQL queries.
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
96 * It also adds escapes for special characters: double quotes and the escape character
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
97 *
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
98 * FYI: it is made public as it is also called from util/Exporter.java
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
99 *
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
100 * @param in the string to quote
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
101 * @return the double quoted string
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
102 */
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
103 public static final String dq(final String in) {
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
104 String ret = in;
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
105 if (ret.contains("\\\\"))
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
106 // all double slashes in input need to be escaped.
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
107 ret = ret.replaceAll("\\\\", "\\\\\\\\");
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
108 if (ret.contains("\""))
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
109 // all double quotes in input need to be escaped.
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
110 ret = ret.replaceAll("\"", "\\\\\"");
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
111 return "\"" + ret + "\"";
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
112 }
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
113
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
114 /**
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
115 * General utility function to add single quotes around string literals as used in SQL queries.
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
116 * It also adds escapes for special characters: single quotes and the escape character
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
117 *
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
118 * FYI: it is made public as it is also called from util/Exporter.java
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
119 *
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
120 * @param in the string to quote
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
121 * @return the single quoted string
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
122 */
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
123 public static final String sq(final String in) {
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
124 String ret = in;
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
125 if (ret.contains("\\\\"))
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
126 // all double slashes in input need to be escaped.
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
127 ret = ret.replaceAll("\\\\", "\\\\\\\\");
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
128 if (ret.contains("'"))
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
129 // all single quotes in input need to be escaped.
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
130 ret = ret.replaceAll("'", "\\\\'");
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
131 return "'" + ret + "'";
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.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents: 406
diff changeset
132 }
0
a5a898f6886c Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff changeset
133 }