Class: Graffiti::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/graffiti/store.rb

Overview

API for the RDF storage access similar to DBI or Sequel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, config) ⇒ Store

initialize class attributes

db is a Sequel database handle

config is a hash of configuraiton options for RdfConfig



33
34
35
36
37
38
39
# File 'lib/graffiti/store.rb', line 33

def initialize(db, config)
  @db = db
  @config = RdfConfig.new(config)

  # cache parsed Squish SELECT queries
  @select_cache = SynCache::Cache.new(nil, 1000)
end

Instance Attribute Details

#configObject (readonly)

storage configuration in an RdfConfig object



43
44
45
# File 'lib/graffiti/store.rb', line 43

def config
  @config
end

Instance Method Details

#assert(query, params = {}) ⇒ Object

merge Squish query into RDF database

returns list of new ids assigned to blank nodes listed in INSERT section



93
94
95
96
97
# File 'lib/graffiti/store.rb', line 93

def assert(query, params={})
  @db.transaction do
    SquishAssert.new(@config, query).run(@db, params)
  end
end

#fetch(query, params = {}) ⇒ Object



58
59
60
# File 'lib/graffiti/store.rb', line 58

def fetch(query, params={})
  @db.fetch(select(query), params)
end

#get_property(subject, property) ⇒ Object

get value of subject’s property



53
54
55
56
# File 'lib/graffiti/store.rb', line 53

def get_property(subject, property)
  fetch(%{SELECT ?object WHERE (#{property} :subject ?object)},
        :subject => subject).get(:object)
end

#ns_shrink(uriref) ⇒ Object

replace schema uri with a prefix from the configured namespaces



47
48
49
# File 'lib/graffiti/store.rb', line 47

def ns_shrink(uriref)
  SquishQuery.ns_shrink(uriref, @config.ns)
end

#select(query) ⇒ Object

accepts String or pre-parsed SquishQuery object, caches SQL by String



81
82
83
84
85
86
87
# File 'lib/graffiti/store.rb', line 81

def select(query)
  query.kind_of?(String) and
    query = @select_cache.fetch_or_add(query) { SquishSelect.new(@config, query) }
  query.kind_of?(SquishSelect) or raise ProgrammingError,
    "String or SquishSelect expected"
  query.to_sql
end

#select_all(query, limit = nil, offset = nil, params = {}, &p) ⇒ Object

get all query answers (similar to DBI#select_all)



70
71
72
73
74
75
76
77
# File 'lib/graffiti/store.rb', line 70

def select_all(query, limit=nil, offset=nil, params={}, &p)
  ds = fetch(query, params).limit(limit, offset)
  if block_given?
    ds.all(&p)
  else
    ds.all
  end
end

#select_one(query, params = {}) ⇒ Object

get one query answer (similar to DBI#select_one)



64
65
66
# File 'lib/graffiti/store.rb', line 64

def select_one(query, params={})
  fetch(query, params).first
end