veritas-sql-generator 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.rvmrc +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +20 -10
- data/Guardfile +22 -0
- data/README.rdoc +2 -0
- data/Rakefile +4 -2
- data/TODO +16 -2
- data/config/flay.yml +2 -2
- data/config/flog.yml +1 -1
- data/config/roodi.yml +5 -5
- data/config/site.reek +21 -19
- data/lib/veritas/sql/generator.rb +25 -2
- data/lib/veritas/sql/generator/core_ext/date.rb +20 -0
- data/lib/veritas/sql/generator/core_ext/date_time.rb +45 -0
- data/lib/veritas/sql/generator/direction.rb +3 -1
- data/lib/veritas/sql/generator/function.rb +54 -0
- data/lib/veritas/sql/generator/function/aggregate.rb +134 -0
- data/lib/veritas/sql/generator/function/connective.rb +53 -0
- data/lib/veritas/sql/generator/function/numeric.rb +135 -0
- data/lib/veritas/sql/generator/function/predicate.rb +266 -0
- data/lib/veritas/sql/generator/function/proposition.rb +38 -0
- data/lib/veritas/sql/generator/function/string.rb +29 -0
- data/lib/veritas/sql/generator/identifier.rb +2 -1
- data/lib/veritas/sql/generator/literal.rb +15 -18
- data/lib/veritas/sql/generator/relation.rb +144 -17
- data/lib/veritas/sql/generator/relation/binary.rb +16 -64
- data/lib/veritas/sql/generator/relation/set.rb +30 -22
- data/lib/veritas/sql/generator/relation/unary.rb +131 -78
- data/lib/veritas/sql/generator/version.rb +1 -1
- data/spec/unit/date/iso8601_spec.rb +23 -0
- data/spec/unit/date_time/iso_8601_spec.rb +115 -0
- data/spec/unit/veritas/sql/generator/class_methods/parenthesize_spec.rb +18 -0
- data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_count_spec.rb +16 -0
- data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_maximum_spec.rb +16 -0
- data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_mean_spec.rb +16 -0
- data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_minimum_spec.rb +16 -0
- data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_standard_deviation_spec.rb +16 -0
- data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_sum_spec.rb +16 -0
- data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_variance_spec.rb +16 -0
- data/spec/unit/veritas/sql/generator/function/connective/visit_veritas_function_connective_conjunction_spec.rb +20 -0
- data/spec/unit/veritas/sql/generator/function/connective/visit_veritas_function_connective_disjunction_spec.rb +20 -0
- data/spec/unit/veritas/sql/generator/function/connective/visit_veritas_function_connective_negation_spec.rb +20 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_absolute_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_addition_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_division_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_exponentiation_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_modulo_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_multiplication_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_square_root_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_subtraction_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_unary_minus_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_unary_plus_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/{logic/visit_veritas_logic_predicate_equality_spec.rb → function/predicate/visit_veritas_function_predicate_equality_spec.rb} +5 -5
- data/spec/unit/veritas/sql/generator/{logic/visit_veritas_logic_predicate_exclusion_spec.rb → function/predicate/visit_veritas_function_predicate_exclusion_spec.rb} +10 -6
- data/spec/unit/veritas/sql/generator/function/predicate/visit_veritas_function_predicate_greater_than_or_equal_to_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/{logic/visit_veritas_logic_predicate_greater_than_spec.rb → function/predicate/visit_veritas_function_predicate_greater_than_spec.rb} +5 -5
- data/spec/unit/veritas/sql/generator/{logic/visit_veritas_logic_predicate_inclusion_spec.rb → function/predicate/visit_veritas_function_predicate_inclusion_spec.rb} +10 -6
- data/spec/unit/veritas/sql/generator/{logic/visit_veritas_logic_predicate_inequality_spec.rb → function/predicate/visit_veritas_function_predicate_inequality_spec.rb} +8 -8
- data/spec/unit/veritas/sql/generator/function/predicate/visit_veritas_function_predicate_less_than_or_equal_to_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/{logic/visit_veritas_logic_predicate_less_than_spec.rb → function/predicate/visit_veritas_function_predicate_less_than_spec.rb} +5 -5
- data/spec/unit/veritas/sql/generator/function/proposition/visit_veritas_function_proposition_contradiction_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/proposition/visit_veritas_function_proposition_tautology_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/function/string/visit_veritas_function_string_length_spec.rb +15 -0
- data/spec/unit/veritas/sql/generator/literal/class_methods/dup_frozen_spec.rb +2 -2
- data/spec/unit/veritas/sql/generator/relation/binary/base/to_subquery_spec.rb +1 -1
- data/spec/unit/veritas/sql/generator/relation/binary/base/{visit_veritas_base_relation_spec.rb → visit_veritas_relation_base_spec.rb} +3 -3
- data/spec/unit/veritas/sql/generator/relation/binary/to_s_spec.rb +2 -2
- data/spec/unit/veritas/sql/generator/relation/binary/to_subquery_spec.rb +2 -2
- data/spec/unit/veritas/sql/generator/relation/binary/visit_veritas_algebra_join_spec.rb +74 -33
- data/spec/unit/veritas/sql/generator/relation/binary/visit_veritas_algebra_product_spec.rb +63 -19
- data/spec/unit/veritas/sql/generator/relation/class_methods/visit_spec.rb +1 -1
- data/spec/unit/veritas/sql/generator/relation/set/class_methods/normalize_operand_headers_spec.rb +35 -0
- data/spec/unit/veritas/sql/generator/relation/set/to_s_spec.rb +1 -1
- data/spec/unit/veritas/sql/generator/relation/set/to_subquery_spec.rb +4 -4
- data/spec/unit/veritas/sql/generator/relation/set/visit_veritas_algebra_difference_spec.rb +83 -30
- data/spec/unit/veritas/sql/generator/relation/set/visit_veritas_algebra_intersection_spec.rb +80 -30
- data/spec/unit/veritas/sql/generator/relation/set/visit_veritas_algebra_union_spec.rb +80 -30
- data/spec/unit/veritas/sql/generator/relation/to_s_spec.rb +50 -0
- data/spec/unit/veritas/sql/generator/relation/to_subquery_spec.rb +49 -0
- data/spec/unit/veritas/sql/generator/relation/unary/to_s_spec.rb +1 -1
- data/spec/unit/veritas/sql/generator/relation/unary/to_subquery_spec.rb +6 -6
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_extension_spec.rb +165 -0
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_projection_spec.rb +84 -29
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_rename_spec.rb +69 -27
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_restriction_spec.rb +64 -22
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_summarization_spec.rb +652 -0
- data/spec/unit/veritas/sql/generator/relation/unary/{visit_veritas_base_relation_spec.rb → visit_veritas_relation_base_spec.rb} +4 -4
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_relation_operation_limit_spec.rb +60 -20
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_relation_operation_offset_spec.rb +60 -20
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_relation_operation_order_spec.rb +63 -23
- data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_relation_operation_reverse_spec.rb +60 -20
- data/spec/unit/veritas/sql/generator/relation/visited_spec.rb +1 -1
- data/tasks/metrics/ci.rake +7 -0
- data/tasks/{quality → metrics}/flay.rake +0 -0
- data/tasks/{quality → metrics}/flog.rake +0 -0
- data/tasks/{quality → metrics}/heckle.rake +1 -0
- data/tasks/{quality → metrics}/metric_fu.rake +3 -0
- data/tasks/{quality → metrics}/reek.rake +0 -0
- data/tasks/{quality → metrics}/roodi.rake +0 -0
- data/tasks/{quality → metrics}/yardstick.rake +0 -0
- data/tasks/spec.rake +1 -0
- data/veritas-sql-generator.gemspec +82 -114
- metadata +137 -125
- data/lib/veritas/base_relation.rb +0 -36
- data/lib/veritas/sql/generator/logic.rb +0 -349
- data/spec/unit/veritas/base_relation/name_spec.rb +0 -45
- data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_connective_conjunction_spec.rb +0 -16
- data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_connective_disjunction_spec.rb +0 -16
- data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_connective_negation_spec.rb +0 -16
- data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_predicate_greater_than_or_equal_to_spec.rb +0 -15
- data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_predicate_less_than_or_equal_to_spec.rb +0 -15
- data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_proposition_contradiction_spec.rb +0 -15
- data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_proposition_tautology_spec.rb +0 -15
- data/spec/unit/veritas/sql/generator/relation/binary/class_methods/subquery_spec.rb +0 -42
- data/spec/unit/veritas/sql/generator/relation/class_methods/subquery_spec.rb +0 -33
- data/tasks/quality/ci.rake +0 -2
@@ -0,0 +1,134 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Veritas
|
4
|
+
module SQL
|
5
|
+
module Generator
|
6
|
+
module Function
|
7
|
+
|
8
|
+
# Generates an SQL statement for an aggregate function
|
9
|
+
module Aggregate
|
10
|
+
include Function
|
11
|
+
|
12
|
+
COUNT = 'COUNT'.freeze
|
13
|
+
SUM = 'SUM'.freeze
|
14
|
+
MINIMUM = 'MIN'.freeze
|
15
|
+
MAXIMUM = 'MAX'.freeze
|
16
|
+
MEAN = 'AVG'.freeze
|
17
|
+
VARIANCE = 'VAR_POP'.freeze
|
18
|
+
STANDARD_DEVIATION = 'STDDEV_POP'.freeze
|
19
|
+
|
20
|
+
# Visit a count aggregate function
|
21
|
+
#
|
22
|
+
# @param [Veritas::Aggregate::Count] count
|
23
|
+
#
|
24
|
+
# @return [#to_s]
|
25
|
+
#
|
26
|
+
# @api private
|
27
|
+
def visit_veritas_aggregate_count(count)
|
28
|
+
unary_prefix_operation_sql(COUNT, count)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Visit a sum aggregate function
|
32
|
+
#
|
33
|
+
# @param [Veritas::Aggregate::Sum] sum
|
34
|
+
#
|
35
|
+
# @return [#to_s]
|
36
|
+
#
|
37
|
+
# @api private
|
38
|
+
def visit_veritas_aggregate_sum(sum)
|
39
|
+
aggregate_function_sql(SUM, sum)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Visit a minimum aggregate function
|
43
|
+
#
|
44
|
+
# @param [Veritas::Aggregate::Minimum] minimum
|
45
|
+
#
|
46
|
+
# @return [#to_s]
|
47
|
+
#
|
48
|
+
# @api private
|
49
|
+
def visit_veritas_aggregate_minimum(minimum)
|
50
|
+
# TODO: wrap this in a coalesce operation once the default can be made sane
|
51
|
+
unary_prefix_operation_sql(MINIMUM, minimum)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Visit a maximum aggregate function
|
55
|
+
#
|
56
|
+
# @param [Veritas::Aggregate::Maximum] maximum
|
57
|
+
#
|
58
|
+
# @return [#to_s]
|
59
|
+
#
|
60
|
+
# @api private
|
61
|
+
def visit_veritas_aggregate_maximum(maximum)
|
62
|
+
# TODO: wrap this in a coalesce operation once the default can be made sane
|
63
|
+
unary_prefix_operation_sql(MAXIMUM, maximum)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Visit a mean aggregate function
|
67
|
+
#
|
68
|
+
# @param [Veritas::Aggregate::Mean] mean
|
69
|
+
#
|
70
|
+
# @return [#to_s]
|
71
|
+
#
|
72
|
+
# @api private
|
73
|
+
def visit_veritas_aggregate_mean(mean)
|
74
|
+
unary_prefix_operation_sql(MEAN, mean)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Visit a variance aggregate function
|
78
|
+
#
|
79
|
+
# @param [Veritas::Aggregate::Variance] variance
|
80
|
+
#
|
81
|
+
# @return [#to_s]
|
82
|
+
#
|
83
|
+
# @api private
|
84
|
+
def visit_veritas_aggregate_variance(variance)
|
85
|
+
unary_prefix_operation_sql(VARIANCE, variance)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Visit a standard deviation aggregate function
|
89
|
+
#
|
90
|
+
# @param [Veritas::Aggregate::StandardDeviation] standard_deviation
|
91
|
+
#
|
92
|
+
# @return [#to_s]
|
93
|
+
#
|
94
|
+
# @api private
|
95
|
+
def visit_veritas_aggregate_standard_deviation(standard_deviation)
|
96
|
+
unary_prefix_operation_sql(STANDARD_DEVIATION, standard_deviation)
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
# Return the SQL
|
102
|
+
#
|
103
|
+
# @param [#to_s] operation
|
104
|
+
#
|
105
|
+
# @param [Veritas::Aggregate] aggregate
|
106
|
+
#
|
107
|
+
# @return [#to_s]
|
108
|
+
#
|
109
|
+
# @api private
|
110
|
+
def aggregate_function_sql(operation, aggregate)
|
111
|
+
default_when_null(
|
112
|
+
unary_prefix_operation_sql(operation, aggregate),
|
113
|
+
aggregate.finalize(aggregate.default)
|
114
|
+
)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Specify a default value when SQL expression evaluates to NULL
|
118
|
+
#
|
119
|
+
# @param [#to_s] sql
|
120
|
+
#
|
121
|
+
# @param [Visitable] default
|
122
|
+
#
|
123
|
+
# @return [#to_s]
|
124
|
+
#
|
125
|
+
# @api private
|
126
|
+
def default_when_null(sql, default)
|
127
|
+
"COALESCE (#{sql}, #{dispatch(default)})"
|
128
|
+
end
|
129
|
+
|
130
|
+
end # module Aggregate
|
131
|
+
end # module Function
|
132
|
+
end # module Generator
|
133
|
+
end # module SQL
|
134
|
+
end # module Veritas
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Veritas
|
4
|
+
module SQL
|
5
|
+
module Generator
|
6
|
+
module Function
|
7
|
+
|
8
|
+
# Generates an SQL statement for a connective
|
9
|
+
module Connective
|
10
|
+
include Function
|
11
|
+
|
12
|
+
AND = 'AND'.freeze
|
13
|
+
OR = 'OR'.freeze
|
14
|
+
NOT = 'NOT'.freeze
|
15
|
+
|
16
|
+
# Visit an Conjunction connective
|
17
|
+
#
|
18
|
+
# @param [Function::Connective::Conjunction] conjunction
|
19
|
+
#
|
20
|
+
# @return [#to_s]
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
def visit_veritas_function_connective_conjunction(conjunction)
|
24
|
+
Generator.parenthesize!(binary_infix_operation_sql(AND, conjunction))
|
25
|
+
end
|
26
|
+
|
27
|
+
# Visit an Disjunction connective
|
28
|
+
#
|
29
|
+
# @param [Function::Connective::Disjunction] disjunction
|
30
|
+
#
|
31
|
+
# @return [#to_s]
|
32
|
+
#
|
33
|
+
# @api private
|
34
|
+
def visit_veritas_function_connective_disjunction(disjunction)
|
35
|
+
Generator.parenthesize!(binary_infix_operation_sql(OR, disjunction))
|
36
|
+
end
|
37
|
+
|
38
|
+
# Visit an Negation connective
|
39
|
+
#
|
40
|
+
# @param [Function::Connective::Negation] negation
|
41
|
+
#
|
42
|
+
# @return [#to_s]
|
43
|
+
#
|
44
|
+
# @api private
|
45
|
+
def visit_veritas_function_connective_negation(negation)
|
46
|
+
unary_prefix_operation_sql(NOT, negation)
|
47
|
+
end
|
48
|
+
|
49
|
+
end # module Connective
|
50
|
+
end # module Function
|
51
|
+
end # module Generator
|
52
|
+
end # module SQL
|
53
|
+
end # module Veritas
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Veritas
|
4
|
+
module SQL
|
5
|
+
module Generator
|
6
|
+
module Function
|
7
|
+
|
8
|
+
# Generates an SQL statement for a numeric function
|
9
|
+
module Numeric
|
10
|
+
include Function
|
11
|
+
|
12
|
+
ABSOLUTE = 'ABS'.freeze
|
13
|
+
ADD = '+'.freeze
|
14
|
+
SUBTRACT = '-'.freeze
|
15
|
+
MULTIPLY = '*'.freeze
|
16
|
+
DIVIDE = '/'.freeze
|
17
|
+
POWER = 'POWER'.freeze
|
18
|
+
MOD = 'MOD'.freeze
|
19
|
+
SQUARE_ROOT = 'SQRT'.freeze
|
20
|
+
|
21
|
+
# Visit an Absolute function
|
22
|
+
#
|
23
|
+
# @param [Function::Numeric::Absolute] absolute
|
24
|
+
#
|
25
|
+
# @return [#to_s]
|
26
|
+
#
|
27
|
+
# @api private
|
28
|
+
def visit_veritas_function_numeric_absolute(absolute)
|
29
|
+
unary_prefix_operation_sql(ABSOLUTE, absolute)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Visit an Addition function
|
33
|
+
#
|
34
|
+
# @param [Function::Numeric::Addition] addition
|
35
|
+
#
|
36
|
+
# @return [#to_s]
|
37
|
+
#
|
38
|
+
# @api private
|
39
|
+
def visit_veritas_function_numeric_addition(addition)
|
40
|
+
Generator.parenthesize!(binary_infix_operation_sql(ADD, addition))
|
41
|
+
end
|
42
|
+
|
43
|
+
# Visit a Division function
|
44
|
+
#
|
45
|
+
# @param [Function::Numeric::Division] division
|
46
|
+
#
|
47
|
+
# @return [#to_s]
|
48
|
+
#
|
49
|
+
# @api private
|
50
|
+
def visit_veritas_function_numeric_division(division)
|
51
|
+
Generator.parenthesize!(binary_infix_operation_sql(DIVIDE, division))
|
52
|
+
end
|
53
|
+
|
54
|
+
# Visit a Exponentiation function
|
55
|
+
#
|
56
|
+
# @param [Function::Numeric::Exponentiation] exponentiation
|
57
|
+
#
|
58
|
+
# @return [#to_s]
|
59
|
+
#
|
60
|
+
# @api private
|
61
|
+
def visit_veritas_function_numeric_exponentiation(exponentiation)
|
62
|
+
binary_prefix_operation_sql(POWER, exponentiation)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Visit a Modulo function
|
66
|
+
#
|
67
|
+
# @param [Function::Numeric::Modulo] modulo
|
68
|
+
#
|
69
|
+
# @return [#to_s]
|
70
|
+
#
|
71
|
+
# @api private
|
72
|
+
def visit_veritas_function_numeric_modulo(modulo)
|
73
|
+
binary_prefix_operation_sql(MOD, modulo)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Visit a Multiplication function
|
77
|
+
#
|
78
|
+
# @param [Function::Numeric::Multiplication] multiplication
|
79
|
+
#
|
80
|
+
# @return [#to_s]
|
81
|
+
#
|
82
|
+
# @api private
|
83
|
+
def visit_veritas_function_numeric_multiplication(multiplication)
|
84
|
+
Generator.parenthesize!(binary_infix_operation_sql(MULTIPLY, multiplication))
|
85
|
+
end
|
86
|
+
|
87
|
+
# Visit a Square Root function
|
88
|
+
#
|
89
|
+
# @param [Function::Numeric::SquareRoot] square_root
|
90
|
+
#
|
91
|
+
# @return [#to_s]
|
92
|
+
#
|
93
|
+
# @api private
|
94
|
+
def visit_veritas_function_numeric_square_root(square_root)
|
95
|
+
unary_prefix_operation_sql(SQUARE_ROOT, square_root)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Visit an Addition function
|
99
|
+
#
|
100
|
+
# @param [Function::Numeric::Addition] subtraction
|
101
|
+
#
|
102
|
+
# @return [#to_s]
|
103
|
+
#
|
104
|
+
# @api private
|
105
|
+
def visit_veritas_function_numeric_subtraction(subtraction)
|
106
|
+
Generator.parenthesize!(binary_infix_operation_sql(SUBTRACT, subtraction))
|
107
|
+
end
|
108
|
+
|
109
|
+
# Visit an Unary Minus function
|
110
|
+
#
|
111
|
+
# @param [Function::Numeric::UnaryMinus] unary_minus
|
112
|
+
#
|
113
|
+
# @return [#to_s]
|
114
|
+
#
|
115
|
+
# @api private
|
116
|
+
def visit_veritas_function_numeric_unary_minus(unary_minus)
|
117
|
+
unary_prefix_operation_sql(SUBTRACT, unary_minus)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Visit an Unary Plus function
|
121
|
+
#
|
122
|
+
# @param [Function::Numeric::UnaryPlus] unary_plus
|
123
|
+
#
|
124
|
+
# @return [#to_s]
|
125
|
+
#
|
126
|
+
# @api private
|
127
|
+
def visit_veritas_function_numeric_unary_plus(unary_plus)
|
128
|
+
unary_prefix_operation_sql(ADD, unary_plus)
|
129
|
+
end
|
130
|
+
|
131
|
+
end # module Numeric
|
132
|
+
end # module Function
|
133
|
+
end # module Generator
|
134
|
+
end # module SQL
|
135
|
+
end # module Veritas
|
@@ -0,0 +1,266 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Veritas
|
4
|
+
module SQL
|
5
|
+
module Generator
|
6
|
+
module Function
|
7
|
+
|
8
|
+
# Generates an SQL statement for a predicate function
|
9
|
+
module Predicate
|
10
|
+
include Function
|
11
|
+
|
12
|
+
EQUAL_TO = '='.freeze
|
13
|
+
EQUAL_TO_NULL = 'IS'.freeze
|
14
|
+
NOT_EQUAL_TO = '<>'.freeze
|
15
|
+
NOT_EQUAL_TO_NULL = 'IS NOT'.freeze
|
16
|
+
GREATER_THAN = '>'.freeze
|
17
|
+
GREATER_THAN_OR_EQUAL_TO = '>='.freeze
|
18
|
+
LESS_THAN = '<'.freeze
|
19
|
+
LESS_THAN_OR_EQUAL_TO = '<='.freeze
|
20
|
+
IN = 'IN'.freeze
|
21
|
+
NOT_IN = 'NOT IN'.freeze
|
22
|
+
BETWEEN = 'BETWEEN'.freeze
|
23
|
+
NOT_BETWEEN = 'NOT BETWEEN'.freeze
|
24
|
+
EMPTY_ARRAY = [].freeze
|
25
|
+
|
26
|
+
# Visit an Equality predicate
|
27
|
+
#
|
28
|
+
# @param [Function::Predicate::Equality] equality
|
29
|
+
#
|
30
|
+
# @return [#to_s]
|
31
|
+
#
|
32
|
+
# @api private
|
33
|
+
def visit_veritas_function_predicate_equality(equality)
|
34
|
+
binary_infix_operation_sql(equality.right.nil? ? EQUAL_TO_NULL : EQUAL_TO, equality)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Visit an Inequality predicate
|
38
|
+
#
|
39
|
+
# @param [Function::Predicate::Inequality] inequality
|
40
|
+
#
|
41
|
+
# @return [#to_s]
|
42
|
+
#
|
43
|
+
# @api private
|
44
|
+
def visit_veritas_function_predicate_inequality(inequality)
|
45
|
+
expressions = inequality_expressions(inequality)
|
46
|
+
expressions.one? ? expressions.first : Generator.parenthesize!(expressions.join(' OR '))
|
47
|
+
end
|
48
|
+
|
49
|
+
# Visit an GreaterThan predicate
|
50
|
+
#
|
51
|
+
# @param [Function::Predicate::GreaterThan] greater_than
|
52
|
+
#
|
53
|
+
# @return [#to_s]
|
54
|
+
#
|
55
|
+
# @api private
|
56
|
+
def visit_veritas_function_predicate_greater_than(greater_than)
|
57
|
+
binary_infix_operation_sql(GREATER_THAN, greater_than)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Visit an GreaterThanOrEqualTo predicate
|
61
|
+
#
|
62
|
+
# @param [Function::Predicate::GreaterThanOrEqualTo] greater_than_or_equal_to
|
63
|
+
#
|
64
|
+
# @return [#to_s]
|
65
|
+
#
|
66
|
+
# @api private
|
67
|
+
def visit_veritas_function_predicate_greater_than_or_equal_to(greater_than_or_equal_to)
|
68
|
+
binary_infix_operation_sql(GREATER_THAN_OR_EQUAL_TO, greater_than_or_equal_to)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Visit an LessThan predicate
|
72
|
+
#
|
73
|
+
# @param [Function::Predicate::LessThan] less_than
|
74
|
+
#
|
75
|
+
# @return [#to_s]
|
76
|
+
#
|
77
|
+
# @api private
|
78
|
+
def visit_veritas_function_predicate_less_than(less_than)
|
79
|
+
binary_infix_operation_sql(LESS_THAN, less_than)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Visit an LessThanOrEqualTo predicate
|
83
|
+
#
|
84
|
+
# @param [Function::Predicate::LessThanOrEqualTo] less_than_or_equal_to
|
85
|
+
#
|
86
|
+
# @return [#to_s]
|
87
|
+
#
|
88
|
+
# @api private
|
89
|
+
def visit_veritas_function_predicate_less_than_or_equal_to(less_than_or_equal_to)
|
90
|
+
binary_infix_operation_sql(LESS_THAN_OR_EQUAL_TO, less_than_or_equal_to)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Visit an Inclusion predicate
|
94
|
+
#
|
95
|
+
# @param [Function::Predicate::Inclusion] inclusion
|
96
|
+
#
|
97
|
+
# @return [#to_s]
|
98
|
+
#
|
99
|
+
# @api private
|
100
|
+
def visit_veritas_function_predicate_inclusion(inclusion)
|
101
|
+
case inclusion.right
|
102
|
+
when Range then range_inclusion_sql(inclusion)
|
103
|
+
when EMPTY_ARRAY then FALSE
|
104
|
+
else
|
105
|
+
binary_infix_operation_sql(IN, inclusion)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Visit an Exclusion predicate
|
110
|
+
#
|
111
|
+
# @param [Function::Predicate::Exclusion] exclusion
|
112
|
+
#
|
113
|
+
# @return [#to_s]
|
114
|
+
#
|
115
|
+
# @api private
|
116
|
+
def visit_veritas_function_predicate_exclusion(exclusion)
|
117
|
+
case exclusion.right
|
118
|
+
when Range then range_exclusion_sql(exclusion)
|
119
|
+
when EMPTY_ARRAY then TRUE
|
120
|
+
else
|
121
|
+
binary_infix_operation_sql(NOT_IN, exclusion)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
|
127
|
+
# Return the SQL for an Inclusion using a Range
|
128
|
+
#
|
129
|
+
# @param [Function::Predicate::Inclusion] predicate
|
130
|
+
#
|
131
|
+
# @return [#to_s]
|
132
|
+
#
|
133
|
+
# @api private
|
134
|
+
def range_inclusion_sql(inclusion)
|
135
|
+
if inclusion.right.exclude_end?
|
136
|
+
exclusive_range_inclusion_sql(inclusion)
|
137
|
+
else
|
138
|
+
inclusive_range_sql(BETWEEN, inclusion)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# Return the SQL for an Exclusion using a Range
|
143
|
+
#
|
144
|
+
# @param [Function::Predicate::Exclusion] exclusion
|
145
|
+
#
|
146
|
+
# @return [#to_s]
|
147
|
+
#
|
148
|
+
# @api private
|
149
|
+
def range_exclusion_sql(exclusion)
|
150
|
+
if exclusion.right.exclude_end?
|
151
|
+
exclusive_range_exclusion_sql(exclusion)
|
152
|
+
else
|
153
|
+
inclusive_range_sql(NOT_BETWEEN, exclusion)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# Return the SQL for an Inclusion using an exclusive Range
|
158
|
+
#
|
159
|
+
# @param [Function::Predicate::Inclusion] inclusion
|
160
|
+
#
|
161
|
+
# @return [#to_s]
|
162
|
+
#
|
163
|
+
# @api private
|
164
|
+
def exclusive_range_inclusion_sql(inclusion)
|
165
|
+
left = new_from_enumerable_predicate(Veritas::Function::Predicate::GreaterThanOrEqualTo, inclusion, :first)
|
166
|
+
right = new_from_enumerable_predicate(Veritas::Function::Predicate::LessThan, inclusion, :last)
|
167
|
+
dispatch left.and(right)
|
168
|
+
end
|
169
|
+
|
170
|
+
# Return the SQL for an Exclusion using an exclusive Range
|
171
|
+
#
|
172
|
+
# @param [Function::Predicate::Exclusion] exclusion
|
173
|
+
#
|
174
|
+
# @return [#to_s]
|
175
|
+
#
|
176
|
+
# @api private
|
177
|
+
def exclusive_range_exclusion_sql(exclusion)
|
178
|
+
left = new_from_enumerable_predicate(Veritas::Function::Predicate::LessThan, exclusion, :first)
|
179
|
+
right = new_from_enumerable_predicate(Veritas::Function::Predicate::GreaterThanOrEqualTo, exclusion, :last)
|
180
|
+
dispatch left.or(right)
|
181
|
+
end
|
182
|
+
|
183
|
+
# Instantiate a new Predicate object from an Enumerable Predicate
|
184
|
+
#
|
185
|
+
# @param [Class<Function::Predicate>] klass
|
186
|
+
# the type of predicate to create
|
187
|
+
# @param [Function::Predicate::Enumerable] predicate
|
188
|
+
# the enumerable predicate
|
189
|
+
# @param [Symbol] method
|
190
|
+
# the method to call on the right operand of the predicate
|
191
|
+
# @return [Function::Predicate]
|
192
|
+
#
|
193
|
+
# @api private
|
194
|
+
def new_from_enumerable_predicate(klass, predicate, method)
|
195
|
+
klass.new(predicate.left, predicate.right.send(method))
|
196
|
+
end
|
197
|
+
|
198
|
+
# Return the expressions for an inequality
|
199
|
+
#
|
200
|
+
# @param [Function::Predicate::Inequality] inequality
|
201
|
+
#
|
202
|
+
# @return [Array<#to_s>]
|
203
|
+
#
|
204
|
+
# @api private
|
205
|
+
def inequality_expressions(inequality)
|
206
|
+
expressions = [
|
207
|
+
inequality_sql(inequality),
|
208
|
+
optional_is_null_sql(inequality.left),
|
209
|
+
optional_is_null_sql(inequality.right),
|
210
|
+
]
|
211
|
+
expressions.compact!
|
212
|
+
expressions
|
213
|
+
end
|
214
|
+
|
215
|
+
# Return the SQL for an inequality predicate
|
216
|
+
#
|
217
|
+
# @param [Function::Predicate::Inequality] inequality
|
218
|
+
#
|
219
|
+
# @return [#to_s]
|
220
|
+
#
|
221
|
+
# @api private
|
222
|
+
def inequality_sql(inequality)
|
223
|
+
binary_infix_operation_sql(inequality.right.nil? ? NOT_EQUAL_TO_NULL : NOT_EQUAL_TO, inequality)
|
224
|
+
end
|
225
|
+
|
226
|
+
# Return the SQL for an operation using an inclusive Range
|
227
|
+
#
|
228
|
+
# @param [#to_s] operator
|
229
|
+
#
|
230
|
+
# @param [Function::Predicate::Enumerable] predicate
|
231
|
+
#
|
232
|
+
# @return [#to_s]
|
233
|
+
#
|
234
|
+
# @api private
|
235
|
+
def inclusive_range_sql(operator, predicate)
|
236
|
+
right = predicate.right
|
237
|
+
"#{dispatch(predicate.left)} #{operator} #{dispatch(right.first)} AND #{dispatch(right.last)}"
|
238
|
+
end
|
239
|
+
|
240
|
+
# Return SQL for an Equality with a nil value for optional attributes
|
241
|
+
#
|
242
|
+
# @param [Attribute] attribute
|
243
|
+
#
|
244
|
+
# @return [#to_sql, nil]
|
245
|
+
#
|
246
|
+
# @api private
|
247
|
+
def optional_is_null_sql(attribute)
|
248
|
+
dispatch(attribute.eq(nil)) if optional?(attribute)
|
249
|
+
end
|
250
|
+
|
251
|
+
# Test if the object is not required
|
252
|
+
#
|
253
|
+
# @param [Object] operand
|
254
|
+
#
|
255
|
+
# @return [Boolean]
|
256
|
+
#
|
257
|
+
# @api private
|
258
|
+
def optional?(operand)
|
259
|
+
operand.respond_to?(:required?) && !operand.required?
|
260
|
+
end
|
261
|
+
|
262
|
+
end # module Predicate
|
263
|
+
end # module Function
|
264
|
+
end # module Generator
|
265
|
+
end # module SQL
|
266
|
+
end # module Veritas
|