arel_extensions 2.0.11 → 2.0.16

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 (98) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +102 -0
  3. data/.travis.yml +2 -0
  4. data/Gemfile +10 -10
  5. data/Rakefile +4 -4
  6. data/gemfiles/rails3.gemfile +9 -9
  7. data/gemfiles/rails4.gemfile +13 -13
  8. data/gemfiles/rails5_0.gemfile +13 -13
  9. data/gemfiles/rails5_1_4.gemfile +13 -13
  10. data/gemfiles/rails5_2.gemfile +13 -13
  11. data/gemfiles/rails6.gemfile +13 -13
  12. data/gemfiles/rails6_1.gemfile +30 -0
  13. data/gemspecs/arel_extensions-v1.gemspec +28 -0
  14. data/{gemspec_v2 → gemspecs}/arel_extensions-v2.gemspec +0 -0
  15. data/generate_gems.sh +4 -3
  16. data/lib/arel_extensions.rb +6 -4
  17. data/lib/arel_extensions/attributes.rb +0 -0
  18. data/lib/arel_extensions/boolean_functions.rb +21 -5
  19. data/lib/arel_extensions/common_sql_functions.rb +2 -4
  20. data/lib/arel_extensions/comparators.rb +11 -14
  21. data/lib/arel_extensions/date_duration.rb +4 -5
  22. data/lib/arel_extensions/insert_manager.rb +16 -17
  23. data/lib/arel_extensions/math.rb +8 -9
  24. data/lib/arel_extensions/math_functions.rb +18 -20
  25. data/lib/arel_extensions/nodes/abs.rb +0 -1
  26. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -1
  27. data/lib/arel_extensions/nodes/blank.rb +0 -1
  28. data/lib/arel_extensions/nodes/case.rb +3 -4
  29. data/lib/arel_extensions/nodes/cast.rb +4 -2
  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 +0 -1
  33. data/lib/arel_extensions/nodes/collate.rb +0 -1
  34. data/lib/arel_extensions/nodes/concat.rb +2 -4
  35. data/lib/arel_extensions/nodes/date_diff.rb +7 -8
  36. data/lib/arel_extensions/nodes/duration.rb +0 -1
  37. data/lib/arel_extensions/nodes/find_in_set.rb +0 -1
  38. data/lib/arel_extensions/nodes/floor.rb +1 -1
  39. data/lib/arel_extensions/nodes/format.rb +27 -1
  40. data/lib/arel_extensions/nodes/formatted_number.rb +0 -1
  41. data/lib/arel_extensions/nodes/function.rb +18 -15
  42. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  43. data/lib/arel_extensions/nodes/json.rb +11 -17
  44. data/lib/arel_extensions/nodes/length.rb +0 -1
  45. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  46. data/lib/arel_extensions/nodes/locate.rb +0 -1
  47. data/lib/arel_extensions/nodes/log10.rb +1 -2
  48. data/lib/arel_extensions/nodes/matches.rb +0 -2
  49. data/lib/arel_extensions/nodes/md5.rb +0 -1
  50. data/lib/arel_extensions/nodes/power.rb +0 -1
  51. data/lib/arel_extensions/nodes/rand.rb +0 -1
  52. data/lib/arel_extensions/nodes/repeat.rb +0 -2
  53. data/lib/arel_extensions/nodes/replace.rb +0 -2
  54. data/lib/arel_extensions/nodes/round.rb +0 -1
  55. data/lib/arel_extensions/nodes/soundex.rb +0 -1
  56. data/lib/arel_extensions/nodes/std.rb +4 -5
  57. data/lib/arel_extensions/nodes/substring.rb +0 -1
  58. data/lib/arel_extensions/nodes/sum.rb +0 -0
  59. data/lib/arel_extensions/nodes/then.rb +0 -0
  60. data/lib/arel_extensions/nodes/trim.rb +0 -2
  61. data/lib/arel_extensions/nodes/union.rb +0 -2
  62. data/lib/arel_extensions/nodes/union_all.rb +0 -2
  63. data/lib/arel_extensions/nodes/wday.rb +0 -4
  64. data/lib/arel_extensions/null_functions.rb +3 -5
  65. data/lib/arel_extensions/predications.rb +5 -6
  66. data/lib/arel_extensions/railtie.rb +5 -5
  67. data/lib/arel_extensions/set_functions.rb +0 -2
  68. data/lib/arel_extensions/string_functions.rb +21 -22
  69. data/lib/arel_extensions/tasks.rb +1 -1
  70. data/lib/arel_extensions/version.rb +1 -1
  71. data/lib/arel_extensions/visitors.rb +68 -60
  72. data/lib/arel_extensions/visitors/convert_format.rb +37 -0
  73. data/lib/arel_extensions/visitors/ibm_db.rb +4 -11
  74. data/lib/arel_extensions/visitors/mssql.rb +49 -44
  75. data/lib/arel_extensions/visitors/mysql.rb +65 -67
  76. data/lib/arel_extensions/visitors/oracle.rb +58 -55
  77. data/lib/arel_extensions/visitors/oracle12.rb +2 -3
  78. data/lib/arel_extensions/visitors/postgresql.rb +41 -34
  79. data/lib/arel_extensions/visitors/sqlite.rb +23 -18
  80. data/lib/arel_extensions/visitors/to_sql.rb +56 -47
  81. data/test/arelx_test_helper.rb +0 -2
  82. data/test/database.yml +2 -0
  83. data/test/real_db_test.rb +27 -42
  84. data/test/support/fake_record.rb +1 -1
  85. data/test/test_comparators.rb +0 -4
  86. data/test/visitors/test_bulk_insert_oracle.rb +0 -1
  87. data/test/visitors/test_bulk_insert_sqlite.rb +0 -2
  88. data/test/visitors/test_oracle.rb +1 -2
  89. data/test/visitors/test_to_sql.rb +16 -25
  90. data/test/with_ar/all_agnostic_test.rb +135 -139
  91. data/test/with_ar/insert_agnostic_test.rb +0 -2
  92. data/test/with_ar/test_bulk_sqlite.rb +0 -4
  93. data/test/with_ar/test_math_sqlite.rb +4 -8
  94. data/test/with_ar/test_string_mysql.rb +1 -5
  95. data/test/with_ar/test_string_sqlite.rb +1 -5
  96. data/version_v1.rb +1 -1
  97. data/version_v2.rb +1 -1
  98. metadata +8 -4
