diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-02 17:56:18 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-03-02 17:56:18 -0800 |
commit | c54392872f024d55e8a23ead3065e6119a52b234 (patch) | |
tree | 905aee0f7c40436ff41979227e9eb72e4a6840ba /lib/active_relation | |
parent | 86550ef2bee377a5e4134dc63dedb138bb9ab7dc (diff) | |
download | rails-c54392872f024d55e8a23ead3065e6119a52b234.tar.gz rails-c54392872f024d55e8a23ead3065e6119a52b234.tar.bz2 rails-c54392872f024d55e8a23ead3065e6119a52b234.zip |
introduced engine dependency for sql strategies
- hacked in default engine for scalars -- BAD
Diffstat (limited to 'lib/active_relation')
-rw-r--r-- | lib/active_relation/engines.rb | 1 | ||||
-rw-r--r-- | lib/active_relation/engines/engine.rb | 4 | ||||
-rw-r--r-- | lib/active_relation/extensions/class.rb | 7 | ||||
-rw-r--r-- | lib/active_relation/extensions/object.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 1 | ||||
-rw-r--r-- | lib/active_relation/relations/compound.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/relations/join.rb | 5 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 14 | ||||
-rw-r--r-- | lib/active_relation/relations/table.rb | 12 | ||||
-rw-r--r-- | lib/active_relation/sessions/session.rb | 12 | ||||
-rw-r--r-- | lib/active_relation/sql.rb | 15 |
11 files changed, 42 insertions, 33 deletions
diff --git a/lib/active_relation/engines.rb b/lib/active_relation/engines.rb new file mode 100644 index 0000000000..55eb817b88 --- /dev/null +++ b/lib/active_relation/engines.rb @@ -0,0 +1 @@ +require 'active_relation/engines/engine'
\ No newline at end of file diff --git a/lib/active_relation/engines/engine.rb b/lib/active_relation/engines/engine.rb new file mode 100644 index 0000000000..36b77b886e --- /dev/null +++ b/lib/active_relation/engines/engine.rb @@ -0,0 +1,4 @@ +module ActiveRelation + class Engine + end +end
\ No newline at end of file diff --git a/lib/active_relation/extensions/class.rb b/lib/active_relation/extensions/class.rb new file mode 100644 index 0000000000..72b3bf0c15 --- /dev/null +++ b/lib/active_relation/extensions/class.rb @@ -0,0 +1,7 @@ +class Class + def memoize(method) + define_method "#{method}_with_memoization" do |*args| + end + alias_method_chain method, :memoization + end +end
\ No newline at end of file diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb index c8a0ff49e5..b0c7ada999 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -12,7 +12,7 @@ class Object end def strategy - ActiveRelation::Sql::Scalar.new + ActiveRelation::Sql::Scalar.new(ActiveRelation::Table.engine) end def metaclass diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index a72acb0c34..baaae1973c 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -1,6 +1,7 @@ module ActiveRelation class Attribute attr_reader :relation, :name, :alias, :ancestor + delegate :engine, :to => :relation def initialize(relation, name, options = {}) @relation, @name, @alias, @ancestor, @column = relation, name, options[:alias], options[:ancestor] diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index 4e583e61e8..a10b7588e7 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -3,7 +3,7 @@ module ActiveRelation attr_reader :relation delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, - :hash, + :hash, :engine, :to => :relation def attributes diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index 8e0dcb2a4b..043b237de7 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -1,6 +1,7 @@ module ActiveRelation class Join < Relation attr_reader :join_sql, :relation1, :relation2, :predicates + delegate :engine, :to => :relation1 def initialize(join_sql, relation1, relation2, *predicates) @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates @@ -57,8 +58,10 @@ module ActiveRelation end Externalizer = Struct.new(:relation) do + delegate :engine, :to => :relation + def table_sql - relation.aggregation?? relation.to_sql(Sql::Aggregation.new) : relation.send(:table_sql) + relation.aggregation?? relation.to_sql(Sql::Aggregation.new(engine)) : relation.send(:table_sql) end def selects diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 889cf59d3a..3d4ff6613c 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -1,7 +1,5 @@ module ActiveRelation class Relation - include Sql::Quoting - def session Session.new end @@ -108,12 +106,12 @@ module ActiveRelation self == other end - def to_sql(strategy = Sql::Relation.new) + def to_sql(strategy = Sql::Relation.new(engine)) strategy.select [ - "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new) }.join(', ')}", + "SELECT #{attributes.collect{ |a| a.to_sql(Sql::Projection.new(engine)) }.join(', ')}", "FROM #{table_sql}", (joins unless joins.blank?), - ("WHERE #{selects.collect{|s| s.to_sql(Sql::Selection.new)}.join("\n\tAND ")}" unless selects.blank?), + ("WHERE #{selects.collect{|s| s.to_sql(Sql::Selection.new(engine))}.join("\n\tAND ")}" unless selects.blank?), ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank?), ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank?), ("LIMIT #{limit.to_sql}" unless limit.blank?), @@ -121,11 +119,7 @@ module ActiveRelation ].compact.join("\n"), self.alias end alias_method :to_s, :to_sql - - def connection - ActiveRecord::Base.connection - end - + def attribute_for_name(name) attributes.detect { |a| a.alias_or_name.to_s == name.to_s } end diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb index 271e76f362..84eb1213ee 100644 --- a/lib/active_relation/relations/table.rb +++ b/lib/active_relation/relations/table.rb @@ -1,10 +1,12 @@ module ActiveRelation class Table < Relation - attr_reader :name + cattr_accessor :engine + attr_reader :name, :engine + delegate :hash, :to => :name - def initialize(name) - @name = name.to_s + def initialize(name, engine = Table.engine) + @name, @engine = name.to_s, engine end def attributes @@ -31,7 +33,7 @@ module ActiveRelation end def columns - @columns ||= connection.columns(name, "#{name} Columns") + @columns ||= engine.columns(name, "#{name} Columns") end def descend @@ -44,7 +46,7 @@ module ActiveRelation protected def table_sql - "#{quote_table_name(name)}" + "#{engine.quote_table_name(name)}" end private diff --git a/lib/active_relation/sessions/session.rb b/lib/active_relation/sessions/session.rb index 7eb66a1395..0724abc5a7 100644 --- a/lib/active_relation/sessions/session.rb +++ b/lib/active_relation/sessions/session.rb @@ -26,25 +26,21 @@ module ActiveRelation end module CRUD - def connection - ActiveRecord::Base.connection - end - def create(insert) - connection.insert(insert.to_sql) + insert.engine.insert(insert.to_sql) end def read(select) @read ||= {} - @read.has_key?(select) ? @read[select] : (@read[select] = connection.select_all(select.to_sql)) + @read.has_key?(select) ? @read[select] : (@read[select] = select.engine.select_all(select.to_sql)) end def update(update) - connection.update(update.to_sql) + update.engine.update(update.to_sql) end def delete(delete) - connection.delete(delete.to_sql) + delete.engine.delete(delete.to_sql) end end include CRUD diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb index 0fc8b3e1be..a5701ca8cd 100644 --- a/lib/active_relation/sql.rb +++ b/lib/active_relation/sql.rb @@ -1,16 +1,17 @@ module ActiveRelation module Sql module Quoting - def connection - Session.new.connection - end - - delegate :quote_table_name, :quote_column_name, :quote, :to => :connection + delegate :quote_table_name, :quote_column_name, :quote, :to => :engine end # module Formatting Context / Strategy # unit test me!!! class Strategy + attr_reader :engine include Quoting + + def initialize(engine) + @engine = engine + end end class Projection < Strategy @@ -51,13 +52,13 @@ module ActiveRelation class Aggregation < Strategy def select(select_sql, aliaz) - "(#{select_sql}) AS #{quote_table_name(aliaz)}" + "(#{select_sql}) AS #{engine.quote_table_name(aliaz)}" end end class Attribute < Predicate def initialize(attribute) - @attribute = attribute + @attribute, @engine = attribute, attribute.engine end def scalar(scalar) |