Class: MiGA::SQLite
Overview
SQLite3 wrapper for MiGA.
Constant Summary
Constants included from MiGA
CITATION, VERSION, VERSION_DATE, VERSION_NAME
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Options hash.
-
#path ⇒ Object
readonly
Database absolute path.
Class Method Summary collapse
-
.default_opts(opts = {}) ⇒ Object
Default parsing options.
Instance Method Summary collapse
-
#initialize(path, opts = {}) ⇒ SQLite
constructor
Create MiGA::SQLite with database in
path
(without opening a connection) and optionsopts
(see.default_opts
). -
#run(*cmd) ⇒ Object
Executes
cmd
and returns the result. -
#run_block(&blk) ⇒ Object
Executes
blk
that accepts a single parameter for the database connection.
Methods inherited from MiGA
CITATION, CITATION_ARRAY, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, #advance, debug?, debug_trace?, initialized?, #like_io?, #num_suffix, rc_path, #result_files_exist?, #say
Methods included from Common::Path
Methods included from Common::Format
#clean_fasta_file, #seqs_length, #tabulate
Methods included from Common::Net
#download_file_ftp, #known_hosts, #remote_connection
Methods included from Common::SystemCall
Constructor Details
#initialize(path, opts = {}) ⇒ SQLite
Create MiGA::SQLite with database in path
(without opening a connection) and options opts
(see .default_opts
)
31 32 33 34 35 |
# File 'lib/miga/sqlite.rb', line 31 def initialize(path, opts = {}) @opts = MiGA::SQLite.default_opts(opts) @path = File.absolute_path(path) MiGA::MiGA.DEBUG("Accessing database: #{path}") end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Options hash
22 23 24 |
# File 'lib/miga/sqlite.rb', line 22 def opts @opts end |
#path ⇒ Object (readonly)
Database absolute path
26 27 28 |
# File 'lib/miga/sqlite.rb', line 26 def path @path end |
Class Method Details
.default_opts(opts = {}) ⇒ Object
Default parsing options. Supported opts
keys:
-
:busy_attempts
: Number of times to retry when database is busy (default: 3)
14 15 16 17 |
# File 'lib/miga/sqlite.rb', line 14 def default_opts(opts = {}) opts[:busy_attempts] ||= 3 opts end |
Instance Method Details
#run(*cmd) ⇒ Object
Executes cmd
and returns the result. Alternatively, if a block is passed, the commands are ignored and the block is executed with a single parameter of the database connection
41 42 43 44 45 46 47 48 49 |
# File 'lib/miga/sqlite.rb', line 41 def run(*cmd) if block_given? run_block { |conn| yield(conn) } else y = nil run_block { |conn| y = conn.execute(*cmd) } y end end |
#run_block(&blk) ⇒ Object
Executes blk
that accepts a single parameter for the database connection
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/miga/sqlite.rb', line 54 def run_block(&blk) busy_attempts ||= 0 io_attempts ||= 0 SQLite3::Database.new(path) { |conn| blk[conn] } rescue SQLite3::BusyException => e busy_attempts += 1 raise "Database busy #{path}: #{e.}" if busy_attempts >= 3 sleep(1) retry rescue SQLite3::IOException => e io_attempts += 1 raise "Database I/O error #{path}: #{e.}" if io_attempts >= 3 sleep(1) retry end |