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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 87

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

#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



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



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
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_record/connection_adapters/turso_adapter/database_statements.rb', line 47

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_positional(type_casted_binds) unless type_casted_binds.empty?

  begin
    if write_query?(sql)
      affected_rows = stmt.execute
      verified!
      notification_payload[:affected_rows] = affected_rows
      notification_payload[:row_count] = 0
      ActiveRecord::Result.empty(affected_rows: affected_rows)
    else
      columns = (0...stmt.column_count).map { |i| stmt.column_name(i) }
      rows = []
      while stmt.step == 1
        rows << stmt.row.to_a
      end
      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.finalize 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) ⇒ Object



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 28

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