arel_extensions 2.4.0 → 2.5.0
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/.gitignore +2 -1
- data/.rubocop.yml +131 -5
- data/Makefile +31 -1
- data/NEWS.md +15 -0
- data/README.md +11 -2
- data/Rakefile +5 -3
- data/arel_extensions.gemspec +3 -5
- data/gemfiles/rails5.gemfile +15 -8
- data/gemfiles/rails5_1_4.gemfile +11 -9
- data/gemfiles/rails5_2.gemfile +16 -9
- data/gemfiles/rails6.gemfile +13 -7
- data/gemfiles/rails6_1.gemfile +13 -7
- data/gemfiles/rails7.gemfile +15 -9
- data/gemfiles/rails7_1.gemfile +15 -8
- data/gemfiles/rails7_2.gemfile +12 -6
- data/gemfiles/rails8.gemfile +7 -7
- data/gemfiles/rails8_1.gemfile +4 -4
- data/gemspecs/arel_extensions-v1.gemspec +3 -5
- data/gemspecs/arel_extensions-v2.gemspec +3 -5
- data/lib/arel_extensions/aliases.rb +3 -3
- data/lib/arel_extensions/attributes.rb +2 -0
- data/lib/arel_extensions/boolean_functions.rb +9 -7
- data/lib/arel_extensions/common_sql_functions.rb +5 -3
- data/lib/arel_extensions/comparators.rb +6 -4
- data/lib/arel_extensions/date_duration.rb +2 -0
- data/lib/arel_extensions/helpers.rb +17 -17
- data/lib/arel_extensions/insert_manager.rb +5 -1
- data/lib/arel_extensions/math.rb +40 -44
- data/lib/arel_extensions/math_functions.rb +28 -30
- data/lib/arel_extensions/nodes/aggregate_function.rb +4 -4
- data/lib/arel_extensions/nodes/blank.rb +6 -4
- data/lib/arel_extensions/nodes/case.rb +15 -12
- data/lib/arel_extensions/nodes/cast.rb +3 -5
- data/lib/arel_extensions/nodes/coalesce.rb +1 -1
- data/lib/arel_extensions/nodes/collate.rb +1 -1
- data/lib/arel_extensions/nodes/concat.rb +6 -6
- data/lib/arel_extensions/nodes/date_diff.rb +40 -49
- data/lib/arel_extensions/nodes/duration.rb +2 -2
- data/lib/arel_extensions/nodes/format.rb +2 -2
- data/lib/arel_extensions/nodes/formatted_date.rb +2 -2
- data/lib/arel_extensions/nodes/formatted_number.rb +2 -2
- data/lib/arel_extensions/nodes/function.rb +6 -4
- data/lib/arel_extensions/nodes/is_null.rb +2 -0
- data/lib/arel_extensions/nodes/json.rb +27 -15
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +2 -2
- data/lib/arel_extensions/nodes/locate.rb +1 -1
- data/lib/arel_extensions/nodes/power.rb +2 -2
- data/lib/arel_extensions/nodes/rand.rb +2 -2
- data/lib/arel_extensions/nodes/repeat.rb +2 -2
- data/lib/arel_extensions/nodes/replace.rb +3 -3
- data/lib/arel_extensions/nodes/rollup.rb +4 -4
- data/lib/arel_extensions/nodes/round.rb +3 -3
- data/lib/arel_extensions/nodes/select.rb +1 -1
- data/lib/arel_extensions/nodes/std.rb +4 -4
- data/lib/arel_extensions/nodes/substring.rb +52 -2
- data/lib/arel_extensions/nodes/then.rb +1 -1
- data/lib/arel_extensions/nodes/trim.rb +2 -2
- data/lib/arel_extensions/nodes/union.rb +3 -3
- data/lib/arel_extensions/nodes/union_all.rb +3 -3
- data/lib/arel_extensions/nodes/wday.rb +2 -2
- data/lib/arel_extensions/null_functions.rb +5 -7
- data/lib/arel_extensions/predications.rb +22 -18
- data/lib/arel_extensions/string_functions.rb +26 -27
- data/lib/arel_extensions/version.rb +3 -1
- data/lib/arel_extensions/visitors/convert_format.rb +2 -2
- data/lib/arel_extensions/visitors/ibm_db.rb +5 -5
- data/lib/arel_extensions/visitors/mssql.rb +91 -92
- data/lib/arel_extensions/visitors/mysql.rb +89 -69
- data/lib/arel_extensions/visitors/oracle.rb +100 -104
- data/lib/arel_extensions/visitors/oracle12.rb +5 -6
- data/lib/arel_extensions/visitors/postgresql.rb +107 -94
- data/lib/arel_extensions/visitors/sqlite.rb +57 -65
- data/lib/arel_extensions/visitors/to_sql.rb +84 -74
- data/lib/arel_extensions/visitors.rb +6 -6
- data/lib/arel_extensions/warning.rb +5 -5
- data/lib/arel_extensions.rb +34 -27
- data/test/real_db_test.rb +31 -31
- data/test/visitors/test_to_sql.rb +2 -2
- data/test/with_ar/all_agnostic_test.rb +149 -15
- data/version_v1.rb +3 -1
- data/version_v2.rb +3 -1
- metadata +2 -30
data/gemfiles/rails7_1.gemfile
CHANGED
|
@@ -1,33 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
source 'https://rubygems.org'
|
|
2
4
|
|
|
3
5
|
gem 'rails', '~> 7.1'
|
|
4
6
|
|
|
5
7
|
group :development, :test do
|
|
6
|
-
gem 'ruby-lsp', platforms: %i[mri]
|
|
7
8
|
gem 'debug', platforms: %i[mri]
|
|
9
|
+
gem 'minitest', '~> 5.9'
|
|
10
|
+
gem 'rake', '~> 12.3.3'
|
|
11
|
+
gem 'rubocop'
|
|
12
|
+
gem 'rubocop-performance'
|
|
13
|
+
gem 'ruby-lsp', platforms: %i[mri]
|
|
8
14
|
|
|
9
|
-
gem 'activesupport', '~> 7.1'
|
|
10
15
|
gem 'activemodel', '~> 7.1'
|
|
11
16
|
gem 'activerecord', '~> 7.1'
|
|
17
|
+
gem 'activesupport', '~> 7.1'
|
|
12
18
|
|
|
13
|
-
gem 'sqlite3', '~> 1.6', platforms: [:mri]
|
|
14
19
|
gem 'mysql2', '~>0.5', platforms: [:mri]
|
|
15
20
|
gem 'pg', '~> 1.5', platforms: [:mri]
|
|
21
|
+
gem 'sqlite3', '~> 1.6', platforms: [:mri]
|
|
16
22
|
|
|
23
|
+
gem 'activerecord-sqlserver-adapter', '~> 7.1.0', platforms: %i[mri mingw x64_mingw mswin]
|
|
17
24
|
gem 'tiny_tds', platforms: %i[mri mingw x64_mingw mswin]
|
|
18
|
-
gem 'activerecord-sqlserver-adapter', '~> 7.1.0.beta1', platforms: %i[mri mingw x64_mingw mswin]
|
|
19
25
|
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw]
|
|
20
26
|
|
|
21
|
-
gem '
|
|
22
|
-
gem '
|
|
27
|
+
gem 'activerecord-oracle_enhanced-adapter', '~> 7.0.0' if ENV.key? 'ORACLE_HOME'
|
|
28
|
+
gem 'ruby-oci8', platforms: %i[mri mswin mingw] if ENV.key? 'ORACLE_HOME'
|
|
23
29
|
|
|
24
30
|
# for JRuby
|
|
25
|
-
gem 'jdbc-mssql', platforms: :jruby, require: true
|
|
26
|
-
gem 'jdbc-sqlite3', platform: :jruby
|
|
27
31
|
gem 'activerecord-jdbc-alt-adapter', '~> 71.0.0.alpha1', platform: :jruby, require: true
|
|
28
32
|
gem 'activerecord-jdbcmysql-adapter', platforms: :jruby
|
|
29
33
|
gem 'activerecord-jdbcpostgresql-adapter', platforms: :jruby
|
|
30
34
|
gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
|
|
35
|
+
gem 'jar-dependencies', '0.4.1', platforms: :jruby
|
|
36
|
+
gem 'jdbc-mssql', platforms: :jruby, require: true
|
|
37
|
+
gem 'jdbc-sqlite3', platform: :jruby
|
|
31
38
|
|
|
32
39
|
# Ruby 3.4+ removes the following gems from the standard distribution.
|
|
33
40
|
# Warnings are emitted from 3.3 .
|
data/gemfiles/rails7_2.gemfile
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
source 'https://rubygems.org'
|
|
2
4
|
|
|
3
5
|
gem 'rails', '~> 7.2'
|
|
4
6
|
|
|
5
7
|
group :development, :test do
|
|
6
|
-
gem 'ruby-lsp', platforms: %i[mri]
|
|
7
8
|
gem 'debug', platforms: %i[mri]
|
|
9
|
+
gem 'minitest', '~> 5.9'
|
|
10
|
+
gem 'rake', '~> 12.3.3'
|
|
11
|
+
gem 'rubocop'
|
|
12
|
+
gem 'rubocop-performance'
|
|
13
|
+
gem 'ruby-lsp', platforms: %i[mri]
|
|
8
14
|
|
|
9
|
-
gem 'activesupport', '~> 7.2'
|
|
10
15
|
gem 'activemodel', '~> 7.2'
|
|
11
16
|
gem 'activerecord', '~> 7.2'
|
|
17
|
+
gem 'activesupport', '~> 7.2'
|
|
12
18
|
|
|
13
|
-
gem 'sqlite3', '~> 1.6', platforms: [:mri]
|
|
14
19
|
gem 'mysql2', '~>0.5', platforms: [:mri]
|
|
15
20
|
gem 'pg', '~> 1.5', platforms: [:mri]
|
|
21
|
+
gem 'sqlite3', '~> 1.6', platforms: [:mri]
|
|
16
22
|
|
|
17
|
-
gem 'tiny_tds', platforms: %i[mri mingw x64_mingw mswin]
|
|
18
23
|
gem 'activerecord-sqlserver-adapter', '~> 7.2', platforms: %i[mri mingw x64_mingw mswin]
|
|
24
|
+
gem 'tiny_tds', platforms: %i[mri mingw x64_mingw mswin]
|
|
19
25
|
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw]
|
|
20
26
|
|
|
21
|
-
gem '
|
|
22
|
-
gem '
|
|
27
|
+
gem 'activerecord-oracle_enhanced-adapter', '~> 7.0.0' if ENV.key? 'ORACLE_HOME'
|
|
28
|
+
gem 'ruby-oci8', platforms: %i[mri mswin mingw] if ENV.key? 'ORACLE_HOME'
|
|
23
29
|
|
|
24
30
|
# for JRuby
|
|
25
31
|
gem 'jdbc-mssql', platforms: :jruby, require: true
|
data/gemfiles/rails8.gemfile
CHANGED
|
@@ -3,30 +3,30 @@ source 'https://rubygems.org'
|
|
|
3
3
|
gem 'rails', '~> 8.0'
|
|
4
4
|
|
|
5
5
|
group :development, :test do
|
|
6
|
-
gem 'ruby-lsp', platforms: %i[mri]
|
|
7
6
|
gem 'debug', platforms: %i[mri]
|
|
7
|
+
gem 'ruby-lsp', platforms: %i[mri]
|
|
8
8
|
|
|
9
|
-
gem 'activesupport', '~> 8.0'
|
|
10
9
|
gem 'activemodel', '~> 8.0'
|
|
11
10
|
gem 'activerecord', '~> 8.0'
|
|
11
|
+
gem 'activesupport', '~> 8.0'
|
|
12
12
|
|
|
13
|
-
gem 'sqlite3', '~> 2.1', platforms: [:mri]
|
|
14
13
|
gem 'mysql2', '~>0.5', platforms: [:mri]
|
|
15
14
|
gem 'pg', '~> 1.5', platforms: [:mri]
|
|
15
|
+
gem 'sqlite3', '~> 2.1', platforms: [:mri]
|
|
16
16
|
|
|
17
|
-
gem 'tiny_tds', platforms: %i[mri mingw x64_mingw mswin]
|
|
18
17
|
gem 'activerecord-sqlserver-adapter', '~> 8.0', platforms: %i[mri mingw x64_mingw mswin]
|
|
18
|
+
gem 'tiny_tds', platforms: %i[mri mingw x64_mingw mswin]
|
|
19
19
|
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw]
|
|
20
20
|
|
|
21
|
-
gem 'ruby-oci8', platforms: %i[mri mswin mingw] if ENV.has_key? 'ORACLE_HOME'
|
|
22
21
|
gem 'activerecord-oracle_enhanced-adapter', '~> 8.0' if ENV.has_key? 'ORACLE_HOME'
|
|
22
|
+
gem 'ruby-oci8', platforms: %i[mri mswin mingw] if ENV.has_key? 'ORACLE_HOME'
|
|
23
23
|
|
|
24
24
|
# for JRuby
|
|
25
|
-
gem 'jdbc-mssql', platforms: :jruby, require: true
|
|
26
|
-
gem 'jdbc-sqlite3', platform: :jruby
|
|
27
25
|
gem 'activerecord-jdbcmysql-adapter', platforms: :jruby
|
|
28
26
|
gem 'activerecord-jdbcpostgresql-adapter', platforms: :jruby
|
|
29
27
|
gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
|
|
28
|
+
gem 'jdbc-mssql', platforms: :jruby, require: true
|
|
29
|
+
gem 'jdbc-sqlite3', platform: :jruby
|
|
30
30
|
|
|
31
31
|
# Ruby 3.4+ removes the following gems from the standard distribution.
|
|
32
32
|
# Warnings are emitted from 3.3.
|
data/gemfiles/rails8_1.gemfile
CHANGED
|
@@ -3,19 +3,19 @@ source 'https://rubygems.org'
|
|
|
3
3
|
gem 'rails', '~> 8.1'
|
|
4
4
|
|
|
5
5
|
group :development, :test do
|
|
6
|
-
gem 'ruby-lsp', platforms: %i[mri]
|
|
7
6
|
gem 'debug', platforms: %i[mri]
|
|
7
|
+
gem 'ruby-lsp', platforms: %i[mri]
|
|
8
8
|
|
|
9
|
-
gem 'activesupport', '~> 8.1'
|
|
10
9
|
gem 'activemodel', '~> 8.1'
|
|
11
10
|
gem 'activerecord', '~> 8.1'
|
|
11
|
+
gem 'activesupport', '~> 8.1'
|
|
12
12
|
|
|
13
|
-
gem 'sqlite3', '~> 2.1', platforms: [:mri]
|
|
14
13
|
gem 'mysql2', '~>0.5', platforms: [:mri]
|
|
15
14
|
gem 'pg', '~> 1.5', platforms: [:mri]
|
|
15
|
+
gem 'sqlite3', '~> 2.1', platforms: [:mri]
|
|
16
16
|
|
|
17
|
-
gem 'tiny_tds', platforms: %i[mri mingw x64_mingw mswin]
|
|
18
17
|
gem 'activerecord-sqlserver-adapter', '~> 8.1', platforms: %i[mri mingw x64_mingw mswin]
|
|
18
|
+
gem 'tiny_tds', platforms: %i[mri mingw x64_mingw mswin]
|
|
19
19
|
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw]
|
|
20
20
|
|
|
21
21
|
gem 'ruby-oci8', platforms: %i[mri mswin mingw] if ENV.has_key? 'ORACLE_HOME'
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
|
2
4
|
require 'arel_extensions/version'
|
|
3
5
|
|
|
4
6
|
Gem::Specification.new do |s|
|
|
@@ -17,11 +19,7 @@ Gem::Specification.new do |s|
|
|
|
17
19
|
|
|
18
20
|
# Manifest
|
|
19
21
|
s.files = `git ls-files`.split("\n")
|
|
20
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
21
22
|
s.require_paths = ['lib']
|
|
22
23
|
|
|
23
24
|
s.add_dependency('arel', '>= 6.0')
|
|
24
|
-
|
|
25
|
-
s.add_development_dependency('minitest', '~> 5.9')
|
|
26
|
-
s.add_development_dependency('rake', '~> 12.3.3')
|
|
27
25
|
end
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
|
2
4
|
require 'arel_extensions/version'
|
|
3
5
|
|
|
4
6
|
Gem::Specification.new do |s|
|
|
@@ -17,11 +19,7 @@ Gem::Specification.new do |s|
|
|
|
17
19
|
|
|
18
20
|
# Manifest
|
|
19
21
|
s.files = `git ls-files`.split("\n")
|
|
20
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
21
22
|
s.require_paths = ['lib']
|
|
22
23
|
|
|
23
24
|
s.add_dependency('activerecord', '>= 6.0')
|
|
24
|
-
|
|
25
|
-
s.add_development_dependency('minitest', '~> 5.9')
|
|
26
|
-
s.add_development_dependency('rake', '~> 12.3.3')
|
|
27
25
|
end
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module ArelExtensions
|
|
2
4
|
module Aliases
|
|
3
|
-
|
|
4
5
|
# Install an alias, if present.
|
|
5
|
-
def xas
|
|
6
|
+
def xas(other)
|
|
6
7
|
if other.present?
|
|
7
8
|
Arel::Nodes::As.new(self, Arel.sql(other))
|
|
8
9
|
else
|
|
9
10
|
self
|
|
10
11
|
end
|
|
11
12
|
end
|
|
12
|
-
|
|
13
13
|
end
|
|
14
14
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'arel_extensions/nodes/then'
|
|
2
4
|
|
|
3
5
|
module ArelExtensions
|
|
@@ -6,7 +8,7 @@ module ArelExtensions
|
|
|
6
8
|
self.and(other)
|
|
7
9
|
end
|
|
8
10
|
|
|
9
|
-
def and
|
|
11
|
+
def and(*others)
|
|
10
12
|
Arel::Nodes::And.new self, others
|
|
11
13
|
end
|
|
12
14
|
|
|
@@ -14,7 +16,7 @@ module ArelExtensions
|
|
|
14
16
|
self.or(other)
|
|
15
17
|
end
|
|
16
18
|
|
|
17
|
-
def or
|
|
19
|
+
def or(*others)
|
|
18
20
|
Arel::Nodes::Or.new self, others
|
|
19
21
|
end
|
|
20
22
|
|
|
@@ -27,7 +29,7 @@ end
|
|
|
27
29
|
class Arel::Nodes::And
|
|
28
30
|
include ArelExtensions::BooleanFunctions
|
|
29
31
|
|
|
30
|
-
def self.new
|
|
32
|
+
def self.new(*children)
|
|
31
33
|
children =
|
|
32
34
|
children.flatten.map { |c|
|
|
33
35
|
c.is_a?(self) ? c.children : c
|
|
@@ -47,15 +49,15 @@ class Arel::Nodes::Or
|
|
|
47
49
|
|
|
48
50
|
attr_reader :children
|
|
49
51
|
|
|
50
|
-
def self.new
|
|
52
|
+
def self.new(*children)
|
|
51
53
|
children =
|
|
52
54
|
children.flatten.map { |c|
|
|
53
55
|
c.is_a?(self) ? c.children : c
|
|
54
56
|
}.flatten
|
|
55
|
-
super
|
|
57
|
+
super
|
|
56
58
|
end
|
|
57
59
|
|
|
58
|
-
def initialize
|
|
60
|
+
def initialize(*children)
|
|
59
61
|
@children = children
|
|
60
62
|
end
|
|
61
63
|
|
|
@@ -80,5 +82,5 @@ class Arel::Nodes::Or
|
|
|
80
82
|
self.class == other.class &&
|
|
81
83
|
children == other.children
|
|
82
84
|
end
|
|
83
|
-
alias
|
|
85
|
+
alias == eql?
|
|
84
86
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module ArelExtensions
|
|
2
4
|
class CommonSqlFunctions
|
|
3
5
|
def initialize(cnx)
|
|
@@ -9,7 +11,7 @@ module ArelExtensions
|
|
|
9
11
|
db.load_extension('/usr/lib/sqlite3/pcre.so')
|
|
10
12
|
db.load_extension('/usr/lib/sqlite3/extension-functions.so')
|
|
11
13
|
db.enable_load_extension(0)
|
|
12
|
-
rescue => e
|
|
14
|
+
rescue StandardError => e
|
|
13
15
|
$load_extension_disabled = true
|
|
14
16
|
puts "cannot load extensions #{e.inspect}"
|
|
15
17
|
end
|
|
@@ -42,14 +44,14 @@ module ArelExtensions
|
|
|
42
44
|
if /sqlite/i.match?(env_db)
|
|
43
45
|
begin
|
|
44
46
|
add_sqlite_functions
|
|
45
|
-
rescue => e
|
|
47
|
+
rescue StandardError => e
|
|
46
48
|
puts "cannot add sqlite functions #{e.inspect}"
|
|
47
49
|
end
|
|
48
50
|
end
|
|
49
51
|
if File.exist?("init/#{env_db}.sql")
|
|
50
52
|
sql = File.read("init/#{env_db}.sql")
|
|
51
53
|
if env_db == 'mssql'
|
|
52
|
-
sql.split(/^GO\s*$/).each {|str|
|
|
54
|
+
sql.split(/^GO\s*$/).each { |str|
|
|
53
55
|
@cnx.execute(str.strip) unless str.blank?
|
|
54
56
|
}
|
|
55
57
|
elsif env_db == 'mysql'
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module ArelExtensions
|
|
2
4
|
module Comparators
|
|
3
5
|
def >(other)
|
|
@@ -19,8 +21,8 @@ module ArelExtensions
|
|
|
19
21
|
# REGEXP function
|
|
20
22
|
# Pattern matching using regular expressions
|
|
21
23
|
def =~(other)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
# arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
|
|
25
|
+
# if arg == :string || arg == :text
|
|
24
26
|
Arel::Nodes::Regexp.new self, convert_regexp(other)
|
|
25
27
|
# end
|
|
26
28
|
end
|
|
@@ -30,8 +32,8 @@ module ArelExtensions
|
|
|
30
32
|
# NOT_REGEXP function
|
|
31
33
|
# Negation of Regexp
|
|
32
34
|
def !~(other)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
# arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
|
|
36
|
+
# if arg == :string || arg == :text
|
|
35
37
|
Arel::Nodes::NotRegexp.new self, convert_regexp(other)
|
|
36
38
|
# end
|
|
37
39
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module ArelExtensions
|
|
2
4
|
#
|
|
3
5
|
# column_of
|
|
@@ -18,7 +20,7 @@ module ArelExtensions
|
|
|
18
20
|
Arel::Table.engine.connection.schema_cache.columns_hash(table_name)[column_name]
|
|
19
21
|
rescue NoMethodError
|
|
20
22
|
nil
|
|
21
|
-
rescue => e
|
|
23
|
+
rescue StandardError => e
|
|
22
24
|
warn("Warning: Unexpected exception caught while fetching column name for #{table_name}.#{column_name} in `column_of_via_arel_table`\n#{e.class}")
|
|
23
25
|
warn(e.backtrace)
|
|
24
26
|
nil
|
|
@@ -26,33 +28,31 @@ module ArelExtensions
|
|
|
26
28
|
|
|
27
29
|
def self.column_of(table_name, column_name)
|
|
28
30
|
pool = ActiveRecord::Base.connection.pool
|
|
29
|
-
use_arel_table = !ActiveRecord::Base.connected?
|
|
30
|
-
(pool.respond_to?(:schema_cache) && pool.schema_cache.nil?)
|
|
31
|
+
use_arel_table = !ActiveRecord::Base.connected? \
|
|
32
|
+
|| (pool.respond_to?(:schema_cache) && pool.schema_cache.nil?)
|
|
31
33
|
|
|
32
34
|
if use_arel_table
|
|
33
35
|
column_of_via_arel_table(table_name, column_name)
|
|
34
|
-
|
|
35
|
-
if pool.respond_to?(:
|
|
36
|
-
if
|
|
37
|
-
|
|
38
|
-
pool.pool_config.schema_reflection.columns_hash(pool, table_name)[column_name]
|
|
39
|
-
else
|
|
40
|
-
pool.pool_config.schema_reflection.columns_hash(ActiveRecord::Base.connection, table_name)[column_name]
|
|
41
|
-
end
|
|
36
|
+
elsif pool.respond_to?(:pool_config)
|
|
37
|
+
if pool.pool_config.respond_to?(:schema_reflection) # activerecord >= 7.1
|
|
38
|
+
if ActiveRecord.version >= Gem::Version.create('7.2')
|
|
39
|
+
pool.pool_config.schema_reflection.columns_hash(pool, table_name)[column_name]
|
|
42
40
|
else
|
|
43
|
-
pool.pool_config.
|
|
41
|
+
pool.pool_config.schema_reflection.columns_hash(ActiveRecord::Base.connection, table_name)[column_name]
|
|
44
42
|
end
|
|
45
|
-
|
|
46
|
-
pool.schema_cache.columns_hash(table_name)[column_name]
|
|
47
|
-
else # activerecord < 5.0
|
|
48
|
-
column_of_via_arel_table(table_name, column_name)
|
|
43
|
+
else # activerecord < 7.1
|
|
44
|
+
pool.pool_config.schema_cache.columns_hash(table_name)[column_name]
|
|
49
45
|
end
|
|
46
|
+
elsif pool.respond_to?(:schema_cache) # activerecord < 6.1
|
|
47
|
+
pool.schema_cache.columns_hash(table_name)[column_name]
|
|
48
|
+
else # activerecord < 5.0
|
|
49
|
+
column_of_via_arel_table(table_name, column_name)
|
|
50
50
|
end
|
|
51
51
|
rescue ActiveRecord::ConnectionNotEstablished
|
|
52
52
|
column_of_via_arel_table(table_name, column_name)
|
|
53
53
|
rescue ActiveRecord::StatementInvalid
|
|
54
54
|
nil
|
|
55
|
-
rescue => e
|
|
55
|
+
rescue StandardError => e
|
|
56
56
|
warn("Warning: Unexpected exception caught while fetching column name for #{table_name}.#{column_name} in `column_of`")
|
|
57
57
|
warn(e)
|
|
58
58
|
warn(e.backtrace)
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'arel'
|
|
2
4
|
|
|
3
5
|
module ArelExtensions
|
|
4
6
|
module InsertManager
|
|
5
7
|
def bulk_insert(cols, data)
|
|
6
8
|
raise ArgumentError, 'cols must be present' if cols.blank?
|
|
9
|
+
|
|
7
10
|
columns =
|
|
8
11
|
case cols.first
|
|
9
12
|
when Array
|
|
@@ -11,7 +14,7 @@ module ArelExtensions
|
|
|
11
14
|
when Arel::Attributes::Attribute
|
|
12
15
|
cols
|
|
13
16
|
when String, Symbol
|
|
14
|
-
cols.map {|c| [@ast.relation[c.first]] }
|
|
17
|
+
cols.map { |c| [@ast.relation[c.first]] }
|
|
15
18
|
else
|
|
16
19
|
raise ArgumentError, "cols has an invalid type: #{cols.first.first.class}"
|
|
17
20
|
end
|
|
@@ -28,6 +31,7 @@ module ArelExtensions
|
|
|
28
31
|
attr_accessor :left, :cols
|
|
29
32
|
|
|
30
33
|
def initialize(cols, values)
|
|
34
|
+
super()
|
|
31
35
|
@left = values
|
|
32
36
|
@cols = cols
|
|
33
37
|
end
|
data/lib/arel_extensions/math.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'arel_extensions/nodes'
|
|
2
4
|
require 'arel_extensions/nodes/function'
|
|
3
5
|
require 'arel_extensions/nodes/concat'
|
|
@@ -17,48 +19,46 @@ module ArelExtensions
|
|
|
17
19
|
def +(other)
|
|
18
20
|
case self
|
|
19
21
|
when Arel::Nodes::Quoted
|
|
20
|
-
|
|
22
|
+
concat(other)
|
|
21
23
|
when Arel::Nodes::Grouping
|
|
22
|
-
if
|
|
23
|
-
|
|
24
|
+
if expr.left.is_a?(String) || expr.right.is_a?(String)
|
|
25
|
+
concat(other)
|
|
24
26
|
else
|
|
25
|
-
Arel.grouping(Arel::Nodes::Addition.new
|
|
27
|
+
Arel.grouping(Arel::Nodes::Addition.new(self, other))
|
|
26
28
|
end
|
|
27
29
|
when ArelExtensions::Nodes::Function, ArelExtensions::Nodes::Case
|
|
28
|
-
case
|
|
29
|
-
when :string, :text
|
|
30
|
-
self.concat(other)
|
|
30
|
+
case return_type
|
|
31
31
|
when :integer, :decimal, :float, :number, :int
|
|
32
|
-
Arel.grouping(Arel::Nodes::Addition.new
|
|
32
|
+
Arel.grouping(Arel::Nodes::Addition.new(self, other))
|
|
33
33
|
when :date, :datetime
|
|
34
34
|
ArelExtensions::Nodes::DateAdd.new [self, other]
|
|
35
35
|
else
|
|
36
|
-
|
|
36
|
+
concat(other)
|
|
37
37
|
end
|
|
38
38
|
when Arel::Nodes::Function
|
|
39
|
-
Arel.grouping(Arel::Nodes::Addition.new
|
|
39
|
+
Arel.grouping(Arel::Nodes::Addition.new(self, other))
|
|
40
40
|
else
|
|
41
41
|
col =
|
|
42
|
-
if
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
Arel.column_of(
|
|
42
|
+
if is_a?(Arel::Attribute) && respond_to?(:type_caster) && able_to_type_cast?
|
|
43
|
+
type_caster
|
|
44
|
+
elsif respond_to?(:relation)
|
|
45
|
+
Arel.column_of(relation.table_name, name.to_s)
|
|
46
46
|
end
|
|
47
|
-
if
|
|
48
|
-
Arel.grouping(Arel::Nodes::Addition.new(self, Arel.quoted(other)))
|
|
49
|
-
else
|
|
47
|
+
if col
|
|
50
48
|
arg = col.type
|
|
51
49
|
if arg == :integer || !arg
|
|
52
50
|
other = other.to_i if other.is_a?(String)
|
|
53
|
-
Arel.grouping(Arel::Nodes::Addition.new
|
|
54
|
-
elsif
|
|
51
|
+
Arel.grouping(Arel::Nodes::Addition.new(self, Arel.quoted(other)))
|
|
52
|
+
elsif %i[decimal float].include?(arg)
|
|
55
53
|
other = Arel.sql(other) if other.is_a?(String) # Arel should accept Float & BigDecimal!
|
|
56
|
-
Arel.grouping(Arel::Nodes::Addition.new
|
|
57
|
-
elsif
|
|
54
|
+
Arel.grouping(Arel::Nodes::Addition.new(self, Arel.quoted(other)))
|
|
55
|
+
elsif %i[datetime date].include?(arg)
|
|
58
56
|
ArelExtensions::Nodes::DateAdd.new [self, other]
|
|
59
|
-
elsif
|
|
60
|
-
|
|
57
|
+
elsif %i[string text].include?(arg)
|
|
58
|
+
concat(other)
|
|
61
59
|
end
|
|
60
|
+
else
|
|
61
|
+
Arel.grouping(Arel::Nodes::Addition.new(self, Arel.quoted(other)))
|
|
62
62
|
end
|
|
63
63
|
end
|
|
64
64
|
end
|
|
@@ -68,17 +68,13 @@ module ArelExtensions
|
|
|
68
68
|
def -(other)
|
|
69
69
|
case self
|
|
70
70
|
when Arel::Nodes::Grouping
|
|
71
|
-
if
|
|
72
|
-
Arel.grouping(ArelExtensions::Nodes::DateSub.new
|
|
71
|
+
if expr.left.is_a?(Date) || expr.left.is_a?(DateTime)
|
|
72
|
+
Arel.grouping(ArelExtensions::Nodes::DateSub.new([self, Arel.quoted(other)]))
|
|
73
73
|
else
|
|
74
74
|
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
75
75
|
end
|
|
76
76
|
when ArelExtensions::Nodes::Function, ArelExtensions::Nodes::Case
|
|
77
|
-
case
|
|
78
|
-
when :string, :text # ???
|
|
79
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other))) # ??
|
|
80
|
-
when :integer, :decimal, :float, :number
|
|
81
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
77
|
+
case return_type
|
|
82
78
|
when :date, :datetime
|
|
83
79
|
ArelExtensions::Nodes::DateSub.new [self, Arel.quoted(other)]
|
|
84
80
|
else
|
|
@@ -88,33 +84,31 @@ module ArelExtensions
|
|
|
88
84
|
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
89
85
|
else
|
|
90
86
|
col =
|
|
91
|
-
if
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
Arel.column_of(
|
|
87
|
+
if is_a?(Arel::Attribute) && respond_to?(:type_caster) && able_to_type_cast?
|
|
88
|
+
type_caster
|
|
89
|
+
elsif respond_to?(:relation)
|
|
90
|
+
Arel.column_of(relation.table_name, name.to_s)
|
|
95
91
|
end
|
|
96
|
-
if
|
|
97
|
-
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
98
|
-
else
|
|
92
|
+
if col
|
|
99
93
|
arg = col.type
|
|
100
|
-
if
|
|
94
|
+
if %i[date datetime].include?(arg)
|
|
101
95
|
case other
|
|
102
96
|
when Arel::Attributes::Attribute
|
|
103
97
|
col2 =
|
|
104
98
|
if other.is_a?(Arel::Attribute) && other.respond_to?(:type_caster) && other.able_to_type_cast?
|
|
105
99
|
other.type_caster
|
|
106
|
-
|
|
107
|
-
Arel.column_of(other.relation.table_name, other.name.to_s)
|
|
100
|
+
elsif other.respond_to?(:relation)
|
|
101
|
+
Arel.column_of(other.relation.table_name, other.name.to_s)
|
|
108
102
|
end
|
|
109
|
-
if
|
|
110
|
-
ArelExtensions::Nodes::DateSub.new [self, other]
|
|
111
|
-
else
|
|
103
|
+
if col2
|
|
112
104
|
arg2 = col2.type
|
|
113
|
-
if
|
|
105
|
+
if %i[date datetime].include?(arg2)
|
|
114
106
|
ArelExtensions::Nodes::DateDiff.new [self, other]
|
|
115
107
|
else
|
|
116
108
|
ArelExtensions::Nodes::DateSub.new [self, other]
|
|
117
109
|
end
|
|
110
|
+
else
|
|
111
|
+
ArelExtensions::Nodes::DateSub.new [self, other]
|
|
118
112
|
end
|
|
119
113
|
when Arel::Nodes::Node, DateTime, Time, String, Date
|
|
120
114
|
ArelExtensions::Nodes::DateDiff.new [self, other]
|
|
@@ -133,6 +127,8 @@ module ArelExtensions
|
|
|
133
127
|
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
134
128
|
end
|
|
135
129
|
end
|
|
130
|
+
else
|
|
131
|
+
Arel.grouping(Arel::Nodes::Subtraction.new(self, Arel.quoted(other)))
|
|
136
132
|
end
|
|
137
133
|
end
|
|
138
134
|
end
|