arel_extensions 1.3.4 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +2 -1
  3. data/.rubocop.yml +4 -31
  4. data/.travis/oracle/download.js +152 -0
  5. data/.travis/oracle/download.sh +30 -0
  6. data/.travis/oracle/download_ojdbc.js +116 -0
  7. data/.travis/oracle/install.sh +34 -0
  8. data/.travis/setup_accounts.sh +9 -0
  9. data/.travis/sqlite3/extension-functions.sh +6 -0
  10. data/.travis.yml +223 -0
  11. data/Gemfile +21 -16
  12. data/README.md +14 -125
  13. data/Rakefile +30 -41
  14. data/TODO +1 -0
  15. data/appveyor.yml +44 -0
  16. data/arel_extensions.gemspec +14 -14
  17. data/functions.html +3 -3
  18. data/gemfiles/rails3.gemfile +10 -10
  19. data/gemfiles/rails4.gemfile +14 -14
  20. data/gemfiles/rails5_0.gemfile +14 -14
  21. data/gemfiles/rails5_1_4.gemfile +14 -14
  22. data/gemfiles/rails5_2.gemfile +14 -15
  23. data/init/mssql.sql +4 -4
  24. data/init/mysql.sql +38 -38
  25. data/init/oracle.sql +0 -0
  26. data/init/postgresql.sql +21 -21
  27. data/init/sqlite.sql +0 -0
  28. data/lib/arel_extensions/attributes.rb +3 -4
  29. data/lib/arel_extensions/boolean_functions.rb +14 -53
  30. data/lib/arel_extensions/common_sql_functions.rb +16 -15
  31. data/lib/arel_extensions/comparators.rb +28 -27
  32. data/lib/arel_extensions/date_duration.rb +14 -13
  33. data/lib/arel_extensions/insert_manager.rb +15 -18
  34. data/lib/arel_extensions/math.rb +44 -31
  35. data/lib/arel_extensions/math_functions.rb +39 -46
  36. data/lib/arel_extensions/nodes/abs.rb +1 -0
  37. data/lib/arel_extensions/nodes/blank.rb +2 -1
  38. data/lib/arel_extensions/nodes/case.rb +16 -16
  39. data/lib/arel_extensions/nodes/cast.rb +6 -8
  40. data/lib/arel_extensions/nodes/ceil.rb +1 -1
  41. data/lib/arel_extensions/nodes/coalesce.rb +3 -2
  42. data/lib/arel_extensions/nodes/collate.rb +10 -9
  43. data/lib/arel_extensions/nodes/concat.rb +18 -9
  44. data/lib/arel_extensions/nodes/date_diff.rb +22 -38
  45. data/lib/arel_extensions/nodes/duration.rb +3 -0
  46. data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
  47. data/lib/arel_extensions/nodes/floor.rb +1 -1
  48. data/lib/arel_extensions/nodes/format.rb +8 -35
  49. data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
  50. data/lib/arel_extensions/nodes/function.rb +37 -42
  51. data/lib/arel_extensions/nodes/is_null.rb +0 -0
  52. data/lib/arel_extensions/nodes/json.rb +39 -48
  53. data/lib/arel_extensions/nodes/length.rb +0 -5
  54. data/lib/arel_extensions/nodes/levenshtein_distance.rb +1 -1
  55. data/lib/arel_extensions/nodes/locate.rb +1 -0
  56. data/lib/arel_extensions/nodes/log10.rb +2 -1
  57. data/lib/arel_extensions/nodes/matches.rb +7 -5
  58. data/lib/arel_extensions/nodes/md5.rb +1 -0
  59. data/lib/arel_extensions/nodes/power.rb +5 -5
  60. data/lib/arel_extensions/nodes/rand.rb +1 -0
  61. data/lib/arel_extensions/nodes/repeat.rb +4 -2
  62. data/lib/arel_extensions/nodes/replace.rb +8 -16
  63. data/lib/arel_extensions/nodes/round.rb +6 -5
  64. data/lib/arel_extensions/nodes/soundex.rb +15 -15
  65. data/lib/arel_extensions/nodes/std.rb +21 -18
  66. data/lib/arel_extensions/nodes/substring.rb +16 -8
  67. data/lib/arel_extensions/nodes/trim.rb +5 -3
  68. data/lib/arel_extensions/nodes/union.rb +8 -5
  69. data/lib/arel_extensions/nodes/union_all.rb +7 -4
  70. data/lib/arel_extensions/nodes/wday.rb +4 -0
  71. data/lib/arel_extensions/nodes.rb +1 -1
  72. data/lib/arel_extensions/null_functions.rb +7 -5
  73. data/lib/arel_extensions/predications.rb +43 -44
  74. data/lib/arel_extensions/railtie.rb +5 -5
  75. data/lib/arel_extensions/set_functions.rb +7 -5
  76. data/lib/arel_extensions/string_functions.rb +29 -58
  77. data/lib/arel_extensions/tasks.rb +6 -6
  78. data/lib/arel_extensions/version.rb +1 -1
  79. data/lib/arel_extensions/visitors/ibm_db.rb +31 -24
  80. data/lib/arel_extensions/visitors/mssql.rb +184 -269
  81. data/lib/arel_extensions/visitors/mysql.rb +206 -271
  82. data/lib/arel_extensions/visitors/oracle.rb +175 -191
  83. data/lib/arel_extensions/visitors/oracle12.rb +31 -18
  84. data/lib/arel_extensions/visitors/postgresql.rb +170 -244
  85. data/lib/arel_extensions/visitors/sqlite.rb +124 -138
  86. data/lib/arel_extensions/visitors/to_sql.rb +237 -269
  87. data/lib/arel_extensions/visitors.rb +59 -75
  88. data/lib/arel_extensions.rb +31 -149
  89. data/test/database.yml +7 -15
  90. data/test/helper.rb +18 -0
  91. data/test/real_db_test.rb +113 -102
  92. data/test/support/fake_record.rb +3 -3
  93. data/test/test_comparators.rb +17 -14
  94. data/test/visitors/test_bulk_insert_oracle.rb +10 -10
  95. data/test/visitors/test_bulk_insert_sqlite.rb +12 -11
  96. data/test/visitors/test_bulk_insert_to_sql.rb +12 -10
  97. data/test/visitors/test_oracle.rb +55 -55
  98. data/test/visitors/test_to_sql.rb +226 -419
  99. data/test/with_ar/all_agnostic_test.rb +357 -567
  100. data/test/with_ar/insert_agnostic_test.rb +19 -25
  101. data/test/with_ar/test_bulk_sqlite.rb +15 -16
  102. data/test/with_ar/test_math_sqlite.rb +26 -26
  103. data/test/with_ar/test_string_mysql.rb +33 -31
  104. data/test/with_ar/test_string_sqlite.rb +34 -30
  105. metadata +23 -29
  106. data/.github/workflows/ruby.yml +0 -389
  107. data/gemfiles/rails6.gemfile +0 -29
  108. data/gemfiles/rails6_1.gemfile +0 -29
  109. data/gemfiles/rails7.gemfile +0 -22
  110. data/gemspecs/arel_extensions-v1.gemspec +0 -28
  111. data/gemspecs/arel_extensions-v2.gemspec +0 -28
  112. data/generate_gems.sh +0 -15
  113. data/lib/arel_extensions/aliases.rb +0 -14
  114. data/lib/arel_extensions/helpers.rb +0 -51
  115. data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
  116. data/lib/arel_extensions/nodes/sum.rb +0 -7
  117. data/lib/arel_extensions/visitors/convert_format.rb +0 -37
  118. data/test/arelx_test_helper.rb +0 -71
  119. data/version_v1.rb +0 -3
  120. data/version_v2.rb +0 -3
