sequel 5.39.0 → 5.62.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +294 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +57 -25
  5. data/bin/sequel +11 -3
  6. data/doc/advanced_associations.rdoc +13 -13
  7. data/doc/association_basics.rdoc +89 -24
  8. data/doc/cheat_sheet.rdoc +11 -3
  9. data/doc/migration.rdoc +12 -6
  10. data/doc/model_hooks.rdoc +1 -1
  11. data/doc/object_model.rdoc +8 -8
  12. data/doc/opening_databases.rdoc +18 -11
  13. data/doc/postgresql.rdoc +16 -8
  14. data/doc/querying.rdoc +5 -3
  15. data/doc/release_notes/5.40.0.txt +40 -0
  16. data/doc/release_notes/5.41.0.txt +25 -0
  17. data/doc/release_notes/5.42.0.txt +136 -0
  18. data/doc/release_notes/5.43.0.txt +98 -0
  19. data/doc/release_notes/5.44.0.txt +32 -0
  20. data/doc/release_notes/5.45.0.txt +34 -0
  21. data/doc/release_notes/5.46.0.txt +87 -0
  22. data/doc/release_notes/5.47.0.txt +59 -0
  23. data/doc/release_notes/5.48.0.txt +14 -0
  24. data/doc/release_notes/5.49.0.txt +59 -0
  25. data/doc/release_notes/5.50.0.txt +78 -0
  26. data/doc/release_notes/5.51.0.txt +47 -0
  27. data/doc/release_notes/5.52.0.txt +87 -0
  28. data/doc/release_notes/5.53.0.txt +23 -0
  29. data/doc/release_notes/5.54.0.txt +27 -0
  30. data/doc/release_notes/5.55.0.txt +21 -0
  31. data/doc/release_notes/5.56.0.txt +51 -0
  32. data/doc/release_notes/5.57.0.txt +23 -0
  33. data/doc/release_notes/5.58.0.txt +31 -0
  34. data/doc/release_notes/5.59.0.txt +73 -0
  35. data/doc/release_notes/5.60.0.txt +22 -0
  36. data/doc/release_notes/5.61.0.txt +43 -0
  37. data/doc/release_notes/5.62.0.txt +132 -0
  38. data/doc/schema_modification.rdoc +1 -1
  39. data/doc/security.rdoc +9 -9
  40. data/doc/sql.rdoc +27 -15
  41. data/doc/testing.rdoc +22 -11
  42. data/doc/transactions.rdoc +6 -6
  43. data/doc/virtual_rows.rdoc +2 -2
  44. data/lib/sequel/adapters/ado/access.rb +1 -1
  45. data/lib/sequel/adapters/ado.rb +17 -17
  46. data/lib/sequel/adapters/amalgalite.rb +3 -5
  47. data/lib/sequel/adapters/ibmdb.rb +2 -2
  48. data/lib/sequel/adapters/jdbc/derby.rb +8 -0
  49. data/lib/sequel/adapters/jdbc/h2.rb +60 -10
  50. data/lib/sequel/adapters/jdbc/hsqldb.rb +6 -0
  51. data/lib/sequel/adapters/jdbc/postgresql.rb +4 -4
  52. data/lib/sequel/adapters/jdbc.rb +16 -18
  53. data/lib/sequel/adapters/mysql.rb +80 -67
  54. data/lib/sequel/adapters/mysql2.rb +54 -49
  55. data/lib/sequel/adapters/odbc.rb +6 -2
  56. data/lib/sequel/adapters/oracle.rb +3 -3
  57. data/lib/sequel/adapters/postgres.rb +83 -40
  58. data/lib/sequel/adapters/shared/access.rb +11 -1
  59. data/lib/sequel/adapters/shared/db2.rb +30 -0
  60. data/lib/sequel/adapters/shared/mssql.rb +58 -7
  61. data/lib/sequel/adapters/shared/mysql.rb +40 -2
  62. data/lib/sequel/adapters/shared/oracle.rb +76 -0
  63. data/lib/sequel/adapters/shared/postgres.rb +418 -174
  64. data/lib/sequel/adapters/shared/sqlanywhere.rb +10 -0
  65. data/lib/sequel/adapters/shared/sqlite.rb +102 -11
  66. data/lib/sequel/adapters/sqlanywhere.rb +1 -1
  67. data/lib/sequel/adapters/sqlite.rb +60 -18
  68. data/lib/sequel/adapters/tinytds.rb +1 -1
  69. data/lib/sequel/adapters/utils/columns_limit_1.rb +22 -0
  70. data/lib/sequel/adapters/utils/mysql_mysql2.rb +1 -1
  71. data/lib/sequel/ast_transformer.rb +6 -0
  72. data/lib/sequel/connection_pool/sharded_single.rb +5 -7
  73. data/lib/sequel/connection_pool/single.rb +6 -8
  74. data/lib/sequel/connection_pool.rb +42 -28
  75. data/lib/sequel/core.rb +28 -18
  76. data/lib/sequel/database/connecting.rb +26 -2
  77. data/lib/sequel/database/misc.rb +69 -14
  78. data/lib/sequel/database/query.rb +38 -1
  79. data/lib/sequel/database/schema_generator.rb +45 -52
  80. data/lib/sequel/database/schema_methods.rb +17 -1
  81. data/lib/sequel/dataset/actions.rb +82 -13
  82. data/lib/sequel/dataset/features.rb +20 -0
  83. data/lib/sequel/dataset/misc.rb +1 -1
  84. data/lib/sequel/dataset/prepared_statements.rb +2 -0
  85. data/lib/sequel/dataset/query.rb +118 -16
  86. data/lib/sequel/dataset/sql.rb +177 -47
  87. data/lib/sequel/extensions/_model_pg_row.rb +0 -12
  88. data/lib/sequel/extensions/_pretty_table.rb +1 -1
  89. data/lib/sequel/extensions/any_not_empty.rb +1 -1
  90. data/lib/sequel/extensions/async_thread_pool.rb +438 -0
  91. data/lib/sequel/extensions/auto_literal_strings.rb +1 -1
  92. data/lib/sequel/extensions/blank.rb +8 -0
  93. data/lib/sequel/extensions/constraint_validations.rb +1 -1
  94. data/lib/sequel/extensions/core_refinements.rb +36 -11
  95. data/lib/sequel/extensions/date_arithmetic.rb +71 -31
  96. data/lib/sequel/extensions/date_parse_input_handler.rb +67 -0
  97. data/lib/sequel/extensions/datetime_parse_to_time.rb +5 -1
  98. data/lib/sequel/extensions/duplicate_columns_handler.rb +1 -1
  99. data/lib/sequel/extensions/eval_inspect.rb +2 -0
  100. data/lib/sequel/extensions/inflector.rb +9 -1
  101. data/lib/sequel/extensions/is_distinct_from.rb +141 -0
  102. data/lib/sequel/extensions/looser_typecasting.rb +3 -0
  103. data/lib/sequel/extensions/migration.rb +7 -2
  104. data/lib/sequel/extensions/named_timezones.rb +22 -6
  105. data/lib/sequel/extensions/pagination.rb +1 -1
  106. data/lib/sequel/extensions/pg_array.rb +23 -3
  107. data/lib/sequel/extensions/pg_array_ops.rb +2 -2
  108. data/lib/sequel/extensions/pg_auto_parameterize.rb +478 -0
  109. data/lib/sequel/extensions/pg_enum.rb +1 -1
  110. data/lib/sequel/extensions/pg_extended_date_support.rb +28 -25
  111. data/lib/sequel/extensions/pg_extended_integer_support.rb +116 -0
  112. data/lib/sequel/extensions/pg_hstore.rb +6 -1
  113. data/lib/sequel/extensions/pg_hstore_ops.rb +53 -3
  114. data/lib/sequel/extensions/pg_inet.rb +10 -11
  115. data/lib/sequel/extensions/pg_inet_ops.rb +1 -1
  116. data/lib/sequel/extensions/pg_interval.rb +45 -19
  117. data/lib/sequel/extensions/pg_json.rb +13 -15
  118. data/lib/sequel/extensions/pg_json_ops.rb +73 -2
  119. data/lib/sequel/extensions/pg_loose_count.rb +3 -1
  120. data/lib/sequel/extensions/pg_multirange.rb +367 -0
  121. data/lib/sequel/extensions/pg_range.rb +10 -23
  122. data/lib/sequel/extensions/pg_range_ops.rb +37 -9
  123. data/lib/sequel/extensions/pg_row.rb +19 -13
  124. data/lib/sequel/extensions/pg_row_ops.rb +1 -1
  125. data/lib/sequel/extensions/query.rb +2 -0
  126. data/lib/sequel/extensions/s.rb +2 -1
  127. data/lib/sequel/extensions/schema_dumper.rb +13 -2
  128. data/lib/sequel/extensions/server_block.rb +8 -12
  129. data/lib/sequel/extensions/sql_comments.rb +110 -3
  130. data/lib/sequel/extensions/sql_log_normalizer.rb +108 -0
  131. data/lib/sequel/extensions/sqlite_json_ops.rb +255 -0
  132. data/lib/sequel/extensions/string_agg.rb +1 -1
  133. data/lib/sequel/extensions/string_date_time.rb +19 -23
  134. data/lib/sequel/extensions/symbol_aref.rb +2 -0
  135. data/lib/sequel/model/associations.rb +324 -95
  136. data/lib/sequel/model/base.rb +51 -27
  137. data/lib/sequel/model/errors.rb +10 -1
  138. data/lib/sequel/model/inflections.rb +1 -1
  139. data/lib/sequel/model/plugins.rb +5 -0
  140. data/lib/sequel/plugins/association_proxies.rb +2 -0
  141. data/lib/sequel/plugins/async_thread_pool.rb +39 -0
  142. data/lib/sequel/plugins/auto_restrict_eager_graph.rb +62 -0
  143. data/lib/sequel/plugins/auto_validations.rb +87 -15
  144. data/lib/sequel/plugins/auto_validations_constraint_validations_presence_message.rb +68 -0
  145. data/lib/sequel/plugins/class_table_inheritance.rb +2 -2
  146. data/lib/sequel/plugins/column_encryption.rb +728 -0
  147. data/lib/sequel/plugins/composition.rb +10 -4
  148. data/lib/sequel/plugins/concurrent_eager_loading.rb +174 -0
  149. data/lib/sequel/plugins/constraint_validations.rb +2 -1
  150. data/lib/sequel/plugins/dataset_associations.rb +4 -1
  151. data/lib/sequel/plugins/dirty.rb +1 -1
  152. data/lib/sequel/plugins/enum.rb +124 -0
  153. data/lib/sequel/plugins/finder.rb +3 -1
  154. data/lib/sequel/plugins/insert_conflict.rb +4 -0
  155. data/lib/sequel/plugins/instance_specific_default.rb +1 -1
  156. data/lib/sequel/plugins/json_serializer.rb +39 -24
  157. data/lib/sequel/plugins/lazy_attributes.rb +3 -0
  158. data/lib/sequel/plugins/list.rb +3 -1
  159. data/lib/sequel/plugins/many_through_many.rb +108 -9
  160. data/lib/sequel/plugins/nested_attributes.rb +12 -7
  161. data/lib/sequel/plugins/pg_array_associations.rb +56 -38
  162. data/lib/sequel/plugins/pg_auto_constraint_validations.rb +3 -1
  163. data/lib/sequel/plugins/prepared_statements.rb +10 -1
  164. data/lib/sequel/plugins/primary_key_lookup_check_values.rb +154 -0
  165. data/lib/sequel/plugins/rcte_tree.rb +27 -19
  166. data/lib/sequel/plugins/require_valid_schema.rb +67 -0
  167. data/lib/sequel/plugins/serialization.rb +9 -3
  168. data/lib/sequel/plugins/serialization_modification_detection.rb +2 -1
  169. data/lib/sequel/plugins/sql_comments.rb +189 -0
  170. data/lib/sequel/plugins/static_cache.rb +1 -1
  171. data/lib/sequel/plugins/subclasses.rb +28 -11
  172. data/lib/sequel/plugins/tactical_eager_loading.rb +15 -2
  173. data/lib/sequel/plugins/timestamps.rb +1 -1
  174. data/lib/sequel/plugins/unused_associations.rb +521 -0
  175. data/lib/sequel/plugins/update_or_create.rb +1 -1
  176. data/lib/sequel/plugins/validation_helpers.rb +38 -11
  177. data/lib/sequel/plugins/xml_serializer.rb +1 -1
  178. data/lib/sequel/sql.rb +1 -1
  179. data/lib/sequel/timezones.rb +12 -14
  180. data/lib/sequel/version.rb +1 -1
  181. metadata +94 -43
