Skip to content

Commit d119a7d

Browse files
committed
Misc changes.
* Create a #configure_connection method that can be overridden. Think "SET TEXTSIZE...". * Create a #configure_application_name method that can be overridden for unique TinyTDS app names * Fixed the #finish_statement_handle to cancel the TinyTDS connection if needed. * Update docs to markdown format.
1 parent 65e56c3 commit d119a7d

File tree

11 files changed

+354
-292
lines changed

11 files changed

+354
-292
lines changed

CHANGELOG

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11

2+
* 3.1.2 *
3+
4+
* Create a #configure_connection method that can be overridden. Think "SET TEXTSIZE...".
5+
6+
* Create a #configure_application_name method that can be overridden for unique TinyTDS app names
7+
8+
* Fixed the #finish_statement_handle to cancel the TinyTDS connection if needed.
9+
10+
211
* 3.1.1 *
312

413
* Make #rollback_db_transaction smarter.

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ else
77
spec = eval(File.read('activerecord-sqlserver-adapter.gemspec'))
88
ar_version = spec.dependencies.detect{ |d|d.name == 'activerecord' }.requirement.requirements.first.last.version
99
gem 'rails', :git => "git://github.com/rails/rails.git", :tag => "v#{ar_version}"
10-
gem 'bcrypt-ruby', '~> 3.0.0'
1110
end
1211

1312
if ENV['AREL']
@@ -27,6 +26,7 @@ group :odbc do
2726
end
2827

2928
group :development do
29+
gem 'bcrypt-ruby', '~> 3.0.0'
3030
gem 'rake', '0.9.2'
3131
gem 'mocha', '0.9.8'
3232
gem 'shoulda', '2.10.3'

README.md

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
* Proper interface to configure the connection and TinyTDS app name reported to SQL Server.
10+
* Rails 3.1 prepared statement support leverages cached query plans.
11+
If you use DBLIB/TinyTDS, you must use FreeTDS 0.91 !!!!!
12+
https://github.com/rails-sqlserver/tiny_tds/issues/41
13+
* We now support your native language date/time formats automatically!
14+
* Default unicode datatypes! Disable with #enable_default_unicode_types to false.
15+
* New #lowercase_schema_reflection configuration option for legacy DBs.
16+
* New dblib connection mode using TinyTDS! Default mode too!
17+
18+
19+
#### Testing Rake Tasks Support
20+
21+
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.
22+
23+
http://wiki.github.com/rails-sqlserver/activerecord-sqlserver-adapter/rails-db-rake-tasks
24+
25+
26+
#### Date/Time Data Type Hinting
27+
28+
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 `coerce_sqlserver_date` or `coerce_sqlserver_time` methods that correspond to 'datetime' columns that need to be cast correctly.
29+
30+
```ruby
31+
class Topic < ActiveRecord::Base
32+
coerce_sqlserver_date :last_read
33+
coerce_sqlserver_time :bonus_time
34+
end
35+
```
36+
37+
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.
38+
39+
40+
#### Executing Stored Procedures
41+
42+
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.
43+
44+
```ruby
45+
Account.execute_procedure :update_totals, 'admin', nil, true
46+
```
47+
48+
#### Native Data Type Support
49+
50+
Currently the following custom data types have been tested for schema definitions.
51+
52+
* char
53+
* nchar
54+
* nvarchar
55+
* ntext
56+
* varchar(max)
57+
* nvarchar(max)
58+
59+
For example:
60+
61+
```ruby
62+
create_table :sql_server_custom_types, :force => true do |t|
63+
t.column :ten_code, :char, :limit => 10
64+
t.column :ten_code_utf8, :nchar, :limit => 10
65+
t.column :title_utf8, :nvarchar
66+
t.column :body, :varchar_max # Creates varchar(max)
67+
t.column :body_utf8, :ntext
68+
t.column :body2_utf8, :nvarchar_max # Creates nvarchar(max)
69+
end
70+
```
71+
72+
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.
73+
74+
75+
#### Native Text/String/Binary Data Type Accessor
76+
77+
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.
78+
79+
```ruby
80+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(8000)'
81+
```
82+
83+
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.
84+
85+
```ruby
86+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
87+
```
88+
89+
By default any :binary column created by migrations will create a `varbinary(max)` data type. This too can be set using an initializer.
90+
91+
```ruby
92+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_binary_database_type = 'image'
93+
```
94+
95+
#### Setting Unicode Types As Default
96+
97+
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.
98+
99+
```ruby
100+
# Default
101+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
102+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'nvarchar(max)'
103+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
104+
105+
# Disabled
106+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = false
107+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(max)'
108+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'varchar'
109+
```
110+
111+
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).
112+
113+
114+
#### Force Schema To Lowercase
115+
116+
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.
117+
118+
```ruby
119+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.lowercase_schema_reflection = true
120+
```
121+
122+
123+
#### Schemas & Users
124+
125+
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.
126+
127+
```ruby
128+
ActiveRecord::Base.table_name_prefix = 'dbo.'
129+
```
130+
131+
132+
#### Schema Information Logging
133+
134+
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.
135+
136+
```ruby
137+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.log_info_schema_queries = true
138+
```
139+
140+
141+
#### Auto Connecting
142+
143+
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.
144+
145+
```ruby
146+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.auto_connect = false
147+
```
148+
149+
150+
#### Configure Connection & App Name
151+
152+
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 and 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.
153+
154+
```ruby
155+
module ActiveRecord
156+
class Base
157+
module ConnectionAdapters
158+
class SQLServerAdapter < AbstractAdapter
159+
160+
def configure_connection
161+
do_execute "SET TEXTSIZE #{64.megabytes}"
162+
end
163+
164+
def configure_application_name
165+
"myapp_#{$$}_#{Thread.current.object_id}".to(29)
166+
end
167+
168+
end
169+
end
170+
end
171+
end
172+
```
173+
174+
175+
## Versions
176+
177+
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.
178+
179+
180+
## Installation
181+
182+
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.
183+
184+
```ruby
185+
gem 'tiny_tds'
186+
gem 'activerecord-sqlserver-adapter', '~> 3.1.0'
187+
```
188+
189+
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!
190+
191+
http://wiki.github.com/rails-sqlserver/activerecord-sqlserver-adapter/platform-installation
192+
193+
194+
195+
## Contributing
196+
197+
If you wouldd 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.
198+
199+
* Github: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
200+
* Google Group: http://groups.google.com/group/rails-sqlserver-adapter
201+
* IRC Room: #rails-sqlserver on irc.freenode.net
202+
203+
204+
## Credits & Contributions
205+
206+
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.
207+
208+
209+
## Contributers
210+
Up-to-date list of contributors: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter/contributors
211+
212+
* metaskills (Ken Collins)
213+
* h-lame (Murray Steele)
214+
* vegantech
215+
* cjheath (Clifford Heath)
216+
* jrafanie (Joe Rafaniello)
217+
* nerdrew (Andrew Ryan)
218+
* snowblink (Jonathan Lim)
219+
* koppen (Jakob Skjerning)
220+
* ebryn (Erik Bryn)
221+
* adzap (Adam Meehan)
222+
* neomindryan (Ryan Findley)
223+
* jeremydurham (Jeremy Durham)
224+
225+
226+
## Donators
227+
228+
I am trying to save up for a Happy Hacking pro keyboard. Help me out! http://pledgie.com/campaigns/15531
229+
230+
231+
## License
232+
233+
Copyright © 2008-2011. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
234+

0 commit comments

Comments
 (0)