Skip to content
Merged
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
14 changes: 13 additions & 1 deletion lib/arjdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@
rescue LoadError => e
warn "activerecord-jdbc-adapter failed to load railtie: #{e.inspect}"
end if defined?(Rails) && ActiveRecord::VERSION::MAJOR >= 3

ActiveSupport.on_load(:active_record) do
ActiveRecord::ConnectionAdapters.register(
"sqlite3", "ActiveRecord::ConnectionAdapters::SQLite3Adapter", "arjdbc/sqlite3/adapter"
)
ActiveRecord::ConnectionAdapters.register(
"postgresql", "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter", "arjdbc/postgresql/adapter"
)
ActiveRecord::ConnectionAdapters.register(
"mysql2", "ActiveRecord::ConnectionAdapters::Mysql2Adapter", "arjdbc/mysql/adapter"
)
end
else
warn "activerecord-jdbc-adapter is for use with JRuby only"
end

require 'arjdbc/version'
require 'arjdbc/version'
5 changes: 5 additions & 0 deletions src/java/arjdbc/jdbc/RubyJdbcConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public class RubyJdbcConnection extends RubyObject {
private IRubyObject adapter; // the AbstractAdapter instance we belong to
private volatile boolean connected = true;
private RubyClass attributeClass;
private RubyClass timeZoneClass;

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

private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
Expand Down Expand Up @@ -2441,6 +2443,9 @@ protected void setStatementParameter(final ThreadContext context,
if (attributeClass.isInstance(attribute)) {
type = jdbcTypeForAttribute(context, attribute);
value = valueForDatabase(context, attribute);
} else if (timeZoneClass.isInstance(attribute)) {
type = jdbcTypeFor("timestamp");
value = attribute;
} else {
type = jdbcTypeForPrimitiveAttribute(context, attribute);
value = attribute;
Expand Down
9 changes: 9 additions & 0 deletions test/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ def test_create_partial_new_entry
Entry.create(:title => "Bloh")
end

def test_find_time_with_raw_prepared_statement
user = User.new(login: "jessec")

assert_equal true, user.save

right_now = Time.current
assert_equal 1, User.where("created_at <= ?", right_now).to_a.size
end

def test_find_and_update_entry
title = "First post!"
content = "Hello from JRuby on Rails!"
Expand Down