arel_extensions 2.0.13 → 2.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +10 -10
  3. data/Rakefile +4 -4
  4. data/gemfiles/rails3.gemfile +9 -9
  5. data/gemfiles/rails4.gemfile +13 -13
  6. data/gemfiles/rails5_0.gemfile +13 -13
  7. data/gemfiles/rails5_1_4.gemfile +13 -13
  8. data/gemfiles/rails5_2.gemfile +13 -13
  9. data/gemfiles/rails6.gemfile +13 -13
  10. data/lib/arel_extensions.rb +2 -2
  11. data/lib/arel_extensions/attributes.rb +0 -0
  12. data/lib/arel_extensions/boolean_functions.rb +3 -7
  13. data/lib/arel_extensions/common_sql_functions.rb +0 -2
  14. data/lib/arel_extensions/comparators.rb +11 -14
  15. data/lib/arel_extensions/date_duration.rb +4 -5
  16. data/lib/arel_extensions/insert_manager.rb +16 -17
  17. data/lib/arel_extensions/math.rb +8 -9
  18. data/lib/arel_extensions/math_functions.rb +15 -17
  19. data/lib/arel_extensions/nodes/abs.rb +0 -1
  20. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -1
  21. data/lib/arel_extensions/nodes/blank.rb +0 -1
  22. data/lib/arel_extensions/nodes/case.rb +3 -4
  23. data/lib/arel_extensions/nodes/cast.rb +0 -1
  24. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  25. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  26. data/lib/arel_extensions/nodes/coalesce.rb +0 -1
  27. data/lib/arel_extensions/nodes/collate.rb +0 -1
  28. data/lib/arel_extensions/nodes/concat.rb +1 -3
  29. data/lib/arel_extensions/nodes/date_diff.rb +4 -5
  30. data/lib/arel_extensions/nodes/duration.rb +0 -1
  31. data/lib/arel_extensions/nodes/find_in_set.rb +0 -1
  32. data/lib/arel_extensions/nodes/floor.rb +1 -1
  33. data/lib/arel_extensions/nodes/format.rb +1 -0
  34. data/lib/arel_extensions/nodes/formatted_number.rb +0 -1
  35. data/lib/arel_extensions/nodes/function.rb +2 -3
  36. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  37. data/lib/arel_extensions/nodes/json.rb +0 -6
  38. data/lib/arel_extensions/nodes/length.rb +0 -1
  39. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  40. data/lib/arel_extensions/nodes/locate.rb +0 -1
  41. data/lib/arel_extensions/nodes/log10.rb +1 -2
  42. data/lib/arel_extensions/nodes/matches.rb +0 -2
  43. data/lib/arel_extensions/nodes/md5.rb +0 -1
  44. data/lib/arel_extensions/nodes/power.rb +0 -1
  45. data/lib/arel_extensions/nodes/rand.rb +0 -1
  46. data/lib/arel_extensions/nodes/repeat.rb +0 -2
  47. data/lib/arel_extensions/nodes/replace.rb +0 -2
  48. data/lib/arel_extensions/nodes/round.rb +0 -1
  49. data/lib/arel_extensions/nodes/soundex.rb +0 -1
  50. data/lib/arel_extensions/nodes/std.rb +0 -1
  51. data/lib/arel_extensions/nodes/substring.rb +0 -1
  52. data/lib/arel_extensions/nodes/sum.rb +0 -0
  53. data/lib/arel_extensions/nodes/then.rb +0 -0
  54. data/lib/arel_extensions/nodes/trim.rb +0 -2
  55. data/lib/arel_extensions/nodes/union.rb +0 -2
  56. data/lib/arel_extensions/nodes/union_all.rb +0 -2
  57. data/lib/arel_extensions/nodes/wday.rb +0 -4
  58. data/lib/arel_extensions/null_functions.rb +3 -5
  59. data/lib/arel_extensions/predications.rb +2 -3
  60. data/lib/arel_extensions/railtie.rb +5 -5
  61. data/lib/arel_extensions/set_functions.rb +0 -2
  62. data/lib/arel_extensions/string_functions.rb +21 -22
  63. data/lib/arel_extensions/tasks.rb +1 -1
  64. data/lib/arel_extensions/version.rb +1 -1
  65. data/lib/arel_extensions/visitors.rb +9 -7
  66. data/lib/arel_extensions/visitors/convert_format.rb +37 -0
  67. data/lib/arel_extensions/visitors/ibm_db.rb +4 -11
  68. data/lib/arel_extensions/visitors/mssql.rb +48 -44
  69. data/lib/arel_extensions/visitors/mysql.rb +63 -65
  70. data/lib/arel_extensions/visitors/oracle.rb +48 -55
  71. data/lib/arel_extensions/visitors/oracle12.rb +2 -3
  72. data/lib/arel_extensions/visitors/postgresql.rb +39 -34
  73. data/lib/arel_extensions/visitors/sqlite.rb +23 -18
  74. data/lib/arel_extensions/visitors/to_sql.rb +42 -44
  75. data/test/arelx_test_helper.rb +0 -2
  76. data/test/real_db_test.rb +26 -41
  77. data/test/support/fake_record.rb +1 -1
  78. data/test/test_comparators.rb +0 -4
  79. data/test/visitors/test_bulk_insert_oracle.rb +0 -1
  80. data/test/visitors/test_bulk_insert_sqlite.rb +0 -2
  81. data/test/visitors/test_oracle.rb +1 -2
  82. data/test/visitors/test_to_sql.rb +16 -25
  83. data/test/with_ar/all_agnostic_test.rb +134 -139
  84. data/test/with_ar/insert_agnostic_test.rb +0 -2
  85. data/test/with_ar/test_bulk_sqlite.rb +0 -4
  86. data/test/with_ar/test_math_sqlite.rb +4 -8
  87. data/test/with_ar/test_string_mysql.rb +1 -5
  88. data/test/with_ar/test_string_sqlite.rb +1 -5
  89. data/version_v1.rb +1 -1
  90. data/version_v2.rb +1 -1
  91. metadata +3 -2
