Permalink
Browse files

Fix for issue #17 for MSeneadza's comment:

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...
1 parent 71f356a commit 6323dd5335fd63dbbe83c8ef879925a7535880e0 @bradphilips bradphilips committed with scryptmouse Apr 11, 2013
Showing with 7 additions and 8 deletions.
  1. +7 −8 lib/slim_scrooge/callsite.rb
@@ -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
@@ -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
#
@@ -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

0 comments on commit 6323dd5

Please sign in to comment.