Skip to content

Commit

Permalink
Fix for issue sdsykes#17 for MSeneadza's comment:
Browse files Browse the repository at this point in the history
BTW, I just tried using v 1.0.13 against a PostgreSQL db. It crashes like this "PG::Error: ERROR: column "1" does not exist
LINE 1: ...erride_fb_pic","users"."id" FROM "users" WHERE "id" IN ("1")"
That "1" is an integer and shouldn't have quotes around it.
  • Loading branch information
bradphilips authored and scryptmouse committed Jun 11, 2014
1 parent 71f356a commit 6323dd5
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/slim_scrooge/callsite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module SlimScrooge
# query is made from a particular place in the app
#
class Callsite
ScroogeComma = ",".freeze
ScroogeComma = ",".freeze
ScroogeRegexJoin = /(?:LEFT|INNER|OUTER|CROSS)*\s*(?:STRAIGHT_JOIN|JOIN)/i

attr_accessor :seen_columns
Expand All @@ -27,11 +27,11 @@ def make_callsite(model_class, original_sql)
def use_scrooge?(model_class, original_sql)
original_sql = original_sql.to_sql if original_sql.respond_to?(:to_sql)

original_sql =~ select_regexp(model_class.table_name) &&
model_class.columns_hash.has_key?(model_class.primary_key) &&
original_sql =~ select_regexp(model_class.table_name) &&
model_class.columns_hash.has_key?(model_class.primary_key) &&
original_sql !~ ScroogeRegexJoin
end

# The regexp that enables us to replace the * from SELECT * with
# the list of columns we actually need
#
Expand Down Expand Up @@ -79,20 +79,19 @@ def scrooged_sql(seen_columns, sql)
sql.gsub(@select_regexp, "SELECT #{scrooge_select_sql(seen_columns)} FROM")
end
end

# List if columns what were not fetched
#
def missing_columns(fetched_columns)
(@all_columns - SimpleSet.new(fetched_columns)) << @primary_key
end

# Returns sql for fetching the unfetched columns for all the rows
# in the result set, specified by primary_keys
#
def reload_sql(primary_keys, fetched_columns)
sql_keys = primary_keys.collect{|pk| "#{connection.quote_column_name(pk)}"}.join(ScroogeComma)
cols = scrooge_select_sql(missing_columns(fetched_columns))
"SELECT #{cols} FROM #{@quoted_table_name} WHERE #{@quoted_primary_key} IN (#{sql_keys})"
"SELECT #{cols} FROM #{@quoted_table_name} WHERE #{@quoted_primary_key} IN (#{primary_keys.join(ScroogeComma)})"
end

def connection
Expand Down

0 comments on commit 6323dd5

Please sign in to comment.