Class: Tina4::DocStore::SqliteDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/docstore.rb

Overview

A SQLite-backed document database (a file of collection tables).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ SqliteDatabase

Returns a new instance of SqliteDatabase.



814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'lib/tina4/docstore.rb', line 814

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

#pathObject (readonly)

Returns the value of attribute path.



812
813
814
# File 'lib/tina4/docstore.rb', line 812

def path
  @path
end

Instance Method Details

#closeObject



843
844
845
# File 'lib/tina4/docstore.rb', line 843

def close
  @conn.close
end

#connectionObject



830
# File 'lib/tina4/docstore.rb', line 830

def connection = @conn

#get_collection(name) ⇒ Object Also known as: []



832
833
834
# File 'lib/tina4/docstore.rb', line 832

def get_collection(name)
  @collections[name.to_s] ||= SqliteCollection.new(@conn, name)
end

#list_collection_namesObject



837
838
839
840
841
# File 'lib/tina4/docstore.rb', line 837

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