@@ -1,7 +1,6 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class IMatches < Arel::Nodes::Matches
4
-
5
4
  attr_accessor :case_sensitive if Arel::VERSION.to_i < 7
6
5
 
7
6
  def initialize(left, right, escape = nil)
@@ -26,6 +25,5 @@ module ArelExtensions
26
25
 
27
26
  class SMatches < IMatches
28
27
  end
29
-
30
28
  end
31
29
  end
@@ -2,7 +2,6 @@ module ArelExtensions
2
2
  module Nodes
3
3
  class MD5 < Function
4
4
  RETURN_TYPE = :string
5
-
6
5
  end
7
6
  end
8
7
  end
@@ -6,7 +6,6 @@ module ArelExtensions
6
6
  def initialize expr
7
7
  super [convert_to_node(expr.first), convert_to_number(expr[1])]
8
8
  end
9
-
10
9
  end
11
10
  end
12
11
  end
@@ -10,7 +10,6 @@ module ArelExtensions
10
10
  super []
11
11
  end
12
12
  end
13
-
14
13
  end
15
14
  end
16
15
  end
@@ -13,8 +13,6 @@ module ArelExtensions
13
13
  def +(other)
14
14
  return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
15
15
  end
16
-
17
16
  end
18
-
19
17
  end
20
18
  end
@@ -14,7 +14,6 @@ module ArelExtensions
14
14
  def +(other)
15
15
  return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
16
16
  end
17
-
18
17
  end
19
18
 
20
19
  class RegexpReplace < Function
@@ -32,6 +31,5 @@ module ArelExtensions
32
31
  return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
33
32
  end
34
33
  end
35
-
36
34
  end
37
35
  end
@@ -10,7 +10,6 @@ module ArelExtensions
10
10
  super [convert_to_node(expr.first), convert_to_number(expr[1])]
11
11
  end
12
12
  end
13
-
14
13
  end
15
14
  end
16
15
  end
@@ -13,7 +13,6 @@ module ArelExtensions
13
13
  def !=(other)
14
14
  Arel::Nodes::NotEqual.new self, Arel::Nodes.build_quoted(other, self)
15
15
  end
16
-
17
16
  end
18
17
  end
19
18
  end
@@ -19,6 +19,5 @@ module ArelExtensions
19
19
  super node, opts
20
20
  end
21
21
  end
22
-
23
22
  end
24
23
  end
@@ -10,7 +10,6 @@ module ArelExtensions
10
10
  end
11
11
  return super(tab)
12
12
  end
13
-
14
13
  end
15
14
  end
16
15
  end
File without changes
File without changes
@@ -13,7 +13,6 @@ module ArelExtensions
13
13
  def +(other)
14
14
  return ArelExtensions::Nodes::Concat.new(self.expressions + [other])
15
15
  end
16
-
17
16
  end
18
17
 
19
18
  class Ltrim < Trim
@@ -23,6 +22,5 @@ module ArelExtensions
23
22
  class Rtrim < Trim
24
23
  RETURN_TYPE = :string
25
24
  end
26
-
27
25
  end
28
26
  end
