dkubb (owner)

Revisions

gist: 221039 Download_button fork
public
Public Clone URL: git://gist.github.com/221039.git
Embed All Files: show embed
Set operations on a Collection #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env ruby -Ku
 
# encoding: utf-8
 
require 'rubygems'
require 'dm-core' # NOTE: dm-core and extlib were installed from git
require 'pp'
 
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
 
class Person
  include DataMapper::Resource
 
  property :id, Serial
  property :name, String, :nullable => false, :unique => true, :unique_index => true
end
 
DataMapper.auto_migrate!
 
customer1 = Person.create(:name => 'Dan Kubb')
customer2 = Person.create(:name => 'Alex Kubb')
 
puts '-' * 80, 'Union:'
pp Person.all(:name => 'Dan Kubb') | Person.all(:name => 'Alex Kubb')
pp Person.all(:name => 'Dan Kubb') + Person.all(:name => 'Alex Kubb')
 
puts '-' * 80, 'Intersection:'
pp Person.all(:name => 'Dan Kubb') & Person.all(:name => 'Alex Kubb')
 
puts '-' * 80, 'Difference:'
pp Person.all(:name => 'Dan Kubb') - Person.all(:name => 'Alex Kubb')
 
__END__
 
OUTPUT:
 
~ (0.000169) SELECT sqlite_version(*)
~ (0.000103) DROP TABLE IF EXISTS "people"
~ (0.000025) PRAGMA table_info("people")
~ (0.000733) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50) NOT NULL)
~ (0.000197) CREATE UNIQUE INDEX "unique_people_name" ON "people" ("name")
~ (0.000065) INSERT INTO "people" ("name") VALUES ('Dan Kubb')
~ (0.000062) INSERT INTO "people" ("name") VALUES ('Alex Kubb')
--------------------------------------------------------------------------------
Union:
~ (0.000096) SELECT "id", "name" FROM "people" WHERE ("name" = 'Dan Kubb' OR "name" = 'Alex Kubb') ORDER BY "id"
[#<Person @id=1 @name="Dan Kubb">, #<Person @id=2 @name="Alex Kubb">]
~ (0.000103) SELECT "id", "name" FROM "people" WHERE ("name" = 'Dan Kubb' OR "name" = 'Alex Kubb') ORDER BY "id"
[#<Person @id=1 @name="Dan Kubb">, #<Person @id=2 @name="Alex Kubb">]
--------------------------------------------------------------------------------
Intersection:
~ (0.000061) SELECT "id", "name" FROM "people" WHERE ("name" = 'Dan Kubb' AND "name" = 'Alex Kubb') ORDER BY "id"
[]
--------------------------------------------------------------------------------
Difference:
~ (0.000049) SELECT "id", "name" FROM "people" WHERE ("name" <> 'Alex Kubb' AND "name" = 'Dan Kubb') ORDER BY "id"
[#<Person @id=1 @name="Dan Kubb">]