sequel 5.45.0 → 5.77.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 +434 -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 +27 -6
- data/doc/model_hooks.rdoc +1 -1
- data/doc/object_model.rdoc +8 -8
- data/doc/opening_databases.rdoc +28 -12
- data/doc/postgresql.rdoc +16 -8
- data/doc/querying.rdoc +5 -3
- 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/release_notes/5.73.0.txt +66 -0
- data/doc/release_notes/5.74.0.txt +45 -0
- data/doc/release_notes/5.75.0.txt +35 -0
- data/doc/release_notes/5.76.0.txt +86 -0
- data/doc/release_notes/5.77.0.txt +63 -0
- data/doc/schema_modification.rdoc +1 -1
- data/doc/security.rdoc +9 -9
- data/doc/sharding.rdoc +3 -1
- data/doc/sql.rdoc +27 -15
- data/doc/testing.rdoc +23 -13
- data/doc/transactions.rdoc +6 -6
- data/doc/virtual_rows.rdoc +1 -1
- data/lib/sequel/adapters/ado/access.rb +1 -1
- data/lib/sequel/adapters/ado.rb +1 -1
- data/lib/sequel/adapters/amalgalite.rb +3 -5
- data/lib/sequel/adapters/ibmdb.rb +3 -3
- data/lib/sequel/adapters/jdbc/derby.rb +8 -0
- data/lib/sequel/adapters/jdbc/h2.rb +63 -10
- data/lib/sequel/adapters/jdbc/hsqldb.rb +8 -0
- data/lib/sequel/adapters/jdbc/postgresql.rb +7 -4
- data/lib/sequel/adapters/jdbc/sqlanywhere.rb +15 -0
- data/lib/sequel/adapters/jdbc/sqlserver.rb +4 -0
- data/lib/sequel/adapters/jdbc.rb +24 -22
- data/lib/sequel/adapters/mysql.rb +92 -67
- data/lib/sequel/adapters/mysql2.rb +56 -51
- data/lib/sequel/adapters/odbc/mssql.rb +1 -1
- data/lib/sequel/adapters/odbc.rb +1 -1
- data/lib/sequel/adapters/oracle.rb +4 -3
- data/lib/sequel/adapters/postgres.rb +89 -45
- data/lib/sequel/adapters/shared/access.rb +11 -1
- data/lib/sequel/adapters/shared/db2.rb +42 -0
- data/lib/sequel/adapters/shared/mssql.rb +91 -10
- data/lib/sequel/adapters/shared/mysql.rb +78 -3
- data/lib/sequel/adapters/shared/oracle.rb +86 -7
- data/lib/sequel/adapters/shared/postgres.rb +576 -171
- data/lib/sequel/adapters/shared/sqlanywhere.rb +21 -5
- data/lib/sequel/adapters/shared/sqlite.rb +92 -8
- data/lib/sequel/adapters/sqlanywhere.rb +1 -1
- data/lib/sequel/adapters/sqlite.rb +99 -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 +57 -31
- data/lib/sequel/core.rb +17 -18
- data/lib/sequel/database/connecting.rb +27 -3
- data/lib/sequel/database/dataset.rb +16 -6
- data/lib/sequel/database/misc.rb +70 -14
- data/lib/sequel/database/query.rb +73 -2
- data/lib/sequel/database/schema_generator.rb +11 -6
- data/lib/sequel/database/schema_methods.rb +23 -4
- data/lib/sequel/database/transactions.rb +6 -0
- data/lib/sequel/dataset/actions.rb +111 -15
- data/lib/sequel/dataset/deprecated_singleton_class_methods.rb +42 -0
- data/lib/sequel/dataset/features.rb +20 -1
- data/lib/sequel/dataset/misc.rb +12 -2
- data/lib/sequel/dataset/placeholder_literalizer.rb +20 -9
- data/lib/sequel/dataset/query.rb +170 -41
- data/lib/sequel/dataset/sql.rb +190 -71
- 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 +2 -2
- data/lib/sequel/extensions/async_thread_pool.rb +14 -13
- data/lib/sequel/extensions/auto_cast_date_and_time.rb +94 -0
- data/lib/sequel/extensions/auto_literal_strings.rb +1 -1
- 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 +36 -8
- 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 +11 -10
- data/lib/sequel/extensions/index_caching.rb +5 -1
- data/lib/sequel/extensions/inflector.rb +1 -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 +57 -15
- data/lib/sequel/extensions/named_timezones.rb +22 -6
- data/lib/sequel/extensions/pagination.rb +1 -1
- data/lib/sequel/extensions/pg_array.rb +33 -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 +1 -2
- data/lib/sequel/extensions/pg_extended_date_support.rb +39 -28
- 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 +11 -11
- data/lib/sequel/extensions/pg_json.rb +13 -15
- data/lib/sequel/extensions/pg_json_ops.rb +125 -2
- data/lib/sequel/extensions/pg_multirange.rb +367 -0
- data/lib/sequel/extensions/pg_range.rb +13 -26
- data/lib/sequel/extensions/pg_range_ops.rb +37 -9
- data/lib/sequel/extensions/pg_row.rb +20 -19
- data/lib/sequel/extensions/pg_row_ops.rb +1 -1
- data/lib/sequel/extensions/pg_timestamptz.rb +27 -3
- data/lib/sequel/extensions/round_timestamps.rb +1 -1
- 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/extensions/transaction_connection_validator.rb +78 -0
- data/lib/sequel/model/associations.rb +286 -92
- data/lib/sequel/model/base.rb +53 -33
- data/lib/sequel/model/dataset_module.rb +3 -0
- data/lib/sequel/model/errors.rb +10 -1
- data/lib/sequel/model/exceptions.rb +15 -3
- data/lib/sequel/model/inflections.rb +1 -1
- data/lib/sequel/plugins/auto_restrict_eager_graph.rb +62 -0
- data/lib/sequel/plugins/auto_validations.rb +74 -16
- data/lib/sequel/plugins/class_table_inheritance.rb +2 -2
- data/lib/sequel/plugins/column_encryption.rb +29 -8
- data/lib/sequel/plugins/composition.rb +3 -2
- data/lib/sequel/plugins/concurrent_eager_loading.rb +4 -4
- data/lib/sequel/plugins/constraint_validations.rb +8 -5
- 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 +2 -2
- data/lib/sequel/plugins/lazy_attributes.rb +3 -0
- data/lib/sequel/plugins/list.rb +8 -3
- 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 +4 -4
- data/lib/sequel/plugins/optimistic_locking.rb +9 -42
- data/lib/sequel/plugins/optimistic_locking_base.rb +55 -0
- data/lib/sequel/plugins/paged_operations.rb +181 -0
- data/lib/sequel/plugins/pg_array_associations.rb +46 -34
- data/lib/sequel/plugins/pg_auto_constraint_validations.rb +9 -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 +7 -4
- data/lib/sequel/plugins/require_valid_schema.rb +67 -0
- data/lib/sequel/plugins/serialization.rb +1 -0
- data/lib/sequel/plugins/serialization_modification_detection.rb +1 -0
- 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 +41 -11
- 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 +109 -19
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
#
|
3
|
+
# The sql_log_normalizer extension normalizes the SQL that is logged,
|
4
|
+
# removing the literal strings and numbers in the SQL, and removing the
|
5
|
+
# logging of any bound variables:
|
6
|
+
#
|
7
|
+
# ds = DB[:table].first(a: 1, b: 'something')
|
8
|
+
# # Without sql_log_normalizer extension
|
9
|
+
# # SELECT * FROM "table" WHERE (("a" = 1) AND ("b" = 'something')) LIMIT 1
|
10
|
+
#
|
11
|
+
# # With sql_log_normalizer_extension
|
12
|
+
# # SELECT * FROM "table" WHERE (("a" = ?) AND ("b" = ?)) LIMIT ?
|
13
|
+
#
|
14
|
+
# The normalization is done by scanning the SQL string being executed
|
15
|
+
# for literal strings and numbers, and replacing them with question
|
16
|
+
# marks. While this should work for all or almost all production queries,
|
17
|
+
# there are pathlogical queries that will not be handled correctly, such as
|
18
|
+
# the use of apostrophes in identifiers:
|
19
|
+
#
|
20
|
+
# DB[:"asf'bar"].where(a: 1, b: 'something').first
|
21
|
+
# # Logged as:
|
22
|
+
# # SELECT * FROM "asf?something')) LIMIT ?
|
23
|
+
#
|
24
|
+
# The expected use case for this extension is when you want to normalize
|
25
|
+
# logs to group similar queries, or when you want to protect sensitive
|
26
|
+
# data from being stored in the logs.
|
27
|
+
#
|
28
|
+
# Related module: Sequel::SQLLogNormalizer
|
29
|
+
|
30
|
+
#
|
31
|
+
module Sequel
|
32
|
+
module SQLLogNormalizer
|
33
|
+
def self.extended(db)
|
34
|
+
type = case db.literal("'")
|
35
|
+
when "''''"
|
36
|
+
:standard
|
37
|
+
when "'\\''"
|
38
|
+
:backslash
|
39
|
+
when "N''''"
|
40
|
+
:n_standard
|
41
|
+
else
|
42
|
+
raise Error, "SQL log normalization is not supported on this database (' literalized as #{db.literal("'").inspect})"
|
43
|
+
end
|
44
|
+
db.instance_variable_set(:@sql_string_escape_type, type)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Normalize the SQL before calling super.
|
48
|
+
def log_connection_yield(sql, conn, args=nil)
|
49
|
+
unless skip_logging?
|
50
|
+
sql = normalize_logged_sql(sql)
|
51
|
+
args = nil
|
52
|
+
end
|
53
|
+
super
|
54
|
+
end
|
55
|
+
|
56
|
+
# Replace literal strings and numbers in SQL with question mark placeholders.
|
57
|
+
def normalize_logged_sql(sql)
|
58
|
+
sql = sql.dup
|
59
|
+
sql.force_encoding('BINARY')
|
60
|
+
start_index = 0
|
61
|
+
check_n = @sql_string_escape_type == :n_standard
|
62
|
+
outside_string = true
|
63
|
+
|
64
|
+
if @sql_string_escape_type == :backslash
|
65
|
+
search_char = /[\\']/
|
66
|
+
escape_char_offset = 0
|
67
|
+
escape_char_value = 92 # backslash
|
68
|
+
else
|
69
|
+
search_char = "'"
|
70
|
+
escape_char_offset = 1
|
71
|
+
escape_char_value = 39 # apostrophe
|
72
|
+
end
|
73
|
+
|
74
|
+
# The approach used here goes against Sequel's philosophy of never attempting
|
75
|
+
# to parse SQL. However, parsing the SQL is basically the only way to implement
|
76
|
+
# this support with Sequel's design, and it's better to be pragmatic and accept
|
77
|
+
# this than not be able to support this.
|
78
|
+
|
79
|
+
# Replace literal strings
|
80
|
+
while outside_string && (index = start_index = sql.index("'", start_index))
|
81
|
+
if check_n && index != 0 && sql.getbyte(index-1) == 78 # N' start
|
82
|
+
start_index -= 1
|
83
|
+
end
|
84
|
+
index += 1
|
85
|
+
outside_string = false
|
86
|
+
|
87
|
+
while (index = sql.index(search_char, index)) && (sql.getbyte(index + escape_char_offset) == escape_char_value)
|
88
|
+
# skip escaped characters inside string literal
|
89
|
+
index += 2
|
90
|
+
end
|
91
|
+
|
92
|
+
if index
|
93
|
+
# Found end of string
|
94
|
+
sql[start_index..index] = '?'
|
95
|
+
start_index += 1
|
96
|
+
outside_string = true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Replace integer and decimal floating point numbers
|
101
|
+
sql.gsub!(/\b-?\d+(?:\.\d+)?\b/, '?')
|
102
|
+
|
103
|
+
sql
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
Database.register_extension(:sql_log_normalizer, SQLLogNormalizer)
|
108
|
+
end
|
@@ -0,0 +1,255 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
#
|
3
|
+
# The sqlite_json_ops extension adds support to Sequel's DSL to make
|
4
|
+
# it easier to call SQLite JSON functions and operators (added
|
5
|
+
# first in SQLite 3.38.0).
|
6
|
+
#
|
7
|
+
# To load the extension:
|
8
|
+
#
|
9
|
+
# Sequel.extension :sqlite_json_ops
|
10
|
+
#
|
11
|
+
# This extension works by calling methods on Sequel::SQLite::JSONOp objects,
|
12
|
+
# which you can create via Sequel.sqlite_json_op:
|
13
|
+
#
|
14
|
+
# j = Sequel.sqlite_json_op(:json_column)
|
15
|
+
#
|
16
|
+
# Also, on most Sequel expression objects, you can call the sqlite_json_op method
|
17
|
+
# to create a Sequel::SQLite::JSONOp object:
|
18
|
+
#
|
19
|
+
# j = Sequel[:json_column].sqlite_json_op
|
20
|
+
#
|
21
|
+
# If you have loaded the {core_extensions extension}[rdoc-ref:doc/core_extensions.rdoc],
|
22
|
+
# or you have loaded the core_refinements extension
|
23
|
+
# and have activated refinements for the file, you can also use Symbol#sqlite_json_op:
|
24
|
+
#
|
25
|
+
# j = :json_column.sqlite_json_op
|
26
|
+
#
|
27
|
+
# The following methods are available for Sequel::SQLite::JSONOp instances:
|
28
|
+
#
|
29
|
+
# j[1] # (json_column ->> 1)
|
30
|
+
# j.get(1) # (json_column ->> 1)
|
31
|
+
# j.get_text(1) # (json_column -> 1)
|
32
|
+
# j.extract('$.a') # json_extract(json_column, '$.a')
|
33
|
+
#
|
34
|
+
# j.array_length # json_array_length(json_column)
|
35
|
+
# j.type # json_type(json_column)
|
36
|
+
# j.valid # json_valid(json_column)
|
37
|
+
# j.json # json(json_column)
|
38
|
+
#
|
39
|
+
# j.insert('$.a', 1) # json_insert(json_column, '$.a', 1)
|
40
|
+
# j.set('$.a', 1) # json_set(json_column, '$.a', 1)
|
41
|
+
# j.replace('$.a', 1) # json_replace(json_column, '$.a', 1)
|
42
|
+
# j.remove('$.a') # json_remove(json_column, '$.a')
|
43
|
+
# j.patch('{"a":2}') # json_patch(json_column, '{"a":2}')
|
44
|
+
#
|
45
|
+
# j.each # json_each(json_column)
|
46
|
+
# j.tree # json_tree(json_column)
|
47
|
+
#
|
48
|
+
# Related modules: Sequel::SQLite::JSONOp
|
49
|
+
|
50
|
+
#
|
51
|
+
module Sequel
|
52
|
+
module SQLite
|
53
|
+
# The JSONOp class is a simple container for a single object that
|
54
|
+
# defines methods that yield Sequel expression objects representing
|
55
|
+
# SQLite json operators and functions.
|
56
|
+
#
|
57
|
+
# In the method documentation examples, assume that:
|
58
|
+
#
|
59
|
+
# json_op = Sequel.sqlite_json_op(:json)
|
60
|
+
class JSONOp < Sequel::SQL::Wrapper
|
61
|
+
GET = ["(".freeze, " ->> ".freeze, ")".freeze].freeze
|
62
|
+
private_constant :GET
|
63
|
+
|
64
|
+
GET_JSON = ["(".freeze, " -> ".freeze, ")".freeze].freeze
|
65
|
+
private_constant :GET_JSON
|
66
|
+
|
67
|
+
# Returns an expression for getting the JSON array element or object field
|
68
|
+
# at the specified path as a SQLite value.
|
69
|
+
#
|
70
|
+
# json_op[1] # (json ->> 1)
|
71
|
+
# json_op['a'] # (json ->> 'a')
|
72
|
+
# json_op['$.a.b'] # (json ->> '$.a.b')
|
73
|
+
# json_op['$[1][2]'] # (json ->> '$[1][2]')
|
74
|
+
def [](key)
|
75
|
+
json_op(GET, key)
|
76
|
+
end
|
77
|
+
alias get []
|
78
|
+
|
79
|
+
# Returns an expression for the length of the JSON array, or the JSON array at
|
80
|
+
# the given path.
|
81
|
+
#
|
82
|
+
# json_op.array_length # json_array_length(json)
|
83
|
+
# json_op.array_length('$[1]') # json_array_length(json, '$[1]')
|
84
|
+
def array_length(*args)
|
85
|
+
Sequel::SQL::NumericExpression.new(:NOOP, function(:array_length, *args))
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns an expression for a set of information extracted from the top-level
|
89
|
+
# members of the JSON array or object, or the top-level members of the JSON array
|
90
|
+
# or object at the given path.
|
91
|
+
#
|
92
|
+
# json_op.each # json_each(json)
|
93
|
+
# json_op.each('$.a') # json_each(json, '$.a')
|
94
|
+
def each(*args)
|
95
|
+
function(:each, *args)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Returns an expression for the JSON array element or object field at the specified
|
99
|
+
# path as a SQLite value, but only accept paths as arguments, and allow the use of
|
100
|
+
# multiple paths.
|
101
|
+
#
|
102
|
+
# json_op.extract('$.a') # json_extract(json, '$.a')
|
103
|
+
# json_op.extract('$.a', '$.b') # json_extract(json, '$.a', '$.b')
|
104
|
+
def extract(*a)
|
105
|
+
function(:extract, *a)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Returns an expression for getting the JSON array element or object field at the
|
109
|
+
# specified path as a JSON value.
|
110
|
+
#
|
111
|
+
# json_op.get_json(1) # (json -> 1)
|
112
|
+
# json_op.get_json('a') # (json -> 'a')
|
113
|
+
# json_op.get_json('$.a.b') # (json -> '$.a.b')
|
114
|
+
# json_op.get_json('$[1][2]') # (json -> '$[1][2]')
|
115
|
+
def get_json(key)
|
116
|
+
self.class.new(json_op(GET_JSON, key))
|
117
|
+
end
|
118
|
+
|
119
|
+
# Returns an expression for creating new entries at the given paths in the JSON array
|
120
|
+
# or object, but not overwriting existing entries.
|
121
|
+
#
|
122
|
+
# json_op.insert('$.a', 1) # json_insert(json, '$.a', 1)
|
123
|
+
# json_op.insert('$.a', 1, '$.b', 2) # json_insert(json, '$.a', 1, '$.b', 2)
|
124
|
+
def insert(path, value, *args)
|
125
|
+
wrapped_function(:insert, path, value, *args)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Returns an expression for a minified version of the JSON.
|
129
|
+
#
|
130
|
+
# json_op.json # json(json)
|
131
|
+
def json
|
132
|
+
self.class.new(SQL::Function.new(:json, self))
|
133
|
+
end
|
134
|
+
alias minify json
|
135
|
+
|
136
|
+
# Returns an expression for updating the JSON object using the RFC 7396 MergePatch algorithm
|
137
|
+
#
|
138
|
+
# json_op.patch('{"a": 1, "b": null}') # json_patch(json, '{"a": 1, "b": null}')
|
139
|
+
def patch(json_patch)
|
140
|
+
wrapped_function(:patch, json_patch)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Returns an expression for removing entries at the given paths from the JSON array or object.
|
144
|
+
#
|
145
|
+
# json_op.remove('$.a') # json_remove(json, '$.a')
|
146
|
+
# json_op.remove('$.a', '$.b') # json_remove(json, '$.a', '$.b')
|
147
|
+
def remove(path, *paths)
|
148
|
+
wrapped_function(:remove, path, *paths)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Returns an expression for replacing entries at the given paths in the JSON array or object,
|
152
|
+
# but not creating new entries.
|
153
|
+
#
|
154
|
+
# json_op.replace('$.a', 1) # json_replace(json, '$.a', 1)
|
155
|
+
# json_op.replace('$.a', 1, '$.b', 2) # json_replace(json, '$.a', 1, '$.b', 2)
|
156
|
+
def replace(path, value, *args)
|
157
|
+
wrapped_function(:replace, path, value, *args)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Returns an expression for creating or replacing entries at the given paths in the
|
161
|
+
# JSON array or object.
|
162
|
+
#
|
163
|
+
# json_op.set('$.a', 1) # json_set(json, '$.a', 1)
|
164
|
+
# json_op.set('$.a', 1, '$.b', 2) # json_set(json, '$.a', 1, '$.b', 2)
|
165
|
+
def set(path, value, *args)
|
166
|
+
wrapped_function(:set, path, value, *args)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Returns an expression for a set of information extracted from the JSON array or object, or
|
170
|
+
# the JSON array or object at the given path.
|
171
|
+
#
|
172
|
+
# json_op.tree # json_tree(json)
|
173
|
+
# json_op.tree('$.a') # json_tree(json, '$.a')
|
174
|
+
def tree(*args)
|
175
|
+
function(:tree, *args)
|
176
|
+
end
|
177
|
+
|
178
|
+
# Returns an expression for the type of the JSON value or the JSON value at the given path.
|
179
|
+
#
|
180
|
+
# json_op.type # json_type(json)
|
181
|
+
# json_op.type('$[1]') # json_type(json, '$[1]')
|
182
|
+
def type(*args)
|
183
|
+
Sequel::SQL::StringExpression.new(:NOOP, function(:type, *args))
|
184
|
+
end
|
185
|
+
alias typeof type
|
186
|
+
|
187
|
+
# Returns a boolean expression for whether the JSON is valid or not.
|
188
|
+
def valid
|
189
|
+
Sequel::SQL::BooleanExpression.new(:NOOP, function(:valid))
|
190
|
+
end
|
191
|
+
|
192
|
+
private
|
193
|
+
|
194
|
+
# Internals of the [], get, get_json methods, using a placeholder literal string.
|
195
|
+
def json_op(str, args)
|
196
|
+
self.class.new(Sequel::SQL::PlaceholderLiteralString.new(str, [self, args]))
|
197
|
+
end
|
198
|
+
|
199
|
+
# Internals of the methods that return functions prefixed with +json_+.
|
200
|
+
def function(name, *args)
|
201
|
+
SQL::Function.new("json_#{name}", self, *args)
|
202
|
+
end
|
203
|
+
|
204
|
+
# Internals of the methods that return functions prefixed with +json_+, that
|
205
|
+
# return JSON values.
|
206
|
+
def wrapped_function(*args)
|
207
|
+
self.class.new(function(*args))
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
module JSONOpMethods
|
212
|
+
# Wrap the receiver in an JSONOp so you can easily use the SQLite
|
213
|
+
# json functions and operators with it.
|
214
|
+
def sqlite_json_op
|
215
|
+
JSONOp.new(self)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
module SQL::Builders
|
221
|
+
# Return the object wrapped in an SQLite::JSONOp.
|
222
|
+
def sqlite_json_op(v)
|
223
|
+
case v
|
224
|
+
when SQLite::JSONOp
|
225
|
+
v
|
226
|
+
else
|
227
|
+
SQLite::JSONOp.new(v)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
class SQL::GenericExpression
|
233
|
+
include Sequel::SQLite::JSONOpMethods
|
234
|
+
end
|
235
|
+
|
236
|
+
class LiteralString
|
237
|
+
include Sequel::SQLite::JSONOpMethods
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
# :nocov:
|
242
|
+
if Sequel.core_extensions?
|
243
|
+
class Symbol
|
244
|
+
include Sequel::SQLite::JSONOpMethods
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
if defined?(Sequel::CoreRefinements)
|
249
|
+
module Sequel::CoreRefinements
|
250
|
+
refine Symbol do
|
251
|
+
send INCLUDE_METH, Sequel::SQLite::JSONOpMethods
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
# :nocov:
|
@@ -4,6 +4,10 @@
|
|
4
4
|
# for converting the strings to a date (e.g. String#to_date), allowing
|
5
5
|
# for backwards compatibility with legacy Sequel code.
|
6
6
|
#
|
7
|
+
# These methods calls +parse+ on the related class, and as such, can
|
8
|
+
# result in denial of service in older versions of Ruby for large
|
9
|
+
# untrusted input, and raise exceptions in newer versions of Ruby.
|
10
|
+
#
|
7
11
|
# To load the extension:
|
8
12
|
#
|
9
13
|
# Sequel.extension :string_date_time
|
@@ -11,42 +15,34 @@
|
|
11
15
|
class String
|
12
16
|
# Converts a string into a Date object.
|
13
17
|
def to_date
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
18
|
-
end
|
18
|
+
Date.parse(self, Sequel.convert_two_digit_years)
|
19
|
+
rescue => e
|
20
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
19
21
|
end
|
20
22
|
|
21
23
|
# Converts a string into a DateTime object.
|
22
24
|
def to_datetime
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
27
|
-
end
|
25
|
+
DateTime.parse(self, Sequel.convert_two_digit_years)
|
26
|
+
rescue => e
|
27
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
28
28
|
end
|
29
29
|
|
30
30
|
# Converts a string into a Time or DateTime object, depending on the
|
31
31
|
# value of Sequel.datetime_class
|
32
32
|
def to_sequel_time
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
Sequel.datetime_class.parse(self)
|
38
|
-
end
|
39
|
-
rescue => e
|
40
|
-
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
33
|
+
if Sequel.datetime_class == DateTime
|
34
|
+
DateTime.parse(self, Sequel.convert_two_digit_years)
|
35
|
+
else
|
36
|
+
Sequel.datetime_class.parse(self)
|
41
37
|
end
|
38
|
+
rescue => e
|
39
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
42
40
|
end
|
43
41
|
|
44
42
|
# Converts a string into a Time object.
|
45
43
|
def to_time
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
50
|
-
end
|
44
|
+
Time.parse(self)
|
45
|
+
rescue => e
|
46
|
+
raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
|
51
47
|
end
|
52
48
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
#
|
3
|
+
# The transaction_connection_validator extension automatically
|
4
|
+
# retries a transaction on a connection if an disconnect error
|
5
|
+
# is raised when sending the statement to begin a new
|
6
|
+
# transaction, as long as the user has not already checked out
|
7
|
+
# a connection. This is safe to do because no other queries
|
8
|
+
# have been issued on the connection, and no user-level code
|
9
|
+
# is run before retrying.
|
10
|
+
#
|
11
|
+
# This approach to connection validation can be significantly
|
12
|
+
# lower overhead than the connection_validator extension,
|
13
|
+
# though it does not handle all cases handled by the
|
14
|
+
# connection_validator extension. However, it performs the
|
15
|
+
# validation checks on every new transaction, so it will
|
16
|
+
# automatically handle disconnected connections in some cases
|
17
|
+
# where the connection_validator extension will not by default
|
18
|
+
# (as the connection_validator extension only checks
|
19
|
+
# connections if they have not been used in the last hour by
|
20
|
+
# default).
|
21
|
+
#
|
22
|
+
# Related module: Sequel::TransactionConnectionValidator
|
23
|
+
|
24
|
+
#
|
25
|
+
module Sequel
|
26
|
+
module TransactionConnectionValidator
|
27
|
+
class DisconnectRetry < DatabaseDisconnectError
|
28
|
+
# The connection that raised the disconnect error
|
29
|
+
attr_accessor :connection
|
30
|
+
|
31
|
+
# The underlying disconnect error, in case it needs to be reraised.
|
32
|
+
attr_accessor :database_error
|
33
|
+
end
|
34
|
+
|
35
|
+
# Rescue disconnect errors raised when beginning a new transaction. If there
|
36
|
+
# is a disconnnect error, it should be safe to retry the transaction using a
|
37
|
+
# new connection, as we haven't yielded control to the user yet.
|
38
|
+
def transaction(opts=OPTS)
|
39
|
+
super
|
40
|
+
rescue DisconnectRetry => e
|
41
|
+
if synchronize(opts[:server]){|conn| conn.equal?(e.connection)}
|
42
|
+
# If retrying would use the same connection, that means the
|
43
|
+
# connection was not removed from the pool, which means the caller has
|
44
|
+
# already checked out the connection, and retrying will not be successful.
|
45
|
+
# In this case, we can only reraise the exception.
|
46
|
+
raise e.database_error
|
47
|
+
end
|
48
|
+
|
49
|
+
num_retries ||= 0
|
50
|
+
num_retries += 1
|
51
|
+
retry if num_retries < 5
|
52
|
+
|
53
|
+
raise e.database_error
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
# Reraise disconnect errors as DisconnectRetry so they can be retried.
|
59
|
+
def begin_new_transaction(conn, opts)
|
60
|
+
super
|
61
|
+
rescue Sequel::DatabaseDisconnectError, *database_error_classes => e
|
62
|
+
if e.is_a?(Sequel::DatabaseDisconnectError) || disconnect_error?(e, OPTS)
|
63
|
+
exception = DisconnectRetry.new(e.message)
|
64
|
+
exception.set_backtrace([])
|
65
|
+
exception.connection = conn
|
66
|
+
unless e.is_a?(Sequel::DatabaseError)
|
67
|
+
e = Sequel.convert_exception_class(e, database_error_class(e, OPTS))
|
68
|
+
end
|
69
|
+
exception.database_error = e
|
70
|
+
raise exception
|
71
|
+
end
|
72
|
+
|
73
|
+
raise
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
Database.register_extension(:transaction_connection_validator, TransactionConnectionValidator)
|
78
|
+
end
|