1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Sequel ships no signatures, so this describes the parts pro-tacts
# calls: one SQLite database, the datasets over it, and the schema DSL
# that creates them. A row is a hash keyed by column, whose values carry
# whatever type the column held, which is why the store converts them at
# its edge rather than trusting them.
#
# The create_table generator is declared because the block runs against
# it rather than against the calling object, and `[self: ...]` is how
# RBS says so; without it every column line in the schema reads as a
# call on Store.
#
# Written against sequel 5.107.0 (lib/sequel/database/, dataset/,
# adapters/sqlite.rb).
module Sequel
  type row = Hash[Symbol, untyped]

  def self.sqlite: (String path, **untyped options) -> Database

  # Loads an extension into Sequel itself; `:migration` is the one this
  # app asks for, and it is what defines Migrator.
  def self.extension: (*Symbol names) -> void

  # A qualified or unqualified identifier: `Sequel[:excluded][:vcard]`,
  # and the comparisons built from one.
  def self.[]: (Symbol name) -> untyped

  # SQL written out by hand, for the one default expression the schema
  # DSL has no spelling for.
  def self.lit: (String sql) -> untyped

  # A column in descending order, for Dataset#order — the ordering the
  # "recently updated" reads sort by.
  def self.desc: (Symbol name) -> untyped

  # A SQL function call: `coalesce` over a group's name and its id, in
  # the read that lends an unnamed group's lines under something.
  def self.function: (Symbol name, *untyped arguments) -> untyped

  # A string bound as a BLOB rather than TEXT: a group photo's bytes.
  def self.blob: (String bytes) -> untyped

  class Database
    def []: (Symbol table) -> Dataset

    # Loads an extension into this database and the datasets it makes.
    def extension: (*Symbol names) -> Database

    def extend_datasets: (Module extension) -> void

    def self.register_extension: (Symbol name, ?Module extension) ?{ (Database) -> void } -> void

    # Runs a statement for its effect. Used only for the pragmas Sequel
    # has no option for.
    def run: (String sql) -> untyped

    # Wraps a statement whose rows are wanted, as a dataset.
    def fetch: (String sql) -> Dataset

    # Joins a transaction already open rather than nesting, so a write
    # inside a write is still one atomic unit.
    def transaction: [T] (?mode: Symbol) { () -> T } -> T

    # SQLite's own: which BEGIN every transaction opens with. Nil,
    # :deferred, :immediate, or :exclusive.
    def transaction_mode=: (Symbol? mode) -> void

    def create_table: (Symbol name, **untyped options) { () [self: Schema::CreateTableGenerator] -> void } -> void

    def disconnect: () -> void
  end

  # The dataset surface lib/sequel/extensions/sole.rb needs of whatever
  # it is mixed into. A structural interface rather than Dataset itself,
  # because Dataset includes Sole and naming it here would be circular.
  interface _Queryable
    def limit: (Integer count) -> Dataset

    def first_source: () -> Symbol
  end

  class Dataset
    include Enumerable[Sequel::row]

    # lib/sequel/extensions/sole.rb. RBS has no way to say "only once
    # DB.extension(:sole) has run", and Store runs it on every open.
    include Sole

    def self.register_extension: (Symbol name, Module extension) -> void

    def where: (*untyped conditions) -> Dataset
    def exclude: (*untyped conditions) -> Dataset
    def order: (*untyped columns) -> Dataset
    def distinct: (*untyped columns) -> Dataset
    # The columns a joined dataset comes back with, named rather than
    # merged by whichever table's column won. Signed here so it reads
    # as the query builder's rather than Enumerable's own #select.
    def select: (*untyped columns) -> Dataset
    def select_append: (*untyped columns) -> Dataset
    def limit: (Integer count) -> Dataset
    # A dataset as a subquery under a name, for a column computed in
    # another dataset's select (Store#group_choices' member count).
    def as: (Symbol name) -> untyped
    # Hash conditions pair the joined table's column with the source's
    # (`id: :card_id` is cards.id = birthdays.card_id); the array form
    # joins same-named columns.
    def join: (Symbol table, Array[Symbol] using) -> Dataset
           | (Symbol table, Sequel::row conditions) -> Dataset

    # The table a dataset selects from; raises when it has no source.
    def first_source: () -> Symbol

    # An upsert: `target` is the conflicting column, `update` what to set
    # on the row already there.
    def insert_conflict: (**untyped options) -> Dataset

    def first: () -> Sequel::row?
    def all: () -> Array[Sequel::row]
    def count: () -> Integer
    # Whether any row matches, asked of the database rather than by
    # fetching one.
    def empty?: () -> bool
    def columns: () -> Array[Symbol]
    def single_value: () -> untyped
    # One column of the first row, or nil for no row.
    def get: (Symbol column) -> untyped
    def select_map: (untyped columns) -> Array[untyped]

    def insert: (Sequel::row values) -> Integer
    def update: (Sequel::row values) -> Integer

    # Rows removed.
    def delete: () -> Integer

    def each: () { (Sequel::row) -> void } -> void
  end

  module Schema
    # The schema DSL. Each column method takes the column name and the
    # options for it; `text: true` is what keeps a String a SQLite TEXT
    # rather than a varchar, which a STRICT table refuses.
    class CreateTableGenerator
      def String: (Symbol name, **untyped options) -> void
      def Integer: (Symbol name, **untyped options) -> void

      # A single argument is the table's own key (an AUTOINCREMENT
      # integer on SQLite); an array is a composite primary key over
      # columns already declared.
      def primary_key: (Symbol | Array[Symbol] name, **untyped options) -> void

      def foreign_key: (Symbol | Array[Symbol] name, ?Symbol table, **untyped options) -> void

      def constraint: (Symbol name, **untyped condition) -> void

      def index: (Symbol | Array[Symbol] columns, **untyped options) -> void
    end
  end

  # Applies the migrations in a directory that the database has not run
  # yet, and does nothing to one that is already current.
  class Migrator
    def self.run: (Database database, String directory, **untyped options) -> untyped
  end

  class Error < StandardError
  end

  # What `first!` raises on an empty dataset, and `sole` with it.
  class NoMatchingRow < Error
    def initialize: (?untyped message_or_dataset) -> void
  end

  class DatabaseError < Error
  end

  class ConstraintViolation < DatabaseError
  end

  class CheckConstraintViolation < ConstraintViolation
  end

  # What an insert onto a taken primary key raises: the answer a group
  # id's draw waits for rather than asking first.
  class UniqueConstraintViolation < ConstraintViolation
  end
end