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



24
25
26
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 24

def combine_named_bind_params(binds)
  binds
end

#default_timezoneObject



20
21
22
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 20

def default_timezone
  ActiveRecord.default_timezone
end

#disable_referential_integrityObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 77

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") unless old_defer_foreign_keys.nil?
    execute("PRAGMA foreign_keys = OFF")
    yield
  ensure
    unless old_defer_foreign_keys.nil?
      execute("PRAGMA defer_foreign_keys = #{old_defer_foreign_keys}")
    end
    execute("PRAGMA foreign_keys = #{old_foreign_keys}")
  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)&.to_a
end

#last_inserted_id(sql) ⇒ Object



12
13
14
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 12

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 38

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

  stmt = if prepare
    @statements[sql] ||= raw_connection.prepare(sql)
  else
    raw_connection.prepare(sql)
  end

  stmt.reset if prepare
  stmt.bind(*type_casted_binds) unless type_casted_binds.empty?

  begin
    if write_query?(sql) && !sql.match?(/\bRETURNING\b/i)
      result = stmt.run
      affected_rows = result[:changes]
      verified!
      notification_payload[:affected_rows] = affected_rows
      notification_payload[:row_count] = 0
      ActiveRecord::Result.empty(affected_rows: affected_rows)
    else
      columns_info = stmt.columns
      columns = columns_info.map { |c| c[:name] }
      rows = stmt.all.map(&:to_a)
      affected_rows = raw_connection.changes
      verified!
      notification_payload[:affected_rows] = affected_rows
      notification_payload[:row_count] = rows.length
      type_map = build_type_map(stmt)
      ActiveRecord::Result.new(columns, rows, type_map, affected_rows: affected_rows)
    end
  ensure
    stmt.close unless prepare
  end
end

#returning_column_values(result) ⇒ Object



16
17
18
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 16

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

#select_rows(arel, name = nil, binds = [], _options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 28

def select_rows(arel, name = nil, binds = [], _options = {})
  arel = arel_from_relation(arel)
  sql, binds = to_sql_and_binds(arel, binds)
  type_casted_binds = type_casted_binds(binds)

  log(sql, name, binds, type_casted_binds) do
    exec_query(sql, name, binds, prepare: false).rows
  end
end