Module: ActiveRecord::ConnectionAdapters::TursoAdapter::DatabaseStatements

Included in:
ActiveRecord::ConnectionAdapters::TursoAdapter
Defined in:
lib/active_record/connection_adapters/turso_adapter/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#combine_named_bind_params(binds) ⇒ Object



59
60
61
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 59

def combine_named_bind_params(binds)
  binds
end

#default_timezoneObject



55
56
57
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 55

def default_timezone
  ActiveRecord.default_timezone
end

#disable_referential_integrityObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 107

def disable_referential_integrity
  old_foreign_keys = query_value("PRAGMA foreign_keys")
  old_defer_foreign_keys = query_value("PRAGMA defer_foreign_keys")

  begin
    execute("PRAGMA defer_foreign_keys = ON")
    execute("PRAGMA foreign_keys = OFF")
    yield
  ensure
    if old_defer_foreign_keys
      execute("PRAGMA defer_foreign_keys = #{old_defer_foreign_keys}")
    end
    execute("PRAGMA foreign_keys = #{old_foreign_keys}")
  end
end

#exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil, returning = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 27

def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil, returning = nil)
  if returning && supports_insert_returning?
    return exec_query(sql, name, binds)
  end

  materialize_transactions

  type_casted_binds = type_casted_binds(binds)
  log(sql, name, binds, type_casted_binds) do
    with_raw_connection do |conn|
      stmt = conn.prepare(sql)
      stmt.bind_positional(type_casted_binds) unless type_casted_binds.empty?
      stmt.execute
      last_id = pk ? last_inserted_id(sql) : nil
      stmt.finalize
      last_id
    end
  end
end

#exec_query(sql, name = "SQL", binds = [], prepare: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 12

def exec_query(sql, name = "SQL", binds = [], prepare: false)
  materialize_transactions

  type_casted_binds = type_casted_binds(binds)
  log(sql, name, binds, type_casted_binds) do
    with_raw_connection do |conn|
      stmt = conn.prepare(sql)
      stmt.bind_positional(type_casted_binds) unless type_casted_binds.empty?
      result = build_result(stmt)
      stmt.finalize
      result
    end
  end
end

#execute(sql, name = nil) ⇒ Object



7
8
9
10
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 7

def execute(sql, name = nil)
  materialize_transactions
  raw_execute(sql, name)
end

#last_inserted_id(sql) ⇒ Object



47
48
49
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 47

def last_inserted_id(sql)
  @raw_connection.last_insert_rowid
end

#perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch: false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 82

def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch: false)
  if batch
    raw_connection.execute_batch(sql)
    return ActiveRecord::Result.empty(affected_rows: 0)
  end

  if write_query?(sql)
    raw_connection.execute(sql, type_casted_binds)
    affected_rows = raw_connection.changes
    verified!
    notification_payload[:affected_rows] = affected_rows
    notification_payload[:row_count] = 0
    ActiveRecord::Result.empty(affected_rows: affected_rows)
  else
    result = raw_connection.query(sql, type_casted_binds)
    columns = result.column_names
    rows = result.map(&:values)
    affected_rows = raw_connection.changes
    verified!
    notification_payload[:affected_rows] = affected_rows
    notification_payload[:row_count] = rows.length
    ActiveRecord::Result.new(columns, rows, nil, affected_rows: affected_rows)
  end
end

#returning_column_values(result) ⇒ Object



51
52
53
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 51

def returning_column_values(result)
  [@raw_connection.last_insert_rowid]
end

#select_rows(arel, name = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 63

def select_rows(arel, name = nil)
  arel = arel_from_relation(arel)
  sql, binds = to_sql_and_binds(arel)
  type_casted_binds = type_casted_binds(binds)

  log(sql, name, binds, type_casted_binds) do
    with_raw_connection do |conn|
      stmt = conn.prepare(sql)
      stmt.bind_positional(type_casted_binds) unless type_casted_binds.empty?
      rows = []
      while stmt.step == 1
        rows << stmt.row.to_a
      end
      stmt.finalize
      rows
    end
  end
end