veritas-sql-generator 0.0.3 → 0.0.4

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.
Files changed (117) hide show
  1. data/.gemtest +0 -0
  2. data/.rvmrc +1 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +20 -10
  5. data/Guardfile +22 -0
  6. data/README.rdoc +2 -0
  7. data/Rakefile +4 -2
  8. data/TODO +16 -2
  9. data/config/flay.yml +2 -2
  10. data/config/flog.yml +1 -1
  11. data/config/roodi.yml +5 -5
  12. data/config/site.reek +21 -19
  13. data/lib/veritas/sql/generator.rb +25 -2
  14. data/lib/veritas/sql/generator/core_ext/date.rb +20 -0
  15. data/lib/veritas/sql/generator/core_ext/date_time.rb +45 -0
  16. data/lib/veritas/sql/generator/direction.rb +3 -1
  17. data/lib/veritas/sql/generator/function.rb +54 -0
  18. data/lib/veritas/sql/generator/function/aggregate.rb +134 -0
  19. data/lib/veritas/sql/generator/function/connective.rb +53 -0
  20. data/lib/veritas/sql/generator/function/numeric.rb +135 -0
  21. data/lib/veritas/sql/generator/function/predicate.rb +266 -0
  22. data/lib/veritas/sql/generator/function/proposition.rb +38 -0
  23. data/lib/veritas/sql/generator/function/string.rb +29 -0
  24. data/lib/veritas/sql/generator/identifier.rb +2 -1
  25. data/lib/veritas/sql/generator/literal.rb +15 -18
  26. data/lib/veritas/sql/generator/relation.rb +144 -17
  27. data/lib/veritas/sql/generator/relation/binary.rb +16 -64
  28. data/lib/veritas/sql/generator/relation/set.rb +30 -22
  29. data/lib/veritas/sql/generator/relation/unary.rb +131 -78
  30. data/lib/veritas/sql/generator/version.rb +1 -1
  31. data/spec/unit/date/iso8601_spec.rb +23 -0
  32. data/spec/unit/date_time/iso_8601_spec.rb +115 -0
  33. data/spec/unit/veritas/sql/generator/class_methods/parenthesize_spec.rb +18 -0
  34. data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_count_spec.rb +16 -0
  35. data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_maximum_spec.rb +16 -0
  36. data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_mean_spec.rb +16 -0
  37. data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_minimum_spec.rb +16 -0
  38. data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_standard_deviation_spec.rb +16 -0
  39. data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_sum_spec.rb +16 -0
  40. data/spec/unit/veritas/sql/generator/function/aggregate/visit_veritas_aggregate_variance_spec.rb +16 -0
  41. data/spec/unit/veritas/sql/generator/function/connective/visit_veritas_function_connective_conjunction_spec.rb +20 -0
  42. data/spec/unit/veritas/sql/generator/function/connective/visit_veritas_function_connective_disjunction_spec.rb +20 -0
  43. data/spec/unit/veritas/sql/generator/function/connective/visit_veritas_function_connective_negation_spec.rb +20 -0
  44. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_absolute_spec.rb +15 -0
  45. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_addition_spec.rb +15 -0
  46. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_division_spec.rb +15 -0
  47. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_exponentiation_spec.rb +15 -0
  48. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_modulo_spec.rb +15 -0
  49. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_multiplication_spec.rb +15 -0
  50. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_square_root_spec.rb +15 -0
  51. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_subtraction_spec.rb +15 -0
  52. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_unary_minus_spec.rb +15 -0
  53. data/spec/unit/veritas/sql/generator/function/numeric/visit_veritas_function_numeric_unary_plus_spec.rb +15 -0
  54. 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
  55. 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
  56. data/spec/unit/veritas/sql/generator/function/predicate/visit_veritas_function_predicate_greater_than_or_equal_to_spec.rb +15 -0
  57. 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
  58. 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
  59. 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
  60. data/spec/unit/veritas/sql/generator/function/predicate/visit_veritas_function_predicate_less_than_or_equal_to_spec.rb +15 -0
  61. 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
  62. data/spec/unit/veritas/sql/generator/function/proposition/visit_veritas_function_proposition_contradiction_spec.rb +15 -0
  63. data/spec/unit/veritas/sql/generator/function/proposition/visit_veritas_function_proposition_tautology_spec.rb +15 -0
  64. data/spec/unit/veritas/sql/generator/function/string/visit_veritas_function_string_length_spec.rb +15 -0
  65. data/spec/unit/veritas/sql/generator/literal/class_methods/dup_frozen_spec.rb +2 -2
  66. data/spec/unit/veritas/sql/generator/relation/binary/base/to_subquery_spec.rb +1 -1
  67. data/spec/unit/veritas/sql/generator/relation/binary/base/{visit_veritas_base_relation_spec.rb → visit_veritas_relation_base_spec.rb} +3 -3
  68. data/spec/unit/veritas/sql/generator/relation/binary/to_s_spec.rb +2 -2
  69. data/spec/unit/veritas/sql/generator/relation/binary/to_subquery_spec.rb +2 -2
  70. data/spec/unit/veritas/sql/generator/relation/binary/visit_veritas_algebra_join_spec.rb +74 -33
  71. data/spec/unit/veritas/sql/generator/relation/binary/visit_veritas_algebra_product_spec.rb +63 -19
  72. data/spec/unit/veritas/sql/generator/relation/class_methods/visit_spec.rb +1 -1
  73. data/spec/unit/veritas/sql/generator/relation/set/class_methods/normalize_operand_headers_spec.rb +35 -0
  74. data/spec/unit/veritas/sql/generator/relation/set/to_s_spec.rb +1 -1
  75. data/spec/unit/veritas/sql/generator/relation/set/to_subquery_spec.rb +4 -4
  76. data/spec/unit/veritas/sql/generator/relation/set/visit_veritas_algebra_difference_spec.rb +83 -30
  77. data/spec/unit/veritas/sql/generator/relation/set/visit_veritas_algebra_intersection_spec.rb +80 -30
  78. data/spec/unit/veritas/sql/generator/relation/set/visit_veritas_algebra_union_spec.rb +80 -30
  79. data/spec/unit/veritas/sql/generator/relation/to_s_spec.rb +50 -0
  80. data/spec/unit/veritas/sql/generator/relation/to_subquery_spec.rb +49 -0
  81. data/spec/unit/veritas/sql/generator/relation/unary/to_s_spec.rb +1 -1
  82. data/spec/unit/veritas/sql/generator/relation/unary/to_subquery_spec.rb +6 -6
  83. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_extension_spec.rb +165 -0
  84. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_projection_spec.rb +84 -29
  85. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_rename_spec.rb +69 -27
  86. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_restriction_spec.rb +64 -22
  87. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_algebra_summarization_spec.rb +652 -0
  88. data/spec/unit/veritas/sql/generator/relation/unary/{visit_veritas_base_relation_spec.rb → visit_veritas_relation_base_spec.rb} +4 -4
  89. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_relation_operation_limit_spec.rb +60 -20
  90. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_relation_operation_offset_spec.rb +60 -20
  91. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_relation_operation_order_spec.rb +63 -23
  92. data/spec/unit/veritas/sql/generator/relation/unary/visit_veritas_relation_operation_reverse_spec.rb +60 -20
  93. data/spec/unit/veritas/sql/generator/relation/visited_spec.rb +1 -1
  94. data/tasks/metrics/ci.rake +7 -0
  95. data/tasks/{quality → metrics}/flay.rake +0 -0
  96. data/tasks/{quality → metrics}/flog.rake +0 -0
  97. data/tasks/{quality → metrics}/heckle.rake +1 -0
  98. data/tasks/{quality → metrics}/metric_fu.rake +3 -0
  99. data/tasks/{quality → metrics}/reek.rake +0 -0
  100. data/tasks/{quality → metrics}/roodi.rake +0 -0
  101. data/tasks/{quality → metrics}/yardstick.rake +0 -0
  102. data/tasks/spec.rake +1 -0
  103. data/veritas-sql-generator.gemspec +82 -114
  104. metadata +137 -125
  105. data/lib/veritas/base_relation.rb +0 -36
  106. data/lib/veritas/sql/generator/logic.rb +0 -349
  107. data/spec/unit/veritas/base_relation/name_spec.rb +0 -45
  108. data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_connective_conjunction_spec.rb +0 -16
  109. data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_connective_disjunction_spec.rb +0 -16
  110. data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_connective_negation_spec.rb +0 -16
  111. data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_predicate_greater_than_or_equal_to_spec.rb +0 -15
  112. data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_predicate_less_than_or_equal_to_spec.rb +0 -15
  113. data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_proposition_contradiction_spec.rb +0 -15
  114. data/spec/unit/veritas/sql/generator/logic/visit_veritas_logic_proposition_tautology_spec.rb +0 -15
  115. data/spec/unit/veritas/sql/generator/relation/binary/class_methods/subquery_spec.rb +0 -42
  116. data/spec/unit/veritas/sql/generator/relation/class_methods/subquery_spec.rb +0 -33
  117. data/tasks/quality/ci.rake +0 -2
