lib/connection_manager/using.rb in connection_manager-1.1.2 vs lib/connection_manager/using.rb in connection_manager-1.1.3
- old
+ new
@@ -61,9 +61,64 @@
def parent
@klass.parent
end
+ if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
+ # Rails 3.0
+ def find_by_sql(sql)
+ connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) }
+ end
+
+ elsif ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 1
+ # Rails 3.1
+ def find_by_sql(sql, binds = [])
+ connection.select_all(sanitize_sql(sql), "#{name} Load", binds).collect! { |record| instantiate(record) }
+ end
+
+ elsif ActiveRecord::VERSION::MAJOR == 3
+ # Rails 3.2
+ def find_by_sql(sql, binds = [])
+ logging_query_plan do
+ connection.select_all(sanitize_sql(sql), "#{name} Load", binds).collect! { |record| instantiate(record) }
+ end
+ end
+
+ elsif ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR <= 1
+ #Rails 4.0 & 4.1
+ def find_by_sql(sql, binds = [])
+ result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
+ column_types = {}
+ if result_set.respond_to? :column_types
+ column_types = result_set.column_types
+ else
+ ActiveSupport::Deprecation.warn "the object returned from `select_all` must respond to `column_types`"
+ end
+ result_set.map { |record| instantiate(record, column_types) }
+ end
+
+ else
+ # Edge Rails
+ def find_by_sql(sql, binds = [])
+ result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
+ column_types = result_set.column_types.dup
+ columns_hash.each_key { |k| column_types.delete k }
+ message_bus = ActiveSupport::Notifications.instrumenter
+ payload = {
+ record_count: result_set.length,
+ class_name: name
+ }
+ message_bus.instrument('instantiation.active_record', payload) do
+ result_set.map { |record| instantiate(record, column_types) }
+ end
+ end
+ end
+
+ def count_by_sql(sql)
+ sql = sanitize_conditions(sql)
+ connection.select_value(sql, "#{name} Count").to_i
+ end
+
# Pass all methods to @klass, this ensures objects
# build from the query are the correct class and
# any settings in the model like table_name_prefix
# are used.
def method_missing(name, *args, &blk)