arel_extensions 2.1.3 → 2.1.4
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.
- checksums.yaml +4 -4
- data/.codeclimate.yml +1 -2
- data/.github/workflows/ruby.yml +4 -4
- data/.rubocop.yml +25 -1
- data/Gemfile +10 -10
- data/README.md +1 -2
- data/Rakefile +4 -4
- data/TODO +0 -1
- data/arel_extensions.gemspec +11 -11
- data/gemfiles/rails3.gemfile +10 -10
- data/gemfiles/rails4.gemfile +13 -13
- data/gemfiles/rails5_0.gemfile +12 -12
- data/gemfiles/rails5_1_4.gemfile +12 -12
- data/gemfiles/rails5_2.gemfile +13 -13
- data/gemfiles/rails6.gemfile +11 -11
- data/gemfiles/rails6_1.gemfile +11 -11
- data/gemfiles/rails7.gemfile +7 -7
- data/gemspecs/arel_extensions-v1.gemspec +12 -12
- data/gemspecs/arel_extensions-v2.gemspec +11 -11
- data/init/mssql.sql +0 -0
- data/init/mysql.sql +0 -0
- data/init/oracle.sql +0 -0
- data/init/postgresql.sql +0 -0
- data/init/sqlite.sql +0 -0
- data/lib/arel_extensions/attributes.rb +2 -2
- data/lib/arel_extensions/boolean_functions.rb +2 -4
- data/lib/arel_extensions/common_sql_functions.rb +10 -10
- data/lib/arel_extensions/comparators.rb +14 -14
- data/lib/arel_extensions/date_duration.rb +7 -7
- data/lib/arel_extensions/helpers.rb +16 -13
- data/lib/arel_extensions/insert_manager.rb +1 -1
- data/lib/arel_extensions/math.rb +16 -18
- data/lib/arel_extensions/math_functions.rb +18 -18
- data/lib/arel_extensions/nodes/abs.rb +0 -0
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -0
- data/lib/arel_extensions/nodes/blank.rb +1 -1
- data/lib/arel_extensions/nodes/case.rb +4 -6
- data/lib/arel_extensions/nodes/cast.rb +3 -3
- data/lib/arel_extensions/nodes/ceil.rb +0 -0
- data/lib/arel_extensions/nodes/change_case.rb +0 -0
- data/lib/arel_extensions/nodes/coalesce.rb +0 -0
- data/lib/arel_extensions/nodes/collate.rb +9 -9
- data/lib/arel_extensions/nodes/concat.rb +2 -2
- data/lib/arel_extensions/nodes/date_diff.rb +6 -6
- data/lib/arel_extensions/nodes/duration.rb +0 -0
- data/lib/arel_extensions/nodes/find_in_set.rb +0 -0
- data/lib/arel_extensions/nodes/floor.rb +0 -0
- data/lib/arel_extensions/nodes/format.rb +0 -0
- data/lib/arel_extensions/nodes/formatted_number.rb +2 -2
- data/lib/arel_extensions/nodes/function.rb +21 -21
- data/lib/arel_extensions/nodes/is_null.rb +0 -0
- data/lib/arel_extensions/nodes/json.rb +7 -7
- data/lib/arel_extensions/nodes/length.rb +0 -0
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
- data/lib/arel_extensions/nodes/locate.rb +0 -0
- data/lib/arel_extensions/nodes/log10.rb +0 -0
- data/lib/arel_extensions/nodes/matches.rb +1 -1
- data/lib/arel_extensions/nodes/md5.rb +0 -0
- data/lib/arel_extensions/nodes/power.rb +0 -0
- data/lib/arel_extensions/nodes/rand.rb +0 -0
- data/lib/arel_extensions/nodes/repeat.rb +0 -0
- data/lib/arel_extensions/nodes/replace.rb +2 -2
- data/lib/arel_extensions/nodes/round.rb +0 -0
- data/lib/arel_extensions/nodes/soundex.rb +2 -2
- data/lib/arel_extensions/nodes/std.rb +0 -0
- data/lib/arel_extensions/nodes/substring.rb +0 -0
- data/lib/arel_extensions/nodes/sum.rb +0 -0
- data/lib/arel_extensions/nodes/then.rb +0 -0
- data/lib/arel_extensions/nodes/trim.rb +0 -0
- data/lib/arel_extensions/nodes/union.rb +4 -4
- data/lib/arel_extensions/nodes/union_all.rb +3 -3
- data/lib/arel_extensions/nodes/wday.rb +0 -0
- data/lib/arel_extensions/nodes.rb +0 -0
- data/lib/arel_extensions/null_functions.rb +0 -0
- data/lib/arel_extensions/predications.rb +10 -10
- data/lib/arel_extensions/railtie.rb +1 -1
- data/lib/arel_extensions/set_functions.rb +3 -3
- data/lib/arel_extensions/string_functions.rb +8 -8
- data/lib/arel_extensions/tasks.rb +2 -2
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/convert_format.rb +0 -0
- data/lib/arel_extensions/visitors/ibm_db.rb +19 -19
- data/lib/arel_extensions/visitors/mssql.rb +127 -121
- data/lib/arel_extensions/visitors/mysql.rb +137 -145
- data/lib/arel_extensions/visitors/oracle.rb +128 -128
- data/lib/arel_extensions/visitors/oracle12.rb +16 -16
- data/lib/arel_extensions/visitors/postgresql.rb +139 -137
- data/lib/arel_extensions/visitors/sqlite.rb +83 -85
- data/lib/arel_extensions/visitors/to_sql.rb +143 -148
- data/lib/arel_extensions/visitors.rb +7 -7
- data/lib/arel_extensions.rb +46 -32
- data/test/arelx_test_helper.rb +14 -13
- data/test/database.yml +5 -5
- data/test/real_db_test.rb +78 -78
- data/test/support/fake_record.rb +1 -1
- data/test/test_comparators.rb +5 -5
- data/test/visitors/test_bulk_insert_oracle.rb +4 -4
- data/test/visitors/test_bulk_insert_sqlite.rb +4 -4
- data/test/visitors/test_bulk_insert_to_sql.rb +4 -4
- data/test/visitors/test_oracle.rb +14 -14
- data/test/visitors/test_to_sql.rb +82 -82
- data/test/with_ar/all_agnostic_test.rb +364 -299
- data/test/with_ar/insert_agnostic_test.rb +17 -14
- data/test/with_ar/test_bulk_sqlite.rb +4 -4
- data/test/with_ar/test_math_sqlite.rb +12 -12
- data/test/with_ar/test_string_mysql.rb +20 -20
- data/test/with_ar/test_string_sqlite.rb +20 -20
- data/version_v1.rb +1 -1
- data/version_v2.rb +1 -1
- metadata +2 -9
- data/.travis/oracle/download.js +0 -152
- data/.travis/oracle/download.sh +0 -30
- data/.travis/oracle/download_ojdbc.js +0 -116
- data/.travis/oracle/install.sh +0 -34
- data/.travis/setup_accounts.sh +0 -9
- data/.travis/sqlite3/extension-functions.sh +0 -6
- data/.travis.yml +0 -193
|
@@ -19,11 +19,11 @@ module ArelExtensions
|
|
|
19
19
|
include ArelExtensions::Predications
|
|
20
20
|
|
|
21
21
|
def ==(other)
|
|
22
|
-
Arel::Nodes::Equality.new self, Arel
|
|
22
|
+
Arel::Nodes::Equality.new self, Arel.quoted(other, self)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def !=(other)
|
|
26
|
-
Arel::Nodes::NotEqual.new self, Arel
|
|
26
|
+
Arel::Nodes::NotEqual.new self, Arel.quoted(other, self)
|
|
27
27
|
end
|
|
28
28
|
end
|
|
29
29
|
end
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
|
-
|
|
3
1
|
require 'arel_extensions/nodes/then'
|
|
4
2
|
|
|
5
3
|
module ArelExtensions
|
|
@@ -32,7 +30,7 @@ class Arel::Nodes::And
|
|
|
32
30
|
def self.new *children
|
|
33
31
|
children =
|
|
34
32
|
children.flatten.map { |c|
|
|
35
|
-
|
|
33
|
+
c.is_a?(self) ? c.children : c
|
|
36
34
|
}.flatten
|
|
37
35
|
super(children)
|
|
38
36
|
end
|
|
@@ -52,7 +50,7 @@ class Arel::Nodes::Or
|
|
|
52
50
|
def self.new *children
|
|
53
51
|
children =
|
|
54
52
|
children.flatten.map { |c|
|
|
55
|
-
|
|
53
|
+
c.is_a?(self) ? c.children : c
|
|
56
54
|
}.flatten
|
|
57
55
|
super(*children)
|
|
58
56
|
end
|
|
@@ -6,8 +6,8 @@ module ArelExtensions
|
|
|
6
6
|
begin
|
|
7
7
|
db = cnx.raw_connection
|
|
8
8
|
db.enable_load_extension(1)
|
|
9
|
-
db.load_extension(
|
|
10
|
-
db.load_extension(
|
|
9
|
+
db.load_extension('/usr/lib/sqlite3/pcre.so')
|
|
10
|
+
db.load_extension('/usr/lib/sqlite3/extension-functions.so')
|
|
11
11
|
db.enable_load_extension(0)
|
|
12
12
|
rescue => e
|
|
13
13
|
$load_extension_disabled = true
|
|
@@ -18,22 +18,22 @@ module ArelExtensions
|
|
|
18
18
|
|
|
19
19
|
def add_sqlite_functions
|
|
20
20
|
db = @cnx.raw_connection
|
|
21
|
-
db.create_function(
|
|
21
|
+
db.create_function('find_in_set', 1) do |func, val, list|
|
|
22
22
|
case list
|
|
23
23
|
when String
|
|
24
24
|
i = list.split(',').index(val.to_s)
|
|
25
|
-
func.result = i ? (i+1) : 0
|
|
25
|
+
func.result = i ? (i + 1) : 0
|
|
26
26
|
when NilClass
|
|
27
27
|
func.result = nil
|
|
28
28
|
else
|
|
29
29
|
i = list.to_s.split(',').index(val.to_s)
|
|
30
|
-
func.result = i ? (i+1) : 0
|
|
30
|
+
func.result = i ? (i + 1) : 0
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
|
-
db.create_function(
|
|
33
|
+
db.create_function('instr', 1) do |func, value1, value2|
|
|
34
34
|
i = value1.to_s.index(value2.to_s)
|
|
35
|
-
func.result = i ? (i+1) : 0
|
|
36
|
-
end rescue
|
|
35
|
+
func.result = i ? (i + 1) : 0
|
|
36
|
+
end rescue 'function instr already here (>= 3.8.5)'
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def add_sql_functions(env_db = nil)
|
|
@@ -52,8 +52,8 @@ module ArelExtensions
|
|
|
52
52
|
sql.split(/^GO\s*$/).each {|str|
|
|
53
53
|
@cnx.execute(str.strip) unless str.blank?
|
|
54
54
|
}
|
|
55
|
-
elsif env_db =='mysql'
|
|
56
|
-
sql.split(
|
|
55
|
+
elsif env_db == 'mysql'
|
|
56
|
+
sql.split('$$')[1..-2].each { |str|
|
|
57
57
|
@cnx.execute(str.strip) unless str.strip.blank?
|
|
58
58
|
}
|
|
59
59
|
else
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
module ArelExtensions
|
|
2
2
|
module Comparators
|
|
3
3
|
def >(other)
|
|
4
|
-
Arel::Nodes::GreaterThan.new self, Arel
|
|
4
|
+
Arel::Nodes::GreaterThan.new self, Arel.quoted(other, self)
|
|
5
5
|
end
|
|
6
6
|
|
|
7
7
|
def >=(other)
|
|
8
|
-
Arel::Nodes::GreaterThanOrEqual.new self, Arel
|
|
8
|
+
Arel::Nodes::GreaterThanOrEqual.new self, Arel.quoted(other, self)
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
def <(other)
|
|
12
|
-
Arel::Nodes::LessThan.new self, Arel
|
|
12
|
+
Arel::Nodes::LessThan.new self, Arel.quoted(other, self)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def <=(other)
|
|
16
|
-
Arel::Nodes::LessThanOrEqual.new self, Arel
|
|
16
|
+
Arel::Nodes::LessThanOrEqual.new self, Arel.quoted(other, self)
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
# REGEXP function
|
|
@@ -21,7 +21,7 @@ module ArelExtensions
|
|
|
21
21
|
def =~(other)
|
|
22
22
|
# arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
|
|
23
23
|
# if arg == :string || arg == :text
|
|
24
|
-
|
|
24
|
+
Arel::Nodes::Regexp.new self, convert_regexp(other)
|
|
25
25
|
# end
|
|
26
26
|
end
|
|
27
27
|
|
|
@@ -30,7 +30,7 @@ module ArelExtensions
|
|
|
30
30
|
def !~(other)
|
|
31
31
|
# arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
|
|
32
32
|
# if arg == :string || arg == :text
|
|
33
|
-
|
|
33
|
+
Arel::Nodes::NotRegexp.new self, convert_regexp(other)
|
|
34
34
|
# end
|
|
35
35
|
end
|
|
36
36
|
|
|
@@ -42,17 +42,17 @@ module ArelExtensions
|
|
|
42
42
|
when String
|
|
43
43
|
# Do nothing.
|
|
44
44
|
when Regexp
|
|
45
|
-
other = other.source.gsub('\A','^')
|
|
46
|
-
other.gsub!('\z','$')
|
|
47
|
-
other.gsub!('\Z','$')
|
|
48
|
-
other.gsub!('\d','[0-9]')
|
|
49
|
-
other.gsub!('\D','[^0-9]')
|
|
50
|
-
other.gsub!('\w','[0-9A-Za-z]')
|
|
51
|
-
other.gsub!('\W','[^A-Za-z0-9_]')
|
|
45
|
+
other = other.source.gsub('\A', '^')
|
|
46
|
+
other.gsub!('\z', '$')
|
|
47
|
+
other.gsub!('\Z', '$')
|
|
48
|
+
other.gsub!('\d', '[0-9]')
|
|
49
|
+
other.gsub!('\D', '[^0-9]')
|
|
50
|
+
other.gsub!('\w', '[0-9A-Za-z]')
|
|
51
|
+
other.gsub!('\W', '[^A-Za-z0-9_]')
|
|
52
52
|
else
|
|
53
53
|
raise(ArgumentError)
|
|
54
54
|
end
|
|
55
|
-
Arel
|
|
55
|
+
Arel.quoted(other, self)
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
end
|
|
@@ -6,22 +6,22 @@ module ArelExtensions
|
|
|
6
6
|
module DateDuration
|
|
7
7
|
# function returns the year (as a number) given a date value.
|
|
8
8
|
def year
|
|
9
|
-
ArelExtensions::Nodes::Duration.new
|
|
9
|
+
ArelExtensions::Nodes::Duration.new 'y', self
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
# function returns the month (as a number) given a date value.
|
|
13
13
|
def month
|
|
14
|
-
ArelExtensions::Nodes::Duration.new
|
|
14
|
+
ArelExtensions::Nodes::Duration.new 'm', self
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
# function returns the week (as a number) given a date value.
|
|
18
18
|
def week
|
|
19
|
-
ArelExtensions::Nodes::Duration.new
|
|
19
|
+
ArelExtensions::Nodes::Duration.new 'w', self
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
# function returns the month (as a number) given a date value.
|
|
23
23
|
def day
|
|
24
|
-
ArelExtensions::Nodes::Duration.new
|
|
24
|
+
ArelExtensions::Nodes::Duration.new 'd', self
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
def wday
|
|
@@ -29,15 +29,15 @@ module ArelExtensions
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def hour
|
|
32
|
-
ArelExtensions::Nodes::Duration.new
|
|
32
|
+
ArelExtensions::Nodes::Duration.new 'h', self
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def minute
|
|
36
|
-
ArelExtensions::Nodes::Duration.new
|
|
36
|
+
ArelExtensions::Nodes::Duration.new 'mn', self
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def second
|
|
40
|
-
ArelExtensions::Nodes::Duration.new
|
|
40
|
+
ArelExtensions::Nodes::Duration.new 's', self
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def format(tpl, time_zone = nil)
|
|
@@ -16,33 +16,36 @@ module ArelExtensions
|
|
|
16
16
|
# coveing all these cases.
|
|
17
17
|
|
|
18
18
|
def self.column_of_via_arel_table(table_name, column_name)
|
|
19
|
-
|
|
20
|
-
Arel::Table.engine.connection.schema_cache.columns_hash(table_name)[column_name]
|
|
19
|
+
Arel::Table.engine.connection.schema_cache.columns_hash(table_name)[column_name]
|
|
21
20
|
rescue NoMethodError
|
|
22
21
|
nil
|
|
23
|
-
rescue
|
|
24
|
-
|
|
25
|
-
puts "This should never be reached."
|
|
26
|
-
puts "#{e.class}: #{e}"
|
|
22
|
+
rescue => e
|
|
23
|
+
warn("Warning: Unexpected exception caught while fetching column name for #{table_name}.#{column_name} in `column_of_via_arel_table`\n#{e.class}: #{e}")
|
|
27
24
|
nil
|
|
28
|
-
end
|
|
29
25
|
end
|
|
30
26
|
|
|
31
27
|
def self.column_of(table_name, column_name)
|
|
28
|
+
pool = ActiveRecord::Base.connection.pool
|
|
32
29
|
use_arel_table = !ActiveRecord::Base.connected? || \
|
|
33
|
-
(
|
|
30
|
+
(pool.respond_to?(:schema_cache) && pool.schema_cache.nil?)
|
|
34
31
|
|
|
35
32
|
if use_arel_table
|
|
36
33
|
column_of_via_arel_table(table_name, column_name)
|
|
37
34
|
else
|
|
38
|
-
if
|
|
39
|
-
|
|
40
|
-
elsif
|
|
41
|
-
|
|
35
|
+
if pool.respond_to?(:pool_config)
|
|
36
|
+
pool.pool_config.schema_cache.columns_hash(table_name)[column_name]
|
|
37
|
+
elsif pool.respond_to?(:schema_cache)
|
|
38
|
+
pool.schema_cache.columns_hash(table_name)[column_name]
|
|
42
39
|
else
|
|
43
|
-
puts ">>> We really shouldn't be here #{table_name}.#{column_name}"
|
|
44
40
|
column_of_via_arel_table(table_name, column_name)
|
|
45
41
|
end
|
|
46
42
|
end
|
|
43
|
+
rescue ActiveRecord::ConnectionNotEstablished
|
|
44
|
+
column_of_via_arel_table(table_name, column_name)
|
|
45
|
+
rescue ActiveRecord::StatementInvalid
|
|
46
|
+
nil
|
|
47
|
+
rescue => e
|
|
48
|
+
warn("Warning: Unexpected exception caught while fetching column name for #{table_name}.#{column_name} in `column_of`\n#{e.class}: #{e}")
|
|
49
|
+
nil
|
|
47
50
|
end
|
|
48
51
|
end
|
data/lib/arel_extensions/math.rb
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require 'arel_extensions/helpers'
|
|
2
|
-
|
|
3
1
|
require 'arel_extensions/nodes'
|
|
4
2
|
require 'arel_extensions/nodes/function'
|
|
5
3
|
require 'arel_extensions/nodes/concat'
|
|
@@ -26,7 +24,7 @@ module ArelExtensions
|
|
|
26
24
|
else
|
|
27
25
|
return Arel.grouping(Arel::Nodes::Addition.new self, other)
|
|
28
26
|
end
|
|
29
|
-
when ArelExtensions::Nodes::Function,ArelExtensions::Nodes::Case
|
|
27
|
+
when ArelExtensions::Nodes::Function, ArelExtensions::Nodes::Case
|
|
30
28
|
return case self.return_type
|
|
31
29
|
when :string, :text
|
|
32
30
|
self.concat(other)
|
|
@@ -40,17 +38,17 @@ module ArelExtensions
|
|
|
40
38
|
when Arel::Nodes::Function
|
|
41
39
|
Arel.grouping(Arel::Nodes::Addition.new self, other)
|
|
42
40
|
else
|
|
43
|
-
col =
|
|
41
|
+
col = Arel.column_of(self.relation.table_name, self.name.to_s) if self.respond_to?(:relation)
|
|
44
42
|
if (!col) # if the column doesn't exist in the database
|
|
45
|
-
Arel.grouping(Arel::Nodes::Addition.new(self, Arel
|
|
43
|
+
Arel.grouping(Arel::Nodes::Addition.new(self, Arel.quoted(other)))
|
|
46
44
|
else
|
|
47
45
|
arg = col.type
|
|
48
46
|
if arg == :integer || (!arg)
|
|
49
47
|
other = other.to_i if other.is_a?(String)
|
|
50
|
-
Arel.grouping(Arel::Nodes::Addition.new self, Arel
|
|
48
|
+
Arel.grouping(Arel::Nodes::Addition.new self, Arel.quoted(other))
|
|
51
49
|
elsif arg == :decimal || arg == :float
|
|
52
50
|
other = Arel.sql(other) if other.is_a?(String) # Arel should accept Float & BigDecimal!
|
|
53
|
-
Arel.grouping(Arel::Nodes::Addition.new self, Arel
|
|
51
|
+
Arel.grouping(Arel::Nodes::Addition.new self, Arel.quoted(other))
|
|
54
52
|
elsif arg == :datetime || arg == :date
|
|
55
53
|
ArelExtensions::Nodes::DateAdd.new [self, other]
|
|
56
54
|
elsif arg == :string || arg == :text
|
|
@@ -66,33 +64,33 @@ module ArelExtensions
|
|
|
66
64
|
case self
|
|
67
65
|
when Arel::Nodes::Grouping
|
|
68
66
|
if self.expr.left.is_a?(Date) || self.expr.left.is_a?(DateTime)
|
|
69
|
-
Arel.grouping(ArelExtensions::Nodes::DateSub.new [self, Arel
|
|
67
|
+
Arel.grouping(ArelExtensions::Nodes::DateSub.new [self, Arel.quoted(other)])
|
|
70
68
|
else
|
|
71
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel
|
|
69
|
+
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
72
70
|
end
|
|
73
71
|
when ArelExtensions::Nodes::Function, ArelExtensions::Nodes::Case
|
|
74
72
|
case self.return_type
|
|
75
73
|
when :string, :text # ???
|
|
76
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel
|
|
74
|
+
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other))) # ??
|
|
77
75
|
when :integer, :decimal, :float, :number
|
|
78
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel
|
|
76
|
+
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
79
77
|
when :date, :datetime
|
|
80
|
-
ArelExtensions::Nodes::DateSub.new [self, Arel
|
|
78
|
+
ArelExtensions::Nodes::DateSub.new [self, Arel.quoted(other)]
|
|
81
79
|
else
|
|
82
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel
|
|
80
|
+
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
83
81
|
end
|
|
84
82
|
when Arel::Nodes::Function
|
|
85
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel
|
|
83
|
+
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
86
84
|
else
|
|
87
|
-
col =
|
|
85
|
+
col = Arel.column_of(self.relation.table_name, self.name.to_s)
|
|
88
86
|
if (!col) # if the column doesn't exist in the database
|
|
89
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel
|
|
87
|
+
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
90
88
|
else
|
|
91
89
|
arg = col.type
|
|
92
90
|
if (arg == :date || arg == :datetime)
|
|
93
91
|
case other
|
|
94
92
|
when Arel::Attributes::Attribute
|
|
95
|
-
col2 =
|
|
93
|
+
col2 = Arel.column_of(other.relation.table_name, other.name.to_s)
|
|
96
94
|
if (!col2) # if the column doesn't exist in the database
|
|
97
95
|
ArelExtensions::Nodes::DateSub.new [self, other]
|
|
98
96
|
else
|
|
@@ -117,7 +115,7 @@ module ArelExtensions
|
|
|
117
115
|
when String
|
|
118
116
|
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.sql(other)))
|
|
119
117
|
else
|
|
120
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel
|
|
118
|
+
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
121
119
|
end
|
|
122
120
|
end
|
|
123
121
|
end
|
|
@@ -15,7 +15,7 @@ module ArelExtensions
|
|
|
15
15
|
def * other
|
|
16
16
|
case other
|
|
17
17
|
when Float, BigDecimal
|
|
18
|
-
super(Arel
|
|
18
|
+
super(Arel.quoted(other))
|
|
19
19
|
else
|
|
20
20
|
super(other)
|
|
21
21
|
end
|
|
@@ -25,7 +25,7 @@ module ArelExtensions
|
|
|
25
25
|
def / other
|
|
26
26
|
case other
|
|
27
27
|
when Float, BigDecimal
|
|
28
|
-
super(Arel
|
|
28
|
+
super(Arel.quoted(other))
|
|
29
29
|
else
|
|
30
30
|
super(other)
|
|
31
31
|
end
|
|
@@ -33,32 +33,32 @@ module ArelExtensions
|
|
|
33
33
|
|
|
34
34
|
# Abs function returns the absolute value of a number passed as argument #
|
|
35
35
|
def abs
|
|
36
|
-
|
|
36
|
+
ArelExtensions::Nodes::Abs.new [self]
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
# will rounded up any positive or negative decimal value within the function upwards #
|
|
40
40
|
def ceil
|
|
41
|
-
|
|
41
|
+
ArelExtensions::Nodes::Ceil.new [self]
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
# function rounded up any positive or negative decimal value down to the next least integer
|
|
45
45
|
def floor
|
|
46
|
-
|
|
46
|
+
ArelExtensions::Nodes::Floor.new [self]
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
# function gives the base 10 log
|
|
50
50
|
def log10
|
|
51
|
-
|
|
51
|
+
ArelExtensions::Nodes::Log10.new [self]
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
# function gives the power of a number
|
|
55
55
|
def pow exposant = 0
|
|
56
|
-
|
|
56
|
+
ArelExtensions::Nodes::Power.new [self, exposant]
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
# function gives the power of a number
|
|
60
60
|
def power exposant = 0
|
|
61
|
-
|
|
61
|
+
ArelExtensions::Nodes::Power.new [self, exposant]
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
# Aggregate Functions
|
|
@@ -71,7 +71,7 @@ module ArelExtensions
|
|
|
71
71
|
end
|
|
72
72
|
|
|
73
73
|
def sum opts = {unbiased: true}
|
|
74
|
-
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new(
|
|
74
|
+
if Gem::Version.new(Arel::VERSION) >= Gem::Version.new('9.0.0')
|
|
75
75
|
Arel::Nodes::Sum.new [self]
|
|
76
76
|
else
|
|
77
77
|
ArelExtensions::Nodes::Sum.new self, **opts
|
|
@@ -86,17 +86,17 @@ module ArelExtensions
|
|
|
86
86
|
|
|
87
87
|
# function is used to round a numeric field to the number of decimals specified
|
|
88
88
|
def round precision = nil
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
89
|
+
if precision
|
|
90
|
+
ArelExtensions::Nodes::Round.new [self, precision]
|
|
91
|
+
else
|
|
92
|
+
ArelExtensions::Nodes::Round.new [self]
|
|
93
|
+
end
|
|
94
94
|
end
|
|
95
95
|
|
|
96
96
|
# function returning a number at a specific format
|
|
97
97
|
def format_number format_string, locale = nil
|
|
98
98
|
begin
|
|
99
|
-
sprintf(format_string,0) # this line is to get the right error message if the format_string is not correct
|
|
99
|
+
sprintf(format_string, 0) # this line is to get the right error message if the format_string is not correct
|
|
100
100
|
m = /^(.*)%([ #+\-0]*)([1-9][0-9]+|[1-9]?)[.]?([0-9]*)([a-zA-Z])(.*)$/.match(format_string)
|
|
101
101
|
opts = {
|
|
102
102
|
prefix: m[1],
|
|
@@ -108,10 +108,10 @@ module ArelExtensions
|
|
|
108
108
|
locale: locale,
|
|
109
109
|
original_string: format_string
|
|
110
110
|
}
|
|
111
|
-
# opts = {:
|
|
112
|
-
ArelExtensions::Nodes::FormattedNumber.new [self,opts]
|
|
111
|
+
# opts = {locale: 'fr_FR', type: "e"/"f"/"d", prefix: "$ ", suffix: " %", flags: " +-#0", width: 5, precision: 6}
|
|
112
|
+
ArelExtensions::Nodes::FormattedNumber.new [self, opts]
|
|
113
113
|
rescue Exception
|
|
114
|
-
Arel
|
|
114
|
+
Arel.quoted('Wrong Format')
|
|
115
115
|
end
|
|
116
116
|
end
|
|
117
117
|
end
|
|
File without changes
|
|
File without changes
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
require 'arel_extensions/helpers'
|
|
2
|
-
|
|
3
1
|
module ArelExtensions
|
|
4
2
|
module Nodes
|
|
5
|
-
if Gem::Version.new(Arel::VERSION) < Gem::Version.new(
|
|
3
|
+
if Gem::Version.new(Arel::VERSION) < Gem::Version.new('7.1.0')
|
|
6
4
|
class Case < Arel::Nodes::Node
|
|
7
5
|
include Arel::Expressions
|
|
8
6
|
include Arel::Math
|
|
@@ -46,7 +44,7 @@ module ArelExtensions
|
|
|
46
44
|
|
|
47
45
|
def return_type
|
|
48
46
|
obj = if @conditions.length > 0
|
|
49
|
-
|
|
47
|
+
@conditions.last.right
|
|
50
48
|
elsif @default
|
|
51
49
|
@default.expr
|
|
52
50
|
end
|
|
@@ -56,10 +54,10 @@ module ArelExtensions
|
|
|
56
54
|
case obj
|
|
57
55
|
when Integer, Float
|
|
58
56
|
:number
|
|
59
|
-
when Date, DateTime,Time
|
|
57
|
+
when Date, DateTime, Time
|
|
60
58
|
:datetime
|
|
61
59
|
when Arel::Attributes::Attribute
|
|
62
|
-
|
|
60
|
+
Arel.column_of(obj.relation.table_name, obj.name.to_s)&.type || :string
|
|
63
61
|
else
|
|
64
62
|
:string
|
|
65
63
|
end
|
|
@@ -19,11 +19,11 @@ module ArelExtensions
|
|
|
19
19
|
when 'text', :text, 'ntext', :ntext
|
|
20
20
|
@as_attr = expr[1].to_sym
|
|
21
21
|
@return_type = :string
|
|
22
|
-
when :datetime, 'datetime','smalldatetime'
|
|
22
|
+
when :datetime, 'datetime', 'smalldatetime'
|
|
23
23
|
@return_type = :datetime
|
|
24
|
-
when :time,'time'
|
|
24
|
+
when :time, 'time'
|
|
25
25
|
@return_type = :time
|
|
26
|
-
when :date,'date'
|
|
26
|
+
when :date, 'date'
|
|
27
27
|
@return_type = :date
|
|
28
28
|
when :binary, 'binary', 'varbinary', 'image'
|
|
29
29
|
@return_type = :binary
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
module ArelExtensions
|
|
2
2
|
module Nodes
|
|
3
3
|
class Collate < Function
|
|
4
|
-
|
|
4
|
+
RETURN_TYPE = :string
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
attr_accessor :ai, :ci, :option
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
def initialize left, option = nil, ai = false, ci = false
|
|
9
|
+
@ai = ai
|
|
10
|
+
@ci = ci
|
|
11
|
+
@option = option
|
|
12
|
+
tab = [convert_to_node(left)]
|
|
13
|
+
return super(tab)
|
|
14
|
+
end
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
17
|
end
|
|
@@ -13,10 +13,10 @@ module ArelExtensions::Nodes
|
|
|
13
13
|
end
|
|
14
14
|
}.flatten.reduce([]) { |res, b|
|
|
15
15
|
# concatenate successive literal strings.
|
|
16
|
-
if b.is_a?(Arel::Nodes::Quoted) && b.expr ==
|
|
16
|
+
if b.is_a?(Arel::Nodes::Quoted) && b.expr == ''
|
|
17
17
|
res
|
|
18
18
|
elsif res.last && res.last.is_a?(Arel::Nodes::Quoted) && b.is_a?(Arel::Nodes::Quoted)
|
|
19
|
-
res[-1] = Arel
|
|
19
|
+
res[-1] = Arel.quoted(res.last.expr + b.expr)
|
|
20
20
|
else
|
|
21
21
|
res << b
|
|
22
22
|
end
|
|
@@ -50,9 +50,9 @@ module ArelExtensions
|
|
|
50
50
|
v = self.expressions.last
|
|
51
51
|
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
|
|
52
52
|
if @date_type == :date
|
|
53
|
-
return Arel
|
|
53
|
+
return Arel.quoted((v.value >= 0 ? '+' : '-') + v.inspect)
|
|
54
54
|
elsif @date_type == :datetime
|
|
55
|
-
return Arel
|
|
55
|
+
return Arel.quoted((v.value >= 0 ? '+' : '-') + v.inspect)
|
|
56
56
|
end
|
|
57
57
|
else
|
|
58
58
|
return v
|
|
@@ -115,7 +115,7 @@ module ArelExtensions
|
|
|
115
115
|
v ||= self.expressions.last
|
|
116
116
|
if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
|
|
117
117
|
if @date_type == :date
|
|
118
|
-
v.to_i / (24*3600)
|
|
118
|
+
v.to_i / (24 * 3600)
|
|
119
119
|
elsif @date_type == :datetime
|
|
120
120
|
if v.parts.size == 1
|
|
121
121
|
# first entry in the dict v.parts; one of [:years, :months, :weeks, :days, :hours, :minutes, :seconds]
|
|
@@ -155,9 +155,9 @@ module ArelExtensions
|
|
|
155
155
|
if ArelExtensions::Nodes::Duration === v
|
|
156
156
|
v.with_interval = true
|
|
157
157
|
case v.left
|
|
158
|
-
when 'd','m','y'
|
|
158
|
+
when 'd', 'm', 'y'
|
|
159
159
|
Arel.sql('day')
|
|
160
|
-
when 'h','mn','s'
|
|
160
|
+
when 'h', 'mn', 's'
|
|
161
161
|
Arel.sql('second')
|
|
162
162
|
when /i\z/
|
|
163
163
|
Arel.sql(ArelExtensions::Visitors::MSSQL::LOADED_VISITOR::DATE_MAPPING[v.left[0..-2]])
|
|
@@ -179,7 +179,7 @@ module ArelExtensions
|
|
|
179
179
|
when DateTime, Time, Date
|
|
180
180
|
raise(ArgumentError, "#{object.class} cannot be converted to Integer")
|
|
181
181
|
when String
|
|
182
|
-
Arel
|
|
182
|
+
Arel.quoted(object)
|
|
183
183
|
else
|
|
184
184
|
raise(ArgumentError, "#{object.class} cannot be converted to Integer")
|
|
185
185
|
end
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -3,10 +3,10 @@ module ArelExtensions
|
|
|
3
3
|
class FormattedNumber < Function
|
|
4
4
|
RETURN_TYPE = :string
|
|
5
5
|
|
|
6
|
-
attr_accessor :locale, :prefix, :suffix, :flags, :scientific_notation, :width
|
|
6
|
+
attr_accessor :locale, :prefix, :suffix, :flags, :scientific_notation, :width, :precision, :type, :original_string
|
|
7
7
|
|
|
8
8
|
def initialize expr
|
|
9
|
-
# expr[1] = {:
|
|
9
|
+
# expr[1] = {locale: 'fr_FR', type: "e"/"f"/"d", prefix: "$ ", suffix: " %", flags: " +-#0", width: 5, precision: 6}
|
|
10
10
|
col = expr.first
|
|
11
11
|
@locale = expr[1][:locale]
|
|
12
12
|
@prefix = expr[1][:prefix]
|