@@ -2,8 +2,8 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe SQL::Generator::Relation::Unary, '#visit_veritas_base_relation' do
6
- subject { object.visit_veritas_base_relation(base_relation) }
5
+ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_base' do
6
+ subject { object.visit_veritas_relation_base(base_relation) }
7
7
 
8
8
  let(:relation_name) { 'users' }
9
9
  let(:id) { Attribute::Integer.new(:id) }
@@ -11,11 +11,11 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_base_relation' do
11
11
  let(:age) { Attribute::Integer.new(:age, :required => false) }
12
12
  let(:header) { [ id, name, age ] }
13
13
  let(:body) { [ [ 1, 'Dan Kubb', 35 ] ].each }
14
- let(:base_relation) { BaseRelation.new(relation_name, header, body) }
14
+ let(:base_relation) { Relation::Base.new(relation_name, header, body) }
15
15
  let(:object) { described_class.new }
16
16
 
17
17
  it_should_behave_like 'a generated SQL SELECT query'
18
18
 
19
19
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users"') }
20
- its(:to_subquery) { should eql('SELECT * FROM "users"') }
20
+ its(:to_subquery) { should eql('(SELECT * FROM "users")') }
21
21
  end
@@ -11,7 +11,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
11
11
  let(:age) { Attribute::Integer.new(:age, :required => false) }
