Skip to content

Commit c56c7b4

Browse files
authored
Merge pull request #1170 from JesseChavez/fix_some_basic_stuff
Fix using gem from local and basic raw prepared statement with Time.current (active Support time with zone)
2 parents 9117182 + c92234d commit c56c7b4

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Diff for: lib/arjdbc.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@
1212
rescue LoadError => e
1313
warn "activerecord-jdbc-adapter failed to load railtie: #{e.inspect}"
1414
end if defined?(Rails) && ActiveRecord::VERSION::MAJOR >= 3
15+
16+
ActiveSupport.on_load(:active_record) do
17+
ActiveRecord::ConnectionAdapters.register(
18+
"sqlite3", "ActiveRecord::ConnectionAdapters::SQLite3Adapter", "arjdbc/sqlite3/adapter"
19+
)
20+
ActiveRecord::ConnectionAdapters.register(
21+
"postgresql", "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter", "arjdbc/postgresql/adapter"
22+
)
23+
ActiveRecord::ConnectionAdapters.register(
24+
"mysql2", "ActiveRecord::ConnectionAdapters::Mysql2Adapter", "arjdbc/mysql/adapter"
25+
)
26+
end
1527
else
1628
warn "activerecord-jdbc-adapter is for use with JRuby only"
1729
end
1830

19-
require 'arjdbc/version'
31+
require 'arjdbc/version'

Diff for: src/java/arjdbc/jdbc/RubyJdbcConnection.java

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public class RubyJdbcConnection extends RubyObject {
126126
private IRubyObject adapter; // the AbstractAdapter instance we belong to
127127
private volatile boolean connected = true;
128128
private RubyClass attributeClass;
129+
private RubyClass timeZoneClass;
129130

130131
private boolean lazy = false; // final once set on initialize
131132
private boolean jndi; // final once set on initialize
@@ -135,6 +136,7 @@ public class RubyJdbcConnection extends RubyObject {
135136
protected RubyJdbcConnection(Ruby runtime, RubyClass metaClass) {
136137
super(runtime, metaClass);
137138
attributeClass = runtime.getModule("ActiveModel").getClass("Attribute");
139+
timeZoneClass = runtime.getModule("ActiveSupport").getClass("TimeWithZone");
138140
}
139141

140142
private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
@@ -2441,6 +2443,9 @@ protected void setStatementParameter(final ThreadContext context,
24412443
if (attributeClass.isInstance(attribute)) {
24422444
type = jdbcTypeForAttribute(context, attribute);
24432445
value = valueForDatabase(context, attribute);
2446+
} else if (timeZoneClass.isInstance(attribute)) {
2447+
type = jdbcTypeFor("timestamp");
2448+
value = attribute;
24442449
} else {
24452450
type = jdbcTypeForPrimitiveAttribute(context, attribute);
24462451
value = attribute;

Diff for: test/simple.rb

+9
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ def test_create_partial_new_entry
243243
Entry.create(:title => "Bloh")
244244
end
245245

246+
def test_find_time_with_raw_prepared_statement
247+
user = User.new(login: "jessec")
248+
249+
assert_equal true, user.save
250+
251+
right_now = Time.current
252+
assert_equal 1, User.where("created_at <= ?", right_now).to_a.size
253+
end
254+
246255
def test_find_and_update_entry
247256
title = "First post!"
248257
content = "Hello from JRuby on Rails!"

0 commit comments

Comments
 (0)