skyward-orm 0.3.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.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +247 -60
- data/lib/skyward/orm/adapters/postgresql/connection.rb +64 -4
- data/lib/skyward/orm/adapters/postgresql.rb +189 -68
- data/lib/skyward/orm/adapters/sqlite/connection.rb +87 -0
- data/lib/skyward/orm/adapters/sqlite/constraints.rb +32 -14
- data/lib/skyward/orm/adapters/sqlite.rb +158 -45
- data/lib/skyward/orm/adapters/statement_cache.rb +51 -0
- data/lib/skyward/orm/adapters.rb +23 -7
- data/lib/skyward/orm/catalog/column.rb +2 -1
- data/lib/skyward/orm/cli/boot_file.rb +4 -4
- data/lib/skyward/orm/cli/migrate.rb +1 -1
- data/lib/skyward/orm/cli/migration.rb +8 -8
- data/lib/skyward/orm/cli/scaffold/source.rb +323 -0
- data/lib/skyward/orm/cli/scaffold.rb +70 -0
- data/lib/skyward/orm/cli/status.rb +5 -3
- data/lib/skyward/orm/commands.rb +20 -8
- data/lib/skyward/orm/connection/instrumented.rb +71 -0
- data/lib/skyward/orm/connection/pool.rb +2 -2
- data/lib/skyward/orm/connection/session.rb +74 -5
- data/lib/skyward/orm/database/decoders.rb +41 -0
- data/lib/skyward/orm/{connection/database → database}/schema_check.rb +22 -10
- data/lib/skyward/orm/{connection/database.rb → database.rb} +70 -36
- data/lib/skyward/orm/execution.rb +9 -0
- data/lib/skyward/orm/migrations/alter_table.rb +6 -2
- data/lib/skyward/orm/migrations/definitions.rb +20 -17
- data/lib/skyward/orm/migrations/diff.rb +156 -32
- data/lib/skyward/orm/migrations/directory.rb +5 -5
- data/lib/skyward/orm/migrations/rename_constraint.rb +8 -0
- data/lib/skyward/orm/migrations/runner.rb +21 -11
- data/lib/skyward/orm/migrations/script.rb +67 -27
- data/lib/skyward/orm/migrations/status.rb +38 -9
- data/lib/skyward/orm/migrations/summary.rb +23 -26
- data/lib/skyward/orm/migrations/versions.rb +3 -4
- data/lib/skyward/orm/migrations.rb +1 -1
- data/lib/skyward/orm/projection.rb +24 -11
- data/lib/skyward/orm/queries/clauses.rb +298 -124
- data/lib/skyward/orm/query.rb +2 -2
- data/lib/skyward/orm/record.rb +27 -9
- data/lib/skyward/orm/{placeholders.rb → statements/placeholders.rb} +4 -4
- data/lib/skyward/orm/statements.rb +178 -31
- data/lib/skyward/orm/tables/column.rb +2 -1
- data/lib/skyward/orm/tables/column_types.rb +63 -16
- data/lib/skyward/orm/tables/declarations.rb +43 -15
- data/lib/skyward/orm/{members.rb → tables/members.rb} +1 -1
- data/lib/skyward/orm/tables/rows.rb +28 -10
- data/lib/skyward/orm/tables/sources.rb +63 -0
- data/lib/skyward/orm/tables/validation.rb +12 -3
- data/lib/skyward/orm/tables/values.rb +124 -14
- data/lib/skyward/orm/tables.rb +2 -1
- data/lib/skyward/orm/transactional.rb +2 -2
- data/lib/skyward/orm/version.rb +1 -1
- data/lib/skyward/orm.rb +52 -17
- data/sig/generated/skyward/orm/adapters/postgresql/connection.rbs +35 -3
- data/sig/generated/skyward/orm/adapters/postgresql.rbs +64 -14
- data/sig/generated/skyward/orm/adapters/sqlite/connection.rbs +55 -0
- data/sig/generated/skyward/orm/adapters/sqlite/constraints.rbs +6 -0
- data/sig/generated/skyward/orm/adapters/sqlite.rbs +72 -7
- data/sig/generated/skyward/orm/adapters/statement_cache.rbs +37 -0
- data/sig/generated/skyward/orm/adapters.rbs +6 -0
- data/sig/generated/skyward/orm/catalog/column.rbs +6 -4
- data/sig/generated/skyward/orm/cli/boot_file.rbs +4 -4
- data/sig/generated/skyward/orm/cli/migrate.rbs +2 -2
- data/sig/generated/skyward/orm/cli/migration.rbs +2 -2
- data/sig/generated/skyward/orm/cli/scaffold/source.rbs +158 -0
- data/sig/generated/skyward/orm/cli/scaffold.rbs +28 -0
- data/sig/generated/skyward/orm/cli/status.rbs +2 -2
- data/sig/generated/skyward/orm/connection/instrumented.rbs +47 -0
- data/sig/generated/skyward/orm/connection/session.rbs +40 -6
- data/sig/generated/skyward/orm/database/decoders.rbs +19 -0
- data/sig/generated/skyward/orm/{connection/database → database}/schema_check.rbs +11 -4
- data/sig/generated/skyward/orm/{connection/database.rbs → database.rbs} +40 -17
- data/sig/generated/skyward/orm/execution.rbs +18 -0
- data/sig/generated/skyward/orm/migrations/alter_table.rbs +11 -5
- data/sig/generated/skyward/orm/migrations/definitions.rbs +2 -2
- data/sig/generated/skyward/orm/migrations/diff.rbs +77 -11
- data/sig/generated/skyward/orm/migrations/rename_constraint.rbs +16 -0
- data/sig/generated/skyward/orm/migrations/runner.rbs +5 -0
- data/sig/generated/skyward/orm/migrations/script.rbs +32 -8
- data/sig/generated/skyward/orm/migrations/status.rbs +15 -2
- data/sig/generated/skyward/orm/migrations/summary.rbs +0 -1
- data/sig/generated/skyward/orm/migrations/versions.rbs +2 -2
- data/sig/generated/skyward/orm/migrations.rbs +1 -1
- data/sig/generated/skyward/orm/projection.rbs +10 -2
- data/sig/generated/skyward/orm/queries/clauses.rbs +129 -40
- data/sig/generated/skyward/orm/record.rbs +14 -7
- data/sig/generated/skyward/orm/{placeholders.rbs → statements/placeholders.rbs} +2 -2
- data/sig/generated/skyward/orm/statements.rbs +84 -8
- data/sig/generated/skyward/orm/tables/column.rbs +6 -4
- data/sig/generated/skyward/orm/tables/column_types.rbs +18 -4
- data/sig/generated/skyward/orm/tables/declarations.rbs +15 -1
- data/sig/generated/skyward/orm/{members.rbs → tables/members.rbs} +2 -2
- data/sig/generated/skyward/orm/tables/rows.rbs +1 -1
- data/sig/generated/skyward/orm/tables/sources.rbs +32 -0
- data/sig/generated/skyward/orm/tables/values.rbs +41 -1
- data/sig/generated/skyward/orm/tables.rbs +3 -1
- data/sig/generated/skyward/orm.rbs +6 -6
- data/sig/manual/pg.rbs +22 -0
- data/sig/manual/skyward/orm/cli/scaffold/source.rbs +3 -0
- data/sig/manual/skyward/orm/cli/scaffold.rbs +3 -0
- data/sig/manual/skyward/orm/database/decoders.rbs +3 -0
- data/sig/manual/skyward/orm/database/schema_check.rbs +3 -0
- data/sig/manual/skyward/orm/statements/placeholders.rbs +3 -0
- data/sig/manual/skyward/orm/tables/members.rbs +3 -0
- data/sig/manual/skyward/orm/tables/sources.rbs +3 -0
- data/sig/manual/skyward/orm.rbs +41 -8
- data/sig/manual/sqlite3.rbs +13 -2
- metadata +51 -14
- data/sig/manual/skyward/orm/connection/database/schema_check.rbs +0 -3
- data/sig/manual/skyward/orm/members.rbs +0 -3
- data/sig/manual/skyward/orm/placeholders.rbs +0 -3
data/lib/skyward/orm/record.rb
CHANGED
|
@@ -6,12 +6,15 @@ class Skyward::ORM::Record
|
|
|
6
6
|
# Each table class defines these for its columns, from Skyward::ORM::Tables::Rows.source.
|
|
7
7
|
# @rbs!
|
|
8
8
|
# private def initialize_columns: (Hash[Symbol, untyped] columns) -> void
|
|
9
|
+
# private def load_columns: (*untyped values) -> self
|
|
9
10
|
# def to_h: () -> Hash[Symbol, untyped]
|
|
11
|
+
# def deconstruct_keys: (Array[Symbol]? keys) -> Hash[Symbol, untyped]
|
|
12
|
+
# def inspect: () -> String
|
|
10
13
|
# def ==: (untyped other) -> bool
|
|
11
14
|
# def eql?: (untyped other) -> bool
|
|
12
15
|
# def hash: () -> Integer
|
|
13
16
|
|
|
14
|
-
# @dynamic initialize_columns, to_h, ==, eql?, hash
|
|
17
|
+
# @dynamic initialize_columns, load_columns, to_h, deconstruct_keys, inspect, ==, eql?, hash
|
|
15
18
|
class_eval(Skyward::ORM::Tables::Rows.source([]), __FILE__, __LINE__)
|
|
16
19
|
|
|
17
20
|
# @rbs return: Skyward::ORM::Query[instance]
|
|
@@ -57,7 +60,7 @@ class Skyward::ORM::Record
|
|
|
57
60
|
# @rbs return: Integer | Skyward::ORM::Violation
|
|
58
61
|
def self.insert(**values)
|
|
59
62
|
database = Skyward::ORM::Connection::SESSION.database
|
|
60
|
-
sql, binds = Skyward::ORM::Statements.insert(database
|
|
63
|
+
sql, binds = Skyward::ORM::Statements.insert(database, self, values, nil)
|
|
61
64
|
database.write(self, sql, binds)
|
|
62
65
|
end
|
|
63
66
|
|
|
@@ -71,6 +74,28 @@ class Skyward::ORM::Record
|
|
|
71
74
|
end
|
|
72
75
|
end
|
|
73
76
|
|
|
77
|
+
# @rbs rows: Array[Hash[Symbol, untyped]]
|
|
78
|
+
# @rbs return: Integer | Skyward::ORM::Violation
|
|
79
|
+
def self.insert_all(rows)
|
|
80
|
+
return 0 if rows.empty?
|
|
81
|
+
|
|
82
|
+
database = Skyward::ORM::Connection::SESSION.database
|
|
83
|
+
sql, binds = Skyward::ORM::Statements.insert_all(
|
|
84
|
+
database.adapter, database.column_types, database.tables, self, rows
|
|
85
|
+
)
|
|
86
|
+
database.write(self, sql, binds)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# @rbs rows: Array[Hash[Symbol, untyped]]
|
|
90
|
+
# @rbs return: Integer
|
|
91
|
+
def self.insert_all!(rows)
|
|
92
|
+
written = insert_all(rows)
|
|
93
|
+
case written
|
|
94
|
+
when Skyward::ORM::Violation then raise Skyward::ORM::ViolationError, written
|
|
95
|
+
else written
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
74
99
|
# @rbs return: Array[Symbol]
|
|
75
100
|
def self.members = schema.columns
|
|
76
101
|
|
|
@@ -86,11 +111,4 @@ class Skyward::ORM::Record
|
|
|
86
111
|
# @rbs **changes: untyped
|
|
87
112
|
# @rbs return: self
|
|
88
113
|
def with(**changes) = self.class.new(**to_h, **changes) #: self
|
|
89
|
-
|
|
90
|
-
# @rbs keys: Array[Symbol]?
|
|
91
|
-
# @rbs return: Hash[Symbol, untyped]
|
|
92
|
-
def deconstruct_keys(keys) = keys ? to_h.slice(*keys) : to_h
|
|
93
|
-
|
|
94
|
-
# @rbs return: String
|
|
95
|
-
def inspect = "#<#{self.class} #{to_h.map { |column, value| "#{column}=#{value.inspect}" }.join(", ")}>"
|
|
96
114
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module Skyward::ORM::Placeholders
|
|
3
|
+
module Skyward::ORM::Statements::Placeholders
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
6
|
# A ? in a string, a quoted name or a comment is the SQL's own, so those
|
|
@@ -14,7 +14,7 @@ module Skyward::ORM::Placeholders
|
|
|
14
14
|
# @rbs return: String
|
|
15
15
|
def replace(sql, count)
|
|
16
16
|
marks = 0
|
|
17
|
-
replaced = sql.gsub(MARKS) { |match| match ==
|
|
17
|
+
replaced = sql.gsub(MARKS) { |match| match == '?' ? yield(marks += 1) : match }
|
|
18
18
|
return replaced if marks == count
|
|
19
19
|
|
|
20
20
|
raise ArgumentError, "The SQL has #{marks} ?, each taking one value, but was given #{count}"
|
|
@@ -28,8 +28,8 @@ module Skyward::ORM::Placeholders
|
|
|
28
28
|
def require_scalars(binds, subject)
|
|
29
29
|
binds.each.with_index(1) do |bind, index|
|
|
30
30
|
kind = case bind
|
|
31
|
-
when Array then
|
|
32
|
-
when Hash then
|
|
31
|
+
when Array then 'an Array'
|
|
32
|
+
when Hash then 'a Hash'
|
|
33
33
|
end
|
|
34
34
|
raise ArgumentError, "#{subject} binds one value to each ?, but value #{index} is #{kind}" if kind
|
|
35
35
|
end
|
|
@@ -3,34 +3,91 @@
|
|
|
3
3
|
module Skyward::ORM::Statements
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
|
+
# @rbs database: Skyward::ORM::Database
|
|
7
|
+
# @rbs table: singleton(Skyward::ORM::Record)
|
|
8
|
+
# @rbs values: Hash[Symbol, untyped]
|
|
9
|
+
# @rbs returned_row: Class?
|
|
10
|
+
# @rbs return: [String, Array[untyped]]
|
|
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
|
|
16
|
+
require_values(column_types, tables, table, 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)]
|
|
25
|
+
end
|
|
26
|
+
|
|
6
27
|
# @rbs adapter: Skyward::ORM::_Adapter
|
|
7
28
|
# @rbs column_types: Skyward::ORM::Tables::ColumnTypes
|
|
29
|
+
# @rbs tables: Array[Skyward::ORM::Catalog::Table]
|
|
8
30
|
# @rbs table: singleton(Skyward::ORM::Record)
|
|
9
|
-
# @rbs
|
|
31
|
+
# @rbs rows: Array[Hash[Symbol, untyped]]
|
|
10
32
|
# @rbs return: [String, Array[untyped]]
|
|
11
|
-
def
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
33
|
+
def insert_all(adapter, column_types, tables, table, rows)
|
|
34
|
+
columns = rows.fetch(0).keys
|
|
35
|
+
# Neither database takes DEFAULT VALUES for more than one row.
|
|
36
|
+
if columns.empty?
|
|
37
|
+
raise ArgumentError, "#{table}.insert_all row 0 has no columns. Use #{table}.insert for a row of defaults."
|
|
38
|
+
end
|
|
16
39
|
|
|
17
|
-
|
|
40
|
+
require_bindable(adapter, table, rows.size, columns.size)
|
|
41
|
+
generated = generated(tables, table)
|
|
42
|
+
rows.each_with_index do |row, index|
|
|
43
|
+
subject = "#{table}.insert_all row #{index}"
|
|
44
|
+
require_alike(subject, columns, row.keys)
|
|
45
|
+
refused = unwritable(column_types, generated, table, row)
|
|
46
|
+
raise ArgumentError, "#{subject}: #{refused}" if refused
|
|
47
|
+
end
|
|
48
|
+
# Every row has row 0's columns by now, so only row 0 can lack one.
|
|
49
|
+
require_present(table, column_types.columns(table), columns, generated, "#{table}.insert_all row 0")
|
|
50
|
+
named = by_name(column_types, table)
|
|
18
51
|
[
|
|
19
|
-
|
|
20
|
-
|
|
52
|
+
insertion(adapter, table, columns, rows.size),
|
|
53
|
+
rows.flat_map { |row| columns.map { |column| encode(adapter, named.fetch(column), row.fetch(column)) } }
|
|
21
54
|
]
|
|
22
55
|
end
|
|
23
56
|
|
|
57
|
+
# @rbs adapter: Skyward::ORM::_Adapter
|
|
58
|
+
# @rbs column_types: Skyward::ORM::Tables::ColumnTypes
|
|
59
|
+
# @rbs table: singleton(Skyward::ORM::Record)
|
|
60
|
+
# @rbs values: Hash[Symbol, untyped]
|
|
61
|
+
# @rbs return: Array[untyped]
|
|
62
|
+
def binds(adapter, column_types, table, values)
|
|
63
|
+
columns = by_name(column_types, table)
|
|
64
|
+
values.map { |name, value| encode(adapter, columns.fetch(name), value) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Only a column says a Hash is JSON, or an Array one value: bound to a ? in
|
|
68
|
+
# SQL, either is refused, since the driver would spread it across
|
|
69
|
+
# placeholders. And only a column says an Integer is a decimal, whose text
|
|
70
|
+
# SQLite compares, so 2 is bound as 2.0, as BigDecimal(2) is.
|
|
71
|
+
# @rbs adapter: Skyward::ORM::_Adapter
|
|
72
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
73
|
+
# @rbs value: untyped
|
|
74
|
+
# @rbs return: untyped
|
|
75
|
+
def encode(adapter, column, value)
|
|
76
|
+
return if value.nil?
|
|
77
|
+
|
|
78
|
+
case column.type
|
|
79
|
+
when :json then JSON.generate(value)
|
|
80
|
+
when :array then adapter.encode_array(value.map { |item| exact(column.element, item) })
|
|
81
|
+
else adapter.encode(exact(column.type, value))
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
24
85
|
# @rbs table: singleton(Skyward::ORM::Record)
|
|
25
86
|
# @rbs columns: Array[Symbol]
|
|
26
87
|
# @rbs return: void
|
|
27
88
|
def require_columns(table, columns)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return unless unknown
|
|
31
|
-
|
|
32
|
-
suggestion = DidYouMean::SpellChecker.new(dictionary: known).correct(unknown).first
|
|
33
|
-
raise ArgumentError, "#{table} has no column #{unknown}.#{" Did you mean #{suggestion}?" if suggestion}"
|
|
89
|
+
refused = unknown(table, columns)
|
|
90
|
+
raise ArgumentError, refused if refused
|
|
34
91
|
end
|
|
35
92
|
|
|
36
93
|
# @rbs column_types: Skyward::ORM::Tables::ColumnTypes
|
|
@@ -38,24 +95,30 @@ module Skyward::ORM::Statements
|
|
|
38
95
|
# @rbs conditions: Hash[Symbol, untyped]
|
|
39
96
|
# @rbs return: void
|
|
40
97
|
def require_conditions(column_types, table, conditions)
|
|
41
|
-
|
|
98
|
+
refused = refusal(column_types, table, conditions) do |column, value|
|
|
42
99
|
Skyward::ORM::Tables::Values.mismatch(table, column, value)
|
|
43
100
|
end
|
|
101
|
+
raise ArgumentError, refused if refused
|
|
44
102
|
end
|
|
45
103
|
|
|
46
104
|
# @rbs column_types: Skyward::ORM::Tables::ColumnTypes
|
|
105
|
+
# @rbs tables: Array[Skyward::ORM::Catalog::Table]
|
|
47
106
|
# @rbs table: singleton(Skyward::ORM::Record)
|
|
48
107
|
# @rbs values: Hash[Symbol, untyped]
|
|
49
108
|
# @rbs return: void
|
|
50
|
-
def require_values(column_types, table, values)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
end
|
|
109
|
+
def require_values(column_types, tables, table, values)
|
|
110
|
+
refused = unwritable(column_types, generated(tables, table), table, values)
|
|
111
|
+
raise ArgumentError, refused if refused
|
|
54
112
|
end
|
|
55
113
|
|
|
56
|
-
# @rbs
|
|
114
|
+
# @rbs column_types: Skyward::ORM::Tables::ColumnTypes
|
|
115
|
+
# @rbs table: singleton(Skyward::ORM::Record)
|
|
116
|
+
# @rbs return: Hash[Symbol, Skyward::ORM::Tables::Column]
|
|
117
|
+
def by_name(column_types, table) = column_types.columns(table).to_h { |column| [column.name, column] }
|
|
118
|
+
|
|
119
|
+
# @rbs row: Class
|
|
57
120
|
# @rbs return: String
|
|
58
|
-
def returning(
|
|
121
|
+
def returning(row) = "RETURNING #{Skyward::ORM::Tables::Members.of(row).map { |column| quote(column) }.join(', ')}"
|
|
59
122
|
|
|
60
123
|
# Quoted, so a column named order or user is still a column.
|
|
61
124
|
# @rbs name: Symbol
|
|
@@ -64,16 +127,68 @@ module Skyward::ORM::Statements
|
|
|
64
127
|
|
|
65
128
|
private
|
|
66
129
|
|
|
130
|
+
# @rbs type: Skyward::ORM::Tables::column_type?
|
|
131
|
+
# @rbs value: untyped
|
|
132
|
+
# @rbs return: untyped
|
|
133
|
+
def exact(type, value) = type == :decimal && value.is_a?(Integer) ? BigDecimal(value) : value
|
|
134
|
+
|
|
135
|
+
# @rbs adapter: Skyward::ORM::_Adapter
|
|
136
|
+
# @rbs table: singleton(Skyward::ORM::Record)
|
|
137
|
+
# @rbs columns: Array[Symbol]
|
|
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(', ')}"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# @rbs table: singleton(Skyward::ORM::Record)
|
|
148
|
+
# @rbs columns: Array[Symbol]
|
|
149
|
+
# @rbs return: String?
|
|
150
|
+
def unknown(table, columns)
|
|
151
|
+
known = table.schema.columns
|
|
152
|
+
name = (columns - known).first #: Symbol?
|
|
153
|
+
return unless name
|
|
154
|
+
|
|
155
|
+
suggestion = Skyward::Core::Diagnostics::Suggestion.closest(name.to_s, known.map(&:to_s))
|
|
156
|
+
"#{table} has no column #{name}.#{" Did you mean #{suggestion}?" if suggestion}"
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# @rbs tables: Array[Skyward::ORM::Catalog::Table]
|
|
160
|
+
# @rbs table: singleton(Skyward::ORM::Record)
|
|
161
|
+
# @rbs return: Array[Symbol]
|
|
162
|
+
def generated(tables, table)
|
|
163
|
+
found = tables.find { |candidate| candidate.name == table.table_name }
|
|
164
|
+
found ? found.columns.select(&:generated).map(&:name) : []
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# @rbs column_types: Skyward::ORM::Tables::ColumnTypes
|
|
168
|
+
# @rbs generated: Array[Symbol]
|
|
169
|
+
# @rbs table: singleton(Skyward::ORM::Record)
|
|
170
|
+
# @rbs values: Hash[Symbol, untyped]
|
|
171
|
+
# @rbs return: String?
|
|
172
|
+
def unwritable(column_types, generated, table, values)
|
|
173
|
+
written = (values.keys & generated).first
|
|
174
|
+
return "#{table}.#{written} is generated by the database, so it can't be written" if written
|
|
175
|
+
|
|
176
|
+
refusal(column_types, table, values) do |column, value|
|
|
177
|
+
Skyward::ORM::Tables::Values.write_mismatch(table, column, value)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
67
181
|
# @rbs column_types: Skyward::ORM::Tables::ColumnTypes
|
|
68
182
|
# @rbs table: singleton(Skyward::ORM::Record)
|
|
69
183
|
# @rbs values: Hash[Symbol, untyped]
|
|
70
184
|
# @rbs &: (Skyward::ORM::Tables::Column column, untyped value) -> String?
|
|
71
|
-
# @rbs return:
|
|
72
|
-
def
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
185
|
+
# @rbs return: String?
|
|
186
|
+
def refusal(column_types, table, values)
|
|
187
|
+
refused = unknown(table, values.keys)
|
|
188
|
+
return refused if refused
|
|
189
|
+
|
|
190
|
+
columns = by_name(column_types, table)
|
|
191
|
+
values.filter_map { |name, value| yield(columns.fetch(name), value) }.first
|
|
77
192
|
end
|
|
78
193
|
|
|
79
194
|
# An untyped column's nullability is unknown, so it's left to the database,
|
|
@@ -81,17 +196,49 @@ module Skyward::ORM::Statements
|
|
|
81
196
|
# @rbs table: singleton(Skyward::ORM::Record)
|
|
82
197
|
# @rbs columns: Array[Skyward::ORM::Tables::Column]
|
|
83
198
|
# @rbs given: Array[Symbol]
|
|
199
|
+
# @rbs generated: Array[Symbol]
|
|
200
|
+
# @rbs subject: String
|
|
84
201
|
# @rbs return: void
|
|
85
|
-
def require_present(table, columns, given)
|
|
202
|
+
def require_present(table, columns, given, generated, subject)
|
|
86
203
|
schema = table.schema
|
|
87
204
|
required = columns.reject do |column|
|
|
88
205
|
column.nullable || column.type == :untyped || column.name == schema.primary_key ||
|
|
89
|
-
schema.defaults.key?(column.name)
|
|
206
|
+
schema.defaults.key?(column.name) || generated.include?(column.name)
|
|
90
207
|
end
|
|
91
208
|
missing = required.map(&:name) - given
|
|
92
209
|
return if missing.empty?
|
|
93
210
|
|
|
94
211
|
raise ArgumentError,
|
|
95
|
-
"#{
|
|
212
|
+
"#{subject} needs #{missing.join(', ')}: a column that can't be NULL and has no default needs a value"
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Matched as sets, since each row's values are read in the first row's order.
|
|
216
|
+
# @rbs subject: String
|
|
217
|
+
# @rbs columns: Array[Symbol]
|
|
218
|
+
# @rbs given: Array[Symbol]
|
|
219
|
+
# @rbs return: void
|
|
220
|
+
def require_alike(subject, columns, given)
|
|
221
|
+
differences = { 'has' => given - columns, 'lacks' => columns - given }.reject { |_, names| names.empty? }
|
|
222
|
+
return if differences.empty?
|
|
223
|
+
|
|
224
|
+
raise ArgumentError,
|
|
225
|
+
"#{subject} #{differences.map { |verb, names| "#{verb} #{names.join(', ')}" }.join(' and ')}, " \
|
|
226
|
+
'unlike row 0: every row needs the same columns'
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# @rbs adapter: Skyward::ORM::_Adapter
|
|
230
|
+
# @rbs table: singleton(Skyward::ORM::Record)
|
|
231
|
+
# @rbs rows: Integer
|
|
232
|
+
# @rbs columns: Integer
|
|
233
|
+
# @rbs return: void
|
|
234
|
+
def require_bindable(adapter, table, rows, columns)
|
|
235
|
+
limit = adapter.bind_limit
|
|
236
|
+
binds = rows * columns
|
|
237
|
+
return if binds <= limit
|
|
238
|
+
|
|
239
|
+
raise ArgumentError,
|
|
240
|
+
"#{table}.insert_all binds #{binds} values, #{columns} for each of #{rows} rows, but the " \
|
|
241
|
+
"database binds at most #{limit} in one statement. Split the rows, in a Skyward.transaction if they " \
|
|
242
|
+
'must land together.'
|
|
96
243
|
end
|
|
97
244
|
end
|
|
@@ -6,8 +6,20 @@ 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 =
|
|
10
|
-
|
|
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
|
+
|
|
12
|
+
ELEMENTS = %i[integer float decimal string bool date time].freeze #: Array[Skyward::Core::Types::scalar]
|
|
13
|
+
|
|
14
|
+
ELEMENT_TYPES = 'An Array column is Array[T], where T is Integer, Float, BigDecimal, String, bool, Date, Time ' \
|
|
15
|
+
"or a union of symbols, and isn't nullable." #: String
|
|
16
|
+
|
|
17
|
+
# The aliases Skyward ORM declares, for its reader to keep as the column type each names.
|
|
18
|
+
REFINEMENTS = {
|
|
19
|
+
'::Skyward::ORM::uuid' => :uuid,
|
|
20
|
+
'::Skyward::ORM::int32' => :int32,
|
|
21
|
+
'::Skyward::ORM::int16' => :int16
|
|
22
|
+
}.freeze #: Hash[String, Skyward::ORM::Tables::refinement]
|
|
11
23
|
|
|
12
24
|
# @rbs reader: Skyward::Core::Types::Reader
|
|
13
25
|
# @rbs return: void
|
|
@@ -28,13 +40,17 @@ class Skyward::ORM::Tables::ColumnTypes
|
|
|
28
40
|
# @rbs return: Array[Skyward::ORM::Tables::Column]
|
|
29
41
|
def resolve(klass)
|
|
30
42
|
type_name = @reader.type_name(klass)
|
|
31
|
-
Skyward::ORM::Members.of(klass).map do |name|
|
|
43
|
+
Skyward::ORM::Tables::Members.of(klass).map do |name|
|
|
32
44
|
returned = @reader.method_type(type_name, name)&.type&.return_type if type_name
|
|
33
|
-
written = returned.to_s.gsub(/(?<!\w)::/,
|
|
45
|
+
written = returned.to_s.gsub(/(?<!\w)::/, '')
|
|
34
46
|
column = returned ? read(name, returned, written) : column(name, UNTYPED, written)
|
|
35
47
|
case column
|
|
36
|
-
when
|
|
37
|
-
else
|
|
48
|
+
when Skyward::ORM::Tables::Column then column
|
|
49
|
+
else
|
|
50
|
+
problem, suggestion = column
|
|
51
|
+
spot = Skyward::ORM::Tables::Sources.reader(klass, name)
|
|
52
|
+
raise Skyward::Core::BootError,
|
|
53
|
+
Skyward::ORM::Tables::Sources.explain("#{klass}##{name} #{problem}", [spot], suggestion)
|
|
38
54
|
end
|
|
39
55
|
end.freeze
|
|
40
56
|
end
|
|
@@ -42,33 +58,64 @@ class Skyward::ORM::Tables::ColumnTypes
|
|
|
42
58
|
# @rbs name: Symbol
|
|
43
59
|
# @rbs returned: RBS::Types::t
|
|
44
60
|
# @rbs written: String
|
|
45
|
-
# @rbs return: Skyward::ORM::Tables::Column | String
|
|
61
|
+
# @rbs return: Skyward::ORM::Tables::Column | [String, String?]
|
|
46
62
|
def read(name, returned, written)
|
|
47
63
|
column(name, @reader.read(returned), written)
|
|
48
64
|
# The reader raises for a type it can't read at all, such as one that refers
|
|
49
65
|
# to itself, and the column should still be named.
|
|
50
66
|
rescue Skyward::Core::BootError => e
|
|
51
|
-
"is #{written}. #{e.message}"
|
|
67
|
+
["is #{written}. #{e.message}", nil]
|
|
52
68
|
end
|
|
53
69
|
|
|
54
70
|
# @rbs name: Symbol
|
|
55
71
|
# @rbs read: Skyward::Core::Types::result
|
|
56
72
|
# @rbs written: String
|
|
57
|
-
# @rbs return: Skyward::ORM::Tables::Column | String
|
|
73
|
+
# @rbs return: Skyward::ORM::Tables::Column | [String, String?]
|
|
58
74
|
def column(name, read, written)
|
|
59
75
|
nullable = read.is_a?(Skyward::Core::Types::Nullable)
|
|
60
76
|
carried = read.is_a?(Skyward::Core::Types::Nullable) ? read.type : read
|
|
61
77
|
case carried
|
|
78
|
+
when Skyward::Core::Types::Record, Skyward::Core::Types::Named
|
|
79
|
+
["is #{written}, which isn't a column type yet. #{COLUMN_TYPES}", nil]
|
|
80
|
+
when Skyward::Core::Types::ArrayOf then array(name, carried.type, nullable, written)
|
|
81
|
+
when Skyward::Core::Types::HashOf
|
|
82
|
+
if carried.type == UNTYPED
|
|
83
|
+
Skyward::ORM::Tables::Column.new(name:, type: :json, nullable:, symbols: [], element: nil)
|
|
84
|
+
else
|
|
85
|
+
["is #{written}, which isn't a column type. A Hash column is Hash[String, untyped], holding any JSON.", nil]
|
|
86
|
+
end
|
|
62
87
|
when Skyward::Core::Types::Scalar
|
|
63
|
-
Skyward::ORM::Tables::Column.new(name:, type: carried.name, nullable:, symbols: [])
|
|
88
|
+
Skyward::ORM::Tables::Column.new(name:, type: carried.name, nullable:, symbols: [], element: nil)
|
|
64
89
|
when Skyward::Core::Types::Enum
|
|
65
|
-
Skyward::ORM::Tables::Column.new(name:, type: :enum, nullable:, symbols: carried.symbols)
|
|
90
|
+
Skyward::ORM::Tables::Column.new(name:, type: :enum, nullable:, symbols: carried.symbols, element: nil)
|
|
91
|
+
when Skyward::Core::Types::Refined
|
|
92
|
+
# The reader refines only the aliases in REFINEMENTS.
|
|
93
|
+
type = carried.as #: Skyward::ORM::Tables::refinement
|
|
94
|
+
Skyward::ORM::Tables::Column.new(name:, type:, nullable:, symbols: [], element: nil)
|
|
66
95
|
when Skyward::Core::Types::Unreadable
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
96
|
+
at = ", at #{carried.at}" if carried.at
|
|
97
|
+
["can't be read: Skyward ORM doesn't read #{carried.type}#{at}. #{COLUMN_TYPES}", carried.suggestion]
|
|
98
|
+
else ["is #{written}, which isn't a column type. #{COLUMN_TYPES}", nil]
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# @rbs name: Symbol
|
|
103
|
+
# @rbs item: Skyward::Core::Types::t
|
|
104
|
+
# @rbs nullable: bool
|
|
105
|
+
# @rbs written: String
|
|
106
|
+
# @rbs return: Skyward::ORM::Tables::Column | [String, String?]
|
|
107
|
+
def array(name, item, nullable, written)
|
|
108
|
+
case item
|
|
109
|
+
when Skyward::Core::Types::Scalar
|
|
110
|
+
if ELEMENTS.include?(item.name)
|
|
111
|
+
Skyward::ORM::Tables::Column.new(name:, type: :array, nullable:, symbols: [], element: item.name)
|
|
112
|
+
else
|
|
113
|
+
["is #{written}, which isn't a column type. #{ELEMENT_TYPES}", nil]
|
|
114
|
+
end
|
|
115
|
+
when Skyward::Core::Types::Enum
|
|
116
|
+
Skyward::ORM::Tables::Column.new(name:, type: :array, nullable:, symbols: item.symbols, element: :enum)
|
|
117
|
+
when Skyward::Core::Types::Refined then ["is #{written}, which isn't a column type yet. #{ELEMENT_TYPES}", nil]
|
|
118
|
+
else ["is #{written}, which isn't a column type. #{ELEMENT_TYPES}", nil]
|
|
72
119
|
end
|
|
73
120
|
end
|
|
74
121
|
end
|
|
@@ -16,13 +16,27 @@ module Skyward::ORM::Tables::Declarations
|
|
|
16
16
|
ON_DELETE = %i[cascade restrict set_null set_default no_action].freeze #: Array[Skyward::ORM::Tables::on_delete]
|
|
17
17
|
|
|
18
18
|
# @rbs @schema: Skyward::ORM::Tables::Schema?
|
|
19
|
+
# @rbs @locations: Hash[[Symbol, Symbol], [String, Integer]]?
|
|
19
20
|
|
|
20
21
|
# @rbs return: Skyward::ORM::Tables::Schema
|
|
21
22
|
def schema = @schema || EMPTY
|
|
22
23
|
|
|
24
|
+
# The schema is compared as a value, so where each name was declared is kept
|
|
25
|
+
# beside it, by the declaration and the column or table it names.
|
|
26
|
+
# @rbs declaration: Symbol
|
|
27
|
+
# @rbs name: Symbol
|
|
28
|
+
# @rbs return: [String, Integer]?
|
|
29
|
+
def declared_at(declaration, name) = @locations&.[]([declaration, name])
|
|
30
|
+
|
|
23
31
|
# @rbs return: Symbol
|
|
24
32
|
def table_name
|
|
25
|
-
schema.table || raise(
|
|
33
|
+
schema.table || raise(
|
|
34
|
+
Skyward::Core::BootError,
|
|
35
|
+
Skyward::ORM::Tables::Sources.explain(
|
|
36
|
+
"#{self} has no table. Add `table :name` to its body.",
|
|
37
|
+
[Skyward::ORM::Tables::Sources.class_name(self)]
|
|
38
|
+
)
|
|
39
|
+
)
|
|
26
40
|
end
|
|
27
41
|
|
|
28
42
|
# Every attr_reader is a column, so each one compiles the row's methods again.
|
|
@@ -35,7 +49,7 @@ module Skyward::ORM::Tables::Declarations
|
|
|
35
49
|
repeated = columns.find { |column| columns.count(column) > 1 }
|
|
36
50
|
raise ArgumentError, "#{self} already has a column #{repeated}" if repeated
|
|
37
51
|
|
|
38
|
-
declare(columns: columns.freeze)
|
|
52
|
+
declare(:attr_reader, readers, columns: columns.freeze)
|
|
39
53
|
# Ruby warns when a method is redefined, so the old ones are removed first.
|
|
40
54
|
(Skyward::ORM::Tables::Rows::METHODS & [*instance_methods(false), *private_instance_methods(false)])
|
|
41
55
|
.each { |method| remove_method(method) }
|
|
@@ -47,7 +61,7 @@ module Skyward::ORM::Tables::Declarations
|
|
|
47
61
|
# @rbs return: void
|
|
48
62
|
def table(name)
|
|
49
63
|
refuse(:table, [name], "the table's name must be a Symbol, such as :todos") unless symbol?(name)
|
|
50
|
-
declare(table: name)
|
|
64
|
+
declare(:table, [name], table: name)
|
|
51
65
|
end
|
|
52
66
|
|
|
53
67
|
# @rbs column: Symbol
|
|
@@ -55,7 +69,7 @@ module Skyward::ORM::Tables::Declarations
|
|
|
55
69
|
def primary_key(column)
|
|
56
70
|
problem = unnamed([column])
|
|
57
71
|
refuse(:primary_key, [column], problem) if problem
|
|
58
|
-
declare(primary_key: column)
|
|
72
|
+
declare(:primary_key, [column], primary_key: column)
|
|
59
73
|
end
|
|
60
74
|
|
|
61
75
|
# @rbs column: Symbol
|
|
@@ -66,13 +80,13 @@ module Skyward::ORM::Tables::Declarations
|
|
|
66
80
|
problem = unnamed([column]) || unreferenceable(table, on_delete)
|
|
67
81
|
refuse(:references, [column], problem) if problem
|
|
68
82
|
reference = Skyward::ORM::Tables::Reference.new(column:, table:, on_delete:)
|
|
69
|
-
declare(references: [*schema.references, reference].freeze)
|
|
83
|
+
declare(:references, [column], references: [*schema.references, reference].freeze)
|
|
70
84
|
end
|
|
71
85
|
|
|
72
86
|
# @rbs **values: untyped
|
|
73
87
|
# @rbs return: void
|
|
74
88
|
def default(**values)
|
|
75
|
-
declare(defaults: schema.defaults.merge(values).freeze)
|
|
89
|
+
declare(:default, values.keys, defaults: schema.defaults.merge(values).freeze)
|
|
76
90
|
end
|
|
77
91
|
|
|
78
92
|
# @rbs *columns: Symbol
|
|
@@ -80,7 +94,7 @@ module Skyward::ORM::Tables::Declarations
|
|
|
80
94
|
def unique(*columns)
|
|
81
95
|
problem = unnamed(columns)
|
|
82
96
|
refuse(:unique, columns, problem) if problem
|
|
83
|
-
declare(uniques: [*schema.uniques, columns.freeze].freeze)
|
|
97
|
+
declare(:unique, columns, uniques: [*schema.uniques, columns.freeze].freeze)
|
|
84
98
|
end
|
|
85
99
|
|
|
86
100
|
# @rbs column: Symbol
|
|
@@ -91,7 +105,8 @@ module Skyward::ORM::Tables::Declarations
|
|
|
91
105
|
rule = unnamed([column]) || check_rule(sql, length)
|
|
92
106
|
case rule
|
|
93
107
|
when String then refuse(:check, [column], rule)
|
|
94
|
-
else
|
|
108
|
+
else
|
|
109
|
+
declare(:check, [column], checks: [*schema.checks, Skyward::ORM::Tables::Check.new(column:, rule:)].freeze)
|
|
95
110
|
end
|
|
96
111
|
end
|
|
97
112
|
|
|
@@ -100,15 +115,28 @@ module Skyward::ORM::Tables::Declarations
|
|
|
100
115
|
def index(*columns)
|
|
101
116
|
problem = unnamed(columns)
|
|
102
117
|
refuse(:index, columns, problem) if problem
|
|
103
|
-
declare(indexes: [*schema.indexes, columns.freeze].freeze)
|
|
118
|
+
declare(:index, columns, indexes: [*schema.indexes, columns.freeze].freeze)
|
|
104
119
|
end
|
|
105
120
|
|
|
106
121
|
private
|
|
107
122
|
|
|
123
|
+
# Only a declaration calls this, so its caller's caller is the class body.
|
|
124
|
+
# An anonymous class, from Class.new, keeps no location, and eval'd code has
|
|
125
|
+
# no file to point at.
|
|
126
|
+
# @rbs declaration: Symbol
|
|
127
|
+
# @rbs names: Array[Symbol]
|
|
108
128
|
# @rbs **changes: untyped
|
|
109
129
|
# @rbs return: void
|
|
110
|
-
def declare(**changes)
|
|
130
|
+
def declare(declaration, names, **changes)
|
|
111
131
|
@schema = schema.with(**changes)
|
|
132
|
+
site = caller_locations(2, 1)&.first
|
|
133
|
+
path = site&.absolute_path
|
|
134
|
+
return unless name && site && path
|
|
135
|
+
|
|
136
|
+
located = names.to_h do |named|
|
|
137
|
+
[[declaration, named], [path, site.lineno]] #: [[Symbol, Symbol], [String, Integer]]
|
|
138
|
+
end
|
|
139
|
+
@locations = (@locations || {}).merge(located)
|
|
112
140
|
end
|
|
113
141
|
|
|
114
142
|
# @rbs declaration: Symbol
|
|
@@ -116,7 +144,7 @@ module Skyward::ORM::Tables::Declarations
|
|
|
116
144
|
# @rbs problem: String
|
|
117
145
|
# @rbs return: bot
|
|
118
146
|
def refuse(declaration, arguments, problem)
|
|
119
|
-
raise ArgumentError, "#{self} #{"#{declaration} #{arguments.map(&:inspect).join(
|
|
147
|
+
raise ArgumentError, "#{self} #{"#{declaration} #{arguments.map(&:inspect).join(', ')}".rstrip}: #{problem}"
|
|
120
148
|
end
|
|
121
149
|
|
|
122
150
|
# @rbs value: untyped
|
|
@@ -126,7 +154,7 @@ module Skyward::ORM::Tables::Declarations
|
|
|
126
154
|
# @rbs columns: Array[untyped]
|
|
127
155
|
# @rbs return: String?
|
|
128
156
|
def unnamed(columns)
|
|
129
|
-
if columns.empty? then
|
|
157
|
+
if columns.empty? then 'it needs at least one column'
|
|
130
158
|
elsif !columns.all?(Symbol) then "a column's name must be a Symbol, such as :title"
|
|
131
159
|
end
|
|
132
160
|
end
|
|
@@ -138,7 +166,7 @@ module Skyward::ORM::Tables::Declarations
|
|
|
138
166
|
if !table.is_a?(Class) || !(table < Skyward::ORM::Record)
|
|
139
167
|
"it references #{table.inspect}, which isn't a table class"
|
|
140
168
|
elsif !on_delete.nil? && !ON_DELETE.include?(on_delete)
|
|
141
|
-
"on_delete: is #{on_delete.inspect}, but it can only be :#{ON_DELETE.join(
|
|
169
|
+
"on_delete: is #{on_delete.inspect}, but it can only be :#{ON_DELETE.join(', :')} or nil"
|
|
142
170
|
end
|
|
143
171
|
end
|
|
144
172
|
|
|
@@ -146,11 +174,11 @@ module Skyward::ORM::Tables::Declarations
|
|
|
146
174
|
# @rbs length: untyped
|
|
147
175
|
# @rbs return: Skyward::ORM::Tables::SQL | Skyward::ORM::Tables::Length | String
|
|
148
176
|
def check_rule(sql, length)
|
|
149
|
-
if sql && length then
|
|
177
|
+
if sql && length then 'it takes Skyward.sql("...") or length:, not both'
|
|
150
178
|
elsif sql.is_a?(Skyward::ORM::Tables::SQL) then sql
|
|
151
179
|
elsif sql.is_a?(String) then "write the SQL as Skyward.sql(#{sql.inspect}), not a plain String"
|
|
152
180
|
elsif sql then "it takes Skyward.sql(\"...\") or length:, not #{sql.inspect}"
|
|
153
|
-
elsif length.nil? then
|
|
181
|
+
elsif length.nil? then 'it needs Skyward.sql("...") or length:'
|
|
154
182
|
else length_rule(length)
|
|
155
183
|
end
|
|
156
184
|
end
|