Class: Tina4::Drivers::SqliteDriver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/tina4/drivers/sqlite_driver.rb', line 6

def connection
  @connection
end

Instance Method Details

#apply_limit(sql, limit, offset = 0) ⇒ Object



46
47
48
# File 'lib/tina4/drivers/sqlite_driver.rb', line 46

def apply_limit(sql, limit, offset = 0)
  "#{sql} LIMIT #{limit} OFFSET #{offset}"
end

#begin_transactionObject



50
51
52
# File 'lib/tina4/drivers/sqlite_driver.rb', line 50

def begin_transaction
  @connection.execute("BEGIN TRANSACTION")
end

#closeObject



21
22
23
# File 'lib/tina4/drivers/sqlite_driver.rb', line 21

def close
  @connection&.close
end

#columns(table_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tina4/drivers/sqlite_driver.rb', line 67

def columns(table_name)
  rows = execute_query("PRAGMA table_info(#{table_name})")
  rows.map do |r|
    {
      name: r[:name],
      type: r[:type],
      nullable: r[:notnull] == 0,
      default: r[:dflt_value],
      primary_key: r[:pk] == 1
    }
  end
end

#commitObject



54
55
56
# File 'lib/tina4/drivers/sqlite_driver.rb', line 54

def commit
  @connection.execute("COMMIT")
end

#connect(connection_string, username: nil, password: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tina4/drivers/sqlite_driver.rb', line 8

def connect(connection_string, username: nil, password: nil)
  require "sqlite3"
  db_path = connection_string.sub(/^sqlite:\/\//, "").sub(/^sqlite:/, "")
  # Windows: sqlite:///C:/Users/app.db → /C:/Users/app.db after stripping scheme.
  # The leading / before the drive letter must be removed.
  db_path = db_path[1..] if db_path.match?(/^\/[A-Za-z]:/)

  @connection = SQLite3::Database.new(db_path)
  @connection.results_as_hash = true
  @connection.execute("PRAGMA journal_mode=WAL")
  @connection.execute("PRAGMA foreign_keys=ON")
end

#execute(sql, params = []) ⇒ Object



30
31
32
# File 'lib/tina4/drivers/sqlite_driver.rb', line 30

def execute(sql, params = [])
  @connection.execute(sql, params)
end

#execute_query(sql, params = []) ⇒ Object



25
26
27
28
# File 'lib/tina4/drivers/sqlite_driver.rb', line 25

def execute_query(sql, params = [])
  results = @connection.execute(sql, params)
  results.map { |row| symbolize_keys(row) }
end

#last_insert_idObject



34
35
36
# File 'lib/tina4/drivers/sqlite_driver.rb', line 34

def last_insert_id
  @connection.last_insert_row_id
end

#placeholderObject



38
39
40
# File 'lib/tina4/drivers/sqlite_driver.rb', line 38

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



42
43
44
# File 'lib/tina4/drivers/sqlite_driver.rb', line 42

def placeholders(count)
  (["?"] * count).join(", ")
end

#rollbackObject



58
59
60
# File 'lib/tina4/drivers/sqlite_driver.rb', line 58

def rollback
  @connection.execute("ROLLBACK")
end

#tablesObject



62
63
64
65
# File 'lib/tina4/drivers/sqlite_driver.rb', line 62

def tables
  rows = execute_query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")
  rows.map { |r| r[:name] }
end