12
12
  let(:header) { [ id, name, age ] }
13
13
  let(:body) { [ [ 1, 'Dan Kubb', 35 ] ].each }
14
- let(:base_relation) { BaseRelation.new(relation_name, header, body) }
14
+ let(:base_relation) { Relation::Base.new(relation_name, header, body) }
15
15
  let(:limit) { operand.take(1) }
16
16
  let(:object) { described_class.new }
17
17
 
@@ -21,7 +21,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
21
21
  it_should_behave_like 'a generated SQL SELECT query'
22
22
 
23
23
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id", "name", "age" LIMIT 1') }
24
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1') }
24
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1)') }
25
25
  end
26
26
 
27
27
  context 'when the operand is a projection' do
@@ -29,8 +29,17 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
29
29
 
30
30
  it_should_behave_like 'a generated SQL SELECT query'
31
31
 
32
- its(:to_s) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name" LIMIT 1') }
33
- its(:to_subquery) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name" LIMIT 1') }
32
+ its(:to_s) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name" LIMIT 1') }
33
+ its(:to_subquery) { should eql('(SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name" LIMIT 1)') }
34
+ end
35
+
36
+ context 'when the operand is an extension' do
37
+ let(:operand) { base_relation.extend { |r| r.add(:one, 1) }.order }
38
+
39
+ it_should_behave_like 'a generated SQL SELECT query'
40
+
41
+ its(:to_s) { should eql('SELECT "id", "name", "age", 1 AS "one" FROM "users" ORDER BY "id", "name", "age", "one" LIMIT 1') }
42
+ its(:to_subquery) { should eql('(SELECT *, 1 AS "one" FROM "users" ORDER BY "id", "name", "age", "one" LIMIT 1)') }
34
43
  end
35
44
 
36
45
  context 'when the operand is a rename' do
@@ -38,17 +47,48 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
38
47
 
39
48
  it_should_behave_like 'a generated SQL SELECT query'
40
49
 
41
- its(:to_s) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "id", "name", "age" LIMIT 1') }
42
- its(:to_subquery) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "id", "name", "age" LIMIT 1') }
50
+ its(:to_s) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "id", "name", "age" LIMIT 1') }
51
+ its(:to_subquery) { should eql('(SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "id", "name", "age" LIMIT 1)') }
43
52
  end
44
53
 
45
54
  context 'when the operand is a restriction' do
46
- let(:operand) { base_relation.order.restrict { |r| r[:id].eq(1) } }
55
+ let(:operand) { base_relation.restrict { |r| r[:id].eq(1) }.order }
47
56
 
48
57
  it_should_behave_like 'a generated SQL SELECT query'
49
58
 
50
59
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age" LIMIT 1') }
51
- its(:to_subquery) { should eql('SELECT * FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age" LIMIT 1') }
60
+ its(:to_subquery) { should eql('(SELECT * FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age" LIMIT 1)') }
61
+ end
62
+
63
+ context 'when the operand is a summarization' do
64
+ context 'summarize per table dee' do
65
+ let(:summarize_per) { TABLE_DEE }
66
+ let(:operand) { base_relation.summarize(summarize_per) { |r| r.add(:count, r[:id].count) }.order }
67
+
68
+ it_should_behave_like 'a generated SQL SELECT query'
69
+
70
+ its(:to_s) { should eql('SELECT COUNT ("id") AS "count" FROM "users" ORDER BY "count" LIMIT 1') }
71
+ its(:to_subquery) { should eql('(SELECT COUNT ("id") AS "count" FROM "users" ORDER BY "count" LIMIT 1)') }
72
+ end
73
+
74
+ context 'summarize per table dum' do
75
+ let(:summarize_per) { TABLE_DUM }
76
+ let(:operand) { base_relation.summarize(summarize_per) { |r| r.add(:count, r[:id].count) }.order }
77
+
78
+ it_should_behave_like 'a generated SQL SELECT query'
79
+
80
+ its(:to_s) { should eql('SELECT COUNT ("id") AS "count" FROM "users" HAVING FALSE ORDER BY "count" LIMIT 1') }
81
+ its(:to_subquery) { should eql('(SELECT COUNT ("id") AS "count" FROM "users" HAVING FALSE ORDER BY "count" LIMIT 1)') }
82
+ end
83
+
84
+ context 'summarize by a subset of the operand header' do
85
+ let(:operand) { base_relation.summarize([ :id, :name ]) { |r| r.add(:count, r[:age].count) }.order }
86
+
87
+ it_should_behave_like 'a generated SQL SELECT query'
88
+
89
+ its(:to_s) { should eql('SELECT "id", "name", COUNT ("age") AS "count" FROM "users" GROUP BY "id", "name" HAVING COUNT (*) > 0 ORDER BY "id", "name", "count" LIMIT 1') }
90
+ its(:to_subquery) { should eql('(SELECT "id", "name", COUNT ("age") AS "count" FROM "users" GROUP BY "id", "name" HAVING COUNT (*) > 0 ORDER BY "id", "name", "count" LIMIT 1)') }
91
+ end
52
92
  end
