arel_extensions 1.2.23 → 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.rb +19 -69
  19. data/lib/arel_extensions/attributes.rb +1 -0
  20. data/lib/arel_extensions/boolean_functions.rb +14 -55
  21. data/lib/arel_extensions/common_sql_functions.rb +8 -7
  22. data/lib/arel_extensions/comparators.rb +15 -14
  23. data/lib/arel_extensions/date_duration.rb +5 -4
  24. data/lib/arel_extensions/insert_manager.rb +16 -17
  25. data/lib/arel_extensions/math.rb +12 -11
  26. data/lib/arel_extensions/math_functions.rb +22 -29
  27. data/lib/arel_extensions/nodes.rb +1 -1
  28. data/lib/arel_extensions/nodes/abs.rb +1 -0
  29. data/lib/arel_extensions/nodes/blank.rb +1 -0
  30. data/lib/arel_extensions/nodes/case.rb +8 -11
  31. data/lib/arel_extensions/nodes/cast.rb +2 -4
  32. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  33. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  34. data/lib/arel_extensions/nodes/coalesce.rb +3 -2
  35. data/lib/arel_extensions/nodes/collate.rb +2 -1
  36. data/lib/arel_extensions/nodes/concat.rb +16 -7
  37. data/lib/arel_extensions/nodes/date_diff.rb +13 -10
  38. data/lib/arel_extensions/nodes/duration.rb +3 -0
  39. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  40. data/lib/arel_extensions/nodes/floor.rb +1 -1
  41. data/lib/arel_extensions/nodes/format.rb +8 -34
  42. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  43. data/lib/arel_extensions/nodes/function.rb +16 -25
  44. data/lib/arel_extensions/nodes/json.rb +36 -43
  45. data/lib/arel_extensions/nodes/length.rb +1 -0
  46. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  47. data/lib/arel_extensions/nodes/locate.rb +1 -0
  48. data/lib/arel_extensions/nodes/log10.rb +2 -1
  49. data/lib/arel_extensions/nodes/matches.rb +6 -4
  50. data/lib/arel_extensions/nodes/md5.rb +1 -0
  51. data/lib/arel_extensions/nodes/power.rb +5 -5
  52. data/lib/arel_extensions/nodes/rand.rb +1 -0
  53. data/lib/arel_extensions/nodes/repeat.rb +4 -2
  54. data/lib/arel_extensions/nodes/replace.rb +6 -22
  55. data/lib/arel_extensions/nodes/round.rb +6 -5
  56. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  57. data/lib/arel_extensions/nodes/std.rb +21 -18
  58. data/lib/arel_extensions/nodes/substring.rb +16 -8
  59. data/lib/arel_extensions/nodes/then.rb +0 -0
  60. data/lib/arel_extensions/nodes/trim.rb +5 -3
  61. data/lib/arel_extensions/nodes/union.rb +5 -2
  62. data/lib/arel_extensions/nodes/union_all.rb +3 -0
  63. data/lib/arel_extensions/nodes/wday.rb +4 -0
  64. data/lib/arel_extensions/null_functions.rb +7 -5
  65. data/lib/arel_extensions/predications.rb +34 -35
  66. data/lib/arel_extensions/railtie.rb +5 -5
  67. data/lib/arel_extensions/set_functions.rb +4 -2
  68. data/lib/arel_extensions/string_functions.rb +22 -43
  69. data/lib/arel_extensions/tasks.rb +5 -5
  70. data/lib/arel_extensions/version.rb +1 -1
  71. data/lib/arel_extensions/visitors.rb +60 -68
  72. data/lib/arel_extensions/visitors/ibm_db.rb +12 -5
  73. data/lib/arel_extensions/visitors/mssql.rb +57 -63
  74. data/lib/arel_extensions/visitors/mysql.rb +98 -149
  75. data/lib/arel_extensions/visitors/oracle.rb +68 -71
  76. data/lib/arel_extensions/visitors/oracle12.rb +15 -2
  77. data/lib/arel_extensions/visitors/postgresql.rb +63 -116
  78. data/lib/arel_extensions/visitors/sqlite.rb +70 -83
  79. data/lib/arel_extensions/visitors/to_sql.rb +109 -141
  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
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
@@ -1,7 +1,8 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module BulkInsertSQLlite
5
+
5
6
  describe 'the sqlite visitor can bulk insert' do
6
7
  before do
7
8
  @conn = FakeRecord::Base.new
@@ -10,11 +11,11 @@ module ArelExtensions
10
11
  Arel::Table.engine = @conn
11
12
  @cols = ['id', 'name', 'comments', 'created_at']
