activerecord-sqlserver-adapter 6.0.2 → 6.1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -56
  3. data/README.md +28 -11
  4. data/VERSION +1 -1
  5. data/activerecord-sqlserver-adapter.gemspec +1 -1
  6. data/lib/active_record/connection_adapters/sqlserver/core_ext/attribute_methods.rb +2 -0
  7. data/lib/active_record/connection_adapters/sqlserver/core_ext/calculations.rb +5 -10
  8. data/lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb +9 -2
  9. data/lib/active_record/connection_adapters/sqlserver/core_ext/finder_methods.rb +2 -0
  10. data/lib/active_record/connection_adapters/sqlserver/core_ext/preloader.rb +2 -0
  11. data/lib/active_record/connection_adapters/sqlserver/database_limits.rb +0 -4
  12. data/lib/active_record/connection_adapters/sqlserver/database_statements.rb +27 -15
  13. data/lib/active_record/connection_adapters/sqlserver/quoting.rb +4 -3
  14. data/lib/active_record/connection_adapters/sqlserver/schema_creation.rb +22 -1
  15. data/lib/active_record/connection_adapters/sqlserver/schema_dumper.rb +9 -3
  16. data/lib/active_record/connection_adapters/sqlserver/schema_statements.rb +8 -6
  17. data/lib/active_record/connection_adapters/sqlserver/sql_type_metadata.rb +36 -7
  18. data/lib/active_record/connection_adapters/sqlserver/table_definition.rb +0 -1
  19. data/lib/active_record/connection_adapters/sqlserver/transaction.rb +2 -2
  20. data/lib/active_record/connection_adapters/sqlserver/type/date.rb +2 -1
  21. data/lib/active_record/connection_adapters/sqlserver/utils.rb +1 -1
  22. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +100 -70
  23. data/lib/active_record/connection_adapters/sqlserver_column.rb +75 -19
  24. data/lib/active_record/sqlserver_base.rb +9 -15
  25. data/lib/active_record/tasks/sqlserver_database_tasks.rb +17 -14
  26. data/lib/arel/visitors/sqlserver.rb +74 -29
  27. data/test/cases/adapter_test_sqlserver.rb +27 -17
  28. data/test/cases/change_column_collation_test_sqlserver.rb +33 -0
  29. data/test/cases/coerced_tests.rb +544 -77
  30. data/test/cases/column_test_sqlserver.rb +4 -0
  31. data/test/cases/disconnected_test_sqlserver.rb +39 -0
  32. data/test/cases/execute_procedure_test_sqlserver.rb +9 -0
  33. data/test/cases/fetch_test_sqlserver.rb +18 -0
  34. data/test/cases/in_clause_test_sqlserver.rb +27 -0
  35. data/test/cases/migration_test_sqlserver.rb +7 -0
  36. data/test/cases/order_test_sqlserver.rb +7 -0
  37. data/test/cases/primary_keys_test_sqlserver.rb +103 -0
  38. data/test/cases/rake_test_sqlserver.rb +38 -2
  39. data/test/cases/schema_dumper_test_sqlserver.rb +9 -0
  40. data/test/migrations/create_clients_and_change_column_collation.rb +19 -0
  41. data/test/models/sqlserver/composite_pk.rb +9 -0
  42. data/test/models/sqlserver/sst_string_collation.rb +3 -0
  43. data/test/schema/sqlserver_specific_schema.rb +25 -0
  44. data/test/support/marshal_compatibility_fixtures/SQLServer/rails_6_0_topic.dump +0 -0
  45. data/test/support/marshal_compatibility_fixtures/SQLServer/rails_6_0_topic_associations.dump +0 -0
  46. data/test/support/sql_counter_sqlserver.rb +14 -12
  47. metadata +23 -8
  48. data/lib/active_record/connection_adapters/sqlserver/core_ext/query_methods.rb +0 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6cf80abcc5b46383279819d86c83c095b5aabcf4969e84bdb2ca3e101777b3a
4
- data.tar.gz: e11e7d40bba8a4ce3c2d47e08002c5321f82c25b91fde75caa0f01b37a7f6e70
3
+ metadata.gz: cc687a6f9cd86f8c1f7b62356b266e84b47833c9abc25d6a339e387122ccbe4b
4
+ data.tar.gz: 7b1cf7d3f5caa773943a722da4f39fe69435dbd03ce451c540e855f87642e640
5
5
  SHA512:
