aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-03-02 17:56:18 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-03-02 17:56:18 -0800
commitc54392872f024d55e8a23ead3065e6119a52b234 (patch)
tree905aee0f7c40436ff41979227e9eb72e4a6840ba
parent86550ef2bee377a5e4134dc63dedb138bb9ab7dc (diff)
downloadrails-c54392872f024d55e8a23ead3065e6119a52b234.tar.gz
rails-c54392872f024d55e8a23ead3065e6119a52b234.tar.bz2
rails-c54392872f024d55e8a23ead3065e6119a52b234.zip
introduced engine dependency for sql strategies
- hacked in default engine for scalars -- BAD
-rw-r--r--lib/active_relation.rb1
-rw-r--r--lib/active_relation/engines.rb1
-rw-r--r--lib/active_relation/engines/engine.rb4
-rw-r--r--lib/active_relation/extensions/class.rb7
-rw-r--r--lib/active_relation/extensions/object.rb2
-rw-r--r--lib/active_relation/primitives/attribute.rb1
-rw-r--r--lib/active_relation/relations/compound.rb2
-rw-r--r--lib/active_relation/relations/join.rb5
-rw-r--r--lib/active_relation/relations/relation.rb14
-rw-r--r--lib/active_relation/relations/table.rb12
-rw-r--r--lib/active_relation/sessions/session.rb12
-rw-r--r--lib/active_relation/sql.rb15
-rw-r--r--spec/active_relation/unit/primitives/attribute_spec.rb6
-rw-r--r--spec/active_relation/unit/relations/join_spec.rb6
-rw-r--r--spec/active_relation/unit/relations/table_spec.rb11
-rw-r--r--spec/active_relation/unit/session/session_spec.rb10
-rw-r--r--spec/spec_helper.rb3
17 files changed, 74 insertions, 38 deletions
diff --git a/lib/active_relation.rb b/lib/active_relation.rb
index 23a80a9c38..f04825ad4c 100644
--- a/lib/active_relation.rb
+++ b/lib/active_relation.rb
@@ -8,4 +8,5 @@ require 'active_relation/sql'
require 'active_relation/extensions'
require 'active_relation/predicates'
require 'active_relation/relations'
+require 'active_relation/engines'
require 'active_relation/primitives' \ No newline at end of file
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)
diff --git a/spec/active_relation/unit/primitives/attribute_spec.rb b/spec/active_relation/unit/primitives/attribute_spec.rb
index 8b4f52c432..95c972d814 100644
--- a/spec/active_relation/unit/primitives/attribute_spec.rb
+++ b/spec/active_relation/unit/primitives/attribute_spec.rb
@@ -51,6 +51,12 @@ module ActiveRelation
end
end
+ describe '#engine' do
+ it "delegates to its relation" do
+ Attribute.new(@relation, :id).engine.should == @relation.engine
+ end
+ end
+
describe Attribute::Congruence do
describe '=~' do
it "obtains if the attributes are identical" do
diff --git a/spec/active_relation/unit/relations/join_spec.rb b/spec/active_relation/unit/relations/join_spec.rb
index 532dc08753..64d41effc5 100644
--- a/spec/active_relation/unit/relations/join_spec.rb
+++ b/spec/active_relation/unit/relations/join_spec.rb
@@ -38,6 +38,12 @@ module ActiveRelation
end
end
+ describe '#engine' do
+ it "delegates to a relation's engine" do
+ Join.new("INNER JOIN", @relation1, @relation2, @predicate).engine.should == @relation1.engine
+ end
+ end
+
describe 'with simple relations' do
describe '#attributes' do
it 'combines the attributes of the two relations' do
diff --git a/spec/active_relation/unit/relations/table_spec.rb b/spec/active_relation/unit/relations/table_spec.rb
index 5c7fa35e63..8f14ce33cf 100644
--- a/spec/active_relation/unit/relations/table_spec.rb
+++ b/spec/active_relation/unit/relations/table_spec.rb
@@ -80,5 +80,16 @@ module ActiveRelation
hash[Table.new(:users)].should == 1
end
end
+
+ describe '#engine' do
+ it "defaults to global engine" do
+ Table.engine = engine = Engine.new
+ Table.new(:users).engine.should == engine
+ end
+
+ it "can be specified" do
+ Table.new(:users, engine = Engine.new).engine.should == engine
+ end
+ end
end
end \ No newline at end of file
diff --git a/spec/active_relation/unit/session/session_spec.rb b/spec/active_relation/unit/session/session_spec.rb
index 118b99948c..1ac69976b5 100644
--- a/spec/active_relation/unit/session/session_spec.rb
+++ b/spec/active_relation/unit/session/session_spec.rb
@@ -40,19 +40,19 @@ module ActiveRelation
describe '#create' do
it "executes an insertion on the connection" do
- mock(@session.connection).insert(@insert.to_sql)
+ mock(@insert.engine).insert(@insert.to_sql)
@session.create(@insert)
end
end
describe '#read' do
it "executes an selection on the connection" do
- mock(@session.connection).select_all(@select.to_sql).once
+ mock(@select.engine).select_all(@select.to_sql).once
@session.read(@select)
end
it "is memoized" do
- mock(@session.connection).select_all(@select.to_sql).once
+ mock(@select.engine).select_all(@select.to_sql).once
@session.read(@select)
@session.read(@select)
end
@@ -60,14 +60,14 @@ module ActiveRelation
describe '#update' do
it "executes an update on the connection" do
- mock(@session.connection).update(@update.to_sql)
+ mock(@update.engine).update(@update.to_sql)
@session.update(@update)
end
end
describe '#delete' do
it "executes a delete on the connection" do
- mock(@session.connection).delete(@delete.to_sql)
+ mock(@delete.engine).delete(@delete.to_sql)
@session.delete(@delete)
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 59202dc9f0..cd9c8e96cb 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -28,4 +28,7 @@ end
Spec::Runner.configure do |config|
config.include(BeLikeMatcher)
config.mock_with :rr
+ config.before do
+ ActiveRelation::Table.engine = ActiveRecord::Base.connection
+ end
end \ No newline at end of file