@@ -1,7 +1,6 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Union < Arel::Nodes::Union
4
-
5
4
  def initialize left,right
6
5
  return super(left,right)
7
6
  end
@@ -18,6 +17,5 @@ module ArelExtensions
18
17
  Arel::Nodes::TableAlias.new Arel::Nodes::Grouping.new(self), Arel::Nodes::SqlLiteral.new(other.to_s)
19
18
  end
20
19
  end
21
-
22
20
  end
23
21
  end
@@ -1,7 +1,6 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class UnionAll < Arel::Nodes::UnionAll
4
-
5
4
  def initialize left,right
6
5
  return super(left,right)
7
6
  end
@@ -14,6 +13,5 @@ module ArelExtensions
14
13
  Arel::Nodes::TableAlias.new Arel::Nodes::Grouping.new(self), Arel::Nodes::SqlLiteral.new(other.to_s)
15
14
  end
16
15
  end
17
-
18
16
  end
19
17
  end
@@ -1,19 +1,15 @@
1
1
  module ArelExtensions
2
2
  module Nodes
3
3
  class Wday < Function
4
-
5
-
6
4
  def initialize other, aliaz = nil
7
5
  tab = Array.new
8
6
  tab << other
9
7
  super(tab, aliaz)
10
8
  end
11
9
 
12
-
13
10
  def date
14
11
  @expressions.first
15
12
  end
16
-
17
13
  end
18
14
  end
19
15
  end
@@ -3,23 +3,21 @@ require 'arel_extensions/nodes/is_null'
3
3
 
4
4
  module ArelExtensions
5
5
  module NullFunctions
6
-
7
- #ISNULL function lets you return an alternative value when an expression is NULL.
6
+ # ISNULL function lets you return an alternative value when an expression is NULL.
8
7
  def is_null
9
8
  ArelExtensions::Nodes::IsNull.new [self]
10
9
  end
11
10
 
12
- #ISNOTNULL function lets you return an alternative value when an expression is NOT NULL.
11
+ # ISNOTNULL function lets you return an alternative value when an expression is NOT NULL.
13
12
  def is_not_null
14
13
  ArelExtensions::Nodes::IsNotNull.new [self]
15
14
  end
16
15
 
17
16
  # returns the first non-null expr in the expression list. You must specify at least two expressions.
18
- #If all occurrences of expr evaluate to null, then the function returns null.
17
+ # If all occurrences of expr evaluate to null, then the function returns null.
19
18
  def coalesce *args
20
19
  args.unshift(self)
21
20
  ArelExtensions::Nodes::Coalesce.new args
22
21
  end
23
-
24
22
  end
25
23
  end
@@ -1,6 +1,5 @@
1
1
  module ArelExtensions
2
2
  module Predications
3
-
4
3
  def when right, expression = nil
5
4
  ArelExtensions::Nodes::Case.new(self).when(right,expression)
6
5
  end
@@ -21,7 +20,7 @@ module ArelExtensions
21
20
  ArelExtensions::Nodes::Cast.new([self,right])
22
21
  end
23
22
 
24
- def in(*other) #In should handle nil element in the Array
23
+ def in(*other) # In should handle nil element in the Array
25
24
  other = other.first if other.length <= 1
26
25
  case other
27
26
  when Range
@@ -49,7 +48,7 @@ module ArelExtensions
49
48
  end
50
49
  end
51
50
 
52
- def not_in(*other) #In should handle nil element in the Array
51
+ def not_in(*other) # In should handle nil element in the Array
53
52
  other = other.first if other.length <= 1
54
53
  case other
55
54
  when Range
@@ -1,9 +1,9 @@
1
1
  require 'rails'
2
2
 
3
3
  module ArelExtensions
4
- class Railtie < Rails::Railtie
5
- rake_tasks do
6
- load 'arel_extensions/tasks.rb'
7
- end
8
- end
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ load 'arel_extensions/tasks.rb'
7
+ end
8
+ end
9
9
  end
@@ -3,7 +3,6 @@ require 'arel_extensions/nodes/union_all'
3
3
 
4
4
  module ArelExtensions
5
5
  module SetFunctions
6
-
7
6
  def +(other)
8
7
  ArelExtensions::Nodes::Union.new(self,other)
9
8
  end
@@ -19,7 +18,6 @@ module ArelExtensions
19
18
  def uniq
20
19
  self
21
20
  end
22
-
23
21
  end
24
22
  end
25
23
 
@@ -1,4 +1,4 @@
1
- require 'arel_extensions/nodes/concat' #if Arel::VERSION.to_i < 7
1
+ require 'arel_extensions/nodes/concat' # if Arel::VERSION.to_i < 7
2
2
  require 'arel_extensions/nodes/length'
