Class: Tina4::Drivers::SqliteDriver
- Inherits:
-
Object
- Object
- Tina4::Drivers::SqliteDriver
- Defined in:
- lib/tina4/drivers/sqlite_driver.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #apply_limit(sql, limit, offset = 0) ⇒ Object
- #begin_transaction ⇒ Object
- #close ⇒ Object
- #columns(table_name) ⇒ Object
- #commit ⇒ Object
- #connect(connection_string, username: nil, password: nil) ⇒ Object
- #execute(sql, params = []) ⇒ Object
- #execute_query(sql, params = []) ⇒ Object
- #last_insert_id ⇒ Object
- #placeholder ⇒ Object
- #placeholders(count) ⇒ Object
- #rollback ⇒ Object
- #tables ⇒ Object
Instance Attribute Details
#connection ⇒ Object (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_transaction ⇒ Object
50 51 52 |
# File 'lib/tina4/drivers/sqlite_driver.rb', line 50 def begin_transaction @connection.execute("BEGIN TRANSACTION") end |
#close ⇒ Object
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 |
#commit ⇒ Object
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_id ⇒ Object
34 35 36 |
# File 'lib/tina4/drivers/sqlite_driver.rb', line 34 def last_insert_id @connection.last_insert_row_id end |
#placeholder ⇒ Object
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 |
#rollback ⇒ Object
58 59 60 |
# File 'lib/tina4/drivers/sqlite_driver.rb', line 58 def rollback @connection.execute("ROLLBACK") end |
#tables ⇒ Object
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 |