Class: Tina4::DocStore::SqliteDatabase
- Inherits:
-
Object
- Object
- Tina4::DocStore::SqliteDatabase
- Defined in:
- lib/tina4/docstore.rb
Overview
A SQLite-backed document database (a file of collection tables).
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #close ⇒ Object
- #connection ⇒ Object
- #get_collection(name) ⇒ Object (also: #[])
-
#initialize(path = nil) ⇒ SqliteDatabase
constructor
A new instance of SqliteDatabase.
- #list_collection_names ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ SqliteDatabase
Returns a new instance of SqliteDatabase.
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
# File 'lib/tina4/docstore.rb', line 661 def initialize(path = nil) @path = path || ENV["TINA4_DOC_STORE_PATH"] || "data/tina4_docstore.db" require "sqlite3" if @path != ":memory:" dir = File.dirname(@path) require "fileutils" FileUtils.mkdir_p(dir) unless dir.empty? || dir == "." || File.directory?(dir) end @conn = SQLite3::Database.new(@path) # Register the REGEXP user function for the $regex operator. @conn.create_function("regexp", 2) do |func, pattern, value| func.result = DocStore.regexp_match(pattern, value) end @collections = {} end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
659 660 661 |
# File 'lib/tina4/docstore.rb', line 659 def path @path end |
Instance Method Details
#close ⇒ Object
690 691 692 |
# File 'lib/tina4/docstore.rb', line 690 def close @conn.close end |
#connection ⇒ Object
677 |
# File 'lib/tina4/docstore.rb', line 677 def connection = @conn |
#get_collection(name) ⇒ Object Also known as: []
679 680 681 |
# File 'lib/tina4/docstore.rb', line 679 def get_collection(name) @collections[name.to_s] ||= SqliteCollection.new(@conn, name) end |
#list_collection_names ⇒ Object
684 685 686 687 688 |
# File 'lib/tina4/docstore.rb', line 684 def list_collection_names @conn.execute( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'" ).map { |row| row.is_a?(Hash) ? (row["name"] || row[:name] || row.values.first) : row.first } end |