Skip to content

Instantly share code, notes, and snippets.

@probablykabari
Forked from farleyknight/gist:298126
Created February 9, 2010 23:27
Show Gist options
  • Save probablykabari/299818 to your computer and use it in GitHub Desktop.
Save probablykabari/299818 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# A one file test to show count
require 'rubygems'
require 'dm-core'
require 'dm-aggregates'
# setup the logger
DataMapper::Logger.new(STDOUT, :debug)
# connect to the DB
DataMapper.setup(:default, 'sqlite3::memory:')
class Thing
include DataMapper::Resource
# properties
property :id, Serial
has n, :widgets
end
class Widget
include DataMapper::Resource
# properties
property :id, Serial
belongs_to :thing
end
DataMapper.auto_migrate!
10.times do
thing = Thing.create
thing.widgets.create
end
@t = Thing.first
puts "-"*80
# modify each
DataMapper::Collection.class_eval do
alias_method :orig_each, :each
def each(eager_load = true, &block)
eager_load ? orig_each(&block) : super(&block)
end
end
# --------
Thing.all.each(false) do |thing|
puts thing.widgets.count
end
__END__
~ (0.000180) SELECT sqlite_version(*)
~ (0.000320) DROP TABLE IF EXISTS "things"
~ (0.000032) DROP TABLE IF EXISTS "widgets"
~ (0.000889) PRAGMA table_info("things")
~ (0.000470) CREATE TABLE "things" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
~ (0.000031) PRAGMA table_info("widgets")
~ (0.000191) CREATE TABLE "widgets" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "thing_id" INTEGER NOT NULL)
~ (0.000886) CREATE INDEX "index_widgets_thing" ON "widgets" ("thing_id")
~ (0.000068) INSERT INTO "things" DEFAULT VALUES
~ (0.000091) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000062) INSERT INTO "things" DEFAULT VALUES
~ (0.000088) INSERT INTO "widgets" ("thing_id") VALUES (2)
~ (0.000078) INSERT INTO "things" DEFAULT VALUES
~ (0.000096) INSERT INTO "widgets" ("thing_id") VALUES (3)
~ (0.000070) INSERT INTO "things" DEFAULT VALUES
~ (0.000091) INSERT INTO "widgets" ("thing_id") VALUES (4)
~ (0.000077) INSERT INTO "things" DEFAULT VALUES
~ (0.000089) INSERT INTO "widgets" ("thing_id") VALUES (5)
~ (0.000128) INSERT INTO "things" DEFAULT VALUES
~ (0.000090) INSERT INTO "widgets" ("thing_id") VALUES (6)
~ (0.000078) INSERT INTO "things" DEFAULT VALUES
~ (0.000093) INSERT INTO "widgets" ("thing_id") VALUES (7)
~ (0.000075) INSERT INTO "things" DEFAULT VALUES
~ (0.000090) INSERT INTO "widgets" ("thing_id") VALUES (8)
~ (0.000064) INSERT INTO "things" DEFAULT VALUES
~ (0.000090) INSERT INTO "widgets" ("thing_id") VALUES (9)
~ (0.000070) INSERT INTO "things" DEFAULT VALUES
~ (0.000099) INSERT INTO "widgets" ("thing_id") VALUES (10)
~ (0.000070) SELECT "id" FROM "things" ORDER BY "id" LIMIT 1
--------------------------------------------------------------------------------
~ (0.000070) SELECT "id" FROM "things" ORDER BY "id"
~ (0.000067) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 1
1
~ (0.000069) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 2
1
~ (0.000062) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 3
1
~ (0.000063) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 4
1
~ (0.000064) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 5
1
~ (0.000064) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 6
1
~ (0.000062) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 7
1
~ (0.000085) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 8
1
~ (0.000074) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 9
1
~ (0.000072) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 10
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment