activerecord-sqlserver-adapter 3.1.1 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,6 +1,21 @@
1
1
 
2
+ * 3.1.2 *
3
+
4
+ * Fix SQL Azure conflicts with DBCC useroptions. Use new #user_options_xyz methods. [kazamachi]
5
+
6
+ * Fix identity inserts for tables with natural PKs. [Gian Carlo Pace]
7
+
8
+ * Create a #configure_connection method that can be overridden. Think "SET TEXTSIZE...".
9
+
10
+ * Create a #configure_application_name method that can be overridden for unique TinyTDS app names
11
+
12
+ * Fixed the #finish_statement_handle to cancel the TinyTDS connection if needed.
13
+
14
+
2
15
  * 3.1.1 *
3
16
 
17
+ * Make #rollback_db_transaction smarter.
18
+
4
19
  * Provide a method to override for the quoted string prefix. Not a config because trumping this method will
5
20
  have drastically bad results. Fixes #124
6
21
 
@@ -54,7 +54,7 @@ module ActiveRecord
54
54
  end
55
55
 
56
56
  def rollback_db_transaction
57
- do_execute "ROLLBACK TRANSACTION" rescue nil
57
+ do_execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
58
58
  end
59
59
 
60
60
  def create_savepoint
@@ -120,6 +120,7 @@ module ActiveRecord
120
120
  end
121
121
 
122
122
  def user_options
123
+ return {} if sqlserver_azure?
123
124
  info_schema_query do
124
125
  select_rows("dbcc useroptions").inject(HashWithIndifferentAccess.new) do |values,row|
125
126
  set_option = row[0].gsub(/\s+/,'_')
@@ -129,10 +130,45 @@ module ActiveRecord
129
130
  end
130
131
  end
131
132
  end
133
+
134
+ def user_options_dateformat
135
+ if sqlserver_azure?
136
+ info_schema_query { select_value "SELECT [dateformat] FROM [sys].[syslanguages] WHERE [langid] = @@LANGID" }
137
+ else
138
+ user_options['dateformat']
139
+ end
140
+ end
141
+
142
+ def user_options_isolation_level
143
+ if sqlserver_azure?
144
+ info_schema_query do
145
+ sql = %|SELECT CASE [transaction_isolation_level]
146
+ WHEN 0 THEN NULL
147
+ WHEN 1 THEN 'READ UNCOMITTED'
148
+ WHEN 2 THEN 'READ COMITTED'
149
+ WHEN 3 THEN 'REPEATABLE'
150
+ WHEN 4 THEN 'SERIALIZABLE'
151
+ WHEN 5 THEN 'SNAPSHOT' END AS [isolation_level]
152
+ FROM [sys].[dm_exec_sessions]
153
+ WHERE [session_id] = @@SPID|.squish
154
+ select_value(sql)
155
+ end
156
+ else
157
+ user_options['isolation_level']
158
+ end
159
+ end
160
+
161
+ def user_options_language
162
+ if sqlserver_azure?
163
+ info_schema_query { select_value "SELECT @@LANGUAGE AS [language]" }
164
+ else
165
+ user_options['language']
166
+ end
167
+ end
132
168
 
133
169
  def run_with_isolation_level(isolation_level)
134
170
  raise ArgumentError, "Invalid isolation level, #{isolation_level}. Supported levels include #{valid_isolation_levels.to_sentence}." if !valid_isolation_levels.include?(isolation_level.upcase)
135
- initial_isolation_level = user_options[:isolation_level] || "READ COMMITTED"
171
+ initial_isolation_level = user_options_isolation_level || "READ COMMITTED"
136
172
  do_execute "SET TRANSACTION ISOLATION LEVEL #{isolation_level}"
137
173
  begin
138
174
  yield
@@ -337,7 +373,8 @@ module ActiveRecord
337
373
 
338
374
  def finish_statement_handle(handle)
339
375
  case @connection_options[:mode]
340
- when :dblib
376
+ when :dblib
377
+ handle.cancel if handle
341
378
  when :odbc
342
379
  handle.drop if handle && handle.respond_to?(:drop) && !handle.finished?
343
380
  end
@@ -14,6 +14,7 @@ module ActiveRecord
14
14
  end
15
15
 
