Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/arjdbc/abstract/database_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ def internal_exec_query(sql, name = nil, binds = NO_BINDS, prepare: false, async

binds = convert_legacy_binds_to_attributes(binds) if binds.first.is_a?(Array)

# puts "[1]internal----->sql: #{sql}, binds: #{binds}"
type_casted_binds = type_casted_binds(binds)
# puts "[2]internal----->sql: #{type_casted_binds.size}, binds: #{type_casted_binds}"

with_raw_connection do |conn|
if without_prepared_statement?(binds)
log(sql, name, async: async) { conn.execute_query(sql) }
else
log(sql, name, binds, async: async) do
log(sql, name, type_casted_binds, async: async) do
# this is different from normal AR that always caches
cached_statement = fetch_cached_statement(sql) if prepare && @jdbc_statement_cache_enabled
conn.execute_prepared_query(sql, binds, cached_statement)
conn.execute_prepared_query(sql, type_casted_binds, cached_statement)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion src/java/arjdbc/jdbc/RubyJdbcConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2453,7 +2453,8 @@ protected void setStatementParameter(final ThreadContext context,

// All the set methods were calling this first so save a method call in the nil case
if ( value == context.nil ) {
statement.setNull(index, type);
// statement.setNull(index, type);
statement.setObject(index, null);
return;
}

Expand Down
36 changes: 18 additions & 18 deletions src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -661,24 +661,24 @@ protected void setStringParameter(final ThreadContext context,
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);
// 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;
// }
// }

statement.setObject(index, value.asString().toString(), Types.OTHER);
}


Expand Down
10 changes: 10 additions & 0 deletions test/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,16 @@ def test_string_id
assert_equal "some_string", f.id
end

def test_time_bind_param
with_timezone_config default: :utc, zone: 'Australia/Melbourne' do
time = Time.zone.local(2000, 1, 1, 16)
entry = Entry.create!(updated_on: time + 1.hour)
sql = 'updated_on >= ?'
entries = Entry.where(sql, time)
assert_equal 1, entries.size
end
end

def test_handles_quotes_inside_of_strings
content_json = {
"comments" => [
Expand Down
Loading