activerecord-sqlserver-adapter 3.1.1 → 3.2.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,9 +41,12 @@ module Arel
41
41
  x
42
42
  when String
43
43
  x.split(',').map do |s|
44
- e, d = s.split
44
+ s = x if x.strip =~ /\A\b\w+\b\(.*,.*\)(\s+(ASC|DESC))?\Z/i # Allow functions with comma(s) to pass thru.
45
+ s.strip!
46
+ d = s =~ /(ASC|DESC)\Z/i ? $1.upcase : nil
47
+ e = d.nil? ? s : s.mb_chars[0...-d.length].strip
45
48
  e = Arel.sql(e)
46
- d =~ /desc/i ? Arel::Nodes::Descending.new(e) : Arel::Nodes::Ascending.new(e)
49
+ d && d == "DESC" ? Arel::Nodes::Descending.new(e) : Arel::Nodes::Ascending.new(e)
47
50
  end
48
51
  else
49
52
  e = Arel.sql(x.to_s)
@@ -99,7 +102,7 @@ module Arel
99
102
 
100
103
  def visit_Arel_Nodes_UpdateStatement(o)
101
104
  if o.orders.any? && o.limit.nil?
102
- o.limit = Nodes::Limit.new(2147483647)
105
+ o.limit = Nodes::Limit.new(9223372036854775807)
103
106
  end
104
107
  super
105
108
  end
@@ -138,9 +141,11 @@ module Arel
138
141
  orders = o.orders.uniq
139
142
  if windowed
140
143
  projections = function_select_statement?(o) ? projections : projections.map { |x| projection_without_expression(x) }
144
+ groups = projections.map { |x| projection_without_expression(x) } if windowed_single_distinct_select_statement?(o) && groups.empty?
145
+ groups += orders.map { |x| Arel.sql(x.expr) } if windowed_single_distinct_select_statement?(o)
141
146
  elsif eager_limiting_select_statement?(o)
142
- groups = projections.map { |x| projection_without_expression(x) }
143
147
  projections = projections.map { |x| projection_without_expression(x) }
148
+ groups = projections.map { |x| projection_without_expression(x) }
144
149
  orders = orders.map do |x|
145
150
  expr = Arel.sql projection_without_expression(x.expr)
146
151
  x.descending? ? Arel::Nodes::Max.new([expr]) : Arel::Nodes::Min.new([expr])
@@ -149,8 +154,9 @@ module Arel
149
154
  projections = projections.map { |x| projection_without_expression(x) }
150
155
  end
151
156
  [ ("SELECT" if !windowed),
157
+ (visit(core.set_quantifier) if core.set_quantifier && !windowed),
152
158
  (visit(o.limit) if o.limit && !windowed),
153
- (projections.map{ |x| visit(x) }.join(', ')),
159
+ (projections.map{ |x| v = visit(x); v == "1" ? "1 AS [__wrp]" : v }.join(', ')),
154
160
  (source_with_lock_for_select_statement(o)),
155
161
  ("WHERE #{core.wheres.map{ |x| visit(x) }.join ' AND ' }" unless core.wheres.empty?),
156
162
  ("GROUP BY #{groups.map { |x| visit x }.join ', ' }" unless groups.empty?),
@@ -160,15 +166,18 @@ module Arel
160
166
  end
161
167
 
162
168
  def visit_Arel_Nodes_SelectStatementWithOffset(o)
169
+ core = o.cores.first
170
+ o.limit ||= Arel::Nodes::Limit.new(9223372036854775807)
163
171
  orders = rowtable_orders(o)
164
172
  [ "SELECT",
165
- (visit(o.limit) if o.limit && !single_distinct_select_statement?(o)),
173
+ (visit(o.limit) if o.limit && !windowed_single_distinct_select_statement?(o)),
166
174
  (rowtable_projections(o).map{ |x| visit(x) }.join(', ')),
167
175
  "FROM (",
168
- "SELECT ROW_NUMBER() OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS [__rn],",
176
+ "SELECT #{core.set_quantifier ? 'DISTINCT DENSE_RANK()' : 'ROW_NUMBER()'} OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS [__rn],",
169
177
  visit_Arel_Nodes_SelectStatementWithOutOffset(o,true),
170
178
  ") AS [__rnt]",
171
179
  (visit(o.offset) if o.offset),
180
+ "ORDER BY [__rnt].[__rn] ASC"
172
181
  ].compact.join ' '
173
182
  end
174
183
 
@@ -200,7 +209,7 @@ module Arel
200
209
  source = "FROM #{visit(core.source).strip}" if core.source
201
210
  if source && o.lock
202
211
  lock = visit o.lock
203
- index = source.match(/FROM [\w\[\]\.]+/)[0].length
212
+ index = source.match(/FROM [\w\[\]\.]+/)[0].mb_chars.length
204
213
  source.insert index, " #{lock}"
205
214
  else
206
215
  source
@@ -238,6 +247,10 @@ module Arel
238
247
  p1.respond_to?(:include?) && p1.include?('DISTINCT'))