@@ -55,11 +55,11 @@ module Sequel
55
55
 
56
56
  begin
57
57
  db = c.new(opts)
58
- if block_given?
58
+ if defined?(yield)
59
59
  return yield(db)
60
60
  end
61
61
  ensure
62
- if block_given?
62
+ if defined?(yield)
63
63
  db.disconnect if db
64
64
  Sequel.synchronize{::Sequel::DATABASES.delete(db)}
65
65
  end
@@ -241,6 +241,30 @@ module Sequel
241
241
  pool.servers
242
242
  end
243
243
 
244
+ # Connect to the given server/shard. Handles database-generic post-connection
245
+ # setup not handled by #connect, using the :after_connect and :connect_sqls
246
+ # options.
247
+ def new_connection(server)
248
+ conn = connect(server)
249
+ opts = server_opts(server)
250
+
251
+ if ac = opts[:after_connect]
252
+ if ac.arity == 2
253
+ ac.call(conn, server)
254
+ else
255
+ ac.call(conn)
256
+ end
257
+ end
258
+
259
+ if cs = opts[:connect_sqls]
260
+ cs.each do |sql|
261
+ log_connection_execute(conn, sql)
262
+ end
263
+ end
264
+
265
+ conn
266
+ end
267
+
244
268
  # Returns true if the database is using a single-threaded connection pool.