53
93
 
54
94
  context 'when the operand is ordered' do
@@ -57,7 +97,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
57
97
  it_should_behave_like 'a generated SQL SELECT query'
58
98
 
59
99
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id", "name", "age" LIMIT 1') }
60
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1') }
100
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1)') }
61
101
  end
62
102
 
63
103
  context 'when the operand is reversed' do
@@ -66,7 +106,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
66
106
  it_should_behave_like 'a generated SQL SELECT query'
67
107
 
68
108
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC LIMIT 1') }
69
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC LIMIT 1') }
109
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC LIMIT 1)') }
70
110
  end
71
111
 
72
112
  context 'when the operand is limited' do
@@ -75,7 +115,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
75
115
  it_should_behave_like 'a generated SQL SELECT query'
76
116
 
77
117
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1) AS "users" LIMIT 1') }
78
- its(:to_subquery) { should eql('SELECT * FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1) AS "users" LIMIT 1') }
118
+ its(:to_subquery) { should eql('(SELECT * FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1) AS "users" LIMIT 1)') }
79
119
  end
80
120
 
81
121
  context 'when the operand is an offset' do
@@ -84,7 +124,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
84
124
  it_should_behave_like 'a generated SQL SELECT query'
85
125
 
86
126
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id", "name", "age" LIMIT 1 OFFSET 1') }
87
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1 OFFSET 1') }
127
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1 OFFSET 1)') }
88
128
  end
89
129
 
90
130
  context 'when the operand is a difference' do
@@ -92,8 +132,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
92
132
 
93
133
  it_should_behave_like 'a generated SQL SELECT query'
94
134
 
95
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") EXCEPT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
96
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") EXCEPT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
135
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") EXCEPT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
136
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") EXCEPT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1)') }
97
137
  end
98
138
 
99
139
  context 'when the operand is an intersection' do
@@ -101,8 +141,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
101
141
 
102
142
  it_should_behave_like 'a generated SQL SELECT query'
103
143
 
104
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") INTERSECT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
105
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") INTERSECT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
144
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") INTERSECT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
145
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") INTERSECT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1)') }
106
146
  end
107
147
 
108
148
  context 'when the operand is a union' do
@@ -110,8 +150,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
110
150
 
111
151
  it_should_behave_like 'a generated SQL SELECT query'
112
152
 
113
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") UNION (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
114
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") UNION (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
153
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") UNION (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
154
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") UNION (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" LIMIT 1)') }
115
155
  end
116
156
 
117
157
  context 'when the operand is a join' do
@@ -119,7 +159,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_lim
119
159
 
120
160
  it_should_behave_like 'a generated SQL SELECT query'
121
161
 