@@ -1,14 +1,15 @@
1
1
 
2
- gem uninstall arel_extensions
2
+ #gem uninstall arel_extensions
3
3
 
4
4
  # VERSION ~> 1
5
5
  cp ./version_v1.rb lib/arel_extensions/version.rb
6
+ cp ./gemspecs/arel_extensions-v1.gemspec ./arel_extensions.gemspec
6
7
  gem build ./arel_extensions.gemspec
7
8
 
8
9
  # VERSION ~> 2
9
10
  cp ./version_v2.rb lib/arel_extensions/version.rb
10
11
  mv ./arel_extensions.gemspec ./arel_extensions.gemspec.bck
11
- cp ./gemspec_v2/arel_extensions-v2.gemspec ./arel_extensions.gemspec
12
+ cp ./gemspecs/arel_extensions-v2.gemspec ./arel_extensions.gemspec
12
13
  gem build ./arel_extensions.gemspec
13
14
  cp ./version_v1.rb lib/arel_extensions/version.rb
14
- cp ./arel_extensions.gemspec.bck ./arel_extensions.gemspec
15
+ cp ./gemspecs/arel_extensions-v1.gemspec ./arel_extensions.gemspec
@@ -16,8 +16,10 @@ class Arel::Nodes::Casted
16
16
  include Arel::AliasPredication
17
17
 
18
18
  # They forget to define hash.
19
- def hash
20
- [self.class, self.val, self.attribute].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
21
23
  end
22
24
  end
23
25
 
@@ -124,7 +126,7 @@ class Arel::Nodes::Function
124
126
  include ArelExtensions::NullFunctions
125
127
  include ArelExtensions::Predications
126
128
 
127
- alias_method :old_as, :as
129
+ alias_method(:old_as, :as) rescue nil
128
130
  def as other
129
131
  Arel::Nodes::As.new(self, Arel.sql(other))
130
132
  end
@@ -178,7 +180,7 @@ class Arel::Nodes::As
178
180
  end
179
181
 
180
182
  class Arel::Table
181
- alias_method :old_alias, :alias
183
+ alias_method(:old_alias, :alias) rescue nil
182
184
  def alias(name = "#{self.name}_2")
183
185
  name.blank? ? self : Arel::Nodes::TableAlias.new(self,name)
184
186
  end
File without changes
@@ -1,9 +1,9 @@
1
1
  # coding: utf-8
2
+
2
3
  require 'arel_extensions/nodes/then'
3
4
 
4
5
  module ArelExtensions
5
6
  module BooleanFunctions
6
-
7
7
  def ⋀(other)
8
8
  self.and(other)
9
9
  end
@@ -23,7 +23,6 @@ module ArelExtensions
23
23
  def then(t, f = nil)