245
269
  def single_threaded?
246
270
  @single_threaded
@@ -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 :after_connect, :connect_sqls, :max_connections, :pool_timeout,
123
- # :servers, and :servers_hash. See the connection pool documentation for details.
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
- unless Sequel.synchronize{@loaded_extensions.include?(ext)}
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
- Sequel.typecast_to_application_timestamp(value)
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
- alias typecast_value_float Float
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
- (value.is_a?(String) && value =~ /\A0+(\d)/) ? Integer(value, 10) : Integer(value)
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,9 @@ 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] && c[:type] == :integer && (min_max = column_schema_integer_min_max_values(c[:db_type]))
179
+ c[:min_value], c[:max_value] = min_max
180
+ end
178
181
  end
179
182
  schema_post_process(cols)
180
183
 
@@ -236,7 +239,7 @@ module Sequel
236
239
  when :date
237
240
  Sequel.string_to_date(default)
238
241
  when :datetime
239
- DateTime.parse(default)
242
+ Sequel.string_to_datetime(default)
240
243
  when :time
241
244
  Sequel.string_to_time(default)
242
245
  when :decimal
@@ -272,6 +275,40 @@ module Sequel
272
275
  column_schema_default_to_ruby_value(default, type) rescue nil
273
276
  end
274
277
 