122
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" NATURAL JOIN "users") AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
123
- its(:to_subquery) { should eql('SELECT * FROM (SELECT * FROM "users" NATURAL JOIN "users") AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
162
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" AS "left" NATURAL JOIN "users" AS "right") AS "users" ORDER BY "id", "name", "age" LIMIT 1') }
163
+ its(:to_subquery) { should eql('(SELECT * FROM (SELECT * FROM "users" AS "left" NATURAL JOIN "users" AS "right") AS "users" ORDER BY "id", "name", "age" LIMIT 1)') }
124
164
  end
125
165
  end
@@ -11,7 +11,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
11
11
  let(:age) { Attribute::Integer.new(:age, :required => false) }
12
12
  let(:header) { [ id, name, age ] }
13
13
  let(:body) { [ [ 1, 'Dan Kubb', 35 ] ].each }
14
- let(:base_relation) { BaseRelation.new(relation_name, header, body) }
14
+ let(:base_relation) { Relation::Base.new(relation_name, header, body) }
15
15
  let(:offset) { operand.drop(1) }
16
16
  let(:object) { described_class.new }
17
17
 
@@ -21,7 +21,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
21
21
  it_should_behave_like 'a generated SQL SELECT query'
22
22
 
23
23
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id", "name", "age" OFFSET 1') }
24
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id", "name", "age" OFFSET 1') }
24
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id", "name", "age" OFFSET 1)') }
25
25
  end
26
26
 
27
27
  context 'when the operand is a projection' do
@@ -29,8 +29,17 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
29
29
 
30
30
  it_should_behave_like 'a generated SQL SELECT query'
31
31
 
32
- its(:to_s) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name" OFFSET 1') }
33
- its(:to_subquery) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name" OFFSET 1') }
32
+ its(:to_s) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name" OFFSET 1') }
33
+ its(:to_subquery) { should eql('(SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name" OFFSET 1)') }
34
+ end
35
+
36
+ context 'when the operand is an extension' do
37
+ let(:operand) { base_relation.extend { |r| r.add(:one, 1) }.order }
38
+
39
+ it_should_behave_like 'a generated SQL SELECT query'
40
+
41
+ its(:to_s) { should eql('SELECT "id", "name", "age", 1 AS "one" FROM "users" ORDER BY "id", "name", "age", "one" OFFSET 1') }
42
+ its(:to_subquery) { should eql('(SELECT *, 1 AS "one" FROM "users" ORDER BY "id", "name", "age", "one" OFFSET 1)') }
34
43
  end
35
44
 
36
45
  context 'when the operand is a rename' do
@@ -38,17 +47,48 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
38
47
 
39
48
  it_should_behave_like 'a generated SQL SELECT query'
40
49
 
41
- its(:to_s) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "id", "name", "age" OFFSET 1') }
42
- its(:to_subquery) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "id", "name", "age" OFFSET 1') }
50
+ its(:to_s) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "id", "name", "age" OFFSET 1') }
51
+ its(:to_subquery) { should eql('(SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "id", "name", "age" OFFSET 1)') }
43
52
  end
44
53
 
45
54
  context 'when the operand is a restriction' do
46
- let(:operand) { base_relation.order.restrict { |r| r[:id].eq(1) } }
55
+ let(:operand) { base_relation.restrict { |r| r[:id].eq(1) }.order }
47
56
 
48
57
  it_should_behave_like 'a generated SQL SELECT query'
49
58
 
50
59
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age" OFFSET 1') }
51
- its(:to_subquery) { should eql('SELECT * FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age" OFFSET 1') }
60
+ its(:to_subquery) { should eql('(SELECT * FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age" OFFSET 1)') }
61
+ end
62
+
63
+ context 'when the operand is a summarization' do
64
+ context 'summarize per table dee' do
65
+ let(:summarize_per) { TABLE_DEE }
66
+ let(:operand) { base_relation.summarize(summarize_per) { |r| r.add(:count, r[:id].count) }.order }
67
+
68
+ it_should_behave_like 'a generated SQL SELECT query'
69
+
70
+ its(:to_s) { should eql('SELECT COUNT ("id") AS "count" FROM "users" ORDER BY "count" OFFSET 1') }
71
+ its(:to_subquery) { should eql('(SELECT COUNT ("id") AS "count" FROM "users" ORDER BY "count" OFFSET 1)') }
72
+ end
73
+
74
+ context 'summarize per table dum' do
75
+ let(:summarize_per) { TABLE_DUM }
76
+ let(:operand) { base_relation.summarize(summarize_per) { |r| r.add(:count, r[:id].count) }.order }
77
+
78
+ it_should_behave_like 'a generated SQL SELECT query'
79
+
80
+ its(:to_s) { should eql('SELECT COUNT ("id") AS "count" FROM "users" HAVING FALSE ORDER BY "count" OFFSET 1') }
81
+ its(:to_subquery) { should eql('(SELECT COUNT ("id") AS "count" FROM "users" HAVING FALSE ORDER BY "count" OFFSET 1)') }
82
+ end
83
+
84
+ context 'summarize by a subset of the operand header' do
85
+ let(:operand) { base_relation.summarize([ :id, :name ]) { |r| r.add(:count, r[:age].count) }.order }
86
+
87
+ it_should_behave_like 'a generated SQL SELECT query'
88
+
89
+ its(:to_s) { should eql('SELECT "id", "name", COUNT ("age") AS "count" FROM "users" GROUP BY "id", "name" HAVING COUNT (*) > 0 ORDER BY "id", "name", "count" OFFSET 1') }
90
+ its(:to_subquery) { should eql('(SELECT "id", "name", COUNT ("age") AS "count" FROM "users" GROUP BY "id", "name" HAVING COUNT (*) > 0 ORDER BY "id", "name", "count" OFFSET 1)') }
91
+ end
52
92
  end
53
93
 
54
94
  context 'when the operand is ordered' do
@@ -57,7 +97,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
57
97
  it_should_behave_like 'a generated SQL SELECT query'
58
98
 
59
99
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id", "name", "age" OFFSET 1') }
60
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id", "name", "age" OFFSET 1') }
100
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id", "name", "age" OFFSET 1)') }
61
101
  end
62
102
 
63
103
  context 'when the operand is reversed' do
@@ -66,7 +106,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
66
106
  it_should_behave_like 'a generated SQL SELECT query'
67
107
 
68
108
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC OFFSET 1') }
69
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC OFFSET 1') }
109
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC OFFSET 1)') }
70
110
  end
71
111
 
72
112
  context 'when the operand is limited' do
@@ -75,7 +115,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
75
115
  it_should_behave_like 'a generated SQL SELECT query'
76
116
 
77
117
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1) AS "users" OFFSET 1') }
78
- its(:to_subquery) { should eql('SELECT * FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1) AS "users" OFFSET 1') }
118
+ its(:to_subquery) { should eql('(SELECT * FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" LIMIT 1) AS "users" OFFSET 1)') }
79
119
  end
