diff --git a/Gemfile b/Gemfile index bc0bf9d1c..18a9547b0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ source 'http://rubygems.org' -gem 'mysql2', '0.2.6' +#gem 'mysql2', '0.2.6' +gem 'pg' gem 'rails', '3.0.3' gem 'foreigner', '0.9.1' diff --git a/Gemfile.lock b/Gemfile.lock index 175aa0d7c..73e4ead76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -226,7 +226,6 @@ GEM multi_json (1.0.3) multi_xml (0.2.2) multipart-post (1.1.2) - mysql2 (0.2.6) net-ldap (0.2.2) net-scp (1.0.4) net-ssh (>= 1.99.1) @@ -283,6 +282,7 @@ GEM oa-openid (= 0.2.6) open4 (1.0.1) orm_adapter (0.0.5) + pg (0.11.0) polyglot (0.3.1) pyu-ruby-sasl (0.0.3.3) rack (1.2.3) @@ -430,11 +430,11 @@ DEPENDENCIES linecache (= 0.43) mini_magick (= 3.2) mongrel - mysql2 (= 0.2.6) newrelic_rpm nokogiri ohai (= 0.5.8) omniauth (= 0.2.6) + pg rails (= 3.0.3) rcov resque (= 1.10.0) diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index f953ab221..d31001afa 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -42,11 +42,27 @@ module Diaspora posts_from_self = posts_from_self.select(select_clause).limit(opts[:limit]).order(order_with_table).where(Post.arel_table[order_field].lt(opts[:max_time])) all_posts = "(#{posts_from_others.to_sql}) UNION ALL (#{posts_from_self.to_sql}) ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}" - post_ids = Post.connection.execute(all_posts).map{|r| r.first} + + post_ids = Post.connection.execute(all_posts).map {|post| id_for(post) } Post.where(:id => post_ids).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table) end + # Determine, cache, and execute the method call needed to extract the id from a raw result row. + # Returns row["id"] for PostgreSQL + # Returns row.first for everything else (MYSQL) + # + # @param row The row to get the id from. + # @return The id of the database row passed in. + def id_for row + @@id_method_for_row ||= if Post.connection.class == ActiveRecord::ConnectionAdapters::PostgreSQLAdapter + [:[], "id"] + else + :first + end + row.send(*@@id_method_for_row) + end + def visible_photos(opts = {}) visible_posts(opts.merge(:type => 'Photo')) end