arel_extensions 1.2.25 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -7
  3. data/.travis.yml +91 -61
  4. data/Gemfile +20 -15
  5. data/README.md +12 -17
  6. data/Rakefile +29 -40
  7. data/appveyor.yml +1 -1
  8. data/arel_extensions.gemspec +3 -3
  9. data/functions.html +3 -3
  10. data/gemfiles/rails3.gemfile +9 -9
  11. data/gemfiles/rails4.gemfile +13 -13
  12. data/gemfiles/rails5_0.gemfile +13 -13
  13. data/gemfiles/rails5_1_4.gemfile +13 -13
  14. data/gemfiles/rails5_2.gemfile +13 -13
  15. data/init/mssql.sql +4 -4
  16. data/init/mysql.sql +38 -38
  17. data/init/postgresql.sql +21 -21
  18. data/lib/arel_extensions/attributes.rb +1 -0
  19. data/lib/arel_extensions/boolean_functions.rb +14 -55
  20. data/lib/arel_extensions/common_sql_functions.rb +8 -7
  21. data/lib/arel_extensions/comparators.rb +15 -14
  22. data/lib/arel_extensions/date_duration.rb +5 -4
  23. data/lib/arel_extensions/insert_manager.rb +16 -17
  24. data/lib/arel_extensions/math.rb +12 -11
  25. data/lib/arel_extensions/math_functions.rb +22 -29
  26. data/lib/arel_extensions/nodes/abs.rb +1 -0
  27. data/lib/arel_extensions/nodes/blank.rb +1 -0
  28. data/lib/arel_extensions/nodes/case.rb +8 -11
  29. data/lib/arel_extensions/nodes/cast.rb +2 -4
  30. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  31. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  32. data/lib/arel_extensions/nodes/coalesce.rb +3 -2
  33. data/lib/arel_extensions/nodes/collate.rb +2 -1
  34. data/lib/arel_extensions/nodes/concat.rb +16 -7
  35. data/lib/arel_extensions/nodes/date_diff.rb +13 -10
  36. data/lib/arel_extensions/nodes/duration.rb +3 -0
  37. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  38. data/lib/arel_extensions/nodes/floor.rb +1 -1
  39. data/lib/arel_extensions/nodes/format.rb +8 -34
  40. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  41. data/lib/arel_extensions/nodes/function.rb +16 -25
  42. data/lib/arel_extensions/nodes/json.rb +36 -43
  43. data/lib/arel_extensions/nodes/length.rb +0 -5
  44. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  45. data/lib/arel_extensions/nodes/locate.rb +1 -0
  46. data/lib/arel_extensions/nodes/log10.rb +2 -1
  47. data/lib/arel_extensions/nodes/matches.rb +6 -4
  48. data/lib/arel_extensions/nodes/md5.rb +1 -0
  49. data/lib/arel_extensions/nodes/power.rb +5 -5
  50. data/lib/arel_extensions/nodes/rand.rb +1 -0
  51. data/lib/arel_extensions/nodes/repeat.rb +4 -2
  52. data/lib/arel_extensions/nodes/replace.rb +6 -22
  53. data/lib/arel_extensions/nodes/round.rb +6 -5
  54. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  55. data/lib/arel_extensions/nodes/std.rb +21 -18
  56. data/lib/arel_extensions/nodes/substring.rb +16 -8
  57. data/lib/arel_extensions/nodes/then.rb +0 -0
  58. data/lib/arel_extensions/nodes/trim.rb +5 -3
  59. data/lib/arel_extensions/nodes/union.rb +5 -2
  60. data/lib/arel_extensions/nodes/union_all.rb +3 -0
  61. data/lib/arel_extensions/nodes/wday.rb +4 -0
  62. data/lib/arel_extensions/nodes.rb +1 -1
  63. data/lib/arel_extensions/null_functions.rb +7 -5
  64. data/lib/arel_extensions/predications.rb +34 -35
  65. data/lib/arel_extensions/railtie.rb +5 -5
  66. data/lib/arel_extensions/set_functions.rb +4 -2
  67. data/lib/arel_extensions/string_functions.rb +23 -52
  68. data/lib/arel_extensions/tasks.rb +5 -5
  69. data/lib/arel_extensions/version.rb +1 -1
  70. data/lib/arel_extensions/visitors/ibm_db.rb +12 -5
  71. data/lib/arel_extensions/visitors/mssql.rb +58 -64
  72. data/lib/arel_extensions/visitors/mysql.rb +98 -149
  73. data/lib/arel_extensions/visitors/oracle.rb +70 -73
  74. data/lib/arel_extensions/visitors/oracle12.rb +15 -2
  75. data/lib/arel_extensions/visitors/postgresql.rb +63 -116
  76. data/lib/arel_extensions/visitors/sqlite.rb +70 -83
  77. data/lib/arel_extensions/visitors/to_sql.rb +110 -142
  78. data/lib/arel_extensions/visitors.rb +60 -68
  79. data/lib/arel_extensions.rb +19 -81
  80. data/test/database.yml +0 -2
  81. data/test/helper.rb +18 -0
  82. data/test/real_db_test.rb +43 -28
  83. data/test/support/fake_record.rb +2 -2
  84. data/test/test_comparators.rb +12 -9
  85. data/test/visitors/test_bulk_insert_oracle.rb +8 -8
  86. data/test/visitors/test_bulk_insert_sqlite.rb +10 -9
  87. data/test/visitors/test_bulk_insert_to_sql.rb +10 -8
  88. data/test/visitors/test_oracle.rb +42 -42
  89. data/test/visitors/test_to_sql.rb +196 -361
  90. data/test/with_ar/all_agnostic_test.rb +160 -195
  91. data/test/with_ar/insert_agnostic_test.rb +4 -3
  92. data/test/with_ar/test_bulk_sqlite.rb +9 -6
  93. data/test/with_ar/test_math_sqlite.rb +12 -8
  94. data/test/with_ar/test_string_mysql.rb +11 -5
  95. data/test/with_ar/test_string_sqlite.rb +12 -4
  96. metadata +11 -22
  97. data/.github/workflows/ruby.yml +0 -102
  98. data/gemfiles/rails6.gemfile +0 -30
  99. data/gemfiles/rails6_1.gemfile +0 -30
  100. data/gemspecs/arel_extensions-v1.gemspec +0 -28
  101. data/gemspecs/arel_extensions-v2.gemspec +0 -28
  102. data/generate_gems.sh +0 -15
  103. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  104. data/lib/arel_extensions/nodes/sum.rb +0 -7
  105. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  106. data/test/arelx_test_helper.rb +0 -26
  107. data/version_v1.rb +0 -3
  108. data/version_v2.rb +0 -3
