Class: ActiveRecord::ConnectionAdapters::WasmSqlite3Adapter::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(js_interface, sql) ⇒ Statement

Returns a new instance of Statement.



69
70
71
72
73
74
75
76
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 69

def initialize(js_interface, sql)
  @js       = js_interface
  @base_sql = sql.to_s
  @sql      = @base_sql
  @executed = false
  @columns  = []
  @rows     = []
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



67
68
69
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 67

def columns
  @columns
end

#rowsObject (readonly)

Returns the value of attribute rows.



67
68
69
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 67

def rows
  @rows
end

Instance Method Details

#bind_params(*params) ⇒ Object

Substitute bound parameters into the SQL (? placeholders).



79
80
81
82
83
84
85
86
87
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 79

def bind_params(*params)
  params = params.flatten(1)
  return if params.empty?
  i = -1
  @sql = @base_sql.gsub('?') do
    i += 1
    quote_value(params[i])
  end
end

#closeObject



116
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 116

def close; end

#column_countObject



113
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 113

def column_count = (execute; @columns.size)

#executeObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 94

def execute
  return if @executed
  @executed = true

  res      = @js.call(:exec, @sql)
  @columns = res[:cols].to_a.map(&:to_s)
  @rows    = res[:rows].to_a.map do |row|
    row.to_a.map do |val|
      str = val.to_s
      case val.typeof
      when 'string'  then str
      when 'boolean' then str == 'true'
      when 'number'  then str.include?('.') ? val.to_f : val.to_i
      else str == 'null' ? nil : str
      end
    end
  end
end

#reset!Object



117
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 117

def reset!; @executed = false; @sql = @base_sql; end

#stepObject



89
90
91
92
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 89

def step
  execute
  nil
end

#to_aObject



115
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 115

def to_a         = (execute; @rows)

#typesObject

nil → default type



114
# File 'lib/generators/wasm_rails/install/templates/wasm_sqlite3_adapter.rb', line 114

def types        = (execute; Array.new(@columns.size)) # nil → default type