Class: Nostrb::Sequel::Storage

Inherits:
Nostrb::SQLite::Storage show all
Defined in:
lib/nostrb/sequel.rb

Direct Known Subclasses

Reader, Setup, Writer

Constant Summary collapse

FILENAME =
'sequel.tmp.db'
TABLES =
[:events, :tags, :r_events, :r_tags]

Constants inherited from Nostrb::SQLite::Storage

Nostrb::SQLite::Storage::CONFIG, Nostrb::SQLite::Storage::GB, Nostrb::SQLite::Storage::KB, Nostrb::SQLite::Storage::MB, Nostrb::SQLite::Storage::PRAGMAS, Nostrb::SQLite::Storage::SQLITE_USAGE

Instance Attribute Summary

Attributes inherited from Nostrb::SQLite::Storage

#db, #filename, #pragma

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Nostrb::SQLite::Storage

#all_index_names, #all_table_names, #compile_options, #database_files, #index_names, #table_names

Constructor Details

#initialize(filename = FILENAME, set_pragmas: true) ⇒ Storage

Returns a new instance of Storage.



16
17
18
19
20
21
# File 'lib/nostrb/sequel.rb', line 16

def initialize(filename = FILENAME, set_pragmas: true)
  @filename = filename
  @db = ::Sequel.sqlite(@filename)
  @db.transaction_mode = :immediate
  self.set_pragmas if set_pragmas
end

Class Method Details

.schema_line(col, cfg) ⇒ Object



10
11
12
13
14
# File 'lib/nostrb/sequel.rb', line 10

def self.schema_line(col, cfg)
  [col.to_s.ljust(9, ' '),
   cfg.map { |(k,v)| [k, v.inspect].join(': ') }.join("\t")
  ].join("\t")
end

Instance Method Details

#pragma_scalarsObject



31
32
33
34
35
36
37
38
# File 'lib/nostrb/sequel.rb', line 31

def pragma_scalars
  pragma = SQLite::Pragma.new(@db.pool.available_connections.sample)
  SQLite::Pragma::SCALAR.map { |p|
    val, enum = pragma.get(p), SQLite::Pragma::ENUM[p]
    val = format("%i (%s)", val, enum[val]) if enum
    format("%s: %s", p, val)
  }
end

#readerObject



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

def reader
  Reader.new(@filename)
end

#reportObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/nostrb/sequel.rb', line 56

def report
  lines = []
  TABLES.each { |t|
    lines << t
    lines += schema(t)
    lines << ''
  }
  lines += self.pragma_scalars
  lines
end

#schema(table) ⇒ Object



52
53
54
# File 'lib/nostrb/sequel.rb', line 52

def schema(table)
  @db.schema(table).map { |a| Storage.schema_line(*a) }
end

#set_pragmasObject



23
24
25
26
27
28
29
# File 'lib/nostrb/sequel.rb', line 23

def set_pragmas
  @db.pool.available_connections.each { |s3db|
    pragma = SQLite::Pragma.new(s3db)
    PRAGMAS.each { |name, val| pragma.set(name, val) }
  }
  PRAGMAS.clone
end

#setupObject



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

def setup
  Setup.new(@filename)
end

#writerObject



48
49
50
# File 'lib/nostrb/sequel.rb', line 48

def writer
  Writer.new(@filename)
end