Skip to content
This repository

sql comments support #157

Open
wants to merge 1 commit into from

1 participant

Michael Grosser
Michael Grosser

@tenderlove as discussed here

scope = User.where(aaa).comment("XXX")
scope.comment("YYY")
scope.to_sql == "SELECT * FROM users WHERE xxx /* XXX YYY */"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jan 08, 2013
Michael Grosser grosser WIP comments 7597f99
This page is out of date. Refresh to see the latest.
2  lib/arel/nodes/node.rb
@@ -3,6 +3,8 @@ module Nodes
3 3 ###
4 4 # Abstract base class for all AST nodes
5 5 class Node
  6 + attr_accessor :comment
  7 +
6 8 include Arel::FactoryMethods
7 9 include Enumerable
8 10
6 lib/arel/select_manager.rb
@@ -254,6 +254,12 @@ def joins manager
254 254 manager.join_sql
255 255 end
256 256
  257 + def comment comment
  258 + @ast.comment ||= []
  259 + @ast.comment << comment
  260 + self
  261 + end
  262 +
257 263 class Row < Struct.new(:data) # :nodoc:
258 264 def id
259 265 data['id']
4 lib/arel/tree_manager.rb
@@ -18,7 +18,9 @@ def visitor
18 18 end
19 19
20 20 def to_sql
21   - visitor.accept @ast
  21 + sql = visitor.accept ast
  22 + sql << " /* #{ast.comment.join(" ")} */" if ast.comment
  23 + sql
22 24 end
23 25
24 26 def initialize_copy other
14 test/test_select_manager.rb
@@ -1155,5 +1155,19 @@ def test_join_sources
1155 1155 manager.ast.cores.last.set_quantifier.must_equal nil
1156 1156 end
1157 1157 end
  1158 +
  1159 + describe 'comment' do
  1160 + it 'makes sql' do
  1161 + manager = Arel::SelectManager.new Table.engine
  1162 + manager.comment("XXX")
  1163 + manager.to_sql.must_be_like 'SELECT /* XXX */'
  1164 + end
  1165 +
  1166 + it 'makes sql chains' do
  1167 + manager = Arel::SelectManager.new Table.engine
  1168 + manager.comment("XXX").comment("YYY")
  1169 + manager.to_sql.must_be_like 'SELECT /* XXX YYY */'
  1170 + end
  1171 + end
1158 1172 end
1159 1173 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.