@@ -1,31 +1,30 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  gem 'rails', '~> 5.2.0'
4
4
  gem 'arel', '~> 9'
5
5
 
6
6
  group :development, :test do
7
- gem 'bigdecimal', '1.3.5', platforms: [:mri, :mingw, :x64_mingw, :mswin]
8
7
  gem 'activesupport', '~> 5.2.0'
9
8
  gem 'activemodel', '~> 5.2.0'
10
9
  gem 'activerecord', '~> 5.2.0'
11
10
 
12
- gem 'sqlite3', '<= 1.3.13', platforms: [:mri]
13
- gem 'mysql2', '0.4.10', platforms: [:mri]
14
- gem 'pg', '< 1.0.0', platforms: [:mri]
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]
15
14
 
16
- gem 'tiny_tds', platforms: [:mri, :mingw, :x64_mingw, :mswin]
17
- gem 'activerecord-sqlserver-adapter', '~> 5.2', platforms: [:mri, :mingw, :x64_mingw, :mswin]
15
+ gem "tiny_tds", :platforms => [:mri, :mingw] if RUBY_PLATFORM =~ /windows/
16
+ #gem "activerecord-sqlserver-adapter", :platforms => [:mri, :mingw]
18
17
 
19
- 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'
20
19
  gem 'activerecord-oracle_enhanced-adapter', '~> 5.2.0' if ENV.has_key? 'ORACLE_HOME'