80
120
 
81
121
  context 'when the operand is an offset' do
@@ -84,7 +124,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
84
124
  it_should_behave_like 'a generated SQL SELECT query'
85
125
 
86
126
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" OFFSET 1) AS "users" OFFSET 1') }
87
- its(:to_subquery) { should eql('SELECT * FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" OFFSET 1) AS "users" OFFSET 1') }
127
+ its(:to_subquery) { should eql('(SELECT * FROM (SELECT * FROM "users" ORDER BY "id", "name", "age" OFFSET 1) AS "users" OFFSET 1)') }
88
128
  end
89
129
 
90
130
  context 'when the operand is a difference' do
@@ -92,8 +132,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
92
132
 
93
133
  it_should_behave_like 'a generated SQL SELECT query'
94
134
 
95
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") EXCEPT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
96
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") EXCEPT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
135
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") EXCEPT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
136
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") EXCEPT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1)') }
97
137
  end
98
138
 
99
139
  context 'when the operand is an intersection' do
@@ -101,8 +141,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
101
141
 
102
142
  it_should_behave_like 'a generated SQL SELECT query'
103
143
 
104
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") INTERSECT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
105
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") INTERSECT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
144
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") INTERSECT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
145
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") INTERSECT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1)') }
106
146
  end
107
147
 
108
148
  context 'when the operand is a union' do
@@ -110,8 +150,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
110
150
 
111
151
  it_should_behave_like 'a generated SQL SELECT query'
112
152
 
113
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") UNION (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
114
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") UNION (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
153
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") UNION (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
154
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") UNION (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age" OFFSET 1)') }
115
155
  end
116
156
 
117
157
  context 'when the operand is a join' do
@@ -119,7 +159,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_off
119
159
 
120
160
  it_should_behave_like 'a generated SQL SELECT query'
121
161
 