16
16
  def table_exists?(table_name)
17
+ return false if table_name.blank?
17
18
  unquoted_table_name = unqualify_table_name(table_name)
18
19
  super || tables.include?(unquoted_table_name) || views.include?(unquoted_table_name)
19
20
  end
@@ -362,7 +363,7 @@ module ActiveRecord
362
363
  if insert_sql?(sql)
363
364
  table_name = get_table_name(sql)
364
365
  id_column = identity_column(table_name)
365
- id_column && sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i ? quote_table_name(table_name) : false
366
+ id_column && id_column.is_integer? && sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i ? quote_table_name(table_name) : false
366
367
  else
367
368
  false
368
369
  end
@@ -3,7 +3,7 @@ module ActiveRecord
3
3
  module Sqlserver
4
4
  module Version
5
5
 
6
- VERSION = '3.1.1'.freeze
6
+ VERSION = '3.1.2'.freeze
7
7
 
8
8
  end
9
9
  end
@@ -378,7 +378,7 @@ module ActiveRecord
378
378
  config = @connection_options
379
379
  @connection = case @connection_options[:mode]
380
380
  when :dblib
381
- appname = config[:appname] || Rails.application.class.name.split('::').first rescue nil
381
+ appname = config[:appname] || configure_application_name || Rails.application.class.name.split('::').first rescue nil
382
382
  login_timeout = config[:login_timeout].present? ? config[:login_timeout].to_i : nil
383
383
  timeout = config[:timeout].present? ? config[:timeout].to_i/1000 : nil
384
384
  encoding = config[:encoding].present? ? config[:encoding] : nil
@@ -427,12 +427,26 @@ module ActiveRecord
427
427
  end
428
428
  end
429
429
  end
430
+ configure_connection
430
431
  rescue
431
432
  raise unless @auto_connecting
432
433
  end
433
434
 
435
+ # Override this method so every connection can be configured to your needs.
436
+ # For example:
437
+ # do_execute "SET TEXTSIZE #{64.megabytes}"
438
+ # do_execute "SET CONCAT_NULL_YIELDS_NULL ON"
439
+ def configure_connection
440
+ end
441
+
442
+ # Override this method so every connection can have a unique name. Max 30 characters. Used by TinyTDS only.
443
+ # For example:
444
+ # "myapp_#{$$}_#{Thread.current.object_id}".to(29)
445
+ def configure_application_name
446
+ end
447
+
434
448
  def initialize_dateformatter
435
- @database_dateformat = user_options['dateformat']
449
+ @database_dateformat = user_options_dateformat
436
450
  a, b, c = @database_dateformat.each_char.to_a
437
451
  [a,b,c].each { |f| f.upcase! if f == 'y' }
438
452
  dateformat = "%#{a}-%#{b}-%#{c}"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-sqlserver-adapter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
- - 1
10
- version: 3.1.1
9
+ - 2
10
+ version: 3.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ken Collins
@@ -19,7 +19,7 @@ autorequire:
19
19
  bindir: bin
20
20
  cert_chain: []
21
21
 
22
- date: 2011-09-22 00:00:00 -04:00
22
+ date: 2011-10-24 00:00:00 -04:00
23
23
  default_executable:
24
24
  dependencies:
25
25
  - !ruby/object:Gem::Dependency
@@ -44,12 +44,11 @@ executables: []
44
44
 
45
45
  extensions: []
46
46
 
47
- extra_rdoc_files:
48
- - README.rdoc
47
+ extra_rdoc_files: []
48
+
49
49
  files:
50
50
  - CHANGELOG
51
51
  - MIT-LICENSE
52
- - README.rdoc
53
52
  - lib/active_record/connection_adapters/sqlserver/core_ext/active_record.rb
54
53
  - lib/active_record/connection_adapters/sqlserver/core_ext/odbc.rb
55
54
  - lib/active_record/connection_adapters/sqlserver/database_limits.rb
@@ -66,9 +65,8 @@ homepage: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
66
65
  licenses: []
67
66
 
68
67
  post_install_message:
69
- rdoc_options:
70
- - --main
71
- - README.rdoc
68
+ rdoc_options: []
69
+
72
70
  require_paths:
73
71
  - lib
74
72
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -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
-