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:
parent
e573a9ea46
commit
38b10d5ea1
6 changed files with 41 additions and 17 deletions
|
@ -3,8 +3,10 @@ require 'arel/table'
|
|||
require 'arel/attributes'
|
||||
require 'arel/tree_manager'
|
||||
require 'arel/nodes'
|
||||
require 'arel/visitors/to_sql'
|
||||
|
||||
# below is deprecated
|
||||
#### these are deprecated
|
||||
require 'arel/sql/engine'
|
||||
require 'arel/sql_literal'
|
||||
####
|
||||
|
||||
require 'arel/visitors/to_sql'
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
module Arel
|
||||
module Nodes
|
||||
class SqlLiteral
|
||||
attr_accessor :string
|
||||
|
||||
def initialize string
|
||||
@string = string
|
||||
end
|
||||
class SqlLiteral < String
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
module Arel
|
||||
class SqlLiteral < Nodes::SqlLiteral
|
||||
def initialize string
|
||||
warn "#{caller.first} should use Nodes::SqlLiteral"
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,8 +10,8 @@ module Arel
|
|||
@engine = engine
|
||||
end
|
||||
|
||||
def where expression
|
||||
TreeManager.new(@engine).from(self).where expression
|
||||
def tm
|
||||
TreeManager.new(@engine).from(self)
|
||||
end
|
||||
|
||||
def columns
|
||||
|
|
|
@ -22,8 +22,8 @@ module Arel
|
|||
def visit_Arel_Nodes_SelectCore o
|
||||
[
|
||||
"SELECT #{o.projections.map { |x| visit x }.join ', '}",
|
||||
"FROM #{o.froms.map { |x| visit x }.join ', ' }",
|
||||
("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.blank?)
|
||||
("FROM #{o.froms.map { |x| visit x }.join ', ' }" unless o.froms.empty?),
|
||||
("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?)
|
||||
].compact.join ' '
|
||||
end
|
||||
|
||||
|
@ -40,9 +40,20 @@ module Arel
|
|||
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
|
||||
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
|
||||
|
||||
def quote_table_name name
|
||||
|
|
|
@ -2,6 +2,26 @@ require 'spec_helper'
|
|||
|
||||
module Arel
|
||||
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
|
||||
it "knows take" do
|
||||
table = Table.new :users
|
||||
|
|
Loading…
Reference in a new issue