Class: Turso::Statement

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

Instance Method Summary collapse

Constructor Details

#initialize(native_statement, native_database) ⇒ Statement

Returns a new instance of Statement.



5
6
7
8
9
# File 'lib/turso/statement.rb', line 5

def initialize(native_statement, native_database)
  @native = native_statement
  @database = native_database
  @owner = Thread.current
end

Instance Method Details

#all(*bind_args) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/turso/statement.rb', line 40

def all(*bind_args)
  check_owner!
  bind(*bind_args) unless bind_args.empty?
  rows = []
  step_loop(consume_rows: true) { |r| rows << r; true }
  rows
end

#bind(*args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/turso/statement.rb', line 11

def bind(*args)
  check_owner!
  return self if args.empty?

  if args.size == 1 && args.first.is_a?(Hash)
    bind_hash(args.first)
  else
    args.each_with_index do |value, index|
      bind_value(index + 1, value)
    end
  end
  self
end

#closeObject



76
77
78
# File 'lib/turso/statement.rb', line 76

def close
  @native.finalize
end

#columnsObject



55
56
57
58
59
60
# File 'lib/turso/statement.rb', line 55

def columns
  check_owner!
  (1..@native.column_count).map do |i|
    { name: @native.column_name(i - 1), type: @native.column_decltype(i - 1) }
  end
end

#each(*bind_args) ⇒ Object



48
49
50
51
52
53
# File 'lib/turso/statement.rb', line 48

def each(*bind_args)
  return to_enum(:each, *bind_args) unless block_given?
  check_owner!
  bind(*bind_args) unless bind_args.empty?
  step_loop(consume_rows: true) { |r| yield r; true }
end

#get(*bind_args) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/turso/statement.rb', line 32

def get(*bind_args)
  check_owner!
  bind(*bind_args) unless bind_args.empty?
  row = nil
  step_loop(consume_rows: true) { |r| row ||= r; false }
  row
end

#reader?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/turso/statement.rb', line 62

def reader?
  @native.column_count > 0
end

#resetObject



71
72
73
74
# File 'lib/turso/statement.rb', line 71

def reset
  check_owner!
  @native.reset
end

#run(*bind_args) ⇒ Object



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

def run(*bind_args)
  check_owner!
  bind(*bind_args) unless bind_args.empty?
  step_loop(consume_rows: false)
  { changes: @database.changes, last_insert_rowid: @database.last_insert_rowid }
end

#stepObject



66
67
68
69
# File 'lib/turso/statement.rb', line 66

def step
  check_owner!
  @native.step
end