278
+ INTEGER1_MIN_MAX = [-128, 127].freeze
279
+ INTEGER2_MIN_MAX = [-32768, 32767].freeze
280
+ INTEGER3_MIN_MAX = [-8388608, 8388607].freeze
281
+ INTEGER4_MIN_MAX = [-2147483648, 2147483647].freeze
282
+ INTEGER8_MIN_MAX = [-9223372036854775808, 9223372036854775807].freeze
283
+ UNSIGNED_INTEGER1_MIN_MAX = [0, 255].freeze
284
+ UNSIGNED_INTEGER2_MIN_MAX = [0, 65535].freeze
285
+ UNSIGNED_INTEGER3_MIN_MAX = [0, 16777215].freeze
286
+ UNSIGNED_INTEGER4_MIN_MAX = [0, 4294967295].freeze
287
+ UNSIGNED_INTEGER8_MIN_MAX = [0, 18446744073709551615].freeze
288
+
289
+ # Look at the db_type and guess the minimum and maximum integer values for
290
+ # the column.
291
+ def column_schema_integer_min_max_values(db_type)
292
+ unsigned = /unsigned/i =~ db_type
293
+ case db_type
294
+ when /big|int8/i
295
+ unsigned ? UNSIGNED_INTEGER8_MIN_MAX : INTEGER8_MIN_MAX
296
+ when /medium/i
297
+ unsigned ? UNSIGNED_INTEGER3_MIN_MAX : INTEGER3_MIN_MAX
298
+ when /small|int2/i
299
+ unsigned ? UNSIGNED_INTEGER2_MIN_MAX : INTEGER2_MIN_MAX
300
+ when /tiny/i
301
+ (unsigned || column_schema_tinyint_type_is_unsigned?) ? UNSIGNED_INTEGER1_MIN_MAX : INTEGER1_MIN_MAX
302
+ else
303
+ unsigned ? UNSIGNED_INTEGER4_MIN_MAX : INTEGER4_MIN_MAX
304
+ end
305
+ end
306
+
307
+ # Whether the tinyint type (if supported by the database) is unsigned by default.
308
+ def column_schema_tinyint_type_is_unsigned?
309
+ false
310
+ end
311
+
275
312
  # Look at the db_type and guess the maximum length of the column.
276
313
  # This assumes types such as varchar(255).
277
314
  def column_schema_max_length(db_type)