239
248
  end
240
249
 
250
+ def windowed_single_distinct_select_statement?(o)
251
+ o.limit && o.offset && single_distinct_select_statement?(o)
252
+ end
253
+
241
254
  def single_distinct_select_everything_statement?(o)
242
255
  single_distinct_select_statement?(o) && visit(o.cores.first.projections.first).ends_with?(".*")
243
256
  end
@@ -316,7 +329,17 @@ module Arel
316
329
 
317
330
  def rowtable_projections(o)
318
331
  core = o.cores.first
319
- if single_distinct_select_statement?(o)
332
+ if windowed_single_distinct_select_statement?(o) && core.groups.blank?
333
+ tn = table_from_select_statement(o).name
334
+ core.projections.map do |x|
335
+ x.dup.tap do |p|
336
+ p.sub! 'DISTINCT', ''
337
+ p.insert 0, visit(o.limit) if o.limit
338
+ p.gsub! /\[?#{tn}\]?\./, '[__rnt].'
339
+ p.strip!
340
+ end
341
+ end
342
+ elsif single_distinct_select_statement?(o)
320
343
  tn = table_from_select_statement(o).name
321
344
  core.projections.map do |x|
322
345
  x.dup.tap do |p|
metadata CHANGED
@@ -1,15 +1,9 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: activerecord-sqlserver-adapter
3
- version: !ruby/object:Gem::Version
4
- hash: 1
5
- prerelease:
6
- segments:
7
- - 3
8
- - 1
9
- - 1
10
- version: 3.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.2.18
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Ken Collins
14
8
  - Murray Steele
15
9
  - Shawn Balestracci
@@ -18,83 +12,72 @@ authors:
18
12
  autorequire:
19
13
  bindir: bin
20
14
  cert_chain: []
21
-
22
- date: 2011-09-22 00:00:00 -04:00
23
- default_executable:
24
- dependencies:
25
- - !ruby/object:Gem::Dependency
15
+ date: 2017-06-19 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
26
18
  name: activerecord
27
- prerelease: false
28
- requirement: &id001 !ruby/object:Gem::Requirement
29
- none: false
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- hash: 3
34
- segments:
35
- - 3
36
- - 1
37
- - 0
38
- version: 3.1.0
19
+ requirement: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "~>"
22
+ - !ruby/object:Gem::Version
23
+ version: 3.2.0
39
24
  type: :runtime
40
- version_requirements: *id001
41
- description: SQL Server 2005 and 2008 Adapter For ActiveRecord
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: 3.2.0
31
+ description: ActiveRecord SQL Server Adapter. For SQL Server 2005 And Higher.
42
32
  email: ken@metaskills.net
43
33
  executables: []
44
-
45
34
  extensions: []
46
-
47
- extra_rdoc_files:
48
- - README.rdoc
49
- files:
35
+ extra_rdoc_files: []
36
+ files:
50
37
  - CHANGELOG
51
38
  - MIT-LICENSE
52
- - README.rdoc
39
+ - VERSION
53
40
  - lib/active_record/connection_adapters/sqlserver/core_ext/active_record.rb
41
+ - lib/active_record/connection_adapters/sqlserver/core_ext/database_statements.rb
42
+ - lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
43
+ - lib/active_record/connection_adapters/sqlserver/core_ext/explain_subscriber.rb
54
44
  - lib/active_record/connection_adapters/sqlserver/core_ext/odbc.rb
45
+ - lib/active_record/connection_adapters/sqlserver/core_ext/relation.rb
55
46
  - lib/active_record/connection_adapters/sqlserver/database_limits.rb
56
47
  - lib/active_record/connection_adapters/sqlserver/database_statements.rb
57
48
  - lib/active_record/connection_adapters/sqlserver/errors.rb
58
49
  - lib/active_record/connection_adapters/sqlserver/quoting.rb
50
+ - lib/active_record/connection_adapters/sqlserver/schema_cache.rb
59
51
  - lib/active_record/connection_adapters/sqlserver/schema_statements.rb
60
- - lib/active_record/connection_adapters/sqlserver/version.rb
52
+ - lib/active_record/connection_adapters/sqlserver/showplan.rb
53
+ - lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
54
+ - lib/active_record/connection_adapters/sqlserver/showplan/printer_xml.rb
55
+ - lib/active_record/connection_adapters/sqlserver/utils.rb
61
56
  - lib/active_record/connection_adapters/sqlserver_adapter.rb
62
57
  - lib/activerecord-sqlserver-adapter.rb
63
58
  - lib/arel/visitors/sqlserver.rb
64
- has_rdoc: true
65
59
  homepage: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
66
60
  licenses: []
67
-
61
+ metadata: {}
68
62
  post_install_message:
69
- rdoc_options:
70
- - --main
71
- - README.rdoc
72
- require_paths:
63
+ rdoc_options: []
64
+ require_paths:
73
65
  - lib
74
- required_ruby_version: !ruby/object:Gem::Requirement
75
- none: false
76
- requirements:
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
77
68
  - - ">="
78
- - !ruby/object:Gem::Version
79
- hash: 3
80
- segments:
81
- - 0
82
- version: "0"
83
- required_rubygems_version: !ruby/object:Gem::Requirement
84
- none: false
85
- requirements:
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
86
73
  - - ">="
87
- - !ruby/object:Gem::Version
88
- hash: 3
89
- segments:
90
- - 0
91
- version: "0"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
92
76
  requirements: []
93
-
94
77
  rubyforge_project: activerecord-sqlserver-adapter
95
- rubygems_version: 1.6.2
78
+ rubygems_version: 2.6.8
96
79
  signing_key:
97
- specification_version: 3
98
- summary: SQL Server 2005 and 2008 Adapter For ActiveRecord.
80
+ specification_version: 4
81
+ summary: ActiveRecord SQL Server Adapter. For SQL Server 2005 And Higher.
99
82
  test_files: []
100
-
83
+ has_rdoc:
data/README.rdoc DELETED
@@ -1,176 +0,0 @@
1
-
2
- == SQL Server 2005/2008 & Azure Adapter For ActiveRecord
3
-
4
- The SQL Server adapter for ActiveRecord. If you need the adapter for SQL Server 2000, you are still in the right spot. Just install the latest 2.3.x version of the adapter. Note, we follow a rational versioning policy that tracks ActiveRecord. That means that our 2.3.x version of the adapter is only for the latest 2.3 version of Rails.
5
-
6
-
7
- == What's New
8
-
9
- * Rails 3.1 prepared statement support leverages cached query plans.
10
- If you use DBLIB/TinyTDS, you must use FreeTDS 0.91 !!!!!
11
- https://github.com/rails-sqlserver/tiny_tds/issues/41
12
- * We now support your native language date/time formats automatically!
13
- * Default unicode datatypes! Disable with #enable_default_unicode_types to false.
14
- * New #lowercase_schema_reflection configuration option for legacy DBs.
15
- * New dblib connection mode using TinyTDS! Default mode too!
16
-
17
-
18
- ==== Testing Rake Tasks Support
19
-
20
- This is a long story, but if you are not working with a legacy database and you can trust your schema.rb to setup you local development or test database, then we have adapter level support for rails :db rake tasks. Please read this wiki page for full details.
21
-
22
- http://wiki.github.com/rails-sqlserver/activerecord-sqlserver-adapter/rails-db-rake-tasks
23
-
24
-
25
- ==== Date/Time Data Type Hinting
26
-
27
- SQL Server 2005 does not include a native data type for just 'date' or 'time', it only has 'datetime'. To pass the ActiveRecord tests we implemented two simple class methods that can teach your models to coerce column information to be cast correctly. Simply pass a list of symbols to either the <tt>coerce_sqlserver_date</tt> or <tt>coerce_sqlserver_time</tt> methods that correspond to 'datetime' columns that need to be cast correctly.
28
-
29
- class Topic < ActiveRecord::Base
30
- coerce_sqlserver_date :last_read
31
- coerce_sqlserver_time :bonus_time
32
- end
33
-
34
- This implementation has some limitations. To date we can only coerce date/time types for models that conform to the expected ActiveRecord class to table naming conventions. So a table of 'foo_bar_widgets' will look for coerced column types in the FooBarWidget class.
35
-
36
-
37
- ==== Executing Stored Procedures
38
-
39
- Every class that sub classes ActiveRecord::Base will now have an execute_procedure class method to use. This method takes the name of the stored procedure which can be a string or symbol and any number of variables to pass to the procedure. Arguments will automatically be quoted per the connection's standards as normal. For example.
40
-
41
- Account.execute_procedure :update_totals, 'admin', nil, true
42
-
43
-
44
- ==== Native Data Type Support
45
-
46
- Currently the following custom data types have been tested for schema definitions.
47
-
48
- * char
49
- * nchar
50
- * nvarchar
51
- * ntext
52
- * varchar(max)
53
- * nvarchar(max)
54
-
55
- For example:
56
-
57
- create_table :sql_server_custom_types, :force => true do |t|
58
- t.column :ten_code, :char, :limit => 10
59
- t.column :ten_code_utf8, :nchar, :limit => 10
60
- t.column :title_utf8, :nvarchar
61
- t.column :body, :varchar_max # Creates varchar(max)
62
- t.column :body_utf8, :ntext
63
- t.column :body2_utf8, :nvarchar_max # Creates nvarchar(max)
64
- end
65
-
66
- Manually creating a varchar(max) is not necessary since this is the default type created when specifying a :text field. As time goes on we will be testing other SQL Server specific data types are handled correctly when created in a migration.
67
-
68
-
69
- ==== Native Text/String/Binary Data Type Accessor
70
-
71
- To pass the ActiveRecord tests we had to implement an class accessor for the native type created for :text columns. By default any :text column created by migrations will create a 'varchar(max)' data type. This type can be queried using the SQL = operator and has plenty of storage space which is why we made it the default. If for some reason you want to change the data type created during migrations you can configure this line to your liking in a config/initializers file.
72
-
73
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(8000)'
74
-
75
- Also, there is a class attribute setter for the native string database type. This is the same for all SQL Server versions, 'varchar'. However it can be used instead of the #enable_default_unicode_types below for finer grain control over which types you want unicode safe when adding or changing the schema.
76
-
77
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
78
-
79
- By default any :binary column created by migrations will create a 'varbinary(max)' data type. This too can be set using an initializer.
80
-
81
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_binary_database_type = 'image'
82
-
83
-
84
- ==== Setting Unicode Types As Default
85
-
86
- By default the adapter will use unicode safe data types for :string and :text types when defining/changing the schema!!! This was changed in version 3.1 since it is about time we push better unicode support and since we default to TinyTDS (DBLIB) which supports unicode queries and data. If you choose, you can set the following class attribute in a config/initializers file that will disable this behavior.
87
-
88
- # Default
89
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
90
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'nvarchar(max)'
91
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
92
-
93
- # Disabled
94
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = false
95
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(max)'
96
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'varchar'
97
-
98
- It is important to remember that unicode types in SQL Server have approximately half the storage capacity as their counter parts. So where a normal string would max out at (8000) a unicode string will top off at (4000).
99
-
100
-
101
- ==== Force Schema To Lowercase
102
-
103
- Although it is not necessary, the Ruby convention is to use lowercase method names. If your database schema is in upper or mixed case, we can force all table and column names during the schema reflection process to be lowercase. Add this to your config/initializers file for the adapter.
104
-
105
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.lowercase_schema_reflection = true
106
-
107
-
108
- ==== Schema Information Logging
109
-
110
- By default all queries to the INFORMATION_SCHEMA table is silenced. If you think logging these queries are useful, you can enable it by adding this like to a initializer file.
111
-
112
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.log_info_schema_queries = true
113
-
114
-
115
- ==== Auto Connecting
116
-
117
- By default the adapter will auto connect to lost DB connections. For every query it will retry at intervals of 2, 4, 8, 16 and 32 seconds. During each retry it will callback out to ActiveRecord::Base.did_retry_sqlserver_connection(connection,count). When all retries fail, it will callback to ActiveRecord::Base.did_lose_sqlserver_connection(connection). Both implementations of these methods are to write to the rails logger, however, they make great override points for notifications like Hoptoad. If you want to disable automatic reconnections use the following in an initializer.
118
-
119
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.auto_connect = false
120
-
121
-
122
- == Versions
123
-
124
- The adapter follows a rational versioning policy that also tracks ActiveRecord's major and minor version. That means the latest 3.1.x version of the adapter will always work for the latest 3.1.x version of ActiveRecord.
125
-
126
-
127
- == Installation
128
-
129
- The adapter has no strict gem dependencies outside of ActiveRecord. You will have to pick a connection mode, the default is dblib which uses the TinyTDS gem. Just bundle the gem and the adapter will use it.
130
-
131
- gem 'tiny_tds'
132
- gem 'activerecord-sqlserver-adapter', '~> 3.1.0'
133
-
134
- If you want to use ruby ODBC, please use at least version 0.99992 since that contains fixes for both native types as well as fixes for proper encoding support under 1.9. If you have any troubles installing the lower level libraries for the adapter, please consult the wiki pages for various platform installation guides. Tons of good info can be found and we ask that you contribute too!
135
-
136
- http://wiki.github.com/rails-sqlserver/activerecord-sqlserver-adapter/platform-installation
137
-
138
-
139
-
140
- == Contributing
141
-
142
- If you’d like to contribute a feature or bugfix, thanks! To make sure your fix/feature has a high chance of being added, please read the following guidelines. First, ask on the Google list, IRC, or post a ticket on github issues. Second, make sure there are tests! We will not accept any patch that is not tested. Please read the RUNNING_UNIT_TESTS file for the details of how to run the unit tests.
143
-
144
- * Github: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
145
- * Google Group: http://groups.google.com/group/rails-sqlserver-adapter
146
- * IRC Room: #rails-sqlserver on irc.freenode.net
147
-
148
-
149
-
150
- == Credits & Contributions
151
-
152
- Many many people have contributed. If you do not see your name here and it should be let us know. Also, many thanks go out to those that have pledged financial contributions.
153
-
154
- === Contributers
155
- Up-to-date list of contributors: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter/contributors
156
-
157
- * metaskills (Ken Collins)
158
- * h-lame (Murray Steele)
159
- * vegantech
160
- * cjheath (Clifford Heath)
161
- * jrafanie (Joe Rafaniello)
162
- * nerdrew (Andrew Ryan)
163
- * snowblink (Jonathan Lim)
164
- * koppen (Jakob Skjerning)
165
- * ebryn (Erik Bryn)
166
- * adzap (Adam Meehan)
167
- * neomindryan (Ryan Findley)
168
- * jeremydurham (Jeremy Durham)
169
-
170
- === Donators
171
- http://pledgie.com/campaigns/15531
172
-
173
- == License
174
-
175
- Copyright © 2008-2011. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
176
-
@@ -1,11 +0,0 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- module Sqlserver
4
- module Version
5
-
6
- VERSION = '3.1.1'.freeze
7
-
8
- end
9
- end
10
- end
11
- end