Class: Taggata::DbAdapters::Sequel

Inherits:
Abstract
  • Object
show all
Defined in:
lib/taggata/db_adapters/sequel.rb

Instance Attribute Summary

Attributes inherited from Abstract

#db

Instance Method Summary collapse

Instance Method Details

#bulk_insert(klass, values) ⇒ Object



8
9
10
# File 'lib/taggata/db_adapters/sequel.rb', line 8

def bulk_insert(klass, values)
  db[klass.table].multi_insert(values)
end

#destroy(klass, options) ⇒ Object



32
33
34
# File 'lib/taggata/db_adapters/sequel.rb', line 32

def destroy(klass, options)
  db[klass.table].where(options).delete
end

#find(klass, options) ⇒ Object



36
37
38
# File 'lib/taggata/db_adapters/sequel.rb', line 36

def find(klass, options)
  db[klass.table].where(options).all
end

#find_one(klass, options) ⇒ Object



40
41
42
# File 'lib/taggata/db_adapters/sequel.rb', line 40

def find_one(klass, options)
  db[klass.table].where(options).limit(1).first
end

#find_tags_without_filesObject



25
26
27
28
29
30
# File 'lib/taggata/db_adapters/sequel.rb', line 25

def find_tags_without_files
  file_tags = db[Persistent::FileTag.table].select(:tag_id).map { |hash| hash[:tag_id] }
  tag_ids = db[Persistent::Tag.table].select(:id).map { |hash| hash[:id] }
  untagged_ids = tag_ids.reject { |id| file_tags.include? id }
  find(Persistent::Tag, :id => untagged_ids)
end

#find_untagged_filesObject



18
19
20
21
22
23
# File 'lib/taggata/db_adapters/sequel.rb', line 18

def find_untagged_files
  file_tags = db[Persistent::FileTag.table].select(:file_id).map { |hash| hash[:file_id] }
  file_ids = db[Persistent::File.table].select(:id).map { |hash| hash[:id] }
  untagged_ids = file_ids.reject { |id| file_tags.include? id }
  find(Taggata::Persistent::File, :id => untagged_ids)
end

#initialize_db(db_path) ⇒ Object



57
58
59
# File 'lib/taggata/db_adapters/sequel.rb', line 57

def initialize_db(db_path)
  @db = ::Sequel.connect db_path
end

#migrate_dbObject



61
62
63
# File 'lib/taggata/db_adapters/sequel.rb', line 61

def migrate_db
  ::Sequel::Migrator.run(db, self.class.migrations_path, table: 'taggata_schema_info')
end

#save(table, hash) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/taggata/db_adapters/sequel.rb', line 48

def save(table, hash)
  existing_record = db[table].where(primary_key(table, hash)).limit(1)
  if existing_record.count == 0
    db[table].insert(hash)
  else
    existing_record.update(hash)
  end
end

#search(klass, options) ⇒ Object



44
45
46
# File 'lib/taggata/db_adapters/sequel.rb', line 44

def search(klass, options)
  db[klass.table].where(options)
end

#transaction(block) ⇒ Object



12
13
14
15
16
# File 'lib/taggata/db_adapters/sequel.rb', line 12

def transaction(block)
  db.transaction do
    block.call
  end
end