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/arel
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
..
attributes Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
collectors SQLString#compile is no longer used since 53521a9e39 2018-09-30 16:49:13 +09:00
nodes Add Relation#annotate for SQL commenting 2019-03-21 20:30:56 -07:00
visitors Add Relation#annotate for SQL commenting 2019-03-21 20:30:56 -07:00
alias_predication.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
attributes.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
crud.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
delete_manager.rb Handle DELETE with LIMIT in Arel 2018-09-30 21:21:54 +09:00
errors.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
expressions.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
factory_methods.rb Make update_counters preparable 2018-09-28 04:14:15 +09:00
insert_manager.rb Get rid of Arel::Nodes::Values 2019-03-18 03:57:16 +09:00
math.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
nodes.rb Add Relation#annotate for SQL commenting 2019-03-21 20:30:56 -07:00
order_predications.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
predications.rb All of queries should return correct result even if including large number 2019-01-18 16:01:07 +09:00
select_manager.rb Add Relation#annotate for SQL commenting 2019-03-21 20:30:56 -07:00
table.rb Use private attr_reader in Arel 2018-09-30 15:24:17 +09:00
tree_manager.rb Add Relation#annotate for SQL commenting 2019-03-21 20:30:56 -07:00
update_manager.rb Handle DELETE with LIMIT in Arel 2018-09-30 21:21:54 +09:00
visitors.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30
window_predications.rb Arel: :nodoc: all 2018-02-24 18:11:47 +10:30