提交 f0f6b7fb 编写于 作者: A Aaron Patterson

adding not equal node, column names are expected to be symbols

上级 1ba8ac08
module Arel
module Attributes
class Attribute < Struct.new :relation, :name, :column
def not_eq other
Nodes::NotEqual.new self, other
end
def eq other
Nodes::Equality.new self, other
end
......
......@@ -8,6 +8,10 @@ module Value # :nodoc:
def value
visitor.accept self
end
def name
super.to_sym
end
end
def initialize engine, collection
......
require 'arel/nodes/binary'
require 'arel/nodes/equality'
require 'arel/nodes/not_equal'
require 'arel/nodes/assignment'
require 'arel/nodes/or'
require 'arel/nodes/and'
......
module Arel
module Nodes
class NotEqual < Arel::Nodes::Binary
end
end
end
......@@ -12,7 +12,7 @@ def initialize name, relation
end
def [] name
name = name.to_s
name = name.to_sym
columns.find { |column| column.name == name }
end
end
......
......@@ -73,12 +73,12 @@ def having expr
def columns
@columns ||= @engine.connection.columns(@name, "#{@name} Columns").map do |column|
Attributes.for(column).new self, column.name, column
Attributes.for(column).new self, column.name.to_sym, column
end
end
def [] name
name = name.to_s
name = name.to_sym
columns.find { |column| column.name == name }
end
end
......
......@@ -159,7 +159,6 @@ def visit_Arel_Nodes_Assignment o
def visit_Arel_Nodes_Equality o
right = o.right
# FIXME: maybe we should visit NilClass?
if right.nil?
"#{visit o.left} IS NULL"
else
......@@ -167,6 +166,16 @@ def visit_Arel_Nodes_Equality o
end
end
def visit_Arel_Nodes_NotEqual o
right = o.right
if right.nil?
"#{visit o.left} IS NOT NULL"
else
"#{visit o.left} != #{visit right}"
end
end
def visit_Arel_Nodes_UnqualifiedColumn o
"#{quote_column_name o.name}"
end
......
......@@ -3,6 +3,31 @@
module Arel
module Attributes
describe 'attribute' do
describe '#not_eq' do
it 'should create a NotEqual node' do
relation = Table.new(:users)
relation[:id].not_eq(10).should be_kind_of Nodes::NotEqual
end
it 'should generate != in sql' do
relation = Table.new(:users)
mgr = relation.project relation[:id]
mgr.where relation[:id].not_eq(10)
mgr.to_sql.should be_like %{
SELECT "users"."id" FROM "users" WHERE "users"."id" != 10
}
end
it 'should handle nil' do
relation = Table.new(:users)
mgr = relation.project relation[:id]
mgr.where relation[:id].not_eq(nil)
mgr.to_sql.should be_like %{
SELECT "users"."id" FROM "users" WHERE "users"."id" IS NOT NULL
}
end
end
describe '#gt' do
it 'should create a GreaterThan node' do
relation = Table.new(:users)
......
......@@ -120,7 +120,7 @@ module Arel
it 'returns a list of columns' do
columns = @relation.columns
check columns.length.should == 2
columns.map { |x| x.name }.sort.should == %w{ name id }.sort
columns.map { |x| x.name.to_s }.sort.should == %w{ name id }.sort
end
end
......@@ -136,7 +136,7 @@ module Arel
describe 'when given a', Symbol do
it "manufactures an attribute if the symbol names an attribute within the relation" do
column = @relation[:id]
check column.name.should == 'id'
check column.name.should == :id
column.should be_kind_of Attributes::Integer
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册