sequel 5.39.0 → 5.72.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 +408 -0
- data/MIT-LICENSE +1 -1
- data/README.rdoc +59 -27
- data/bin/sequel +11 -3
- data/doc/advanced_associations.rdoc +16 -14
- data/doc/association_basics.rdoc +119 -24
- data/doc/cheat_sheet.rdoc +11 -3
- data/doc/mass_assignment.rdoc +1 -1
- data/doc/migration.rdoc +13 -6
- data/doc/model_hooks.rdoc +1 -1
- data/doc/object_model.rdoc +8 -8
- data/doc/opening_databases.rdoc +26 -12
- data/doc/postgresql.rdoc +16 -8
- data/doc/querying.rdoc +5 -3
- data/doc/release_notes/5.40.0.txt +40 -0
- data/doc/release_notes/5.41.0.txt +25 -0
- data/doc/release_notes/5.42.0.txt +136 -0
- data/doc/release_notes/5.43.0.txt +98 -0
- data/doc/release_notes/5.44.0.txt +32 -0
- data/doc/release_notes/5.45.0.txt +34 -0
- data/doc/release_notes/5.46.0.txt +87 -0
- data/doc/release_notes/5.47.0.txt +59 -0
- data/doc/release_notes/5.48.0.txt +14 -0
- data/doc/release_notes/5.49.0.txt +59 -0
- data/doc/release_notes/5.50.0.txt +78 -0
- data/doc/release_notes/5.51.0.txt +47 -0
- 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/release_notes/5.57.0.txt +23 -0
- data/doc/release_notes/5.58.0.txt +31 -0
- data/doc/release_notes/5.59.0.txt +73 -0
- data/doc/release_notes/5.60.0.txt +22 -0
- data/doc/release_notes/5.61.0.txt +43 -0
- data/doc/release_notes/5.62.0.txt +132 -0
- data/doc/release_notes/5.63.0.txt +33 -0
- data/doc/release_notes/5.64.0.txt +50 -0
- data/doc/release_notes/5.65.0.txt +21 -0
- data/doc/release_notes/5.66.0.txt +24 -0
- data/doc/release_notes/5.67.0.txt +32 -0
- data/doc/release_notes/5.68.0.txt +61 -0
- data/doc/release_notes/5.69.0.txt +26 -0
- data/doc/release_notes/5.70.0.txt +35 -0
- data/doc/release_notes/5.71.0.txt +21 -0
- data/doc/release_notes/5.72.0.txt +33 -0
- data/doc/schema_modification.rdoc +1 -1
- data/doc/security.rdoc +9 -9
- data/doc/sharding.rdoc +3 -1
- data/doc/sql.rdoc +28 -16
- data/doc/testing.rdoc +22 -11
- data/doc/transactions.rdoc +6 -6
- data/doc/virtual_rows.rdoc +2 -2
- data/lib/sequel/adapters/ado/access.rb +1 -1
- data/lib/sequel/adapters/ado.rb +17 -17
- data/lib/sequel/adapters/amalgalite.rb +3 -5
- data/lib/sequel/adapters/ibmdb.rb +2 -2
- data/lib/sequel/adapters/jdbc/derby.rb +8 -0
- data/lib/sequel/adapters/jdbc/h2.rb +60 -10
- data/lib/sequel/adapters/jdbc/hsqldb.rb +6 -0
- data/lib/sequel/adapters/jdbc/postgresql.rb +7 -4
- data/lib/sequel/adapters/jdbc.rb +16 -18
- data/lib/sequel/adapters/mysql.rb +92 -67
- data/lib/sequel/adapters/mysql2.rb +54 -49
- data/lib/sequel/adapters/odbc.rb +6 -2
- data/lib/sequel/adapters/oracle.rb +4 -3
- data/lib/sequel/adapters/postgres.rb +83 -40
- data/lib/sequel/adapters/shared/access.rb +11 -1
- data/lib/sequel/adapters/shared/db2.rb +30 -0
- data/lib/sequel/adapters/shared/mssql.rb +90 -9
- data/lib/sequel/adapters/shared/mysql.rb +47 -2
- data/lib/sequel/adapters/shared/oracle.rb +82 -1
- data/lib/sequel/adapters/shared/postgres.rb +496 -178
- data/lib/sequel/adapters/shared/sqlanywhere.rb +11 -1
- data/lib/sequel/adapters/shared/sqlite.rb +116 -11
- data/lib/sequel/adapters/sqlanywhere.rb +1 -1
- data/lib/sequel/adapters/sqlite.rb +60 -18
- data/lib/sequel/adapters/tinytds.rb +1 -1
- data/lib/sequel/adapters/trilogy.rb +117 -0
- data/lib/sequel/adapters/utils/columns_limit_1.rb +22 -0
- data/lib/sequel/adapters/utils/mysql_mysql2.rb +1 -1
- data/lib/sequel/ast_transformer.rb +6 -0
- data/lib/sequel/connection_pool/sharded_single.rb +5 -7
- data/lib/sequel/connection_pool/sharded_threaded.rb +16 -11
- data/lib/sequel/connection_pool/sharded_timed_queue.rb +374 -0
- data/lib/sequel/connection_pool/single.rb +6 -8
- data/lib/sequel/connection_pool/threaded.rb +14 -8
- data/lib/sequel/connection_pool/timed_queue.rb +270 -0
- data/lib/sequel/connection_pool.rb +55 -31
- data/lib/sequel/core.rb +28 -18
- data/lib/sequel/database/connecting.rb +27 -3
- data/lib/sequel/database/dataset.rb +16 -6
- data/lib/sequel/database/misc.rb +69 -14
- data/lib/sequel/database/query.rb +73 -2
- data/lib/sequel/database/schema_generator.rb +46 -53
- data/lib/sequel/database/schema_methods.rb +18 -2
- data/lib/sequel/dataset/actions.rb +108 -14
- data/lib/sequel/dataset/deprecated_singleton_class_methods.rb +42 -0
- data/lib/sequel/dataset/features.rb +20 -0
- data/lib/sequel/dataset/misc.rb +12 -2
- data/lib/sequel/dataset/placeholder_literalizer.rb +20 -9
- data/lib/sequel/dataset/prepared_statements.rb +2 -0
- data/lib/sequel/dataset/query.rb +171 -44
- data/lib/sequel/dataset/sql.rb +182 -47
- data/lib/sequel/dataset.rb +4 -0
- data/lib/sequel/extensions/_model_pg_row.rb +0 -12
- data/lib/sequel/extensions/_pretty_table.rb +1 -1
- data/lib/sequel/extensions/any_not_empty.rb +1 -1
- data/lib/sequel/extensions/async_thread_pool.rb +439 -0
- data/lib/sequel/extensions/auto_literal_strings.rb +1 -1
- data/lib/sequel/extensions/blank.rb +8 -0
- data/lib/sequel/extensions/connection_expiration.rb +15 -9
- data/lib/sequel/extensions/connection_validator.rb +16 -11
- data/lib/sequel/extensions/constraint_validations.rb +1 -1
- data/lib/sequel/extensions/core_refinements.rb +36 -11
- data/lib/sequel/extensions/date_arithmetic.rb +71 -31
- 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/duplicate_columns_handler.rb +1 -1
- data/lib/sequel/extensions/eval_inspect.rb +2 -0
- data/lib/sequel/extensions/index_caching.rb +5 -1
- data/lib/sequel/extensions/inflector.rb +9 -1
- data/lib/sequel/extensions/is_distinct_from.rb +141 -0
- data/lib/sequel/extensions/looser_typecasting.rb +3 -0
- data/lib/sequel/extensions/migration.rb +11 -2
- data/lib/sequel/extensions/named_timezones.rb +26 -6
- data/lib/sequel/extensions/pagination.rb +1 -1
- data/lib/sequel/extensions/pg_array.rb +32 -4
- data/lib/sequel/extensions/pg_array_ops.rb +2 -2
- data/lib/sequel/extensions/pg_auto_parameterize.rb +509 -0
- data/lib/sequel/extensions/pg_auto_parameterize_in_array.rb +110 -0
- data/lib/sequel/extensions/pg_enum.rb +2 -3
- data/lib/sequel/extensions/pg_extended_date_support.rb +38 -27
- data/lib/sequel/extensions/pg_extended_integer_support.rb +116 -0
- data/lib/sequel/extensions/pg_hstore.rb +6 -1
- data/lib/sequel/extensions/pg_hstore_ops.rb +53 -3
- data/lib/sequel/extensions/pg_inet.rb +10 -11
- data/lib/sequel/extensions/pg_inet_ops.rb +1 -1
- data/lib/sequel/extensions/pg_interval.rb +45 -19
- data/lib/sequel/extensions/pg_json.rb +13 -15
- data/lib/sequel/extensions/pg_json_ops.rb +73 -2
- data/lib/sequel/extensions/pg_loose_count.rb +3 -1
- data/lib/sequel/extensions/pg_multirange.rb +367 -0
- data/lib/sequel/extensions/pg_range.rb +11 -24
- data/lib/sequel/extensions/pg_range_ops.rb +37 -9
- data/lib/sequel/extensions/pg_row.rb +21 -19
- data/lib/sequel/extensions/pg_row_ops.rb +1 -1
- data/lib/sequel/extensions/query.rb +2 -0
- data/lib/sequel/extensions/s.rb +2 -1
- data/lib/sequel/extensions/schema_caching.rb +1 -1
- data/lib/sequel/extensions/schema_dumper.rb +45 -11
- data/lib/sequel/extensions/server_block.rb +10 -13
- data/lib/sequel/extensions/set_literalizer.rb +58 -0
- data/lib/sequel/extensions/sql_comments.rb +110 -3
- data/lib/sequel/extensions/sql_log_normalizer.rb +108 -0
- data/lib/sequel/extensions/sqlite_json_ops.rb +255 -0
- data/lib/sequel/extensions/string_agg.rb +1 -1
- data/lib/sequel/extensions/string_date_time.rb +19 -23
- data/lib/sequel/extensions/symbol_aref.rb +2 -0
- data/lib/sequel/model/associations.rb +345 -101
- data/lib/sequel/model/base.rb +51 -27
- data/lib/sequel/model/dataset_module.rb +3 -0
- data/lib/sequel/model/errors.rb +10 -1
- data/lib/sequel/model/inflections.rb +1 -1
- data/lib/sequel/model/plugins.rb +5 -0
- data/lib/sequel/plugins/association_proxies.rb +2 -0
- data/lib/sequel/plugins/async_thread_pool.rb +39 -0
- data/lib/sequel/plugins/auto_restrict_eager_graph.rb +62 -0
- data/lib/sequel/plugins/auto_validations.rb +87 -15
- data/lib/sequel/plugins/auto_validations_constraint_validations_presence_message.rb +68 -0
- data/lib/sequel/plugins/class_table_inheritance.rb +2 -2
- data/lib/sequel/plugins/column_encryption.rb +728 -0
- data/lib/sequel/plugins/composition.rb +10 -4
- data/lib/sequel/plugins/concurrent_eager_loading.rb +174 -0
- data/lib/sequel/plugins/constraint_validations.rb +10 -6
- data/lib/sequel/plugins/dataset_associations.rb +4 -1
- data/lib/sequel/plugins/defaults_setter.rb +16 -0
- data/lib/sequel/plugins/dirty.rb +1 -1
- data/lib/sequel/plugins/enum.rb +124 -0
- data/lib/sequel/plugins/finder.rb +4 -2
- data/lib/sequel/plugins/insert_conflict.rb +4 -0
- data/lib/sequel/plugins/instance_specific_default.rb +1 -1
- data/lib/sequel/plugins/json_serializer.rb +39 -24
- data/lib/sequel/plugins/lazy_attributes.rb +3 -0
- data/lib/sequel/plugins/list.rb +3 -1
- data/lib/sequel/plugins/many_through_many.rb +109 -10
- data/lib/sequel/plugins/mssql_optimistic_locking.rb +8 -38
- data/lib/sequel/plugins/nested_attributes.rb +12 -7
- data/lib/sequel/plugins/optimistic_locking.rb +9 -42
- data/lib/sequel/plugins/optimistic_locking_base.rb +55 -0
- data/lib/sequel/plugins/pg_array_associations.rb +56 -38
- data/lib/sequel/plugins/pg_auto_constraint_validations.rb +11 -3
- data/lib/sequel/plugins/pg_xmin_optimistic_locking.rb +109 -0
- data/lib/sequel/plugins/prepared_statements.rb +12 -2
- data/lib/sequel/plugins/prepared_statements_safe.rb +2 -1
- data/lib/sequel/plugins/primary_key_lookup_check_values.rb +154 -0
- data/lib/sequel/plugins/rcte_tree.rb +27 -19
- data/lib/sequel/plugins/require_valid_schema.rb +67 -0
- data/lib/sequel/plugins/serialization.rb +9 -3
- data/lib/sequel/plugins/serialization_modification_detection.rb +2 -1
- data/lib/sequel/plugins/single_table_inheritance.rb +8 -0
- data/lib/sequel/plugins/sql_comments.rb +189 -0
- data/lib/sequel/plugins/static_cache.rb +39 -1
- data/lib/sequel/plugins/static_cache_cache.rb +5 -1
- data/lib/sequel/plugins/subclasses.rb +28 -11
- data/lib/sequel/plugins/tactical_eager_loading.rb +23 -10
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/unused_associations.rb +521 -0
- data/lib/sequel/plugins/update_or_create.rb +1 -1
- data/lib/sequel/plugins/validate_associated.rb +22 -12
- data/lib/sequel/plugins/validation_helpers.rb +46 -12
- data/lib/sequel/plugins/validation_helpers_generic_type_messages.rb +73 -0
- data/lib/sequel/plugins/xml_serializer.rb +1 -1
- data/lib/sequel/sql.rb +1 -1
- data/lib/sequel/timezones.rb +12 -14
- data/lib/sequel/version.rb +1 -1
- metadata +132 -38
data/lib/sequel/database/misc.rb
CHANGED
@@ -91,11 +91,23 @@ module Sequel
|
|
91
91
|
# The specific default size of string columns for this Sequel::Database, usually 255 by default.
|
92
92
|
attr_accessor :default_string_column_size
|
93
93
|
|
94
|
+
# Whether to check the bytesize of strings before typecasting (to avoid typecasting strings that
|
95
|
+
# would be too long for the given type), true by default. Strings that are too long will raise
|
96
|
+
# a typecasting error.
|
97
|
+
attr_accessor :check_string_typecast_bytesize
|
98
|
+
|
94
99
|
# Constructs a new instance of a database connection with the specified
|
95
100
|
# options hash.
|
96
101
|
#
|
97
102
|
# Accepts the following options:
|
103
|
+
# :after_connect :: A callable object called after each new connection is made, with the
|
104
|
+
# connection object (and server argument if the callable accepts 2 arguments),
|
105
|
+
# useful for customizations that you want to apply to all connections.
|
106
|
+
# :before_preconnect :: Callable that runs after extensions from :preconnect_extensions are loaded,
|
107
|
+
# but before any connections are created.
|
98
108
|
# :cache_schema :: Whether schema should be cached for this Database instance
|
109
|
+
# :check_string_typecast_bytesize :: Whether to check the bytesize of strings before typecasting.
|
110
|
+
# :connect_sqls :: An array of sql strings to execute on each new connection, after :after_connect runs.
|
99
111
|
# :default_string_column_size :: The default size of string columns, 255 by default.
|
100
112
|
# :extensions :: Extensions to load into this Database instance. Can be a symbol, array of symbols,
|
101
113
|
# or string with extensions separated by columns. These extensions are loaded after
|
@@ -105,7 +117,7 @@ module Sequel
|
|
105
117
|
# :loggers :: An array of loggers to use.
|
106
118
|
# :log_connection_info :: Whether connection information should be logged when logging queries.
|
107
119
|
# :log_warn_duration :: The number of elapsed seconds after which queries should be logged at warn level.
|
108
|
-
# :name :: A name to use for the Database object, displayed in PoolTimeout
|
120
|
+
# :name :: A name to use for the Database object, displayed in PoolTimeout.
|
109
121
|
# :preconnect :: Automatically create the maximum number of connections, so that they don't
|
110
122
|
# need to be created as needed. This is useful when connecting takes a long time
|
111
123
|
# and you want to avoid possible latency during runtime.
|
@@ -114,13 +126,15 @@ module Sequel
|
|
114
126
|
# :preconnect_extensions :: Similar to the :extensions option, but loads the extensions before the
|
115
127
|
# connections are made by the :preconnect option.
|
116
128
|
# :quote_identifiers :: Whether to quote identifiers.
|
117
|
-
# :servers :: A hash specifying a server/shard specific options, keyed by shard symbol
|
129
|
+
# :servers :: A hash specifying a server/shard specific options, keyed by shard symbol.
|
118
130
|
# :single_threaded :: Whether to use a single-threaded connection pool.
|
119
131
|
# :sql_log_level :: Method to use to log SQL to a logger, :info by default.
|
120
132
|
#
|
133
|
+
# For sharded connection pools, :after_connect and :connect_sqls can be specified per-shard.
|
134
|
+
#
|
121
135
|
# All options given are also passed to the connection pool. Additional options respected by
|
122
|
-
# the connection pool are :
|
123
|
-
#
|
136
|
+
# the connection pool are :max_connections, :pool_timeout, :servers, and :servers_hash. See the
|
137
|
+
# connection pool documentation for details.
|
124
138
|
def initialize(opts = OPTS)
|
125
139
|
@opts ||= opts
|
126
140
|
@opts = connection_pool_default_options.merge(@opts)
|
@@ -130,6 +144,7 @@ module Sequel
|
|
130
144
|
@opts[:adapter_class] = self.class
|
131
145
|
@opts[:single_threaded] = @single_threaded = typecast_value_boolean(@opts.fetch(:single_threaded, Sequel.single_threaded))
|
132
146
|
@default_string_column_size = @opts[:default_string_column_size] || DEFAULT_STRING_COLUMN_SIZE
|
147
|
+
@check_string_typecast_bytesize = typecast_value_boolean(@opts.fetch(:check_string_typecast_bytesize, true))
|
133
148
|
|
134
149
|
@schemas = {}
|
135
150
|
@prepared_statements = {}
|
@@ -160,6 +175,10 @@ module Sequel
|
|
160
175
|
|
161
176
|
initialize_load_extensions(:preconnect_extensions)
|
162
177
|
|
178
|
+
if before_preconnect = @opts[:before_preconnect]
|
179
|
+
before_preconnect.call(self)
|
180
|
+
end
|
181
|
+
|
163
182
|
if typecast_value_boolean(@opts[:preconnect]) && @pool.respond_to?(:preconnect, true)
|
164
183
|
concurrent = typecast_value_string(@opts[:preconnect]) == "concurrently"
|
165
184
|
@pool.send(:preconnect, concurrent)
|
@@ -213,8 +232,7 @@ module Sequel
|
|
213
232
|
Sequel.extension(*exts)
|
214
233
|
exts.each do |ext|
|
215
234
|
if pr = Sequel.synchronize{EXTENSIONS[ext]}
|
216
|
-
|
217
|
-
Sequel.synchronize{@loaded_extensions << ext}
|
235
|
+
if Sequel.synchronize{@loaded_extensions.include?(ext) ? false : (@loaded_extensions << ext)}
|
218
236
|
pr.call(self)
|
219
237
|
end
|
220
238
|
else
|
@@ -460,6 +478,21 @@ module Sequel
|
|
460
478
|
# Don't rescue other exceptions, they will be raised normally.
|
461
479
|
end
|
462
480
|
|
481
|
+
# Check the bytesize of a string before conversion. There is no point
|
482
|
+
# trying to typecast strings that would be way too long.
|
483
|
+
def typecast_check_string_length(string, max_size)
|
484
|
+
if @check_string_typecast_bytesize && string.bytesize > max_size
|
485
|
+
raise InvalidValue, "string too long to typecast (bytesize: #{string.bytesize}, max: #{max_size})"
|
486
|
+
end
|
487
|
+
string
|
488
|
+
end
|
489
|
+
|
490
|
+
# Check the bytesize of the string value, if value is a string.
|
491
|
+
def typecast_check_length(value, max_size)
|
492
|
+
typecast_check_string_length(value, max_size) if String === value
|
493
|
+
value
|
494
|
+
end
|
495
|
+
|
463
496
|
# Typecast the value to an SQL::Blob
|
464
497
|
def typecast_value_blob(value)
|
465
498
|
value.is_a?(Sequel::SQL::Blob) ? value : Sequel::SQL::Blob.new(value)
|
@@ -483,9 +516,9 @@ module Sequel
|
|
483
516
|
when Date
|
484
517
|
value
|
485
518
|
when String
|
486
|
-
Sequel.string_to_date(value)
|
519
|
+
Sequel.string_to_date(typecast_check_string_length(value, 100))
|
487
520
|
when Hash
|
488
|
-
Date.new(*[:year, :month, :day].map{|x| (value[x] || value[x.to_s]).to_i})
|
521
|
+
Date.new(*[:year, :month, :day].map{|x| typecast_check_length(value[x] || value[x.to_s], 100).to_i})
|
489
522
|
else
|
490
523
|
raise InvalidValue, "invalid value for Date: #{value.inspect}"
|
491
524
|
end
|
@@ -493,7 +526,17 @@ module Sequel
|
|
493
526
|
|
494
527
|
# Typecast the value to a DateTime or Time depending on Sequel.datetime_class
|
495
528
|
def typecast_value_datetime(value)
|
496
|
-
|
529
|
+
case value
|
530
|
+
when String
|
531
|
+
Sequel.typecast_to_application_timestamp(typecast_check_string_length(value, 100))
|
532
|
+
when Hash
|
533
|
+
[:year, :month, :day, :hour, :minute, :second, :nanos, :offset].each do |x|
|
534
|
+
typecast_check_length(value[x] || value[x.to_s], 100)
|
535
|
+
end
|
536
|
+
Sequel.typecast_to_application_timestamp(value)
|
537
|
+
else
|
538
|
+
Sequel.typecast_to_application_timestamp(value)
|
539
|
+
end
|
497
540
|
end
|
498
541
|
|
499
542
|
if RUBY_VERSION >= '2.4'
|
@@ -526,18 +569,30 @@ module Sequel
|
|
526
569
|
when Numeric
|
527
570
|
BigDecimal(value.to_s)
|
528
571
|
when String
|
529
|
-
_typecast_value_string_to_decimal(value)
|
572
|
+
_typecast_value_string_to_decimal(typecast_check_string_length(value, 1000))
|
530
573
|
else
|
531
574
|
raise InvalidValue, "invalid value for BigDecimal: #{value.inspect}"
|
532
575
|
end
|
533
576
|
end
|
534
577
|
|
535
578
|
# Typecast the value to a Float
|
536
|
-
|
579
|
+
def typecast_value_float(value)
|
580
|
+
Float(typecast_check_length(value, 1000))
|
581
|
+
end
|
537
582
|
|
538
583
|
# Typecast the value to an Integer
|
539
584
|
def typecast_value_integer(value)
|
540
|
-
|
585
|
+
case value
|
586
|
+
when String
|
587
|
+
typecast_check_string_length(value, 100)
|
588
|
+
if value =~ /\A-?0+(\d)/
|
589
|
+
Integer(value, 10)
|
590
|
+
else
|
591
|
+
Integer(value)
|
592
|
+
end
|
593
|
+
else
|
594
|
+
Integer(value)
|
595
|
+
end
|
541
596
|
end
|
542
597
|
|
543
598
|
# Typecast the value to a String
|
@@ -560,9 +615,9 @@ module Sequel
|
|
560
615
|
SQLTime.create(value.hour, value.min, value.sec, value.nsec/1000.0)
|
561
616
|
end
|
562
617
|
when String
|
563
|
-
Sequel.string_to_time(value)
|
618
|
+
Sequel.string_to_time(typecast_check_string_length(value, 100))
|
564
619
|
when Hash
|
565
|
-
SQLTime.create(*[:hour, :minute, :second].map{|x| (value[x] || value[x.to_s]).to_i})
|
620
|
+
SQLTime.create(*[:hour, :minute, :second].map{|x| typecast_check_length(value[x] || value[x.to_s], 100).to_i})
|
566
621
|
else
|
567
622
|
raise Sequel::InvalidValue, "invalid value for Time: #{value.inspect}"
|
568
623
|
end
|
@@ -175,6 +175,15 @@ module Sequel
|
|
175
175
|
if !c[:max_length] && c[:type] == :string && (max_length = column_schema_max_length(c[:db_type]))
|
176
176
|
c[:max_length] = max_length
|
177
177
|
end
|
178
|
+
if !c[:max_value] && !c[:min_value]
|
179
|
+
min_max = case c[:type]
|
180
|
+
when :integer
|
181
|
+
column_schema_integer_min_max_values(c)
|
182
|
+
when :decimal
|
183
|
+
column_schema_decimal_min_max_values(c)
|
184
|
+
end
|
185
|
+
c[:min_value], c[:max_value] = min_max if min_max
|
186
|
+
end
|
178
187
|
end
|
179
188
|
schema_post_process(cols)
|
180
189
|
|
@@ -236,7 +245,7 @@ module Sequel
|
|
236
245
|
when :date
|
237
246
|
Sequel.string_to_date(default)
|
238
247
|
when :datetime
|
239
|
-
|
248
|
+
Sequel.string_to_datetime(default)
|
240
249
|
when :time
|
241
250
|
Sequel.string_to_time(default)
|
242
251
|
when :decimal
|
@@ -272,6 +281,68 @@ module Sequel
|
|
272
281
|
column_schema_default_to_ruby_value(default, type) rescue nil
|
273
282
|
end
|
274
283
|
|
284
|
+
INTEGER1_MIN_MAX = [-128, 127].freeze
|
285
|
+
INTEGER2_MIN_MAX = [-32768, 32767].freeze
|
286
|
+
INTEGER3_MIN_MAX = [-8388608, 8388607].freeze
|
287
|
+
INTEGER4_MIN_MAX = [-2147483648, 2147483647].freeze
|
288
|
+
INTEGER8_MIN_MAX = [-9223372036854775808, 9223372036854775807].freeze
|
289
|
+
UNSIGNED_INTEGER1_MIN_MAX = [0, 255].freeze
|
290
|
+
UNSIGNED_INTEGER2_MIN_MAX = [0, 65535].freeze
|
291
|
+
UNSIGNED_INTEGER3_MIN_MAX = [0, 16777215].freeze
|
292
|
+
UNSIGNED_INTEGER4_MIN_MAX = [0, 4294967295].freeze
|
293
|
+
UNSIGNED_INTEGER8_MIN_MAX = [0, 18446744073709551615].freeze
|
294
|
+
|
295
|
+
# Look at the db_type and guess the minimum and maximum integer values for
|
296
|
+
# the column.
|
297
|
+
def column_schema_integer_min_max_values(column)
|
298
|
+
db_type = column[:db_type]
|
299
|
+
if /decimal|numeric|number/i =~ db_type
|
300
|
+
if min_max = column_schema_decimal_min_max_values(column)
|
301
|
+
min_max.map!(&:to_i)
|
302
|
+
end
|
303
|
+
return min_max
|
304
|
+
end
|
305
|
+
|
306
|
+
unsigned = /unsigned/i =~ db_type
|
307
|
+
case db_type
|
308
|
+
when /big|int8/i
|
309
|
+
unsigned ? UNSIGNED_INTEGER8_MIN_MAX : INTEGER8_MIN_MAX
|
310
|
+
when /medium/i
|
311
|
+
unsigned ? UNSIGNED_INTEGER3_MIN_MAX : INTEGER3_MIN_MAX
|
312
|
+
when /small|int2/i
|
313
|
+
unsigned ? UNSIGNED_INTEGER2_MIN_MAX : INTEGER2_MIN_MAX
|
314
|
+
when /tiny/i
|
315
|
+
(unsigned || column_schema_tinyint_type_is_unsigned?) ? UNSIGNED_INTEGER1_MIN_MAX : INTEGER1_MIN_MAX
|
316
|
+
else
|
317
|
+
unsigned ? UNSIGNED_INTEGER4_MIN_MAX : INTEGER4_MIN_MAX
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
# Look at the db_type and guess the minimum and maximum decimal values for
|
322
|
+
# the column.
|
323
|
+
def column_schema_decimal_min_max_values(column)
|
324
|
+
if column[:column_size] && column[:scale]
|
325
|
+
precision = column[:column_size]
|
326
|
+
scale = column[:scale]
|
327
|
+
elsif /\((\d+)(?:,\s*(-?\d+))?\)/ =~ column[:db_type]
|
328
|
+
precision = $1.to_i
|
329
|
+
scale = $2.to_i if $2
|
330
|
+
end
|
331
|
+
|
332
|
+
if precision
|
333
|
+
limit = BigDecimal("9" * precision)
|
334
|
+
if scale
|
335
|
+
limit /= 10**(scale)
|
336
|
+
end
|
337
|
+
[-limit, limit]
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
# Whether the tinyint type (if supported by the database) is unsigned by default.
|
342
|
+
def column_schema_tinyint_type_is_unsigned?
|
343
|
+
false
|
344
|
+
end
|
345
|
+
|
275
346
|
# Look at the db_type and guess the maximum length of the column.
|
276
347
|
# This assumes types such as varchar(255).
|
277
348
|
def column_schema_max_length(db_type)
|
@@ -333,7 +404,7 @@ module Sequel
|
|
333
404
|
:boolean
|
334
405
|
when /\A(real|float( unsigned)?|double( precision)?|double\(\d+,\d+\)( unsigned)?)\z/io
|
335
406
|
:float
|
336
|
-
when /\A(?:(?:(?:num(?:ber|eric)?|decimal)(?:\(\d+,\s*(
|
407
|
+
when /\A(?:(?:(?:num(?:ber|eric)?|decimal)(?:\(\d+,\s*(-?\d+|false|true)\))?))\z/io
|
337
408
|
$1 && ['0', 'false'].include?($1) ? :integer : :decimal
|
338
409
|
when /bytea|blob|image|(var)?binary/io
|
339
410
|
:blob
|
@@ -146,6 +146,9 @@ module Sequel
|
|
146
146
|
#
|
147
147
|
# :generated_type :: Set the type of column when using :generated_always_as,
|
148
148
|
# should be :virtual or :stored to force a type.
|
149
|
+
# :on_update_current_timestamp :: Use ON UPDATE CURRENT TIMESTAMP when defining the column,
|
150
|
+
# which will update the column value to CURRENT_TIMESTAMP
|
151
|
+
# on every UPDATE.
|
149
152
|
#
|
150
153
|
# Microsoft SQL Server specific options:
|
151
154
|
#
|
@@ -159,7 +162,7 @@ module Sequel
|
|
159
162
|
nil
|
160
163
|
end
|
161
164
|
|
162
|
-
# Adds a named constraint (or unnamed if name is nil),
|
165
|
+
# Adds a named CHECK constraint (or unnamed if name is nil),
|
163
166
|
# with the given block or args. To provide options for the constraint, pass
|
164
167
|
# a hash as the first argument.
|
165
168
|
#
|
@@ -167,6 +170,15 @@ module Sequel
|
|
167
170
|
# # CONSTRAINT blah CHECK num >= 1 AND num <= 5
|
168
171
|
# constraint({name: :blah, deferrable: true}, num: 1..5)
|
169
172
|
# # CONSTRAINT blah CHECK num >= 1 AND num <= 5 DEFERRABLE INITIALLY DEFERRED
|
173
|
+
#
|
174
|
+
# If the first argument is a hash, the following options are supported:
|
175
|
+
#
|
176
|
+
# Options:
|
177
|
+
# :name :: The name of the CHECK constraint
|
178
|
+
# :deferrable :: Whether the CHECK constraint should be marked DEFERRABLE.
|
179
|
+
#
|
180
|
+
# PostgreSQL specific options:
|
181
|
+
# :not_valid :: Whether the CHECK constraint should be marked NOT VALID.
|
170
182
|
def constraint(name, *args, &block)
|
171
183
|
opts = name.is_a?(Hash) ? name : {:name=>name}
|
172
184
|
constraints << opts.merge(:type=>:check, :check=>block || args)
|
@@ -205,14 +217,12 @@ module Sequel
|
|
205
217
|
end
|
206
218
|
|
207
219
|
# Add a full text index on the given columns.
|
220
|
+
# See #index for additional options.
|
208
221
|
#
|
209
222
|
# PostgreSQL specific options:
|
210
223
|
# :index_type :: Can be set to :gist to use a GIST index instead of the
|
211
224
|
# default GIN index.
|
212
225
|
# :language :: Set a language to use for the index (default: simple).
|
213
|
-
#
|
214
|
-
# Microsoft SQL Server specific options:
|
215
|
-
# :key_index :: The KEY INDEX to use for the full text index.
|
216
226
|
def full_text_index(columns, opts = OPTS)
|
217
227
|
index(columns, opts.merge(:type => :full_text))
|
218
228
|
end
|
@@ -222,35 +232,44 @@ module Sequel
|
|
222
232
|
columns.any?{|c| c[:name] == name}
|
223
233
|
end
|
224
234
|
|
225
|
-
# Add an index on the given column(s) with the given options.
|
235
|
+
# Add an index on the given column(s) with the given options. Examples:
|
236
|
+
#
|
237
|
+
# index :name
|
238
|
+
# # CREATE INDEX table_name_index ON table (name)
|
239
|
+
#
|
240
|
+
# index [:artist_id, :name]
|
241
|
+
# # CREATE INDEX table_artist_id_name_index ON table (artist_id, name)
|
242
|
+
#
|
243
|
+
# index [:artist_id, :name], name: :foo
|
244
|
+
# # CREATE INDEX foo ON table (artist_id, name)
|
245
|
+
#
|
226
246
|
# General options:
|
227
247
|
#
|
248
|
+
# :include :: Include additional column values in the index, without
|
249
|
+
# actually indexing on those values (only supported by
|
250
|
+
# some databases).
|
228
251
|
# :name :: The name to use for the index. If not given, a default name
|
229
252
|
# based on the table and columns is used.
|
230
|
-
# :type :: The type of index to use (only supported by some databases
|
253
|
+
# :type :: The type of index to use (only supported by some databases,
|
254
|
+
# :full_text and :spatial values are handled specially).
|
231
255
|
# :unique :: Make the index unique, so duplicate values are not allowed.
|
232
|
-
# :where ::
|
256
|
+
# :where :: A filter expression, used to create a partial index (only
|
257
|
+
# supported by some databases).
|
233
258
|
#
|
234
259
|
# PostgreSQL specific options:
|
235
260
|
#
|
236
261
|
# :concurrently :: Create the index concurrently, so it doesn't block
|
237
262
|
# operations on the table while the index is being
|
238
263
|
# built.
|
239
|
-
# :
|
240
|
-
# :
|
241
|
-
#
|
264
|
+
# :if_not_exists :: Only create the index if an index of the same name doesn't already exist.
|
265
|
+
# :nulls_distinct :: Set whether separate NULLs should be considered distinct values in unique indexes.
|
266
|
+
# :opclass :: Set an opclass to use for all columns (per-column opclasses require
|
267
|
+
# custom SQL).
|
242
268
|
# :tablespace :: Specify tablespace for index.
|
243
269
|
#
|
244
270
|
# Microsoft SQL Server specific options:
|
245
271
|
#
|
246
|
-
# :
|
247
|
-
# actually indexing on those values.
|
248
|
-
#
|
249
|
-
# index :name
|
250
|
-
# # CREATE INDEX table_name_index ON table (name)
|
251
|
-
#
|
252
|
-
# index [:artist_id, :name]
|
253
|
-
# # CREATE INDEX table_artist_id_name_index ON table (artist_id, name)
|
272
|
+
# :key_index :: Sets the KEY INDEX to the given value.
|
254
273
|
def index(columns, opts = OPTS)
|
255
274
|
indexes << {:columns => Array(columns)}.merge!(opts)
|
256
275
|
nil
|
@@ -288,7 +307,7 @@ module Sequel
|
|
288
307
|
# Examples:
|
289
308
|
# primary_key(:id)
|
290
309
|
# primary_key(:id, type: :Bignum, keep_order: true)
|
291
|
-
# primary_key([:street_number, :house_number], name: :
|
310
|
+
# primary_key([:street_number, :house_number], name: :some_constraint_name)
|
292
311
|
def primary_key(name, *args)
|
293
312
|
return composite_primary_key(name, *args) if name.is_a?(Array)
|
294
313
|
column = @db.serial_primary_key_options.merge({:name => name})
|
@@ -316,6 +335,7 @@ module Sequel
|
|
316
335
|
end
|
317
336
|
|
318
337
|
# Add a spatial index on the given columns.
|
338
|
+
# See #index for additional options.
|
319
339
|
def spatial_index(columns, opts = OPTS)
|
320
340
|
index(columns, opts.merge(:type => :spatial))
|
321
341
|
end
|
@@ -371,8 +391,7 @@ module Sequel
|
|
371
391
|
end
|
372
392
|
|
373
393
|
# Add a column with the given name, type, and opts.
|
374
|
-
# See CreateTableGenerator#column for the available options
|
375
|
-
# separate +add_index+ call to add an index for the column).
|
394
|
+
# See CreateTableGenerator#column for the available options.
|
376
395
|
#
|
377
396
|
# add_column(:name, String) # ADD COLUMN name varchar(255)
|
378
397
|
#
|
@@ -385,7 +404,10 @@ module Sequel
|
|
385
404
|
# :after :: The name of an existing column that the new column should be positioned after
|
386
405
|
# :first :: Create this new column before all other existing columns
|
387
406
|
def add_column(name, type, opts = OPTS)
|
388
|
-
|
407
|
+
op = {:op => :add_column, :name => name, :type => type}.merge!(opts)
|
408
|
+
index_opts = op.delete(:index)
|
409
|
+
@operations << op
|
410
|
+
add_index(name, index_opts.is_a?(Hash) ? index_opts : OPTS) if index_opts
|
389
411
|
nil
|
390
412
|
end
|
391
413
|
|
@@ -414,8 +436,7 @@ module Sequel
|
|
414
436
|
end
|
415
437
|
|
416
438
|
# Add a foreign key with the given name and referencing the given table.
|
417
|
-
# See CreateTableGenerator#column for the available options
|
418
|
-
# separate +add_index+ call to add an index for the column).
|
439
|
+
# See CreateTableGenerator#column for the available options.
|
419
440
|
#
|
420
441
|
# You can also pass an array of column names for creating composite foreign
|
421
442
|
# keys. In this case, it will assume the columns exist and will only add
|
@@ -442,7 +463,7 @@ module Sequel
|
|
442
463
|
end
|
443
464
|
|
444
465
|
# Add a full text index on the given columns.
|
445
|
-
# See CreateTableGenerator#
|
466
|
+
# See CreateTableGenerator#full_text_index for available options.
|
446
467
|
def add_full_text_index(columns, opts = OPTS)
|
447
468
|
add_index(columns, {:type=>:full_text}.merge!(opts))
|
448
469
|
end
|
@@ -451,34 +472,6 @@ module Sequel
|
|
451
472
|
# CreateTableGenerator#index for available options.
|
452
473
|
#
|
453
474
|
# add_index(:artist_id) # CREATE INDEX table_artist_id_index ON table (artist_id)
|
454
|
-
#
|
455
|
-
# Options:
|
456
|
-
#
|
457
|
-
# :name :: Give a specific name for the index. Highly recommended if you plan on
|
458
|
-
# dropping the index later.
|
459
|
-
# :where :: A filter expression, used to setup a partial index (if supported).
|
460
|
-
# :unique :: Create a unique index.
|
461
|
-
#
|
462
|
-
# PostgreSQL specific options:
|
463
|
-
#
|
464
|
-
# :concurrently :: Create the index concurrently, so it doesn't require an exclusive lock
|
465
|
-
# on the table.
|
466
|
-
# :index_type :: The underlying index type to use for a full_text index, gin by default).
|
467
|
-
# :language :: The language to use for a full text index (simple by default).
|
468
|
-
# :opclass :: Set an opclass to use for all columns (per-column opclasses require
|
469
|
-
# custom SQL).
|
470
|
-
# :type :: Set the index type (e.g. full_text, spatial, hash, gin, gist, btree).
|
471
|
-
# :if_not_exists :: Only create the index if an index of the same name doesn't already exists
|
472
|
-
#
|
473
|
-
# MySQL specific options:
|
474
|
-
#
|
475
|
-
# :type :: Set the index type, with full_text and spatial indexes handled specially.
|
476
|
-
#
|
477
|
-
# Microsoft SQL Server specific options:
|
478
|
-
#
|
479
|
-
# :include :: Includes additional columns in the index.
|
480
|
-
# :key_index :: Sets the KEY INDEX to the given value.
|
481
|
-
# :type :: clustered uses a clustered index, full_text uses a full text index.
|
482
475
|
def add_index(columns, opts = OPTS)
|
483
476
|
@operations << {:op => :add_index, :columns => Array(columns)}.merge!(opts)
|
484
477
|
nil
|
@@ -63,7 +63,7 @@ module Sequel
|
|
63
63
|
# definitions using <tt>create_table</tt>, and +add_index+ accepts all the options
|
64
64
|
# available for index definition.
|
65
65
|
#
|
66
|
-
# See <tt>Schema::AlterTableGenerator</tt> and the {
|
66
|
+
# See <tt>Schema::AlterTableGenerator</tt> and the {Migrations guide}[rdoc-ref:doc/migration.rdoc].
|
67
67
|
def alter_table(name, &block)
|
68
68
|
generator = alter_table_generator(&block)
|
69
69
|
remove_cached_schema(name)
|
@@ -183,6 +183,15 @@ module Sequel
|
|
183
183
|
# keys.
|
184
184
|
# :tablespace :: The tablespace to use for the table.
|
185
185
|
#
|
186
|
+
# SQLite specific options:
|
187
|
+
# :strict :: Create a STRICT table, which checks that the values for the columns
|
188
|
+
# are the correct type (similar to all other SQL databases). Note that
|
189
|
+
# when using this option, all column types used should be one of the
|
190
|
+
# following: +int+, +integer+, +real+, +text+, +blob+, and +any+.
|
191
|
+
# The +any+ type is treated like a SQLite column in a non-strict table,
|
192
|
+
# allowing any type of data to be stored. This option is supported on
|
193
|
+
# SQLite 3.37.0+.
|
194
|
+
#
|
186
195
|
# See <tt>Schema::CreateTableGenerator</tt> and the {"Schema Modification" guide}[rdoc-ref:doc/schema_modification.rdoc].
|
187
196
|
def create_table(name, options=OPTS, &block)
|
188
197
|
remove_cached_schema(name)
|
@@ -262,6 +271,10 @@ module Sequel
|
|
262
271
|
# # SELECT * FROM items WHERE foo
|
263
272
|
# # WITH CHECK OPTION
|
264
273
|
#
|
274
|
+
# DB.create_view(:bar_items, DB[:items].select(:foo), columns: [:bar])
|
275
|
+
# # CREATE VIEW bar_items (bar) AS
|
276
|
+
# # SELECT foo FROM items
|
277
|
+
#
|
265
278
|
# Options:
|
266
279
|
# :columns :: The column names to use for the view. If not given,
|
267
280
|
# automatically determined based on the input dataset.
|
@@ -282,6 +295,9 @@ module Sequel
|
|
282
295
|
# in a subquery, if you are providing a Dataset as the source
|
283
296
|
# argument, if should probably call the union method with the
|
284
297
|
# all: true and from_self: false options.
|
298
|
+
# :security_invoker :: Set the security_invoker property on the view, making
|
299
|
+
# the access to the view use the current user's permissions,
|
300
|
+
# instead of the view owner's permissions.
|
285
301
|
# :tablespace :: The tablespace to use for materialized views.
|
286
302
|
def create_view(name, source, options = OPTS)
|
287
303
|
execute_ddl(create_view_sql(name, source, options))
|
@@ -884,7 +900,7 @@ module Sequel
|
|
884
900
|
#
|
885
901
|
# Any other object given is just converted to a string, with "_" converted to " " and upcased.
|
886
902
|
def on_delete_clause(action)
|
887
|
-
action.to_s.
|
903
|
+
action.to_s.tr("_", " ").upcase
|
888
904
|
end
|
889
905
|
|
890
906
|
# Alias of #on_delete_clause, since the two usually behave the same.
|