skyward-orm 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +66 -48
  4. data/lib/skyward/orm/adapters/postgresql/connection.rb +64 -4
  5. data/lib/skyward/orm/adapters/postgresql.rb +95 -76
  6. data/lib/skyward/orm/adapters/sqlite/connection.rb +87 -0
  7. data/lib/skyward/orm/adapters/sqlite/constraints.rb +15 -15
  8. data/lib/skyward/orm/adapters/sqlite.rb +59 -55
  9. data/lib/skyward/orm/adapters/statement_cache.rb +51 -0
  10. data/lib/skyward/orm/adapters.rb +12 -6
  11. data/lib/skyward/orm/cli/boot_file.rb +2 -2
  12. data/lib/skyward/orm/cli/migration.rb +7 -7
  13. data/lib/skyward/orm/cli/scaffold/source.rb +49 -30
  14. data/lib/skyward/orm/cli/scaffold.rb +6 -6
  15. data/lib/skyward/orm/commands.rb +9 -9
  16. data/lib/skyward/orm/connection/instrumented.rb +4 -2
  17. data/lib/skyward/orm/connection/pool.rb +2 -2
  18. data/lib/skyward/orm/database/decoders.rb +41 -0
  19. data/lib/skyward/orm/database/schema_check.rb +3 -3
  20. data/lib/skyward/orm/database.rb +65 -31
  21. data/lib/skyward/orm/migrations/definitions.rb +13 -13
  22. data/lib/skyward/orm/migrations/diff.rb +4 -4
  23. data/lib/skyward/orm/migrations/directory.rb +5 -5
  24. data/lib/skyward/orm/migrations/runner.rb +8 -8
  25. data/lib/skyward/orm/migrations/script.rb +9 -9
  26. data/lib/skyward/orm/migrations/status.rb +7 -7
  27. data/lib/skyward/orm/migrations/summary.rb +22 -22
  28. data/lib/skyward/orm/migrations/versions.rb +2 -3
  29. data/lib/skyward/orm/projection.rb +6 -12
  30. data/lib/skyward/orm/queries/clauses.rb +201 -112
  31. data/lib/skyward/orm/query.rb +2 -2
  32. data/lib/skyward/orm/record.rb +5 -9
  33. data/lib/skyward/orm/statements/placeholders.rb +3 -3
  34. data/lib/skyward/orm/statements.rb +34 -28
  35. data/lib/skyward/orm/tables/column_types.rb +7 -7
  36. data/lib/skyward/orm/tables/declarations.rb +5 -5
  37. data/lib/skyward/orm/tables/rows.rb +28 -10
  38. data/lib/skyward/orm/tables/sources.rb +3 -3
  39. data/lib/skyward/orm/tables/values.rb +18 -18
  40. data/lib/skyward/orm/version.rb +1 -1
  41. data/lib/skyward/orm.rb +15 -14
  42. data/sig/generated/skyward/orm/adapters/postgresql/connection.rbs +35 -3
  43. data/sig/generated/skyward/orm/adapters/postgresql.rbs +14 -3
  44. data/sig/generated/skyward/orm/adapters/sqlite/connection.rbs +55 -0
  45. data/sig/generated/skyward/orm/adapters/sqlite.rbs +11 -6
  46. data/sig/generated/skyward/orm/adapters/statement_cache.rbs +37 -0
  47. data/sig/generated/skyward/orm/adapters.rbs +3 -0
  48. data/sig/generated/skyward/orm/cli/scaffold/source.rbs +11 -0
  49. data/sig/generated/skyward/orm/connection/instrumented.rbs +4 -2
  50. data/sig/generated/skyward/orm/database/decoders.rbs +19 -0
  51. data/sig/generated/skyward/orm/database.rbs +38 -14
  52. data/sig/generated/skyward/orm/queries/clauses.rbs +81 -28
  53. data/sig/generated/skyward/orm/record.rbs +6 -7
  54. data/sig/generated/skyward/orm/statements.rbs +8 -10
  55. data/sig/generated/skyward/orm/tables/rows.rbs +1 -1
  56. data/sig/generated/skyward/orm.rbs +2 -2
  57. data/sig/manual/pg.rbs +10 -0
  58. data/sig/manual/skyward/orm/database/decoders.rbs +3 -0
  59. data/sig/manual/skyward/orm.rbs +9 -3
  60. data/sig/manual/sqlite3.rbs +13 -2
  61. metadata +11 -3
@@ -3,18 +3,25 @@
3
3
  module Skyward::ORM::Statements
4
4
  extend self
5
5
 
6
- # @rbs adapter: Skyward::ORM::_Adapter
7
- # @rbs column_types: Skyward::ORM::Tables::ColumnTypes
8
- # @rbs tables: Array[Skyward::ORM::Catalog::Table]
6
+ # @rbs database: Skyward::ORM::Database
9
7
  # @rbs table: singleton(Skyward::ORM::Record)
10
8
  # @rbs values: Hash[Symbol, untyped]
