A platform for community discussion. Free, open, simple.
DEV: remove all calls to SqlBuilder use DB.build instead
Samcb824a6b338b
diff --git a/Gemfile.lock b/Gemfile.lockindex 1f0490eff79..248845f02b6 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -43,8 +43,8 @@ GEMrake (>= 10.4, < 13.0)arel (9.0.0)ast (2.4.0)- aws-eventstream (1.0.0)- aws-partitions (1.91.0)+ aws-eventstream (1.0.1)+ aws-partitions (1.92.0)aws-sdk-core (3.21.2)aws-eventstream (~> 1.0)aws-partitions (~> 1.0)@@ -53,7 +53,7 @@ GEMaws-sdk-kms (1.5.0)aws-sdk-core (~> 3)aws-sigv4 (~> 1.0)- aws-sdk-s3 (1.13.0)+ aws-sdk-s3 (1.14.0)aws-sdk-core (~> 3, >= 3.21.2)aws-sdk-kms (~> 1)aws-sigv4 (~> 1.0)@@ -108,7 +108,7 @@ GEMerubi (1.7.1)excon (0.62.0)execjs (2.7.0)- exifr (1.2.5)+ exifr (1.3.4)fabrication (2.20.1)fakeweb (1.3.0)faraday (0.12.2)@@ -130,7 +130,7 @@ GEMguess_html_encoding (0.0.11)hashdiff (0.3.7)hashie (3.5.7)- highline (1.7.10)+ highline (2.0.0)hiredis (0.6.1)hkdf (0.3.0)htmlentities (4.3.4)@@ -176,7 +176,7 @@ GEMmini_portile2 (2.3.0)mini_racer (0.1.15)libv8 (~> 6.3)- mini_sql (0.1.5)+ mini_sql (0.1.9)mini_suffix (0.3.0)ffi (~> 1.9)minitest (5.11.3)@@ -261,7 +261,7 @@ GEMrack-openid (1.3.1)rack (>= 1.1.0)ruby-openid (>= 2.1.8)- rack-protection (2.0.2)+ rack-protection (2.0.3)rackrack-test (1.0.0)rack (>= 1.0, < 3)diff --git a/app/models/category_user.rb b/app/models/category_user.rbindex 0ae88ecd423..321089335eb 100644--- a/app/models/category_user.rb+++ b/app/models/category_user.rb@@ -64,13 +64,13 @@ class CategoryUser < ActiveRecord::Basedef self.auto_track(opts = {})- builder = SqlBuilder.new <<SQL- UPDATE topic_users tu- SET notification_level = :tracking,- notifications_reason_id = :auto_track_category- FROM topics t, category_users cu- /*where*/-SQL+ builder = DB.build <<~SQL+ UPDATE topic_users tu+ SET notification_level = :tracking,+ notifications_reason_id = :auto_track_category+ FROM topics t, category_users cu+ /*where*/+ SQLbuilder.where("tu.topic_id = t.id ANDcu.category_id = t.category_id AND@@ -90,45 +90,47 @@ SQLbuilder.where("tu.user_id = :user_id", user_id: user_id)end- builder.exec(tracking: notification_levels[:tracking],- regular: notification_levels[:regular],- auto_track_category: TopicUser.notification_reasons[:auto_track_category])+ builder.exec(+ tracking: notification_levels[:tracking],+ regular: notification_levels[:regular],+ auto_track_category: TopicUser.notification_reasons[:auto_track_category]+ )enddef self.auto_watch(opts = {})- builder = SqlBuilder.new <<SQL- UPDATE topic_users tu- SET notification_level =- CASE WHEN should_track THEN :tracking- WHEN should_watch THEN :watching- ELSE notification_level- END,- notifications_reason_id =- CASE WHEN should_track THEN null- WHEN should_watch THEN :auto_watch_category- ELSE notifications_reason_id- END- FROM (- SELECT tu1.topic_id,- tu1.user_id,- CASE WHEN- cu.user_id IS NULL AND tu1.notification_level = :watching AND tu1.notifications_reason_id = :auto_watch_category THEN true+ builder = DB.build <<~SQL+ UPDATE topic_users tu+ SET notification_level =+ CASE WHEN should_track THEN :tracking+ WHEN should_watch THEN :watching+ ELSE notification_level+ END,+ notifications_reason_id =+ CASE WHEN should_track THEN null+ WHEN should_watch THEN :auto_watch_category+ ELSE notifications_reason_id+ END+ FROM (+ SELECT tu1.topic_id,+ tu1.user_id,+ CASE WHEN+ cu.user_id IS NULL AND tu1.notification_level = :watching AND tu1.notifications_reason_id = :auto_watch_category THEN true+ ELSE false+ END should_track,+ CASE WHEN+ cu.user_id IS NOT NULL AND tu1.notification_level in (:regular, :tracking) THEN trueELSE false- END should_track,- CASE WHEN- cu.user_id IS NOT NULL AND tu1.notification_level in (:regular, :tracking) THEN true- ELSE false- END should_watch+ END should_watch- FROM topic_users tu1- JOIN topics t ON t.id = tu1.topic_id- LEFT JOIN category_users cu ON cu.category_id = t.category_id AND cu.user_id = tu1.user_id AND cu.notification_level = :watching- /*where2*/- ) as X+ FROM topic_users tu1+ JOIN topics t ON t.id = tu1.topic_id+ LEFT JOIN category_users cu ON cu.category_id = t.category_id AND cu.user_id = tu1.user_id AND cu.notification_level = :watching+ /*where2*/+ ) as X- /*where*/-SQL+ /*where*/+ SQLbuilder.where("X.topic_id = tu.topic_id AND X.user_id = tu.user_id")builder.where("should_watch OR should_track")@@ -147,10 +149,12 @@ SQLbuilder.where2("tu1.user_id = :user_id", user_id: user_id)end- builder.exec(watching: notification_levels[:watching],- tracking: notification_levels[:tracking],- regular: notification_levels[:regular],- auto_watch_category: TopicUser.notification_reasons[:auto_watch_category])+ builder.exec(+ watching: notification_levels[:watching],+ tracking: notification_levels[:tracking],+ regular: notification_levels[:regular],+ auto_watch_category: TopicUser.notification_reasons[:auto_watch_category]+ )enddiff --git a/app/models/post.rb b/app/models/post.rbindex 4e04c736f26..9919729c3d0 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -567,7 +567,7 @@ class Post < ActiveRecord::Base# each post.def self.calculate_avg_time(min_topic_age = nil)retry_lock_error do- builder = SqlBuilder.new("UPDATE posts+ builder = DB.build("UPDATE postsSET avg_time = (x.gmean / 1000)FROM (SELECT post_timings.topic_id,post_timings.post_number,@@ -692,7 +692,7 @@ class Post < ActiveRecord::BaseMAX_REPLY_LEVEL ||= 1000def reply_ids(guardian = nil, only_replies_to_single_post: true)- builder = SqlBuilder.new(<<~SQL, Post)+ builder = DB.build(<<~SQL)WITH RECURSIVE breadcrumb(id, level) AS (SELECT :post_id, 0UNION@@ -723,8 +723,8 @@ class Post < ActiveRecord::Base# for example it skips a post when it contains 2 quotes (which are replies) from different postsbuilder.where("count = 1") if only_replies_to_single_post- replies = builder.exec(post_id: id, max_reply_level: MAX_REPLY_LEVEL).to_a- replies.map! { |r| { id: r["id"].to_i, level: r["level"].to_i } }+ replies = builder.query_hash(post_id: id, max_reply_level: MAX_REPLY_LEVEL)+ replies.each { |r| r.symbolize_keys! }secured_ids = Post.secured(guardian).where(id: replies.map { |r| r[:id] }).pluck(:id).to_setdiff --git a/app/models/post_action.rb b/app/models/post_action.rbindex cf4d50d2dc2..bd1563a3dd6 100644--- a/app/models/post_action.rb+++ b/app/models/post_action.rb@@ -101,18 +101,19 @@ class PostAction < ActiveRecord::Base#topic_ids = topics.map(&:id)map = {}- builder = SqlBuilder.new <<SQL- SELECT p.topic_id, p.post_number- FROM post_actions pa- JOIN posts p ON pa.post_id = p.id- WHERE p.deleted_at IS NULL AND pa.deleted_at IS NULL AND- pa.post_action_type_id = :post_action_type_id AND- pa.user_id = :user_id AND- p.topic_id IN (:topic_ids)- ORDER BY p.topic_id, p.post_number-SQL-- builder.map_exec(OpenStruct, user_id: user.id, post_action_type_id: post_action_type_id, topic_ids: topic_ids).each do |row|++ builder = DB.build <<~SQL+ SELECT p.topic_id, p.post_number+ FROM post_actions pa+ JOIN posts p ON pa.post_id = p.id+ WHERE p.deleted_at IS NULL AND pa.deleted_at IS NULL AND+ pa.post_action_type_id = :post_action_type_id AND+ pa.user_id = :user_id AND+ p.topic_id IN (:topic_ids)+ ORDER BY p.topic_id, p.post_number+ SQL++ builder.query(user_id: user.id, post_action_type_id: post_action_type_id, topic_ids: topic_ids).each do |row|(map[row.topic_id] ||= []) << row.post_numberenddiff --git a/app/models/tag_user.rb b/app/models/tag_user.rbindex d54b787c405..87eafe5404d 100644--- a/app/models/tag_user.rb+++ b/app/models/tag_user.rb@@ -66,40 +66,39 @@ class TagUser < ActiveRecord::Baseenddef self.auto_watch(opts)- builder = SqlBuilder.new <<SQL-- UPDATE topic_users- SET notification_level = CASE WHEN should_watch THEN :watching ELSE :tracking END,- notifications_reason_id = CASE WHEN should_watch THEN :auto_watch_tag ELSE NULL END- FROM- (- SELECT tu.topic_id, tu.user_id, CASE- WHEN MAX(tag_users.notification_level) = :watching THEN true- ELSE false- END- should_watch,-- CASE WHEN MAX(tag_users.notification_level) IS NULL AND- tu.notification_level = :watching AND- tu.notifications_reason_id = :auto_watch_tag- THEN true- ELSE false- END- should_track-- FROM topic_users tu- LEFT JOIN topic_tags ON tu.topic_id = topic_tags.topic_id- LEFT JOIN tag_users ON tag_users.user_id = tu.user_id- AND topic_tags.tag_id = tag_users.tag_id- AND tag_users.notification_level = :watching- /*where*/- GROUP BY tu.topic_id, tu.user_id, tu.notification_level, tu.notifications_reason_id- ) AS X- WHERE X.topic_id = topic_users.topic_id AND- X.user_id = topic_users.user_id AND- (should_track OR should_watch)--SQL+ builder = DB.build <<~SQL+ UPDATE topic_users+ SET notification_level = CASE WHEN should_watch THEN :watching ELSE :tracking END,+ notifications_reason_id = CASE WHEN should_watch THEN :auto_watch_tag ELSE NULL END+ FROM+ (+ SELECT tu.topic_id, tu.user_id, CASE+ WHEN MAX(tag_users.notification_level) = :watching THEN true+ ELSE false+ END+ should_watch,++ CASE WHEN MAX(tag_users.notification_level) IS NULL AND+ tu.notification_level = :watching AND+ tu.notifications_reason_id = :auto_watch_tag+ THEN true+ ELSE false+ END+ should_track++ FROM topic_users tu+ LEFT JOIN topic_tags ON tu.topic_id = topic_tags.topic_id+ LEFT JOIN tag_users ON tag_users.user_id = tu.user_id+ AND topic_tags.tag_id = tag_users.tag_id+ AND tag_users.notification_level = :watching+ /*where*/+ GROUP BY tu.topic_id, tu.user_id, tu.notification_level, tu.notifications_reason_id+ ) AS X+ WHERE X.topic_id = topic_users.topic_id AND+ X.user_id = topic_users.user_id AND+ (should_track OR should_watch)++ SQLbuilder.where("tu.notification_level in (:tracking, :regular, :watching)")@@ -120,23 +119,23 @@ SQLdef self.auto_track(opts)- builder = SqlBuilder.new <<SQL- UPDATE topic_users- SET notification_level = :tracking, notifications_reason_id = :auto_track_tag- FROM (- SELECT DISTINCT tu.topic_id, tu.user_id- FROM topic_users tu- JOIN topic_tags ON tu.topic_id = topic_tags.topic_id- JOIN tag_users ON tag_users.user_id = tu.user_id- AND topic_tags.tag_id = tag_users.tag_id- AND tag_users.notification_level = :tracking- /*where*/- ) as X- WHERE- topic_users.notification_level = :regular AND- topic_users.topic_id = X.topic_id AND- topic_users.user_id = X.user_id-SQL+ builder = DB.build <<~SQL+ UPDATE topic_users+ SET notification_level = :tracking, notifications_reason_id = :auto_track_tag+ FROM (+ SELECT DISTINCT tu.topic_id, tu.user_id+ FROM topic_users tu+ JOIN topic_tags ON tu.topic_id = topic_tags.topic_id+ JOIN tag_users ON tag_users.user_id = tu.user_id+ AND topic_tags.tag_id = tag_users.tag_id+ AND tag_users.notification_level = :tracking+ /*where*/+ ) as X+ WHERE+ topic_users.notification_level = :regular AND+ topic_users.topic_id = X.topic_id AND+ topic_users.user_id = X.user_id+ SQLif topic_id = opts[:topic_id]builder.where("tu.topic_id = :topic_id", topic_id: topic_id)diff --git a/app/models/topic.rb b/app/models/topic.rbindex b0e1e628018..c03848c5ed8 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1241,7 +1241,7 @@ class Topic < ActiveRecord::Basedef self.time_to_first_response(sql, opts = nil)opts ||= {}- builder = SqlBuilder.new(sql)+ builder = DB.build(sql)builder.where("t.created_at >= :start_date", start_date: opts[:start_date]) if opts[:start_date]builder.where("t.created_at < :end_date", end_date: opts[:end_date]) if opts[:end_date]builder.where("t.category_id = :category_id", category_id: opts[:category_id]) if opts[:category_id]@@ -1253,7 +1253,7 @@ class Topic < ActiveRecord::Basebuilder.where("p.user_id in (:user_ids)", user_ids: opts[:user_ids]) if opts[:user_ids]builder.where("p.post_type = :post_type", post_type: Post.types[:regular])builder.where("EXTRACT(EPOCH FROM p.created_at - t.created_at) > 0")- builder.exec+ builder.query_hashenddef self.time_to_first_response_per_day(start_date, end_date, opts = {})@@ -1280,13 +1280,13 @@ class Topic < ActiveRecord::BaseSQLdef self.with_no_response_per_day(start_date, end_date, category_id = nil)- builder = SqlBuilder.new(WITH_NO_RESPONSE_SQL)+ builder = DB.build(WITH_NO_RESPONSE_SQL)builder.where("t.created_at >= :start_date", start_date: start_date) if start_datebuilder.where("t.created_at < :end_date", end_date: end_date) if end_datebuilder.where("t.category_id = :category_id", category_id: category_id) if category_idbuilder.where("t.archetype <> '#{Archetype.private_message}'")builder.where("t.deleted_at IS NULL")- builder.exec+ builder.query_hashendWITH_NO_RESPONSE_TOTAL_SQL ||= <<-SQL@@ -1302,11 +1302,11 @@ class Topic < ActiveRecord::BaseSQLdef self.with_no_response_total(opts = {})- builder = SqlBuilder.new(WITH_NO_RESPONSE_TOTAL_SQL)+ builder = DB.build(WITH_NO_RESPONSE_TOTAL_SQL)builder.where("t.category_id = :category_id", category_id: opts[:category_id]) if opts[:category_id]builder.where("t.archetype <> '#{Archetype.private_message}'")builder.where("t.deleted_at IS NULL")- builder.exec.first["count"].to_i+ builder.query_single.first.to_ienddef convert_to_public_topic(user)diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rbindex 61b3fab8710..fe71d76bad2 100644--- a/app/models/topic_tracking_state.rb+++ b/app/models/topic_tracking_state.rb@@ -174,8 +174,12 @@ class TopicTrackingStatesql << "\nUNION ALL\n\n"sql << report_raw_sql(topic_id: topic_id, skip_new: true, skip_order: true, staff: user.staff?)- SqlBuilder.new(sql)- .map_exec(TopicTrackingState, user_id: user.id, topic_id: topic_id, min_new_topic_date: Time.at(SiteSetting.min_new_topics_time).to_datetime)+ DB.query(+ sql,+ user_id: user.id,+ topic_id: topic_id,+ min_new_topic_date: Time.at(SiteSetting.min_new_topics_time).to_datetime+ )enddef self.report_raw_sql(opts = nil)diff --git a/app/models/topic_user.rb b/app/models/topic_user.rbindex b41fabb3daa..9cbf1b6383b 100644--- a/app/models/topic_user.rb+++ b/app/models/topic_user.rb@@ -370,28 +370,28 @@ class TopicUser < ActiveRecord::Basereturnend- builder = SqlBuilder.new <<SQL- UPDATE topic_users tu- SET #{action_type_name} = x.state- FROM (- SELECT CASE WHEN EXISTS (- SELECT 1- FROM post_actions pa- JOIN posts p on p.id = pa.post_id- JOIN topics t ON t.id = p.topic_id- WHERE pa.deleted_at IS NULL AND- p.deleted_at IS NULL AND- t.deleted_at IS NULL AND- pa.post_action_type_id = :action_type_id AND- tu2.topic_id = t.id AND- tu2.user_id = pa.user_id- LIMIT 1- ) THEN true ELSE false END state, tu2.topic_id, tu2.user_id- FROM topic_users tu2- /*where*/- ) x- WHERE x.topic_id = tu.topic_id AND x.user_id = tu.user_id AND x.state != tu.#{action_type_name}-SQL+ builder = DB.build <<~SQL+ UPDATE topic_users tu+ SET #{action_type_name} = x.state+ FROM (+ SELECT CASE WHEN EXISTS (+ SELECT 1+ FROM post_actions pa+ JOIN posts p on p.id = pa.post_id+ JOIN topics t ON t.id = p.topic_id+ WHERE pa.deleted_at IS NULL AND+ p.deleted_at IS NULL AND+ t.deleted_at IS NULL AND+ pa.post_action_type_id = :action_type_id AND+ tu2.topic_id = t.id AND+ tu2.user_id = pa.user_id+ LIMIT 1+ ) THEN true ELSE false END state, tu2.topic_id, tu2.user_id+ FROM topic_users tu2+ /*where*/+ ) x+ WHERE x.topic_id = tu.topic_id AND x.user_id = tu.user_id AND x.state != tu.#{action_type_name}+ SQLif user_idbuilder.where("tu2.user_id = :user_id", user_id: user_id)@@ -440,32 +440,31 @@ SQL# we up these numbers so they are not in-sync# the simple fix is to add a column here, but table is already quite big# long term we want to split up topic_users and allow for this better- builder = SqlBuilder.new <<SQL--UPDATE topic_users t- SET- last_read_post_number = LEAST(GREATEST(last_read, last_read_post_number), max_post_number),- highest_seen_post_number = LEAST(max_post_number,GREATEST(t.highest_seen_post_number, last_read))-FROM (- SELECT topic_id, user_id, MAX(post_number) last_read- FROM post_timings- GROUP BY topic_id, user_id-) as X-JOIN (- SELECT p.topic_id, MAX(p.post_number) max_post_number from posts p- GROUP BY p.topic_id-) as Y on Y.topic_id = X.topic_id-/*where*/-SQL-- builder.where <<SQL-X.topic_id = t.topic_id AND-X.user_id = t.user_id AND-(- last_read_post_number <> LEAST(GREATEST(last_read, last_read_post_number), max_post_number) OR- highest_seen_post_number <> LEAST(max_post_number,GREATEST(t.highest_seen_post_number, last_read))-)-SQL+ builder = DB.build <<~SQL+ UPDATE topic_users t+ SET+ last_read_post_number = LEAST(GREATEST(last_read, last_read_post_number), max_post_number),+ highest_seen_post_number = LEAST(max_post_number,GREATEST(t.highest_seen_post_number, last_read))+ FROM (+ SELECT topic_id, user_id, MAX(post_number) last_read+ FROM post_timings+ GROUP BY topic_id, user_id+ ) as X+ JOIN (+ SELECT p.topic_id, MAX(p.post_number) max_post_number from posts p+ GROUP BY p.topic_id+ ) as Y on Y.topic_id = X.topic_id+ /*where*/+ SQL++ builder.where <<~SQL+ X.topic_id = t.topic_id AND+ X.user_id = t.user_id AND+ (+ last_read_post_number <> LEAST(GREATEST(last_read, last_read_post_number), max_post_number) OR+ highest_seen_post_number <> LEAST(max_post_number,GREATEST(t.highest_seen_post_number, last_read))+ )+ SQLif topic_idbuilder.where("t.topic_id = :topic_id", topic_id: topic_id)diff --git a/app/models/topic_view_item.rb b/app/models/topic_view_item.rbindex 3549825dec8..9fbb9aa98a0 100644--- a/app/models/topic_view_item.rb+++ b/app/models/topic_view_item.rb@@ -28,7 +28,7 @@ class TopicViewItem < ActiveRecord::Base/*where*/)"- builder = SqlBuilder.new(sql)+ builder = DB.build(sql)if !user_idbuilder.where("ip_address = :ip_address AND topic_id = :topic_id AND user_id IS NULL")@@ -41,7 +41,7 @@ class TopicViewItem < ActiveRecord::BaseTopic.where(id: topic_id).update_all 'views = views + 1'- if result.cmd_tuples > 0+ if result > 0UserStat.where(user_id: user_id).update_all 'topics_entered = topics_entered + 1' if user_idenddiff --git a/app/models/user_action.rb b/app/models/user_action.rbindex 981cb4a9c44..325300389a7 100644--- a/app/models/user_action.rb+++ b/app/models/user_action.rb@@ -39,16 +39,6 @@ class UserAction < ActiveRecord::BaseASSIGNED,].each_with_index.to_a.flatten]- # note, this is temporary until we upgrade to rails 4- # in rails 4 types are mapped correctly so you dont end up- # having strings where you would expect bools- class UserActionRow < OpenStruct- include ActiveModel::SerializerSupport-- def as_json(options = nil)- @table.as_json(options)- end- enddef self.last_action_in_topic(user_id, topic_id)UserAction.where(user_id: user_id,@@ -59,25 +49,24 @@ class UserAction < ActiveRecord::Basedef self.stats(user_id, guardian)# Sam: I tried this in AR and it got complex- builder = UserAction.sql_builder <<SQL-- SELECT action_type, COUNT(*) count- FROM user_actions a- LEFT JOIN topics t ON t.id = a.target_topic_id- LEFT JOIN posts p on p.id = a.target_post_id- LEFT JOIN posts p2 on p2.topic_id = a.target_topic_id and p2.post_number = 1- LEFT JOIN categories c ON c.id = t.category_id- /*where*/- GROUP BY action_type-SQL+ builder = DB.build <<~SQL++ SELECT action_type, COUNT(*) count+ FROM user_actions a+ LEFT JOIN topics t ON t.id = a.target_topic_id+ LEFT JOIN posts p on p.id = a.target_post_id+ LEFT JOIN posts p2 on p2.topic_id = a.target_topic_id and p2.post_number = 1+ LEFT JOIN categories c ON c.id = t.category_id+ /*where*/+ GROUP BY action_type+ SQLbuilder.where('a.user_id = :user_id', user_id: user_id)apply_common_filters(builder, user_id, guardian)- results = builder.exec.to_a+ results = builder.queryresults.sort! { |a, b| ORDER[a.action_type] <=> ORDER[b.action_type] }-resultsend@@ -139,19 +128,50 @@ SQLstream(action_id: action_id, guardian: guardian).firstend+ NULL_QUEUED_STREAM_COLS = %i{+ cooked+ uploaded_avatar_id+ acting_name+ acting_username+ acting_user_id+ target_name+ target_username+ target_user_id+ post_number+ post_id+ deleted+ hidden+ post_type+ action_type+ action_code+ action_code_who+ topic_closed+ topic_id+ topic_archived+ }.map! { |s| "NULL as #{s}" }.join(", ")+def self.stream_queued(opts = nil)opts ||= {}offset = opts[:offset] || 0limit = opts[:limit] || 60- builder = SqlBuilder.new <<-SQL+ # this is somewhat ugly, but the serializer wants all these columns+ # it is more correct to have an object with all the fields needed+ # cause then we can catch and change if we ever add columns+ builder = DB.build <<~SQLSELECTa.id,- t.title, a.action_type, a.created_at, t.id topic_id,- u.username, u.name, u.id AS user_id,+ t.title,+ a.action_type,+ a.created_at,+ t.id topic_id,+ u.username,+ u.name,+ u.id AS user_id,qp.raw,- t.category_id+ t.category_id,+ #{NULL_QUEUED_STREAM_COLS}FROM user_actions as aJOIN queued_posts AS qp ON qp.id = a.queued_post_idLEFT OUTER JOIN topics t on t.id = qp.topic_id@@ -169,7 +189,7 @@ SQL.order_by("a.created_at desc").offset(offset.to_i).limit(limit.to_i)- .map_exec(UserActionRow)+ .queryenddef self.stream(opts = nil)@@ -197,7 +217,7 @@ SQL# The weird thing is that target_post_id can be null, so it makes everything# ever so more complex. Should we allow this, not sure.- builder = SqlBuilder.new <<-SQL+ builder = DB.build <<~SQLSELECTa.id,t.title, a.action_type, a.created_at, t.id topic_id,@@ -249,7 +269,7 @@ SQL.limit(limit.to_i)end- builder.map_exec(UserActionRow)+ builder.queryenddef self.log_action!(hash)@@ -321,19 +341,19 @@ SQLdef self.synchronize_target_topic_ids(post_ids = nil)# nuke all dupes, using magic- builder = SqlBuilder.new <<SQL-DELETE FROM user_actions USING user_actions ua2-/*where*/-SQL-- builder.where <<SQL- user_actions.action_type = ua2.action_type AND- user_actions.user_id = ua2.user_id AND- user_actions.acting_user_id = ua2.acting_user_id AND- user_actions.target_post_id = ua2.target_post_id AND- user_actions.target_post_id > 0 AND- user_actions.id > ua2.id-SQL+ builder = DB.build <<~SQL+ DELETE FROM user_actions USING user_actions ua2+ /*where*/+ SQL++ builder.where <<~SQL+ user_actions.action_type = ua2.action_type AND+ user_actions.user_id = ua2.user_id AND+ user_actions.acting_user_id = ua2.acting_user_id AND+ user_actions.target_post_id = ua2.target_post_id AND+ user_actions.target_post_id > 0 AND+ user_actions.id > ua2.id+ SQLif post_idsbuilder.where("user_actions.target_post_id in (:post_ids)", post_ids: post_ids)@@ -341,9 +361,11 @@ SQLbuilder.exec- builder = SqlBuilder.new("UPDATE user_actions- SET target_topic_id = (select topic_id from posts where posts.id = target_post_id)- /*where*/")+ builder = DB.build <<~SQL+ UPDATE user_actions+ SET target_topic_id = (select topic_id from posts where posts.id = target_post_id)+ /*where*/+ SQLbuilder.where("target_topic_id <> (select topic_id from posts where posts.id = target_post_id)")if post_idsdiff --git a/app/models/user_profile_view.rb b/app/models/user_profile_view.rbindex 237318a101c..6864b571793 100644--- a/app/models/user_profile_view.rb+++ b/app/models/user_profile_view.rb@@ -25,7 +25,7 @@ class UserProfileView < ActiveRecord::Base/*where*/)"- builder = SqlBuilder.new(sql)+ builder = DB.build(sql)if !user_idbuilder.where("viewed_at = :viewed_at AND ip_address = :ip_address AND user_profile_id = :user_profile_id AND user_id IS NULL")@@ -35,7 +35,7 @@ class UserProfileView < ActiveRecord::Baseresult = builder.exec(user_profile_id: user_profile_id, ip_address: ip, viewed_at: at, user_id: user_id)- if result.cmd_tuples > 0+ if result > 0UserProfile.find(user_profile_id).increment!(:views)endenddiff --git a/app/models/user_search.rb b/app/models/user_search.rbindex a9eceda1e9a..3acdf917c3e 100644--- a/app/models/user_search.rb+++ b/app/models/user_search.rb@@ -54,7 +54,7 @@ class UserSearchusers = users.includes(:user_search_data).references(:user_search_data).where("user_search_data.search_data @@ #{query}")- .order(User.sql_fragment("CASE WHEN username_lower LIKE ? THEN 0 ELSE 1 END ASC", @term_like))+ .order(DB.sql_fragment("CASE WHEN username_lower LIKE ? THEN 0 ELSE 1 END ASC", @term_like))elseusers = users.where("username_lower LIKE :term_like", term_like: @term_like)diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rbindex 8446b5bcda7..6999be26e1a 100644--- a/app/services/badge_granter.rb+++ b/app/services/badge_granter.rb@@ -184,7 +184,7 @@ class BadgeGranter# hack to allow for params, otherwise sanitizer will trigger sprintfcount_sql = "SELECT COUNT(*) count FROM (#{sql}) q WHERE :backfill = :backfill"- grant_count = SqlBuilder.map_exec(OpenStruct, count_sql, params).first.count.to_i+ grant_count = DB.query_single(count_sql, params).first.to_igrants_sql =if opts[:target_posts]diff --git a/app/services/user_merger.rb b/app/services/user_merger.rbindex 4fc678b13f8..1d31a802e7c 100644--- a/app/services/user_merger.rb+++ b/app/services/user_merger.rb@@ -386,7 +386,7 @@ class UserMergerconditions = Array.wrap(opts[:conditions])updates = Array.wrap(opts[:updates])- builder = SqlBuilder.new(<<~SQL)+ builder = DB.build(<<~SQL)UPDATE #{table_name} AS x/*set*/WHERE x.#{user_id_column_name} = :source_user_id AND NOT EXISTS(diff --git a/db/migrate/20141014191645_fix_tos_name.rb b/db/migrate/20141014191645_fix_tos_name.rbindex 42daa0a1508..16976a944dd 100644--- a/db/migrate/20141014191645_fix_tos_name.rb+++ b/db/migrate/20141014191645_fix_tos_name.rb@@ -1,7 +1,7 @@class FixTosName < ActiveRecord::Migration[4.2]def upI18n.overrides_disabled do- execute ActiveRecord::Base.sql_fragment('UPDATE user_fields SET name = ? WHERE name = ?', I18n.t('terms_of_service.title'), I18n.t("terms_of_service.signup_form_message"))+ execute DB.sql_fragment('UPDATE user_fields SET name = ? WHERE name = ?', I18n.t('terms_of_service.title'), I18n.t("terms_of_service.signup_form_message"))endenddiff --git a/db/migrate/20170313192741_add_themes.rb b/db/migrate/20170313192741_add_themes.rbindex be8fa82e044..374d4abe2e2 100644--- a/db/migrate/20170313192741_add_themes.rb+++ b/db/migrate/20170313192741_add_themes.rb@@ -40,7 +40,7 @@ class AddThemes < ActiveRecord::Migration[4.2]RETURNING *SQL- sql = ActiveRecord::Base.sql_fragment(sql, now: Time.zone.now, key: theme_key)+ sql = DB.sql_fragment(sql, now: Time.zone.now, key: theme_key)theme_id = execute(sql).to_a[0]["id"].to_iend@@ -62,7 +62,7 @@ SQLINSERT INTO site_settings(name, data_type, value, created_at, updated_at)VALUES('default_theme_key', 1, :key, :now, :now)SQL- sql = ActiveRecord::Base.sql_fragment(sql, now: Time.zone.now, key: theme_key)+ sql = DB.sql_fragment(sql, now: Time.zone.now, key: theme_key)execute(sql)enddiff --git a/lib/flag_query.rb b/lib/flag_query.rbindex e963269a4f3..43b3206ddf3 100644--- a/lib/flag_query.rb+++ b/lib/flag_query.rb@@ -32,9 +32,9 @@ module FlagQuerypost_ids = post_ids_relation.pluck(:post_id).uniq- posts = SqlBuilder.new("+ posts = DB.query(<<~SQL, post_ids: post_ids)SELECT p.id,- p.cooked,+ p.cooked as excerpt,p.raw,p.user_id,p.topic_id,@@ -43,10 +43,13 @@ module FlagQueryp.hidden,p.deleted_at,p.user_deleted,+ NULL as post_actions,+ NULL as post_action_ids,(SELECT created_at FROM post_revisions WHERE post_id = p.id AND user_id = p.user_id ORDER BY created_at DESC LIMIT 1) AS last_revised_at,(SELECT COUNT(*) FROM post_actions WHERE (disagreed_at IS NOT NULL OR agreed_at IS NOT NULL OR deferred_at IS NOT NULL) AND post_id = p.id)::int AS previous_flags_countFROM posts p- WHERE p.id in (:post_ids)").map_exec(OpenStruct, post_ids: post_ids)+ WHERE p.id in (:post_ids)+ SQLpost_lookup = {}user_ids = Set.new@@ -55,8 +58,7 @@ module FlagQueryposts.each do |p|user_ids << p.user_idtopic_ids << p.topic_id- p.excerpt = Post.excerpt(p.cooked)- p.delete_field(:cooked)+ p.excerpt = Post.excerpt(p.excerpt)post_lookup[p.id] = pend@@ -127,7 +129,7 @@ module FlagQuery# maintain orderposts = post_ids.map { |id| post_lookup[id] }# TODO: add serializer so we can skip this- posts.map!(&:marshal_dump)+ posts.map!(&:to_h)users = User.includes(:user_stat).where(id: user_ids.to_a).to_aUser.preload_custom_fields(users, User.whitelisted_user_custom_fields(guardian))diff --git a/lib/migration/column_dropper.rb b/lib/migration/column_dropper.rbindex a594105622b..5d9e508e808 100644--- a/lib/migration/column_dropper.rb+++ b/lib/migration/column_dropper.rb@@ -32,21 +32,22 @@ module Migrationenddef droppable?- builder = SqlBuilder.new(<<~SQL)+ builder = DB.build(<<~SQL)SELECT 1FROM INFORMATION_SCHEMA.COLUMNS/*where*/LIMIT 1SQL- builder.where("table_schema = 'public'")+ builder+ .where("table_schema = 'public'").where("table_name = :table").where("column_name IN (:columns)").where(previous_migration_done).exec(table: @table,columns: @columns,delay: "#{@delay} seconds",- after_migration: @after_migration).to_a.length > 0+ after_migration: @after_migration) > 0enddef execute_drop!diff --git a/lib/mini_sql_multisite_connection.rb b/lib/mini_sql_multisite_connection.rbindex 5af8ed2d2e7..c6842fe19ab 100644--- a/lib/mini_sql_multisite_connection.rb+++ b/lib/mini_sql_multisite_connection.rb@@ -36,4 +36,13 @@ class MiniSqlMultisiteConnection < MiniSql::Connectiondef build(sql)CustomBuilder.new(self, sql)end++ def sql_fragment(query, *args)+ if args.length > 0+ param_encoder.encode(query, *args)+ else+ query+ end+ end+enddiff --git a/lib/score_calculator.rb b/lib/score_calculator.rbindex baf93cb457e..41272243ddd 100644--- a/lib/score_calculator.rb+++ b/lib/score_calculator.rb@@ -32,7 +32,7 @@ class ScoreCalculator@weightings.each_key { |k| components << "COALESCE(posts.#{k}, 0) * :#{k}" }components = components.join(" + ")- builder = SqlBuilder.new <<SQL+ builder = DB.build <<SQLUPDATE posts pSET score = x.scoreFROM (@@ -48,66 +48,72 @@ SQLfilter_topics(builder, opts)- while builder.exec.cmd_tuples == limit+ while builder.exec == limitendenddef update_posts_rank(opts)limit = 20000- builder = SqlBuilder.new <<SQL-UPDATE posts-SET percent_rank = X.percent_rank-FROM (- SELECT posts.id, Y.percent_rank- FROM posts- JOIN (- SELECT id, percent_rank()- OVER (PARTITION BY topic_id ORDER BY SCORE DESC) as percent_rank- FROM posts- ) Y ON Y.id = posts.id- JOIN topics ON posts.topic_id = topics.id- /*where*/- LIMIT #{limit}-) AS X-WHERE posts.id = X.id-SQL+ builder = DB.build <<~SQL+ UPDATE posts+ SET percent_rank = X.percent_rank+ FROM (+ SELECT posts.id, Y.percent_rank+ FROM posts+ JOIN (+ SELECT id, percent_rank()+ OVER (PARTITION BY topic_id ORDER BY SCORE DESC) as percent_rank+ FROM posts+ ) Y ON Y.id = posts.id+ JOIN topics ON posts.topic_id = topics.id+ /*where*/+ LIMIT #{limit}+ ) AS X+ WHERE posts.id = X.id+ SQLbuilder.where("posts.percent_rank IS NULL OR Y.percent_rank <> posts.percent_rank")filter_topics(builder, opts)- while builder.exec.cmd_tuples == limit+ while builder.exec == limitendenddef update_topics_rank(opts)- builder = SqlBuilder.new("UPDATE topics AS topics- SET has_summary = (topics.like_count >= :likes_required AND- topics.posts_count >= :posts_required AND- x.max_score >= :score_required),- score = x.avg_score- FROM (SELECT p.topic_id,- MAX(p.score) AS max_score,- AVG(p.score) AS avg_score- FROM posts AS p- GROUP BY p.topic_id) AS x- /*where*/")-- builder.where("x.topic_id = topics.id AND- (- (topics.score <> x.avg_score OR topics.score IS NULL) OR- (topics.has_summary IS NULL OR topics.has_summary <> (- topics.like_count >= :likes_required AND- topics.posts_count >= :posts_required AND- x.max_score >= :score_required- ))- )- ",- likes_required: SiteSetting.summary_likes_required,- posts_required: SiteSetting.summary_posts_required,- score_required: SiteSetting.summary_score_threshold)+ builder = DB.build <<~SQL+ UPDATE topics AS topics+ SET has_summary = (topics.like_count >= :likes_required AND+ topics.posts_count >= :posts_required AND+ x.max_score >= :score_required),+ score = x.avg_score+ FROM (SELECT p.topic_id,+ MAX(p.score) AS max_score,+ AVG(p.score) AS avg_score+ FROM posts AS p+ GROUP BY p.topic_id) AS x+ /*where*/+ SQL++ defaults = {+ likes_required: SiteSetting.summary_likes_required,+ posts_required: SiteSetting.summary_posts_required,+ score_required: SiteSetting.summary_score_threshold+ }++ builder.where(<<~SQL, defaults)+ x.topic_id = topics.id AND+ (+ (topics.score <> x.avg_score OR topics.score IS NULL) OR+ (topics.has_summary IS NULL OR topics.has_summary <> (+ topics.like_count >= :likes_required AND+ topics.posts_count >= :posts_required AND+ x.max_score >= :score_required+ ))+ )+ SQLfilter_topics(builder, opts)@@ -116,11 +122,13 @@ SQLdef update_topics_percent_rank(opts)- builder = SqlBuilder.new("UPDATE topics SET percent_rank = x.percent_rank- FROM (SELECT id, percent_rank()- OVER (ORDER BY SCORE DESC) as percent_rank- FROM topics) AS x- /*where*/")+ builder = DB.build <<~SQL+ UPDATE topics SET percent_rank = x.percent_rank+ FROM (SELECT id, percent_rank()+ OVER (ORDER BY SCORE DESC) as percent_rank+ FROM topics) AS x+ /*where*/+ SQLbuilder.where("x.id = topics.id AND (topics.percent_rank <> x.percent_rank OR topics.percent_rank IS NULL)")diff --git a/lib/site_settings/db_provider.rb b/lib/site_settings/db_provider.rbindex 0887e0edc4d..9300be2dba1 100644--- a/lib/site_settings/db_provider.rb+++ b/lib/site_settings/db_provider.rb@@ -14,15 +14,14 @@ class SiteSettings::DbProviderreturn [] unless table_exists?# Not leaking out AR records, cause I want all editing to happen via this API- SqlBuilder.new("SELECT name, data_type, value FROM #{@model.table_name}").map_exec(OpenStruct)+ DB.query("SELECT name, data_type, value FROM #{@model.table_name}")enddef find(name)return nil unless table_exists?# Not leaking out AR records, cause I want all editing to happen via this API- SqlBuilder.new("SELECT name, data_type, value FROM #{@model.table_name} WHERE name = :name")- .map_exec(OpenStruct, name: name)+ DB.query("SELECT name, data_type, value FROM #{@model.table_name} WHERE name = ?", name).firstenddiff --git a/lib/sql_builder.rb b/lib/sql_builder.rbindex 3498cbe6175..f21ae4b1f87 100644--- a/lib/sql_builder.rb+++ b/lib/sql_builder.rb@@ -1,6 +1,9 @@class SqlBuilderdef initialize(template, klass = nil)++ Discourse.deprecate("SqlBuilder is deprecated and will be removed, please use DB.build instead!")+@args = {}@sql = template@sections = {}@@ -75,12 +78,8 @@ class SqlBuilderclass RailsDateTimeDecoder < PG::SimpleDecoderdef decode(string, tuple = nil, field = nil)- if Rails.version >= "4.2.0"- @caster ||= ActiveRecord::Type::DateTime.new- @caster.cast(string)- else- ActiveRecord::ConnectionAdapters::Column.string_to_time string- end+ @caster ||= ActiveRecord::Type::DateTime.new+ @caster.cast(string)endend@@ -118,9 +117,3 @@ class SqlBuilderendend--class ActiveRecord::Base- def self.sql_builder(template)- SqlBuilder.new(template, self)- end-enddiff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rakeindex 9909bb3c01e..cafe17bc2c6 100644--- a/lib/tasks/posts.rake+++ b/lib/tasks/posts.rake@@ -283,7 +283,7 @@ task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|Post.transaction do# update sort_order and flip post_number to prevent# unique constraint violations when updating post_number- builder = SqlBuilder.new(<<~SQL)+ builder = DB.build(<<~SQL)WITH ordered_posts AS (SELECTid,diff --git a/spec/components/sql_builder_spec.rb b/spec/components/sql_builder_spec.rbdeleted file mode 100644index 4ccdde49a25..00000000000--- a/spec/components/sql_builder_spec.rb+++ /dev/null@@ -1,75 +0,0 @@-# encoding: utf-8-require 'rails_helper'-require_dependency 'sql_builder'--describe SqlBuilder do-- describe "attached" do- before do- @builder = Post.sql_builder("select * from posts /*where*/ /*limit*/")- end-- it "should find a post by id" do- p = Fabricate(:post)- @builder.where('id = :id and topic_id = :topic_id', id: p.id, topic_id: p.topic_id)- p2 = @builder.exec.first- expect(p2.id).to eq(p.id)- expect(p2).to eq(p)- end- end-- describe "map_exec" do- class SqlBuilder::TestClass- attr_accessor :int, :string, :date, :text, :bool- end-- it "correctly maps to a klass" do- rows = SqlBuilder.new("SELECT- 1 AS int,- 'string' AS string,- CAST(NOW() at time zone 'utc' AS timestamp without time zone) AS date,- 'text'::text AS text,- true AS bool")- .map_exec(SqlBuilder::TestClass)-- expect(rows.count).to eq(1)- row = rows[0]- expect(row.int).to eq(1)- expect(row.string).to eq("string")- expect(row.text).to eq("text")- expect(row.bool).to eq(true)- expect(row.date).to be_within(10.seconds).of(DateTime.now)- end- end-- describe "detached" do- before do- @builder = SqlBuilder.new("select * from (select :a A union all select :b) as X /*where*/ /*order_by*/ /*limit*/ /*offset*/")- end-- it "should allow for 1 param exec" do- expect(@builder.exec(a: 1, b: 2).values[0][0]).to eq(1)- end-- it "should allow for a single where" do- @builder.where(":a = 1")- expect(@builder.exec(a: 1, b: 2).values[0][0]).to eq(1)- end-- it "should allow where chaining" do- @builder.where(":a = 1")- @builder.where("2 = 1")- expect(@builder.exec(a: 1, b: 2).to_a.length).to eq(0)- end-- it "should allow order by" do- expect(@builder.order_by("A desc").limit(1)- .exec(a: 1, b: 2).values[0][0]).to eq(2)- end- it "should allow offset" do- expect(@builder.order_by("A desc").offset(1)- .exec(a: 1, b: 2).values[0][0]).to eq(1)- end- end--enddiff --git a/spec/models/user_action_spec.rb b/spec/models/user_action_spec.rbindex 6ab8a1537ab..9a172ded34f 100644--- a/spec/models/user_action_spec.rb+++ b/spec/models/user_action_spec.rb@@ -43,7 +43,7 @@ describe UserAction doenddef stats_for_user(viewer = nil)- UserAction.stats(user.id, Guardian.new(viewer)).map { |r| r["action_type"].to_i }.sort+ UserAction.stats(user.id, Guardian.new(viewer)).map { |r| r.action_type.to_i }.sortenddef stream(viewer = nil)