arel_toolkit 0.3.0 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +3 -0
- data/.github/workflows/develop.yml +90 -0
- data/.github/workflows/master.yml +67 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +13 -5
- data/Appraisals +13 -0
- data/CHANGELOG.md +94 -5
- data/Gemfile +5 -0
- data/Gemfile.lock +62 -33
- data/Guardfile +4 -0
- data/README.md +67 -23
- data/Rakefile +11 -1
- data/arel_toolkit.gemspec +15 -6
- data/benchmark.rb +54 -0
- data/ext/pg_result_init/extconf.rb +52 -0
- data/ext/pg_result_init/pg_result_init.c +138 -0
- data/ext/pg_result_init/pg_result_init.h +6 -0
- data/gemfiles/active_record_6.gemfile +7 -0
- data/gemfiles/active_record_6.gemfile.lock +210 -0
- data/gemfiles/arel_gems.gemfile +10 -0
- data/gemfiles/arel_gems.gemfile.lock +284 -0
- data/gemfiles/default.gemfile +5 -0
- data/gemfiles/default.gemfile.lock +208 -0
- data/lib/arel/enhance.rb +17 -0
- data/lib/arel/enhance/context_enhancer/arel_table.rb +92 -0
- data/lib/arel/enhance/node.rb +232 -0
- data/lib/arel/enhance/path.rb +38 -0
- data/lib/arel/enhance/path_node.rb +26 -0
- data/lib/arel/enhance/query.rb +38 -0
- data/lib/arel/enhance/query_methods.rb +23 -0
- data/lib/arel/enhance/visitor.rb +97 -0
- data/lib/arel/extensions.rb +32 -6
- data/lib/arel/extensions/active_model_attribute_with_cast_value.rb +22 -0
- data/lib/arel/extensions/active_record_relation_query_attribute.rb +22 -0
- data/lib/arel/extensions/active_record_type_caster_connection.rb +7 -0
- data/lib/arel/extensions/active_record_type_caster_map.rb +7 -0
- data/lib/arel/extensions/array.rb +2 -9
- data/lib/arel/extensions/at_time_zone.rb +10 -3
- data/lib/arel/extensions/attributes_attribute.rb +47 -0
- data/lib/arel/extensions/binary.rb +7 -0
- data/lib/arel/extensions/bind_param.rb +15 -0
- data/lib/arel/extensions/bit_string.rb +2 -9
- data/lib/arel/extensions/case.rb +17 -0
- data/lib/arel/extensions/coalesce.rb +17 -3
- data/lib/arel/extensions/conflict.rb +9 -0
- data/lib/arel/extensions/contains.rb +27 -5
- data/lib/arel/extensions/current_catalog.rb +4 -0
- data/lib/arel/extensions/current_date.rb +4 -0
- data/lib/arel/extensions/current_of_expression.rb +2 -9
- data/lib/arel/extensions/current_role.rb +4 -0
- data/lib/arel/extensions/current_row.rb +7 -0
- data/lib/arel/extensions/current_schema.rb +4 -0
- data/lib/arel/extensions/current_user.rb +4 -0
- data/lib/arel/extensions/dealocate.rb +31 -0
- data/lib/arel/extensions/default_values.rb +4 -0
- data/lib/arel/extensions/delete_manager.rb +22 -6
- data/lib/arel/extensions/delete_statement.rb +46 -24
- data/lib/arel/extensions/dot.rb +11 -0
- data/lib/arel/extensions/exists.rb +59 -0
- data/lib/arel/extensions/extract_from.rb +3 -10
- data/lib/arel/extensions/factorial.rb +10 -2
- data/lib/arel/extensions/false.rb +7 -0
- data/lib/arel/extensions/function.rb +44 -14
- data/lib/arel/extensions/greatest.rb +17 -3
- data/lib/arel/extensions/indirection.rb +3 -12
- data/lib/arel/extensions/infer.rb +7 -7
- data/lib/arel/extensions/infix_operation.rb +17 -0
- data/lib/arel/extensions/insert_manager.rb +19 -3
- data/lib/arel/extensions/insert_statement.rb +31 -12
- data/lib/arel/extensions/into.rb +21 -0
- data/lib/arel/extensions/least.rb +17 -3
- data/lib/arel/extensions/named_argument.rb +3 -8
- data/lib/arel/extensions/named_function.rb +7 -0
- data/lib/arel/extensions/node.rb +10 -0
- data/lib/arel/extensions/ordering.rb +21 -6
- data/lib/arel/extensions/overlaps.rb +9 -0
- data/lib/arel/extensions/overlay.rb +9 -0
- data/lib/arel/extensions/position.rb +3 -8
- data/lib/arel/extensions/prepare.rb +39 -0
- data/lib/arel/extensions/range_function.rb +10 -2
- data/lib/arel/extensions/row.rb +3 -8
- data/lib/arel/extensions/select_core.rb +73 -0
- data/lib/arel/extensions/select_manager.rb +22 -6
- data/lib/arel/extensions/select_statement.rb +31 -9
- data/lib/arel/extensions/session_user.rb +4 -0
- data/lib/arel/extensions/set_to_default.rb +4 -0
- data/lib/arel/extensions/substring.rb +8 -0
- data/lib/arel/extensions/table.rb +43 -10
- data/lib/arel/extensions/time_with_precision.rb +6 -0
- data/lib/arel/extensions/to_sql.rb +27 -0
- data/lib/arel/extensions/top.rb +8 -0
- data/lib/arel/extensions/transaction.rb +3 -8
- data/lib/arel/extensions/tree_manager.rb +15 -0
- data/lib/arel/extensions/trim.rb +8 -0
- data/lib/arel/extensions/true.rb +7 -0
- data/lib/arel/extensions/type_cast.rb +7 -0
- data/lib/arel/extensions/unary.rb +7 -0
- data/lib/arel/extensions/unary_operation.rb +16 -0
- data/lib/arel/extensions/unknown.rb +4 -0
- data/lib/arel/extensions/update_manager.rb +22 -6
- data/lib/arel/extensions/update_statement.rb +36 -33
- data/lib/arel/extensions/user.rb +4 -0
- data/lib/arel/extensions/values_list.rb +15 -0
- data/lib/arel/extensions/variable_set.rb +9 -0
- data/lib/arel/extensions/variable_show.rb +3 -8
- data/lib/arel/middleware.rb +5 -1
- data/lib/arel/middleware/active_record_extension.rb +13 -0
- data/lib/arel/middleware/cache_accessor.rb +35 -0
- data/lib/arel/middleware/chain.rb +108 -33
- data/lib/arel/middleware/database_executor.rb +77 -0
- data/lib/arel/middleware/no_op_cache.rb +9 -0
- data/lib/arel/middleware/postgresql_adapter.rb +41 -5
- data/lib/arel/middleware/railtie.rb +15 -1
- data/lib/arel/middleware/result.rb +170 -0
- data/lib/arel/middleware/to_sql_executor.rb +15 -0
- data/lib/arel/middleware/to_sql_middleware.rb +33 -0
- data/lib/arel/sql_to_arel.rb +6 -3
- data/lib/arel/sql_to_arel/pg_query_visitor.rb +67 -38
- data/lib/arel/sql_to_arel/pg_query_visitor/frame_options.rb +1 -1
- data/lib/arel/sql_to_arel/result.rb +17 -4
- data/lib/arel/transformer.rb +8 -0
- data/lib/arel/transformer/prefix_schema_name.rb +183 -0
- data/lib/arel/transformer/remove_active_record_info.rb +40 -0
- data/lib/arel/transformer/replace_table_with_subquery.rb +31 -0
- data/lib/arel_toolkit.rb +15 -2
- data/lib/arel_toolkit/version.rb +1 -1
- metadata +179 -42
- data/.travis.yml +0 -29
- data/lib/arel/extensions/generate_series.rb +0 -9
- data/lib/arel/extensions/rank.rb +0 -9
- data/lib/arel/extensions/unbound_column_reference.rb +0 -5
- data/lib/arel/sql_formatter.rb +0 -59
@@ -4,14 +4,7 @@
|
|
4
4
|
module Arel
|
5
5
|
module Nodes
|
6
6
|
# https://www.postgresql.org/docs/9.1/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
|
7
|
-
class ExtractFrom < Arel::Nodes::
|
8
|
-
attr_reader :field
|
9
|
-
|
10
|
-
def initialize(expr, field)
|
11
|
-
super(expr)
|
12
|
-
|
13
|
-
@field = field
|
14
|
-
end
|
7
|
+
class ExtractFrom < Arel::Nodes::Binary
|
15
8
|
end
|
16
9
|
end
|
17
10
|
|
@@ -19,9 +12,9 @@ module Arel
|
|
19
12
|
class ToSql
|
20
13
|
def visit_Arel_Nodes_ExtractFrom(o, collector)
|
21
14
|
collector << 'extract('
|
22
|
-
visit o.
|
15
|
+
visit o.right, collector
|
23
16
|
collector << ' from '
|
24
|
-
visit o.
|
17
|
+
visit o.left, collector
|
25
18
|
collector << ')'
|
26
19
|
end
|
27
20
|
end
|
@@ -4,11 +4,12 @@
|
|
4
4
|
module Arel
|
5
5
|
module Nodes
|
6
6
|
# https://www.postgresql.org/docs/9.4/functions-math.html
|
7
|
-
class Factorial < Arel::Nodes::
|
7
|
+
class Factorial < Arel::Nodes::Node
|
8
8
|
attr_accessor :prefix
|
9
|
+
attr_accessor :expr
|
9
10
|
|
10
11
|
def initialize(expr, prefix)
|
11
|
-
|
12
|
+
@expr = expr
|
12
13
|
@prefix = prefix
|
13
14
|
end
|
14
15
|
end
|
@@ -26,6 +27,13 @@ module Arel
|
|
26
27
|
end
|
27
28
|
end
|
28
29
|
end
|
30
|
+
|
31
|
+
class Dot
|
32
|
+
def visit_Arel_Nodes_Factorial(o)
|
33
|
+
visit_edge o, 'expr'
|
34
|
+
visit_edge o, 'prefix'
|
35
|
+
end
|
36
|
+
end
|
29
37
|
end
|
30
38
|
end
|
31
39
|
|
@@ -3,20 +3,27 @@
|
|
3
3
|
module Arel
|
4
4
|
module Nodes
|
5
5
|
# Postgres: https://www.postgresql.org/docs/9.1/functions-comparison.html
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
6
|
+
class Function
|
7
|
+
module FunctionExtension
|
8
|
+
# postgres only: https://www.postgresql.org/docs/9.5/functions-aggregate.html
|
9
|
+
attr_accessor :orders
|
10
|
+
attr_accessor :filter
|
11
|
+
attr_accessor :within_group
|
12
|
+
attr_accessor :variardic
|
13
|
+
# postgres only: https://www.postgresql.org/docs/10/ddl-schemas.html
|
14
|
+
attr_accessor :schema_name
|
15
|
+
|
16
|
+
def initialize(expr, aliaz = nil)
|
17
|
+
super
|
18
|
+
|
19
|
+
@expressions = expr
|
20
|
+
@alias = aliaz && SqlLiteral.new(aliaz)
|
21
|
+
@distinct = false
|
22
|
+
@orders = []
|
23
|
+
end
|
19
24
|
end
|
25
|
+
|
26
|
+
prepend FunctionExtension
|
20
27
|
end
|
21
28
|
end
|
22
29
|
|
@@ -26,6 +33,7 @@ module Arel
|
|
26
33
|
# rubocop:disable Metrics/CyclomaticComplexity
|
27
34
|
# rubocop:disable Metrics/AbcSize
|
28
35
|
def aggregate(name, o, collector)
|
36
|
+
collector << "#{o.schema_name}." if o.schema_name
|
29
37
|
collector << "#{name}("
|
30
38
|
collector << 'DISTINCT ' if o.distinct
|
31
39
|
collector << 'VARIADIC ' if o.variardic
|
@@ -38,7 +46,7 @@ module Arel
|
|
38
46
|
end
|
39
47
|
|
40
48
|
if o.orders.any?
|
41
|
-
collector <<
|
49
|
+
collector << ' ' unless o.within_group
|
42
50
|
collector << 'ORDER BY '
|
43
51
|
collector = inject_join o.orders, collector, ', '
|
44
52
|
end
|
@@ -62,6 +70,28 @@ module Arel
|
|
62
70
|
# rubocop:enable Metrics/CyclomaticComplexity
|
63
71
|
# rubocop:enable Metrics/AbcSize
|
64
72
|
end
|
73
|
+
|
74
|
+
class Dot
|
75
|
+
module FunctionExtension
|
76
|
+
def function(o)
|
77
|
+
super
|
78
|
+
|
79
|
+
visit_edge o, 'orders'
|
80
|
+
visit_edge o, 'filter'
|
81
|
+
visit_edge o, 'within_group'
|
82
|
+
visit_edge o, 'variardic'
|
83
|
+
visit_edge o, 'schema_name'
|
84
|
+
end
|
85
|
+
|
86
|
+
alias visit_Arel_Nodes_Min function
|
87
|
+
alias visit_Arel_Nodes_Max function
|
88
|
+
alias visit_Arel_Nodes_Avg function
|
89
|
+
alias visit_Arel_Nodes_Sum function
|
90
|
+
alias visit_Arel_Nodes_Count function
|
91
|
+
end
|
92
|
+
|
93
|
+
prepend FunctionExtension
|
94
|
+
end
|
65
95
|
end
|
66
96
|
end
|
67
97
|
|
@@ -1,9 +1,23 @@
|
|
1
|
+
# rubocop:disable Naming/MethodName
|
2
|
+
# rubocop:disable Naming/UncommunicativeMethodParamName
|
3
|
+
|
1
4
|
module Arel
|
2
5
|
module Nodes
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
# https://www.postgresql.org/docs/10/functions-conditional.html
|
7
|
+
class Greatest < Arel::Nodes::Unary
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Visitors
|
12
|
+
class ToSql
|
13
|
+
def visit_Arel_Nodes_Greatest(o, collector)
|
14
|
+
collector << 'GREATEST('
|
15
|
+
collector = inject_join(o.expr, collector, ', ')
|
16
|
+
collector << ')'
|
6
17
|
end
|
7
18
|
end
|
8
19
|
end
|
9
20
|
end
|
21
|
+
|
22
|
+
# rubocop:enable Naming/MethodName
|
23
|
+
# rubocop:enable Naming/UncommunicativeMethodParamName
|
@@ -3,25 +3,16 @@
|
|
3
3
|
|
4
4
|
module Arel
|
5
5
|
module Nodes
|
6
|
-
class Indirection < Arel::Nodes::
|
7
|
-
attr_reader :arg
|
8
|
-
attr_reader :indirection
|
9
|
-
|
10
|
-
def initialize(arg, indirection)
|
11
|
-
super()
|
12
|
-
|
13
|
-
@arg = arg
|
14
|
-
@indirection = indirection
|
15
|
-
end
|
6
|
+
class Indirection < Arel::Nodes::Binary
|
16
7
|
end
|
17
8
|
end
|
18
9
|
|
19
10
|
module Visitors
|
20
11
|
class ToSql
|
21
12
|
def visit_Arel_Nodes_Indirection(o, collector)
|
22
|
-
visit(o.
|
13
|
+
visit(o.left, collector)
|
23
14
|
collector << '['
|
24
|
-
visit(o.
|
15
|
+
visit(o.right, collector)
|
25
16
|
collector << ']'
|
26
17
|
end
|
27
18
|
end
|
@@ -4,9 +4,9 @@
|
|
4
4
|
module Arel
|
5
5
|
module Nodes
|
6
6
|
# https://www.postgresql.org/docs/9.5/sql-insert.html
|
7
|
-
class Infer < Arel::Nodes::
|
8
|
-
|
9
|
-
|
7
|
+
class Infer < Arel::Nodes::Binary
|
8
|
+
alias name left
|
9
|
+
alias indexes right
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -15,13 +15,13 @@ module Arel
|
|
15
15
|
def visit_Arel_Nodes_Infer(o, collector)
|
16
16
|
if o.name
|
17
17
|
collector << 'ON CONSTRAINT '
|
18
|
-
collector << o.
|
19
|
-
collector <<
|
18
|
+
collector << o.left
|
19
|
+
collector << ' '
|
20
20
|
end
|
21
21
|
|
22
|
-
if o.
|
22
|
+
if o.right
|
23
23
|
collector << '('
|
24
|
-
inject_join o.
|
24
|
+
inject_join o.right, collector, ', '
|
25
25
|
collector << ') '
|
26
26
|
end
|
27
27
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# rubocop:disable Naming/MethodName
|
2
|
+
# rubocop:disable Naming/UncommunicativeMethodParamName
|
3
|
+
|
4
|
+
module Arel
|
5
|
+
module Visitors
|
6
|
+
class Dot
|
7
|
+
def visit_Arel_Nodes_InfixOperation(o)
|
8
|
+
visit_edge o, 'operator'
|
9
|
+
visit_edge o, 'left'
|
10
|
+
visit_edge o, 'right'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# rubocop:enable Naming/MethodName
|
17
|
+
# rubocop:enable Naming/UncommunicativeMethodParamName
|
@@ -1,5 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# rubocop:disable Naming/MethodName
|
2
|
+
# rubocop:disable Naming/UncommunicativeMethodParamName
|
3
|
+
|
4
|
+
module Arel
|
5
|
+
class InsertManager < Arel::TreeManager
|
6
|
+
def ==(other)
|
7
|
+
other.is_a?(self.class) && @ast == other.ast
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Visitors
|
12
|
+
class Dot
|
13
|
+
def visit_Arel_InsertManager(o)
|
14
|
+
visit_edge o, 'ast'
|
15
|
+
end
|
16
|
+
end
|
4
17
|
end
|
5
18
|
end
|
19
|
+
|
20
|
+
# rubocop:enable Naming/MethodName
|
21
|
+
# rubocop:enable Naming/UncommunicativeMethodParamName
|
@@ -3,19 +3,22 @@
|
|
3
3
|
|
4
4
|
module Arel
|
5
5
|
module Nodes
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
class InsertStatement
|
7
|
+
# https://www.postgresql.org/docs/9.5/sql-insert.html
|
8
|
+
module InsertStatementExtension
|
9
|
+
attr_accessor :with
|
10
|
+
attr_accessor :conflict
|
11
|
+
attr_accessor :override
|
12
|
+
attr_accessor :returning
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
old_initialize
|
14
|
+
def initialize
|
15
|
+
super
|
16
16
|
|
17
|
-
|
17
|
+
@returning = []
|
18
|
+
end
|
18
19
|
end
|
20
|
+
|
21
|
+
prepend(InsertStatementExtension)
|
19
22
|
end
|
20
23
|
end
|
21
24
|
|
@@ -27,7 +30,7 @@ module Arel
|
|
27
30
|
def visit_Arel_Nodes_InsertStatement(o, collector)
|
28
31
|
if o.with
|
29
32
|
collector = visit o.with, collector
|
30
|
-
collector <<
|
33
|
+
collector << ' '
|
31
34
|
end
|
32
35
|
|
33
36
|
collector << 'INSERT INTO '
|
@@ -57,18 +60,34 @@ module Arel
|
|
57
60
|
collector
|
58
61
|
end
|
59
62
|
|
63
|
+
visit(o.conflict, collector) if o.conflict
|
64
|
+
|
60
65
|
unless o.returning.empty?
|
61
66
|
collector << ' RETURNING '
|
62
67
|
collector = inject_join o.returning, collector, ', '
|
63
68
|
end
|
64
69
|
|
65
|
-
visit(o.conflict, collector) if o.conflict
|
66
70
|
collector
|
67
71
|
end
|
68
72
|
# rubocop:enable Metrics/CyclomaticComplexity
|
69
73
|
# rubocop:enable Metrics/AbcSize
|
70
74
|
# rubocop:enable Metrics/PerceivedComplexity
|
71
75
|
end
|
76
|
+
|
77
|
+
class Dot
|
78
|
+
module InsertStatementExtension
|
79
|
+
def visit_Arel_Nodes_InsertStatement(o)
|
80
|
+
super
|
81
|
+
|
82
|
+
visit_edge o, 'with'
|
83
|
+
visit_edge o, 'conflict'
|
84
|
+
visit_edge o, 'override'
|
85
|
+
visit_edge o, 'returning'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
prepend(InsertStatementExtension)
|
90
|
+
end
|
72
91
|
end
|
73
92
|
end
|
74
93
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# rubocop:disable Naming/MethodName
|
2
|
+
# rubocop:disable Naming/UncommunicativeMethodParamName
|
3
|
+
|
4
|
+
module Arel
|
5
|
+
module Nodes
|
6
|
+
class Into < Arel::Nodes::Unary
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Visitors
|
11
|
+
class ToSql
|
12
|
+
def visit_Arel_Nodes_Into(o, collector)
|
13
|
+
collector << 'INTO '
|
14
|
+
visit o.expr, collector
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# rubocop:enable Naming/MethodName
|
21
|
+
# rubocop:enable Naming/UncommunicativeMethodParamName
|
@@ -1,9 +1,23 @@
|
|
1
|
+
# rubocop:disable Naming/MethodName
|
2
|
+
# rubocop:disable Naming/UncommunicativeMethodParamName
|
3
|
+
|
1
4
|
module Arel
|
2
5
|
module Nodes
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
# https://www.postgresql.org/docs/10/functions-conditional.html
|
7
|
+
class Least < Arel::Nodes::Unary
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Visitors
|
12
|
+
class ToSql
|
13
|
+
def visit_Arel_Nodes_Least(o, collector)
|
14
|
+
collector << 'LEAST('
|
15
|
+
collector = inject_join(o.expr, collector, ', ')
|
16
|
+
collector << ')'
|
6
17
|
end
|
7
18
|
end
|
8
19
|
end
|
9
20
|
end
|
21
|
+
|
22
|
+
# rubocop:enable Naming/MethodName
|
23
|
+
# rubocop:enable Naming/UncommunicativeMethodParamName
|
@@ -3,14 +3,9 @@
|
|
3
3
|
|
4
4
|
module Arel
|
5
5
|
module Nodes
|
6
|
-
class NamedArgument < Arel::Nodes::
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(name, value)
|
11
|
-
@name = name
|
12
|
-
@value = value
|
13
|
-
end
|
6
|
+
class NamedArgument < Arel::Nodes::Binary
|
7
|
+
alias name left
|
8
|
+
alias value right
|
14
9
|
end
|
15
10
|
end
|
16
11
|
|