diff --git a/lib/arjdbc/postgresql/connection_methods.rb b/lib/arjdbc/postgresql/connection_methods.rb index a1b5d7441..4ee4acd57 100644 --- a/lib/arjdbc/postgresql/connection_methods.rb +++ b/lib/arjdbc/postgresql/connection_methods.rb @@ -30,6 +30,7 @@ def postgresql_connection(config) config[:username] ||= ( config[:user] || ENV['PGUSER'] || ENV_JAVA['user.name'] ) config[:password] ||= ENV['PGPASSWORD'] unless config.key?(:password) properties = ( config[:properties] ||= {} ) + properties['stringtype'] ||= 'unspecified' # for simple strings looking like UUIDs # PG :connect_timeout - maximum time to wait for connection to succeed if connect_timeout = ( config[:connect_timeout] || ENV['PGCONNECT_TIMEOUT'] ) properties['socketTimeout'] ||= connect_timeout diff --git a/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java b/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java index 0bd3e5928..6f5590335 100644 --- a/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +++ b/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java @@ -75,7 +75,6 @@ public class PostgreSQLRubyJdbcConnection extends arjdbc.jdbc.RubyJdbcConnection private static final long serialVersionUID = 7235537759545717760L; private static final int HSTORE_TYPE = 100000 + 1111; private static final Pattern doubleValuePattern = Pattern.compile("(-?\\d+(?:\\.\\d+)?)"); - private static final Pattern uuidPattern = Pattern.compile("\\{?\\p{XDigit}{4}(?:-?(\\p{XDigit}{4})){7}\\}?"); // Fuzzy match postgres's allowed formats private static final Map POSTGRES_JDBC_TYPE_FOR = new HashMap<>(32, 1); static { @@ -650,33 +649,6 @@ private void setRangeParameter(final ThreadContext context, statement.setObject(index, pgRange); } - @Override - protected void setStringParameter(final ThreadContext context, - final Connection connection, final PreparedStatement statement, - final int index, final IRubyObject value, - final IRubyObject attribute, final int type) throws SQLException { - - if ( attributeSQLType(context, attribute) == context.nil ) { - /* - We have to check for a uuid here because in some cases - (for example, when doing "exists?" checks, or with legacy binds) - ActiveRecord doesn't send us the actual type of the attribute - and Postgres won't compare a uuid column with a string - */ - final String uuid = value.toString(); - int length = uuid.length(); - - // Checking the length so we don't have the overhead of the regex unless it "looks" like a UUID - if (length >= 32 && length < 40 && uuidPattern.matcher(uuid).matches()) { - setUUIDParameter(statement, index, uuid); - return; - } - } - - super.setStringParameter(context, connection, statement, index, value, attribute, type); - } - - private void setUUIDParameter(final PreparedStatement statement, final int index, final IRubyObject value) throws SQLException { setUUIDParameter(statement, index, value.toString()); diff --git a/test/db/postgresql/types_test.rb b/test/db/postgresql/types_test.rb index 0bf1ff835..0f7196b25 100644 --- a/test/db/postgresql/types_test.rb +++ b/test/db/postgresql/types_test.rb @@ -31,7 +31,8 @@ def self.startup execute "CREATE TABLE postgresql_uuids (" << " id SERIAL PRIMARY KEY," << " guid uuid," << - " compact_guid uuid" << + " compact_guid uuid," << + " string varchar(50)" << ");" execute("CREATE TABLE postgresql_ranges (" << @@ -237,7 +238,7 @@ def setup @connection.execute("INSERT INTO postgresql_timestamp_with_zones (id, time) VALUES (1, '2010-01-01 10:00:00-1')") - @connection.execute("INSERT INTO postgresql_uuids (id, guid, compact_guid) VALUES(1, 'd96c3da0-96c1-012f-1316-64ce8f32c6d8', 'f06c715096c1012f131764ce8f32c6d8')") + @connection.execute("INSERT INTO postgresql_uuids (id, guid, compact_guid, string) VALUES(1, 'd96c3da0-96c1-012f-1316-64ce8f32c6d8', 'f06c715096c1012f131764ce8f32c6d8', '3a66d5d3-c18e-4437-889a-a7c1037bf4ad')") @first_uuid = PostgresqlUUID.find(1) end @@ -570,6 +571,19 @@ def test_network_address_values_ipaddr def test_uuid_values assert_equal 'd96c3da0-96c1-012f-1316-64ce8f32c6d8', @first_uuid.guid assert_equal 'f06c7150-96c1-012f-1317-64ce8f32c6d8', @first_uuid.compact_guid + assert_equal '3a66d5d3-c18e-4437-889a-a7c1037bf4ad', @first_uuid.string + end + + def test_query_string_like_uuid_where_in + assert_equal 1, PostgresqlUUID.where(:string => [ + '3a66d5d3-c18e-4437-889a-a7c1037bf4ad', 'not uuid' + ]).first.id + end + + def test_query_uuid_where_in + assert_equal 1, PostgresqlUUID.where(:guid => [ + 'd96c3da0-96c1-012f-1316-64ce8f32c6d8', 'a2170479-a3a2-42df-b80f-ae78f99cdca7' + ]).first.id end def test_bit_string_values