1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

project works with strings

This commit is contained in:
Aaron Patterson 2010-08-13 08:11:08 -07:00
parent e573a9ea46
commit 38b10d5ea1
6 changed files with 41 additions and 17 deletions

View file

@ -3,8 +3,10 @@ require 'arel/table'
require 'arel/attributes' require 'arel/attributes'
require 'arel/tree_manager' require 'arel/tree_manager'
require 'arel/nodes' require 'arel/nodes'
require 'arel/visitors/to_sql'
# below is deprecated #### these are deprecated
require 'arel/sql/engine' require 'arel/sql/engine'
require 'arel/sql_literal' require 'arel/sql_literal'
####
require 'arel/visitors/to_sql'

View file

@ -1,11 +1,6 @@
module Arel module Arel
module Nodes module Nodes
class SqlLiteral class SqlLiteral < String
attr_accessor :string
def initialize string
@string = string
end
end end
end end
end end

View file

@ -1,8 +1,4 @@
module Arel module Arel
class SqlLiteral < Nodes::SqlLiteral class SqlLiteral < Nodes::SqlLiteral
def initialize string
warn "#{caller.first} should use Nodes::SqlLiteral"
super
end
end end
end end

View file

@ -10,8 +10,8 @@ module Arel
@engine = engine @engine = engine
end end
def where expression def tm
TreeManager.new(@engine).from(self).where expression TreeManager.new(@engine).from(self)
end end
def columns def columns

View file

@ -22,8 +22,8 @@ module Arel
def visit_Arel_Nodes_SelectCore o def visit_Arel_Nodes_SelectCore o
[ [
"SELECT #{o.projections.map { |x| visit x }.join ', '}", "SELECT #{o.projections.map { |x| visit x }.join ', '}",
"FROM #{o.froms.map { |x| visit x }.join ', ' }", ("FROM #{o.froms.map { |x| visit x }.join ', ' }" unless o.froms.empty?),
("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.blank?) ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?)
].compact.join ' ' ].compact.join ' '
end end
@ -40,9 +40,20 @@ module Arel
end end
def visit_Fixnum o; o end def visit_Fixnum o; o end
alias :visit_String :visit_Fixnum
alias :visit_Arel_Nodes_SqlLiteral :visit_Fixnum
alias :visit_Arel_SqlLiteral :visit_Fixnum # This is deprecated
DISPATCH = {}
def visit object def visit object
send :"visit_#{object.class.name.gsub('::', '_')}", object send DISPATCH[object.class], object
end
private_instance_methods(false).each do |method|
method = method.to_s
next unless method =~ /^visit_(.*)$/
const = $1.split('_').inject(Object) { |m,s| m.const_get s }
DISPATCH[const] = method
end end
def quote_table_name name def quote_table_name name

View file

@ -2,6 +2,26 @@ require 'spec_helper'
module Arel module Arel
describe 'tree manager' do describe 'tree manager' do
describe 'project' do
it 'takes strings' do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager.project '*'
manager.to_sql.should == %{
SELECT *
}.gsub("\n", '').gsub(/(^\s*|\s*$)/, '').squeeze(' ')
end
it "takes sql literals" do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager.project Nodes::SqlLiteral.new '*'
manager.to_sql.should == %{
SELECT *
}.gsub("\n", '').gsub(/(^\s*|\s*$)/, '').squeeze(' ')
end
end
describe 'take' do describe 'take' do
it "knows take" do it "knows take" do
table = Table.new :users table = Table.new :users