9
+ # @rbs returned_row: Class?
11
10
  # @rbs return: [String, Array[untyped]]
12
- def insert(adapter, column_types, tables, table, values)
11
+ def insert(database, table, values, returned_row)
12
+ adapter = database.adapter
13
+ column_types = database.column_types
14
+ tables = database.tables
15
+ columns = values.keys
13
16
  require_values(column_types, tables, table, values)
14
- require_present(table, column_types.columns(table), values.keys, generated(tables, table), "#{table}.insert")
15
- return ["INSERT INTO #{quote(table.table_name)} DEFAULT VALUES", []] if values.empty?
16
-
17
- inserting(adapter, column_types, table, values.keys, [values])
17
+ require_present(table, column_types.columns(table), columns, generated(tables, table), "#{table}.insert")
18
+ sql = database.cached_sql([:insert, table, columns, returned_row]) do
19
+ inserted = if columns.empty? then "INSERT INTO #{quote(table.table_name)} DEFAULT VALUES"
20
+ else insertion(adapter, table, columns, 1)
21
+ end
22
+ [inserted, (returning(returned_row) if returned_row)].compact.join(' ')
23
+ end
24
+ [sql, binds(adapter, column_types, table, values)]
18
25
  end
19
26
 
20
27
  # @rbs adapter: Skyward::ORM::_Adapter
@@ -40,7 +47,11 @@ module Skyward::ORM::Statements
40
47
  end
41
48
  # Every row has row 0's columns by now, so only row 0 can lack one.
42
49
  require_present(table, column_types.columns(table), columns, generated, "#{table}.insert_all row 0")
43
- inserting(adapter, column_types, table, columns, rows)
50
+ named = by_name(column_types, table)
51
+ [
52
+ insertion(adapter, table, columns, rows.size),
53
+ rows.flat_map { |row| columns.map { |column| encode(adapter, named.fetch(column), row.fetch(column)) } }
54
+ ]
44
55
  end
45
56
 
46
57
  # @rbs adapter: Skyward::ORM::_Adapter
@@ -105,9 +116,9 @@ module Skyward::ORM::Statements
105
116
  # @rbs return: Hash[Symbol, Skyward::ORM::Tables::Column]
106
117
  def by_name(column_types, table) = column_types.columns(table).to_h { |column| [column.name, column] }
107
118
 
108
- # @rbs columns: Array[Symbol]
119
+ # @rbs row: Class
109
120
  # @rbs return: String
110
- def returning(columns) = "RETURNING #{columns.map { |column| quote(column) }.join(", ")}"
121
+ def returning(row) = "RETURNING #{Skyward::ORM::Tables::Members.of(row).map { |column| quote(column) }.join(', ')}"
111
122
 
112
123
  # Quoted, so a column named order or user is still a column.
113
124
  # @rbs name: Symbol
@@ -122,20 +133,15 @@ module Skyward::ORM::Statements
122
133
  def exact(type, value) = type == :decimal && value.is_a?(Integer) ? BigDecimal(value) : value
123
134
 
124
135
  # @rbs adapter: Skyward::ORM::_Adapter
125
- # @rbs column_types: Skyward::ORM::Tables::ColumnTypes
126
136
  # @rbs table: singleton(Skyward::ORM::Record)
127
137
  # @rbs columns: Array[Symbol]
128
- # @rbs rows: Array[Hash[Symbol, untyped]]
129
- # @rbs return: [String, Array[untyped]]
130
- def inserting(adapter, column_types, table, columns, rows)
131
- tuples = Array.new(rows.size * columns.size) { |index| adapter.placeholder(index + 1) }
132
- .each_slice(columns.size).map { |placeholders| "(#{placeholders.join(", ")})" }
133
- named = by_name(column_types, table)
134
- [
135
- "INSERT INTO #{quote(table.table_name)} (#{columns.map { |column| quote(column) }.join(", ")}) " \
136
- "VALUES #{tuples.join(", ")}",
137
- rows.flat_map { |row| columns.map { |column| encode(adapter, named.fetch(column), row.fetch(column)) } }
138
- ]
138
+ # @rbs rows: Integer
139
+ # @rbs return: String
140
+ def insertion(adapter, table, columns, rows)
141
+ tuples = Array.new(rows * columns.size) { |index| adapter.placeholder(index + 1) }
142
+ .each_slice(columns.size).map { |placeholders| "(#{placeholders.join(', ')})" }
143
+ "INSERT INTO #{quote(table.table_name)} (#{columns.map { |column| quote(column) }.join(', ')}) " \
144
+ "VALUES #{tuples.join(', ')}"
139
145
  end
140
146
 
141
147
  # @rbs table: singleton(Skyward::ORM::Record)
@@ -203,7 +209,7 @@ module Skyward::ORM::Statements
203
209
  return if missing.empty?
204
210
 
205
211
  raise ArgumentError,