24
24
  ArelExtensions::Nodes::Then.new [self, t, f]
25
25
  end
26
-
27
26
  end
28
27
  end
29
28
 
@@ -34,10 +33,9 @@ class Arel::Nodes::And
34
33
  children =
35
34
  children.flatten.map { |c|
36
35
  c.is_a?(self) ? c.children : c
37
- }.flatten
36
+ }.flatten
38
37
  super(children)
39
38
  end
40
-
41
39
  end
42
40
 
43
41
  # For some reason, Arel's And is properly defined as variadic (it
@@ -55,7 +53,7 @@ class Arel::Nodes::Or
55
53
  children =
56
54
  children.flatten.map { |c|
57
55
  c.is_a?(self) ? c.children : c
58
- }.flatten
56
+ }.flatten
59
57
  super(*children)
60
58
  end
61
59
 
@@ -63,8 +61,26 @@ class Arel::Nodes::Or
63
61
  @children = children
64
62
  end
65
63
 
64
+ def initialize_copy(other)
65
+ super
66
+ @children = other.children.copy if other.children
67
+ end
68
+
69
+ def left
70
+ children.first
71
+ end
72
+
73
+ def right
74
+ children[1]
75
+ end
76
+
66
77
  def hash
67
78
  children.hash
68
79
  end
69
80
 
81
+ def eql?(other)
82
+ self.class == other.class &&
83
+ children == other.children
84
+ end
85
+ alias :== :eql?
70
86
  end
@@ -1,6 +1,5 @@
1
1
  module ArelExtensions
2
2
  class CommonSqlFunctions
3
-
4
3
  def initialize(cnx)
5
4
  @cnx = cnx
6
5
  if cnx && cnx.adapter_name =~ /sqlite/i && !$load_extension_disabled
@@ -12,7 +11,7 @@ module ArelExtensions
12
11
  db.enable_load_extension(0)
13
12
  rescue => e
14
13
  $load_extension_disabled = true
15
- puts "can not load extensions #{e.inspect}"
14
+ puts "cannot load extensions #{e.inspect}"
16
15
  end
17
16
  end
18
17
  end
@@ -44,7 +43,7 @@ module ArelExtensions
44
43
  begin
45
44
  add_sqlite_functions
46
45
  rescue => e
47
- puts "can not add sqlite functions #{e.inspect}"
46
+ puts "cannot add sqlite functions #{e.inspect}"
48
47
  end
49
48
  end
50
49
  if File.exist?("init/#{env_db}.sql")
@@ -62,6 +61,5 @@ module ArelExtensions
62
61
  end
63
62
  end
64
63
  end
65
-
66
64
  end
67
65
  end
@@ -1,7 +1,5 @@
1
1
  module ArelExtensions
2
2
  module Comparators
3
-
4
-
5
3
  def >(other)
6
4
  Arel::Nodes::GreaterThan.new self, Arel::Nodes.build_quoted(other, self)
7
5
  end
@@ -18,26 +16,26 @@ module ArelExtensions
18
16
  Arel::Nodes::LessThanOrEqual.new self, Arel::Nodes.build_quoted(other, self)
19
17
  end
20
18
 
21
-
22
- #REGEXP function
23
- #Pattern matching using regular expressions
19
+ # REGEXP function
20
+ # Pattern matching using regular expressions
24
21
  def =~(other)
25
- # arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
26
- # if arg == :string || arg == :text
22
+ # arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
23
+ # if arg == :string || arg == :text
27
24
  Arel::Nodes::Regexp.new self, convert_regexp(other)
28
- # end
25
+ # end
29
26
  end
30
27
 
31
- #NOT_REGEXP function
32
- #Negation of Regexp
28
+ # NOT_REGEXP function
29
+ # Negation of Regexp
33
30
  def !~(other)
34
- # arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
35
- # if arg == :string || arg == :text
31
+ # arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
32
+ # if arg == :string || arg == :text
36
33
  Arel::Nodes::NotRegexp.new self, convert_regexp(other)
37
- # end
34
+ # end
38
35
  end
39
36
 
40
37
  private
38
+
41
39
  # Function used for not_regexp.
42
40
  def convert_regexp(other)
43
41
  case other
@@ -56,6 +54,5 @@ module ArelExtensions
56
54
  end
57
55
  Arel::Nodes.build_quoted(other, self)
58
56
  end
59
-
60
57
  end
61
58
  end
@@ -4,22 +4,22 @@ require 'arel_extensions/nodes/wday'
4
4
 
5
5
  module ArelExtensions
6
6
  module DateDuration
