Class: Tina4::Adapters::Sqlite3Adapter
- Inherits:
-
Object
- Object
- Tina4::Adapters::Sqlite3Adapter
- Defined in:
- lib/tina4/database/sqlite3_adapter.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#db_path ⇒ Object
readonly
Returns the value of attribute db_path.
Instance Method Summary collapse
- #apply_limit(sql, limit, offset = 0) ⇒ Object
-
#begin_transaction ⇒ Object
Transaction support.
- #close ⇒ Object
-
#columns(table) ⇒ Object
Get column metadata for a table.
- #commit ⇒ Object
- #connect(connection_string) ⇒ Object
- #connected? ⇒ Boolean
-
#exec(sql, params = []) ⇒ Object
Execute DDL or DML without returning rows.
-
#fetch(sql, limit = 100, offset = nil) ⇒ Object
Paginated fetch.
-
#initialize(connection_string = nil) ⇒ Sqlite3Adapter
constructor
A new instance of Sqlite3Adapter.
-
#last_insert_id ⇒ Object
Get last inserted row id.
-
#placeholder ⇒ Object
Convenience: placeholder for parameterized queries.
- #placeholders(count) ⇒ Object
-
#query(sql, params = []) ⇒ Object
Execute a query and return rows as array of symbol-keyed hashes.
- #rollback ⇒ Object
-
#table_exists?(table) ⇒ Boolean
Check if a table exists.
-
#tables ⇒ Object
List all user tables.
- #transaction ⇒ Object
Constructor Details
#initialize(connection_string = nil) ⇒ Sqlite3Adapter
Returns a new instance of Sqlite3Adapter.
8 9 10 11 12 13 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 8 def initialize(connection_string = nil) @connection = nil @db_path = nil @in_transaction = false connect(connection_string) if connection_string end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 6 def connection @connection end |
#db_path ⇒ Object (readonly)
Returns the value of attribute db_path.
6 7 8 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 6 def db_path @db_path end |
Instance Method Details
#apply_limit(sql, limit, offset = 0) ⇒ Object
126 127 128 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 126 def apply_limit(sql, limit, offset = 0) "#{sql} LIMIT #{limit} OFFSET #{offset}" end |
#begin_transaction ⇒ Object
Transaction support
90 91 92 93 94 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 90 def begin_transaction return if @in_transaction @connection.execute("BEGIN TRANSACTION") @in_transaction = true end |
#close ⇒ Object
25 26 27 28 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 25 def close @connection.close if @connection @connection = nil end |
#columns(table) ⇒ Object
Get column metadata for a table
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 66 def columns(table) query("PRAGMA table_info(#{table})").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
96 97 98 99 100 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 96 def commit return unless @in_transaction @connection.execute("COMMIT") @in_transaction = false end |
#connect(connection_string) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 15 def connect(connection_string) require "sqlite3" @db_path = connection_string.to_s.sub(/^sqlite3?:\/\//, "").sub(/^sqlite3?:/, "") @connection = SQLite3::Database.new(@db_path) @connection.results_as_hash = true @connection.execute("PRAGMA journal_mode=WAL") @connection.execute("PRAGMA foreign_keys=ON") self end |
#connected? ⇒ Boolean
30 31 32 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 30 def connected? !@connection.nil? && !@connection.closed? end |
#exec(sql, params = []) ⇒ Object
Execute DDL or DML without returning rows
51 52 53 54 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 51 def exec(sql, params = []) @connection.execute(sql, params) { affected_rows: @connection.changes } end |
#fetch(sql, limit = 100, offset = nil) ⇒ Object
Paginated fetch
41 42 43 44 45 46 47 48 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 41 def fetch(sql, limit = 100, offset = nil) effective_sql = sql if limit effective_sql = "#{sql} LIMIT #{limit}" effective_sql += " OFFSET #{offset}" if offset && offset > 0 end query(effective_sql) end |
#last_insert_id ⇒ Object
Get last inserted row id
85 86 87 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 85 def last_insert_id @connection.last_insert_row_id end |
#placeholder ⇒ Object
Convenience: placeholder for parameterized queries
118 119 120 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 118 def placeholder "?" end |
#placeholders(count) ⇒ Object
122 123 124 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 122 def placeholders(count) (["?"] * count).join(", ") end |
#query(sql, params = []) ⇒ Object
Execute a query and return rows as array of symbol-keyed hashes
35 36 37 38 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 35 def query(sql, params = []) results = @connection.execute(sql, params) results.map { |row| symbolize_keys(row) } end |
#rollback ⇒ Object
102 103 104 105 106 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 102 def rollback return unless @in_transaction @connection.execute("ROLLBACK") @in_transaction = false end |
#table_exists?(table) ⇒ Boolean
Check if a table exists
57 58 59 60 61 62 63 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 57 def table_exists?(table) rows = query( "SELECT name FROM sqlite_master WHERE type='table' AND name = ?", [table.to_s] ) !rows.empty? end |
#tables ⇒ Object
List all user tables
79 80 81 82 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 79 def tables rows = query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'") rows.map { |r| r[:name] } end |
#transaction ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/tina4/database/sqlite3_adapter.rb', line 108 def transaction begin_transaction yield self commit rescue => e rollback raise e end |