@@ -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 :: Create a partial index (only supported by some databases)
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
- # :opclass :: Use a specific operator class in the index.
240
- # :include :: Include additional column values in the index, without
241
- # actually indexing on those values (PostgreSQL 11+).
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
- # :include :: Include additional column values in the index, without
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
@@ -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 (except for +:index+, use a
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
- @operations << {:op => :add_column, :name => name, :type => type}.merge!(opts)
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 (except for +:index+, use a
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#index for available options.
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 {"Migrations and Schema Modification" guide}[rdoc-ref:doc/migration.rdoc].
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))
@@ -19,7 +19,7 @@ module Sequel
19
19
  METHS
20
20
 
21
21
  # The clone options to use when retrieving columns for a dataset.
22
- COLUMNS_CLONE_OPTIONS = {:distinct => nil, :limit => 1, :offset=>nil, :where=>nil, :having=>nil, :order=>nil, :row_proc=>nil, :graph=>nil, :eager_graph=>nil}.freeze
22
+ COLUMNS_CLONE_OPTIONS = {:distinct => nil, :limit => 0, :offset=>nil, :where=>nil, :having=>nil, :order=>nil, :row_proc=>nil, :graph=>nil, :eager_graph=>nil}.freeze
23
23
 
24
24
  # Inserts the given argument into the database. Returns self so it
25
25
  # can be used safely when chaining:
@@ -313,14 +313,18 @@ module Sequel
313
313
 
314
314
  # Inserts multiple records into the associated table. This method can be
315
315
  # used to efficiently insert a large number of records into a table in a
316
- # single query if the database supports it. Inserts
317
- # are automatically wrapped in a transaction.
316
+ # single query if the database supports it. Inserts are automatically
317
+ # wrapped in a transaction if necessary.
318
318
  #
319
319
  # This method is called with a columns array and an array of value arrays:
320
320
  #
321
321
  # DB[:table].import([:x, :y], [[1, 2], [3, 4]])
322
322
  # # INSERT INTO table (x, y) VALUES (1, 2)
323
- # # INSERT INTO table (x, y) VALUES (3, 4)
323
+ # # INSERT INTO table (x, y) VALUES (3, 4)
324
+ #
325
+ # or, if the database supports it:
326
+ #
327
+ # # INSERT INTO table (x, y) VALUES (1, 2), (3, 4)
324
328
  #
325
329
  # This method also accepts a dataset instead of an array of value arrays:
326
330
  #
@@ -328,9 +332,13 @@ module Sequel
328
332
  # # INSERT INTO table (x, y) SELECT a, b FROM table2
329
333
  #
330
334
  # Options:
331
- # :commit_every :: Open a new transaction for every given number of records.
332
- # For example, if you provide a value of 50, will commit
333
- # after every 50 records.
335
+ # :commit_every :: Open a new transaction for every given number of
336
+ # records. For example, if you provide a value of 50,
337
+ # will commit after every 50 records. When a
338
+ # transaction is not required, this option controls
339
+ # the maximum number of values to insert with a single
340
+ # statement; it does not force the use of a
341
+ # transaction.
334
342
  # :return :: When this is set to :primary_key, returns an array of
335
343
  # autoincremented primary key values for the rows inserted.
336
344
  # This does not have an effect if +values+ is a Dataset.
@@ -457,6 +465,55 @@ module Sequel
457
465
  _aggregate(:max, arg)
458
466
  end
459
467
 
