activerecord-jdbcsqlserver-adapter 50.0.0 → 52.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/.travis.yml +4 -5
  4. data/CHANGELOG.md +22 -101
  5. data/{Dockerfile → Dockerfile.ci} +0 -0
  6. data/Gemfile +1 -3
  7. data/README.md +5 -9
  8. data/VERSION +1 -1
  9. data/activerecord-jdbcsqlserver-adapter.gemspec +2 -2
  10. data/appveyor.yml +1 -1
  11. data/docker-compose.ci.yml +7 -5
  12. data/lib/active_record/connection_adapters/sqlserver/core_ext/active_record.rb +3 -1
  13. data/lib/active_record/connection_adapters/sqlserver/core_ext/attribute_methods.rb +3 -1
  14. data/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb +51 -0
  15. data/lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb +18 -20
  16. data/lib/active_record/connection_adapters/sqlserver/core_ext/explain_subscriber.rb +5 -3
  17. data/lib/active_record/connection_adapters/sqlserver/core_ext/finder_methods.rb +43 -0
  18. data/lib/active_record/connection_adapters/sqlserver/core_ext/query_methods.rb +26 -0
  19. data/lib/active_record/connection_adapters/sqlserver/database_limits.rb +13 -2
  20. data/lib/active_record/connection_adapters/sqlserver/database_statements.rb +94 -28
  21. data/lib/active_record/connection_adapters/sqlserver/database_tasks.rb +1 -0
  22. data/lib/active_record/connection_adapters/sqlserver/jdbc_overrides.rb +5 -25
  23. data/lib/active_record/connection_adapters/sqlserver/schema_creation.rb +24 -1
  24. data/lib/active_record/connection_adapters/sqlserver/schema_dumper.rb +23 -2
  25. data/lib/active_record/connection_adapters/sqlserver/schema_statements.rb +110 -74
  26. data/lib/active_record/connection_adapters/sqlserver/table_definition.rb +15 -7
  27. data/lib/active_record/connection_adapters/sqlserver/transaction.rb +3 -4
  28. data/lib/active_record/connection_adapters/sqlserver/type/big_integer.rb +0 -4
  29. data/lib/active_record/connection_adapters/sqlserver/type/data.rb +5 -0
  30. data/lib/active_record/connection_adapters/sqlserver/type/datetime.rb +3 -6
  31. data/lib/active_record/connection_adapters/sqlserver/type/json.rb +1 -1
  32. data/lib/active_record/connection_adapters/sqlserver/type/string.rb +7 -0
  33. data/lib/active_record/connection_adapters/sqlserver/type/time.rb +1 -0
  34. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +47 -24
  35. data/lib/active_record/tasks/sqlserver_database_tasks.rb +5 -3
  36. data/lib/activerecord-jdbcsqlserver-adapter.rb +4 -1
  37. data/lib/arel/visitors/sqlserver.rb +17 -4
  38. data/lib/arel_sqlserver.rb +0 -1
  39. data/lib/jdbc_mssql_driver_loader.rb +22 -0
  40. data/test/bin/install-freetds.sh +18 -0
  41. data/test/bin/setup.sh +19 -0
  42. data/test/cases/adapter_test_sqlserver.rb +43 -39
  43. data/test/cases/change_column_null_test_sqlserver.rb +42 -0
  44. data/test/cases/coerced_tests.rb +419 -39
  45. data/test/cases/column_test_sqlserver.rb +496 -462
  46. data/test/cases/connection_test_sqlserver.rb +2 -2
  47. data/test/cases/fetch_test_sqlserver.rb +5 -5
  48. data/test/cases/helper_sqlserver.rb +12 -1
  49. data/test/cases/json_test_sqlserver.rb +6 -6
  50. data/test/cases/migration_test_sqlserver.rb +13 -3
  51. data/test/cases/order_test_sqlserver.rb +19 -19
  52. data/test/cases/pessimistic_locking_test_sqlserver.rb +37 -20
  53. data/test/cases/rake_test_sqlserver.rb +20 -20
  54. data/test/cases/schema_dumper_test_sqlserver.rb +44 -43
  55. data/test/cases/schema_test_sqlserver.rb +2 -2
  56. data/test/cases/showplan_test_sqlserver.rb +25 -10
  57. data/test/cases/specific_schema_test_sqlserver.rb +11 -17
  58. data/test/cases/transaction_test_sqlserver.rb +9 -9
  59. data/test/cases/trigger_test_sqlserver.rb +31 -0
  60. data/test/cases/utils_test_sqlserver.rb +36 -36
  61. data/test/cases/uuid_test_sqlserver.rb +8 -8
  62. data/test/config.yml +2 -2
  63. data/test/migrations/create_clients_and_change_column_null.rb +23 -0
  64. data/test/models/sqlserver/trigger.rb +7 -0
  65. data/test/models/sqlserver/trigger_history.rb +3 -0
  66. data/test/schema/datatypes/2012.sql +1 -0
  67. data/test/schema/sqlserver_specific_schema.rb +47 -5
  68. data/test/support/core_ext/query_cache.rb +29 -0
  69. data/test/support/sql_counter_sqlserver.rb +1 -1
  70. metadata +32 -15
  71. data/RAILS5-TODO.md +0 -5
  72. data/test/models/sqlserver/dot_table_name.rb +0 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d264fa23b69908328c1e4f17fc5b69b2d2de67ed14394ef5127e9767379ccfe9
