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,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