Skip to content

Instantly share code, notes, and snippets.

@farleyknight
Created February 8, 2010 13:16
Show Gist options
  • Save farleyknight/298126 to your computer and use it in GitHub Desktop.
Save farleyknight/298126 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
Thing.all.each do |thing|
puts thing.widgets.count
end
__END__
~ (0.000180) SELECT sqlite_version(*)
~ (0.000134) DROP TABLE IF EXISTS "things"
~ (0.000017) DROP TABLE IF EXISTS "widgets"
~ (0.000019) PRAGMA table_info("things")
~ (0.000366) CREATE TABLE "things" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
~ (0.000012) PRAGMA table_info("widgets")
~ (0.000116) CREATE TABLE "widgets" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "thing_id" INTEGER NOT NULL)
~ (0.000128) CREATE INDEX "index_widgets_thing" ON "widgets" ("thing_id")
~ (0.000034) INSERT INTO "things" DEFAULT VALUES
~ (0.000030) INSERT INTO "things" DEFAULT VALUES
~ (0.000035) INSERT INTO "things" DEFAULT VALUES
~ (0.000032) INSERT INTO "things" DEFAULT VALUES
~ (0.000033) INSERT INTO "things" DEFAULT VALUES
~ (0.000033) INSERT INTO "things" DEFAULT VALUES
~ (0.000032) INSERT INTO "things" DEFAULT VALUES
~ (0.000036) INSERT INTO "things" DEFAULT VALUES
~ (0.000032) INSERT INTO "things" DEFAULT VALUES
~ (0.000036) INSERT INTO "things" DEFAULT VALUES
~ (0.000042) SELECT "id" FROM "things" ORDER BY "id" LIMIT 1
~ (0.000056) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000045) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000040) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000040) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000042) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000041) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000040) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000040) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000041) INSERT INTO "widgets" ("thing_id") VALUES (1)
~ (0.000040) INSERT INTO "widgets" ("thing_id") VALUES (1)
--------------------------------------------------------------------------------
~ (0.000033) SELECT "id" FROM "things" ORDER BY "id"
~ (0.000071) SELECT "id", "thing_id" FROM "widgets" WHERE "thing_id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ORDER BY "id"
~ (0.000026) SELECT "id" FROM "things" ORDER BY "id"
~ (0.000079) SELECT "id", "thing_id" FROM "widgets" WHERE "thing_id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ORDER BY "id"
~ (0.000087) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 1
1
~ (0.000031) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 2
1
~ (0.000029) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 3
1
~ (0.000030) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 4
1
~ (0.000029) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 5
1
~ (0.000030) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 6
1
~ (0.000030) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 7
1
~ (0.000038) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 8
1
~ (0.000032) SELECT COUNT(*) FROM "widgets" WHERE "thing_id" = 9
1
~ (0.000029) 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