468
+ # Execute a MERGE statement, which allows for INSERT, UPDATE, and DELETE
469
+ # behavior in a single query, based on whether rows from a source table
470
+ # match rows in the current table, based on the join conditions.
471
+ #
472
+ # Unless the dataset uses static SQL, to use #merge, you must first have
473
+ # called #merge_using to specify the merge source and join conditions.
474
+ # You will then likely to call one or more of the following methods
475
+ # to specify MERGE behavior by adding WHEN [NOT] MATCHED clauses:
476
+ #
477
+ # * #merge_insert
478
+ # * #merge_update
479
+ # * #merge_delete
480
+ #
481
+ # The WHEN [NOT] MATCHED clauses are added to the SQL in the order these
482
+ # methods were called on the dataset. If none of these methods are
483
+ # called, an error is raised.
484
+ #
485
+ # Example:
486
+ #
487
+ # DB[:m1]
488
+ # merge_using(:m2, i1: :i2).
489
+ # merge_insert(i1: :i2, a: Sequel[:b]+11).
490
+ # merge_delete{a > 30}.
491
+ # merge_update(i1: Sequel[:i1]+:i2+10, a: Sequel[:a]+:b+20).
492
+ # merge
493
+ #
494
+ # SQL:
495
+ #
496
+ # MERGE INTO m1 USING m2 ON (i1 = i2)
497
+ # WHEN NOT MATCHED THEN INSERT (i1, a) VALUES (i2, (b + 11))
498
+ # WHEN MATCHED AND (a > 30) THEN DELETE
499
+ # WHEN MATCHED THEN UPDATE SET i1 = (i1 + i2 + 10), a = (a + b + 20)
500
+ #
501
+ # On PostgreSQL, two additional merge methods are supported, for the
502
+ # PostgreSQL-specific DO NOTHING syntax.
503
+ #
504
+ # * #merge_do_nothing_when_matched
505
+ # * #merge_do_nothing_when_not_matched
506
+ #
507
+ # This method is supported on Oracle, but Oracle's MERGE support is
508
+ # non-standard, and has the following issues:
509
+ #
510
+ # * DELETE clause requires UPDATE clause
511
+ # * DELETE clause requires a condition
512
+ # * DELETE clause only affects rows updated by UPDATE clause
513
+ def merge
514
+ execute_ddl(merge_sql)
515
+ end
516
+
460
517
  # Returns the minimum value for the given column/expression.
461
518
  # Uses a virtual row block if no argument is given.
462
519
  #
@@ -527,7 +584,7 @@ module Sequel
527
584
  # # SELECT * FROM table ORDER BY id LIMIT 1000 OFFSET 1000
528
585
  # # ...
529
586
  #
530
- # DB[:table].order(:id).paged_each(:rows_per_fetch=>100){|row| }
587
+ # DB[:table].order(:id).paged_each(rows_per_fetch: 100){|row| }
531
588
  # # SELECT * FROM table ORDER BY id LIMIT 100
532
589
  # # SELECT * FROM table ORDER BY id LIMIT 100 OFFSET 100
533
590
  # # ...
@@ -546,7 +603,7 @@ module Sequel
546
603
  unless @opts[:order]
547
604
  raise Sequel::Error, "Dataset#paged_each requires the dataset be ordered"
548
605
  end
549
- unless block_given?
606
+ unless defined?(yield)
550
607
  return enum_for(:paged_each, opts)
551
608
  end
552
609
 
@@ -974,18 +1031,19 @@ module Sequel
974
1031
 
975
1032
  # Internals of #import. If primary key values are requested, use
976
1033
  # separate insert commands for each row. Otherwise, call #multi_insert_sql
977
- # and execute each statement it gives separately.
1034
+ # and execute each statement it gives separately. A transaction is only used
1035
+ # if there are multiple statements to execute.
978
1036
  def _import(columns, values, opts)
979
1037
  trans_opts = Hash[opts]
980
1038
  trans_opts[:server] = @opts[:server]
981
1039
  if opts[:return] == :primary_key
982
- @db.transaction(trans_opts){values.map{|v| insert(columns, v)}}
1040
+ _import_transaction(values, trans_opts){values.map{|v| insert(columns, v)}}
983
1041
  else
984
1042
  stmts = multi_insert_sql(columns, values)
985
- @db.transaction(trans_opts){stmts.each{|st| execute_dui(st)}}
1043
+ _import_transaction(stmts, trans_opts){stmts.each{|st| execute_dui(st)}}
986
1044
  end
987
1045
  end
988
-
1046
+
989
1047
  # Return an array of arrays of values given by the symbols in ret_cols.
990
1048
  def _select_map_multiple(ret_cols)
991
1049
  map{|r| r.values_at(*ret_cols)}
@@ -1024,6 +1082,17 @@ module Sequel
1024
1082
  end
1025
1083
  end
1026
1084
 
1085
+ # Use a transaction when yielding to the block if multiple values/statements
1086
+ # are provided. When only a single value or statement is provided, then yield
1087
+ # without using a transaction.
1088
+ def _import_transaction(values, trans_opts, &block)
1089
+ if values.length > 1
1090
+ @db.transaction(trans_opts, &block)
1091
+ else
1092
+ yield
1093
+ end
1094
+ end
1095
+
1027
1096
  # Internals of +select_hash+ and +select_hash_groups+
1028
1097
  def _select_hash(meth, key_column, value_column, opts=OPTS)
1029
1098
  select(*(key_column.is_a?(Array) ? key_column : [key_column]) + (value_column.is_a?(Array) ? value_column : [value_column])).