Modified:
branches/branch_5_1/connector-j/src/com/mysql/jdbc/PreparedStatement.java
Log:
implemented PreparedStatement.setNString method
Modified: branches/branch_5_1/connector-j/src/com/mysql/jdbc/PreparedStatement.java
===================================================================
--- branches/branch_5_1/connector-j/src/com/mysql/jdbc/PreparedStatement.java 2006-01-25 20:46:03 UTC (rev 4846)
+++ branches/branch_5_1/connector-j/src/com/mysql/jdbc/PreparedStatement.java 2006-01-25 20:56:22 UTC (rev 4847)
@@ -2410,11 +2410,113 @@
throw new ToBeImplementedException();
}
- public void setNString(int parameterIndex, String value)
- throws SQLException {
- throw new ToBeImplementedException();
- }
+ /**
+ * Set a parameter to a Java String value. The driver converts this to a SQL
+ * VARCHAR or LONGVARCHAR value with introducer _utf8 (depending on the
+ * arguments size relative to the driver's limits on VARCHARs) when it sends
+ * it to the database.
+ *
+ * @param parameterIndex
+ * the first parameter is 1...
+ * @param x
+ * the parameter value
+ *
+ * @exception SQLException
+ * if a database access error occurs
+ */
+ public void setNString(int parameterIndex, String x) throws SQLException {
+ // if the passed string is null, then set this column to null
+ if (x == null) {
+ setNull(parameterIndex, Types.CHAR);
+ } else {
+ int stringLength = x.length();
+ // Add introducer _utf8 for NATIONAL CHARACTER
+ StringBuffer buf = new StringBuffer((int) (x.length() * 1.1 + 4));
+ buf.append("_utf8");
+ buf.append('\'');
+
+ //
+ // Note: buf.append(char) is _faster_ than
+ // appending in blocks, because the block
+ // append requires a System.arraycopy()....
+ // go figure...
+ //
+
+ for (int i = 0; i < stringLength; ++i) {
+ char c = x.charAt(i);
+
+ switch (c) {
+ case 0: /* Must be escaped for 'mysql' */
+ buf.append('\\');
+ buf.append('0');
+
+ break;
+
+ case '\n': /* Must be escaped for logs */
+ buf.append('\\');
+ buf.append('n');
+
+ break;
+
+ case '\r':
+ buf.append('\\');
+ buf.append('r');
+
+ break;
+
+ case '\\':
+ buf.append('\\');
+ buf.append('\\');
+
+ break;
+
+ case '\'':
+ buf.append('\\');
+ buf.append('\'');
+
+ break;
+
+ case '"': /* Better safe than sorry */
+ if (this.usingAnsiMode) {
+ buf.append('\\');
+ }
+
+ buf.append('"');
+
+ break;
+
+ case '\032': /* This gives problems on Win32 */
+ buf.append('\\');
+ buf.append('Z');
+
+ break;
+
+ default:
+ buf.append(c);
+ }
+ }
+
+ buf.append('\'');
+
+ String parameterAsString = buf.toString();
+
+ byte[] parameterAsBytes = null;
+
+ if (!this.isLoadDataQuery) {
+ parameterAsBytes = StringUtils.getBytes(parameterAsString,
+ this.connection.getCharsetConverter("UTF-8"), "UTF-8",
+ this.connection.getServerCharacterEncoding(),
+ this.connection.parserKnowsUnicode());
+ } else {
+ // Send with platform character encoding
+ parameterAsBytes = parameterAsString.getBytes();
+ }
+
+ setInternal(parameterIndex, parameterAsBytes);
+ }
+ }
+
/**
* Set a parameter to SQL NULL
*
| Thread |
|---|
| • Connector/J commit: r4847 - branches/branch_5_1/connector-j/src/com/mysql/jdbc | tikeda | 25 Jan |