Skip to content

Instantly share code, notes, and snippets.

@snusnu
Created June 1, 2012 10:11
Show Gist options
  • Save snusnu/2850960 to your computer and use it in GitHub Desktop.
Save snusnu/2850960 to your computer and use it in GitHub Desktop.
Mapping DM1 resources to veritas gateways
require 'dm-core'
require 'dm-migrations'
URI = 'postgres://localhost/test'.freeze
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, URI)
class Account
include DataMapper::Resource
property :id, Serial
property :email, String, :required => true, :unique => true, :unique_index => true
has 1, :person
end
class Person
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
belongs_to :account, :unique => true
end
DataMapper.finalize.auto_migrate!
Account.create(:email => 'test@test.com', :person => Person.new(:name => 'snusnu'))
require 'pp'
require 'backports'
require 'backports/basic_object'
require 'veritas'
require 'veritas-do-adapter'
require 'veritas-optimizer'
module DataMapper
module Veritas
TYPE_MAP = {
DataMapper::Property::Serial => Integer,
DataMapper::Property::String => String,
DataMapper::Property::Integer => Integer,
# this is obviously incomplete ...
}
class Repository
attr_reader :adapter
attr_reader :registry
def initialize(uri)
@adapter = ::Veritas::Adapter::DataObjects.new(uri)
@registry = DataMapper::Veritas::RelationRegisty.new
initialize_registry
end
def [](relation_name)
@registry[relation_name]
end
private
def initialize_registry
DataMapper::Model.descendants.each do |model|
name = model.storage_names[:default]
header = model.properties.map { |property| [ property.name, DataMapper::Veritas::TYPE_MAP[property.class] ] }
relation = ::Veritas::Relation::Base.new(name, header)
registry << ::Veritas::Relation::Gateway.new(adapter, relation)
end
end
end # class Repository
class RelationRegisty
def initialize(relations = {})
@relations = relations
end
def [](name)
@relations[name.to_sym]
end
def []=(name, relation)
@relations[name.to_sym] = relation
end
def <<(relation)
self[relation.name] = relation
end
end # class RelationRegisty
end # module Veritas
end # DataMapper
class App
def self.db
@db ||= DataMapper::Veritas::Repository.new(URI)
end
end
accounts = App.db[:accounts]
people = App.db[:people]
accounts.each { |tuple| pp(tuple.to_ary) }
relation = accounts.join(people).optimize
relation.each { |tuple| pp(tuple.to_ary) }
__END__
~ (0.000368) SET backslash_quote = off
~ (0.000127) SET standard_conforming_strings = on
~ (0.000168) SET client_min_messages = warning
~ (0.000797) SELECT version()
~ (0.000120) SET client_min_messages = warning
~ (0.058407) DROP TABLE IF EXISTS "accounts"
~ (0.000231) RESET client_min_messages
~ (0.000145) SET client_min_messages = warning
~ (0.000427) SELECT current_schema()
~ (0.005512) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = 'public' AND "table_name" = 'accounts'
~ (0.009155) CREATE TABLE "accounts" ("id" SERIAL NOT NULL, "email" VARCHAR(50) NOT NULL, PRIMARY KEY("id"))
~ (0.003243) CREATE UNIQUE INDEX "unique_accounts_email" ON "accounts" ("email")
~ (0.000248) RESET client_min_messages
~ (0.000227) SET client_min_messages = warning
~ (0.003470) DROP TABLE IF EXISTS "people"
~ (0.000150) RESET client_min_messages
~ (0.000130) SET client_min_messages = warning
~ (0.001146) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = 'public' AND "table_name" = 'people'
~ (0.013248) CREATE TABLE "people" ("id" SERIAL NOT NULL, "name" VARCHAR(50) NOT NULL, "account_id" INTEGER NOT NULL, PRIMARY KEY("id"))
~ (0.002963) CREATE INDEX "index_people_account" ON "people" ("account_id")
~ (0.003103) CREATE UNIQUE INDEX "unique_people_account_id" ON "people" ("account_id")
~ (0.000227) RESET client_min_messages
~ (0.001573) INSERT INTO "accounts" ("email") VALUES ('test@test.com') RETURNING "id"
~ (0.001897) INSERT INTO "people" ("name", "account_id") VALUES ('snusnu', 1) RETURNING "id"
~ (0.000340) SET backslash_quote = off
~ (0.000138) SET standard_conforming_strings = on
~ (0.000514) SET client_min_messages = warning
~ (0.001514) SELECT "id", "email" FROM "accounts"
[1, "test@test.com"]
~ (0.001434) SELECT "id", "email", "name", "account_id" FROM "accounts" AS "left" NATURAL JOIN "people" AS "right"
[1, "test@test.com", "snusnu", 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment