arel_extensions 1.2.23 → 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.rb +19 -69
  19. data/lib/arel_extensions/attributes.rb +1 -0
  20. data/lib/arel_extensions/boolean_functions.rb +14 -55
  21. data/lib/arel_extensions/common_sql_functions.rb +8 -7
  22. data/lib/arel_extensions/comparators.rb +15 -14
  23. data/lib/arel_extensions/date_duration.rb +5 -4
  24. data/lib/arel_extensions/insert_manager.rb +16 -17
  25. data/lib/arel_extensions/math.rb +12 -11
  26. data/lib/arel_extensions/math_functions.rb +22 -29
  27. data/lib/arel_extensions/nodes.rb +1 -1
  28. data/lib/arel_extensions/nodes/abs.rb +1 -0
  29. data/lib/arel_extensions/nodes/blank.rb +1 -0
  30. data/lib/arel_extensions/nodes/case.rb +8 -11
  31. data/lib/arel_extensions/nodes/cast.rb +2 -4
  32. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  33. data/lib/arel_extensions/nodes/change_case.rb +0 -0
  34. data/lib/arel_extensions/nodes/coalesce.rb +3 -2
  35. data/lib/arel_extensions/nodes/collate.rb +2 -1
  36. data/lib/arel_extensions/nodes/concat.rb +16 -7
  37. data/lib/arel_extensions/nodes/date_diff.rb +13 -10
  38. data/lib/arel_extensions/nodes/duration.rb +3 -0
  39. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  40. data/lib/arel_extensions/nodes/floor.rb +1 -1
  41. data/lib/arel_extensions/nodes/format.rb +8 -34
  42. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  43. data/lib/arel_extensions/nodes/function.rb +16 -25
  44. data/lib/arel_extensions/nodes/json.rb +36 -43
  45. data/lib/arel_extensions/nodes/length.rb +1 -0
  46. data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
  47. data/lib/arel_extensions/nodes/locate.rb +1 -0
  48. data/lib/arel_extensions/nodes/log10.rb +2 -1
  49. data/lib/arel_extensions/nodes/matches.rb +6 -4
  50. data/lib/arel_extensions/nodes/md5.rb +1 -0
  51. data/lib/arel_extensions/nodes/power.rb +5 -5
  52. data/lib/arel_extensions/nodes/rand.rb +1 -0
  53. data/lib/arel_extensions/nodes/repeat.rb +4 -2
  54. data/lib/arel_extensions/nodes/replace.rb +6 -22
  55. data/lib/arel_extensions/nodes/round.rb +6 -5
  56. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  57. data/lib/arel_extensions/nodes/std.rb +21 -18
  58. data/lib/arel_extensions/nodes/substring.rb +16 -8
  59. data/lib/arel_extensions/nodes/then.rb +0 -0
  60. data/lib/arel_extensions/nodes/trim.rb +5 -3
  61. data/lib/arel_extensions/nodes/union.rb +5 -2
  62. data/lib/arel_extensions/nodes/union_all.rb +3 -0
  63. data/lib/arel_extensions/nodes/wday.rb +4 -0
  64. data/lib/arel_extensions/null_functions.rb +7 -5
  65. data/lib/arel_extensions/predications.rb +34 -35
  66. data/lib/arel_extensions/railtie.rb +5 -5
  67. data/lib/arel_extensions/set_functions.rb +4 -2
  68. data/lib/arel_extensions/string_functions.rb +22 -43
  69. data/lib/arel_extensions/tasks.rb +5 -5
  70. data/lib/arel_extensions/version.rb +1 -1
  71. data/lib/arel_extensions/visitors.rb +60 -68
  72. data/lib/arel_extensions/visitors/ibm_db.rb +12 -5
  73. data/lib/arel_extensions/visitors/mssql.rb +57 -63
  74. data/lib/arel_extensions/visitors/mysql.rb +98 -149
  75. data/lib/arel_extensions/visitors/oracle.rb +68 -71
  76. data/lib/arel_extensions/visitors/oracle12.rb +15 -2
  77. data/lib/arel_extensions/visitors/postgresql.rb +63 -116
  78. data/lib/arel_extensions/visitors/sqlite.rb +70 -83
  79. data/lib/arel_extensions/visitors/to_sql.rb +109 -141
  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
data/appveyor.yml CHANGED
@@ -41,4 +41,4 @@ environment:
41
41
  - RUBY_VERSION: 22
42
42
  - RUBY_VERSION: 23
43
43
  - RUBY_VERSION: 23-x64
44
-
44
+
@@ -20,9 +20,9 @@ Gem::Specification.new do |s|
20
20
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
21
  s.require_paths = ["lib"]
22
22
 
23
- s.add_dependency('arel', '>= 6.0')
23
+ s.add_dependency('activerecord', '>= 6.0.0.rc2')
24
24
 
25
25
  s.add_development_dependency('minitest', '~> 5.9')
26
26
  s.add_development_dependency('rdoc', '~> 4.0')
27
- s.add_development_dependency('rake', '~> 12.3.3')
28
- end
27
+ s.add_development_dependency('rake', '~> 11')
28
+ end
data/functions.html CHANGED
@@ -55,7 +55,7 @@
55
55
  <td class="tg-yw4l">CEIL<br>column.ceil</td>
56
56
  <td class="ok">✔</td>
57
57
  <td class="ok">✔</td>
58
- <td class="tg-j6lv">CASE + CAST</td>
58
+ <td class="tg-j6lv">CASE + ROUND</td>
59
59
  <td class="ok">✔</td>
60
60
  <td class="tg-j6lv">CEILING()</td>
61
61
  <td class="tg-j6lv">CEILING()</td>
@@ -64,7 +64,7 @@
64
64
  <td class="tg-yw4l">FLOOR<br>column.floor</td>
65
65
  <td class="ok">✔</td>
66
66
  <td class="ok">✔</td>
67
- <td class="tg-j6lv">CASE + CAST</td>
67
+ <td class="tg-j6lv">CASE + ROUND</td>
68
68
  <td class="ok">✔</td>
69
69
  <td class="ok">✔</td>
70
70
  <td class="ok">✔</td>
@@ -379,7 +379,7 @@
379
379
  <td class="ok">✔</td>
380
380
  <td class="ok">✔</td>
381
381
  <td class="ok">✔</td>
382
- </tr>
382
+ </tr>
383
383
  <tr>
384
384
  <th class="bulk_insert" rowspan="1"><div>Set<br/> Operators</div></th>
385
385
  <td class="tg-yw4l">UNION ( + )<br/>query + query</td>
@@ -3,18 +3,18 @@ source "https://rubygems.org"
3
3
  gem 'arel', '~> 3.0' # too old...
4
4
 
5
5
  group :development, :test do
6
- gem "sqlite3", '<= 1.3.13', platform: [:ruby, :mswin, :mingw]
7
- gem "mysql2", '0.4.10', platform: [:ruby, :mswin, :mingw]
8
- gem "activerecord-mysql2-adapter", platform: [:ruby, :mswin, :mingw]
9
- gem "pg", platform: [:ruby, :mswin, :mingw]
6
+ gem "sqlite3", '<= 1.3.13', :platform => [:ruby, :mswin, :mingw]
7
+ gem "mysql2", '0.4.10', :platform => [:ruby, :mswin, :mingw]
8
+ gem "activerecord-mysql2-adapter", :platform => [:ruby, :mswin, :mingw]
9
+ gem "pg", :platform => [:ruby, :mswin, :mingw]
10
10
  # for JRuby
11
- gem "jdbc-sqlite3", platform: :jruby
12
- gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
13
- gem "activerecord-jdbcmysql-adapter", platform: :jruby
14
- gem "activerecord-jdbcpostgresql-adapter", platform: :jruby
11
+ gem "jdbc-sqlite3", :platform => :jruby
12
+ gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
13
+ gem "activerecord-jdbcmysql-adapter", :platform => :jruby
14
+ gem "activerecord-jdbcpostgresql-adapter", :platform => :jruby
15
15
  gem 'activesupport', '~> 3.2'
16
16
  gem 'activemodel', '~> 3.2'
17
17
  gem 'activerecord', '~> 3.2'
18
18
  end
19
19
 
20
- gemspec path: "../"
20
+ gemspec :path => "../"
@@ -7,23 +7,23 @@ group :development, :test do
7
7
  gem 'activemodel', '~> 4.0'
8
8
  gem 'activerecord', '~> 4.0'
9
9
 
10
- gem "sqlite3", '<= 1.3.13', platforms: [:mri, :mswin, :mingw]
11
- gem "mysql2", '0.4.10', platforms: [:mri, :mswin, :mingw]
12
- gem "pg",'< 1.0.0', platforms: [:mri, :mingw]
10
+ gem "sqlite3", '<= 1.3.13', :platforms => [:mri, :mswin, :mingw]
11
+ gem "mysql2", '0.4.10', :platforms => [:mri, :mswin, :mingw]
12
+ gem "pg",'< 1.0.0', :platforms => [:mri, :mingw]
13
13
 
14
- gem "tiny_tds", platforms: [:mri, :mingw, :mswin] if RUBY_PLATFORM =~ /windows/
15
- gem "activerecord-sqlserver-adapter", '~> 4.2.0', platforms: [:mri, :mingw, :mswin] if RUBY_PLATFORM =~ /windows/
14
+ gem "tiny_tds", :platforms => [:mri, :mingw, :mswin] if RUBY_PLATFORM =~ /windows/
15
+ gem "activerecord-sqlserver-adapter", '~> 4.2.0', :platforms => [:mri, :mingw, :mswin] if RUBY_PLATFORM =~ /windows/
16
16
 
17
- gem 'ruby-oci8', platforms: [:mri, :mswin, :mingw] if ENV.has_key? 'ORACLE_HOME'
17
+ gem 'ruby-oci8', :platforms => [:mri, :mswin, :mingw] if ENV.has_key? 'ORACLE_HOME'
18
18
  gem 'activerecord-oracle_enhanced-adapter', '~> 1.6.0' if ENV.has_key? 'ORACLE_HOME'
19
19
 
20
20
  # for JRuby
21
- gem 'activerecord-jdbc-adapter', '~> 1.3',platforms: :jruby
22
- gem "jdbc-sqlite3", platforms: :jruby
23
- gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby
24
- gem "activerecord-jdbcmysql-adapter", platforms: :jruby
25
- gem "activerecord-jdbcpostgresql-adapter", platforms: :jruby
26
- gem "activerecord-jdbcmssql-adapter", platforms: :jruby
21
+ gem 'activerecord-jdbc-adapter', :platforms => :jruby
22
+ gem "jdbc-sqlite3", :platforms => :jruby
23
+ gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
24
+ gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
25
+ gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
26
+ gem "activerecord-jdbcmssql-adapter", :platforms => :jruby
27
27
  end
28
28
 
29
- gemspec path: "../"
29
+ gemspec :path => "../"
@@ -7,23 +7,23 @@ group :development, :test do
7
7
  gem 'activemodel', '~> 5.0'
8
8
  gem 'activerecord', '5.0.6'
9
9
 
10
- gem "sqlite3", '<= 1.3.13', platforms: [:mri, :mswin, :mingw]
11
- gem "mysql2", '0.4.10', platforms: [:mri, :mswin, :mingw]
12
- gem "pg",'< 1.0.0', platforms: [:mri, :mingw]
10
+ gem "sqlite3", '<= 1.3.13', :platforms => [:mri, :mswin, :mingw]
11
+ gem "mysql2", '0.4.10', :platforms => [:mri, :mswin, :mingw]
12
+ gem "pg",'< 1.0.0', :platforms => [:mri, :mingw]
13
13
 
14
- gem "tiny_tds", platforms: [:mri, :mingw] if RUBY_PLATFORM =~ /windows/
15
- # gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
14
+ gem "tiny_tds", :platforms => [:mri, :mingw] if RUBY_PLATFORM =~ /windows/
15
+ #gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
16
16
 
17
- gem 'ruby-oci8', platforms: [:mri, :mswin, :mingw] if ENV.has_key? 'ORACLE_HOME'
17
+ gem 'ruby-oci8', :platforms => [:mri, :mswin, :mingw] if ENV.has_key? 'ORACLE_HOME'
18
18
  gem 'activerecord-oracle_enhanced-adapter', '~> 1.7' if ENV.has_key? 'ORACLE_HOME'
19
19
 
20
20
  # for JRuby
21
- gem 'activerecord-jdbc-adapter', github: 'jruby/activerecord-jdbc-adapter', tag: 'v50.0', platforms: :jruby
22
- gem "jdbc-sqlite3", platforms: :jruby
23
- gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby
24
- gem "activerecord-jdbcmysql-adapter", platforms: :jruby
25
- gem "activerecord-jdbcpostgresql-adapter", platforms: :jruby
26
- gem "activerecord-jdbcmssql-adapter", platforms: :jruby
21
+ gem 'activerecord-jdbc-adapter', :github => 'jruby/activerecord-jdbc-adapter', :tag => 'v50.0', :platforms => :jruby
22
+ gem "jdbc-sqlite3", :platforms => :jruby
23
+ gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
24
+ gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
25
+ gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
26
+ gem "activerecord-jdbcmssql-adapter", :platforms => :jruby
27
27
  end
28
28
 
29
- gemspec path: "../"
29
+ gemspec :path => "../"
@@ -8,23 +8,23 @@ group :development, :test do
8
8
  gem 'activemodel', '5.1.4'
9
9
  gem 'activerecord', '5.1.4'
10
10
 
11
- gem "sqlite3", '<= 1.3.13', platforms: [:mri, :mswin, :mingw]
12
- gem "mysql2", '0.4.10', platforms: [:mri, :mswin, :mingw]
13
- gem "pg",'< 1.0.0', platforms: [:mri, :mingw]
11
+ gem "sqlite3", '<= 1.3.13', :platforms => [:mri, :mswin, :mingw]
12
+ gem "mysql2", '0.4.10', :platforms => [:mri, :mswin, :mingw]
13
+ gem "pg",'< 1.0.0', :platforms => [:mri, :mingw]
14
14
 
15
- gem "tiny_tds", platforms: [:mri, :mingw] if RUBY_PLATFORM =~ /windows/
16
- # gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
15
+ gem "tiny_tds", :platforms => [:mri, :mingw] if RUBY_PLATFORM =~ /windows/
16
+ #gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
17
17
 
18
- gem 'ruby-oci8', platforms: [:mri, :mswin, :mingw] if ENV.has_key? 'ORACLE_HOME'
18
+ gem 'ruby-oci8', :platforms => [:mri, :mswin, :mingw] if ENV.has_key? 'ORACLE_HOME'
19
19
  gem 'activerecord-oracle_enhanced-adapter', '~> 1.8' if ENV.has_key? 'ORACLE_HOME'
20
20
 
21
21
  # for JRuby
22
- gem 'activerecord-jdbc-adapter', github: 'jruby/activerecord-jdbc-adapter', tag: 'v51.0', platforms: :jruby
23
- gem "jdbc-sqlite3", platforms: :jruby
24
- gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby
25
- gem "activerecord-jdbcmysql-adapter", platforms: :jruby
26
- gem "activerecord-jdbcpostgresql-adapter", platforms: :jruby
27
- gem "activerecord-jdbcmssql-adapter", platforms: :jruby
22
+ gem 'activerecord-jdbc-adapter', :github => 'jruby/activerecord-jdbc-adapter', :tag => 'v51.0', :platforms => :jruby
23
+ gem "jdbc-sqlite3", :platforms => :jruby
24
+ gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
25
+ gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
26
+ gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
27
+ gem "activerecord-jdbcmssql-adapter", :platforms => :jruby
28
28
  end
29
29
 
30
- gemspec path: "../"
30
+ gemspec :path => "../"
@@ -8,23 +8,23 @@ group :development, :test do
8
8
  gem 'activemodel', '~> 5.2.0'
9
9
  gem 'activerecord', '~> 5.2.0'
10
10
 
11
- gem "sqlite3", '<= 1.3.13', platforms: [:mri, :mswin, :mingw]
12
- gem "mysql2", '0.4.10', platforms: [:mri, :mswin, :mingw]
13
- gem "pg",'< 1.0.0', platforms: [:mri, :mingw]
11
+ gem "sqlite3", '<= 1.3.13', :platforms => [:mri, :mswin, :mingw]
12
+ gem "mysql2", '0.4.10', :platforms => [:mri, :mswin, :mingw]
13
+ gem "pg",'< 1.0.0', :platforms => [:mri, :mingw]
14
14
 
