Class: Sequel::CloudflareD1::Database

Inherits:
Database
  • Object
show all
Includes:
UnmodifiedIdentifiers::DatabaseMethods
Defined in:
lib/sequel/adapters/cloudflare_d1.rb

Instance Method Summary collapse

Instance Method Details

#connect(server) ⇒ Object

Raises:

  • (Error)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 14

def connect(server)
  opts = server_opts(server)

  @account_id = opts[:account_id] || ENV["CLOUDFLARE_ACCOUNT_ID"]
  @api_token = opts[:api_token] || ENV["CLOUDFLARE_API_TOKEN"]
  @database_id = opts[:database] || opts[:database_id]

  raise Error, "account_id is required" unless @account_id
  raise Error, "api_token is required" unless @api_token
  raise Error, "database_id is required" unless @database_id

  @client = ::Cloudflare::D1::Client.new(
    account_id: @account_id,
    api_token: @api_token
  )
end

#database_typeObject



166
167
168
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 166

def database_type
  :cloudflare_d1
end

#dataset_class_defaultObject



170
171
172
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 170

def dataset_class_default
  Dataset
end

#disconnect_connection(conn) ⇒ Object



31
32
33
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 31

def disconnect_connection(conn)
  # No persistent connection to close
end

#execute(sql, opts = OPTS) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 35

def execute(sql, opts = OPTS)
  synchronize(opts[:server]) do |conn|
    result = @client.query(
      database_id: @database_id,
      sql: sql
    )

    result.dig("result", 0, "results")
  end
end

#execute_ddl(sql, opts = OPTS) ⇒ Object



75
76
77
78
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 75

def execute_ddl(sql, opts = OPTS)
  execute(sql, opts)
  nil
end

#execute_dui(sql, opts = OPTS) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 46

def execute_dui(sql, opts = OPTS)
  synchronize(opts[:server]) do |conn|
    result = @client.query(
      database_id: @database_id,
      sql: sql
    )

    meta = result.dig("result", 0, "meta")
    meta["changes"] || 0
  end
end

#execute_insert(sql, opts = OPTS) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 58

def execute_insert(sql, opts = OPTS)
  synchronize(opts[:server]) do |conn|
    result = @client.query(
      database_id: @database_id,
      sql: sql
    )

    meta = result.dig("result", 0, "meta")
    meta["last_row_id"]
  end
end

#fetch_rows(sql) ⇒ Object



70
71
72
73
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 70

def fetch_rows(sql)
  rows = execute(sql)
  rows.each { |row| yield row } if rows
end

#indexes(table, opts = OPTS) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 111

def indexes(table, opts = OPTS)
  sql = "PRAGMA index_list(#{literal(table.to_s)})"
  index_list = execute(sql)

  index_list.map do |idx|
    index_name = idx["name"]
    unique = idx["unique"] == 1

    columns_sql = "PRAGMA index_info(#{literal(index_name)})"
    columns = execute(columns_sql)
      .sort_by { |c| c["seqno"] }
      .map { |c| c["name"].to_sym }

    {
      name: index_name.to_sym,
      unique: unique,
      columns: columns
    }
  end
end

#schema(table, opts = OPTS) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 94

def schema(table, opts = OPTS)
  sql = "PRAGMA table_info(#{literal(table.to_s)})"
  rows = execute(sql)

  rows.map do |row|
    column = row["name"].to_sym
    info = {
      type: type_literal_to_sequel_type(row["type"]),
      allow_null: row["notnull"] == 0,
      default: row["dflt_value"],
      primary_key: row["pk"] == 1,
      db_type: row["type"]
    }
    [column, info]
  end
end

#supports_create_table_if_not_exists?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 132

def supports_create_table_if_not_exists?
  true
end

#supports_deferrable_constraints?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 152

def supports_deferrable_constraints?
  true
end

#supports_drop_table_if_exists?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 136

def supports_drop_table_if_exists?
  true
end

#supports_prepared_transactions?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 148

def supports_prepared_transactions?
  false
end

#supports_savepoints?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 144

def supports_savepoints?
  false
end

#supports_transaction_isolation_levels?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 140

def supports_transaction_isolation_levels?
  false
end

#supports_transactional_ddl?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 156

def supports_transactional_ddl?
  false
end

#tables(opts = OPTS) ⇒ Object

Schema methods



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 82

def tables(opts = OPTS)
  sql = <<-SQL
    SELECT name FROM sqlite_master
    WHERE type = 'table'
    AND name NOT LIKE 'sqlite_%'
    ORDER BY name
  SQL

  rows = execute(sql)
  rows.map { |row| row["name"].to_sym }
end

#transaction(opts = OPTS) ⇒ Object



160
161
162
163
164
# File 'lib/sequel/adapters/cloudflare_d1.rb', line 160

def transaction(opts = OPTS)
  # D1 API doesn't support transactions across HTTP requests
  # So we just yield without transaction support
  yield
end