@@ -1,89 +1,81 @@
1
- require 'arel_extensions/visitors/convert_format'
2
1
  require 'arel_extensions/visitors/to_sql'
3
2
  require 'arel_extensions/visitors/mysql'
3
+ require 'arel_extensions/visitors/oracle'
4
+ require 'arel_extensions/visitors/oracle12'
4
5
  require 'arel_extensions/visitors/postgresql'
5
6
  require 'arel_extensions/visitors/sqlite'
7
+ require 'arel_extensions/visitors/mssql'
6
8
 
7
- if defined?(Arel::Visitors::Oracle)
8
- require 'arel_extensions/visitors/oracle'
9
- require 'arel_extensions/visitors/oracle12'
10
- end
11
-
12
- if defined?(Arel::Visitors::MSSQL)
13
- require 'arel_extensions/visitors/mssql'
9
+ Arel::Visitors::MSSQL.class_eval do
10
+ include ArelExtensions::Visitors::MSSQL
14
11
 
15
- class Arel::Visitors::MSSQL
16
- include ArelExtensions::Visitors::MSSQL
17
-
18
- alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
19
- def visit_Arel_Nodes_As o, collector
20
- if o.left.is_a?(Arel::Nodes::Binary)
21
- collector << '('
22
- collector = visit o.left, collector
23
- collector << ')'
24
- else
25
- collector = visit o.left, collector
26
- end
27
- collector << " AS ["
28
- collector = visit o.right, collector
29
- collector << "]"
30
- collector
12
+ alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
13
+ def visit_Arel_Nodes_As o, collector
14
+ if o.left.is_a?(Arel::Nodes::Binary)
15
+ collector << '('
16
+ collector = visit o.left, collector
17
+ collector << ')'
18
+ else
19
+ collector = visit o.left, collector
31
20
  end
21
+ collector << " AS ["
22
+ collector = visit o.right, collector
23
+ collector << "]"
24
+ collector
25
+ end
32
26
 