12
13
  @data = [
13
- [23, 'nom1', "sdfdsfdsfsdf", '2016-01-01'],
14
- [25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
14
+ [23, 'nom1', "sdfdsfdsfsdf", '2016-01-01'],
15
+ [25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
15
16
  ]
16
17
  end
17
-
18
+
18
19
  def compile node
19
20
  if Arel::VERSION.to_i > 5
20
21
  @visitor.accept(node, Arel::Collectors::SQLString.new).value
@@ -26,10 +27,10 @@ module ArelExtensions
26
27
  it "should import large set of data" do
27
28
  insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
28
29
  insert_manager.bulk_insert(@cols, @data)
29
- _(compile(insert_manager.ast))
30
- .must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at")
31
- SELECT 23 AS 'id', 'nom1' AS 'name', 'sdfdsfdsfsdf' AS 'comments', '2016-01-01' AS 'created_at' UNION ALL SELECT 25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01']
30
+ sql = compile(insert_manager.ast)
31
+ sql.must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at") SELECT 23 AS 'id', 'nom1' AS 'name', 'sdfdsfdsfsdf' AS 'comments', '2016-01-01' AS 'created_at' UNION ALL SELECT 25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01']
32
32
  end
33
- end
33
+
34
+ end
34
35
  end
35
- end
36
+ end
@@ -1,4 +1,4 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module BulkInsertToSql
@@ -10,8 +10,8 @@ module ArelExtensions
10
10
  @table = Arel::Table.new(:users)
11
11
  @cols = ['id', 'name', 'comments', 'created_at']
12
12
  @data = [
13
- [23, 'nom1', "sdfdsfdsfsdf", '2016-01-01'],
14
- [25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
13
+ [23, 'nom1', "sdfdsfdsfsdf", '2016-01-01'],
14
+ [25, 'nom2', "sdfdsfdsfsdf", '2016-01-01']
15
15
  ]
16
16
  end
17
17
 
@@ -26,10 +26,12 @@ module ArelExtensions
26
26
 
27
27
  it "should import large set of data using ToSql" do
28
28
  insert_manager = Arel::VERSION.to_i > 6 ? Arel::InsertManager.new().into(@table) : Arel::InsertManager.new(@conn).into(@table)
29
- insert_manager.bulk_insert(@cols, @data)
30
- _(compile(insert_manager.ast))
31
- .must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at") VALUES (23, 'nom1', 'sdfdsfdsfsdf', '2016-01-01'), (25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01')]
29
+ insert_manager.bulk_insert(@cols, @data)
30
+ sql = compile(insert_manager.ast)
31
+ sql.must_be_like %Q[INSERT INTO "users" ("id", "name", "comments", "created_at") VALUES (23, 'nom1', 'sdfdsfdsfsdf', '2016-01-01'), (25, 'nom2', 'sdfdsfdsfsdf', '2016-01-01')]
32
32
  end
33
- end
33
+
34
+
35
+ end
34
36
  end
35
- end
37
+ end
@@ -1,4 +1,4 @@
1
- require 'arelx_test_helper'
1
+ require 'helper'
2
2
 
3
3
  module ArelExtensions
4
4
  module VisitorOracle
@@ -23,87 +23,87 @@ module ArelExtensions
23
23
  # Comparators
24
24
 
25
25
  it "should accept comparators on integers" do
26
- _(compile(@table[:id] == 42)).must_match %{"users"."id" = 42}
27
- _(compile(@table[:id] == @table[:id])).must_be_like %{"users"."id" = "users"."id"}
28
- _(compile(@table[:id] != 42)).must_match %{"users"."id" != 42}
29
- _(compile(@table[:id] > 42)).must_match %{"users"."id" > 42}
30
- _(compile(@table[:id] >= 42)).must_match %{"users"."id" >= 42}
31
- _(compile(@table[:id] >= @table[:id])).must_be_like %{"users"."id" >= "users"."id"}
32
- _(compile(@table[:id] < 42)).must_match %{"users"."id" < 42}
33
- _(compile(@table[:id] <= 42)).must_match %{"users"."id" <= 42}
34
- _(compile((@table[:id] <= 42).as('new_name'))).must_match %{("users"."id" <= 42) AS new_name}
26
+ compile(@table[:id] == 42).must_match %{"users"."id" = 42}
27
+ compile(@table[:id] == @table[:id]).must_be_like %{"users"."id" = "users"."id"}
28
+ compile(@table[:id] != 42).must_match %{"users"."id" != 42}
29
+ compile(@table[:id] > 42).must_match %{"users"."id" > 42}
30
+ compile(@table[:id] >= 42).must_match %{"users"."id" >= 42}
31
+ compile(@table[:id] >= @table[:id]).must_be_like %{"users"."id" >= "users"."id"}
32
+ compile(@table[:id] < 42).must_match %{"users"."id" < 42}
33
+ compile(@table[:id] <= 42).must_match %{"users"."id" <= 42}
34
+ compile((@table[:id] <= 42).as('new_name')).must_match %{("users"."id" <= 42) AS new_name}
35
35
  end
36
36
 
37
37
  it "should accept comparators on dates" do
38
38
  c = @table[:created_at]
39
39
  u = @table[:updated_at]
40
- _(compile(c > @date)).must_be_like %{"users"."created_at" > '2016-03-31'}
41
- _(compile(u >= @date)).must_be_like %{"users"."updated_at" >= '2016-03-31'}
42
- _(compile(c < u)).must_be_like %{"users"."created_at" < "users"."updated_at"}
40
+ compile(c > @date).must_be_like %{"users"."created_at" > '2016-03-31'}
41
+ compile(u >= @date).must_be_like %{"users"."updated_at" >= '2016-03-31'}
42
+ compile(c < u).must_be_like %{"users"."created_at" < "users"."updated_at"}
43
43
  end
44
44
 
45
45
  it "should accept comparators on strings" do
46
46
  c = @table[:name]
47
- _(compile(c == 'test')).must_be_like %{"users"."name" = 'test'}
48
- _(compile(c != 'test')).must_be_like %{"users"."name" != 'test'}
49
- _(compile(c > 'test')).must_be_like %{"users"."name" > 'test'}
50
- _(compile((c >= 'test').as('new_name'))).must_be_like %{("users"."name" >= 'test') AS new_name}
51
- _(compile(c <= @table[:comments])).must_be_like %{"users"."name" <= "users"."comments"}
52
- _(compile(c =~ /\Atest\Z/)).must_be_like %{REGEXP_LIKE("users"."name", '^test$')}
53
- _(compile(c =~ /\Atest\z/)).must_be_like %{REGEXP_LIKE("users"."name", '^test$')}
54
- _(compile(c =~ '^test$')).must_be_like %{REGEXP_LIKE("users"."name", '^test$')}
55
- _(compile(c !~ /\Ate\Dst\Z/)).must_be_like %{NOT REGEXP_LIKE("users"."name", '^te[^0-9]st$')}
56
- _(compile(c.imatches('%test%'))).must_be_like %{LOWER("users"."name") LIKE LOWER('%test%')}
57
- _(compile(c.imatches_any(['%test%', 't2']))).must_be_like %{((LOWER("users"."name") LIKE LOWER('%test%')) OR (LOWER("users"."name") LIKE LOWER('t2')))}
58
- _(compile(c.idoes_not_match('%test%'))).must_be_like %{LOWER("users"."name") NOT LIKE LOWER('%test%')}
47
+ compile(c == 'test').must_be_like %{"users"."name" = 'test'}
48
+ compile(c != 'test').must_be_like %{"users"."name" != 'test'}
49
+ compile(c > 'test').must_be_like %{"users"."name" > 'test'}
50
+ compile((c >= 'test').as('new_name')).must_be_like %{("users"."name" >= 'test') AS new_name}
51
+ compile(c <= @table[:comments]).must_be_like %{"users"."name" <= "users"."comments"}
52
+ compile(c =~ /\Atest\Z/).must_be_like %{REGEXP_LIKE("users"."name", '^test$')}
53
+ compile(c =~ '^test$').must_be_like %{REGEXP_LIKE("users"."name", '^test$')}
54
+ compile(c !~ /\Ate\Dst\Z/).must_be_like %{NOT REGEXP_LIKE("users"."name", '^te[^0-9]st$')}
55
+ compile(c.imatches('%test%')).must_be_like %{LOWER("users"."name") LIKE LOWER('%test%')}
56
+ compile(c.imatches_any(['%test%', 't2'])).must_be_like %{((LOWER("users"."name") LIKE LOWER('%test%')) OR (LOWER("users"."name") LIKE LOWER('t2')))}
57
+ compile(c.idoes_not_match('%test%')).must_be_like %{LOWER("users"."name") NOT LIKE LOWER('%test%')}
59
58
  end
60
59
 
61
60
  # Maths
62
61
  # DateDiff
63
62
  it "should diff date col and date" do
64
- _(compile(@table[:created_at] - Date.new(2016, 3, 31))).must_match %{"users"."created_at" - TO_DATE('2016-03-31')}
63
+ compile(@table[:created_at] - Date.new(2016, 3, 31)).must_match %{"users"."created_at" - TO_DATE('2016-03-31')}
65
64
  end
66
65
 
67
66
  it "should diff date col and datetime col" do
68
- _(compile(@table[:created_at] - @table[:updated_at])).must_match %{"users"."created_at" - "users"."updated_at"}
67
+ compile(@table[:created_at] - @table[:updated_at]).must_match %{"users"."created_at" - "users"."updated_at"}
69
68
  end
70
69
 
71
70
  it "should diff date col and datetime col with AS" do
72
71
  sql = compile((@table[:updated_at] - @table[:created_at]).as('new_name'))
73
- # sql.must_be_like %{(TO_DATE("users"."updated_at") - "users"."created_at") * 86400 AS new_name}
74
- _(sql).must_be_like %{("users"."updated_at" - "users"."created_at") * (CASE WHEN (TRUNC("users"."updated_at", 'DDD') = "users"."updated_at") THEN 1 ELSE 86400 END) AS new_name}
72
+ # sql.must_be_like %{(TO_DATE("users"."updated_at") - "users"."created_at") * 86400 AS new_name}
73
+ sql.must_be_like %{("users"."updated_at" - "users"."created_at") * (CASE WHEN (TRUNC("users"."updated_at", 'DDD') = "users"."updated_at") THEN 1 ELSE 86400 END) AS new_name}
75
74
  end
76
75
 
77
76
  it "should diff between time values" do
78
77
  d2 = Time.new(2015,6,1)
79
78
  d1 = DateTime.new(2015,6,2)
80
- _(compile(ArelExtensions::Nodes::DateDiff.new([d1,d2])))
81
- .must_match("TO_DATE('2015-06-02') - TO_DATE('2015-06-01')")
79
+ sql = compile(ArelExtensions::Nodes::DateDiff.new([d1,d2]))
80
+ sql.must_match("TO_DATE('2015-06-02') - TO_DATE('2015-06-01')")
82
81
  end
83
82
 
84
83
  it "should diff between time values and time col" do
85
84
  d1 = DateTime.new(2015,6,2)
86
- _(compile(ArelExtensions::Nodes::DateDiff.new([d1, @table[:updated_at]])))
87
- .must_match %{TO_DATE('2015-06-02') - "users"."updated_at"}
85
+ sql = compile(ArelExtensions::Nodes::DateDiff.new([d1, @table[:updated_at]]))
86
+ sql.must_match %{TO_DATE('2015-06-02') - "users"."updated_at"}
88
87
  end
89
88
 
90
89
  it "should accept operators on dates with numbers" do
91
90
  c = @table[:created_at]
92
- _(compile(c - 42)).must_be_like %{DATE_SUB("users"."created_at", 42)}
93
- _(compile(c - @table[:id])).must_be_like %{DATE_SUB("users"."created_at", "users"."id")}
91
+ compile(c - 42).must_be_like %{DATE_SUB("users"."created_at", 42)}
92
+ compile(c - @table[:id]).must_be_like %{DATE_SUB("users"."created_at", "users"."id")}
94
93
  end
95
94
 
96
95
  # Maths on sums
97
96
  it "should accept math operators on anything" do
98
97
  c = @table[:name]
99
- _((c == 'test').to_sql).must_be_like %{"users"."name" = 'test'}
100
- _((c != 'test').to_sql).must_be_like %{"users"."name" != 'test'}
101
- _((c > 'test').to_sql).must_be_like %{"users"."name" > 'test'}
102
- _(compile((c >= 'test').as('new_name'))).must_be_like %{("users"."name" >= 'test') AS new_name}
103
- _(compile(c <= @table[:comments])).must_be_like %{"users"."name" <= "users"."comments"}
104
- _(compile(c =~ /\Atest\Z/)).must_be_like %{REGEXP_LIKE("users"."name", '^test$')}
105
- _(compile(c !~ /\Ate\Dst\Z/)).must_be_like %{NOT REGEXP_LIKE("users"."name", '^te[^0-9]st$')}
98
+ (c == 'test').to_sql.must_be_like %{"users"."name" = 'test'}
99
+ (c != 'test').to_sql.must_be_like %{"users"."name" != 'test'}
100
+ (c > 'test').to_sql.must_be_like %{"users"."name" > 'test'}
101
+ compile((c >= 'test').as('new_name')).must_be_like %{("users"."name" >= 'test') AS new_name}
102
+ compile(c <= @table[:comments]).must_be_like %{"users"."name" <= "users"."comments"}
103
+ compile(c =~ /\Atest\Z/).must_be_like %{REGEXP_LIKE("users"."name", '^test$')}
104
+ compile(c !~ /\Ate\Dst\Z/).must_be_like %{NOT REGEXP_LIKE("users"."name", '^te[^0-9]st$')}
106
105
  end
106
+
107
107
  end
108
108
  end
109
109
  end