21
20
 
22
21
  # for JRuby
23
- gem 'activerecord-jdbc-adapter', github: 'jruby/activerecord-jdbc-adapter', tag: 'v52.7', platforms: :jruby
24
- gem 'jdbc-sqlite3', platforms: :jruby
25
- gem 'activerecord-jdbcsqlite3-adapter', platforms: :jruby
26
- gem 'activerecord-jdbcmysql-adapter', platforms: :jruby
27
- gem 'activerecord-jdbcpostgresql-adapter', platforms: :jruby
28
- gem 'activerecord-jdbcsqlserver-adapter', '~> 52.0', 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
29
28
  end
30
29
 
31
- 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/oracle.sql CHANGED
File without changes
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;
data/init/sqlite.sql CHANGED
File without changes
@@ -1,4 +1,3 @@
1
- require 'arel_extensions/aliases'
2
1
  require 'arel_extensions/math'
3
2
  require 'arel_extensions/comparators'
4
3
  require 'arel_extensions/date_duration'
@@ -9,7 +8,6 @@ require 'arel_extensions/predications'
9
8
 
10
9
  module ArelExtensions
11
10
  module Attributes
12
- include ArelExtensions::Aliases
13
11
  include ArelExtensions::Math
14
12
  include ArelExtensions::Comparators
15
13
  include ArelExtensions::DateDuration
@@ -19,11 +17,12 @@ module ArelExtensions
19
17
  include ArelExtensions::Predications
20
18
 
21
19
  def ==(other)
22
- Arel::Nodes::Equality.new self, Arel.quoted(other, self)
20
+ Arel::Nodes::Equality.new self, Arel::Nodes.build_quoted(other, self)
23
21
  end
24
22
 
25
23
  def !=(other)
26
- Arel::Nodes::NotEqual.new self, Arel.quoted(other, self)
24
+ Arel::Nodes::NotEqual.new self, Arel::Nodes.build_quoted(other, self)
27
25
  end
26
+
28
27
  end
29
28
  end
@@ -2,12 +2,13 @@ require 'arel_extensions/nodes/then'
2
2
 
3
3
  module ArelExtensions
4
4
  module BooleanFunctions
5
+
5
6
  def ⋀(other)
6
7
  self.and(other)
7
8
  end
8
9
 
9
10
  def and *others
10
- Arel::Nodes::And.new self, others
11
+ Arel::Nodes::And.new([self]+ others.flatten)
11
12
  end
12
13
 
13
14
  def ⋁(other)
@@ -15,7 +16,12 @@ module ArelExtensions
15
16
  end
16
17
 
17
18
  def or *others
18
- Arel::Nodes::Or.new self, others
19
+ args = others.flatten
20
+ if args.length == 1
21
+ Arel::Nodes::Or.new(self, args.first)
22
+ else
23
+ ArelExtensions::Nodes::Or.new([self]+ args)
24
+ end
19
25
  end
20
26
 
21
27
  def then(t, f = nil)
@@ -24,61 +30,16 @@ module ArelExtensions
24
30
  end
25
31
  end
26
32
 
27
- class Arel::Nodes::And
33
+ Arel::Nodes::And.class_eval do
28
34
  include ArelExtensions::BooleanFunctions
29
-
30
- def self.new *children
31
- children =
32
- children.flatten.map { |c|
33
- c.is_a?(self) ? c.children : c
34
- }.flatten
35
- super(children)
36
- end
37
35
  end
38
36
 
39
- # For some reason, Arel's And is properly defined as variadic (it
40
- # stores @children, and hashes it all). However Arel's Or is defined
41
- # as binary, with only @left and @right, and hashing only @left and @right.
42
- #
43
- # So reimplement its ctor and accessors.
44
-
45
- class Arel::Nodes::Or
37
+ Arel::Nodes::Or.class_eval do
46
38
  include ArelExtensions::BooleanFunctions
39
+ end
47
40
 
48
- attr_reader :children
49
-
50
- def self.new *children
51
- children =
52
- children.flatten.map { |c|
53
- c.is_a?(self) ? c.children : c
54
- }.flatten
55
- super(*children)
56
- end
57
-
58
- def initialize *children
59
- @children = children
60
- end
61
-
62
- def initialize_copy(other)
63
- super
64
- @children = other.children.copy if other.children
65
- end
66
-
67
- def left
68
- children.first
69
- end
70
-
71
- def right
72
- children[1]
73
- end
41
+ ArelExtensions::Nodes.const_set('Or',Class.new(Arel::Nodes::And)).class_eval do
42
+ include ArelExtensions::BooleanFunctions
43
+ end
74
44
 
75
- def hash
76
- children.hash
77
- end
78
45
 
79
- def eql?(other)
80
- self.class == other.class &&
81
- children == other.children
82
- end
83
- alias :== :eql?
84
- end
@@ -1,49 +1,49 @@
1
1
  module ArelExtensions
2
2
  class CommonSqlFunctions
3
+
3
4
  def initialize(cnx)
4
5
  @cnx = cnx
5
6
  if cnx && cnx.adapter_name =~ /sqlite/i && !$load_extension_disabled
6
7
  begin
7
8
  db = cnx.raw_connection
8
9
  db.enable_load_extension(1)
9
- db.load_extension('/usr/lib/sqlite3/pcre.so')
10
- db.load_extension('/usr/lib/sqlite3/extension-functions.so')
10
+ db.load_extension("/usr/lib/sqlite3/pcre.so")
11
+ db.load_extension("/usr/lib/sqlite3/extension-functions.so")
11
12
  db.enable_load_extension(0)
12
13
  rescue => e
13
14
  $load_extension_disabled = true
14
- puts "cannot load extensions #{e.inspect}"
15
+ puts "can not load extensions #{e.inspect}"
15
16
  end
16
17
  end
17
18
  end
18
19
 
19
20
  def add_sqlite_functions
20
21
  db = @cnx.raw_connection
21
- db.create_function('find_in_set', 1) do |func, val, list|
22
+ db.create_function("find_in_set", 1) do |func, val, list|
22
23
  case list
23
24
  when String
24
25
  i = list.split(',').index(val.to_s)
25
- func.result = i ? (i + 1) : 0
26
+ func.result = i ? (i+1) : 0
26
27
  when NilClass
27
28
  func.result = nil
28
29
  else
29
30
  i = list.to_s.split(',').index(val.to_s)
30
- func.result = i ? (i + 1) : 0
31
+ func.result = i ? (i+1) : 0
31
32
  end
32
33
  end
33
- db.create_function('instr', 1) do |func, value1, value2|
34
+ db.create_function("instr", 1) do |func, value1, value2|
34
35
  i = value1.to_s.index(value2.to_s)
