sequel 5.39.0 → 5.72.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen-string-literal: true
|
2
2
|
|
3
|
+
require_relative '../utils/columns_limit_1'
|
4
|
+
|
3
5
|
module Sequel
|
4
6
|
module SqlAnywhere
|
5
7
|
Sequel::Database.set_shared_adapter_scheme(:sqlanywhere, self)
|
@@ -35,7 +37,7 @@ module Sequel
|
|
35
37
|
row[:auto_increment] = auto_increment == 1 || auto_increment == true
|
36
38
|
row[:primary_key] = row.delete(:pkey) == 'Y'
|
37
39
|
row[:allow_null] = row[:nulls_allowed].is_a?(Integer) ? row.delete(:nulls_allowed) == 1 : row.delete(:nulls_allowed)
|
38
|
-
row[:db_type] = row.delete(:
|
40
|
+
row[:db_type] = row.delete(:domain_name_with_size)
|
39
41
|
row[:type] = if row[:db_type] =~ /numeric/i and (row[:scale].is_a?(Integer) ? row[:scale] == 0 : !row[:scale])
|
40
42
|
:integer
|
41
43
|
else
|
@@ -193,6 +195,11 @@ module Sequel
|
|
193
195
|
end
|
194
196
|
end
|
195
197
|
|
198
|
+
# SQLAnywhere tinyint types are unsigned.
|
199
|
+
def column_schema_tinyint_type_is_unsigned?
|
200
|
+
true
|
201
|
+
end
|
202
|
+
|
196
203
|
# SqlAnywhere doesn't support CREATE TABLE AS, it only supports SELECT INTO.
|
197
204
|
# Emulating CREATE TABLE AS using SELECT INTO is only possible if a dataset
|
198
205
|
# is given as the argument, it can't work with a string, so raise an
|
@@ -211,6 +218,8 @@ module Sequel
|
|
211
218
|
def schema_column_type(db_type)
|
212
219
|
if convert_smallint_to_bool && db_type =~ /smallint/i
|
213
220
|
:boolean
|
221
|
+
elsif db_type =~ /unsigned (big)?int/i
|
222
|
+
:integer
|
214
223
|
else
|
215
224
|
super
|
216
225
|
end
|
@@ -234,6 +243,7 @@ module Sequel
|
|
234
243
|
module DatasetMethods
|
235
244
|
Dataset.def_sql_method(self, :insert, %w'insert into columns values')
|
236
245
|
Dataset.def_sql_method(self, :select, %w'with select distinct limit columns into from join where group having window compounds order lock')
|
246
|
+
include ::Sequel::Dataset::ColumnsLimit1
|
237
247
|
|
238
248
|
# Whether to convert smallint to boolean arguments for this dataset.
|
239
249
|
# Defaults to the IBMDB module setting.
|
@@ -169,6 +169,7 @@ module Sequel
|
|
169
169
|
# DB.values([[1, 2], [3, 4]])
|
170
170
|
# # VALUES ((1, 2), (3, 4))
|
171
171
|
def values(v)
|
172
|
+
raise Error, "Cannot provide an empty array for values" if v.empty?
|
172
173
|
@default_dataset.clone(:values=>v)
|
173
174
|
end
|
174
175
|
|
@@ -239,8 +240,12 @@ module Sequel
|
|
239
240
|
super
|
240
241
|
end
|
241
242
|
when :drop_column
|
242
|
-
|
243
|
-
|
243
|
+
if sqlite_version >= 33500
|
244
|
+
super
|
245
|
+
else
|
246
|
+
ocp = lambda{|oc| oc.delete_if{|c| c.to_s == op[:name].to_s}}
|
247
|
+
duplicate_table(table, :old_columns_proc=>ocp){|columns| columns.delete_if{|s| s[:name].to_s == op[:name].to_s}}
|
248
|
+
end
|
244
249
|
when :rename_column
|
245
250
|
if sqlite_version >= 32500
|
246
251
|
super
|
@@ -316,6 +321,12 @@ module Sequel
|
|
316
321
|
end
|
317
322
|
end
|
318
323
|
|
324
|
+
# SQLite does not restrict the integer or decimal type to a specific range.
|
325
|
+
def column_schema_integer_min_max_values(column)
|
326
|
+
nil
|
327
|
+
end
|
328
|
+
alias column_schema_decimal_min_max_values column_schema_integer_min_max_values
|
329
|
+
|
319
330
|
# Array of PRAGMA SQL statements based on the Database options that should be applied to
|
320
331
|
# new connections.
|
321
332
|
def connection_pragmas
|
@@ -333,6 +344,11 @@ module Sequel
|
|
333
344
|
ps
|
334
345
|
end
|
335
346
|
|
347
|
+
# Support creating STRICT tables via :strict option
|
348
|
+
def create_table_sql(name, generator, options)
|
349
|
+
"#{super}#{' STRICT' if options[:strict]}"
|
350
|
+
end
|
351
|
+
|
336
352
|
# SQLite support creating temporary views.
|
337
353
|
def create_view_prefix_sql(name, options)
|
338
354
|
create_view_sql_append_columns("CREATE #{'TEMPORARY 'if options[:temp]}VIEW #{quote_schema_table(name)}", options[:columns])
|
@@ -341,8 +357,10 @@ module Sequel
|
|
341
357
|
DATABASE_ERROR_REGEXPS = {
|
342
358
|
/(is|are) not unique\z|PRIMARY KEY must be unique\z|UNIQUE constraint failed: .+\z/ => UniqueConstraintViolation,
|
343
359
|
/foreign key constraint failed\z/i => ForeignKeyConstraintViolation,
|
360
|
+
/\ASQLITE ERROR 3091/ => CheckConstraintViolation,
|
344
361
|
/\A(SQLITE ERROR 275 \(CONSTRAINT_CHECK\) : )?CHECK constraint failed/ => CheckConstraintViolation,
|
345
362
|
/\A(SQLITE ERROR 19 \(CONSTRAINT\) : )?constraint failed\z/ => ConstraintViolation,
|
363
|
+
/\Acannot store [A-Z]+ value in [A-Z]+ column / => ConstraintViolation,
|
346
364
|
/may not be NULL\z|NOT NULL constraint failed: .+\z/ => NotNullConstraintViolation,
|
347
365
|
/\ASQLITE ERROR \d+ \(\) : CHECK constraint failed: / => CheckConstraintViolation
|
348
366
|
}.freeze
|
@@ -389,7 +407,7 @@ module Sequel
|
|
389
407
|
old_columns = def_columns.map{|c| c[:name]}
|
390
408
|
opts[:old_columns_proc].call(old_columns) if opts[:old_columns_proc]
|
391
409
|
|
392
|
-
yield def_columns if
|
410
|
+
yield def_columns if defined?(yield)
|
393
411
|
|
394
412
|
constraints = (opts[:constraints] || []).dup
|
395
413
|
pks = []
|
@@ -424,10 +442,10 @@ module Sequel
|
|
424
442
|
skip_indexes = []
|
425
443
|
indexes(table, :only_autocreated=>true).each do |name, h|
|
426
444
|
skip_indexes << name
|
427
|
-
if h[:unique]
|
445
|
+
if h[:unique] && !opts[:no_unique]
|
428
446
|
if h[:columns].length == 1
|
429
447
|
unique_columns.concat(h[:columns])
|
430
|
-
elsif h[:columns].map(&:to_s) != pks
|
448
|
+
elsif h[:columns].map(&:to_s) != pks
|
431
449
|
constraints << {:type=>:unique, :columns=>h[:columns]}
|
432
450
|
end
|
433
451
|
end
|
@@ -558,10 +576,10 @@ module Sequel
|
|
558
576
|
EXTRACT_MAP = {:year=>"'%Y'", :month=>"'%m'", :day=>"'%d'", :hour=>"'%H'", :minute=>"'%M'", :second=>"'%f'"}.freeze
|
559
577
|
EXTRACT_MAP.each_value(&:freeze)
|
560
578
|
|
561
|
-
Dataset.def_sql_method(self, :delete, [['if db.sqlite_version >= 30803', %w'with delete from where'], ["else", %w'delete from where']])
|
562
|
-
Dataset.def_sql_method(self, :insert, [['if db.sqlite_version >= 30803', %w'with insert conflict into columns values on_conflict'], ["else", %w'insert conflict into columns values']])
|
579
|
+
Dataset.def_sql_method(self, :delete, [['if db.sqlite_version >= 33500', %w'with delete from where returning'], ['elsif db.sqlite_version >= 30803', %w'with delete from where'], ["else", %w'delete from where']])
|
580
|
+
Dataset.def_sql_method(self, :insert, [['if db.sqlite_version >= 33500', %w'with insert conflict into columns values on_conflict returning'], ['elsif db.sqlite_version >= 30803', %w'with insert conflict into columns values on_conflict'], ["else", %w'insert conflict into columns values']])
|
563
581
|
Dataset.def_sql_method(self, :select, [['if opts[:values]', %w'with values compounds'], ['else', %w'with select distinct columns from join where group having window compounds order limit lock']])
|
564
|
-
Dataset.def_sql_method(self, :update, [['if db.sqlite_version >= 30803', %w'with update table set where'], ["else", %w'update table set where']])
|
582
|
+
Dataset.def_sql_method(self, :update, [['if db.sqlite_version >= 33500', %w'with update table set from where returning'], ['elsif db.sqlite_version >= 33300', %w'with update table set from where'], ['elsif db.sqlite_version >= 30803', %w'with update table set where'], ["else", %w'update table set where']])
|
565
583
|
|
566
584
|
def cast_sql_append(sql, expr, type)
|
567
585
|
if type == Time or type == DateTime
|
@@ -635,10 +653,16 @@ module Sequel
|
|
635
653
|
# SQLite performs a TRUNCATE style DELETE if no filter is specified.
|
636
654
|
# Since we want to always return the count of records, add a condition
|
637
655
|
# that is always true and then delete.
|
638
|
-
def delete
|
639
|
-
@opts[:where] ? super : where(1=>1).delete
|
656
|
+
def delete(&block)
|
657
|
+
@opts[:where] ? super : where(1=>1).delete(&block)
|
640
658
|
end
|
641
659
|
|
660
|
+
# Always return false when using VALUES
|
661
|
+
def empty?
|
662
|
+
return false if @opts[:values]
|
663
|
+
super
|
664
|
+
end
|
665
|
+
|
642
666
|
# Return an array of strings specifying a query explanation for a SELECT of the
|
643
667
|
# current dataset. Currently, the options are ignored, but it accepts options
|
644
668
|
# to be compatible with other adapters.
|
@@ -653,10 +677,25 @@ module Sequel
|
|
653
677
|
|
654
678
|
# HAVING requires GROUP BY on SQLite
|
655
679
|
def having(*cond)
|
656
|
-
raise(InvalidOperation, "Can only specify a HAVING clause on a grouped dataset")
|
680
|
+
raise(InvalidOperation, "Can only specify a HAVING clause on a grouped dataset") if !@opts[:group] && db.sqlite_version < 33900
|
657
681
|
super
|
658
682
|
end
|
659
683
|
|
684
|
+
# Support insert select for associations, so that the model code can use
|
685
|
+
# returning instead of a separate query.
|
686
|
+
def insert_select(*values)
|
687
|
+
return unless supports_insert_select?
|
688
|
+
# Handle case where query does not return a row
|
689
|
+
server?(:default).with_sql_first(insert_select_sql(*values)) || false
|
690
|
+
end
|
691
|
+
|
692
|
+
# The SQL to use for an insert_select, adds a RETURNING clause to the insert
|
693
|
+
# unless the RETURNING clause is already present.
|
694
|
+
def insert_select_sql(*values)
|
695
|
+
ds = opts[:returning] ? self : returning
|
696
|
+
ds.insert_sql(*values)
|
697
|
+
end
|
698
|
+
|
660
699
|
# SQLite uses the nonstandard ` (backtick) for quoting identifiers.
|
661
700
|
def quoted_identifier_append(sql, c)
|
662
701
|
sql << '`' << c.to_s.gsub('`', '``') << '`'
|
@@ -738,6 +777,13 @@ module Sequel
|
|
738
777
|
insert_conflict(:ignore)
|
739
778
|
end
|
740
779
|
|
780
|
+
# Automatically add aliases to RETURNING values to work around SQLite bug.
|
781
|
+
def returning(*values)
|
782
|
+
return super if values.empty?
|
783
|
+
raise Error, "RETURNING is not supported on #{db.database_type}" unless supports_returning?(:insert)
|
784
|
+
clone(:returning=>_returning_values(values).freeze)
|
785
|
+
end
|
786
|
+
|
741
787
|
# SQLite 3.8.3+ supports common table expressions.
|
742
788
|
def supports_cte?(type=:select)
|
743
789
|
db.sqlite_version >= 30803
|
@@ -753,6 +799,11 @@ module Sequel
|
|
753
799
|
false
|
754
800
|
end
|
755
801
|
|
802
|
+
# SQLite does not support deleting from a joined dataset
|
803
|
+
def supports_deleting_joins?
|
804
|
+
false
|
805
|
+
end
|
806
|
+
|
756
807
|
# SQLite does not support INTERSECT ALL or EXCEPT ALL
|
757
808
|
def supports_intersect_except_all?
|
758
809
|
false
|
@@ -763,11 +814,21 @@ module Sequel
|
|
763
814
|
false
|
764
815
|
end
|
765
816
|
|
817
|
+
# SQLite 3.33.0 supports modifying joined datasets
|
818
|
+
def supports_modifying_joins?
|
819
|
+
db.sqlite_version >= 33300
|
820
|
+
end
|
821
|
+
|
766
822
|
# SQLite does not support multiple columns for the IN/NOT IN operators
|
767
823
|
def supports_multiple_column_in?
|
768
824
|
false
|
769
825
|
end
|
770
826
|
|
827
|
+
# SQLite 3.35.0 supports RETURNING on INSERT/UPDATE/DELETE.
|
828
|
+
def supports_returning?(_)
|
829
|
+
db.sqlite_version >= 33500
|
830
|
+
end
|
831
|
+
|
771
832
|
# SQLite supports timezones in literal timestamps, since it stores them
|
772
833
|
# as text. But using timezones in timestamps breaks SQLite datetime
|
773
834
|
# functions, so we allow the user to override the default per database.
|
@@ -800,6 +861,26 @@ module Sequel
|
|
800
861
|
|
801
862
|
private
|
802
863
|
|
864
|
+
# Add aliases to symbols and identifiers to work around SQLite bug.
|
865
|
+
def _returning_values(values)
|
866
|
+
values.map do |v|
|
867
|
+
case v
|
868
|
+
when Symbol
|
869
|
+
_, c, a = split_symbol(v)
|
870
|
+
a ? v : Sequel.as(v, c)
|
871
|
+
when SQL::Identifier, SQL::QualifiedIdentifier
|
872
|
+
Sequel.as(v, unqualified_column_for(v))
|
873
|
+
else
|
874
|
+
v
|
875
|
+
end
|
876
|
+
end
|
877
|
+
end
|
878
|
+
|
879
|
+
# Use from_self for aggregate dataset using VALUES.
|
880
|
+
def aggreate_dataset_use_from_self?
|
881
|
+
super || @opts[:values]
|
882
|
+
end
|
883
|
+
|
803
884
|
# SQLite uses string literals instead of identifiers in AS clauses.
|
804
885
|
def as_sql_append(sql, aliaz, column_aliases=nil)
|
805
886
|
raise Error, "sqlite does not support derived column lists" if column_aliases
|
@@ -825,6 +906,13 @@ module Sequel
|
|
825
906
|
end
|
826
907
|
end
|
827
908
|
|
909
|
+
# Raise an InvalidOperation exception if insert is not allowed for this dataset.
|
910
|
+
def check_insert_allowed!
|
911
|
+
raise(InvalidOperation, "Grouped datasets cannot be modified") if opts[:group]
|
912
|
+
raise(InvalidOperation, "Joined datasets cannot be modified") if joined_dataset?
|
913
|
+
end
|
914
|
+
alias check_delete_allowed! check_insert_allowed!
|
915
|
+
|
828
916
|
# SQLite supports a maximum of 500 rows in a VALUES clause.
|
829
917
|
def default_import_slice
|
830
918
|
500
|
@@ -944,6 +1032,23 @@ module Sequel
|
|
944
1032
|
def _truncate_sql(table)
|
945
1033
|
"DELETE FROM #{table}"
|
946
1034
|
end
|
1035
|
+
|
1036
|
+
# Use FROM to specify additional tables in an update query
|
1037
|
+
def update_from_sql(sql)
|
1038
|
+
if(from = @opts[:from][1..-1]).empty?
|
1039
|
+
raise(Error, 'Need multiple FROM tables if updating/deleting a dataset with JOINs') if @opts[:join]
|
1040
|
+
else
|
1041
|
+
sql << ' FROM '
|
1042
|
+
source_list_append(sql, from)
|
1043
|
+
select_join_sql(sql)
|
1044
|
+
end
|
1045
|
+
end
|
1046
|
+
|
1047
|
+
# Only include the primary table in the main update clause
|
1048
|
+
def update_table_sql(sql)
|
1049
|
+
sql << ' '
|
1050
|
+
source_list_append(sql, @opts[:from][0..0])
|
1051
|
+
end
|
947
1052
|
end
|
948
1053
|
end
|
949
1054
|
end
|
@@ -98,6 +98,11 @@ module Sequel
|
|
98
98
|
# The conversion procs to use for this database
|
99
99
|
attr_reader :conversion_procs
|
100
100
|
|
101
|
+
def initialize(opts = OPTS)
|
102
|
+
super
|
103
|
+
@allow_regexp = typecast_value_boolean(opts[:setup_regexp_function])
|
104
|
+
end
|
105
|
+
|
101
106
|
# Connect to the database. Since SQLite is a file based database,
|
102
107
|
# available options are limited:
|
103
108
|
#
|
@@ -119,6 +124,12 @@ module Sequel
|
|
119
124
|
end
|
120
125
|
|
121
126
|
connection_pragmas.each{|s| log_connection_yield(s, db){db.execute_batch(s)}}
|
127
|
+
|
128
|
+
if typecast_value_boolean(opts[:setup_regexp_function])
|
129
|
+
db.create_function("regexp", 2) do |func, regexp_str, string|
|
130
|
+
func.result = Regexp.new(regexp_str).match(string) ? 1 : 0
|
131
|
+
end
|
132
|
+
end
|
122
133
|
|
123
134
|
class << db
|
124
135
|
attr_reader :prepared_statements
|
@@ -128,6 +139,12 @@ module Sequel
|
|
128
139
|
db
|
129
140
|
end
|
130
141
|
|
142
|
+
# Whether this Database instance is setup to allow regexp matching.
|
143
|
+
# True if the :setup_regexp_function option was passed when creating the Database.
|
144
|
+
def allow_regexp?
|
145
|
+
@allow_regexp
|
146
|
+
end
|
147
|
+
|
131
148
|
# Disconnect given connections from the database.
|
132
149
|
def disconnect_connection(c)
|
133
150
|
c.prepared_statements.each_value{|v| v.first.close}
|
@@ -189,26 +206,24 @@ module Sequel
|
|
189
206
|
# Yield an available connection. Rescue
|
190
207
|
# any SQLite3::Exceptions and turn them into DatabaseErrors.
|
191
208
|
def _execute(type, sql, opts, &block)
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
conn.changes
|
207
|
-
end
|
209
|
+
synchronize(opts[:server]) do |conn|
|
210
|
+
return execute_prepared_statement(conn, type, sql, opts, &block) if sql.is_a?(Symbol)
|
211
|
+
log_args = opts[:arguments]
|
212
|
+
args = {}
|
213
|
+
opts.fetch(:arguments, OPTS).each{|k, v| args[k] = prepared_statement_argument(v)}
|
214
|
+
case type
|
215
|
+
when :select
|
216
|
+
log_connection_yield(sql, conn, log_args){conn.query(sql, args, &block)}
|
217
|
+
when :insert
|
218
|
+
log_connection_yield(sql, conn, log_args){conn.execute(sql, args)}
|
219
|
+
conn.last_insert_row_id
|
220
|
+
when :update
|
221
|
+
log_connection_yield(sql, conn, log_args){conn.execute_batch(sql, args)}
|
222
|
+
conn.changes
|
208
223
|
end
|
209
|
-
rescue SQLite3::Exception => e
|
210
|
-
raise_error(e)
|
211
224
|
end
|
225
|
+
rescue SQLite3::Exception => e
|
226
|
+
raise_error(e)
|
212
227
|
end
|
213
228
|
|
214
229
|
# The SQLite adapter does not need the pool to convert exceptions.
|
@@ -323,6 +338,28 @@ module Sequel
|
|
323
338
|
BindArgumentMethods = prepared_statements_module(:bind, ArgumentMapper)
|
324
339
|
PreparedStatementMethods = prepared_statements_module(:prepare, BindArgumentMethods)
|
325
340
|
|
341
|
+
# Support regexp functions if using :setup_regexp_function Database option.
|
342
|
+
def complex_expression_sql_append(sql, op, args)
|
343
|
+
case op
|
344
|
+
when :~, :'!~', :'~*', :'!~*'
|
345
|
+
return super unless supports_regexp?
|
346
|
+
|
347
|
+
case_insensitive = [:'~*', :'!~*'].include?(op)
|
348
|
+
sql << 'NOT ' if [:'!~', :'!~*'].include?(op)
|
349
|
+
sql << '('
|
350
|
+
sql << 'LOWER(' if case_insensitive
|
351
|
+
literal_append(sql, args[0])
|
352
|
+
sql << ')' if case_insensitive
|
353
|
+
sql << ' REGEXP '
|
354
|
+
sql << 'LOWER(' if case_insensitive
|
355
|
+
literal_append(sql, args[1])
|
356
|
+
sql << ')' if case_insensitive
|
357
|
+
sql << ')'
|
358
|
+
else
|
359
|
+
super
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
326
363
|
def fetch_rows(sql)
|
327
364
|
execute(sql) do |result|
|
328
365
|
cps = db.conversion_procs
|
@@ -346,6 +383,11 @@ module Sequel
|
|
346
383
|
end
|
347
384
|
end
|
348
385
|
end
|
386
|
+
|
387
|
+
# Support regexp if using :setup_regexp_function Database option.
|
388
|
+
def supports_regexp?
|
389
|
+
db.allow_regexp?
|
390
|
+
end
|
349
391
|
|
350
392
|
private
|
351
393
|
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
require 'trilogy'
|
4
|
+
require_relative 'shared/mysql'
|
5
|
+
|
6
|
+
module Sequel
|
7
|
+
module Trilogy
|
8
|
+
class Database < Sequel::Database
|
9
|
+
include Sequel::MySQL::DatabaseMethods
|
10
|
+
|
11
|
+
QUERY_FLAGS = ::Trilogy::QUERY_FLAGS_CAST | ::Trilogy::QUERY_FLAGS_CAST_BOOLEANS
|
12
|
+
LOCAL_TIME_QUERY_FLAGS = QUERY_FLAGS | ::Trilogy::QUERY_FLAGS_LOCAL_TIMEZONE
|
13
|
+
|
14
|
+
set_adapter_scheme :trilogy
|
15
|
+
|
16
|
+
# Connect to the database. See Trilogy documentation for options.
|
17
|
+
def connect(server)
|
18
|
+
opts = server_opts(server)
|
19
|
+
opts[:username] ||= opts.delete(:user)
|
20
|
+
opts[:found_rows] = true
|
21
|
+
conn = ::Trilogy.new(opts)
|
22
|
+
mysql_connection_setting_sqls.each{|sql| log_connection_yield(sql, conn){conn.query(sql)}}
|
23
|
+
conn
|
24
|
+
end
|
25
|
+
|
26
|
+
def disconnect_connection(c)
|
27
|
+
c.discard!
|
28
|
+
rescue ::Trilogy::Error
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
|
32
|
+
# Execute the given SQL on the given connection and yield the result.
|
33
|
+
def execute(sql, opts)
|
34
|
+
r = synchronize(opts[:server]) do |conn|
|
35
|
+
log_connection_yield((log_sql = opts[:log_sql]) ? sql + log_sql : sql, conn) do
|
36
|
+
conn.query_with_flags(sql, timezone.nil? || timezone == :local ? LOCAL_TIME_QUERY_FLAGS : QUERY_FLAGS)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
yield r
|
40
|
+
rescue ::Trilogy::Error => e
|
41
|
+
raise_error(e)
|
42
|
+
end
|
43
|
+
|
44
|
+
def execute_dui(sql, opts=OPTS)
|
45
|
+
execute(sql, opts, &:affected_rows)
|
46
|
+
end
|
47
|
+
|
48
|
+
def execute_insert(sql, opts=OPTS)
|
49
|
+
execute(sql, opts, &:last_insert_id)
|
50
|
+
end
|
51
|
+
|
52
|
+
def freeze
|
53
|
+
server_version
|
54
|
+
super
|
55
|
+
end
|
56
|
+
|
57
|
+
# Return the version of the MySQL server to which we are connecting.
|
58
|
+
def server_version(_server=nil)
|
59
|
+
@server_version ||= super()
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def database_specific_error_class(exception, opts)
|
65
|
+
case exception.message
|
66
|
+
when /1205 - Lock wait timeout exceeded; try restarting transaction\z/
|
67
|
+
DatabaseLockTimeout
|
68
|
+
else
|
69
|
+
super
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def connection_execute_method
|
74
|
+
:query
|
75
|
+
end
|
76
|
+
|
77
|
+
def database_error_classes
|
78
|
+
[::Trilogy::Error]
|
79
|
+
end
|
80
|
+
|
81
|
+
def dataset_class_default
|
82
|
+
Dataset
|
83
|
+
end
|
84
|
+
|
85
|
+
# Convert tinyint(1) type to boolean if convert_tinyint_to_bool is true
|
86
|
+
def schema_column_type(db_type)
|
87
|
+
db_type =~ /\Atinyint\(1\)/ ? :boolean : super
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class Dataset < Sequel::Dataset
|
92
|
+
include Sequel::MySQL::DatasetMethods
|
93
|
+
|
94
|
+
def fetch_rows(sql)
|
95
|
+
execute(sql) do |r|
|
96
|
+
self.columns = r.fields.map!{|c| output_identifier(c.to_s)}
|
97
|
+
r.each_hash{|h| yield h}
|
98
|
+
end
|
99
|
+
self
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def execute(sql, opts=OPTS)
|
105
|
+
opts = Hash[opts]
|
106
|
+
opts[:type] = :select
|
107
|
+
super
|
108
|
+
end
|
109
|
+
|
110
|
+
# Handle correct quoting of strings using ::Trilogy#escape.
|
111
|
+
def literal_string_append(sql, v)
|
112
|
+
sql << "'" << db.synchronize(@opts[:server]){|c| c.escape(v)} << "'"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
class Dataset
|
5
|
+
module ColumnsLimit1
|
6
|
+
COLUMNS_CLONE_OPTIONS = {:distinct => nil, :limit => 1, :offset=>nil, :where=>nil, :having=>nil, :order=>nil, :row_proc=>nil, :graph=>nil, :eager_graph=>nil}.freeze
|
7
|
+
|
8
|
+
# Use a limit of 1 instead of a limit of 0 when
|
9
|
+
# getting the columns.
|
10
|
+
def columns!
|
11
|
+
ds = clone(COLUMNS_CLONE_OPTIONS)
|
12
|
+
ds.each{break}
|
13
|
+
|
14
|
+
if cols = ds.cache[:_columns]
|
15
|
+
self.columns = cols
|
16
|
+
else
|
17
|
+
[]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -34,7 +34,7 @@ module Sequel
|
|
34
34
|
def execute(sql, opts=OPTS, &block)
|
35
35
|
if opts[:sproc]
|
36
36
|
call_sproc(sql, opts, &block)
|
37
|
-
elsif sql.is_a?(Symbol)
|
37
|
+
elsif sql.is_a?(Symbol) || sql.is_a?(Sequel::Dataset::ArgumentMapper)
|
38
38
|
execute_prepared_statement(sql, opts, &block)
|
39
39
|
else
|
40
40
|
synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)}
|
@@ -80,6 +80,12 @@ module Sequel
|
|
80
80
|
SQL::DelayedEvaluation.new(lambda{|ds| v(o.call(ds))})
|
81
81
|
when SQL::Wrapper
|
82
82
|
SQL::Wrapper.new(v(o.value))
|
83
|
+
when SQL::Expression
|
84
|
+
if o.respond_to?(:sequel_ast_transform)
|
85
|
+
o.sequel_ast_transform(method(:v))
|
86
|
+
else
|
87
|
+
o
|
88
|
+
end
|
83
89
|
else
|
84
90
|
o
|
85
91
|
end
|
@@ -55,13 +55,11 @@ class Sequel::ShardedSingleConnectionPool < Sequel::ConnectionPool
|
|
55
55
|
# Yields the connection to the supplied block for the given server.
|
56
56
|
# This method simulates the ConnectionPool#hold API.
|
57
57
|
def hold(server=:default)
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
raise
|
64
|
-
end
|
58
|
+
server = pick_server(server)
|
59
|
+
yield(@conns[server] ||= make_new(server))
|
60
|
+
rescue Sequel::DatabaseDisconnectError, *@error_classes => e
|
61
|
+
disconnect_server(server) if disconnect_error?(e)
|
62
|
+
raise
|
65
63
|
end
|
66
64
|
|
67
65
|
# The ShardedSingleConnectionPool always has a maximum size of 1.
|