Skip to content

Commit 8a82739

Browse files
committed
Postgres, pass type casted binds for select queries
1 parent 7009b5c commit 8a82739

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

lib/arjdbc/abstract/database_statements.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ def internal_exec_query(sql, name = nil, binds = NO_BINDS, prepare: false, async
4444

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

47+
# puts "internal----->sql: #{sql}, binds: #{binds}"
48+
type_casted_binds = type_casted_binds(binds)
49+
4750
with_raw_connection do |conn|
4851
if without_prepared_statement?(binds)
4952
log(sql, name, async: async) { conn.execute_query(sql) }
5053
else
51-
log(sql, name, binds, async: async) do
54+
log(sql, name, type_casted_binds, async: async) do
5255
# this is different from normal AR that always caches
5356
cached_statement = fetch_cached_statement(sql) if prepare && @jdbc_statement_cache_enabled
54-
conn.execute_prepared_query(sql, binds, cached_statement)
57+
conn.execute_prepared_query(sql, type_casted_binds, cached_statement)
5558
end
5659
end
5760
end

src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ protected void setStringParameter(final ThreadContext context,
691691
}
692692
}
693693

694-
super.setStringParameter(context, connection, statement, index, value, attribute, type);
694+
statement.setObject(index, value.asString().toString(), Types.OTHER);
695695
}
696696

697697

test/simple.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,16 @@ def test_string_id
890890
assert_equal "some_string", f.id
891891
end
892892

893+
def test_time_bind_param
894+
with_timezone_config default: :utc, zone: 'Australia/Melbourne' do
895+
time = Time.zone.local(2000, 1, 1, 16)
896+
entry = Entry.create!(updated_on: time + 1.hour)
897+
sql = 'updated_on >= ?'
898+
entries = Entry.where(sql, time)
899+
assert_equal 1, entries.size
900+
end
901+
end
902+
893903
def test_handles_quotes_inside_of_strings
894904
content_json = {
895905
"comments" => [

0 commit comments

Comments
 (0)