Class: Sqlbible
- Inherits:
-
Object
- Object
- Sqlbible
- Defined in:
- lib/sqlbible.rb,
lib/sqlbible/convert.rb,
lib/sqlbible/version.rb
Defined Under Namespace
Classes: Verse
Constant Summary collapse
- VERSION =
'1.7.1'
Class Attribute Summary collapse
-
.bible ⇒ Object
Returns the value of attribute bible.
-
.bibles_dir ⇒ Object
readonly
Returns the value of attribute bibles_dir.
-
.color_emphasize ⇒ Object
Returns the value of attribute color_emphasize.
-
.color_reference ⇒ Object
Returns the value of attribute color_reference.
-
.colorize ⇒ Object
Returns the value of attribute colorize.
-
.lang ⇒ Object
Returns the value of attribute lang.
Instance Attribute Summary collapse
-
#db_filename ⇒ Object
readonly
Returns the value of attribute db_filename.
Class Method Summary collapse
-
.bible_names ⇒ Object
Get a list of the names of the bibles in bibles_dir.
-
.convert(osis_fn, sqlite_fn) ⇒ Object
Convert a bible file in OSIS XML format to another file in SQLite format for Sqlbible.
-
.db_schema ⇒ Object
Get the database schema as string.
- .resolve_db_filename(db_name, check_existence: false) ⇒ Object
-
.schema_major_minor ⇒ Object
Get an array of the major and minor version of the database schema of the actual version of the lib.
-
.schema_version ⇒ Object
Get the version of the database schema of the actual version of the lib.
-
.select_schema_major_minor(db) ⇒ Object
Get an array of the major and minor version of an Sqlbible database.
-
.select_schema_version(db) ⇒ Object
Get the version of an Sqlbible database.
Instance Method Summary collapse
-
#initialize(db_name = Sqlbible.bible) ⇒ Sqlbible
constructor
A new instance of Sqlbible.
-
#passage(pass) ⇒ Object
Get an array of Verse instances for a Scripref::Passage.
-
#reference(ref) ⇒ Object
Get an array of arrays of Verse instances for a Scripref reference (array of Scripref::Passage instances).
-
#schema_version ⇒ Object
Get the schema version of the SQLite database of the instance.
-
#search(*searches, range: nil) ⇒ Object
Get an array of Verse instances for a search with one ore more regular expressions (optional a search range of a Scripref reference is possible).
Constructor Details
#initialize(db_name = Sqlbible.bible) ⇒ Sqlbible
Returns a new instance of Sqlbible.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sqlbible.rb', line 14 def initialize db_name=Sqlbible.bible @db_filename = Sqlbible.resolve_db_filename(db_name, check_existence: true) @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 begin @db.execute("select regexp('.', '.')") rescue @db.create_function 'regexp', 2 do |f, re, s| if s =~ /#{re}/ f.result = 1 else f.result = nil end end end check_schema_version end |
Class Attribute Details
.bible ⇒ Object
Returns the value of attribute bible.
92 93 94 |
# File 'lib/sqlbible.rb', line 92 def bible @bible end |
.bibles_dir ⇒ Object (readonly)
Returns the value of attribute bibles_dir.
91 92 93 |
# File 'lib/sqlbible.rb', line 91 def bibles_dir @bibles_dir end |
.color_emphasize ⇒ Object
Returns the value of attribute color_emphasize.
92 93 94 |
# File 'lib/sqlbible.rb', line 92 def color_emphasize @color_emphasize end |
.color_reference ⇒ Object
Returns the value of attribute color_reference.
92 93 94 |
# File 'lib/sqlbible.rb', line 92 def color_reference @color_reference end |
.colorize ⇒ Object
Returns the value of attribute colorize.
92 93 94 |
# File 'lib/sqlbible.rb', line 92 def colorize @colorize end |
.lang ⇒ Object
Returns the value of attribute lang.
92 93 94 |
# File 'lib/sqlbible.rb', line 92 def lang @lang end |
Instance Attribute Details
#db_filename ⇒ Object (readonly)
Returns the value of attribute db_filename.
12 13 14 |
# File 'lib/sqlbible.rb', line 12 def db_filename @db_filename end |
Class Method Details
.bible_names ⇒ Object
Get a list of the names of the bibles in bibles_dir
100 101 102 |
# File 'lib/sqlbible.rb', line 100 def bible_names Dir[File.join(bibles_dir, '*.sqlbible')].map {|fn| File.basename(fn, '.sqlbible')}.sort end |
.convert(osis_fn, sqlite_fn) ⇒ Object
Convert a bible file in OSIS XML format to another file in SQLite format for Sqlbible
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 45 46 47 |
# File 'lib/sqlbible/convert.rb', line 13 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'] next unless 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_schema ⇒ Object
Get the database schema as string
95 96 97 |
# File 'lib/sqlbible.rb', line 95 def db_schema File.read(File.join(__dir__, '../schema.sql')) end |
.resolve_db_filename(db_name, check_existence: false) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/sqlbible.rb', line 127 def resolve_db_filename db_name, check_existence: false db_name = db_name.to_s if File.basename(db_name, '.sqlbible') == db_name fn = File.join(bibles_dir, format('%s.sqlbible', db_name)) if check_existence && !File.exist?(fn) msg = format('database %s not found, file %s does not exist', db_name, fn) sc = DidYouMean::SpellChecker.new(dictionary: bible_names) res = sc.correct(db_name) unless res.empty? msg = format("%s\ndid you mean %s", msg, res.join(', ')) end fail ArgumentError, msg end else fn = db_name if check_existence && !File.exist?(fn) fail ArgumentError, format('file %s does not exist', fn) end end fn end |
.schema_major_minor ⇒ Object
Get an array of the major and minor version of the database schema of the actual version of the lib
106 107 108 109 110 |
# File 'lib/sqlbible.rb', line 106 def schema_major_minor lib_db = SQLite3::Database.new(':memory:') lib_db.execute_batch(Sqlbible.db_schema) select_schema_major_minor(lib_db) end |
.schema_version ⇒ Object
Get the version of the database schema of the actual version of the lib
113 114 115 |
# File 'lib/sqlbible.rb', line 113 def schema_version schema_major_minor.join('.') end |
.select_schema_major_minor(db) ⇒ Object
Get an array of the major and minor version of an Sqlbible database
118 119 120 |
# File 'lib/sqlbible.rb', line 118 def select_schema_major_minor db db.execute("select major, minor from schema_version").first end |
.select_schema_version(db) ⇒ Object
Get the version of an Sqlbible database
123 124 125 |
# File 'lib/sqlbible.rb', line 123 def select_schema_version db select_schema_major_minor(db).join('.') end |
Instance Method Details
#passage(pass) ⇒ Object
Get an array of Verse instances for a Scripref::Passage
35 36 37 38 39 |
# File 'lib/sqlbible.rb', line 35 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
Get an array of arrays of Verse instances for a Scripref reference (array of Scripref::Passage instances)
43 44 45 |
# File 'lib/sqlbible.rb', line 43 def reference ref ref.select {|e| e.kind_of?(Scripref::Passage)}.map {|pass| passage(pass)} end |
#schema_version ⇒ Object
Get the schema version of the SQLite database of the instance
73 74 75 |
# File 'lib/sqlbible.rb', line 73 def schema_version Sqlbible.select_schema_version(@db) end |
#search(*searches, range: nil) ⇒ Object
Get an array of Verse instances for a search with one ore more regular expressions (optional a search range of a Scripref reference is possible)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sqlbible.rb', line 49 def search *searches, 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 searches.flatten.each do |o| case o when Regexp where_arr << 'plaintext regexp ?' args << o.to_s else fail format('search objects of class %s are not (yet) supported', o.class) end end where = and_join_where(where_arr) res = select_text(where: where, args: args) parse_verse_list(res) end |