7
- #function returns the year (as a number) given a date value.
7
+ # function returns the year (as a number) given a date value.
8
8
  def year
9
9
  ArelExtensions::Nodes::Duration.new "y", self
10
10
  end
11
11
 
12
- #function returns the month (as a number) given a date value.
12
+ # function returns the month (as a number) given a date value.
13
13
  def month
14
14
  ArelExtensions::Nodes::Duration.new "m", self
15
15
  end
16
16
 
17
- #function returns the week (as a number) given a date value.
17
+ # function returns the week (as a number) given a date value.
18
18
  def week
19
19
  ArelExtensions::Nodes::Duration.new "w", self
20
20
  end
21
21
 
22
- #function returns the month (as a number) given a date value.
22
+ # function returns the month (as a number) given a date value.
23
23
  def day
24
24
  ArelExtensions::Nodes::Duration.new "d", self
25
25
  end
@@ -43,6 +43,5 @@ module ArelExtensions
43
43
  def format(tpl)
44
44
  ArelExtensions::Nodes::Format.new [self, tpl]
45
45
  end
46
-
47
46
  end
48
47
  end
@@ -2,34 +2,33 @@ require 'arel'
2
2
 
3
3
  module ArelExtensions
4
4
  module InsertManager
5
-
6
5
  def bulk_insert(cols, data)
7
6
  res_columns = []
8
- case cols.first
9
- when String, Symbol
10
- cols.each { |c|
11
- res_columns << @ast.relation[c]
12
- }
13
- when Array
14
- if String === cols.first.first
15
- res_columns = cols.map {|c| [@ast.relation[c.first]] }
16
- elsif Arel::Attributes::Attribute == cols.first.first
17
- res_columns = cols
18
- end
19
- when NilClass
20
- res_columns = @ast.relation.columns
7
+ case cols.first
8
+ when String, Symbol
9
+ cols.each { |c|
10
+ res_columns << @ast.relation[c]
11
+ }
12
+ when Array
13
+ if String === cols.first.first
14
+ res_columns = cols.map {|c| [@ast.relation[c.first]] }
15
+ elsif Arel::Attributes::Attribute == cols.first.first
16
+ res_columns = cols
21
17
  end
22
- self.values = BulkValues.new(res_columns, data)
23
- @ast.columns = res_columns
18
+ when NilClass
19
+ res_columns = @ast.relation.columns
20
+ end
21
+ self.values = BulkValues.new(res_columns, data)
22
+ @ast.columns = res_columns
24
23
  end
25
24
 
26
25
  class BulkValues < Arel::Nodes::Node
27
26
  attr_accessor :left, :cols
27
+
28
28
  def initialize(cols, values)
29
29
  @left = values
30
30
  @cols = cols
31
31
  end
32
32
  end
33
-
34
33
  end
35
34
  end
@@ -11,9 +11,9 @@ require 'arel_extensions/nodes/union_all'
11
11
 
12
12
  module ArelExtensions
13
13
  module Math
14
- #function + between
15
- #String and others (convert in string) allows you to concatenate 2 or more strings together.
16
- #Date and integer adds or subtracts a specified time interval from a date.
14
+ # function + between
15
+ # String and others (convert in string) allows you to concatenate 2 or more strings together.
16
+ # Date and integer adds or subtracts a specified time interval from a date.
17
17
  def +(other)
18
18
  case self
19
19
  when Arel::Nodes::Quoted
@@ -43,7 +43,7 @@ module ArelExtensions
43
43
  rescue Exception
44
44
  col = nil
45
45
  end
46
- if (!col) #if the column doesn't exist in the database
46
+ if (!col) # if the column doesn't exist in the database
47
47
  Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new(self, other))
48
48
  else
49
49
  arg = col.type
@@ -62,8 +62,8 @@ module ArelExtensions
62
62
  end
63
63
  end
64
64
 
65
- #function returns the time between two dates
66
- #function returns the substraction between two ints
65
+ # function returns the time between two dates
66
+ # function returns the substraction between two ints
67
67
  def -(other)
68
68
  case self
69
69
  when Arel::Nodes::Grouping
@@ -91,7 +91,7 @@ module ArelExtensions
91
91
  rescue Exception
92
92
  col = nil
93
93
  end
94
- if (!col) #if the column doesn't exist in the database
94
+ if (!col) # if the column doesn't exist in the database
95
95
  Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
96
96
  else
97
97
  arg = col.type
@@ -103,7 +103,7 @@ module ArelExtensions
103
103
  rescue Exception
104
104
  col2 = nil
105
105
  end
