73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/constant_table_saver.rb', line 73
def find_by_sql(sql, binds = [], preparable: nil, &block)
@cached_records ||= super(relation.to_sql, &nil).each(&:freeze).freeze
@cached_results ||= @cached_records.each_with_object({
to_sql_and_binds(relation) => @cached_records,
to_sql_and_binds(relation.order(relation.table[primary_key].asc).limit(1).arel) => [@cached_records.first].compact,
to_sql_and_binds(relation.order(relation.table[primary_key].desc).limit(1).arel) => [@cached_records.last].compact,
}) do |record, results|
results[to_sql_and_binds(relation.where(relation.table[primary_key].eq(relation.predicate_builder.build_bind_attribute(primary_key, record.id))).limit(1).arel)] = [record]
end.freeze
sql_and_binds = to_sql_and_binds(sql, binds)
sql_and_binds = [sql_and_binds.first, []] unless connection.prepared_statements
if results = @cached_results[sql_and_binds]
results
else
super(sql, binds, preparable: preparable, &block)
end
end
|