35
- func.result = i ? (i + 1) : 0
36
- end rescue 'function instr already here (>= 3.8.5)'
36
+ func.result = i ? (i+1) : 0
37
+ end rescue "function instr already here (>= 3.8.5)"
37
38
  end
38
39
 
39
40
  def add_sql_functions(env_db = nil)
40
41
  env_db ||= @cnx.adapter_name
41
- env_db = 'mysql' if env_db =~ /mysql/i
42
42
  if env_db =~ /sqlite/i
43
43
  begin
44
44
  add_sqlite_functions
45
45
  rescue => e
46
- puts "cannot add sqlite functions #{e.inspect}"
46
+ puts "can not add sqlite functions #{e.inspect}"
47
47
  end
48
48
  end
49
49
  if File.exist?("init/#{env_db}.sql")
@@ -52,14 +52,15 @@ 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('$$')[1..-2].each { |str|
57
- @cnx.execute(str.strip) unless str.strip.blank?
58
- }
55
+ elsif env_db == 'mysql'
56
+ sql.split("$$")[1..-2].each { |str|
57
+ @cnx.execute(str.strip) unless str.strip.blank?
58
+ }
59
59
  else
60
60
  @cnx.execute(sql) unless sql.blank?
61
61
  end
62
62
  end
63
63
  end
64
+
64
65
  end
65
66
  end
@@ -1,58 +1,59 @@
1
1
  module ArelExtensions
2
2
  module Comparators
3
+
3
4
  def >(other)
4
- Arel::Nodes::GreaterThan.new self, Arel.quoted(other, self)
5
+ Arel::Nodes::GreaterThan.new self, Arel::Nodes.build_quoted(other, self)
5
6
  end
6
7
 
7
8
  def >=(other)
8
- Arel::Nodes::GreaterThanOrEqual.new self, Arel.quoted(other, self)
9
+ Arel::Nodes::GreaterThanOrEqual.new self, Arel::Nodes.build_quoted(other, self)
9
10
  end
10
11
 
11
12
  def <(other)
12
- Arel::Nodes::LessThan.new self, Arel.quoted(other, self)
13
+ Arel::Nodes::LessThan.new self, Arel::Nodes.build_quoted(other, self)
13
14
  end
14
15
 
15
16
  def <=(other)
16
- Arel::Nodes::LessThanOrEqual.new self, Arel.quoted(other, self)
17
+ Arel::Nodes::LessThanOrEqual.new self, Arel::Nodes.build_quoted(other, self)
17
18
  end
18
19
 
19
- # REGEXP function
20
- # Pattern matching using regular expressions
20
+
21
+ #REGEXP function
22
+ #Pattern matching using regular expressions
21
23
  def =~(other)
22
- # arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
23
- # if arg == :string || arg == :text
24
- Arel::Nodes::Regexp.new self, convert_regexp(other)
25
- # end
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
26
+ Arel::Nodes::Regexp.new self, convert_regexp(other)
27
+ # end
26
28
  end
27
29
 
28
- # NOT_REGEXP function
29
- # Negation of Regexp
30
+ #NOT_REGEXP function
31
+ #Negation of Regexp
30
32
  def !~(other)
31
- # arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
32
- # if arg == :string || arg == :text
33
- Arel::Nodes::NotRegexp.new self, convert_regexp(other)
34
- # end
33
+ # arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
34
+ # if arg == :string || arg == :text
35
+ Arel::Nodes::NotRegexp.new self, convert_regexp(other)
36
+ # end
35
37
  end
36
38
 
37
39
  private
38
-
39
- # Function used for not_regexp.
40
+ #Function use for not_regexp
40
41
  def convert_regexp(other)
41
42
  case other
42
43
  when String
43
- # Do nothing.
44
+ #Do nothing
44
45
  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_]')
46
+ other = other.source.gsub('\A','^')
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.quoted(other, self)
55
+ Arel::Nodes.build_quoted(other, self)
56
56
  end
57
+
57
58
  end
58
59
  end
@@ -4,24 +4,24 @@ require 'arel_extensions/nodes/wday'
4
4
 
