Class: Schron::Datastore::Sequel

Inherits:
Object
  • Object
show all
Includes:
Interface
Defined in:
lib/schron/datastore/sequel.rb

Constant Summary collapse

MAX_LIMIT =
2**61

Instance Method Summary collapse

Methods included from Interface

#get

Constructor Details

#initialize(db) ⇒ Sequel

Returns a new instance of Sequel.



15
16
17
# File 'lib/schron/datastore/sequel.rb', line 15

def initialize(db)
  @db = db
end

Instance Method Details

#exec_count(query) ⇒ Object



23
24
25
# File 'lib/schron/datastore/sequel.rb', line 23

def exec_count(query)
  dataset_for_query(query).count
end

#exec_query(query) ⇒ Object



19
20
21
# File 'lib/schron/datastore/sequel.rb', line 19

def exec_query(query)
  dataset_for_query(query).all
end

#insert(kind, hash) ⇒ Object



32
33
34
35
36
# File 'lib/schron/datastore/sequel.rb', line 32

def insert(kind, hash)
  hash[:id] = Schron::Id.generate unless hash[:id]
  @db[kind].insert(serialize(kind, hash))
  hash
end

#multi_get(kind, ids) ⇒ Object



27
28
29
30
# File 'lib/schron/datastore/sequel.rb', line 27

def multi_get(kind, ids)
  results = unserialize_all(kind, @db[kind].where(id: ids).all)
  Schron::Util.sorted_by_id_list(results, ids)
end

#multi_insert(kind, hashes) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/schron/datastore/sequel.rb', line 38

def multi_insert(kind, hashes)
  hashes.each do |h|
    h[:id] = Schron::Id.generate unless h[:id]
  end
  @db[kind].multi_insert(serialize_all(kind, hashes))
  hashes
end

#multi_remove(kind, ids) ⇒ Object



62
63
64
65
# File 'lib/schron/datastore/sequel.rb', line 62

def multi_remove(kind, ids)
  @db[kind].where(id: ids).delete
  nil
end

#multi_update(kind, hashes) ⇒ Object



52
53
54
55
# File 'lib/schron/datastore/sequel.rb', line 52

def multi_update(kind, hashes)
  raise "records must all have ids to multi update" unless hashes.all?{ |h| h[:id] }
  hashes.map { |h| update(kind, h) }
end

#remove(kind, id) ⇒ Object



57
58
59
60
# File 'lib/schron/datastore/sequel.rb', line 57

def remove(kind, id)
  @db[kind].where(id: id).delete
  nil
end

#reset!Object



67
68
69
70
71
72
73
# File 'lib/schron/datastore/sequel.rb', line 67

def reset!
  protect_reset do
    @db.tables.each do |table|
      @db.drop_table(table)
    end
  end
end

#update(kind, hash) ⇒ Object



46
47
48
49
50
# File 'lib/schron/datastore/sequel.rb', line 46

def update(kind, hash)
  raise "must provide an id" unless hash.key?(:id)
  @db[kind].where(id: hash[:id]).update(serialize(kind, hash))
  hash
end