3
3
  require 'arel_extensions/nodes/locate'
4
4
  require 'arel_extensions/nodes/substring'
@@ -19,19 +19,18 @@ require 'arel_extensions/nodes/md5'
19
19
 
20
20
  module ArelExtensions
21
21
  module StringFunctions
22
-
23
- #*FindInSet function .......
22
+ # *FindInSet function .......
24
23
  def &(other)
25
24
  ArelExtensions::Nodes::FindInSet.new [other, self]
26
25
  end
27
26
 
28
- #LENGTH function returns the length of the value in a text field.
27
+ # LENGTH function returns the length of the value in a text field.
29
28
  def length
30
29
  ArelExtensions::Nodes::Length.new [self]
31
30
  end
32
31
 
33
- #LOCATE function returns the first starting position of a string in another string.
34
- #If string1 or string2 is NULL then it returns NULL. If string1 not found in string2 then it returns 0.
32
+ # LOCATE function returns the first starting position of a string in another string.
33
+ # If string1 or string2 is NULL then it returns NULL. If string1 not found in string2 then it returns 0.
35
34
  def locate val
36
35
  ArelExtensions::Nodes::Locate.new [self, val]
37
36
  end
@@ -51,7 +50,7 @@ module ArelExtensions
51
50
  end
52
51
  end
53
52
 
54
- #SOUNDEX function returns a character string containing the phonetic representation of char.
53
+ # SOUNDEX function returns a character string containing the phonetic representation of char.
55
54
  def soundex
56
55
  ArelExtensions::Nodes::Soundex.new [self]
57
56
  end
@@ -64,18 +63,18 @@ module ArelExtensions
64
63
  grouping_any :imatches, others, escape
65
64
  end
66
65
 
67
- # def grouping_any method, others, *extra
68
- # puts "*******************"
69
- # puts method
70
- # puts others.inspect
71
- # puts extra.inspect
72
- # puts "-------------------"
73
- # res = super(method,others,*extra)
74
- # puts res.to_sql
75
- # puts res.inspect
76
- # puts "*******************"
77
- # res
78
- # end
66
+ # def grouping_any method, others, *extra
67
+ # puts "*******************"
68
+ # puts method
69
+ # puts others.inspect
70
+ # puts extra.inspect
71
+ # puts "-------------------"
72
+ # res = super(method,others,*extra)
73
+ # puts res.to_sql
74
+ # puts res.inspect
75
+ # puts "*******************"
76
+ # res
77
+ # end
79
78
 
80
79
  def imatches_all others, escape = nil
81
80
  grouping_all :imatches, others, escape, escape
@@ -117,7 +116,7 @@ module ArelExtensions
117
116
  ArelExtensions::Nodes::Collate.new(self,option,ai,ci)
118
117
  end
119
118
 
120
- #REPLACE function replaces a sequence of characters in a string with another set of characters, not case-sensitive.
119
+ # REPLACE function replaces a sequence of characters in a string with another set of characters, not case-sensitive.
121
120
  def replace pattern, substitute
122
121
  if pattern.is_a? Regexp
123
122
  ArelExtensions::Nodes::RegexpReplace.new self, pattern, substitute
@@ -134,7 +133,7 @@ module ArelExtensions
134
133
  ArelExtensions::Nodes::Concat.new [self, other]
135
134
  end
136
135
 
137
- #concat elements of a group, separated by sep and ordered by a list of Ascending or Descending
136
+ # concat elements of a group, separated by sep and ordered by a list of Ascending or Descending
138
137
  def group_concat(sep = nil, *orders, group: nil, order: nil)
139
138
  if orders.present?
140
139
  warn("Warning : ArelExtensions: group_concat: you should now use the kwarg 'order' to specify an order in the group_concat.")
@@ -149,7 +148,7 @@ module ArelExtensions
149
148
  ArelExtensions::Nodes::GroupConcat.new(self, sep, group: group, order: (order || order_tabs))
150
149
  end
151
150
 
152
- #Function returns a string after removing left, right or the both prefixes or suffixes int argument
151
+ # Function returns a string after removing left, right or the both prefixes or suffixes int argument
153
152
  def trim other = ' '
154
153
  ArelExtensions::Nodes::Trim.new [self, other]
155
154
  end
@@ -1,6 +1,6 @@
1
1
  namespace :arel_extensions do
2
2
  desc 'Install DB functions into current DB'
