mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
f41825809c
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.
77 lines
1.4 KiB
Ruby
77 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Arel # :nodoc: all
|
|
class TreeManager
|
|
include Arel::FactoryMethods
|
|
|
|
module StatementMethods
|
|
def take(limit)
|
|
@ast.limit = Nodes::Limit.new(Nodes.build_quoted(limit)) if limit
|
|
self
|
|
end
|
|
|
|
def offset(offset)
|
|
@ast.offset = Nodes::Offset.new(Nodes.build_quoted(offset)) if offset
|
|
self
|
|
end
|
|
|
|
def order(*expr)
|
|
@ast.orders = expr
|
|
self
|
|
end
|
|
|
|
def key=(key)
|
|
@ast.key = Nodes.build_quoted(key)
|
|
end
|
|
|
|
def key
|
|
@ast.key
|
|
end
|
|
|
|
def wheres=(exprs)
|
|
@ast.wheres = exprs
|
|
end
|
|
|
|
def where(expr)
|
|
@ast.wheres << expr
|
|
self
|
|
end
|
|
|
|
def comment(*values)
|
|
@ast.comment = Nodes::Comment.new(values)
|
|
self
|
|
end
|
|
end
|
|
|
|
attr_reader :ast
|
|
|
|
def initialize
|
|
@ctx = nil
|
|
end
|
|
|
|
def to_dot
|
|
collector = Arel::Collectors::PlainString.new
|
|
collector = Visitors::Dot.new.accept @ast, collector
|
|
collector.value
|
|
end
|
|
|
|
def to_sql(engine = Table.engine)
|
|
collector = Arel::Collectors::SQLString.new
|
|
collector = engine.connection.visitor.accept @ast, collector
|
|
collector.value
|
|
end
|
|
|
|
def initialize_copy(other)
|
|
super
|
|
@ast = @ast.clone
|
|
end
|
|
|
|
def where(expr)
|
|
if Arel::TreeManager === expr
|
|
expr = expr.ast
|
|
end
|
|
@ctx.wheres << expr
|
|
self
|
|
end
|
|
end
|
|
end
|