· 7 years ago · Oct 16, 2018, 12:10 PM
1require 'dm-core'
2require 'dm-migrations'
3
4URI = 'postgres://localhost/test'.freeze
5
6DataMapper::Logger.new($stdout, :debug)
7DataMapper.setup(:default, URI)
8
9class Account
10 include DataMapper::Resource
11
12 property :id, Serial
13 property :email, String, :required => true, :unique => true, :unique_index => true
14
15 has 1, :person
16end
17
18class Person
19 include DataMapper::Resource
20
21 property :id, Serial
22 property :name, String, :required => true
23
24 belongs_to :account, :unique => true
25end
26
27DataMapper.finalize.auto_migrate!
28
29Account.create(:email => 'test@test.com', :person => Person.new(:name => 'snusnu'))
30
31require 'pp'
32require 'backports'
33require 'backports/basic_object'
34require 'veritas'
35require 'veritas-do-adapter'
36require 'veritas-optimizer'
37
38module DataMapper
39 module Veritas
40
41 TYPE_MAP = {
42 DataMapper::Property::Serial => Integer,
43 DataMapper::Property::String => String,
44 DataMapper::Property::Integer => Integer,
45 # this is obviously incomplete ...
46 }
47
48 class Repository
49 attr_reader :adapter
50 attr_reader :registry
51
52 def initialize(uri)
53 @adapter = ::Veritas::Adapter::DataObjects.new(uri)
54 @registry = DataMapper::Veritas::RelationRegisty.new
55
56 initialize_registry
57 end
58
59 def [](relation_name)
60 @registry[relation_name]
61 end
62
63 private
64
65 def initialize_registry
66 DataMapper::Model.descendants.each do |model|
67 name = model.storage_names[:default]
68 header = model.properties.map { |property| [ property.name, DataMapper::Veritas::TYPE_MAP[property.class] ] }
69 relation = ::Veritas::Relation::Base.new(name, header)
70
71 registry << ::Veritas::Relation::Gateway.new(adapter, relation)
72 end
73 end
74 end # class Repository
75
76 class RelationRegisty
77
78 def initialize(relations = {})
79 @relations = relations
80 end
81
82 def [](name)
83 @relations[name.to_sym]
84 end
85
86 def []=(name, relation)
87 @relations[name.to_sym] = relation
88 end
89
90 def <<(relation)
91 self[relation.name] = relation
92 end
93 end # class RelationRegisty
94 end # module Veritas
95end # DataMapper
96
97class App
98 def self.db
99 @db ||= DataMapper::Veritas::Repository.new(URI)
100 end
101end
102
103accounts = App.db[:accounts]
104people = App.db[:people]
105
106accounts.each { |tuple| pp(tuple.to_ary) }
107
108relation = accounts.join(people).optimize
109
110relation.each { |tuple| pp(tuple.to_ary) }
111
112__END__
113
114 ~ (0.000368) SET backslash_quote = off
115 ~ (0.000127) SET standard_conforming_strings = on
116 ~ (0.000168) SET client_min_messages = warning
117 ~ (0.000797) SELECT version()
118 ~ (0.000120) SET client_min_messages = warning
119 ~ (0.058407) DROP TABLE IF EXISTS "accounts"
120 ~ (0.000231) RESET client_min_messages
121 ~ (0.000145) SET client_min_messages = warning
122 ~ (0.000427) SELECT current_schema()
123 ~ (0.005512) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = 'public' AND "table_name" = 'accounts'
124 ~ (0.009155) CREATE TABLE "accounts" ("id" SERIAL NOT NULL, "email" VARCHAR(50) NOT NULL, PRIMARY KEY("id"))
125 ~ (0.003243) CREATE UNIQUE INDEX "unique_accounts_email" ON "accounts" ("email")
126 ~ (0.000248) RESET client_min_messages
127 ~ (0.000227) SET client_min_messages = warning
128 ~ (0.003470) DROP TABLE IF EXISTS "people"
129 ~ (0.000150) RESET client_min_messages
130 ~ (0.000130) SET client_min_messages = warning
131 ~ (0.001146) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = 'public' AND "table_name" = 'people'
132 ~ (0.013248) CREATE TABLE "people" ("id" SERIAL NOT NULL, "name" VARCHAR(50) NOT NULL, "account_id" INTEGER NOT NULL, PRIMARY KEY("id"))
133 ~ (0.002963) CREATE INDEX "index_people_account" ON "people" ("account_id")
134 ~ (0.003103) CREATE UNIQUE INDEX "unique_people_account_id" ON "people" ("account_id")
135 ~ (0.000227) RESET client_min_messages
136 ~ (0.001573) INSERT INTO "accounts" ("email") VALUES ('test@test.com') RETURNING "id"
137 ~ (0.001897) INSERT INTO "people" ("name", "account_id") VALUES ('snusnu', 1) RETURNING "id"
138 ~ (0.000340) SET backslash_quote = off
139 ~ (0.000138) SET standard_conforming_strings = on
140 ~ (0.000514) SET client_min_messages = warning
141 ~ (0.001514) SELECT "id", "email" FROM "accounts"
142[1, "test@test.com"]
143 ~ (0.001434) SELECT "id", "email", "name", "account_id" FROM "accounts" AS "left" NATURAL JOIN "people" AS "right"
144[1, "test@test.com", "snusnu", 1]