206
- "#{subject} needs #{missing.join(", ")}: a column that can't be NULL and has no default needs a value"
212
+ "#{subject} needs #{missing.join(', ')}: a column that can't be NULL and has no default needs a value"
207
213
  end
208
214
 
209
215
  # Matched as sets, since each row's values are read in the first row's order.
@@ -212,12 +218,12 @@ module Skyward::ORM::Statements
212
218
  # @rbs given: Array[Symbol]
213
219
  # @rbs return: void
214
220
  def require_alike(subject, columns, given)
215
- differences = { "has" => given - columns, "lacks" => columns - given }.reject { |_, names| names.empty? }
221
+ differences = { 'has' => given - columns, 'lacks' => columns - given }.reject { |_, names| names.empty? }
216
222
  return if differences.empty?
217
223
 
218
224
  raise ArgumentError,
219
- "#{subject} #{differences.map { |verb, names| "#{verb} #{names.join(", ")}" }.join(" and ")}, " \
220
- "unlike row 0: every row needs the same columns"
225
+ "#{subject} #{differences.map { |verb, names| "#{verb} #{names.join(', ')}" }.join(' and ')}, " \
226
+ 'unlike row 0: every row needs the same columns'
221
227
  end
222
228
 
223
229
  # @rbs adapter: Skyward::ORM::_Adapter
@@ -233,6 +239,6 @@ module Skyward::ORM::Statements
233
239
  raise ArgumentError,
234
240
  "#{table}.insert_all binds #{binds} values, #{columns} for each of #{rows} rows, but the " \
235
241
  "database binds at most #{limit} in one statement. Split the rows, in a Skyward.transaction if they " \
236
- "must land together."
242
+ 'must land together.'
237
243
  end
238
244
  end
@@ -6,19 +6,19 @@ class Skyward::ORM::Tables::ColumnTypes
6
6
 
7
7
  UNTYPED = Skyward::Core::Types::Scalar.new(name: :untyped) #: Skyward::Core::Types::Scalar
8
8
 
9
- COLUMN_TYPES = "A column is Integer, Float, BigDecimal, String, bool, Date, Time, uuid, int32, int16, " \
10
- "a union of symbols, Hash[String, untyped] or Array[T], any of them nullable, or untyped." #: String
9
+ COLUMN_TYPES = 'A column is Integer, Float, BigDecimal, String, bool, Date, Time, uuid, int32, int16, ' \
10
+ 'a union of symbols, Hash[String, untyped] or Array[T], any of them nullable, or untyped.' #: String
11
11
 
12
12
  ELEMENTS = %i[integer float decimal string bool date time].freeze #: Array[Skyward::Core::Types::scalar]
13
13
 
14
- ELEMENT_TYPES = "An Array column is Array[T], where T is Integer, Float, BigDecimal, String, bool, Date, Time " \
14
+ ELEMENT_TYPES = 'An Array column is Array[T], where T is Integer, Float, BigDecimal, String, bool, Date, Time ' \
15
15
  "or a union of symbols, and isn't nullable." #: String
16
16
 
17
17
  # The aliases Skyward ORM declares, for its reader to keep as the column type each names.
18
18
  REFINEMENTS = {
19
- "::Skyward::ORM::uuid" => :uuid,
20
- "::Skyward::ORM::int32" => :int32,
21
- "::Skyward::ORM::int16" => :int16
19
+ '::Skyward::ORM::uuid' => :uuid,
20
+ '::Skyward::ORM::int32' => :int32,
21
+ '::Skyward::ORM::int16' => :int16
22
22
  }.freeze #: Hash[String, Skyward::ORM::Tables::refinement]
23
23
 
24
24
  # @rbs reader: Skyward::Core::Types::Reader
@@ -42,7 +42,7 @@ class Skyward::ORM::Tables::ColumnTypes
42
42
  type_name = @reader.type_name(klass)
43
43
  Skyward::ORM::Tables::Members.of(klass).map do |name|
44
44
  returned = @reader.method_type(type_name, name)&.type&.return_type if type_name
45
- written = returned.to_s.gsub(/(?<!\w)::/, "")
45
+ written = returned.to_s.gsub(/(?<!\w)::/, '')
46
46
  column = returned ? read(name, returned, written) : column(name, UNTYPED, written)
47
47
  case column
48
48
  when Skyward::ORM::Tables::Column then column
@@ -144,7 +144,7 @@ module Skyward::ORM::Tables::Declarations
144
144
  # @rbs problem: String
145
145
  # @rbs return: bot
146
146
  def refuse(declaration, arguments, problem)
147
- raise ArgumentError, "#{self} #{"#{declaration} #{arguments.map(&:inspect).join(", ")}".rstrip}: #{problem}"
147
+ raise ArgumentError, "#{self} #{"#{declaration} #{arguments.map(&:inspect).join(', ')}".rstrip}: #{problem}"
148
148
  end
149
149
 
150
150
  # @rbs value: untyped