33
- alias_method(:old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement) rescue nil
34
- def visit_Arel_Nodes_SelectStatement o, collector
35
- if !collector.value.blank? && o.limit.blank? && o.offset.blank?
36
- o = o.dup
37
- o.orders = []
38
- end
39
- old_visit_Arel_Nodes_SelectStatement(o,collector)
27
+ alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
28
+ def visit_Arel_Nodes_SelectStatement o, collector
29
+ if !collector.value.blank? && o.limit.blank? && o.offset.blank?
30
+ o = o.dup
31
+ o.orders = []
40
32
  end
33
+ old_visit_Arel_Nodes_SelectStatement(o,collector)
41
34
  end
35
+ end
42
36
 
43
- begin
44
- require 'arel_sqlserver'
45
- if Arel::VERSION.to_i == 6
46
- if Arel::Visitors::VISITORS['sqlserver'] && Arel::Visitors::VISITORS['sqlserver'] != Arel::Visitors::MSSQL
47
- Arel::Visitors::VISITORS['sqlserver'].class_eval do
48
- include ArelExtensions::Visitors::MSSQL
37
+ begin
38
+ require 'arel_sqlserver'
39
+ if Arel::VERSION.to_i == 6
40
+ if Arel::Visitors::VISITORS['sqlserver'] && Arel::Visitors::VISITORS['sqlserver'] != Arel::Visitors::MSSQL
41
+ Arel::Visitors::VISITORS['sqlserver'].class_eval do
42
+ include ArelExtensions::Visitors::MSSQL
49
43
 
50
- alias_method(:old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement) rescue nil
51
- def visit_Arel_Nodes_SelectStatement o, collector
52
- if !collector.value.blank? && o.limit.blank? && o.offset.blank?
53
- o = o.dup
54
- o.orders = []
55
- end
56
- old_visit_Arel_Nodes_SelectStatement(o,collector)
44
+ alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
45
+ def visit_Arel_Nodes_SelectStatement o, collector
46
+ if !collector.value.blank? && o.limit.blank? && o.offset.blank?
47
+ o = o.dup
48
+ o.orders = []
57
49
  end
50
+ old_visit_Arel_Nodes_SelectStatement(o,collector)
51
+ end
58
52
 
59
- alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
60
- def visit_Arel_Nodes_As o, collector
61
- if o.left.is_a?(Arel::Nodes::Binary)
62
- collector << '('
63
- collector = visit o.left, collector
64
- collector << ')'
65
- else
66
- collector = visit o.left, collector
67
- end
68
- collector << " AS ["
69
- collector = visit o.right, collector
70
- collector << "]"
71
- collector
53
+ alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
54
+ def visit_Arel_Nodes_As o, collector
55
+ if o.left.is_a?(Arel::Nodes::Binary)
56
+ collector << '('
57
+ collector = visit o.left, collector
58
+ collector << ')'
59
+ else
60
+ collector = visit o.left, collector
72
61
  end
62
+ collector << " AS ["
63
+ collector = visit o.right, collector
64
+ collector << "]"
65
+ collector
66
+ end
73
67
 
74
- alias_method(:old_primary_Key_From_Table, :primary_Key_From_Table) rescue nil
75
- def primary_Key_From_Table t
76
- return unless t
77
-
78
- column_name = @connection.schema_cache.primary_keys(t.name) ||
79
- @connection.schema_cache.columns_hash(t.name).first.try(:second).try(:name)
80
- column_name ? t[column_name] : nil
81
- end
68
+ alias_method :old_primary_Key_From_Table, :primary_Key_From_Table
69
+ def primary_Key_From_Table t
70
+ return unless t
71
+ column_name = @connection.schema_cache.primary_keys(t.name) ||
72
+ @connection.schema_cache.columns_hash(t.name).first.try(:second).try(:name)
73
+ column_name ? t[column_name] : nil
82
74
  end
83
75
  end
84
76
  end
85
- rescue LoadError
86
- rescue => e
87
- e
88
77
  end
78
+ rescue LoadError
79
+ rescue => e
80
+ e
89
81
  end
@@ -7,45 +7,35 @@ require 'arel_extensions/railtie' if defined?(Rails::Railtie)
7
7
  # Count|NamedFunction < Function < Arel::Nodes::Node
8
8
 
9
9
  # pure Arel internals improvements