4
- data.tar.gz: c9fa8ecde64528237123e97bf8ed99a26d03d9d83341e7510f3d67d8de4b2b63
3
+ metadata.gz: c764412410f757d3f4ee0312e3950ba5eb3cd1b3f7e21645e2d081ce4974f7b2
4
+ data.tar.gz: 463f9a3245a8d95ff78d76fd2284a8cacbc64b8510bfb95915d55c2e2b50021f
5
5
  SHA512:
6
- metadata.gz: ce260ce665bbde6fabeacef58e7b7a4d417e1b453d46c440942c0dba50caf0acbe764932cfa23fc45cb468753e9cd681e88be58f30f1b8ff7e6830c5404eff6b
7
- data.tar.gz: c17157b072dae81728644a308b89dc401e623d277db94be9f246f5a1b986e63becec559decb4af2a504de961213dc1c026d61a38cf0f908dc1157768523d01b5
6
+ metadata.gz: 703e10527b9d65e5e237dde6b5cdc381cb2f398f8fcd3cad780f64567694c193e9922ac58f5b4e591611157cb480ed405c75093d314e535c00210ad2ccbaeb99
7
+ data.tar.gz: f229972c054e17396b5aa1f6f554ae0a661a4e8d69ca9cb8e9ed52e4c7c3afef0f2d0eeb3e217f3e8b9c0323140329c16aa1e0986e2bc2b57da7817524a0232e
data/.gitignore CHANGED
@@ -9,7 +9,9 @@ Gemfile.lock
9
9
  test/profile/output/*
10
10
  .rvmrc
11
11
  .rbenv-version
12
+ .tool-versions
12
13
  .idea
13
14
  coverage/*
14
15
  .flooignore
15
- .floo
16
+ .floo
17
+ .byebug_history
@@ -4,8 +4,8 @@ services:
4
4
  - docker
5
5
  env:
6
6
  global:
7
- - COMPOSE_FILE=docker-compose.ci.yml
8
- #- ARJDBC_BRANCH=mssql-support
7
+ - COMPOSE_FILE: docker-compose.ci.yml
8
+ - ARJDBC_BRANCH: 52-stable
9
9
  matrix:
10
10
  - TARGET_VERSION=9.1
11
11
  - TARGET_VERSION=9.2
@@ -17,11 +17,10 @@ before_install:
17
17
  - sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
18
18
  - sudo chmod +x /usr/local/bin/docker-compose
19
19
  install:
20
- - docker-compose build --build-arg TARGET_VERSION=$TARGET_VERSION-jdk-alpine
21
- #- docker-compose build --build-arg TARGET_VERSION=$TARGET_VERSION-jdk-alpine --build-arg TARGET_ARJDBC_BRANCH=$ARJDBC_BRANCH
20
+ #- docker-compose build --build-arg TARGET_VERSION=$TARGET_VERSION-jdk-alpine
21
+ - docker-compose build --build-arg TARGET_VERSION=$TARGET_VERSION-jdk-alpine --build-arg TARGET_ARJDBC_BRANCH=$ARJDBC_BRANCH
22
22
  script:
23
23
  - docker-compose run ci
24
24
  matrix:
25
25
  allow_failures:
26
26
  - env: TARGET_VERSION=9.1
27
-
@@ -1,124 +1,45 @@
1
- ## v50.0.0
2
1
 
3
- * Forked original gem to provide JRuby support
2
+ ## v52.0.0
4
3
 
5
- ## v5.0.8
4
+ - Forked to support JRuby
6
5
 
7
- * Memorized SQL Server Version to prevent a flood of SELECT @@version queries. Fixes #632
8
-
9
- ## v5.0.7
10
-
11
- * Add `WITH NO_INFOMSGS` to `user_options` method. Fixes #580
12
-
13
-
14
- ## v5.0.6
6
+ ## v5.2.1
15
7
 
16
8
  #### Fixed
17
9
 
18
- * Performance w/inserts. Check binds & use schema cache for id inserts.
19
- Fixes #572. Thanks @noelr.
20
- * Add smalldatetime type for migrations. Fixes #507
21
-
22
- #### Changed
23
-
24
- * Misc index enhancements or testing. Fixes #570
25
- Enable `supports_index_sort_order?`, test `supports_partial_index?`, test how expression indexes work.
26
-
27
- #### Added
28
-
29
- * New `primary_key_nonclustered` type for easy In-Memory table creation.
30
- * Examples for an In-Memory table.
31
-
32
- ```ruby
33
- create_table :in_memory_table, id: false,
34
- options: 'WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA)' do |t|
35
- t.primary_key_nonclustered :id
36
- t.string :name
37
- t.timestamps
38
- end
39
- ```
40
-
41
- * Enable supports_json? Fixes #577.
42
-
43
- ```ruby
44
- create_table :users do |t|
45
- t.string :name, :email
46
- t.json :data # Creates a nvarchar(max) column.
47
- end
48
-
49
- class Users < ActiveRecord::Base
50
- attribute :data, ActiveRecord::Type::SQLServer::Json.new
51
- end
52
-
53
- User.create! name: 'Ken Collins', data: { 'admin' => true, 'foo' => 'bar' }
54
-
55
- admin = User.where("JSON_VALUE(data, '$.admin') = CAST(1 AS BIT)").first
56
- admin.data['foo'] # => "bar"
57
- ```
58
-
59
-
60
- ## v5.0.5
61
-
62
- #### Changed
63
-
64
- * Add TinyTDS as a runtime dependency.
10
+ - [#691](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/691) Fix constraints bug
11
+ - [#700](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/700) SET SINGLE_USER before dropping the database
12
+ - [#733](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/733) Calculate should not remove ordering for MSSQL
13
+ - [#735](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/735) Order by selected items when using distinct exists
14
+ - [#737](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/737) Use default precision for 'time' column type
15
+ - [#744](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/744) Adapter does not use prepared statement cache
16
+ - [#743](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/743) Set default time precision when registering time type
17
+ - [#745](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/745) Quoted table names containing square brackets need to be regex escaped
65
18
 
66
-
67
- ## v5.0.4
19
+ ## v5.2.0
68
20
 
69
21
  #### Fixed
70
22
 
71
- * Allow `datetimeoffset` to be used in migrations and represented in schema.
72
- * Using transactions and resetting isolation level correctly when `READ_COMMITTED_SNAPSHOT` is set to `ON` Fixes #520
73
-
74
-
75
- ## v5.0.3
76
-
77
- #### Changed
78
-
79
- * Reduce view information reflection to per table vs. column. Fixes #552
80
- * The `user_options` parsing. Works for hash/array. Fixes #535
81
- * Pass the `:contained` option to TinyTDS. Fixes #527
82
-
23
+ - [#686](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/686) sql_for_insert set table name in case when pk is not nil
83
24
 
84
- ## v5.0.2
25
+ ## v5.2.0.rc2
85
26
 
86
27
  #### Fixed
87
28
 
88
- * Filter table constraints with matching table schema to column. Fixes #478
89
-
90
-
91
- ## v5.0.1
92
-
93
- #### Changed
29
+ - [#681](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/681) change_column_null should not clear other column attributes. Fixes #582.
30
+ - [#684](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/684) Fix explain with array conditions. Fixes #673.
94
31
 
95
- * Set `tds_version` fallback to `7.3`.
32
+ ## v5.2.0.rc1
96
33
 
97
34
  #### Fixed
98
35
 
99
- * Support 2014, 2012 drop table statement.
100
-
101
-
102
- ## v5.0.0
103
-
104
- #### Added
105
-
106
- * Support for `supports_datetime_with_precision`.
107
- * Support for `unprepared_statement` blocks on the connection.
36
+ - [#638](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/638) Don't disable referential integrity for the same table twice.
37
+ - [#646](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/646) Make String equality check work for Type::Data values. Fixes #645.
38
+ - [#671](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/671) Fix tinyint columns schema migration. Fixes #670.
108
39
 
109
40
  #### Changed
110
41
 
111
- * Major refactoring of all type objects. Especially time types.
42
+ - [#642](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/642) Added with (nolock) hint to information_schema.views.
112
43
 
113
- #### Deprecated
114
-
115
- * Support for a handful of standard Rails deprecations in 5-0-stable suite.
116
-
117
- #### Removed
118
-
119
- * ODBC connection mode. Not been maintained since Rails 4.0.
120
- * View table name detection in `with_identity_insert_enabled` method for fixtures. Perf hit.
121
-
122
- #### Fixed
123
44
 
124
- * Do not output column collation in schema when same as database.
45
+ Please check [5-1-stable-jdbc](https://github.com/jruby/activerecord-jdbcsqlserver-adapter/blob/5-1-stable-jdbc/CHANGELOG.md) for previous changes.
File without changes
data/Gemfile CHANGED
@@ -2,9 +2,7 @@ require 'openssl'
2
2
  source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
- gem 'rb-readline', platform: :mri
6
- gem 'sqlite3', platform: :mri
7
- gem 'minitest', '< 5.3.4'
5
+ gem 'sqlite3', '~> 1.3.6', platform: :mri
8
6
  gem 'bcrypt'
9
7
  gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
10
8
 
data/README.md CHANGED
@@ -1,19 +1,16 @@
1
- # ActiveRecord SQL Server Adapter. For SQL Server 2012 And Higher.
1
+ # ActiveRecord JDBC SQL Server Adapter. For SQL Server 2012 And Higher.
2
2
 
3
3
  [![Gem Version](http://img.shields.io/gem/v/activerecord-jdbcsqlserver-adapter.svg)](https://rubygems.org/gems/activerecord-jdbcsqlserver-adapter) - Gem Version
4
4
 
5
+ ## About The Adapter
5
6
 
6
- The JDBC SQL Server adapter for ActiveRecord v5.0 using SQL Server 2012 or higher.
7
+ The JDBC SQL Server adapter for ActiveRecord v5.2 using SQL Server 2012 or higher.
7
8
 
8
9
  We currently support JRuby 9.2+. Older versions may work but they are not tested and there is a known date issue with JRuby 9.1.
9
10
 
10
11
  For older versions see [activerecord-jdbcmssql-adapter](https://rubygems.org/gems/activerecord-jdbcmssql-adapter)
11
12
 
12
13
 
13
- #### Native Data Type Support
14
-
15
- The Rails v5 adapter supports ActiveRecord's `datetime_with_precision` setting. This means that passing `:precision` to a datetime column is supported. Using a pecision with the `:datetime` type will signal the adapter to use the `datetime2` type under the hood.
16
-
17
14
  #### Schemas & Users
18
15
 
19
16
  Depending on your user and schema setup, it may be needed to use a table name prefix of `dbo.`. So something like this in your initializer file for ActiveRecord or the adapter.
@@ -49,7 +46,7 @@ gem 'activerecord-jdbcsqlserver-adapter'
49
46
 
50
47
  ## Contributing
51
48
 
52
- If you would 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 Gitter, 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.
49
+ If you would 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 Gitter, 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`](RUNNING_UNIT_TESTS.md) file for the details of how to run the unit tests.
53
50
 
54
51
  This is a fork of the activerecord-sqlserver-adapter. If you find a bug in the base code, please submit patches to: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
55
52
 
@@ -61,7 +58,7 @@ If the bug/feature is JRuby or specific to this gem, please submit patches to: h
61
58
  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.
62
59
 
63
60
 
64
- ## Contributers
61
+ ## Contributors
65
62
 
66
63
  Up-to-date list of contributors: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter/contributors
67
64
 
@@ -90,4 +87,3 @@ JDBC version of the gem:
90
87
  ## License
91
88
 
92
89
  Copyright © 2008-2019. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
93
-
data/VERSION CHANGED
@@ -1 +1 @@
1
- 50.0.0
1
+ 52.0.0
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
15
15
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
  spec.require_paths = ['lib']
18
- spec.add_dependency 'activerecord', '~> 5.0.0', '>= 5.0.6'
19
- spec.add_dependency 'activerecord-jdbc-adapter' , '~> 50.5'
18
+ spec.add_dependency 'activerecord', '~> 5.2.0', '>= 5.2.3'
19
+ spec.add_dependency 'activerecord-jdbc-adapter' , '~> 52.7'
20
20
  spec.add_dependency 'jdbc-mssql', '>= 0.6.0'
21
21
  end
@@ -2,7 +2,7 @@ init:
2
2
  - SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
3
3
  - SET PATH=C:\MinGW\msys\1.0\bin;%PATH%
4
4
  - SET RAKEOPT=-rdevkit
5
- - SET TINYTDS_VERSION=1.3.0
5
+ - SET TINYTDS_VERSION=2.1.0
6
6
  clone_depth: 5
7
7
  skip_tags: true
8
8
  matrix:
@@ -1,11 +1,13 @@
1
1
  version: "2.2"
2
2
  services:
3
- database:
3
+ sqlserver:
4
4
  image: metaskills/mssql-server-linux-rails
5
5
  ci:
6
6
  environment:
7
- - ACTIVERECORD_UNITTEST_HOST=database
8
- build: .
9
- command: test/bin/wait-for.sh database:1433 -- bundle exec rake test
7
+ - ACTIVERECORD_UNITTEST_HOST=sqlserver
8
+ build:
9
+ context: .
10
+ dockerfile: Dockerfile.ci
11
+ command: test/bin/wait-for.sh sqlserver:1433 -- bundle exec rake test
10
12
  depends_on:
11
- - "database"
13
+ - "sqlserver"
@@ -24,4 +24,6 @@ module ActiveRecord
24
24
  end
25
25
  end
26
26
 
27
- ActiveRecord::Base.send :include, ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::ActiveRecord
27
+ ActiveSupport.on_load(:active_record) do
28
+ include ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::ActiveRecord
29
+ end
@@ -22,4 +22,6 @@ module ActiveRecord
22
22
  end
23
23
  end
24
24
 
25
- ActiveRecord::Base.send :include, ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::AttributeMethods
25
+ ActiveSupport.on_load(:active_record) do
26
+ include ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::AttributeMethods
27
+ end
@@ -0,0 +1,51 @@
1
+ require 'active_record/relation'
2
+ require 'active_record/version'
3
+
4
+ module ActiveRecord
5
+ module ConnectionAdapters
6
+ module SQLServer
7
+ module CoreExt
8
+ module Calculations
9
+
10
+ # Same as original except we don't perform PostgreSQL hack that removes ordering.
11
+ def calculate(operation, column_name)
12
+ if has_include?(column_name)
13
+ relation = apply_join_dependency
14
+
15
+ if operation.to_s.downcase == "count"
16
+ unless distinct_value || distinct_select?(column_name || select_for_count)
17
+ relation.distinct!
18
+ relation.select_values = [ klass.primary_key || table[Arel.star] ]
19
+ end
20
+ end
21
+
22
+ relation.calculate(operation, column_name)
23
+ else
24
+ perform_calculation(operation, column_name)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def build_count_subquery(relation, column_name, distinct)
31
+ super(relation.unscope(:order), column_name, distinct)
32
+ end
33
+
34
+ def type_cast_calculated_value(value, type, operation = nil)
35
+ case operation
36
+ when "count" then value.to_i
37
+ when "sum" then type.deserialize(value || 0)
38
+ when "average" then value&.respond_to?(:to_d) ? value.to_d : value
39
+ else type.deserialize(value)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ ActiveSupport.on_load(:active_record) do
49
+ mod = ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Calculations
50
+ ActiveRecord::Relation.prepend(mod)
51
+ end
@@ -5,12 +5,11 @@ module ActiveRecord
5
5
  module Explain
6
6
 
7
7
  SQLSERVER_STATEMENT_PREFIX = 'EXEC sp_executesql '.freeze
8
- SQLSERVER_PARAM_MATCHER = /@\d+ = (.*)/
9
- SQLSERVER_NATIONAL_STRING_MATCHER = /N'(.*)'/m
8
+ SQLSERVER_STATEMENT_REGEXP = /N'(.+)', N'(.+)', (.+)/
10
9
 
11
10
  def exec_explain(queries)
12
11
  unprepared_queries = queries.map do |(sql, binds)|
13
- [unprepare_sqlserver_statement(sql), binds]
12
+ [unprepare_sqlserver_statement(sql, binds), binds]
14
13
  end
15
14
  super(unprepared_queries)
16
15
  end
@@ -19,22 +18,19 @@ module ActiveRecord
19
18
 
20
19
  # This is somewhat hacky, but it should reliably reformat our prepared sql statment
21
20
  # which uses sp_executesql to just the first argument, then unquote it. Likewise our
22
- # `sp_executesql` method should substitude the @n args withe the quoted values.
23
- def unprepare_sqlserver_statement(sql)
24
- if sql.starts_with?(SQLSERVER_STATEMENT_PREFIX)
25
- executesql = sql.from(SQLSERVER_STATEMENT_PREFIX.length)
26
- args = executesql.split(', ')
27
- unprepared_sql = args.shift.strip.match(SQLSERVER_NATIONAL_STRING_MATCHER)[1]
28
- unprepared_sql = Utils.unquote_string(unprepared_sql)
29
- args = args.from(args.length / 2)
30
- args.each_with_index do |arg, index|
31
- value = arg.match(SQLSERVER_PARAM_MATCHER)[1]
32
- unprepared_sql.sub! "@#{index}", value
33
- end
34
- unprepared_sql
35
- else
36
- sql
21
+ # `sp_executesql` method should substitude the @n args with the quoted values.
22
+ def unprepare_sqlserver_statement(sql, binds)
23
+ return sql unless sql.starts_with?(SQLSERVER_STATEMENT_PREFIX)
24
+
25
+ executesql = sql.from(SQLSERVER_STATEMENT_PREFIX.length)
26
+ executesql = executesql.match(SQLSERVER_STATEMENT_REGEXP).to_a[1]
27
+
28
+ binds.each_with_index do |bind, index|
29
+ value = connection.quote(bind)
30
+ executesql = executesql.sub("@#{index}", value)
37
31
  end
32
+
33
+ executesql
38
34
  end
39
35
 
40
36
  end
@@ -43,5 +39,7 @@ module ActiveRecord
43
39
  end
44
40
  end
45
41
 
46
- ActiveRecord::Base.extend ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Explain
47
- ActiveRecord::Relation.send :include, ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Explain
42
+ ActiveSupport.on_load(:active_record) do
43
+ extend ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Explain
44
+ ActiveRecord::Relation.include(ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Explain)
45
+ end
@@ -1,4 +1,6 @@
1
- silence_warnings do
2
- # Already defined in Rails
3
- ActiveRecord::ExplainSubscriber::EXPLAINED_SQLS = /(select|update|delete|insert)\b/i
1
+ ActiveSupport.on_load(:active_record) do
2
+ silence_warnings do
3
+ # Already defined in Rails
4
+ ActiveRecord::ExplainSubscriber::EXPLAINED_SQLS = /(select|update|delete|insert)\b/i
5
+ end
4
6
  end
@@ -0,0 +1,43 @@
1
+ require 'active_record/relation'
2
+ require 'active_record/version'
3
+
4
+ module ActiveRecord
5
+ module ConnectionAdapters
6
+ module SQLServer
7
+ module CoreExt
8
+ module FinderMethods
9
+
10
+ private
11
+
12
+ # Same as original except we order by values in distinct select if present.
13
+ def construct_relation_for_exists(conditions)
14
+ if distinct_value && offset_value
15
+ relation = limit!(1)
16
+
17
+ if select_values.present?
18
+ relation = relation.order(*select_values)
19
+ else
20
+ relation = relation.except(:order)
21
+ end
22
+ else
23
+ relation = except(:select, :distinct, :order)._select!(::ActiveRecord::FinderMethods::ONE_AS_ONE).limit!(1)
24
+ end
25
+
26
+ case conditions
27
+ when Array, Hash
28
+ relation.where!(conditions) unless conditions.empty?
29
+ else
30
+ relation.where!(primary_key => conditions) unless conditions == :none
31
+ end
32
+
33
+ relation
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ ActiveSupport.on_load(:active_record) do
42
+ ActiveRecord::Relation.include(ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::FinderMethods)
43
+ end