@@ -154,7 +154,7 @@ module Skyward::ORM::Tables::Declarations
154
154
  # @rbs columns: Array[untyped]
155
155
  # @rbs return: String?
156
156
  def unnamed(columns)
157
- if columns.empty? then "it needs at least one column"
157
+ if columns.empty? then 'it needs at least one column'
158
158
  elsif !columns.all?(Symbol) then "a column's name must be a Symbol, such as :title"
159
159
  end
160
160
  end
@@ -166,7 +166,7 @@ module Skyward::ORM::Tables::Declarations
166
166
  if !table.is_a?(Class) || !(table < Skyward::ORM::Record)
167
167
  "it references #{table.inspect}, which isn't a table class"
168
168
  elsif !on_delete.nil? && !ON_DELETE.include?(on_delete)
169
- "on_delete: is #{on_delete.inspect}, but it can only be :#{ON_DELETE.join(", :")} or nil"
169
+ "on_delete: is #{on_delete.inspect}, but it can only be :#{ON_DELETE.join(', :')} or nil"
170
170
  end
171
171
  end
172
172
 
@@ -174,11 +174,11 @@ module Skyward::ORM::Tables::Declarations
174
174
  # @rbs length: untyped
175
175
  # @rbs return: Skyward::ORM::Tables::SQL | Skyward::ORM::Tables::Length | String
176
176
  def check_rule(sql, length)
177
- if sql && length then "it takes Skyward.sql(\"...\") or length:, not both"
177
+ if sql && length then 'it takes Skyward.sql("...") or length:, not both'
178
178
  elsif sql.is_a?(Skyward::ORM::Tables::SQL) then sql
179
179
  elsif sql.is_a?(String) then "write the SQL as Skyward.sql(#{sql.inspect}), not a plain String"
180
180
  elsif sql then "it takes Skyward.sql(\"...\") or length:, not #{sql.inspect}"
181
- elsif length.nil? then "it needs Skyward.sql(\"...\") or length:"
181
+ elsif length.nil? then 'it needs Skyward.sql("...") or length:'
182
182
  else length_rule(length)
183
183
  end
184
184
  end
@@ -3,27 +3,45 @@
3
3
  module Skyward::ORM::Tables::Rows
4
4
  extend self
5
5
 
6
- METHODS = %i[initialize_columns to_h == eql? hash].freeze #: Array[Symbol]
6
+ METHODS = %i[initialize_columns load_columns to_h deconstruct_keys inspect == eql? hash].freeze #: Array[Symbol]
7
7
 
8
- # Each row sets, compares and hashes its columns' instance variables
8
+ # Each row sets, reads, compares and hashes its columns' instance variables
9
9
  # directly, rather than reflecting on them for every row.
10
10
  # @rbs columns: Array[Symbol]
11
11
  # @rbs return: String
12
12
  def source(columns)
13
13
  mismatch = "raise ArgumentError, Skyward::ORM::Tables::Rows.mismatch(self.class, #{columns.inspect}, columns.keys)"
14
+ # A column may be named after a keyword, such as end, which can't name a parameter.
15
+ values = columns.each_index.map { |index| "value#{index}" }
14
16
  <<~RUBY
15
17
  private def initialize_columns(columns)
16
18
  #{columns.map { |column| "@#{column} = columns.fetch(#{column.inspect}) { #{mismatch} }" }.join("\n")}
17
19
  #{mismatch} unless columns.size == #{columns.size}
18
20
  end
19
21
 