10
- class Arel::Nodes::Binary
10
+ Arel::Nodes::Binary.class_eval do
11
11
  include Arel::AliasPredication
12
12
  include Arel::Expressions
13
13
  end
14
14
 
15
- class Arel::Nodes::Casted
15
+ Arel::Nodes::Casted.class_eval do
16
16
  include Arel::AliasPredication
17
-
18
- # They forget to define hash.
19
- if Gem::Version.new(Arel::VERSION) < Gem::Version.new("10.0.0")
20
- def hash
21
- [self.class, self.val, self.attribute].hash
22
- end
23
- end
24
17
  end
25
18
 
26
- class Arel::Nodes::Unary
19
+ Arel::Nodes::Unary.class_eval do
27
20
  include Arel::Math
28
21
  include Arel::AliasPredication
29
22
  include Arel::Expressions
30
23
  end
31
24
 
32
- class Arel::Nodes::Grouping
25
+ Arel::Nodes::Grouping.class_eval do
26
+ include Arel::Math
27
+ include Arel::AliasPredication
33
28
  include Arel::OrderPredications
29
+ include Arel::Expressions
34
30
  end
35
31
 
36
- class Arel::Nodes::Ordering
37
- def eql? other
38
- self.hash.eql? other.hash
39
- end
40
- end
41
-
42
- class Arel::Nodes::Function
32
+ Arel::Nodes::Function.class_eval do
43
33
  include Arel::Math
44
34
  include Arel::Expressions
45
35
  end
46
36
 
47
- if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("7.1.0")
48
- class Arel::Nodes::Case
37
+ if Arel::VERSION >= "7.1.0"
38
+ Arel::Nodes::Case.class_eval do
49
39
  include Arel::Math
50
40
  include Arel::Expressions
51
41
  end
@@ -103,26 +93,14 @@ module Arel
103
93
  ArelExtensions::Nodes::Duration.new(s.to_s+'i',expr)
104
94
  end
105
95
 
106
- def self.true
107
- Arel::Nodes::Equality.new(1,1)
108
- end
109
-
110
- def self.false
111
- Arel::Nodes::Equality.new(1,0)
112
- end
113
-
114
- def self.tuple *v
115
- tmp = Arel::Nodes::Grouping.new(nil)
116
- Arel::Nodes::Grouping.new(v.map{|e| tmp.convert_to_node(e)})
117
- end
118
96
  end
119
97
 
120
- class Arel::Attributes::Attribute
98
+ Arel::Attributes::Attribute.class_eval do
121
99
  include Arel::Math
122
100
  include ArelExtensions::Attributes
123
101
  end
124
102
 
125
- class Arel::Nodes::Function
103
+ Arel::Nodes::Function.class_eval do
126
104
  include ArelExtensions::Math
127
105
  include ArelExtensions::Comparators
128
106
  include ArelExtensions::DateDuration
@@ -131,84 +109,44 @@ class Arel::Nodes::Function
131
109
  include ArelExtensions::BooleanFunctions
132
110
  include ArelExtensions::NullFunctions
133
111
  include ArelExtensions::Predications
134
-
135
- alias_method(:old_as, :as) rescue nil
136
- def as other
137
- res = Arel::Nodes::As.new(self.clone, Arel.sql(other))
138
- if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("9.0.0")
139
- self.alias = Arel.sql(other)
140
- end
141
- res
142
- end
143
- end
144
-
145
- class Arel::Nodes::Grouping
146
- include ArelExtensions::Math
147
- include ArelExtensions::Comparators
148
- include ArelExtensions::DateDuration
149
- include ArelExtensions::MathFunctions
150
- include ArelExtensions::NullFunctions
151
- include ArelExtensions::StringFunctions
152
- include ArelExtensions::Predications
153
112
  end
154
113
 
155
- class Arel::Nodes::Unary
114
+ Arel::Nodes::Unary.class_eval do
156
115
  include ArelExtensions::Math
157
116
  include ArelExtensions::Attributes
158
117
  include ArelExtensions::MathFunctions
159
118
  include ArelExtensions::Comparators
160
119
  include ArelExtensions::Predications
161
- def eql? other
162
- hash == other.hash
163
- end
164
120
  end
165
121
 
166
- class Arel::Nodes::Binary
122
+ Arel::Nodes::Binary.class_eval do
167
123
  include ArelExtensions::Math
168
124
  include ArelExtensions::Attributes
