diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-23 19:29:18 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-23 19:29:18 -0800 |
commit | 51fdd769c0cb096d6e6f04afc3ebb91833d625bc (patch) | |
tree | 9e6078f474ccf3424495e567cb39b232ce5c5d25 /lib | |
parent | f10d3be703cada60783c671866dd48194b800002 (diff) | |
download | rails-51fdd769c0cb096d6e6f04afc3ebb91833d625bc.tar.gz rails-51fdd769c0cb096d6e6f04afc3ebb91833d625bc.tar.bz2 rails-51fdd769c0cb096d6e6f04afc3ebb91833d625bc.zip |
Introduced concept of session. It does not yet support multiple databases, nor transactions, but it's a start!
Diffstat (limited to 'lib')
-rw-r--r-- | lib/active_relation/relations.rb | 3 | ||||
-rw-r--r-- | lib/active_relation/relations/deletion.rb | 7 | ||||
-rw-r--r-- | lib/active_relation/relations/insertion.rb | 13 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 45 | ||||
-rw-r--r-- | lib/active_relation/relations/update.rb | 8 | ||||
-rw-r--r-- | lib/active_relation/sessions/session.rb | 38 | ||||
-rw-r--r-- | lib/active_relation/sql.rb | 2 |
7 files changed, 89 insertions, 27 deletions
diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb index 97c17b3792..f992fca8be 100644 --- a/lib/active_relation/relations.rb +++ b/lib/active_relation/relations.rb @@ -11,4 +11,5 @@ require 'active_relation/relations/rename' require 'active_relation/relations/deletion' require 'active_relation/relations/insertion' require 'active_relation/relations/update' -require 'active_relation/relations/alias'
\ No newline at end of file +require 'active_relation/relations/alias' +require 'active_relation/sessions/session'
\ No newline at end of file diff --git a/lib/active_relation/relations/deletion.rb b/lib/active_relation/relations/deletion.rb index 4ac40a146b..f3d81baf27 100644 --- a/lib/active_relation/relations/deletion.rb +++ b/lib/active_relation/relations/deletion.rb @@ -10,6 +10,11 @@ module ActiveRelation "FROM #{table_sql}", ("WHERE #{selects.collect(&:to_sql).join('\n\tAND ')}" unless selects.blank?) ].compact.join("\n") - end + end + + def ==(other) + self.class == other.class and + relation == other.relation + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/insertion.rb b/lib/active_relation/relations/insertion.rb index 31e4339ee0..dd5b8ec530 100644 --- a/lib/active_relation/relations/insertion.rb +++ b/lib/active_relation/relations/insertion.rb @@ -11,13 +11,14 @@ module ActiveRelation "INSERT", "INTO #{table_sql}", "(#{record.keys.collect(&:to_sql).join(', ')})", - "VALUES #{inserts.collect(&:to_sql).join(', ')}" + "VALUES #{record.to_sql}" ].join("\n") - end - - protected - def inserts - relation.inserts + [record] + end + + def ==(other) + self.class == other.class and + relation == other.relation and + record == other.record end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index f20561c5b2..1d5e194923 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -2,18 +2,22 @@ module ActiveRelation class Relation include Sql::Quoting - module Iteration - include Enumerable - + def session + Session.instance + end + + module Enumerable + include ::Enumerable + def each(&block) - connection.select_all(to_s).each(&block) + session.read(self).each(&block) end - + def first - connection.select_one(to_s) + session.read(self).first end end - include Iteration + include Enumerable module Operations def join(other) @@ -58,18 +62,25 @@ module ActiveRelation def rename(attribute, aliaz) Rename.new(self, attribute => aliaz) end - - def insert(record) - Insertion.new(self, record) - end - - def delete - Deletion.new(self) - end - + def aggregate(*expressions) AggregateOperation.new(self, expressions) end + + module Writes + def insert(record) + session.create Insertion.new(self, record); self + end + + def update(assignments) + session.update Update.new(self, assignments); self + end + + def delete + session.delete Deletion.new(self); self + end + end + include Writes JoinOperation = Struct.new(:join_sql, :relation1, :relation2) do def on(*predicates) @@ -84,7 +95,7 @@ module ActiveRelation end end include Operations - + def aggregation? false end diff --git a/lib/active_relation/relations/update.rb b/lib/active_relation/relations/update.rb index d28608fdd5..8909ee80a0 100644 --- a/lib/active_relation/relations/update.rb +++ b/lib/active_relation/relations/update.rb @@ -12,6 +12,12 @@ module ActiveRelation assignments.inject([]) { |assignments, (attribute, value)| assignments << "#{attribute.to_sql} = #{value.to_sql}" }.join(" "), ("WHERE #{selects.collect(&:to_sql).join('\n\tAND ')}" unless selects.blank?) ].join("\n") - end + end + + def ==(other) + self.class == other.class and + relation == other.relation and + assignments == other.assignments + end end end
\ No newline at end of file diff --git a/lib/active_relation/sessions/session.rb b/lib/active_relation/sessions/session.rb new file mode 100644 index 0000000000..d6e2ae7169 --- /dev/null +++ b/lib/active_relation/sessions/session.rb @@ -0,0 +1,38 @@ +require 'singleton' + +module ActiveRelation + class Session + include Singleton + + module CRUD + def connection + ActiveRecord::Base.connection + end + + def create(insert) + connection.insert(insert.to_sql) + end + + def read(select) + connection.select_all(select.to_sql) + end + + def update(update) + connection.update(update.to_sql) + end + + def delete(delete) + connection.delete(delete.to_sql) + end + end + include CRUD + + module Transactions + end + include Transactions + + module UnitOfWork + end + include UnitOfWork + end +end
\ No newline at end of file diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index 0384de44ef..85bcb4107b 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -2,7 +2,7 @@ module ActiveRelation module Sql module Quoting def connection - ActiveRecord::Base.connection + Session.instance.connection end delegate :quote_table_name, :quote_column_name, :quote, :to => :connection |