15
- gem "tiny_tds", platforms: [:mri, :mingw] if RUBY_PLATFORM =~ /windows/
16
- # gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
15
+ gem "tiny_tds", :platforms => [:mri, :mingw] if RUBY_PLATFORM =~ /windows/
16
+ #gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
17
17
 
18
- gem 'ruby-oci8', platforms: [:mri, :mswin, :mingw] if ENV.has_key? 'ORACLE_HOME'
18
+ gem 'ruby-oci8', :platforms => [:mri, :mswin, :mingw] if ENV.has_key? 'ORACLE_HOME'
19
19
  gem 'activerecord-oracle_enhanced-adapter', '~> 5.2.0' if ENV.has_key? 'ORACLE_HOME'
20
20
 
21
21
  # for JRuby
22
- gem 'activerecord-jdbc-adapter', github: 'jruby/activerecord-jdbc-adapter', tag: 'v52.0', platforms: :jruby
23
- gem "jdbc-sqlite3", platforms: :jruby
24
- gem "activerecord-jdbcsqlite3-adapter", platforms: :jruby
25
- gem "activerecord-jdbcmysql-adapter", platforms: :jruby
26
- gem "activerecord-jdbcpostgresql-adapter", platforms: :jruby
27
- gem "activerecord-jdbcmssql-adapter", platforms: :jruby
22
+ gem 'activerecord-jdbc-adapter', :github => 'jruby/activerecord-jdbc-adapter', :tag => 'v52.0', :platforms => :jruby
23
+ gem "jdbc-sqlite3", :platforms => :jruby
24
+ gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
25
+ gem "activerecord-jdbcmysql-adapter", :platforms => :jruby
26
+ gem "activerecord-jdbcpostgresql-adapter", :platforms => :jruby
27
+ gem "activerecord-jdbcmssql-adapter", :platforms => :jruby
28
28
  end
29
29
 
30
- gemspec path: "../"
30
+ gemspec :path => "../"
data/init/mssql.sql CHANGED
@@ -83,7 +83,7 @@ BEGIN
83
83
  END
84
84
  SELECT @cv1 = @cv0, @i = @i + 1
85
85
  END
86
- RETURN @c
86
+ RETURN @c
87
87
  END
88
88
  GO
89
89
 
@@ -94,9 +94,9 @@ GO
94
94
  --RETURNS TABLE
95
95
  --AS
96
96
  --BEGIN