3
- task :install_functions => :environment do
3
+ task install_functions: :environment do
4
4
  @env_db = if ENV['DB'] == 'oracle' && ((defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx") || (RUBY_PLATFORM == 'java')) # not supported
5
5
  (RUBY_PLATFORM == 'java' ? "jdbc-sqlite" : 'sqlite')
6
6
  else
@@ -1,3 +1,3 @@
1
1
  module ArelExtensions
2
- VERSION = "2.0.13".freeze
2
+ VERSION = "2.0.14".freeze
3
3
  end
@@ -1,3 +1,4 @@
1
+ require 'arel_extensions/visitors/convert_format'
1
2
  require 'arel_extensions/visitors/to_sql'
2
3
  require 'arel_extensions/visitors/mysql'
3
4
  require 'arel_extensions/visitors/oracle'
@@ -9,7 +10,7 @@ require 'arel_extensions/visitors/mssql'
9
10
  class Arel::Visitors::MSSQL
10
11
  include ArelExtensions::Visitors::MSSQL
11
12
 
12
- alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
13
+ alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
13
14
  def visit_Arel_Nodes_As o, collector
14
15
  if o.left.is_a?(Arel::Nodes::Binary)
15
16
  collector << '('
@@ -24,7 +25,7 @@ class Arel::Visitors::MSSQL
24
25
  collector
25
26
  end
26
27
 
27
- alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
28
+ alias_method(:old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement) rescue nil
28
29
  def visit_Arel_Nodes_SelectStatement o, collector
29
30
  if !collector.value.blank? && o.limit.blank? && o.offset.blank?
30
31
  o = o.dup
@@ -41,7 +42,7 @@ begin
41
42
  Arel::Visitors::VISITORS['sqlserver'].class_eval do
42
43
  include ArelExtensions::Visitors::MSSQL
43
44
 
44
- alias_method :old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement
45
+ alias_method(:old_visit_Arel_Nodes_SelectStatement, :visit_Arel_Nodes_SelectStatement) rescue nil
45
46
  def visit_Arel_Nodes_SelectStatement o, collector
46
47
  if !collector.value.blank? && o.limit.blank? && o.offset.blank?
47
48
  o = o.dup
@@ -50,7 +51,7 @@ begin
50
51
  old_visit_Arel_Nodes_SelectStatement(o,collector)
51
52
  end
52
53
 
53
- alias_method :old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As
54
+ alias_method(:old_visit_Arel_Nodes_As, :visit_Arel_Nodes_As) rescue nil
54
55
  def visit_Arel_Nodes_As o, collector
55
56
  if o.left.is_a?(Arel::Nodes::Binary)
56
57
  collector << '('
@@ -65,17 +66,18 @@ begin
65
66
  collector
66
67
  end
67
68
 
68
- alias_method :old_primary_Key_From_Table, :primary_Key_From_Table
69
+ alias_method(:old_primary_Key_From_Table, :primary_Key_From_Table) rescue nil
69
70
  def primary_Key_From_Table t
70
71
  return unless t
72
+
71
73
  column_name = @connection.schema_cache.primary_keys(t.name) ||
72
- @connection.schema_cache.columns_hash(t.name).first.try(:second).try(:name)
74
+ @connection.schema_cache.columns_hash(t.name).first.try(:second).try(:name)
73
75
  column_name ? t[column_name] : nil
74
76
  end
75
77
  end
76
78
  end
77
79
  end
78
- rescue LoadError
80
+ rescue LoadError
79
81
  rescue => e
80
82
  e
81
83
  end
@@ -0,0 +1,37 @@
1
+ module ArelExtensions
2
+ module Visitors
3
+ # Convert date format in strftime syntax to whatever the RDBMs
4
+ # wants, based on the table of conversion +mapping+.
5
+ def self.strftime_to_format format, mapping
6
+ @mapping_regexps ||= {}
7
+ @mapping_regexps[mapping] ||=
8
+ Regexp.new(
9
+ mapping
10
+ .keys
11
+ .map{|k| Regexp.escape(k)}
12
+ .join('|')
13
+ )
14
+
15
+ regexp = @mapping_regexps[mapping]
16
+ s = StringScanner.new format
17
+ res = StringIO.new
18
+ while !s.eos?
19
+ res <<
20
+ case
21
+ when s.scan(regexp)
22
+ if v = mapping[s.matched]
23
+ v
24
+ else
25
+ # Should never happen.
26
+ s.matched
27
+ end
28
+ when s.scan(/[^%]+/)
29
+ s.matched
30
+ when s.scan(/./)
31
+ s.matched
32
+ end
33
+ end
34
+ res.string
35
+ end
36
+ end
37
+ end