Created
July 19, 2009 12:46
-
-
Save myabc/149904 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/do_jdbc/src/main/java/data_objects/Connection.java b/do_jdbc/src/main/java/data_objects/Connection.java | |
index f0ce819..b857a8e 100644 | |
--- a/do_jdbc/src/main/java/data_objects/Connection.java | |
+++ b/do_jdbc/src/main/java/data_objects/Connection.java | |
@@ -28,8 +28,7 @@ import org.jruby.runtime.ObjectAllocator; | |
import org.jruby.runtime.builtin.IRubyObject; | |
import data_objects.drivers.DriverDefinition; | |
-import java.util.regex.Matcher; | |
-import java.util.regex.Pattern; | |
+import data_objects.drivers.EncodingAwareDriverDefinition; | |
import org.jruby.runtime.callback.Callback; | |
/** | |
@@ -199,27 +198,9 @@ public class Connection extends DORubyObject { | |
if (driver.supportsConnectionEncodings()) { | |
// we set encoding properties, and retry on failure | |
- driver.setEncodingProperty(props, encoding); | |
- try { | |
- conn = DriverManager.getConnection(jdbcUri, props); | |
- } catch (SQLException eex) { | |
- // TODO: Make this non-MySQL specific | |
- Pattern p = Pattern.compile("Unsupported character encoding '(.+)'."); | |
- Matcher m = p.matcher(eex.getMessage()); | |
- | |
- if (m.matches()) { | |
- // re-attempt connection, but this time with UTF-8 | |
- // set as the encoding | |
- runtime.getWarnings().warn(String.format( | |
- "Encoding %s is not a known Ruby encoding for %s\n", | |
- m.group(1), driver.getModuleName())); | |
- driver.setEncodingProperty(props, UTF8_ENCODING); | |
- api.setInstanceVariable(this, "@encoding", runtime.newString(UTF8_ENCODING)); | |
- conn = DriverManager.getConnection(jdbcUri, props); | |
- } else { | |
- throw eex; | |
- } | |
- } | |
+ ((EncodingAwareDriverDefinition) driver).setEncodingProperty(props, encoding); | |
+ conn = ((EncodingAwareDriverDefinition) driver) | |
+ .getConnectionWithEncoding(runtime, this, jdbcUri, props); | |
} else { | |
// if the driver does not use encoding, connect normally | |
conn = DriverManager.getConnection(jdbcUri, props); | |
diff --git a/do_jdbc/src/main/java/data_objects/drivers/AbstractDriverDefinition.java b/do_jdbc/src/main/java/data_objects/drivers/AbstractDriverDefinition.java | |
index ae26a83..13226c4 100644 | |
--- a/do_jdbc/src/main/java/data_objects/drivers/AbstractDriverDefinition.java | |
+++ b/do_jdbc/src/main/java/data_objects/drivers/AbstractDriverDefinition.java | |
@@ -485,10 +485,6 @@ public abstract class AbstractDriverDefinition implements DriverDefinition { | |
// do nothing | |
} | |
- public void setEncodingProperty(Properties props, String encodingName) { | |
- // do nothing | |
- } | |
- | |
public String quoteString(String str) { | |
StringBuffer quotedValue = new StringBuffer(str.length() + 2); | |
quotedValue.append("\'"); | |
diff --git a/do_jdbc/src/main/java/data_objects/drivers/DriverDefinition.java b/do_jdbc/src/main/java/data_objects/drivers/DriverDefinition.java | |
index 15c9e8b..7650b13 100644 | |
--- a/do_jdbc/src/main/java/data_objects/drivers/DriverDefinition.java | |
+++ b/do_jdbc/src/main/java/data_objects/drivers/DriverDefinition.java | |
@@ -143,16 +143,6 @@ public interface DriverDefinition { | |
*/ | |
public void afterConnectionCallback(Connection connection, Map<String, String> query) throws SQLException; | |
- /** | |
- * If the driver supports setting connection encodings, specify the appropriate | |
- * property to set the connection encoding. | |
- * | |
- * @param props | |
- * @param encodingName | |
- * @see #supportsConnectionEncodings() | |
- */ | |
- public void setEncodingProperty(Properties props, String encodingName); | |
- | |
public String quoteString(String str); | |
public String statementToString(Statement s); | |
diff --git a/do_jdbc/src/main/java/data_objects/drivers/EncodingAwareDriverDefinition.java b/do_jdbc/src/main/java/data_objects/drivers/EncodingAwareDriverDefinition.java | |
new file mode 100644 | |
index 0000000..3cbe0aa | |
--- /dev/null | |
+++ b/do_jdbc/src/main/java/data_objects/drivers/EncodingAwareDriverDefinition.java | |
@@ -0,0 +1,37 @@ | |
+package data_objects.drivers; | |
+ | |
+import java.sql.Connection; | |
+import java.sql.SQLException; | |
+import java.util.Properties; | |
+ | |
+import org.jruby.Ruby; | |
+import org.jruby.runtime.builtin.IRubyObject; | |
+ | |
+/** | |
+ * | |
+ * @author alexbcoles | |
+ */ | |
+public interface EncodingAwareDriverDefinition extends DriverDefinition { | |
+ | |
+ /** | |
+ * If the driver supports setting connection encodings, specify the appropriate | |
+ * property to set the connection encoding. | |
+ * | |
+ * @param props | |
+ * @param encodingName | |
+ * @see #supportsConnectionEncodings() | |
+ */ | |
+ void setEncodingProperty(Properties props, String encodingName); | |
+ | |
+ /** | |
+ * | |
+ * @param url | |
+ * @param props | |
+ * @return | |
+ * @throws SQLException | |
+ * @see java.sql.DriverManager#getConnection | |
+ */ | |
+ Connection getConnectionWithEncoding(Ruby runtime, IRubyObject connection, | |
+ String url, Properties props) throws SQLException; | |
+ | |
+} | |
diff --git a/do_mysql/ext-java/src/main/java/do_mysql/MySqlDriverDefinition.java b/do_mysql/ext-java/src/main/java/do_mysql/MySqlDriverDefinition.java | |
index 7d5c859..a21e0ee 100644 | |
--- a/do_mysql/ext-java/src/main/java/do_mysql/MySqlDriverDefinition.java | |
+++ b/do_mysql/ext-java/src/main/java/do_mysql/MySqlDriverDefinition.java | |
@@ -13,11 +13,17 @@ import org.jruby.runtime.builtin.IRubyObject; | |
import data_objects.RubyType; | |
import data_objects.drivers.AbstractDriverDefinition; | |
+import data_objects.drivers.EncodingAwareDriverDefinition; | |
+import java.sql.DriverManager; | |
+import java.util.regex.Matcher; | |
+import java.util.regex.Pattern; | |
-public class MySqlDriverDefinition extends AbstractDriverDefinition { | |
+public class MySqlDriverDefinition extends AbstractDriverDefinition | |
+ implements EncodingAwareDriverDefinition { | |
public final static String URI_SCHEME = "mysql"; | |
public final static String RUBY_MODULE_NAME = "Mysql"; | |
+ private final static String UTF8_ENCODING = "UTF-8"; | |
public MySqlDriverDefinition() { | |
super(URI_SCHEME, RUBY_MODULE_NAME); | |
@@ -85,11 +91,36 @@ public class MySqlDriverDefinition extends AbstractDriverDefinition { | |
return props; | |
} | |
- @Override | |
public void setEncodingProperty(Properties props, String encodingName) { | |
props.put("characterEncoding", encodingName); | |
} | |
+ public java.sql.Connection getConnectionWithEncoding(Ruby runtime, | |
+ IRubyObject connection, String url, Properties props) throws SQLException { | |
+ java.sql.Connection conn; | |
+ try { | |
+ conn = DriverManager.getConnection(url, props); | |
+ } catch (SQLException eex) { | |
+ Pattern p = Pattern.compile("Unsupported character encoding '(.+)'."); | |
+ Matcher m = p.matcher(eex.getMessage()); | |
+ | |
+ if (m.matches()) { | |
+ // re-attempt connection, but this time with UTF-8 | |
+ // set as the encoding | |
+ runtime.getWarnings().warn(String.format( | |
+ "Encoding %s is not a known Ruby encoding for %s\n", | |
+ m.group(1), RUBY_MODULE_NAME)); | |
+ setEncodingProperty(props, UTF8_ENCODING); | |
+ API.setInstanceVariable(connection, | |
+ "@encoding", runtime.newString(UTF8_ENCODING)); | |
+ conn = DriverManager.getConnection(url, props); | |
+ } else { | |
+ throw eex; | |
+ } | |
+ } | |
+ return conn; | |
+ } | |
+ | |
@Override | |
public String quoteString(String str) { | |
StringBuffer quotedValue = new StringBuffer(str.length() + 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment