Class: Sequel::D1::Database

Inherits:
Sequel::Database show all
Includes:
SQLite::DatabaseMethods
Defined in:
lib/sequel/adapters/d1.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sequel::Database

#[]

Class Method Details

.options_from_uri(uri) ⇒ Object

Accepts connection via Hash (the path routes use) OR a ‘d1://` URL for migrator-side CLI usage (no D1 binding available there; raises at connect time if actually used).



115
116
117
# File 'lib/sequel/adapters/d1.rb', line 115

def self.options_from_uri(uri)
  { binding_name: uri.host.to_s }
end

Instance Method Details

#connect(_server) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/sequel/adapters/d1.rb', line 119

def connect(_server)
  d1_binding = @opts[:d1] || @opts[:database]
  unless d1_binding
    raise Error, "Sequel D1 adapter requires a :d1 option (object responding to #prepare). " \
                 "Example: Sequel.connect(adapter: :d1, d1: env['cloudflare.DB'])"
  end
  Connection.new(d1_binding)
end

#execute(sql, opts = OPTS, &block) ⇒ Object


Sequel execute API (Database#execute is the core plumbing Dataset#all / #insert / #update / #delete all funnel through)




133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sequel/adapters/d1.rb', line 133

def execute(sql, opts = OPTS, &block)
  # Materialise rows via await. The inner synchronize block
  # awaits the D1 Promise; we await synchronize's Promise
  # result here so callers see a plain Array. D1-side failures
  # (Promise rejection / Cloudflare::D1Error / any JS exception
  # bubbled via Opal) are caught and re-raised as Sequel::D1::Error
  # with the offending SQL attached so Sequel's error handling
  # path classifies them via database_error_classes.
  rows = synchronize(opts[:server]) do |conn|
    conn.query(sql, Array(opts[:arguments])).__await__
  end.__await__
  rows.each(&block) if block
  rows
rescue Error
  raise
rescue ::Exception => e
  raise wrap_d1_error(e, sql, 'execute')
end

#execute_ddl(sql, opts = OPTS) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/sequel/adapters/d1.rb', line 214

def execute_ddl(sql, opts = OPTS)
  synchronize(opts[:server]) do |conn|
    conn.exec(sql).__await__
  end.__await__
  nil
rescue Error
  raise
rescue ::Exception => e
  raise wrap_d1_error(e, sql, 'execute_ddl')
end

#execute_dui(sql, opts = OPTS) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/sequel/adapters/d1.rb', line 163

def execute_dui(sql, opts = OPTS)
  synchronize(opts[:server]) do |conn|
    raw = conn.run(sql, Array(opts[:arguments])).__await__
    d1_meta_value(raw, 'changes')
  end.__await__
rescue Error
  raise
rescue ::Exception => e
  raise wrap_d1_error(e, sql, 'execute_dui')
end

#execute_insert(sql, opts = OPTS) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/sequel/adapters/d1.rb', line 152

def execute_insert(sql, opts = OPTS)
  synchronize(opts[:server]) do |conn|
    raw = conn.run(sql, Array(opts[:arguments])).__await__
    d1_meta_value(raw, 'last_row_id')
  end.__await__
rescue Error
  raise
rescue ::Exception => e
  raise wrap_d1_error(e, sql, 'execute_insert')
end