6
- metadata.gz: b6f04caed7f5cc895fc185e8976d306901dc0563152b8867f40be0319f8572099495ef27bc205aad7733c8aac0e4baf451769cdc46719c2db248f50a802be324
7
- data.tar.gz: 3c8f05837c093f0d44909d1a26bbc97010f8dc5ab07cd5d48bba94d09624c5fe2e5f21488915931c528207eb726860ae0d59b8ca229f353efe0846ff16f7a955
6
+ metadata.gz: cfa048c171f318b7b4e46662b305e04a87e923c42cd1729f5d62bcd6c3e4e8cbbdacec56b767f537becb51c64d162cf556b1b9f69637732b346c37c823833393
7
+ data.tar.gz: e4da6b9fa4c35f21afba43390d790097481dbe321c5c808d3be586eaec745d98e9af8ac10b394990266e733661f65081b12e10b958a64e457609910de4379205
data/CHANGELOG.md CHANGED
@@ -1,78 +1,55 @@
1
- ## v6.0.2
1
+ ## v6.1.2.0
2
+
3
+ [Full changelog](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/compare/v6.1.1.0...v6.1.2.0)
2
4
 
3
5
  #### Fixed
4
6
 
5
- - [#858](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/858) Allow table existence to be tested across database schemas.
7
+ - [#940](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/940) Primary key violation should result in RecordNotUnique error
6
8
 
7
9
  #### Changed
8
10
 
9
- - [#852](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/852) Updated the column name matchers to accept database and owner names
10
-
11
- #### Added
11
+ - [#941](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/941) No longer support configuring the application name by overriding the 'configure_application_name' method.
12
12
 
13
- - [#855](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/855) Add helpers to create/change/drop a schema.
14
- - [#857](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/857) Included WAITFOR as read query type.
15
- - [#865](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/865) Implemented optimizer hints.
16
- - [#845](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/845) Add support for lateral using CROSS/OUTER APPLY.
17
- - [#870](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/870) Added DecimalWithoutScale type
13
+ ## v6.1.1.0
18
14
 
19
- ## v6.0.1
15
+ [Full changelog](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/compare/v6.1.0.0...v6.1.1.0)
20
16
 
21
17
  #### Fixed
22
18
 
23
- - [#851](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/851) Updated 'column_definitions_sql' to ensure that only primary key key constraints are queried for
24
-
25
- ## v6.0.0
26
-
27
- **No Changes**
28
-
29
- ## v6.0.0.rc2
19
+ - [#933](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/933) Conditionally apply SQL Server monkey patches to ActiveRecord so that it is safe to use this gem alongside other database adapters (e.g. PostgreSQL) in a multi-database Rails app
20
+ - [#935](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/935) Fix schema cache generation
21
+ (**breaking change**)
22
+ - [#936](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/936) Fix deteministic fetch when table has a composite primary key
23
+ - [#938](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/938) Fix date columns serialization for range values
30
24
 
31
- #### Fixed
32
-
33
- - [#639](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/639) Primary key should be lowercase if schema forced to lowercase
34
- - [#720](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/720) quoted_date doesn't work for Type::DateTime
25
+ ## v6.1.0.0
35
26
 
36
- #### Changed
27
+ - No changes
37
28
 
38
- - [#826](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/826) Rubocop: Enable Style/StringLiterals cop
39
- - [#827](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/827) Rubocop: Enable Layout/EmptyLinesAroundClassBody cop
40
- - [#828](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/828) Rubocop: Enable Layout/EmptyLines cop
41
- - [#829](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/829) Rubocop: Enable Layout/Layout/EmptyLinesAround* cops
42
- - [#830](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/830) Rubocop: Enable Layout/IndentationWidth and Layout/TrailingWhitespace cops
43
- - [#831](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/831) Rubocop: Enable Spacing cops
44
- - [#832](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/832) Rubocop: Enable Bundler cops
45
- - [#833](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/833) Rubocop: Enable Layout/* cops
46
- - [#834](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/834) Rubocop: Enable Lint/UselessAssignment cop
47
- - [#835](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/835) Rubocop: Configure Naming cops
29
+ ## v6.1.0.0.rc1
48
30
 
49
- ## v6.0.0.rc1
31
+ [Full changelog](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/compare/6-0-stable...v6.1.0.0.rc1)
50
32
 
51
33
  #### Fixed
52
34
 
53
- - [#690](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/690) Rails 6 support
54
- - [#805](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/805) Rails 6: Fix database tasks tests for SQL Server
55
- - [#807](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/807) Rails 6: Skip binary fixtures test on Windows
56
- - [#809](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/809) Rails 6: Coerce reaper test using fork
57
- - [#810](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/810) Rails 6: Fix randomly failing tests due to schema load
58
- - [#812](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/812) Rails 6: Coerce ReloadModelsTest test on Windows
59
- - [#818](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/818) Handle false return by TinyTDS if connection fails and fixed CI
60
- - [#819](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/819) Fix Ruby 2.7 kwargs warnings
61
- - [#825](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/825) Adjust error message when connection is dead
35
+ - [#872](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/872) Use native String#start_with
36
+ - [#876](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/876) Use native String#end_with
37
+ - [#873](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/873) Various fixes to get the tests running for Rails 6.1
38
+ - [#874](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/874) Deduplicate schema cache structures
39
+ - [#875](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/875) Handle default boolean column values when deduplicating
40
+ - [#879](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/879) Added visit method for HomogeneousIn
41
+ - [#880](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/880) Handle any default column class when deduplicating
42
+ - [#861](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/861) Fix Rails 6.1 database config
43
+ - [#890](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/890) Fix removal of invalid ordering from select statements
44
+ - [#881](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/881) Dump column collation to schema.rb and allow collation changes using column_change
45
+ - [#891](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/891) Add support for if_not_exists to indexes
46
+ - [#892](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/892) Add support for if_exists on remove_column
47
+ - [#883](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/885) Fix quoting of ActiveRecord::Relation::QueryAttribute and ActiveModel::Attributes
48
+ - [#893](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/893) Add Active Record Marshal forward compatibility tests
49
+ - [#903](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/903) Raise ActiveRecord::ConnectionNotEstablished on calls to execute with a disconnected connection
62
50
 
63
51
  #### Changed
64
52
 
65
- - [#716](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/716) Translate the connection timed out error
66
- - [#763](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/763) Refactor columns introspection query to make it faster
67
- - [#783](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/783) Update test matrix
68
- - [#820](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/820) Enable frozen strings for tests
69
- - [#821](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/821) Enable frozen strings - part 1
70
- - [#822](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/822) Enable frozen strings - part 2
71
- - [#823](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/823) Enable frozen strings - final
72
- - [#824](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/824) Tidy up Gemfile
73
-
74
- #### Added
75
-
76
- - [#726](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/726) How to Develop ActiveRecord SQL Server Adapter with Pre-Installed MS SQL
53
+ - [#917](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/917) Refactored to use new_client connection pattern
77
54
 
78
- Please check [5-2-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/5-2-stable/CHANGELOG.md) for previous changes.
55
+ Please check [6-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/6-0-stable/CHANGELOG.md) for previous changes.
data/README.md CHANGED
@@ -7,9 +7,20 @@
7
7
 
8
8
  ## About The Adapter
9
9
 
10
- The SQL Server adapter for ActiveRecord v6.0 using SQL Server 2012 or higher.
10
+ The SQL Server adapter for ActiveRecord using SQL Server 2012 or higher.
11
11
 
12
- Interested in older versions? We follow a rational versioning policy that tracks Rails. That means that our 5.2.x version of the adapter is only for the latest 5.2 version of Rails. If you need the adapter for SQL Server 2008 or 2005, you are still in the right spot. Just install the latest 3.2.x to 4.1.x version of the adapter that matches your Rails version. We also have stable branches for each major/minor release of ActiveRecord.
12
+ Interested in older versions? We follow a rational versioning policy that tracks Rails. That means that our 6.x version of the adapter is only for the latest 6.x version of Rails. If you need the adapter for SQL Server 2008 or 2005, you are still in the right spot. Just install the latest 3.2.x to 4.1.x version of the adapter that matches your Rails version. We also have stable branches for each major/minor release of ActiveRecord.
13
+
14
+ | Adapter Version | Rails Version | Support |
15
+ | --------------- | ------------- | ------------------------------------------------------------------------------------------- |
16
+ | `6.1.2.0` | `6.1.x` | [active](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/main) |
17
+ | `6.0.2` | `6.0.x` | [active](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/6-0-stable) |
18
+ | `5.2.1` | `5.2.x` | [active](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/5-2-stable) |
19
+ | `5.1.6` | `5.1.x` | [ended](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/5-1-stable) |
20
+ | `4.2.18` | `4.2.x` | [ended](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/4-2-stable) |
21
+ | `4.1.8` | `4.1.x` | [ended](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/4-1-stable) |
22
+
23
+ For older versions, please check their stable branches.
13
24
 
14
25
  #### Native Data Type Support
15
26
 
@@ -71,34 +82,40 @@ end
71
82
  ```
72
83
 
73
84
 
74
- #### Configure Connection & App Name
85
+ #### Configure Connection
75
86
 
76
- We currently conform to an unpublished and non-standard AbstractAdapter interface to configure connections made to the database. To do so, just override the `configure_connection` method in an initializer like so. In this case below we are setting the `TEXTSIZE` to 64 megabytes. Also, TinyTDS supports an application name when it logs into SQL Server. This can be used to identify the connection in SQL Server's activity monitor. By default it will use the `appname` from your database.yml file or a lowercased version of your Rails::Application name. It is now possible to define a `configure_application_name` method that can give you per instance details. Below shows how you might use this to get the process id and thread id of the current connection.
87
+ We currently conform to an unpublished and non-standard AbstractAdapter interface to configure connections made to the database. To do so, just override the `configure_connection` method in an initializer like so. In this case below we are setting the `TEXTSIZE` to 64 megabytes.
77
88
 
78
89
  ```ruby
79
90
  module ActiveRecord
80
91
  module ConnectionAdapters
81
92
  class SQLServerAdapter < AbstractAdapter
82
-
83
93
  def configure_connection
84
94
  raw_connection_do "SET TEXTSIZE #{64.megabytes}"
85
95
  end
86
-
87
- def configure_application_name
88
- "myapp_#{$$}_#{Thread.current.object_id}".to(29)
89
- end
90
-
91
96
  end
92
97
  end
93
98
  end
94
99
  ```
95
100
 
101
+ #### Configure Application Name
102
+
103
+ TinyTDS supports an application name when it logs into SQL Server. This can be used to identify the connection in SQL Server's activity monitor. By default it will use the `appname` from your database.yml file or your Rails::Application name.
104
+
105
+ Below shows how you might use the database.yml file to use the process ID in your application name.
106
+
107
+ ```yaml
108
+ development:
109
+ adapter: sqlserver
110
+ appname: <%= myapp_#{Process.pid} %>
111
+ ```
112
+
96
113
  #### Executing Stored Procedures
97
114
 
98
115
  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:
99
116
 
100
117
  ```ruby
101
- Account.execute_procedure(:update_totals, 'admin', nil, true
118
+ Account.execute_procedure(:update_totals, 'admin', nil, true)
102
119
  # Or with named parameters.
103
120
  Account.execute_procedure(:update_totals, named: 'params')
104
121
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 6.0.2
1
+ 6.1.2.0
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- spec.add_dependency "activerecord", "~> 6.0.0"
30
+ spec.add_dependency "activerecord", "~> 6.1.0"
31
31
  spec.add_dependency "tiny_tds"
32
32
  end
@@ -10,6 +10,8 @@ module ActiveRecord
10
10
  private
11
11
 
12
12
  def attributes_for_update(attribute_names)
13
+ return super unless self.class.connection.adapter_name == "SQLServer"
14
+
13
15
  super.reject do |name|
14
16
  column = self.class.columns_hash[name]
15
17
  column && column.respond_to?(:is_identity?) && column.is_identity?
@@ -10,6 +10,8 @@ module ActiveRecord
10
10
  module Calculations
11
11
  # Same as original except we don't perform PostgreSQL hack that removes ordering.
12
12
  def calculate(operation, column_name)
13
+ return super unless klass.connection.adapter_name == "SQLServer"
14
+
13
15
  if has_include?(column_name)
14
16
  relation = apply_join_dependency
15
17
 
@@ -29,16 +31,9 @@ module ActiveRecord
29
31
  private
30
32
 
31
33
  def build_count_subquery(relation, column_name, distinct)
32
- super(relation.unscope(:order), column_name, distinct)
33
- end
34
+ return super unless klass.connection.adapter_name == "SQLServer"
34
35
 
35
- def type_cast_calculated_value(value, type, operation = nil)
36
- case operation
37
- when "count" then value.to_i
38
- when "sum" then type.deserialize(value || 0)
39
- when "average" then value&.respond_to?(:to_d) ? value.to_d : value
40
- else type.deserialize(value)
41
- end
36
+ super(relation.unscope(:order), column_name, distinct)
42
37
  end
43
38
  end
44
39
  end
@@ -48,5 +43,5 @@ end
48
43
 
49
44
  ActiveSupport.on_load(:active_record) do
50
45
  mod = ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Calculations
51
- ActiveRecord::Relation.prepend(mod)
46
+ ActiveRecord::Relation.include(mod)
52
47
  end
@@ -9,6 +9,8 @@ module ActiveRecord
9
9
  SQLSERVER_STATEMENT_REGEXP = /N'(.+)', N'(.+)', (.+)/
10
10
 
11
11
  def exec_explain(queries)
12
+ return super unless connection.adapter_name == "SQLServer"
13
+
12
14
  unprepared_queries = queries.map do |(sql, binds)|
13
15
  [unprepare_sqlserver_statement(sql, binds), binds]
14
16
  end
@@ -21,13 +23,18 @@ module ActiveRecord
21
23
  # which uses sp_executesql to just the first argument, then unquote it. Likewise our
22
24
  # `sp_executesql` method should substitude the @n args with the quoted values.
23
25
  def unprepare_sqlserver_statement(sql, binds)
24
- return sql unless sql.starts_with?(SQLSERVER_STATEMENT_PREFIX)
26
+ return sql unless sql.start_with?(SQLSERVER_STATEMENT_PREFIX)
25
27
 
26
28
  executesql = sql.from(SQLSERVER_STATEMENT_PREFIX.length)
27
29
  executesql = executesql.match(SQLSERVER_STATEMENT_REGEXP).to_a[1]
28
30
 
29
31
  binds.each_with_index do |bind, index|
30
- value = connection.quote(bind)
32
+
33
+ value = if bind.is_a?(::ActiveModel::Attribute) then
34
+ connection.quote(bind.value_for_database)
35
+ else
36
+ connection.quote(bind)
37
+ end
31
38
  executesql = executesql.sub("@#{index}", value)
32
39
  end
33
40
 
@@ -12,6 +12,8 @@ module ActiveRecord
12
12
 
13
13
  # Same as original except we order by values in distinct select if present.
14
14
  def construct_relation_for_exists(conditions)
15
+ return super unless klass.connection.adapter_name == "SQLServer"
16
+
15
17
  conditions = sanitize_forbidden_attributes(conditions)
16
18
 
17
19
  if distinct_value && offset_value
@@ -10,6 +10,8 @@ module ActiveRecord
10
10
  private
11
11
 
12
12
  def records_for(ids)
13
+ return super unless klass.connection.adapter_name == "SQLServer"
14
+
13
15
  ids.each_slice(in_clause_length).flat_map do |slice|
14
16
  scope.where(association_key_name => slice).load do |record|
15
17
  # Processing only the first owner
@@ -37,10 +37,6 @@ module ActiveRecord
37
37
  end
38
38
  deprecate :columns_per_multicolumn_index
39
39
 
40
- def in_clause_length
41
- 10_000
42
- end
43
-
44
40
  def sql_query_length
45
41
  65_536 * 4_096
46
42
  end
@@ -17,6 +17,7 @@ module ActiveRecord
17
17
  end
18
18
 
19
19
  materialize_transactions
20
+ mark_transaction_written_if_write(sql)
20
21
 
21
22
  if id_insert_table_name = query_requires_identity_insert?(sql)
22
23
  with_identity_insert_enabled(id_insert_table_name) { do_execute(sql, name) }
@@ -31,6 +32,7 @@ module ActiveRecord
31
32
  end
32
33
 
33
34
  materialize_transactions
35
+ mark_transaction_written_if_write(sql)
34
36
 
35
37
  sp_executesql(sql, name, binds, prepare: prepare)
36
38
  end
@@ -54,7 +56,7 @@ module ActiveRecord
54
56
  end
55
57
 
56
58
  def begin_db_transaction
57
- do_execute "BEGIN TRANSACTION"
59
+ do_execute "BEGIN TRANSACTION", "TRANSACTION"
58
60
  end
59
61
 
60
62
  def transaction_isolation_levels
@@ -67,25 +69,25 @@ module ActiveRecord
67
69
  end
68
70
 
69
71
  def set_transaction_isolation_level(isolation_level)
70
- do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}"
72
+ do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}", "TRANSACTION"
71
73
  end
72
74
 
73
75
  def commit_db_transaction
74
- do_execute "COMMIT TRANSACTION"
76
+ do_execute "COMMIT TRANSACTION", "TRANSACTION"
75
77
  end
76
78
 
77
79
  def exec_rollback_db_transaction
78
- do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
80
+ do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION", "TRANSACTION"
79
81
  end
80
82
 
81
83
  include Savepoints
82
84
 
83
85
  def create_savepoint(name = current_savepoint_name)
84
- do_execute "SAVE TRANSACTION #{name}"
86
+ do_execute "SAVE TRANSACTION #{name}", "TRANSACTION"
85
87
  end
86
88
 
87
89
  def exec_rollback_to_savepoint(name = current_savepoint_name)
88
- do_execute "ROLLBACK TRANSACTION #{name}"
90
+ do_execute "ROLLBACK TRANSACTION #{name}", "TRANSACTION"
89
91
  end
90
92
 
91
93
  def release_savepoint(name = current_savepoint_name)
@@ -165,7 +167,7 @@ module ActiveRecord
165
167
  log(sql, name) do
166
168
  case @connection_options[:mode]
167
169
  when :dblib
168
- result = @connection.execute(sql)
170
+ result = ensure_established_connection! { dblib_execute(sql) }
169
171
  options = { as: :hash, cache_rows: true, timezone: ActiveRecord::Base.default_timezone || :utc }
170
172
  result.each(options) do |row|
171
173
  r = row.with_indifferent_access
@@ -291,6 +293,7 @@ module ActiveRecord
291
293
 
292
294
  def do_execute(sql, name = "SQL")
293
295
  materialize_transactions
296
+ mark_transaction_written_if_write(sql)
294
297
 
295
298
  log(sql, name) { raw_connection_do(sql) }
296
299
  end
@@ -354,13 +357,7 @@ module ActiveRecord
354
357
  def raw_connection_do(sql)
355
358
  case @connection_options[:mode]
356
359
  when :dblib
357
- result = @connection.execute(sql)
358
-
359
- # TinyTDS returns false instead of raising an exception if connection fails.
360
- # Getting around this by raising an exception ourselves while this PR
361
- # https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
362
- raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
363
-
360
+ result = ensure_established_connection! { dblib_execute(sql) }
364
361
  result.do
365
362
  end
366
363
  ensure
@@ -425,7 +422,7 @@ module ActiveRecord
425
422
  def raw_connection_run(sql)
426
423
  case @connection_options[:mode]
427
424
  when :dblib
428
- @connection.execute(sql)
425
+ ensure_established_connection! { dblib_execute(sql) }
429
426
  end
430
427
  end
431
428
 
@@ -459,6 +456,21 @@ module ActiveRecord
459
456
  end
460
457
  handle
461
458
  end
459
+
460
+ def dblib_execute(sql)
461
+ @connection.execute(sql).tap do |result|
462
+ # TinyTDS returns false instead of raising an exception if connection fails.
463
+ # Getting around this by raising an exception ourselves while this PR
464
+ # https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
465
+ raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
466
+ end
467
+ end
468
+
469
+ def ensure_established_connection!
470
+ raise TinyTds::Error, 'SQL Server client is not connected' unless @connection
471
+
472
+ yield
473
+ end
462
474
  end
463
475
  end
464
476
  end
@@ -10,14 +10,15 @@ module ActiveRecord
10
10
 
11
11
  def fetch_type_metadata(sql_type, sqlserver_options = {})
12
12
  cast_type = lookup_cast_type(sql_type)
13
- SQLServer::SqlTypeMetadata.new(
13
+ simple_type = SqlTypeMetadata.new(
14
14
  sql_type: sql_type,
15
15
  type: cast_type.type,
16
16
  limit: cast_type.limit,
17
17
  precision: cast_type.precision,
18
- scale: cast_type.scale,
19
- sqlserver_options: sqlserver_options
18
+ scale: cast_type.scale
20
19
  )
20
+
21
+ SQLServer::TypeMetadata.new(simple_type, **sqlserver_options)
21
22
  end
22
23
 
23
24
  def quote_string(s)