From 2bbf8ca9d2af3ea959a21c3729b4894bc31f088b Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 13:58:29 -0400 Subject: reorganized call Conflicts: doc/TODO lib/arel/relations/relation.rb lib/arel/relations/writes/delete.rb lib/arel/relations/writes/insert.rb lib/arel/relations/writes/update.rb lib/arel/session.rb spec/arel/unit/relations/delete_spec.rb spec/arel/unit/relations/insert_spec.rb spec/arel/unit/relations/relation_spec.rb spec/arel/unit/relations/update_spec.rb spec/arel/unit/session/session_spec.rb --- doc/TODO | 16 +++++++--------- lib/arel/engine.rb | 24 ++++++++++++++++++++++++ lib/arel/relations.rb | 4 +--- lib/arel/relations/array.rb | 19 +++++++++++++++++++ lib/arel/relations/relation.rb | 9 ++------- lib/arel/relations/writes/delete.rb | 4 ++-- lib/arel/relations/writes/insert.rb | 4 ++-- lib/arel/relations/writes/update.rb | 4 ++-- lib/arel/session.rb | 8 ++++---- spec/arel/unit/relations/array_spec.rb | 27 +++++++++++++++++++++++++++ spec/arel/unit/relations/delete_spec.rb | 8 -------- spec/arel/unit/relations/insert_spec.rb | 11 ----------- spec/arel/unit/relations/relation_spec.rb | 7 ------- spec/arel/unit/relations/update_spec.rb | 11 ----------- spec/arel/unit/session/session_spec.rb | 10 +++++----- 15 files changed, 95 insertions(+), 71 deletions(-) create mode 100644 lib/arel/relations/array.rb create mode 100644 spec/arel/unit/relations/array_spec.rb diff --git a/doc/TODO b/doc/TODO index 740e5db5fe..ad12640881 100644 --- a/doc/TODO +++ b/doc/TODO @@ -1,19 +1,16 @@ todo: -- joining with LIMIT is like aggregations!! +- refactor adapter pattern +- implement in memory adapter +- implement mnesia adapter +- joins become subselects in writes: users.delete().where( addresses.c.user_id== select([users.c.id]). where(users.c.name=='jack') ) - - SELECT id, name, - (select count(*) FROM addresses WHERE - user_id=users.id) - FROM users - - SELECT users.*, (SELECT count(id) FROM addresses WHERE - addresses.user_id=users.id) FROM users +- rename externalize to derived. +- and/or w/ predicates - blocks for all operations - result sets to attr correlation too - cache expiry on write @@ -85,6 +82,7 @@ done: - test: find_attribute_given_attribute and all @attribute ||= everywhere and memoization of table class. - rename select to where - rename all ion classes +- joining with LIMIT is like aggregations!! icebox: - #bind in Attribute and Expression should be doing a descend? diff --git a/lib/arel/engine.rb b/lib/arel/engine.rb index 5a5ef06878..2bb4bbbd90 100644 --- a/lib/arel/engine.rb +++ b/lib/arel/engine.rb @@ -14,5 +14,29 @@ module Arel def method_missing(method, *args, &block) @ar.connection.send(method, *args, &block) end + + module CRUD + def create(relation) + connection.insert(relation.to_sql) + end + + def read(relation) + results = connection.execute(relation.to_sql) + rows = [] + results.each do |row| + rows << attributes.zip(row).to_hash + end + rows + end + + def update(relation) + connection.update(relation.to_sql) + end + + def delete(relation) + connection.delete(relation.to_sql) + end + end + include CRUD end end diff --git a/lib/arel/relations.rb b/lib/arel/relations.rb index 3394fac7cb..fd758ed15d 100644 --- a/lib/arel/relations.rb +++ b/lib/arel/relations.rb @@ -1,8 +1,6 @@ require 'arel/relations/relation' - require 'arel/relations/utilities' - require 'arel/relations/table' - +require 'arel/relations/array' require 'arel/relations/writes' require 'arel/relations/operations' \ No newline at end of file diff --git a/lib/arel/relations/array.rb b/lib/arel/relations/array.rb new file mode 100644 index 0000000000..dac65abbc2 --- /dev/null +++ b/lib/arel/relations/array.rb @@ -0,0 +1,19 @@ +module Arel + class Array < Relation + include Recursion::BaseCase + + def initialize(array, attribute_names) + @array, @attribute_names = array, attribute_names + end + + def attributes + @attributes ||= @attribute_names.collect do |name| + Attribute.new(self, name.to_sym) + end + end + + def call(connection = nil) + @array.collect { |row| attributes.zip(row).to_hash } + end + end +end \ No newline at end of file diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 5d2c336a15..ef295dfdd7 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -32,13 +32,8 @@ module Arel "IN" end - def call(connection = engine) - results = connection.execute(to_sql) - rows = [] - results.each do |row| - rows << attributes.zip(row).to_hash - end - rows + def call + engine.read(self) end def bind(relation) diff --git a/lib/arel/relations/writes/delete.rb b/lib/arel/relations/writes/delete.rb index b1ff3bef27..0a04454e7f 100644 --- a/lib/arel/relations/writes/delete.rb +++ b/lib/arel/relations/writes/delete.rb @@ -12,8 +12,8 @@ module Arel ].compact.join("\n") end - def call(connection = engine) - connection.delete(to_sql) + def call + engine.delete(self) end end end diff --git a/lib/arel/relations/writes/insert.rb b/lib/arel/relations/writes/insert.rb index d579ad06d0..d05bd9cdc8 100644 --- a/lib/arel/relations/writes/insert.rb +++ b/lib/arel/relations/writes/insert.rb @@ -16,8 +16,8 @@ module Arel ].join("\n") end - def call(connection = engine) - connection.insert(to_sql) + def call + engine.create(self) end end end diff --git a/lib/arel/relations/writes/update.rb b/lib/arel/relations/writes/update.rb index 2e90de3a52..fd0aadb479 100644 --- a/lib/arel/relations/writes/update.rb +++ b/lib/arel/relations/writes/update.rb @@ -18,8 +18,8 @@ module Arel ].join("\n") end - def call(connection = engine) - connection.update(to_sql) + def call + engine.update(self) end end end diff --git a/lib/arel/session.rb b/lib/arel/session.rb index d9a6e4b5e4..9c2ddc535b 100644 --- a/lib/arel/session.rb +++ b/lib/arel/session.rb @@ -25,21 +25,21 @@ module Arel module CRUD def create(insert) - insert.call(insert.engine) + insert.call end def read(select) (@read ||= Hash.new do |hash, select| - hash[select] = select.call(select.engine) + hash[select] = select.call end)[select] end def update(update) - update.call(update.engine) + update.call end def delete(delete) - delete.call(delete.engine) + delete.call end end include CRUD diff --git a/spec/arel/unit/relations/array_spec.rb b/spec/arel/unit/relations/array_spec.rb new file mode 100644 index 0000000000..1330f0f205 --- /dev/null +++ b/spec/arel/unit/relations/array_spec.rb @@ -0,0 +1,27 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') + +module Arel + describe Array do + before do + @relation = Array.new([[1], [2], [3]], [:id]) + end + + describe '#attributes' do + it 'manufactures attributes corresponding to the names given on construction' do + @relation.attributes.should == [ + Attribute.new(@relation, :id) + ] + end + end + + describe '#call' do + it "manufactures an array of hashes of attributes to values" do + @relation.call.should == [ + { @relation[:id] => 1 }, + { @relation[:id] => 2 }, + { @relation[:id] => 3 } + ] + end + end + end +end \ No newline at end of file diff --git a/spec/arel/unit/relations/delete_spec.rb b/spec/arel/unit/relations/delete_spec.rb index 3798178ccc..23aca563f7 100644 --- a/spec/arel/unit/relations/delete_spec.rb +++ b/spec/arel/unit/relations/delete_spec.rb @@ -59,13 +59,5 @@ module Arel end end end - - describe '#call' do - it 'executes a delete on the connection' do - deletion = Deletion.new(@relation) - mock(connection = Object.new).delete(deletion.to_sql) - deletion.call(connection) - end - end end end diff --git a/spec/arel/unit/relations/insert_spec.rb b/spec/arel/unit/relations/insert_spec.rb index 2fd07dd96c..5ab3ef1299 100644 --- a/spec/arel/unit/relations/insert_spec.rb +++ b/spec/arel/unit/relations/insert_spec.rb @@ -87,16 +87,5 @@ module Arel end end end - - describe '#call' do - before do - @insertion = Insert.new(@relation, @relation[:name] => "nick") - end - - it 'executes an insert on the connection' do - mock(connection = Object.new).insert(@insertion.to_sql) - @insertion.call(connection) - end - end end end diff --git a/spec/arel/unit/relations/relation_spec.rb b/spec/arel/unit/relations/relation_spec.rb index a3bfa67353..7df10be59c 100644 --- a/spec/arel/unit/relations/relation_spec.rb +++ b/spec/arel/unit/relations/relation_spec.rb @@ -165,12 +165,5 @@ module Arel @relation.first.should == @relation.session.read(@relation).first end end - - describe '#call' do - it 'executes a select_all on the connection' do - mock(connection = Object.new).execute(@relation.to_sql) { [] } - @relation.call(connection) - end - end end end diff --git a/spec/arel/unit/relations/update_spec.rb b/spec/arel/unit/relations/update_spec.rb index 0bbc9113c6..e0d7ddd295 100644 --- a/spec/arel/unit/relations/update_spec.rb +++ b/spec/arel/unit/relations/update_spec.rb @@ -117,16 +117,5 @@ module Arel end end - describe '#call' do - before do - @update = Update.new(@relation, @relation[:name] => "nick") - end - - it 'executes an update on the connection' do - mock(connection = Object.new).update(@update.to_sql) - @update.call(connection) - end - end - end end diff --git a/spec/arel/unit/session/session_spec.rb b/spec/arel/unit/session/session_spec.rb index 6e73d74f2d..c30ba6195f 100644 --- a/spec/arel/unit/session/session_spec.rb +++ b/spec/arel/unit/session/session_spec.rb @@ -40,19 +40,19 @@ module Arel describe '#create' do it "executes an insertion on the connection" do - mock(@insert).call(@insert.engine) + mock(@insert).call @session.create(@insert) end end describe '#read' do it "executes an selection on the connection" do - mock(@read).call(@read.engine) + mock(@read).call @session.read(@read) end it "is memoized" do - mock(@read).call(@read.engine).once + mock(@read).call.once @session.read(@read) @session.read(@read) end @@ -60,14 +60,14 @@ module Arel describe '#update' do it "executes an update on the connection" do - mock(@update).call(@update.engine) + mock(@update).call @session.update(@update) end end describe '#delete' do it "executes a delete on the connection" do - mock(@delete).call(@delete.engine) + mock(@delete).call @session.delete(@delete) end end -- cgit v1.2.3