Class: Sqlbible

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlbible.rb,
lib/sqlbible/convert.rb

Defined Under Namespace

Classes: Verse

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_filename) ⇒ Sqlbible

Returns a new instance of Sqlbible.



11
12
13
14
15
16
17
# File 'lib/sqlbible.rb', line 11

def initialize db_filename
  @db = SQLite3::Database.new(db_filename)
  @db.enable_load_extension(true)
  pcre_extension_file = ENV['SQLITE_PCRE_EXTENSION']
  @db.load_extension(pcre_extension_file) if pcre_extension_file
  check_schema_version
end

Class Method Details

.convert(osis_fn, sqlite_fn) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sqlbible/convert.rb', line 11

def self.convert osis_fn, sqlite_fn
  last_bookid = nil
  booknumber = 0

  doc = Nokogiri.XML(File.read(osis_fn))
  db = SQLite3::Database.new(sqlite_fn)
  db.execute_batch(Sqlbible.db_schema)

  doc.css('verse').each do |v|
    osisid = v['osisID']
    bookid, chapter, verse = osisid.split('.')
    unless bookid == last_bookid
      booknumber += 1
      db.execute "insert into books (bookid, booknumber) values (?, ?)", [bookid, booknumber]
      last_bookid = bookid
    end
    xml = v.children.to_xml
    plaintext = ''
    v.children.each do |e|
      case e.name
      when 'note'
        # ignore
      when 'divineName'
        plaintext << e.text.upcase
      when 'lb'
        plaintext << "\n"
      else
        plaintext << e.text
      end
    end
    plaintext.strip!
    db.execute "insert into text (osisid, bookid, booknumber, chapter, verse, xml, plaintext) values (?, ?, ?, ?, ?, ?, ?)", [osisid, bookid, booknumber, chapter, verse, xml, plaintext]
  end
end

.db_schemaObject



57
58
59
# File 'lib/sqlbible.rb', line 57

def db_schema
  File.read(File.join(__dir__, '../schema.sql'))
end

Instance Method Details

#passage(pass) ⇒ Object



19
20
21
22
23
# File 'lib/sqlbible.rb', line 19

def passage pass
  rowid1, rowid2 = passage2rowids(pass)
  res = select_text(where: 'rowid between ? and ?', args: [rowid1, rowid2])
  parse_verse_list(res)
end

#reference(ref) ⇒ Object



25
26
27
# File 'lib/sqlbible.rb', line 25

def reference ref
  ref.select {|e| e.kind_of?(Scripref::Passage)}.map {|pass| passage(pass)}
end

#search(obj, range: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sqlbible.rb', line 29

def search obj, range: nil
  where_arr = []
  args = []
  if range
    passages = Array(range).select {|e| e.kind_of? Scripref::Passage}
    passages.size.times {where_arr << 'rowid between ? and ?'}
    where_arr == [or_join_where(where_arr)]
    args = passages.map {|p| passage2rowids(p)}.flatten
  end
  case obj
  when Regexp
    where_arr << 'plaintext regexp ?'
    args << obj.source
    res = select_text(where: and_join_where(where_arr), args: args)
  else
    fail 'unknown search object'
  end
  parse_verse_list(res)
end