169
125
  include ArelExtensions::MathFunctions
170
126
  include ArelExtensions::Comparators
171
127
  include ArelExtensions::BooleanFunctions
172
128
  include ArelExtensions::Predications
173
- def eql? other
174
- hash == other.hash
175
- end
176
129
  end
177
130
 
178
- class Arel::Nodes::Equality
131
+ Arel::Nodes::Equality.class_eval do
179
132
  include ArelExtensions::Comparators
180
133
  include ArelExtensions::DateDuration
181
134
  include ArelExtensions::MathFunctions
182
135
  include ArelExtensions::StringFunctions
183
136
  end
184
137
 
185
- class Arel::InsertManager
138
+
139
+ Arel::InsertManager.class_eval do
186
140
  include ArelExtensions::InsertManager
187
141
  end
188
142
 
189
- class Arel::SelectManager
143
+ Arel::SelectManager.class_eval do
190
144
  include ArelExtensions::SetFunctions
191
145
  include ArelExtensions::Nodes
192
146
  end
193
147
 
194
- class Arel::Nodes::As
148
+ Arel::Nodes::As.class_eval do
195
149
  include ArelExtensions::Nodes
196
150
  end
197
151
 
198
- class Arel::Table
199
- alias_method(:old_alias, :alias) rescue nil
200
- def alias(name = "#{self.name}_2")
201
- name.blank? ? self : Arel::Nodes::TableAlias.new(self,name)
202
- end
203
- end
204
152
 
205
- class Arel::Nodes::TableAlias
206
- def method_missing(*args)
207
- met = args.shift.to_sym
208
- if self.relation.respond_to?(met)
209
- self.relation.send(met,args)
210
- else
211
- super(met,*args)
212
- end
213
- end
214
- end
data/test/database.yml CHANGED
@@ -10,8 +10,6 @@ mysql:
10
10
  adapter: mysql2
11
11
  database: arext_test
12
12
  username: travis
13
- host: 127.0.0.1
14
- port: 3306
15
13
  encoding: utf8
16
14
  jdbc-mysql:
17
15
  adapter: jdbcmysql
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'fileutils'
4
+ require 'arel'
5
+ require 'active_record'
6
+
7
+ require 'arel_extensions'
8
+
9
+ require 'support/fake_record'
10
+ Arel::Table.engine = FakeRecord::Base.new
11
+
12
+ $arel_silence_type_casting_deprecation = true
13
+
14
+ class Object
15
+ def must_be_like other
16
+ gsub(/\s+/, ' ').strip.must_equal other.gsub(/\s+/, ' ').strip
17
+ end
18
+ end
data/test/real_db_test.rb CHANGED
@@ -21,15 +21,15 @@ def setup_db
21
21
  db.enable_load_extension(0)
22
22
  rescue => e
23
23
  $load_extension_disabled = true
24
- $stderr << "cannot load extensions #{e.inspect}\n"
24
+ $stderr << "can not load extensions #{e.inspect}\n"
25
25
  end
26
26
  end
27
- # function find_in_set
27
+ #function find_in_set
28
28
  db.create_function("find_in_set", 1) do |func, value1, value2|
29
29
  func.result = value1.index(value2)
30
30
  end
31
31
  end
32
- @cnx.drop_table(:users) rescue nil
32
+ @cnx.drop_table(:users) rescue nil
33
33
  @cnx.create_table :users do |t|
34
34
  t.column :age, :integer
35
35
  t.column :name, :string
@@ -44,13 +44,15 @@ def teardown_db
44
44
  end
45
45
 
46
46
  class User < ActiveRecord::Base
47
+
47
48
  end
48
49
 
49
50
  class ListTest < Minitest::Test
51
+
50
52
  def setup
51
53
  d = Date.new(2016,05,23)
52
54
  setup_db
53
- User.create :age => 5, :name => "Lucas", :created_at => d, :score => 20.16
55
+ User.create :age => 5, :name => "Lucas", :created_at => d , :score => 20.16
54
56
  User.create :age => 15, :name => "Sophie", :created_at => d, :score => 20.16
55
57
  User.create :age => 20, :name => "Camille", :created_at => d, :score => 20.16
56
58
  User.create :age => 21, :name => "Arthur", :created_at => d, :score => 65.62
@@ -88,28 +90,30 @@ class ListTest < Minitest::Test
88
90
  end
