Created
September 30, 2010 07:09
-
-
Save durandom/604156 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'dm-core' | |
require 'dm-migrations' | |
DataMapper::Logger.new(STDOUT, :debug) | |
DataMapper.setup(:default, 'sqlite::memory:') | |
class Customer | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String, :required => true, :length => 1..100 | |
has n, :orders | |
has n, :items, :through => :orders | |
end | |
class Order | |
include DataMapper::Resource | |
property :id, Serial | |
property :reference, String, :required => true, :length => 1..20 | |
belongs_to :customer | |
has n, :order_lines | |
has n, :items, :through => :order_lines | |
end | |
class OrderLine | |
include DataMapper::Resource | |
property :id, Serial | |
property :quantity, Integer, :required => true, :default => 1, :min => 1 | |
property :unit_price, Decimal, :required => true, :default => lambda { |r, p| r.item.unit_price } | |
belongs_to :order | |
belongs_to :item | |
end | |
class Item | |
include DataMapper::Resource | |
property :id, Serial | |
property :sku, String, :required => true, :length => 1..20 | |
property :unit_price, Decimal, :required => true, :min => 0 | |
has n, :order_lines | |
end | |
DataMapper.finalize | |
DataMapper.auto_migrate! | |
# create some objects | |
5.times do | |
c = { :name => rand(1000).to_s, :orders => [] } | |
5.times do | |
o = { :reference => rand(1000).to_s, :order_lines => [] } | |
5.times do | |
ol = { :quantity => rand(10).to_i, | |
:item => { | |
:sku => rand(1000).to_s, | |
:unit_price => rand(1000) | |
} | |
} | |
o[:order_lines] << ol | |
end | |
c[:orders] << o | |
end | |
Customer.create(c) | |
end | |
# now print all orders, | |
# with eager loading it should fire 4 queries (Customers, Orders, OrderLines, Items) | |
Customer.all(:limit => 3).each do |c| | |
c.orders.each do |o| | |
o.order_lines.each do |ol| | |
a = "#{c.name}: #{o.reference} #{ol.quantity} x #{ol.item.sku}" | |
end | |
end | |
end | |
# But it does: | |
# ~ (0.000028) SELECT "id", "name" FROM "customers" ORDER BY "id" LIMIT 3 | |
# ~ (0.000031) SELECT "id", "reference", "customer_id" FROM "orders" WHERE "customer_id" IN (1, 2, 3) ORDER BY "id" | |
# ~ (0.000031) SELECT "id", "quantity", "unit_price", "order_id", "item_id" FROM "order_lines" WHERE "order_id" IN (1, 2, 3, 4, 5) ORDER BY "id" | |
# ~ (0.000031) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (1, 2, 3, 4, 5) ORDER BY "id" | |
# ~ (0.000027) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (6, 7, 8, 9, 10) ORDER BY "id" | |
# ~ (0.000025) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (11, 12, 13, 14, 15) ORDER BY "id" | |
# ~ (0.000033) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (16, 17, 18, 19, 20) ORDER BY "id" | |
# ~ (0.000028) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (21, 22, 23, 24, 25) ORDER BY "id" | |
# ~ (0.000032) SELECT "id", "quantity", "unit_price", "order_id", "item_id" FROM "order_lines" WHERE "order_id" IN (6, 7, 8, 9, 10) ORDER BY "id" | |
# ~ (0.000026) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (26, 27, 28, 29, 30) ORDER BY "id" | |
# ~ (0.000026) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (31, 32, 33, 34, 35) ORDER BY "id" | |
# ~ (0.000027) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (36, 37, 38, 39, 40) ORDER BY "id" | |
# ~ (0.000025) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (41, 42, 43, 44, 45) ORDER BY "id" | |
# ~ (0.000025) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (46, 47, 48, 49, 50) ORDER BY "id" | |
# ~ (0.000030) SELECT "id", "quantity", "unit_price", "order_id", "item_id" FROM "order_lines" WHERE "order_id" IN (11, 12, 13, 14, 15) ORDER BY "id" | |
# ~ (0.000056) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (51, 52, 53, 54, 55) ORDER BY "id" | |
# ~ (0.000026) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (56, 57, 58, 59, 60) ORDER BY "id" | |
# ~ (0.000026) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (61, 62, 63, 64, 65) ORDER BY "id" | |
# ~ (0.000045) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (66, 67, 68, 69, 70) ORDER BY "id" | |
# ~ (0.000030) SELECT "id", "sku", "unit_price" FROM "items" WHERE "id" IN (71, 72, 73, 74, 75) ORDER BY "id" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment