Class: Turso::Database
- Inherits:
-
Object
- Object
- Turso::Database
- Defined in:
- lib/turso/database.rb
Instance Method Summary collapse
- #busy_timeout ⇒ Object
- #busy_timeout=(ms) ⇒ Object
- #changes ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #execute(sql, *bind_args) ⇒ Object
- #execute_batch(sql) ⇒ Object
- #get_first_row(sql, *bind_args) ⇒ Object
- #get_first_value(sql, *bind_args) ⇒ Object
-
#initialize(path = ":memory:", **options) ⇒ Database
constructor
A new instance of Database.
- #interrupt ⇒ Object
- #last_insert_rowid ⇒ Object
- #prepare(sql) ⇒ Object
- #query(sql, *bind_args) ⇒ Object
- #total_changes ⇒ Object
- #transaction(mode = :deferred) ⇒ Object
Constructor Details
#initialize(path = ":memory:", **options) ⇒ Database
Returns a new instance of Database.
5 6 7 8 9 |
# File 'lib/turso/database.rb', line 5 def initialize(path = ":memory:", **) [:experimental_features] = normalize_experimental_features([:experimental_features]) @native = NativeDatabase.new(path, ) @closed = false end |
Instance Method Details
#busy_timeout ⇒ Object
89 90 91 |
# File 'lib/turso/database.rb', line 89 def busy_timeout @native.connection.busy_timeout end |
#busy_timeout=(ms) ⇒ Object
85 86 87 |
# File 'lib/turso/database.rb', line 85 def busy_timeout=(ms) @native.connection.busy_timeout = ms end |
#changes ⇒ Object
97 98 99 |
# File 'lib/turso/database.rb', line 97 def changes @native.connection.changes end |
#close ⇒ Object
25 26 27 28 29 |
# File 'lib/turso/database.rb', line 25 def close return if closed? @native.close @closed = true end |
#closed? ⇒ Boolean
21 22 23 |
# File 'lib/turso/database.rb', line 21 def closed? @closed || !@native.open? end |
#execute(sql, *bind_args) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/turso/database.rb', line 36 def execute(sql, *bind_args) stmt = prepare(sql) begin stmt.bind(*bind_args) unless bind_args.empty? stmt.run ensure stmt.close end end |
#execute_batch(sql) ⇒ Object
46 47 48 49 |
# File 'lib/turso/database.rb', line 46 def execute_batch(sql) raise Turso::Exception, "database is closed" if closed? @native.connection.execute_batch(sql) end |
#get_first_row(sql, *bind_args) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/turso/database.rb', line 57 def get_first_row(sql, *bind_args) stmt = prepare(sql) begin stmt.bind(*bind_args) unless bind_args.empty? stmt.get ensure stmt.close end end |
#get_first_value(sql, *bind_args) ⇒ Object
67 68 69 70 |
# File 'lib/turso/database.rb', line 67 def get_first_value(sql, *bind_args) row = get_first_row(sql, *bind_args) row&.first end |
#interrupt ⇒ Object
105 106 107 |
# File 'lib/turso/database.rb', line 105 def interrupt @native.connection.interrupt end |
#last_insert_rowid ⇒ Object
101 102 103 |
# File 'lib/turso/database.rb', line 101 def last_insert_rowid @native.last_insert_rowid end |
#prepare(sql) ⇒ Object
31 32 33 34 |
# File 'lib/turso/database.rb', line 31 def prepare(sql) raise Turso::Exception, "database is closed" if closed? Statement.new(@native.connection.prepare(sql), @native.connection) end |
#query(sql, *bind_args) ⇒ Object
51 52 53 54 55 |
# File 'lib/turso/database.rb', line 51 def query(sql, *bind_args) stmt = prepare(sql) stmt.bind(*bind_args) unless bind_args.empty? ResultSet.new(stmt) end |
#total_changes ⇒ Object
93 94 95 |
# File 'lib/turso/database.rb', line 93 def total_changes @native.total_changes end |
#transaction(mode = :deferred) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/turso/database.rb', line 72 def transaction(mode = :deferred) raise Turso::Exception, "database is closed" if closed? execute("BEGIN #{mode.to_s.upcase}") begin result = yield self execute("COMMIT") result rescue ::Exception execute("ROLLBACK") raise end end |