Class: Turso::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/turso/database.rb

Instance Method Summary collapse

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:", **options)
  options[:experimental_features] = normalize_experimental_features(options[:experimental_features])
  @native = NativeDatabase.new(path, options)
  @closed = false
end

Instance Method Details

#busy_timeoutObject



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

#changesObject



97
98
99
# File 'lib/turso/database.rb', line 97

def changes
  @native.connection.changes
end

#closeObject



25
26
27
28
29
# File 'lib/turso/database.rb', line 25

def close
  return if closed?
  @native.close
  @closed = true
end

#closed?Boolean

Returns:

  • (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

Raises:

  • (Turso::Exception)


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

#interruptObject



105
106
107
# File 'lib/turso/database.rb', line 105

def interrupt
  @native.connection.interrupt
end

#last_insert_rowidObject



101
102
103
# File 'lib/turso/database.rb', line 101

def last_insert_rowid
  @native.last_insert_rowid
end

#prepare(sql) ⇒ Object

Raises:

  • (Turso::Exception)


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_changesObject



93
94
95
# File 'lib/turso/database.rb', line 93

def total_changes
  @native.total_changes
end

#transaction(mode = :deferred) ⇒ Object

Raises:

  • (Turso::Exception)


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