Class: Sequel::D1::Connection
- Inherits:
-
Object
- Object
- Sequel::D1::Connection
- 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
- #close ⇒ Object
- #exec(sql) ⇒ Object
-
#execute(sql, bindings = []) ⇒ Object
Sequel’s transaction machinery calls ‘conn.execute(sql)` to emit BEGIN / COMMIT / ROLLBACK statements directly.
-
#initialize(d1_binding) ⇒ Connection
constructor
A new instance of Connection.
- #query(sql, bindings = []) ⇒ Object
-
#run(sql, bindings = []) ⇒ Object
Execute a write (INSERT/UPDATE/DELETE/DDL).
Constructor Details
#initialize(d1_binding) ⇒ Connection
Returns a new instance of Connection.
68 69 70 |
# File 'lib/sequel/adapters/d1.rb', line 68 def initialize(d1_binding) @binding = d1_binding end |
Instance Method Details
#close ⇒ Object
102 103 104 105 |
# File 'lib/sequel/adapters/d1.rb', line 102 def close # JS binding is isolate-scoped; nothing to close. true end |
#exec(sql) ⇒ Object
91 92 93 |
# File 'lib/sequel/adapters/d1.rb', line 91 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.
98 99 100 |
# File 'lib/sequel/adapters/d1.rb', line 98 def execute(sql, bindings = []) run(sql, bindings) end |
#query(sql, bindings = []) ⇒ Object
85 86 87 88 89 |
# File 'lib/sequel/adapters/d1.rb', line 85 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.
75 76 77 78 79 80 81 82 83 |
# File 'lib/sequel/adapters/d1.rb', line 75 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 |