20
- def to_h = { #{columns.map { |column| "#{column}: @#{column}" }.join(", ")} }
22
+ # A decoder passes a value for each column, in order, so nothing is checked.
23
+ private def load_columns(#{values.join(', ')})
24
+ #{columns.zip(values).map { |column, value| "@#{column} = #{value}" }.join("\n")}
25
+ freeze
26
+ end
27
+
28
+ def to_h = { #{columns.map { |column| "#{column}: @#{column}" }.join(', ')} }
29
+
30
+ def deconstruct_keys(keys)
31
+ return to_h unless keys
32
+
33
+ found = {}
34
+ #{columns.map { |column| "found[#{column.inspect}] = @#{column} if keys.include?(#{column.inspect})" }.join("\n")}
35
+ found
36
+ end
37
+
38
+ def inspect = "#<\#{self.class} #{columns.map { |column| "#{column}=\#{@#{column}.inspect}" }.join(', ')}>"
21
39
 
22
- def ==(other) = #{compare(columns, "==")}
40
+ def ==(other) = #{compare(columns, '==')}
23
41
 
24
- def eql?(other) = #{compare(columns, "eql?")}
42
+ def eql?(other) = #{compare(columns, 'eql?')}
25
43
 
26
- def hash = [#{["self.class", *columns.map { |column| "@#{column}" }].join(", ")}].hash
44
+ def hash = [#{['self.class', *columns.map { |column| "@#{column}" }].join(', ')}].hash
27
45
  RUBY
28
46
  end
29
47
 
@@ -35,10 +53,10 @@ module Skyward::ORM::Tables::Rows
35
53
  missing = columns - given
36
54
  unknown = given - columns
37
55
  problems = [
38
- ("is missing #{missing.join(", ")}" unless missing.empty?),
39
- ("has no column #{unknown.join(", ")}" unless unknown.empty?)
56
+ ("is missing #{missing.join(', ')}" unless missing.empty?),
57
+ ("has no column #{unknown.join(', ')}" unless unknown.empty?)
40
58
  ]
41
- "#{klass} #{problems.compact.join(", and ")}"
59
+ "#{klass} #{problems.compact.join(', and ')}"
42
60
  end
43
61
 
44
62
  private
@@ -47,6 +65,6 @@ module Skyward::ORM::Tables::Rows
47
65
  # @rbs method: String
48
66
  # @rbs return: String
49
67
  def compare(columns, method)
50
- ["other.instance_of?(self.class)", *columns.map { |column| "@#{column}.#{method}(other.#{column})" }].join(" && ")
68
+ ['other.instance_of?(self.class)', *columns.map { |column| "@#{column}.#{method}(other.#{column})" }].join(' && ')
51
69
  end
52
70
  end
@@ -40,11 +40,11 @@ module Skyward::ORM::Tables::Sources
40
40
  def class_name(klass)
41
41
  name = klass.name
42
42
  # A class in an anonymous module has a temporary name, which const_source_location refuses.
43
- path, line = Object.const_source_location(name) if name && !name.start_with?("#<")
43
+ path, line = Object.const_source_location(name) if name && !name.start_with?('#<')
44
44
  return unless name && path && line && File.file?(path)
45
45
 
46
- text = File.readlines(path).fetch(line - 1, "").chomp
47
- start, finish = text.match(/\bclass\s+\K(?:\w+::)*#{Regexp.escape(name.split("::").last.to_s)}\b/)&.offset(0)
46
+ text = File.readlines(path).fetch(line - 1, '').chomp
47
+ start, finish = text.match(/\bclass\s+\K(?:\w+::)*#{Regexp.escape(name.split('::').last.to_s)}\b/)&.offset(0)
48
48
  Skyward::Core::Diagnostics::Spot.new(path:, line:, text:, columns: start...finish) if start && finish
49
49
  end
50
50
 
@@ -17,17 +17,17 @@ module Skyward::ORM::Tables::Values
17
17
  }.freeze #: Hash[Symbol, Array[Module]]
18
18
 
19
19
  WRITTEN = {
20
- integer: "Integer",
21
- float: "Float",
22
- decimal: "BigDecimal",
23
- string: "String",
24
- bool: "bool",
25
- date: "Date",
26
- time: "Time",
27
- uuid: "uuid",
28
- int32: "int32",
29
- int16: "int16",
30
- json: "Hash[String, untyped]"
20
+ integer: 'Integer',
21
+ float: 'Float',
22
+ decimal: 'BigDecimal',
23
+ string: 'String',
24
+ bool: 'bool',
25
+ date: 'Date',
26
+ time: 'Time',
27
+ uuid: 'uuid',
28
+ int32: 'int32',
29
+ int16: 'int16',
30
+ json: 'Hash[String, untyped]'
31
31
  }.freeze #: Hash[Symbol, String]
32
32
 
33
33
  UUID = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/ #: Regexp
@@ -39,12 +39,12 @@ module Skyward::ORM::Tables::Values
39
39
 
40
40
  WHOLE = {
41
41
  json: "a #{WRITTEN.fetch(:json)}",
42
- array: "an Array"
42
+ array: 'an Array'
43
43
  }.freeze #: Hash[Symbol, String]
44
44
 
45
- UNTYPED = "untyped, which takes any value but a Hash or a nested Array" #: String
45
+ UNTYPED = 'untyped, which takes any value but a Hash or a nested Array' #: String
46
46
 
47
- UNTYPED_WRITE = "untyped, which takes any one value but an Array, a Hash or a Range" #: String
47
+ UNTYPED_WRITE = 'untyped, which takes any one value but an Array, a Hash or a Range' #: String
48
48
 
49
49
  # @rbs table: singleton(Skyward::ORM::Record)
50
50
  # @rbs column: Skyward::ORM::Tables::Column
@@ -57,7 +57,7 @@ module Skyward::ORM::Tables::Values
57
57
  whole = WHOLE[column.type]
58
58
  if whole && !value.nil?
59
59
  return "#{table}.#{column.name} is #{whole} column, which where matches only to nil. " \
60
- "Match its contents with an SQL string."
60
+ 'Match its contents with an SQL string.'
61
61
  end
62
62
 
63
63
  refused = candidates(value).reject { |candidate| accepts?(column, candidate) }
@@ -203,10 +203,10 @@ module Skyward::ORM::Tables::Values
203
203
  case column.type
204
204
  when :untyped then untyped
205
205
  when :enum
206
- symbols = column.symbols.map(&:inspect).join(" | ")
206
+ symbols = column.symbols.map(&:inspect).join(' | ')
207
207
  column.nullable ? "(#{symbols})?" : symbols
208
- when :array then "Array[#{written(item(column), untyped)}]#{"?" if column.nullable}"
209
- else "#{WRITTEN.fetch(column.type)}#{"?" if column.nullable}"
208
+ when :array then "Array[#{written(item(column), untyped)}]#{'?' if column.nullable}"
209
+ else "#{WRITTEN.fetch(column.type)}#{'?' if column.nullable}"
210
210
  end
211
211
  end
212
212
  end
@@ -4,7 +4,7 @@ module Skyward
4
4
  module ORM
5
5
  # x-release-please-start-version
6
6
 
7
- VERSION = "0.4.0" #: String
7
+ VERSION = '0.4.1' #: String
8
8
 
9
9
  # x-release-please-end
10
10
  end
data/lib/skyward/orm.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bigdecimal"
4
- require "date"
5
- require "json"
6
- require "skyward/core"
7
- require "zeitwerk"
8
- require_relative "orm/version"
3
+ require 'bigdecimal'
4
+ require 'date'
5
+ require 'json'
6
+ require 'skyward/core'
7
+ require 'zeitwerk'
8
+ require_relative 'orm/version'
9
9
 
10
10
  loader = Zeitwerk::Loader.for_gem_extension(Skyward)
11
11
  loader.inflector.inflect(
12
- "orm" => "ORM", "sql" => "SQL", "sqlite" => "SQLite", "postgresql" => "PostgreSQL", "cli" => "CLI"
12
+ 'orm' => 'ORM', 'sql' => 'SQL', 'sqlite' => 'SQLite', 'postgresql' => 'PostgreSQL', 'cli' => 'CLI'
13
13
  )
14
14
  # The skyward executable loads it, and it defines no constant.
15
15
  loader.ignore("#{__dir__}/orm/commands.rb")
@@ -17,18 +17,19 @@ loader.setup
17
17
 
18
18
  # An app's signatures subclass Skyward::ORM::Record, so RBS needs its declaration.
19
19
  dir = __dir__ #: String
20
- Skyward::Core::Signatures.register(File.expand_path("../../sig", dir))
20
+ Skyward::Core::Signatures.register(File.expand_path('../../sig', dir))
21
21
 
22
22
  module Skyward
23
23
  # @rbs (String url, ?signatures: String, ?pool: Integer, ?check: bool, ?default: bool,
24
- # ?instrument: ORM::_Instrument?) -> ORM::Database
24
+ # ?instrument: ORM::_Instrument?, ?prepared_statements: bool) -> ORM::Database
25
25
  def self.connect(
26
26
  url,
27
- signatures: "sig",
27
+ signatures: 'sig',
28
28
  pool: 5,
29
29
  check: ORM::Connection::SESSION.check?,
30
30
  default: true,
31
- instrument: nil
31
+ instrument: nil,
32
+ prepared_statements: true
32
33
  )
33
34
  # Steep knows instrument is one, but untyped code can pass anything.
34
35
  unless instrument.nil? || instrument.respond_to?(:call) # steep:ignore NoMethod
@@ -55,7 +56,7 @@ module Skyward
55
56
  end
56
57
  connections = ORM::Connection::Pool.new(adapter.pool_size(location, pool), opener)
57
58
  tables = connections.with { |connection| adapter.tables(connection) }
58
- database = ORM::Database.new(adapter, connections, column_types, tables, check:)
59
+ database = ORM::Database.new(adapter, connections, column_types, tables, check:, prepared_statements:)
59
60
  database.check(ORM::Tables::Loaded.table_classes(ORM::Record)) if check
60
61
  default ? ORM::Connection::SESSION.connect(database) : database
61
62
  end
@@ -66,7 +67,7 @@ module Skyward
66
67
  unless database.is_a?(ORM::Database) # steep:ignore UnreachableBranch
67
68
  raise ArgumentError, "Skyward.using takes a database Skyward.connect returned, not a #{database.class}"
68
69
  end
69
- raise ArgumentError, "Skyward.using takes a block to run on the database" unless block_given?
70
+ raise ArgumentError, 'Skyward.using takes a block to run on the database' unless block_given?
70
71
 
71
72
  ORM::Connection::SESSION.using(database, &)
72
73
  end
@@ -86,7 +87,7 @@ module Skyward
86
87
  case row
87
88
  when String then database.hashes(row, arguments)
88
89
  when Class then database.rows(row, arguments.fetch(0), arguments.drop(1))
89
- else raise ArgumentError, "Skyward.query takes a row class or the SQL as its first argument"
90
+ else raise ArgumentError, 'Skyward.query takes a row class or the SQL as its first argument'
90
91
  end
91
92
  end
92
93
 
@@ -3,28 +3,39 @@
3
3
  # PG::Connection runs SQL with exec_params, which returns a PG::Result, so this
4
4
  # gives it the execute every adapter's connection has.
5
5
  class Skyward::ORM::Adapters::PostgreSQL::Connection
6
+ # What PostgreSQL reports when a migration changed a column a kept statement
7
+ # returns. Other errors share its SQLSTATE, 0A000.
8
+ REVALIDATE: String
9
+
6
10
  @connection: PG::Connection
7
11
 
12
+ @statements: Skyward::ORM::Adapters::StatementCache[String]
13
+
14
+ @prepared: Integer
15
+
8
16
  @changes: Integer
9
17
 
10
18
  # @rbs connection: PG::Connection
19
+ # @rbs statements: Skyward::ORM::Adapters::StatementCache[String]
11
20
  # @rbs return: void
12
- def initialize: (PG::Connection connection) -> void
21
+ def initialize: (PG::Connection connection, Skyward::ORM::Adapters::StatementCache[String] statements) -> void
13
22
 
14
23
  # The block form clears each result as soon as its rows are read, so the
15
24
  # rows it changed are kept for changes, as SQLite keeps them for its own.
16
25
  # @rbs sql: String
17
26
  # @rbs binds: Array[untyped]
27
+ # @rbs prepare: bool
18
28
  # @rbs return: Array[Array[String?]]
19
- def execute: (String sql, ?Array[untyped] binds) -> Array[Array[String?]]
29
+ def execute: (String sql, ?Array[untyped] binds, ?prepare: bool) -> Array[Array[String?]]
20
30
 
21
31
  # @dynamic changes
22
32
  attr_reader changes: Integer
23
33
 
24
34
  # @rbs sql: String
25
35
  # @rbs *binds: untyped
36
+ # @rbs prepare: bool
26
37
  # @rbs return: Array[Array[String?]]
27
- def execute2: (String sql, *untyped binds) -> Array[Array[String?]]
38
+ def execute2: (String sql, *untyped binds, ?prepare: bool) -> Array[Array[String?]]
28
39
 
29
40
  # exec, unlike exec_params, runs every statement in the SQL.
30
41
  # @rbs sql: String
@@ -38,4 +49,25 @@ class Skyward::ORM::Adapters::PostgreSQL::Connection
38
49
 
39
50
  # @rbs return: void
40
51
  def close: () -> void
52
+
53
+ private
54
+
55
+ # @rbs [T] (String sql, Array[untyped] binds, bool prepare) { (PG::Result result) -> T } -> T
56
+ def run: [T] (String sql, Array[untyped] binds, bool prepare) { (PG::Result result) -> T } -> T
57
+
58
+ # Prepared before the evicted one is closed, so in an aborted transaction,
59
+ # which refuses to prepare, nothing is evicted. It's closed with the
60
+ # protocol's Close, which PgBouncer tracks as it does a prepare, and which,
61
+ # unlike DEALLOCATE, succeeds on a name DISCARD ALL dropped, so it can't
62
+ # abort a transaction. A name is never used again, so a statement an
63
+ # exception left on the server unkept can't collide.
64
+ # @rbs sql: String
65
+ # @rbs return: String
66
+ def keep: (String sql) -> String
67
+
68
+ # A statement DISCARD ALL dropped is missing, and one a migration changed the
69
+ # result of is refused.
70
+ # @rbs error: PG::Error
71
+ # @rbs return: bool
72
+ def stale?: (PG::Error error) -> bool
41
73
  end
@@ -66,6 +66,12 @@ module Skyward::ORM::Adapters::PostgreSQL
66
66
  # @rbs return: Skyward::ORM::_Connection
67
67
  def connect: (String location) -> Skyward::ORM::_Connection
68
68
 
69
+ # A connection closes each statement it evicts with close_prepared, which pg
70
+ # compiles only against libpq 17 and later.
71
+ # @rbs connection: Module
72
+ # @rbs return: void
73
+ def require_close_prepared: (Module connection) -> void
74
+
69
75
  # @rbs (String _location, Integer size) -> Integer
70
76
  def pool_size: (String _location, Integer size) -> Integer
71
77
 
@@ -137,6 +143,11 @@ module Skyward::ORM::Adapters::PostgreSQL
137
143
  # @rbs return: String
138
144
  def encode_array: (Array[untyped] items) -> String
139
145
 
146
+ # @rbs column: Skyward::ORM::Tables::Column
147
+ # @rbs value: String
148
+ # @rbs return: String
149
+ def decoding: (Skyward::ORM::Tables::Column column, String value) -> String
150
+
140
151
  # @rbs column: Skyward::ORM::Tables::Column
141
152
  # @rbs value: untyped
142
153
  # @rbs return: untyped
@@ -157,9 +168,9 @@ module Skyward::ORM::Adapters::PostgreSQL
157
168
  def accepts?: (Skyward::ORM::Tables::column_type type, String sql_type) -> bool
158
169
 
159
170
  # @rbs type: Skyward::ORM::Tables::column_type
160
- # @rbs value: untyped
161
- # @rbs return: untyped
162
- def scalar: (Skyward::ORM::Tables::column_type type, untyped value) -> untyped
171
+ # @rbs value: String
172
+ # @rbs return: String
173
+ def scalar: (Skyward::ORM::Tables::column_type type, String value) -> String
163
174
 
164
175
  # @rbs rows: Array[Array[untyped]]
165
176
  # @rbs return: Array[Skyward::ORM::Catalog::Index]
@@ -0,0 +1,55 @@
1
+ # Generated from lib/skyward/orm/adapters/sqlite/connection.rb with RBS::Inline
2
+
3
+ # SQLite3::Database#execute prepares, runs and closes a new statement on every
4
+ # call, so this keeps the statements it's asked to prepare, to run again.
5
+ class Skyward::ORM::Adapters::SQLite::Connection
6
+ @database: SQLite3::Database[SQLite3::result_as_array]
7
+
8
+ @statements: Skyward::ORM::Adapters::StatementCache[SQLite3::Statement]
9
+
10
+ # @rbs database: SQLite3::Database[SQLite3::result_as_array]
11
+ # @rbs statements: Skyward::ORM::Adapters::StatementCache[SQLite3::Statement]
12
+ # @rbs return: void
13
+ def initialize: (SQLite3::Database[SQLite3::result_as_array] database, Skyward::ORM::Adapters::StatementCache[SQLite3::Statement] statements) -> void
14
+
15
+ # @rbs sql: String
16
+ # @rbs binds: Array[untyped]
17
+ # @rbs prepare: bool
18
+ # @rbs return: Array[Array[untyped]]
19
+ def execute: (String sql, ?Array[untyped] binds, ?prepare: bool) -> Array[Array[untyped]]
20
+
21
+ # @rbs sql: String
22
+ # @rbs *binds: untyped
23
+ # @rbs prepare: bool
24
+ # @rbs return: Array[Array[untyped]]
25
+ def execute2: (String sql, *untyped binds, ?prepare: bool) -> Array[Array[untyped]]
26
+
27
+ # @rbs sql: String
28
+ # @rbs return: void
29
+ def execute_batch: (String sql) -> void
30
+
31
+ # @rbs return: Integer
32
+ def changes: () -> Integer
33
+
34
+ # @rbs return: bool
35
+ def transaction_active?: () -> bool
36
+
37
+ # SQLite holds a closed database open until each of its statements is closed too.
38
+ # @rbs return: void
39
+ def close: () -> void
40
+
41
+ private
42
+
43
+ # @rbs sql: String
44
+ # @rbs binds: Array[untyped]
45
+ # @rbs prepare: bool
46
+ # @rbs return: SQLite3::Statement?
47
+ def kept: (String sql, Array[untyped] binds, bool prepare) -> SQLite3::Statement?
48
+
49
+ # Reset however it ends, so no kept statement is left running, where it would
50
+ # stop a migration's DROP TABLE with "database table is locked".
51
+ # @rbs statement: SQLite3::Statement
52
+ # @rbs binds: Array[untyped]
53
+ # @rbs return: Array[Array[untyped]]
54
+ def run: (SQLite3::Statement statement, Array[untyped] binds) -> Array[Array[untyped]]
55
+ end
@@ -136,6 +136,11 @@ module Skyward::ORM::Adapters::SQLite
136
136
  # @rbs return: String
137
137
  def encode_array: (Array[untyped] items) -> String
138
138
 
139
+ # @rbs column: Skyward::ORM::Tables::Column
140
+ # @rbs value: String
141
+ # @rbs return: String
142
+ def decoding: (Skyward::ORM::Tables::Column column, String value) -> String
143
+
139
144
  # @rbs column: Skyward::ORM::Tables::Column
140
145
  # @rbs value: untyped
141
146
  # @rbs return: untyped
@@ -171,15 +176,15 @@ module Skyward::ORM::Adapters::SQLite
171
176
  private
172
177
 
173
178
  # @rbs type: Skyward::ORM::Tables::column_type
174
- # @rbs value: untyped
175
- # @rbs return: untyped
176
- def scalar: (Skyward::ORM::Tables::column_type type, untyped value) -> untyped
179
+ # @rbs value: String
180
+ # @rbs return: String
181
+ def scalar: (Skyward::ORM::Tables::column_type type, String value) -> String
177
182
 
178
183
  # An Array[Float]'s JSON may hold an Integer, as an Array[Float] takes one.
179
184
  # @rbs type: Skyward::ORM::Tables::column_type
180
- # @rbs value: untyped
181
- # @rbs return: untyped
182
- def item: (Skyward::ORM::Tables::column_type type, untyped value) -> untyped
185
+ # @rbs value: String
186
+ # @rbs return: String
187
+ def item: (Skyward::ORM::Tables::column_type type, String value) -> String
183
188
 
184
189
  # @rbs sql_type: String
185
190
  # @rbs return: Symbol