5
5
  module ArelExtensions
6
6
  module DateDuration
7
- # function returns the year (as a number) given a date value.
7
+ #function returns the year (as a number) given a date value.
8
8
  def year
9
- ArelExtensions::Nodes::Duration.new 'y', self
9
+ ArelExtensions::Nodes::Duration.new "y", self
10
10
  end
11
11
 
12
- # function returns the month (as a number) given a date value.
12
+ #function returns the month (as a number) given a date value.
13
13
  def month
14
- ArelExtensions::Nodes::Duration.new 'm', self
14
+ ArelExtensions::Nodes::Duration.new "m", self
15
15
  end
16
16
 
17
- # function returns the week (as a number) given a date value.
17
+ #function returns the week (as a number) given a date value.
18
18
  def week
19
- ArelExtensions::Nodes::Duration.new 'w', self
19
+ ArelExtensions::Nodes::Duration.new "w", self
20
20
  end
21
21
 
22
- # function returns the month (as a number) given a date value.
22
+ #function returns the month (as a number) given a date value.
23
23
  def day
24
- ArelExtensions::Nodes::Duration.new 'd', self
24
+ ArelExtensions::Nodes::Duration.new "d", self
25
25
  end
26
26
 
27
27
  def wday
@@ -29,19 +29,20 @@ module ArelExtensions
29
29
  end
30
30
 
31
31
  def hour
32
- ArelExtensions::Nodes::Duration.new 'h', self
32
+ ArelExtensions::Nodes::Duration.new "h", self
33
33
  end
34
34
 
35
35
  def minute
36
- ArelExtensions::Nodes::Duration.new 'mn', self
36
+ ArelExtensions::Nodes::Duration.new "mn", self
37
37
  end
38
38
 
39
39
  def second
40
- ArelExtensions::Nodes::Duration.new 's', self
40
+ ArelExtensions::Nodes::Duration.new "s", self
41
41
  end
42
42
 
43
- def format(tpl, time_zone = nil)
44
- ArelExtensions::Nodes::Format.new [self, tpl, time_zone]
43
+ def format(tpl)
44
+ ArelExtensions::Nodes::Format.new [self, tpl]
45
45
  end
46
+
46
47
  end
47
48
  end
@@ -2,35 +2,32 @@ require 'arel'
2
2
 
3
3
  module ArelExtensions
4
4
  module InsertManager
5
+
5
6
  def bulk_insert(cols, data)
6
- raise ArgumentError, 'cols must be present' if cols.blank?
7
- columns =
8
- case cols.first
7
+ case cols.first
8
+ when String, Symbol
9
+ cols.each { |c|
10
+ @ast.columns << @ast.relation[c]
11
+ }
9
12
  when Array
10
- case cols.first.first
11
- when Arel::Attributes::Attribute
12
- cols
13
- when String, Symbol
14
- cols.map {|c| [@ast.relation[c.first]] }
15
- else
16
- raise ArgumentError, "cols has an invalid type: #{cols.first.first.class}"
13
+ if String === cols.first.first
14
+ @ast.columns = cols.map {|c| [@ast.relation[c.first]] }
15
+ elsif Arel::Attributes::Attribute == cols.first.first
16
+ @ast.columns = cols
17
17
  end
18
- when String, Symbol
19
- cols.map { |c| @ast.relation[c] }
20
- else
21
- raise ArgumentError, "cols has an invalid type: #{cols.first.class}"
22
- end
23
- self.values = BulkValues.new(columns, data)
24
- @ast.columns = columns
18
+ when NilClass
19
+ @ast.columns = @ast.relation.columns
20
+ end
21
+ self.values = BulkValues.new(@ast.columns, data)
25
22
  end
26
23
 
27
24
  class BulkValues < Arel::Nodes::Node
28
25
  attr_accessor :left, :cols
29
-
30
26
  def initialize(cols, values)
31
27
  @left = values
32
28
  @cols = cols
33
29
  end
34
30
  end
31
+
35
32
  end
36
33
  end