Class: Sequel::D1::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/adapters/d1.rb

Overview

Thin wrapper around a duck-typed D1 binding. Backed by Opal ‘.__await__` resolution so Ruby callers see a synchronous method signature. One instance per Sequel::D1::Database; SingleConnectionPool keeps this at exactly one “connection” per Database instance.

Instance Method Summary collapse

Constructor Details

#initialize(d1_binding) ⇒ Connection

Returns a new instance of Connection.



67
68
69
# File 'lib/sequel/adapters/d1.rb', line 67

def initialize(d1_binding)
  @binding = d1_binding
end

Instance Method Details

#closeObject



101
102
103
104
# File 'lib/sequel/adapters/d1.rb', line 101

def close
  # JS binding is isolate-scoped; nothing to close.
  true
end

#exec(sql) ⇒ Object



90
91
92
# File 'lib/sequel/adapters/d1.rb', line 90

def exec(sql)
  run(sql.to_s, [])
end

#execute(sql, bindings = []) ⇒ Object

Sequel’s transaction machinery calls ‘conn.execute(sql)` to emit BEGIN / COMMIT / ROLLBACK statements directly. Forward these to `run` so the full TX lifecycle reaches D1.



97
98
99
# File 'lib/sequel/adapters/d1.rb', line 97

def execute(sql, bindings = [])
  run(sql, bindings)
end

#query(sql, bindings = []) ⇒ Object



84
85
86
87
88
# File 'lib/sequel/adapters/d1.rb', line 84

def query(sql, bindings = [])
  stmt = @binding.prepare(sql.to_s)
  stmt = stmt.bind(*bindings) unless bindings.empty?
  stmt.all
end

#run(sql, bindings = []) ⇒ Object

Execute a write (INSERT/UPDATE/DELETE/DDL). Returns a Hash with :last_insert_rowid and :changes keys so Sequel execute_insert / execute_dui can pass them back.



74
75
76
77
78
79
80
81
82
# File 'lib/sequel/adapters/d1.rb', line 74

def run(sql, bindings = [])
  # Always stringify — Sequel's SQL builder uses
  # HomuraSqlBuffer (buffer wrapper for Opal), and D1's
  # prepare() validator (Zod) rejects non-string inputs with
  # "Expected string, received object".
  stmt = @binding.prepare(sql.to_s)
  stmt = stmt.bind(*bindings) unless bindings.empty?
  stmt.run
end