1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib/active_record/relation
Matt Yoho f41825809c Add Relation#annotate for SQL commenting
This patch has two main portions:

1. Add SQL comment support to Arel via Arel::Nodes::Comment.
2. Implement a Relation#annotate method on top of that.

== Adding SQL comment support

Adds a new Arel::Nodes::Comment node that represents an optional SQL
comment and teachers the relevant visitors how to handle it.

Comment nodes may be added to the basic CRUD statement nodes and set
through any of the four (Select|Insert|Update|Delete)Manager objects.

For example:

    manager = Arel::UpdateManager.new
    manager.table table
    manager.comment("annotation")
    manager.to_sql # UPDATE "users" /* annotation */

This new node type will be used by ActiveRecord::Relation to enable
query annotation via SQL comments.

== Implementing the Relation#annotate method

Implements `ActiveRecord::Relation#annotate`, which accepts a comment
string that will be appeneded to any queries generated by the relation.

Some examples:

    relation = Post.where(id: 123).annotate("metadata string")
    relation.first
    # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123
    # LIMIT 1 /* metadata string */

    class Tag < ActiveRecord::Base
      scope :foo_annotated, -> { annotate("foo") }
    end
    Tag.foo_annotated.annotate("bar").first
    # SELECT "tags".* FROM "tags" LIMIT 1 /* foo */ /* bar */

Also wires up the plumbing so this works with `#update_all` and
`#delete_all` as well.

This feature is useful for instrumentation and general analysis of
queries generated at runtime.
2019-03-21 20:30:56 -07:00
..
batches Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
predicate_builder Consolidate the duplicated code that building range predicate 2019-01-08 16:45:17 +09:00
batches.rb Extract Relation#bind_attribute for internal use 2018-07-30 00:57:44 +09:00
calculations.rb fixes different count calculation when using size manual select with DISTINCT 2019-02-26 12:21:14 -03:00
delegation.rb Refactor generated_relation_methods to remove duplicated code on ClassSpecificRelation 2019-02-28 01:32:57 +09:00
finder_methods.rb Replace “can not” with “cannot”. 2019-03-06 16:35:52 -05:00
from_clause.rb Refactor Active Record to let Arel manage bind params 2017-07-24 09:07:24 -04:00
merger.rb Bugfix ActiveRecord::Relation#merge special case of from clause 2018-09-28 11:46:40 +03:00
predicate_builder.rb activerecord: Fix where nil condition on composed_of attribute 2019-01-18 11:10:10 -05:00
query_attribute.rb Don't allow where with invalid value matches to nil values 2019-02-18 16:57:10 +09:00
query_methods.rb Add Relation#annotate for SQL commenting 2019-03-21 20:30:56 -07:00
record_fetch_warning.rb Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
spawn_methods.rb Deprecate using class level querying methods if the receiver scope regarded as leaked 2019-02-15 17:40:15 +09:00
where_clause.rb Fix eager loading polymorphic association with mixed table conditions 2019-02-18 00:41:43 +09:00
where_clause_factory.rb Use private attr_reader 2018-02-23 11:10:51 +09:00