97
- -- RETURN ( SELECT [Value] FROM
98
- -- (
99
- -- SELECT
97
+ -- RETURN ( SELECT [Value] FROM
98
+ -- (
99
+ -- SELECT
100
100
  -- [Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
101
101
  -- CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
102
102
  -- FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
data/init/mysql.sql CHANGED
@@ -1,45 +1,45 @@
1
1
  DELIMITER $$
2
2
 
3
3
  DROP FUNCTION IF EXISTS levenshtein_distance; $$
4
- CREATE FUNCTION levenshtein_distance( s1 VARCHAR(255), s2 VARCHAR(255) )
5
- RETURNS INT
6
- DETERMINISTIC
7
- BEGIN
8
- DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
9
- DECLARE s1_char CHAR;
10
- -- max strlen=255
11
- DECLARE cv0, cv1 VARBINARY(256);
4
+ CREATE FUNCTION levenshtein_distance( s1 VARCHAR(255), s2 VARCHAR(255) )
5
+ RETURNS INT
6
+ DETERMINISTIC
7
+ BEGIN
8
+ DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
9
+ DECLARE s1_char CHAR;
10
+ -- max strlen=255
11
+ DECLARE cv0, cv1 VARBINARY(256);
12
12
  SET s1 = IFNULL(s1,''), s2 = IFNULL(s2,'');
13
- SET s1_len = CHAR_LENGTH(IFNULL(s1,'')), s2_len = CHAR_LENGTH(IFNULL(s2,'')), cv1 = 0x00, j = 1, i = 1, c = 0;
14
- IF s1_len = 0 THEN
15
- RETURN s2_len;
16
- ELSEIF s2_len = 0 THEN
17
- RETURN s1_len;
18
- ELSEIF s1 = s2 THEN
13
+ SET s1_len = CHAR_LENGTH(IFNULL(s1,'')), s2_len = CHAR_LENGTH(IFNULL(s2,'')), cv1 = 0x00, j = 1, i = 1, c = 0;
14
+ IF s1_len = 0 THEN
15
+ RETURN s2_len;
16
+ ELSEIF s2_len = 0 THEN
17
+ RETURN s1_len;
18
+ ELSEIF s1 = s2 THEN
19
19
  RETURN 0;
20
- ELSE
21
- WHILE j <= s2_len DO
22
- SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
23
- END WHILE;
24
- WHILE i <= s1_len DO
25
- SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
26
- WHILE j <= s2_len DO
27
- SET c = c + 1;
28
- IF s1_char = SUBSTRING(s2, j, 1) THEN
29
- SET cost = 0; ELSE SET cost = 1;
30
- END IF;
31
- SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
32
- IF c > c_temp THEN SET c = c_temp; END IF;
33
- SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
34
- IF c > c_temp THEN
35
- SET c = c_temp;
36
- END IF;
37
- SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
38
- END WHILE;
39
- SET cv1 = cv0, i = i + 1;
40
- END WHILE;
41
- END IF;
42
- RETURN c;
20
+ ELSE
21
+ WHILE j <= s2_len DO
22
+ SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
23
+ END WHILE;
24
+ WHILE i <= s1_len DO
25
+ SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
26
+ WHILE j <= s2_len DO
27
+ SET c = c + 1;
28
+ IF s1_char = SUBSTRING(s2, j, 1) THEN
29
+ SET cost = 0; ELSE SET cost = 1;
30
+ END IF;
31
+ SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
32
+ IF c > c_temp THEN SET c = c_temp; END IF;
33
+ SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
34
+ IF c > c_temp THEN
35
+ SET c = c_temp;
36
+ END IF;
37
+ SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
38
+ END WHILE;
39
+ SET cv1 = cv0, i = i + 1;
40
+ END WHILE;
41
+ END IF;
42
+ RETURN c;
43
43
  END;$$
44
-
44
+
45
45
  DELIMITER ;
data/init/postgresql.sql CHANGED
@@ -4,17 +4,17 @@ CREATE OR REPLACE FUNCTION public.find_in_set(n INTEGER, s TEXT)
4
4
  AS $function$
5
5
  SELECT * FROM (
6
6
  select int4(z.row_number) from (
7
- select row_number() over(), y.x
7
+ select row_number() over(), y.x
8
8
  from (select unnest(('{' || $2 || '}')::int[]) as x) as y
9
9
  ) as z
10
10
  where z.x = $1
11
- UNION ALL
11
+ UNION ALL
12
12
  SELECT 0) z
13
13
  LIMIT 1
14
14
  $function$
15
15
  ;
16
16
 
17
- CREATE OR REPLACE FUNCTION public.levenshtein_distance(s text, t text)
17
+ CREATE OR REPLACE FUNCTION public.levenshtein_distance(s text, t text)
18
18
  RETURNS integer AS $$
19
19
  DECLARE i integer;
20
20
  DECLARE j integer;
@@ -48,58 +48,58 @@ BEGIN
48
48
  d[i*(n+1)+j] := LEAST(d[(i-1)*(n+1)+j]+1, d[i*(n+1)+j-1]+1, d[(i-1)*(n+1)+j-1]+c);
49
49
  END LOOP;
50
50
  END LOOP;
51
-
52
- return d[m*(n+1)+n];
51
+
52
+ return d[m*(n+1)+n];
53
53
  END;
54
54
  $$ LANGUAGE plpgsql IMMUTABLE;
55
55
 
56
- CREATE OR REPLACE FUNCTION public.DateDiff(units VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP)
56
+ CREATE OR REPLACE FUNCTION public.DateDiff(units VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP)
57
57
  RETURNS INT AS $$
58
58
  DECLARE
59
- diff_interval INTERVAL;
59
+ diff_interval INTERVAL;
60
60
  diff INT = 0;
61
61
  years_diff INT = 0;
62
62
  BEGIN
63
63
  IF units IN ('yy', 'yyyy', 'year', 'mm', 'm', 'month') THEN
64
64
  years_diff = DATE_PART('year', end_t) - DATE_PART('year', start_t);
65
-
65
+
66
66
  IF units IN ('yy', 'yyyy', 'year') THEN
67
67
  -- SQL Server does not count full years passed (only difference between year parts)
68
68
  RETURN years_diff;
69
69
  ELSE
70
70
  -- If end month is less than start month it will subtracted
71
- RETURN years_diff * 12 + (DATE_PART('month', end_t) - DATE_PART('month', start_t));
71
+ RETURN years_diff * 12 + (DATE_PART('month', end_t) - DATE_PART('month', start_t));
72
72
  END IF;
73
73
  END IF;
74
-
75
- -- Minus operator returns interval 'DDD days HH:MI:SS'
74
+
75
+ -- Minus operator returns interval 'DDD days HH:MI:SS'
76
76
  diff_interval = end_t - start_t;
77
-
77
+
78
78
  diff = diff + DATE_PART('day', diff_interval);
79
-
79
+
80
80
  IF units IN ('wk', 'ww', 'week') THEN
81
81
  diff = diff/7;
82
82
  RETURN diff;
83
83
  END IF;
84
-
84
+
85
85
  IF units IN ('dd', 'd', 'day') THEN
86
86
  RETURN diff;
87
87
  END IF;
88
-
89
- diff = diff * 24 + DATE_PART('hour', diff_interval);
90
-
88
+
89
+ diff = diff * 24 + DATE_PART('hour', diff_interval);
90
+
91
91
  IF units IN ('hh', 'hour') THEN
92
92
  RETURN diff;
93
93
  END IF;
94
-
94
+
95
95
  diff = diff * 60 + DATE_PART('minute', diff_interval);
96
-
96
+
97
97
  IF units IN ('mi', 'n', 'minute') THEN
98
98
  RETURN diff;
99
99
  END IF;
100
-
100
+
101
101
  diff = diff * 60 + DATE_PART('second', diff_interval);
102
-
102
+
103
103
  RETURN diff;
104
104
  END;
105
105
  $$ LANGUAGE plpgsql;
@@ -7,39 +7,35 @@ require 'arel_extensions/railtie' if defined?(Rails::Railtie)
7
7
  # Count|NamedFunction < Function < Arel::Nodes::Node
8
8
 
9
9
  # pure Arel internals improvements
10
- class Arel::Nodes::Binary
10
+ Arel::Nodes::Binary.class_eval do
11
11
  include Arel::AliasPredication
12
12
  include Arel::Expressions
13
13
  end
14
14
 
15
- class Arel::Nodes::Casted
15
+ Arel::Nodes::Casted.class_eval do
16
16
  include Arel::AliasPredication
17
-
18
- # They forget to define 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
23
- end
24
17
  end
25
18
 
26
- class Arel::Nodes::Unary
19
+ Arel::Nodes::Unary.class_eval do
27
20
  include Arel::Math
28
21
  include Arel::AliasPredication
29
22
  include Arel::Expressions
30
23
  end
31
24
 
32
- class Arel::Nodes::Grouping
25
+ Arel::Nodes::Grouping.class_eval do
26
+ include Arel::Math
27
+ include Arel::AliasPredication
33
28
  include Arel::OrderPredications
29
+ include Arel::Expressions
34
30
  end
35
31
 
36
- class Arel::Nodes::Function
32
+ Arel::Nodes::Function.class_eval do
37
33
  include Arel::Math
38
34
  include Arel::Expressions
39
35
  end
40
36
 
41
- if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("7.1.0")
42
- class Arel::Nodes::Case
37
+ if Arel::VERSION >= "7.1.0"
38
+ Arel::Nodes::Case.class_eval do
43
39
  include Arel::Math
44
40
  include Arel::Expressions
45
41
  end
@@ -97,26 +93,14 @@ module Arel
97
93
  ArelExtensions::Nodes::Duration.new(s.to_s+'i',expr)
98
94
  end
99
95
 
100
- def self.true
101
- Arel::Nodes::Equality.new(1,1)
102
- end
103
-
104
- def self.false
105
- Arel::Nodes::Equality.new(1,0)
106
- end
107
-
108
- def self.tuple *v
109
- tmp = Arel::Nodes::Grouping.new(nil)
110
- Arel::Nodes::Grouping.new(v.map{|e| tmp.convert_to_node(e)})
111
- end
112
96
  end
113
97
 
114
- class Arel::Attributes::Attribute
98
+ Arel::Attributes::Attribute.class_eval do
115
99
  include Arel::Math
116
100
  include ArelExtensions::Attributes
117
101
  end
118
102
 
119
- class Arel::Nodes::Function
103
+ Arel::Nodes::Function.class_eval do
120
104
  include ArelExtensions::Math
121
105
  include ArelExtensions::Comparators
122
106
  include ArelExtensions::DateDuration
@@ -125,28 +109,9 @@ class Arel::Nodes::Function
125
109
  include ArelExtensions::BooleanFunctions
126
110
  include ArelExtensions::NullFunctions
127
111
  include ArelExtensions::Predications
128
-
129
- alias_method(:old_as, :as) rescue nil
130
- def as other
131
- res = Arel::Nodes::As.new(self.clone, Arel.sql(other))
132
- if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("9.0.0")
133
- self.alias = Arel.sql(other)
134
- end
135
- res
136
- end
137
- end
138
-
139
- class Arel::Nodes::Grouping
140
- include ArelExtensions::Math
141
- include ArelExtensions::Comparators
142
- include ArelExtensions::DateDuration
143
- include ArelExtensions::MathFunctions
144
- include ArelExtensions::NullFunctions
145
- include ArelExtensions::StringFunctions
146
- include ArelExtensions::Predications
147
112
  end
148
113
 
149
- class Arel::Nodes::Unary
114
+ Arel::Nodes::Unary.class_eval do
150
115
  include ArelExtensions::Math
151
116
  include ArelExtensions::Attributes
152
117
  include ArelExtensions::MathFunctions
@@ -154,7 +119,7 @@ class Arel::Nodes::Unary
154
119
  include ArelExtensions::Predications
155
120
  end
156
121
 
157
- class Arel::Nodes::Binary
122
+ Arel::Nodes::Binary.class_eval do
158
123
  include ArelExtensions::Math
159
124
  include ArelExtensions::Attributes
160
125
  include ArelExtensions::MathFunctions
@@ -163,40 +128,25 @@ class Arel::Nodes::Binary
163
128
  include ArelExtensions::Predications
164
129
  end
165
130
 
166
- class Arel::Nodes::Equality
131
+ Arel::Nodes::Equality.class_eval do
167
132
  include ArelExtensions::Comparators
168
133
  include ArelExtensions::DateDuration
169
134
  include ArelExtensions::MathFunctions
170
135
  include ArelExtensions::StringFunctions
171
136
  end
172
137
 
173
- class Arel::InsertManager
138
+
139
+ Arel::InsertManager.class_eval do
174
140
  include ArelExtensions::InsertManager
175
141
  end
176
142
 
177
- class Arel::SelectManager
143
+ Arel::SelectManager.class_eval do
178
144
  include ArelExtensions::SetFunctions
179
145
  include ArelExtensions::Nodes
180
146
  end
181
147
 
182
- class Arel::Nodes::As
148
+ Arel::Nodes::As.class_eval do
183
149
  include ArelExtensions::Nodes
184
150
  end
185
151
 
186
- class Arel::Table
187
- alias_method(:old_alias, :alias) rescue nil
188
- def alias(name = "#{self.name}_2")
189
- name.blank? ? self : Arel::Nodes::TableAlias.new(self,name)
190
- end
191
- end
192
152
 
193
- class Arel::Nodes::TableAlias
194
- def method_missing(*args)
195
- met = args.shift.to_sym
196
- if self.relation.respond_to?(met)
197
- self.relation.send(met,args)
198
- else
199
- super(met,*args)
200
- end
201
- end
202
- end