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.



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

#pathObject (readonly)

Returns the value of attribute path.



659
660
661
# File 'lib/tina4/docstore.rb', line 659

def path
  @path
end

Instance Method Details

#closeObject



690
691
692
# File 'lib/tina4/docstore.rb', line 690

def close
  @conn.close
end

#connectionObject



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_namesObject



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