106
- if (!col2) #if the column doesn't exist in the database
106
+ if (!col2) # if the column doesn't exist in the database
107
107
  ArelExtensions::Nodes::DateSub.new [self, other]
108
108
  else
109
109
  arg2 = col2.type
@@ -133,6 +133,5 @@ module ArelExtensions
133
133
  end
134
134
  end
135
135
  end
136
-
137
136
  end
138
137
  end
@@ -11,7 +11,6 @@ require 'arel_extensions/nodes/sum'
11
11
 
12
12
  module ArelExtensions
13
13
  module MathFunctions
14
-
15
14
  # Arel does not handle Decimal literal properly
16
15
  def * other
17
16
  case other
@@ -47,7 +46,7 @@ module ArelExtensions
47
46
  ArelExtensions::Nodes::Floor.new [self]
48
47
  end
49
48
 
50
- # function gives the base 10 log
49
+ # function gives the base 10 log
51
50
  def log10
52
51
  ArelExtensions::Nodes::Log10.new [self]
53
52
  end
@@ -64,24 +63,24 @@ module ArelExtensions
64
63
 
65
64
  # Aggregate Functions
66
65
  def std opts = {unbiased: true}
67
- ArelExtensions::Nodes::Std.new self, opts
66
+ ArelExtensions::Nodes::Std.new self, **opts
68
67
  end
69
68
 
70
69
  def variance opts = {unbiased: true}
71
- ArelExtensions::Nodes::Variance.new self, opts
70
+ ArelExtensions::Nodes::Variance.new self, **opts
72
71
  end
73
72
 
74
73
  def sum opts = {unbiased: true}
75
- ArelExtensions::Nodes::Sum.new self, opts
74
+ ArelExtensions::Nodes::Sum.new self, **opts
76
75
  end
77
76
 
78
- #function that can be invoked to produce random numbers between 0 and 1
79
- # def rand seed = nil
80
- # ArelExtensions::Nodes::Rand.new [seed]
81
- # end
82
- alias_method :random, :rand
77
+ # function that can be invoked to produce random numbers between 0 and 1
78
+ # def rand seed = nil
79
+ # ArelExtensions::Nodes::Rand.new [seed]
80
+ # end
81
+ alias_method(:random, :rand) rescue nil
83
82
 
84
- #function is used to round a numeric field to the number of decimals specified
83
+ # function is used to round a numeric field to the number of decimals specified
85
84
  def round precision = nil
86
85
  if precision
87
86
  ArelExtensions::Nodes::Round.new [self, precision]
@@ -96,14 +95,14 @@ module ArelExtensions
96
95
  sprintf(format_string,0) # this line is to get the right error message if the format_string is not correct
97
96
  m = /^(.*)%([ #+\-0]*)([1-9][0-9]+|[1-9]?)[.]?([0-9]*)([a-zA-Z])(.*)$/.match(format_string)
98
97
  opts = {
99
- :prefix => m[1],
100
- :flags => m[2].split(//).uniq.join,
101
- :width => m[3].to_i,
102
- :precision => m[4] != '' ? m[4].to_i : 6,
103
- :type => m[5],
104
- :suffix => m[6],
105
- :locale => locale,
106
- :original_string => format_string
98
+ prefix: m[1],
99
+ flags: m[2].split(//).uniq.join,
100
+ width: m[3].to_i,
101
+ precision: m[4] != '' ? m[4].to_i : 6,
102
+ type: m[5],
103
+ suffix: m[6],
104
+ locale: locale,
105
+ original_string: format_string
107
106
  }
108
107
  # opts = {:locale => 'fr_FR', :type => "e"/"f"/"d", :prefix => "$ ", :suffix => " %", :flags => " +-#0", :width => 5, :precision => 6}
109
108
  ArelExtensions::Nodes::FormattedNumber.new [self,opts]
@@ -111,6 +110,5 @@ module ArelExtensions
111
110
  Arel::Nodes.build_quoted('Wrong Format')
112
111
  end
113
112
  end
114
-
115
113
  end
116
114
  end
@@ -2,7 +2,6 @@ module ArelExtensions
2
2
  module Nodes
3
3
  class Abs < Function
4
4
  RETURN_TYPE = :number
5
-
6
5
  end
7
6
  end
8
7
  end
@@ -9,6 +9,5 @@ module ArelExtensions
9
9
  super [node]
10
10
  end
11
11
  end
12
-
13
12
  end
14
13
  end
@@ -19,6 +19,5 @@ module ArelExtensions
19
19
  super expr.first
20
20
  end
21
21
  end
22
-
23
22
  end
24
23
  end