89
91
 
90
92
  def test_Comparator
91
- assert_equal 2,User.where(User.arel_table[:age] < 6).count
92
- assert_equal 2,User.where(User.arel_table[:age] <=10).count
93
- assert_equal 3,User.where(User.arel_table[:age] > 20).count
94
- assert_equal 4,User.where(User.arel_table[:age] >=20).count
95
- assert_equal 1,User.where(User.arel_table[:age] > 5).where(User.arel_table[:age] < 20).count
93
+ assert_equal 2,User.where(User.arel_table[:age] < 6 ).count
94
+ assert_equal 2,User.where(User.arel_table[:age] <=10 ).count
95
+ assert_equal 3,User.where(User.arel_table[:age] > 20 ).count
96
+ assert_equal 4,User.where(User.arel_table[:age] >=20 ).count
97
+ assert_equal 1,User.where(User.arel_table[:age] > 5 ).where(User.arel_table[:age] < 20 ).count
96
98
  end
97
99
 
98
100
  def test_date_duration
99
- # Year
101
+ #Year
100
102
  assert_equal 2016,User.where(User.arel_table[:name].eq("Lucas")).select((User.arel_table[:created_at].year).as("res")).first.res.to_i
101
103
  assert_equal 0,User.where(User.arel_table[:created_at].year.eq("2012")).count
102
- # Month
104
+ #Month
103
105
  assert_equal 5,User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:created_at].month).as("res")).first.res.to_i
104
106
  assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
105
- # Week
107
+ #Week
106
108
  assert_equal 21,User.where(User.arel_table[:name].eq("Arthur")).select((User.arel_table[:created_at].week).as("res")).first.res.to_i
107
109
  assert_equal 8,User.where(User.arel_table[:created_at].month.eq("05")).count
108
- # Day
110
+ #Day
109
111
  assert_equal 23,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:created_at].day).as("res")).first.res.to_i
110
112
  assert_equal 0,User.where(User.arel_table[:created_at].day.eq("05")).count
111
113
  end
112
114
 
115
+
116
+
113
117
  def test_length
114
118
  assert_equal 7,User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].length).as("res")).first.res
115
119
  assert_equal 5,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:name].length).as("res")).first.res
@@ -132,6 +136,7 @@ class ListTest < Minitest::Test
132
136
  end
133
137
  end
134
138
 
139
+
135
140
  def test_floor
136
141
  if !$sqlite || !$load_extension_disabled
137
142
  assert_equal 0,User.where(User.arel_table[:name].eq("Negatif")).select((User.arel_table[:score].floor).as("res")).first.res
@@ -139,51 +144,57 @@ class ListTest < Minitest::Test
139
144
  end
140
145
  end
141
146
 
147
+
142
148
  def test_findinset
143
149
  db = ActiveRecord::Base.connection.raw_connection
144
- assert_equal 3,db.get_first_value("select find_in_set(name,'i') from users where name = 'Camille'")
145
- assert_equal "",db.get_first_value("select find_in_set(name,'p') from users where name = 'Camille'").to_s
146
- # number
147
- # assert_equal 1,User.select(User.arel_table[:name] & ("l")).count
148
- # assert_equal 3,(User.select(User.arel_table[:age] & [5,15,20]))
149
- # string
150
+ assert_equal 3,db.get_first_value( "select find_in_set(name,'i') from users where name = 'Camille'" )
151
+ assert_equal "",db.get_first_value( "select find_in_set(name,'p') from users where name = 'Camille'" ).to_s
152
+ #number
153
+ #assert_equal 1,User.select(User.arel_table[:name] & ("l")).count
154
+ #assert_equal 3,(User.select(User.arel_table[:age] & [5,15,20]))
155
+ #string
150
156
  end
151
157
 
158
+
152
159
  def test_math_plus
153
160
  d = Date.new(1997,06,15)
154
- # Concat String
161
+ #Concat String
155
162
  assert_equal "SophiePhan",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + "Phan").as("res")).first.res
156
- assert_equal "Sophie2",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + 2).as("res")).first.res
163
+ assert_equal "Sophie2",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + 2 ).as("res")).first.res
157
164
  assert_equal "Sophie1997-06-15",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + d).as("res")).first.res
158
165
  assert_equal "Sophie15",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:age]).as("res")).first.res
159
166
  assert_equal "SophieSophie",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:name]).as("res")).first.res
