From f0f6b7fb90b936cb78d786896598486821db6559 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 10 Sep 2010 16:58:19 -0700 Subject: adding not equal node, column names are expected to be symbols --- lib/arel/attributes/attribute.rb | 4 ++++ lib/arel/compatibility/wheres.rb | 4 ++++ lib/arel/nodes.rb | 1 + lib/arel/nodes/not_equal.rb | 6 ++++++ lib/arel/nodes/table_alias.rb | 2 +- lib/arel/table.rb | 4 ++-- lib/arel/visitors/to_sql.rb | 11 ++++++++++- spec/arel/attributes/attribute_spec.rb | 25 +++++++++++++++++++++++++ spec/arel/table_spec.rb | 4 ++-- 9 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 lib/arel/nodes/not_equal.rb diff --git a/lib/arel/attributes/attribute.rb b/lib/arel/attributes/attribute.rb index d77574f33e..617112f681 100644 --- a/lib/arel/attributes/attribute.rb +++ b/lib/arel/attributes/attribute.rb @@ -1,6 +1,10 @@ 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 diff --git a/lib/arel/compatibility/wheres.rb b/lib/arel/compatibility/wheres.rb index eeb7a2fa38..b73b178651 100644 --- a/lib/arel/compatibility/wheres.rb +++ b/lib/arel/compatibility/wheres.rb @@ -8,6 +8,10 @@ module Arel def value visitor.accept self end + + def name + super.to_sym + end end def initialize engine, collection diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb index 1f31219417..7510214cdf 100644 --- a/lib/arel/nodes.rb +++ b/lib/arel/nodes.rb @@ -1,5 +1,6 @@ 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' diff --git a/lib/arel/nodes/not_equal.rb b/lib/arel/nodes/not_equal.rb new file mode 100644 index 0000000000..7f892940cb --- /dev/null +++ b/lib/arel/nodes/not_equal.rb @@ -0,0 +1,6 @@ +module Arel + module Nodes + class NotEqual < Arel::Nodes::Binary + end + end +end diff --git a/lib/arel/nodes/table_alias.rb b/lib/arel/nodes/table_alias.rb index 4f1d70ee54..656a820a60 100644 --- a/lib/arel/nodes/table_alias.rb +++ b/lib/arel/nodes/table_alias.rb @@ -12,7 +12,7 @@ module Arel end def [] name - name = name.to_s + name = name.to_sym columns.find { |column| column.name == name } end end diff --git a/lib/arel/table.rb b/lib/arel/table.rb index 58e0c46959..efdc34ed94 100644 --- a/lib/arel/table.rb +++ b/lib/arel/table.rb @@ -73,12 +73,12 @@ module Arel 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 diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 8638bf43c3..761f315763 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -159,7 +159,6 @@ module Arel 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 @@ module Arel 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 diff --git a/spec/arel/attributes/attribute_spec.rb b/spec/arel/attributes/attribute_spec.rb index ae0f70c5ba..6679ca8b7a 100644 --- a/spec/arel/attributes/attribute_spec.rb +++ b/spec/arel/attributes/attribute_spec.rb @@ -3,6 +3,31 @@ require 'spec_helper' 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) diff --git a/spec/arel/table_spec.rb b/spec/arel/table_spec.rb index 6512ab310b..7cc0b20a25 100644 --- a/spec/arel/table_spec.rb +++ b/spec/arel/table_spec.rb @@ -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 -- cgit v1.2.3