122
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" NATURAL JOIN "users") AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
123
- its(:to_subquery) { should eql('SELECT * FROM (SELECT * FROM "users" NATURAL JOIN "users") AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
162
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" AS "left" NATURAL JOIN "users" AS "right") AS "users" ORDER BY "id", "name", "age" OFFSET 1') }
163
+ its(:to_subquery) { should eql('(SELECT * FROM (SELECT * FROM "users" AS "left" NATURAL JOIN "users" AS "right") AS "users" ORDER BY "id", "name", "age" OFFSET 1)') }
124
164
  end
125
165
  end
@@ -11,7 +11,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
11
11
  let(:age) { Attribute::Integer.new(:age, :required => false) }
12
12
  let(:header) { [ id, name, age ] }
13
13
  let(:body) { [ [ 1, 'Dan Kubb', 35 ] ].each }
14
- let(:base_relation) { BaseRelation.new(relation_name, header, body) }
14
+ let(:base_relation) { Relation::Base.new(relation_name, header, body) }
15
15
  let(:order) { operand.order }
16
16
  let(:object) { described_class.new }
17
17
 
@@ -21,7 +21,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
21
21
  it_should_behave_like 'a generated SQL SELECT query'
22
22
 
23
23
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id", "name", "age"') }
24
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id", "name", "age"') }
24
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id", "name", "age")') }
25
25
  end
26
26
 
27
27
  context 'when the operand is a projection' do
@@ -30,8 +30,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
30
30
 
31
31
  it_should_behave_like 'a generated SQL SELECT query'
32
32
 
33
- its(:to_s) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name"') }
34
- its(:to_subquery) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name"') }
33
+ its(:to_s) { should eql('SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name"') }
34
+ its(:to_subquery) { should eql('(SELECT DISTINCT "id", "name" FROM "users" ORDER BY "id", "name")') }
35
35
  end
36
36
 
37
37
  context 'when the projection contains an order' do
@@ -39,18 +39,27 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
39
39
 
40
40
  it_should_behave_like 'a generated SQL SELECT query'
41
41
 
42
- its(:to_s) { should eql('SELECT DISTINCT "id", "name" FROM (SELECT * FROM "users" ORDER BY "id", "name", "age") AS "users" ORDER BY "id", "name"') }
43
- its(:to_subquery) { should eql('SELECT DISTINCT "id", "name" FROM (SELECT * FROM "users" ORDER BY "id", "name", "age") AS "users" ORDER BY "id", "name"') }
42
+ its(:to_s) { should eql('SELECT DISTINCT "id", "name" FROM (SELECT * FROM "users" ORDER BY "id", "name", "age") AS "users" ORDER BY "id", "name"') }
43
+ its(:to_subquery) { should eql('(SELECT DISTINCT "id", "name" FROM (SELECT * FROM "users" ORDER BY "id", "name", "age") AS "users" ORDER BY "id", "name")') }
44
44
  end
45
45
  end
46
46
 
47
+ context 'when the operand is an extension' do
48
+ let(:operand) { base_relation.extend { |r| r.add(:one, 1) }.order }
49
+
50
+ it_should_behave_like 'a generated SQL SELECT query'
51
+
52
+ its(:to_s) { should eql('SELECT "id", "name", "age", 1 AS "one" FROM "users" ORDER BY "id", "name", "age", "one"') }
53
+ its(:to_subquery) { should eql('(SELECT *, 1 AS "one" FROM "users" ORDER BY "id", "name", "age", "one")') }
54
+ end
55
+
47
56
  context 'when the operand is a rename' do
48
57
  let(:operand) { base_relation.rename(:id => :user_id) }
49
58
 
50
59
  it_should_behave_like 'a generated SQL SELECT query'
51
60
 
52
- its(:to_s) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "user_id", "name", "age"') }
53
- its(:to_subquery) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "user_id", "name", "age"') }
61
+ its(:to_s) { should eql('SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "user_id", "name", "age"') }
62
+ its(:to_subquery) { should eql('(SELECT "id" AS "user_id", "name", "age" FROM "users" ORDER BY "user_id", "name", "age")') }
54
63
  end
55
64
 
56
65
  context 'when the operand is a restriction' do
@@ -59,7 +68,38 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
59
68
  it_should_behave_like 'a generated SQL SELECT query'
60
69
 
61
70
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age"') }
62
- its(:to_subquery) { should eql('SELECT * FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age"') }
71
+ its(:to_subquery) { should eql('(SELECT * FROM "users" WHERE "id" = 1 ORDER BY "id", "name", "age")') }
72
+ end
73
+
74
+ context 'when the operand is a summarization' do
75
+ context 'summarize per table dee' do
76
+ let(:summarize_per) { TABLE_DEE }
77
+ let(:operand) { base_relation.summarize(summarize_per) { |r| r.add(:count, r[:id].count) }.order }
78
+
79
+ it_should_behave_like 'a generated SQL SELECT query'
80
+
81
+ its(:to_s) { should eql('SELECT COUNT ("id") AS "count" FROM "users" ORDER BY "count"') }
82
+ its(:to_subquery) { should eql('(SELECT COUNT ("id") AS "count" FROM "users" ORDER BY "count")') }
83
+ end
84
+
85
+ context 'summarize per table dum' do
86
+ let(:summarize_per) { TABLE_DUM }
87
+ let(:operand) { base_relation.summarize(summarize_per) { |r| r.add(:count, r[:id].count) }.order }
88
+
89
+ it_should_behave_like 'a generated SQL SELECT query'
90
+
91
+ its(:to_s) { should eql('SELECT COUNT ("id") AS "count" FROM "users" HAVING FALSE ORDER BY "count"') }
92
+ its(:to_subquery) { should eql('(SELECT COUNT ("id") AS "count" FROM "users" HAVING FALSE ORDER BY "count")') }
93
+ end
94
+
95
+ context 'summarize by a subset of the operand header' do
96
+ let(:operand) { base_relation.summarize([ :id, :name ]) { |r| r.add(:count, r[:age].count) }.order }
97
+
98
+ it_should_behave_like 'a generated SQL SELECT query'
99
+
100
+ its(:to_s) { should eql('SELECT "id", "name", COUNT ("age") AS "count" FROM "users" GROUP BY "id", "name" HAVING COUNT (*) > 0 ORDER BY "id", "name", "count"') }
101
+ its(:to_subquery) { should eql('(SELECT "id", "name", COUNT ("age") AS "count" FROM "users" GROUP BY "id", "name" HAVING COUNT (*) > 0 ORDER BY "id", "name", "count")') }
102
+ end
63
103
  end
64
104
 
65
105
  context 'when the operand is ordered' do
@@ -68,7 +108,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
68
108
  it_should_behave_like 'a generated SQL SELECT query'
69
109
 
70
110
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id", "name", "age"') }
71
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id", "name", "age"') }
111
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id", "name", "age")') }
72
112
  end
73
113
 
74
114
  context 'when the operand is reversed' do
@@ -77,25 +117,25 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
77
117
  it_should_behave_like 'a generated SQL SELECT query'
78
118
 
79
119
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM "users" ORDER BY "id", "name", "age"') }
80
- its(:to_subquery) { should eql('SELECT * FROM "users" ORDER BY "id", "name", "age"') }
120
+ its(:to_subquery) { should eql('(SELECT * FROM "users" ORDER BY "id", "name", "age")') }
81
121
  end
82
122
 
83
123
  context 'when the operand is limited' do
84
- let(:operand) { base_relation.order([ id.desc, name.desc, age.desc ]).take(1) }
124
+ let(:operand) { base_relation.order { [ id.desc, name.desc, age.desc ] }.take(1) }
85
125
 
86
126
  it_should_behave_like 'a generated SQL SELECT query'
87
127
 
88
128
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC LIMIT 1) AS "users" ORDER BY "id", "name", "age"') }
89
- its(:to_subquery) { should eql('SELECT * FROM (SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC LIMIT 1) AS "users" ORDER BY "id", "name", "age"') }
129
+ its(:to_subquery) { should eql('(SELECT * FROM (SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC LIMIT 1) AS "users" ORDER BY "id", "name", "age")') }
90
130
  end
91
131
 
92
132
  context 'when the operand is an offset' do
93
- let(:operand) { base_relation.order([ id.desc, name.desc, age.desc ]).drop(1) }
133
+ let(:operand) { base_relation.order { [ id.desc, name.desc, age.desc ] }.drop(1) }
94
134
 
95
135
  it_should_behave_like 'a generated SQL SELECT query'
96
136
 
97
137
  its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC OFFSET 1) AS "users" ORDER BY "id", "name", "age"') }