160
167
  assert_equal "Sophie2016-05-23",User.where(User.arel_table[:name].eq("Sophie")).select((User.arel_table[:name] + User.arel_table[:created_at]).as("res")).first.res
161
- # concat Integer
168
+ #concat Integer
162
169
  assert_equal 1, User.where((User.arel_table[:age] + 10).eq(33)).count
163
170
  assert_equal 1, User.where((User.arel_table[:age] + "1").eq(6)).count
164
171
  assert_equal 1, User.where((User.arel_table[:age] + User.arel_table[:age]).eq(10)).count
165
- # concat Date
166
- # puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
172
+ #concat Date
173
+ # puts((User.arel_table[:created_at] + 1).as("res").to_sql.inspect)
167
174
  assert_equal "2016-05-24", @myung.select((User.arel_table[:created_at] + 1).as("res")).first.res.to_date.to_s
168
175
  assert_equal "2016-05-25", @myung.select((User.arel_table[:created_at] + 2.day).as("res")).first.res.to_date.to_s
169
176
  end
170
177
 
178
+
171
179
  def test_math_moins
172
180
  d = Date.new(2016,05,20)
173
- # Datediff
181
+ #Datediff
174
182
  assert_equal 8,User.where((User.arel_table[:created_at] - User.arel_table[:created_at]).eq(0)).count
175
183
  assert_equal 3,User.where(User.arel_table[:name].eq("Laure")).select((User.arel_table[:created_at] - d).as("res")).first.res.abs.to_i
176
- # Substraction
184
+ #Substraction
177
185
  assert_equal 0, User.where((User.arel_table[:age] - 10).eq(50)).count
178
186
  assert_equal 0, User.where((User.arel_table[:age] - "10").eq(50)).count
179
187
  end
180
188
 
189
+
190
+
181
191
  def test_rand
182
192
  assert_equal 5,User.where(User.arel_table[:score].eq(20.16)).select(User.arel_table[:id]).order(Arel.rand).take(50).count
183
- # test_alias :random :rand
193
+ #test_alias :random :rand
184
194
  assert_equal 8,User.select(User.arel_table[:name]).order(Arel.rand).take(50).count
185
195
  end
186
196
 
197
+
187
198
  def test_regexp_not_regex
188
199
  if !$sqlite || !$load_extension_disabled
189
200
  assert_equal 1, User.where(User.arel_table[:name] =~ '^M').count
@@ -196,11 +207,13 @@ class ListTest < Minitest::Test
196
207
  assert_equal "replace",User.where(User.arel_table[:name].eq("Lucas")).select(((User.arel_table[:name]).replace(User.arel_table[:name],"replace")).as("res")).first.res
197
208
  end
198
209
 
210
+
199
211
  def test_round
200
212
  assert_equal 1, User.where(((User.arel_table[:age]).round(0)).eq(5.0)).count
201
213
  assert_equal 0, User.where(((User.arel_table[:age]).round(-1)).eq(6.0)).count
202
214
  end
203
215
 
216
+
204
217
  def test_Soundex
205
218
  if !$sqlite || !$load_extension_disabled
206
219
  assert_equal "C540",User.where(User.arel_table[:name].eq("Camille")).select((User.arel_table[:name].soundex).as("res")).first.res.to_s
@@ -209,13 +222,14 @@ class ListTest < Minitest::Test
209
222
  end
210
223
 
211
224
  def test_Sum
212
- # .take(50) because of limit by ORDER BY
225
+ #.take(50) because of limit by ORDER BY
213
226
  assert_equal 110,User.select((User.arel_table[:age].sum + 1).as("res")).take(50).first.res
214
227
  assert_equal 218,User.select((User.arel_table[:age].sum + User.arel_table[:age].sum).as("res")).take(50).first.res
215
228
  assert_equal 327,User.select(((User.arel_table[:age] * 3).sum).as("res")).take(50).first.res
216
229
  assert_equal 2245,User.select(((User.arel_table[:age] * User.arel_table[:age]).sum).as("res")).take(50).first.res
217
230
  end
218
231
 
232
+
219
233
  def test_trim
220
234
  assert_equal "Myun",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].rtrim("g").as("res")).first.res
221
235
  assert_equal "yung",User.where(User.arel_table[:name].eq("Myung")).select(User.arel_table[:name].ltrim("M").as("res")).first.res
