aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/sql_algebra.rb2
-rw-r--r--lib/sql_algebra/extensions/base.rb54
-rw-r--r--lib/sql_algebra/extensions/hash.rb7
-rw-r--r--lib/sql_algebra/relations/relation.rb15
-rw-r--r--lib/sql_algebra/sql_builder/equals_condition_builder.rb2
-rw-r--r--lib/sql_algebra/sql_builder/order_builder.rb2
6 files changed, 68 insertions, 14 deletions
diff --git a/lib/sql_algebra.rb b/lib/sql_algebra.rb
index 475bb23138..fbd053541c 100644
--- a/lib/sql_algebra.rb
+++ b/lib/sql_algebra.rb
@@ -34,6 +34,8 @@ require 'sql_algebra/predicates/match_predicate'
require 'sql_algebra/extensions/range'
require 'sql_algebra/extensions/object'
require 'sql_algebra/extensions/array'
+require 'sql_algebra/extensions/base'
+require 'sql_algebra/extensions/hash'
require 'sql_algebra/sql_builder/sql_builder'
require 'sql_algebra/sql_builder/select_builder'
diff --git a/lib/sql_algebra/extensions/base.rb b/lib/sql_algebra/extensions/base.rb
index 0143caf23d..0dbdef703f 100644
--- a/lib/sql_algebra/extensions/base.rb
+++ b/lib/sql_algebra/extensions/base.rb
@@ -1,15 +1,47 @@
class ActiveRecord::Base
- def self.bring_forth(record, includes = [])
- object = cache.get(record % klass.primary_key) { Klass.instantiate(record % Klass.attributes) }
- includes.each do |include|
- case include
- when Symbol
- object.send(association = include).bring_forth(record)
- when Hash
- include.each do |association, nested_associations|
- object.send(association).bring_forth(record, nested_associations)
- end
- end
+ class << self
+ def cache
+ @identity_map ||= IdentityMap.new
end
+
+ def relation
+ @relation ||= TableRelation.new(table_name)
+ end
+ end
+
+ class IdentityMap
+ def initialize
+ @map = {}
+ end
+
+ def get(record, &block)
+ @map[record] ||= yield
+ end
+ end
+end
+
+class ActiveRecord::Associations::BelongsToAssociation
+ def instantiate(record, joins = [])
+ @target = proxy_reflection.klass.instantiate(record, joins)
+ loaded
+ end
+
+ # this basically disables belongs_to from loading themselves
+ def reload
+ @target = 'hack'
+ end
+end
+
+class ActiveRecord::Associations::AssociationCollection
+ def instantiate(record, joins = [])
+ @target << proxy_reflection.klass.instantiate(record, joins)
+ loaded # technically, this isn't true. doesn't matter though
+ end
+end
+
+class ActiveRecord::Associations::HasManyThroughAssociation
+ def instantiate(record, joins = [])
+ @target << proxy_reflection.klass.instantiate(record, joins)
+ loaded # again, not really true.
end
end \ No newline at end of file
diff --git a/lib/sql_algebra/extensions/hash.rb b/lib/sql_algebra/extensions/hash.rb
new file mode 100644
index 0000000000..c83ee0d04f
--- /dev/null
+++ b/lib/sql_algebra/extensions/hash.rb
@@ -0,0 +1,7 @@
+class Hash
+ def alias(&block)
+ inject({}) do |aliased, (key, value)|
+ aliased.merge(yield(key) => value)
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/sql_algebra/relations/relation.rb b/lib/sql_algebra/relations/relation.rb
index 13742cb570..8efe0c7d9f 100644
--- a/lib/sql_algebra/relations/relation.rb
+++ b/lib/sql_algebra/relations/relation.rb
@@ -1,4 +1,17 @@
class Relation
+ module Iteration
+ include Enumerable
+
+ def each(&block)
+ connection.select_all(to_s).each(&block)
+ end
+
+ def first
+ connection.select_one(to_s)
+ end
+ end
+ include Iteration
+
module Operations
def <=>(other)
InnerJoinOperation.new(self, other)
@@ -60,7 +73,7 @@ class Relation
end
end
delegate :to_s, :to => :to_sql
-
+
protected
def attributes; [] end
def joins; [] end
diff --git a/lib/sql_algebra/sql_builder/equals_condition_builder.rb b/lib/sql_algebra/sql_builder/equals_condition_builder.rb
index 70067c20ca..cfa919c34c 100644
--- a/lib/sql_algebra/sql_builder/equals_condition_builder.rb
+++ b/lib/sql_algebra/sql_builder/equals_condition_builder.rb
@@ -5,7 +5,7 @@ class EqualsConditionBuilder < SqlBuilder
end
def column(table, column, aliaz = nil)
- @operands << (aliaz ? quote(aliaz) : "#{quote_table_name(table)}.#{quote_column_name(column)}")
+ @operands << "#{quote_table_name(table)}.#{quote_column_name(column)}"
end
def value(value)
diff --git a/lib/sql_algebra/sql_builder/order_builder.rb b/lib/sql_algebra/sql_builder/order_builder.rb
index 43f705faf0..66a8cfdba9 100644
--- a/lib/sql_algebra/sql_builder/order_builder.rb
+++ b/lib/sql_algebra/sql_builder/order_builder.rb
@@ -5,7 +5,7 @@ class OrderBuilder < SqlBuilder
end
def column(table, column, aliaz = nil)
- @orders << (aliaz ? quote(aliaz) : "#{quote_table_name(table)}.#{quote_column_name(column)}")
+ @orders << "#{quote_table_name(table)}.#{quote_column_name(column)}"
end
def to_s