98
- its(:to_subquery) { should eql('SELECT * FROM (SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC OFFSET 1) AS "users" ORDER BY "id", "name", "age"') }
138
+ its(:to_subquery) { should eql('(SELECT * FROM (SELECT * FROM "users" ORDER BY "id" DESC, "name" DESC, "age" DESC OFFSET 1) AS "users" ORDER BY "id", "name", "age")') }
99
139
  end
100
140
 
101
141
  context 'when the operand is a difference' do
@@ -103,8 +143,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
103
143
 
104
144
  it_should_behave_like 'a generated SQL SELECT query'
105
145
 
106
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") EXCEPT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
107
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") EXCEPT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
146
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") EXCEPT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
147
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") EXCEPT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age")') }
108
148
  end
109
149
 
110
150
  context 'when the operand is an intersection' do
@@ -112,8 +152,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
112
152
 
113
153
  it_should_behave_like 'a generated SQL SELECT query'
114
154
 
115
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") INTERSECT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
116
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") INTERSECT (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
155
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") INTERSECT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
156
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") INTERSECT (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age")') }
117
157
  end
118
158
 
119
159
  context 'when the operand is a union' do
@@ -121,8 +161,8 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
121
161
 
122
162
  it_should_behave_like 'a generated SQL SELECT query'
123
163
 
124
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT * FROM "users") UNION (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
125
- its(:to_subquery) { should eql('SELECT * FROM ((SELECT * FROM "users") UNION (SELECT * FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
164
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM ((SELECT "id", "name", "age" FROM "users") UNION (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age"') }
165
+ its(:to_subquery) { should eql('(SELECT * FROM ((SELECT "id", "name", "age" FROM "users") UNION (SELECT "id", "name", "age" FROM "users")) AS "users" ORDER BY "id", "name", "age")') }
126
166
  end
127
167
 
128
168
  context 'when the operand is a join' do
@@ -130,7 +170,7 @@ describe SQL::Generator::Relation::Unary, '#visit_veritas_relation_operation_ord
130
170
 
131
171
  it_should_behave_like 'a generated SQL SELECT query'
132
172
 
133
- its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" NATURAL JOIN "users") AS "users" ORDER BY "id", "name", "age"') }
134
- its(:to_subquery) { should eql('SELECT * FROM (SELECT * FROM "users" NATURAL JOIN "users") AS "users" ORDER BY "id", "name", "age"') }
173
+ its(:to_s) { should eql('SELECT "id", "name", "age" FROM (SELECT * FROM "users" AS "left" NATURAL JOIN "users" AS "right") AS "users" ORDER BY "id", "name", "age"') }
174
+ its(:to_subquery) { should eql('(SELECT * FROM (SELECT * FROM "users" AS "left" NATURAL JOIN "users" AS "right") AS "users" ORDER BY "id", "name", "age")') }
135
175
  end
136
176
  end