@@ -229,4 +243,5 @@ class ListTest < Minitest::Test
229
243
  assert_equal 1,User.where(User.arel_table[:name].eq("Myung")).select((User.arel_table[:created_at].wday).as("res")).first.res.to_i
230
244
  assert_equal 0,User.select(d.wday).as("res").first.to_i
231
245
  end
246
+
232
247
  end
@@ -7,7 +7,7 @@ module FakeRecord
7
7
  attr_accessor :visitor
8
8
 
9
9
  def initialize(visitor = nil)
10
- @tables = %w{users photos developers products}
10
+ @tables = %w{ users photos developers products}
11
11
  @columns = {
12
12
  'users' => [
13
13
  Column.new('id', :integer),
@@ -66,7 +66,7 @@ module FakeRecord
66
66
  end
67
67
 
68
68
  def in_clause_length
69
- 10000
69
+ 6
70
70
  end
71
71
 
72
72
  def quote thing, column = nil
@@ -1,8 +1,10 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module Nodes
5
+
5
6
  describe ArelExtensions::Comparators do
7
+
6
8
  before do
7
9
  @conn = FakeRecord::Base.new
8
10
  Arel::Table.engine = @conn
@@ -18,25 +20,26 @@ module ArelExtensions
18
20
  end
19
21
 
20
22
  it "< is equal lt" do
21
- _(compile(@table[:id] < 10)).must_be_like('"users"."id" < 10')
23
+ compile(@table[:id] < 10).must_be_like('"users"."id" < 10')
22
24
  end
23
-
25
+
24
26
  it "<= is equal lteq" do
25
- _(compile(@table[:id] <= 10)).must_be_like('"users"."id" <= 10')
27
+ compile(@table[:id] <= 10).must_be_like('"users"."id" <= 10')
26
28
  end
27
29
 
28
30
  it "> is equal gt" do
29
- _(compile(@table[:id] > 10)).must_be_like('"users"."id" > 10')
31
+ compile(@table[:id] > 10).must_be_like('"users"."id" > 10')
30
32
  end
31
33
 
32
34
  it "< is equal gteq" do
33
- _(compile(@table[:id] >= 10)).must_be_like('"users"."id" >= 10')
35
+ compile(@table[:id] >= 10).must_be_like('"users"."id" >= 10')
34
36
  end
35
37
 
36
38
  it "should compare with dates" do
37
- _(compile(@table[:created_at] >= Date.new(2016, 3, 31)))
38
- .must_be_like %{"users"."created_at" >= '2016-03-31'}
39
+ compile(@table[:created_at] >= Date.new(2016, 3, 31)).must_be_like %{"users"."created_at" >= '2016-03-31'}
39
40
  end
41
+
40
42
  end
43
+
41
44
  end
42
- end
45
+ end
@@ -1,4 +1,4 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module BulkInsertOracle
@@ -9,8 +9,8 @@ module ArelExtensions
9
9
  @table = Arel::Table.new(:users)
10
10
  @cols = ['name', 'comments', 'created_at']
11
11
  @data = [
12
- ['nom1', "sdfdsfdsfsdf", '2016-01-01'],
13
- ['nom2', "sdfdsfdsfsdf", '2016-01-01']
12
+ ['nom1', "sdfdsfdsfsdf", '2016-01-01'],
13
+ ['nom2', "sdfdsfdsfsdf", '2016-01-01']
14
14
  ]
15
15
  end
16
16
 
@@ -24,11 +24,11 @@ module ArelExtensions
24
24
 
25
25
  it "should import large set of data in Oracle" do
26
26
  insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
27
- insert_manager.bulk_insert(@cols, @data)
28
- _(compile(insert_manager.ast))
29
- .must_be_like %Q[INSERT INTO "users" ("name", "comments", "created_at")
30
- ((SELECT 'nom1', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL) UNION ALL (SELECT 'nom2', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL))]
27
+ insert_manager.bulk_insert(@cols, @data)
28
+ sql = compile(insert_manager.ast)
29
+ sql.must_be_like %Q[INSERT INTO "users" ("name", "comments", "created_at") ((SELECT 'nom1', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL) UNION ALL (SELECT 'nom2', 'sdfdsfdsfsdf', '2016-01-01' FROM DUAL))]
31
30
  end
32
- end
31
+
32
+ end
33
33
  end
34
34
  end