sequel 5.51.0 → 5.56.0
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/CHANGELOG +62 -0
- data/MIT-LICENSE +1 -1
- data/README.rdoc +5 -0
- data/doc/opening_databases.rdoc +4 -1
- data/doc/querying.rdoc +3 -1
- data/doc/release_notes/5.52.0.txt +87 -0
- data/doc/release_notes/5.53.0.txt +23 -0
- data/doc/release_notes/5.54.0.txt +27 -0
- data/doc/release_notes/5.55.0.txt +21 -0
- data/doc/release_notes/5.56.0.txt +51 -0
- data/doc/sql.rdoc +1 -1
- data/doc/testing.rdoc +3 -1
- data/lib/sequel/adapters/amalgalite.rb +3 -5
- data/lib/sequel/adapters/jdbc/h2.rb +55 -10
- data/lib/sequel/adapters/jdbc.rb +12 -14
- data/lib/sequel/adapters/mysql.rb +80 -67
- data/lib/sequel/adapters/mysql2.rb +53 -48
- data/lib/sequel/adapters/postgres.rb +17 -21
- data/lib/sequel/adapters/shared/mysql.rb +3 -2
- data/lib/sequel/adapters/shared/postgres.rb +2 -2
- data/lib/sequel/adapters/shared/sqlite.rb +6 -0
- data/lib/sequel/adapters/sqlite.rb +60 -18
- data/lib/sequel/adapters/utils/mysql_mysql2.rb +1 -1
- data/lib/sequel/connection_pool/sharded_single.rb +5 -7
- data/lib/sequel/connection_pool/single.rb +6 -8
- data/lib/sequel/core.rb +17 -18
- data/lib/sequel/database/query.rb +1 -1
- data/lib/sequel/database/schema_generator.rb +6 -5
- data/lib/sequel/database/schema_methods.rb +9 -0
- data/lib/sequel/dataset/sql.rb +3 -2
- data/lib/sequel/extensions/core_refinements.rb +36 -11
- data/lib/sequel/extensions/date_parse_input_handler.rb +67 -0
- data/lib/sequel/extensions/datetime_parse_to_time.rb +5 -1
- data/lib/sequel/extensions/pg_array_ops.rb +2 -2
- data/lib/sequel/extensions/pg_hstore_ops.rb +1 -1
- data/lib/sequel/extensions/pg_inet_ops.rb +1 -1
- data/lib/sequel/extensions/pg_interval.rb +1 -0
- data/lib/sequel/extensions/pg_json.rb +3 -5
- data/lib/sequel/extensions/pg_json_ops.rb +3 -2
- data/lib/sequel/extensions/pg_range_ops.rb +1 -1
- data/lib/sequel/extensions/pg_row_ops.rb +1 -1
- data/lib/sequel/extensions/s.rb +2 -1
- data/lib/sequel/extensions/schema_dumper.rb +2 -2
- data/lib/sequel/extensions/server_block.rb +8 -12
- data/lib/sequel/extensions/sql_comments.rb +110 -3
- data/lib/sequel/extensions/sqlite_json_ops.rb +255 -0
- data/lib/sequel/extensions/string_date_time.rb +19 -23
- data/lib/sequel/model/base.rb +8 -12
- data/lib/sequel/plugins/auto_restrict_eager_graph.rb +62 -0
- data/lib/sequel/plugins/column_encryption.rb +1 -1
- data/lib/sequel/plugins/enum.rb +124 -0
- data/lib/sequel/plugins/instance_specific_default.rb +1 -1
- data/lib/sequel/plugins/sql_comments.rb +189 -0
- data/lib/sequel/plugins/subclasses.rb +28 -11
- data/lib/sequel/plugins/unused_associations.rb +2 -2
- data/lib/sequel/timezones.rb +12 -14
- data/lib/sequel/version.rb +1 -1
- metadata +21 -6
@@ -0,0 +1,255 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
#
|
3
|
+
# The sqlite_json_ops extension adds support to Sequel's DSL to make
|
4
|
+
# it easier to call SQLite JSON functions and operators (added
|
5
|
+
# first in SQLite 3.38.0).
|
6
|
+
#
|
7
|
+
# To load the extension:
|
8
|
+
#
|
9
|
+
# Sequel.extension :sqlite_json_ops
|
10
|
+
#
|
11
|
+
# This extension works by calling methods on Sequel::SQLite::JSONOp objects,
|
12
|
+
# which you can create via Sequel.sqlite_json_op:
|
13
|
+
#
|
14
|
+
# j = Sequel.sqlite_json_op(:json_column)
|
15
|
+
#
|
16
|
+
# Also, on most Sequel expression objects, you can call the sqlite_json_op method
|
17
|
+
# to create a Sequel::SQLite::JSONOp object:
|
18
|
+
#
|
19
|
+
# j = Sequel[:json_column].sqlite_json_op
|
20
|
+
#
|
21
|
+
# If you have loaded the {core_extensions extension}[rdoc-ref:doc/core_extensions.rdoc],
|
22
|
+
# or you have loaded the core_refinements extension
|
23
|
+
# and have activated refinements for the file, you can also use Symbol#sqlite_json_op:
|
24
|
+
#
|
25
|
+
# j = :json_column.sqlite_json_op
|
26
|
+
#
|
27
|
+
# The following methods are available for Sequel::SQLite::JSONOp instances:
|
28
|
+
#
|
29
|
+
# j[1] # (json_column ->> 1)
|
30
|
+
# j.get(1) # (json_column ->> 1)
|
31
|
+
# j.get_text(1) # (json_column -> 1)
|
32
|
+
# j.extract('$.a') # json_extract(json_column, '$.a')
|
33
|
+
#
|
34
|
+
# j.array_length # json_array_length(json_column)
|
35
|
+
# j.type # json_type(json_column)
|
36
|
+
# j.valid # json_valid(json_column)
|
37
|
+
# j.json # json(json_column)
|
38
|
+
#
|
39
|
+
# j.insert('$.a', 1) # json_insert(json_column, '$.a', 1)
|
40
|
+
# j.set('$.a', 1) # json_set(json_column, '$.a', 1)
|
41
|
+
# j.replace('$.a', 1) # json_replace(json_column, '$.a', 1)
|
42
|
+
# j.remove('$.a') # json_remove(json_column, '$.a')
|
43
|
+
# j.patch('{"a":2}') # json_patch(json_column, '{"a":2}')
|
44
|
+
#
|
45
|
+
# j.each # json_each(json_column)
|
46
|
+
# j.tree # json_tree(json_column)
|
47
|
+
#
|
48
|
+
# Related modules: Sequel::SQLite::JSONOp
|
49
|
+
|
50
|
+
#
|
51
|
+
module Sequel
|
52
|
+
module SQLite
|
53
|
+
# The JSONOp class is a simple container for a single object that
|
54
|
+
# defines methods that yield Sequel expression objects representing
|
55
|
+
# SQLite json operators and functions.
|
56
|
+
#
|
57
|
+
# In the method documentation examples, assume that:
|
58
|
+
#
|
59
|
+
# json_op = Sequel.sqlite_json_op(:json)
|
60
|
+
class JSONOp < Sequel::SQL::Wrapper
|
61
|
+
GET = ["(".freeze, " ->> ".freeze, ")".freeze].freeze
|
62
|
+
private_constant :GET
|
63
|
+
|
64
|
+
GET_JSON = ["(".freeze, " -> ".freeze, ")".freeze].freeze
|
65
|
+
private_constant :GET_JSON
|
66
|
+
|
67
|
+
# Returns an expression for getting the JSON array element or object field
|
68
|
+
# at the specified path as a SQLite value.
|
69
|
+
#
|
70
|
+
# json_op[1] # (json ->> 1)
|
71
|
+
# json_op['a'] # (json ->> 'a')
|
72
|
+
# json_op['$.a.b'] # (json ->> '$.a.b')
|
73
|
+
# json_op['$[1][2]'] # (json ->> '$[1][2]')
|
74
|
+
def [](key)
|
75
|
+
json_op(GET, key)
|
76
|
+
end
|
77
|
+
alias get []
|
78
|
+
|
79
|
+
# Returns an expression for the length of the JSON array, or the JSON array at
|
80
|
+
# the given path.
|
81
|
+
#
|
82
|
+
# json_op.array_length # json_array_length(json)
|
83
|
+
# json_op.array_length('$[1]') # json_array_length(json, '$[1]')
|
84
|
+
def array_length(*args)
|
85
|
+
Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length, *args))
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns an expression for a set of information extracted from the top-level
|
89
|
+
# members of the JSON array or object, or the top-level members of the JSON array
|
90
|
+
# or object at the given path.
|
91
|
+
#
|
92
|
+
# json_op.each # json_each(json)
|
93
|
+
# json_op.each('$.a') # json_each(json, '$.a')
|
94
|
+
def each(*args)
|
95
|
+
function(:each, *args)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Returns an expression for the JSON array element or object field at the specified
|
99
|
+
# path as a SQLite value, but only accept paths as arguments, and allow the use of
|
100
|
+
# multiple paths.
|
101
|
+
#
|
102
|
+
# json_op.extract('$.a') # json_extract(json, '$.a')
|
103
|
+
# json_op.extract('$.a', '$.b') # json_extract(json, '$.a', '$.b')
|
104
|
+
def extract(*a)
|
105
|
+
function(:extract, *a)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Returns an expression for getting the JSON array element or object field at the
|
109
|
+
# specified path as a JSON value.
|
110
|
+
#
|
111
|
+
# json_op.get_json(1) # (json -> 1)
|
112
|
+
# json_op.get_json('a') # (json -> 'a')
|
113
|
+
# json_op.get_json('$.a.b') # (json -> '$.a.b')
|
114
|
+
# json_op.get_json('$[1][2]') # (json -> '$[1][2]')
|
115
|
+
def get_json(key)
|
116
|
+
self.class.new(json_op(GET_JSON, key))
|
117
|
+
end
|
118
|
+
|
119
|
+
# Returns an expression for creating new entries at the given paths in the JSON array
|
120
|
+
# or object, but not overwriting existing entries.
|
121
|
+
#
|
122
|
+
# json_op.insert('$.a', 1) # json_insert(json, '$.a', 1)
|
123
|
+
# json_op.insert('$.a', 1, '$.b', 2) # json_insert(json, '$.a', 1, '$.b', 2)
|
124
|
+
def insert(path, value, *args)
|
125
|
+
wrapped_function(:insert, path, value, *args)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Returns an expression for a minified version of the JSON.
|
129
|
+
#
|
130
|
+
# json_op.json # json(json)
|
131
|
+
def json
|
132
|
+
self.class.new(SQL::Function.new(:json, self))
|
133
|
+
end
|
134
|
+
alias minify json
|
135
|
+
|
136
|
+
# Returns an expression for updating the JSON object using the RFC 7396 MergePatch algorithm
|
137
|
+
#
|
138
|
+
# json_op.patch('{"a": 1, "b": null}') # json_patch(json, '{"a": 1, "b": null}')
|
139
|
+
def patch(json_patch)
|
140
|
+
wrapped_function(:patch, json_patch)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Returns an expression for removing entries at the given paths from the JSON array or object.
|
144
|
+
#
|
145
|
+
# json_op.remove('$.a') # json_remove(json, '$.a')
|
146
|
+
# json_op.remove('$.a', '$.b') # json_remove(json, '$.a', '$.b')
|
147
|
+
def remove(path, *paths)
|
148
|
+
wrapped_function(:remove, path, *paths)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Returns an expression for replacing entries at the given paths in the JSON array or object,
|
152
|
+
# but not creating new entries.
|
153
|
+
#
|
154
|
+
# json_op.replace('$.a', 1) # json_replace(json, '$.a', 1)
|
155
|
+
# json_op.replace('$.a', 1, '$.b', 2) # json_replace(json, '$.a', 1, '$.b', 2)
|
156
|
+
def replace(path, value, *args)
|
157
|
+
wrapped_function(:replace, path, value, *args)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Returns an expression for creating or replacing entries at the given paths in the
|
161
|
+
# JSON array or object.
|
162
|
+
#
|
163
|
+
# json_op.set('$.a', 1) # json_set(json, '$.a', 1)
|
164
|
+
# json_op.set('$.a', 1, '$.b', 2) # json_set(json, '$.a', 1, '$.b', 2)
|
165
|
+
def set(path, value, *args)
|
166
|
+
wrapped_function(:set, path, value, *args)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Returns an expression for a set of information extracted from the JSON array or object, or
|
170
|
+
# the JSON array or object at the given path.
|
171
|
+
#
|
172
|
+
# json_op.tree # json_tree(json)
|
173
|
+
# json_op.tree('$.a') # json_tree(json, '$.a')
|
174
|
+
def tree(*args)
|
175
|
+
function(:tree, *args)
|
176
|
+
end
|
177
|
+
|
178
|
+
# Returns an expression for the type of the JSON value or the JSON value at the given path.
|
179
|
+
#
|
180
|
+
# json_op.type # json_type(json)
|
181
|
+
# json_op.type('$[1]') # json_type(json, '$[1]')
|
182
|
+
def type(*args)
|
183
|
+
Sequel::SQL::StringExpression.new(:NOOP, function(:type, *args))
|
184
|
+
end
|
185
|
+
alias typeof type
|
186
|
+
|
187
|
+
# Returns a boolean expression for whether the JSON is valid or not.
|
188
|
+
def valid
|
189
|
+
Sequel::SQL::BooleanExpression.new(:NOOP, function(:valid))
|
190
|
+
end
|
191
|
+
|
192
|
+
private
|
193
|
+
|
194
|
+
# Internals of the [], get, get_json methods, using a placeholder literal string.
|
195
|
+
def json_op(str, args)
|
196
|
+
self.class.new(Sequel::SQL::PlaceholderLiteralString.new(str, [self, args]))
|
197
|
+
end
|
198
|
+
|
199
|
+
# Internals of the methods that return functions prefixed with +json_+.
|
200
|
+
def function(name, *args)
|
201
|
+
SQL::Function.new("json_#{name}", self, *args)
|
202
|
+
end
|
203
|
+
|
204
|
+
# Internals of the methods that return functions prefixed with +json_+, that
|
205
|
+
# return JSON values.
|
206
|
+
def wrapped_function(*args)
|
207
|
+
self.class.new(function(*args))
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
module JSONOpMethods
|
212
|
+
# Wrap the receiver in an JSONOp so you can easily use the SQLite
|
213
|
+
# json functions and operators with it.
|
214
|
+
def sqlite_json_op
|
215
|
+
JSONOp.new(self)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
module SQL::Builders
|
221
|
+
# Return the object wrapped in an SQLite::JSONOp.
|
222
|
+
def sqlite_json_op(v)
|
223
|
+
case v
|
224
|
+
when SQLite::JSONOp
|
225
|
+
v
|
226
|
+
else
|
227
|
+
SQLite::JSONOp.new(v)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
class SQL::GenericExpression
|
233
|
+
include Sequel::SQLite::JSONOpMethods
|
234
|
+
end
|
235
|
+
|
236
|
+
class LiteralString
|
237
|
+
include Sequel::SQLite::JSONOpMethods
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
# :nocov:
|
242
|
+
if Sequel.core_extensions?
|
243
|
+
class Symbol
|
244
|
+
include Sequel::SQLite::JSONOpMethods
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
if defined?(Sequel::CoreRefinements)
|
249
|
+
module Sequel::CoreRefinements
|
250
|
+
refine Symbol do
|
251
|
+
send INCLUDE_METH, Sequel::SQLite::JSONOpMethods
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
# :nocov:
|
@@ -4,6 +4,10 @@
|
|
4
4
|
# for converting the strings to a date (e.g. String#to_date), allowing
|
5
5
|
# for backwards compatibility with legacy Sequel code.
|
6
6
|
#
|
7
|
+
# These methods calls +parse+ on the related class, and as such, can
|
8
|
+
# result in denial of service in older versions of Ruby for large
|
9
|
+
# untrusted input, and raise exceptions in newer versions of Ruby.
|
10
|
+
#
|
7
11
|
# To load the extension:
|
8
12
|
#
|
9
13
|
# Sequel.extension :string_date_time
|
@@ -11,42 +15,34 @@
|
|
11
15
|
class String
|
12
16
|
# Converts a string into a Date object.
|
13
17
|
def to_date
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
18
|
-
end
|
18
|
+
Date.parse(self, Sequel.convert_two_digit_years)
|
19
|
+
rescue => e
|
20
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
19
21
|
end
|
20
22
|
|
21
23
|
# Converts a string into a DateTime object.
|
22
24
|
def to_datetime
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
27
|
-
end
|
25
|
+
DateTime.parse(self, Sequel.convert_two_digit_years)
|
26
|
+
rescue => e
|
27
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
28
28
|
end
|
29
29
|
|
30
30
|
# Converts a string into a Time or DateTime object, depending on the
|
31
31
|
# value of Sequel.datetime_class
|
32
32
|
def to_sequel_time
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
Sequel.datetime_class.parse(self)
|
38
|
-
end
|
39
|
-
rescue => e
|
40
|
-
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
33
|
+
if Sequel.datetime_class == DateTime
|
34
|
+
DateTime.parse(self, Sequel.convert_two_digit_years)
|
35
|
+
else
|
36
|
+
Sequel.datetime_class.parse(self)
|
41
37
|
end
|
38
|
+
rescue => e
|
39
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
42
40
|
end
|
43
41
|
|
44
42
|
# Converts a string into a Time object.
|
45
43
|
def to_time
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
50
|
-
end
|
44
|
+
Time.parse(self)
|
45
|
+
rescue => e
|
46
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
51
47
|
end
|
52
48
|
end
|
data/lib/sequel/model/base.rb
CHANGED
@@ -682,13 +682,11 @@ module Sequel
|
|
682
682
|
|
683
683
|
# Yield to the passed block and if do_raise is false, swallow all errors other than DatabaseConnectionErrors.
|
684
684
|
def check_non_connection_error(do_raise=require_valid_table)
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
raise if do_raise
|
691
|
-
end
|
685
|
+
db.transaction(:savepoint=>:only){yield}
|
686
|
+
rescue Sequel::DatabaseConnectionError
|
687
|
+
raise
|
688
|
+
rescue Sequel::Error
|
689
|
+
raise if do_raise
|
692
690
|
end
|
693
691
|
|
694
692
|
# Convert the given object to a Dataset that should be used as
|
@@ -1630,11 +1628,9 @@ module Sequel
|
|
1630
1628
|
# artist.set(name: 'Invalid').valid? # => false
|
1631
1629
|
# artist.errors.full_messages # => ['name cannot be Invalid']
|
1632
1630
|
def valid?(opts = OPTS)
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
false
|
1637
|
-
end
|
1631
|
+
_valid?(opts)
|
1632
|
+
rescue HookFailed
|
1633
|
+
false
|
1638
1634
|
end
|
1639
1635
|
|
1640
1636
|
private
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Plugins
|
5
|
+
# The auto_restrict_eager_graph plugin will automatically disallow the use
|
6
|
+
# of eager_graph for associations that have associated blocks but no :graph_*
|
7
|
+
# association options. The reason for this is the block will have an effect
|
8
|
+
# during regular and eager loading, but not loading via eager_graph, and it
|
9
|
+
# is likely that whatever the block is doing should have an equivalent done
|
10
|
+
# when eager_graphing. Most likely, not including a :graph_* option was either
|
11
|
+
# an oversight (and one should be added), or use with eager_graph was never
|
12
|
+
# intended (and usage should be forbidden). Disallowing eager_graph in this
|
13
|
+
# case prevents likely unexpected behavior during eager_graph.
|
14
|
+
#
|
15
|
+
# As an example of this, consider the following code:
|
16
|
+
#
|
17
|
+
# Album.one_to_many :popular_tracks, class: :Track do |ds|
|
18
|
+
# ds = ds.where(popular: true)
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# Album.eager(:popular_tracks).all
|
22
|
+
# # SELECT * FROM albums
|
23
|
+
# # SELECT * FROM tracks WHERE ((popular IS TRUE) AND (album_id IN (...)))
|
24
|
+
#
|
25
|
+
# # Notice that no condition for tracks.popular is added.
|
26
|
+
# Album.eager_graph(:popular_tracks).all
|
27
|
+
# # SELECT ... FROM albums LEFT JOIN tracks ON (tracks.album_id = albums.id)
|
28
|
+
#
|
29
|
+
# With the auto_restrict_eager_graph plugin, the eager_graph call above will
|
30
|
+
# raise an error, alerting you to the fact that you either should not be
|
31
|
+
# using eager_graph with the association, or that you should be adding an
|
32
|
+
# appropriate :graph_* option, such as:
|
33
|
+
#
|
34
|
+
# Album.one_to_many :popular_tracks, class: :Track, graph_conditions: {popular: true} do |ds|
|
35
|
+
# ds = ds.where(popular: true)
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# Usage:
|
39
|
+
#
|
40
|
+
# # Automatically restrict eager_graph for associations if appropriate for all
|
41
|
+
# # model subclasses (called before loading subclasses)
|
42
|
+
# Sequel::Model.plugin :auto_restrict_eager_graph
|
43
|
+
#
|
44
|
+
# # Automatically restrict eager_graph for associations in Album class
|
45
|
+
# Album.plugin :auto_restrict_eager_graph
|
46
|
+
module AutoRestrictEagerGraph
|
47
|
+
module ClassMethods
|
48
|
+
# When defining an association, if a block is given for the association, but
|
49
|
+
# a :graph_* option is not used, disallow the use of eager_graph.
|
50
|
+
def associate(type, name, opts = OPTS, &block)
|
51
|
+
opts = super
|
52
|
+
|
53
|
+
if opts[:block] && !opts.has_key?(:allow_eager_graph) && !opts[:orig_opts].any?{|k,| /\Agraph_/ =~ k}
|
54
|
+
opts[:allow_eager_graph] = false
|
55
|
+
end
|
56
|
+
|
57
|
+
opts
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -356,7 +356,7 @@ module Sequel
|
|
356
356
|
|
357
357
|
# Keys should be an array of arrays containing key_id, key string, auth_data, and padding.
|
358
358
|
def initialize(keys)
|
359
|
-
if keys.empty?
|
359
|
+
if !keys || keys.empty?
|
360
360
|
raise Error, "Cannot initialize encryptor without encryption key"
|
361
361
|
end
|
362
362
|
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Plugins
|
5
|
+
# The enum plugin allows for easily adding methods to modify the value of
|
6
|
+
# a column. It allows treating the column itself as an enum, returning a
|
7
|
+
# symbol for the related enum value. It also allows for setting up dataset
|
8
|
+
# methods to easily find records having or not having each enum value.
|
9
|
+
#
|
10
|
+
# After loading the plugin, you can call the +enum+ method to define the
|
11
|
+
# methods. The +enum+ method accepts a symbol for the underlying
|
12
|
+
# database column, and a hash with symbol keys for the enum values.
|
13
|
+
# For example, the following call:
|
14
|
+
#
|
15
|
+
# Album.enum :status_id, good: 1, bad: 2
|
16
|
+
#
|
17
|
+
# Will define the following instance methods:
|
18
|
+
#
|
19
|
+
# Album#good! :: Change +status_id+ to +1+ (does not save the receiver)
|
20
|
+
# Album#bad! :: Change +status_id+ to +2+ (does not save the receiver)
|
21
|
+
# Album#good? :: Return whether +status_id+ is +1+
|
22
|
+
# Album#bad? :: Return whether +status_id+ is +2+
|
23
|
+
#
|
24
|
+
# It will override the following instance methods:
|
25
|
+
#
|
26
|
+
# Album#status_id :: Return +:good+/+:bad+ instead of +1+/+2+ (other values returned as-is)
|
27
|
+
# Album#status_id= :: Allow calling with +:good+/+:bad+ to set +status_id+ to +1+/+2+ (other values,
|
28
|
+
# such as <tt>'good'</tt>/<tt>'bad'</tt> set as-is)
|
29
|
+
#
|
30
|
+
# If will define the following dataset methods:
|
31
|
+
#
|
32
|
+
# Album.dataset.good :: Return a dataset filtered to rows where +status_id+ is +1+
|
33
|
+
# Album.dataset.not_good :: Return a dataset filtered to rows where +status_id+ is not +1+
|
34
|
+
# Album.dataset.bad:: Return a dataset filtered to rows where +status_id+ is +2+
|
35
|
+
# Album.dataset.not_bad:: Return a dataset filtered to rows where +status_id+ is not +2+
|
36
|
+
#
|
37
|
+
# When calling +enum+, you can also provide the following options:
|
38
|
+
#
|
39
|
+
# :prefix :: Use a prefix for methods defined for each enum value. If +true+ is provided at the value, use the column name as the prefix.
|
40
|
+
# For example, with <tt>prefix: 'status'</tt>, the instance methods defined above would be +status_good?+, +status_bad?+,
|
41
|
+
# +status_good!+, and +status_bad!+, and the dataset methods defined would be +status_good+, +status_not_good+, +status_bad+,
|
42
|
+
# and +status_not_bad+.
|
43
|
+
# :suffix :: Use a suffix for methods defined for each enum value. If +true+ is provided at the value, use the column name as the suffix.
|
44
|
+
# For example, with <tt>suffix: 'status'</tt>, the instance methods defined above would be +good_status?+, +bad_status?+,
|
45
|
+
# +good_status!+, and +bad_status!+, and the dataset methods defined would be +good_status+, +not_good_status+, +bad_status+,
|
46
|
+
# and +not_bad_status+.
|
47
|
+
# :override_accessors :: Set to +false+ to not override the column accessor methods.
|
48
|
+
# :dataset_methods :: Set to +false+ to not define dataset methods.
|
49
|
+
#
|
50
|
+
# Note that this does not use a true enum column in the database. If you are
|
51
|
+
# looking for enum support in the database, and your are using PostgreSQL,
|
52
|
+
# Sequel supports that via the pg_enum Database extension.
|
53
|
+
#
|
54
|
+
# Usage:
|
55
|
+
#
|
56
|
+
# # Make all model subclasses handle enums
|
57
|
+
# Sequel::Model.plugin :enum
|
58
|
+
#
|
59
|
+
# # Make the Album class handle enums
|
60
|
+
# Album.plugin :enum
|
61
|
+
module Enum
|
62
|
+
module ClassMethods
|
63
|
+
# Define instance and dataset methods in this class to treat column
|
64
|
+
# as a enum. See Enum documentation for usage.
|
65
|
+
def enum(column, values, opts=OPTS)
|
66
|
+
raise Sequel::Error, "enum column must be a symbol" unless column.is_a?(Symbol)
|
67
|
+
raise Sequel::Error, "enum values must be provided as a hash with symbol keys" unless values.is_a?(Hash) && values.all?{|k,| k.is_a?(Symbol)}
|
68
|
+
|
69
|
+
if prefix = opts[:prefix]
|
70
|
+
prefix = column if prefix == true
|
71
|
+
prefix = "#{prefix}_"
|
72
|
+
end
|
73
|
+
|
74
|
+
if suffix = opts[:suffix]
|
75
|
+
suffix = column if suffix == true
|
76
|
+
suffix = "_#{suffix}"
|
77
|
+
end
|
78
|
+
|
79
|
+
values = Hash[values].freeze
|
80
|
+
inverted = values.invert.freeze
|
81
|
+
|
82
|
+
unless @enum_methods
|
83
|
+
@enum_methods = Module.new
|
84
|
+
include @enum_methods
|
85
|
+
end
|
86
|
+
|
87
|
+
@enum_methods.module_eval do
|
88
|
+
unless opts[:override_accessors] == false
|
89
|
+
define_method(column) do
|
90
|
+
v = super()
|
91
|
+
inverted.fetch(v, v)
|
92
|
+
end
|
93
|
+
|
94
|
+
define_method(:"#{column}=") do |v|
|
95
|
+
super(values.fetch(v, v))
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
values.each do |key, value|
|
100
|
+
define_method(:"#{prefix}#{key}#{suffix}!") do
|
101
|
+
self[column] = value
|
102
|
+
nil
|
103
|
+
end
|
104
|
+
|
105
|
+
define_method(:"#{prefix}#{key}#{suffix}?") do
|
106
|
+
self[column] == value
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
unless opts[:dataset_methods] == false
|
112
|
+
dataset_module do
|
113
|
+
values.each do |key, value|
|
114
|
+
cond = Sequel[column=>value]
|
115
|
+
where :"#{prefix}#{key}#{suffix}", cond
|
116
|
+
where :"#{prefix}not_#{key}#{suffix}", ~cond
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -29,7 +29,7 @@ module Sequel
|
|
29
29
|
# end
|
30
30
|
#
|
31
31
|
# +first_track+ is not instance specific, but +last_track+ and +recent_tracks+ are.
|
32
|
-
# +
|
32
|
+
# +last_track+ is because the +num_tracks+ call in the block is calling
|
33
33
|
# <tt>Album#num_tracks</tt>. +recent_tracks+ is because the value will change over
|
34
34
|
# time. This plugin allows you to find these cases, and set the :instance_specific
|
35
35
|
# option appropriately for them:
|