activerecord-sqlserver-adapter 2.3.7 → 3.2.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +385 -61
  3. data/MIT-LICENSE +1 -1
  4. data/VERSION +1 -0
  5. data/lib/active_record/connection_adapters/sqlserver/core_ext/active_record.rb +42 -0
  6. data/lib/active_record/connection_adapters/sqlserver/core_ext/database_statements.rb +97 -0
  7. data/lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb +41 -0
  8. data/lib/active_record/connection_adapters/sqlserver/core_ext/explain_subscriber.rb +26 -0
  9. data/lib/active_record/connection_adapters/sqlserver/core_ext/odbc.rb +38 -0
  10. data/lib/active_record/connection_adapters/sqlserver/core_ext/relation.rb +19 -0
  11. data/lib/active_record/connection_adapters/sqlserver/database_limits.rb +49 -0
  12. data/lib/active_record/connection_adapters/sqlserver/database_statements.rb +458 -0
  13. data/lib/active_record/connection_adapters/sqlserver/errors.rb +36 -0
  14. data/lib/active_record/connection_adapters/sqlserver/quoting.rb +113 -0
  15. data/lib/active_record/connection_adapters/sqlserver/schema_cache.rb +85 -0
  16. data/lib/active_record/connection_adapters/sqlserver/schema_statements.rb +376 -0
  17. data/lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb +69 -0
  18. data/lib/active_record/connection_adapters/sqlserver/showplan/printer_xml.rb +25 -0
  19. data/lib/active_record/connection_adapters/sqlserver/showplan.rb +67 -0
  20. data/lib/active_record/connection_adapters/sqlserver/utils.rb +32 -0
  21. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +344 -1055
  22. data/lib/arel/visitors/sqlserver.rb +389 -0
  23. metadata +60 -83
  24. data/README.rdoc +0 -190
  25. data/RUNNING_UNIT_TESTS +0 -65
  26. data/Rakefile +0 -41
  27. data/autotest/discover.rb +0 -4
  28. data/autotest/railssqlserver.rb +0 -16
  29. data/autotest/sqlserver.rb +0 -54
  30. data/lib/active_record/connection_adapters/sqlserver_adapter/core_ext/active_record.rb +0 -151
  31. data/lib/active_record/connection_adapters/sqlserver_adapter/core_ext/odbc.rb +0 -40
  32. data/test/cases/aaaa_create_tables_test_sqlserver.rb +0 -19
  33. data/test/cases/adapter_test_sqlserver.rb +0 -756
  34. data/test/cases/attribute_methods_test_sqlserver.rb +0 -33
  35. data/test/cases/basics_test_sqlserver.rb +0 -21
  36. data/test/cases/calculations_test_sqlserver.rb +0 -20
  37. data/test/cases/column_test_sqlserver.rb +0 -285
  38. data/test/cases/connection_test_sqlserver.rb +0 -146
  39. data/test/cases/eager_association_test_sqlserver.rb +0 -42
  40. data/test/cases/execute_procedure_test_sqlserver.rb +0 -44
  41. data/test/cases/inheritance_test_sqlserver.rb +0 -28
  42. data/test/cases/method_scoping_test_sqlserver.rb +0 -28
  43. data/test/cases/migration_test_sqlserver.rb +0 -123
  44. data/test/cases/named_scope_test_sqlserver.rb +0 -21
  45. data/test/cases/offset_and_limit_test_sqlserver.rb +0 -108
  46. data/test/cases/pessimistic_locking_test_sqlserver.rb +0 -125
  47. data/test/cases/query_cache_test_sqlserver.rb +0 -24
  48. data/test/cases/schema_dumper_test_sqlserver.rb +0 -72
  49. data/test/cases/specific_schema_test_sqlserver.rb +0 -97
  50. data/test/cases/sqlserver_helper.rb +0 -127
  51. data/test/cases/table_name_test_sqlserver.rb +0 -38
  52. data/test/cases/transaction_test_sqlserver.rb +0 -93
  53. data/test/cases/unicode_test_sqlserver.rb +0 -50
  54. data/test/cases/validations_test_sqlserver.rb +0 -35
  55. data/test/connections/native_sqlserver/connection.rb +0 -25
  56. data/test/connections/native_sqlserver_odbc/connection.rb +0 -27
  57. data/test/migrations/transaction_table/1_table_will_never_be_created.rb +0 -11
  58. data/test/schema/sqlserver_specific_schema.rb +0 -94
data/README.rdoc DELETED
@@ -1,190 +0,0 @@
1
-
2
- == Rails SQL Server 2000, 2005 and 2008 Adapter
3
-
4
- The SQL Server adapter for rails is back for ActiveRecord 2.2 and up! We are currently passing all tests and hope to continue to do so moving forward.
5
-
6
-
7
- == What's New
8
-
9
- * Integration with rails :db namespaced rake tasks.
10
- * IronRuby support using ADONET connection mode.
11
- * Direct ODBC mode. No DBI anymore, means around 20% faster!
12
- * Now supports SQL Server 2008 too!
13
- * Fully tested under 1.9!!! Correctly encodes/decodes UTF-8 types in ruby 1.9 too.
14
- * Now supports both rails 2.2 & 2.3!!!
15
- * An ActiveRecord::Base.execute_procedure method that can be used by classes.
16
- * Enabled support for DDL transactions.
17
- * Micro second support. Time#usec is automatically converted to SQL Server's 3.33 millisecond limitation.
18
- * Datetime data types before type casting are represented correctly. For example: 1998-01-01 23:59:59.997
19
- * Implementation for #disable_referential_integrity used by ActiveRecord's Fixtures class.
20
- * Pessimistic locking suppot. See the #add_lock! method for details.
21
- * Enabled #case_sensitive_equality_operator used by unique validations.
22
- * Unicode character support for nchar, nvarchar and ntext data types. Configuration option for defaulting all string data types to the unicode safe types.
23
- * View support for table names, identity inserts, and column defaults.
24
- * A block method to run queries within a specific isolation level.
25
- * Automatically reconnects to lost database connections.
26
-
27
-
28
- ==== Testing Rake Tasks Support
29
-
30
- 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.
31
-
32
- http://wiki.github.com/rails-sqlserver/2000-2005-adapter/rails-db-rake-tasks
33
-
34
-
35
- ==== SQL Server 2008 Support
36
-
37
- Because this adapter is primarily coded to SQL Server 2000/2005, it does not take advantage of many of the things in 2008 that would speed up the code. At some point in the future there will be a branch of this code that is specifically targeted for 2008. That adapter version will remove much of the dirty innards of this version that are meant to code around many previous SQL Server 2000/2005 short comings, the biggest being no limit/offset.
38
-
39
-
40
-
41
- ==== Date/Time Data Type Hinting
42
-
43
- Both SQL Server 2000 and 2005 do not include native data types 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 past 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.
44
-
45
- class Topic < ActiveRecord::Base
46
- coerce_sqlserver_date :last_read
47
- coerce_sqlserver_time :bonus_time
48
- end
49
-
50
- 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 convention. So a table of 'foo_bar_widgets' will look for coerced types in the FooBarWidget class if it exists.
51
-
52
- ==== Executing Stored Procedures
53
-
54
- 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.
55
-
56
- Account.execute_procedure :update_totals, 'admin', nil, true
57
-
58
- ==== Native Data Type Support
59
-
60
- Currently the following custom data types have been tested for schema definitions.
61
-
62
- * char
63
- * nchar
64
- * nvarchar
65
- * ntext
66
- * varchar(max) for SQL Server 2005 only.
67
- * nvarchar(max) for SQL Server 2005 only.
68
-
69
- For example:
70
-
71
- create_table :sql_server_custom_types, :force => true do |t|
72
- t.column :ten_code, :char, :limit => 10
73
- t.column :ten_code_utf8, :nchar, :limit => 10
74
- t.column :title_utf8, :nvarchar
75
- t.column :body, :varchar_max # Creates varchar(max)
76
- t.column :body_utf8, :ntext
77
- t.column :body2_utf8, :nvarchar_max # Creates nvarchar(max)
78
- end
79
-
80
- Manually creating a varchar(max) on SQL Server 2005 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.
81
-
82
-
83
- ==== Native Text/String/Binary Data Type Accessor
84
-
85
- 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 these native types.
86
-
87
- * SQL Server 2000 is 'text'
88
- * SQL Server 2005 is 'varchar(max)'
89
-
90
- During testing this type is set to 'varchar(8000)' for SQL Server 2000. The reason is that rails expects to be able to use SQL = operators on text data types and this is not possible with a native 'text' data type in SQL Server. The default 'varchar(max)' for SQL Server 2005 can be queried using the SQL = operator and has plenty of storage space which is why we made it the default for 2005. If for some reason you want to change the data type created during migrations for any SQL Server version, you can configure this line to your liking in a config/initializers file.
91
-
92
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(8000)'
93
-
94
- Also, there is a class attribute setter for the native string database type. This is the same for both SQL Server 2000 and 2005, 'varchar'. However in 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.
95
-
96
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
97
-
98
- By default any :binary column created by migrations will create these native types
99
-
100
- * SQL Server 2000 is 'image'
101
- * SQL Server 2005 is 'varbinary(max)'
102
-
103
-
104
- ==== Setting Unicode Types As Default
105
-
106
- By default the adapter will use non-unicode safe data types for :string and :text types when DEFINING or CHANGING the schema. If you choose, you can set the following class attribute in a config/initializers file that will change this behavior. When set to true it has the equivalent meaning as the two lower items. These examples show detail level alternatives to achieve similar effects.
107
-
108
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
109
-
110
- # SQL Server 2000
111
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'ntext'
112
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
113
-
114
- # SQL Server 2005
115
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'nvarchar(max)'
116
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
117
-
118
- 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).
119
-
120
-
121
- ==== Schema Information Logging
122
-
123
- 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 config/initializers file.
124
-
125
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.log_info_schema_queries = true
126
-
127
- ==== Auto Connecting
128
-
129
- 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.
130
-
131
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.auto_connect = false
132
-
133
-
134
- == Versions
135
-
136
- It is our goal to match the adapter version with each version of rails. However we will track our own tiny version independent of ActiveRecord. For example, an adapter version of 2.2.x will work on any 2.2.x version of ActiveRecord. This convention will be used in both the Git tags as well as the Rubygems versioning.
137
-
138
-
139
- == Installation
140
-
141
- You will need Ruby ODBC. If you are using the adapter under 1.9, then you need at least ruby-odbc version 0.9996. ODBC is the preferred mode, however if you are using IronRuby you can use the ADONET connection mode which uses native System.Data connection. Other connection modes may be supported, possibly a straight FreeTDS layer. The sky is the limit now and we have a code that can be accept these optional transports. If you are interested in helping, open a ticket and submit a patch. Or start a conversation on the Google Group.
142
-
143
- $ gem install activerecord-sqlserver-adapter
144
-
145
- Optionally configure your gem dependencies in your rails environment.rb file.
146
-
147
- config.gem 'activerecord-sqlserver-adapter', :version => 'x.x.xx'
148
-
149
- Here are some external links for libraries and/or tutorials on how to install any other additional components to use this adapter. If you know of a good one that we can include here, just let us know.
150
-
151
- * http://www.ch-werner.de/rubyodbc/
152
-
153
-
154
- == IronRuby ADONET Mode
155
-
156
- A few details on this implementation. All that is needed in your database.yml configuration file is "mode: adonet" vs "odbc" and if you are running IronRuby, the connection will be native. You can also specify an "integrated_security: true" option in your configuration, remember to remove the username/password options too. To use this adapter, you will not need need ANY DBI middle layer or special extension gems to the adapter.
157
-
158
- This adapter is opinionated in regards to IronRuby on types going in and out of the DB. For example strings will be String, not System::String and DateTime vs System::Datetime. There are many more examples but the rule of thumb is that the types will be simple types that correlate to a standard Ruby implementation. We enforce this basic rule because it is necessary to pass the tests and let the framework do its job. We recommend sticking to native Ruby types in your application code too.
159
-
160
- The adapter establishes a System::Data::SqlClient connection that has both MultipleActiveResultSets (MARS) and Pooling turned off. There are good reasons for this one because the connection would not work otherwise for all the code issued by ActiveRecord. Remember too that ActiveRecord has it's own connection pooling and these underlying features like MARS/Pooling work against the adapter code.
161
-
162
- Currently IronRuby is passing most of the ActiveRecord and Adapter tests. Here is a list of the ones remaining. Some are in the adapter's realm and some are in Marshaling area of IronRuby's core to fix. Feel like helping knock these out? Submit a patch to github issues.
163
-
164
- http://gist.github.com/381101
165
-
166
-
167
- == Contributing
168
-
169
- 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.
170
-
171
- * Github: http://github.com/rails-sqlserver
172
- * Google Group: http://groups.google.com/group/rails-sqlserver-adapter
173
- * IRC Room: #rails-sqlserver on irc.freenode.net
174
-
175
-
176
- == Credits
177
-
178
- Many many people have contributed. If you do not see your name here and it should be let us know.
179
-
180
- * Ken Collins
181
- * Murray Steele
182
- * Shawn Balestracci
183
- * Joe Rafaniello
184
- * Tom Ward
185
-
186
-
187
- == License
188
-
189
- Copyright © 2008. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
190
-
data/RUNNING_UNIT_TESTS DELETED
@@ -1,65 +0,0 @@
1
- == Creating the test database
2
-
3
- The default names for the test databases are "activerecord_unittest" and
4
- "activerecord_unittest2". If you want to use another database name then be sure
5
- to update the connection adapter setups you want to test with in
6
- test/connections/<your database>/connection.rb.
7
-
8
- The connection files make certain assumptions. For instance, the ODBC connection
9
- assumes you have a DSN setup that matches the name of the default database names.
10
-
11
-
12
- == Requirements
13
-
14
- The following gems need to be installed. Make sure you have gems.github.com as a
15
- source. Info here. http://gems.github.com/
16
-
17
- We use echoe for packagemanagement to rubyforge. Not needed really for the tests
18
- but since we need it... you need to install it.
19
-
20
- * gem install echoe
21
- * gem install thoughtbot-shoulda -s http://gems.github.com
22
- * gem install mocha
23
-
24
- The tests of this adapter depend on the existence of rails checkout. All the tests
25
- defined by rails are re-used. For this to work the following directory structure
26
- is assumed to exist:
27
-
28
- #{RAILS_ROOT}/vendor/plugins/adapters/sqlserver
29
- #{RAILS_ROOT}/vendor/rails/activerecord/test
30
-
31
- Define a user named 'rails' in SQL Server with all privileges granted for the
32
- test databases. Use an empty password for said user.
33
-
34
-
35
- == Running with Rake
36
-
37
- The easiest way to run the unit tests is through Rake. Either run "rake test" which
38
- defaults to ODBC mode or being mode specific using either "rake sqlserver:test:adonet"
39
- or "rake sqlserver:test:odbc". For more information, checkout the full array of rake tasks
40
- with "rake -T"
41
-
42
- Rake can be found at http://rake.rubyforge.org
43
-
44
-
45
- == Running with Autotest
46
-
47
- Using autotest is easy, just run "autotest" and the tests will run continually in the
48
- same order as the rake test command. By default autotest will use ODBC connection. If
49
- you want to change this you can edit the autotest/sqlserver.rb file and set odbc_mode
50
- to false.
51
-
52
- Lastly, you can run autotest on just the adapter specific tests with "autotest sqlserver".
53
- This will continuously run ONLY the SQL Sever specific behavior tests which are much
54
- quicker to run than the entire active record test suite.
55
-
56
-
57
- == Running by hand
58
-
59
- Unit tests are located in test directory. If you only want to run a single test suite,
60
- you can do so with:
61
-
62
- rake test_sqlserver TEST=base_test.rb
63
-
64
- That'll run the base suite using the SQLServer-Ruby adapter.
65
-
data/Rakefile DELETED
@@ -1,41 +0,0 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
-
6
- namespace :sqlserver do
7
-
8
- namespace :test do
9
-
10
- ['odbc','adonet'].each do |mode|
11
-
12
- Rake::TestTask.new(mode) do |t|
13
- t.libs << "test"
14
- t.libs << "test/connections/native_sqlserver#{mode == 'adonet' ? '' : "_#{mode}"}"
15
- t.libs << "../../../rails/activerecord/test/"
16
- t.test_files = (
17
- Dir.glob("test/cases/**/*_test_sqlserver.rb").sort +
18
- Dir.glob("../../../rails/activerecord/test/**/*_test.rb").sort )
19
- t.verbose = true
20
- end
21
-
22
- end
23
-
24
- desc 'Test with unicode types enabled, uses ODBC mode.'
25
- task :unicode_types do
26
- ENV['ENABLE_DEFAULT_UNICODE_TYPES'] = 'true'
27
- test = Rake::Task['sqlserver:test:odbc']
28
- test.invoke
29
- end
30
-
31
- end
32
-
33
- end
34
-
35
-
36
- desc 'Default runs tests for the adapters ODBC mode.'
37
- task :test do
38
- test = Rake::Task['sqlserver:test:odbc']
39
- test.invoke
40
- end
41
-
data/autotest/discover.rb DELETED
@@ -1,4 +0,0 @@
1
-
2
- Autotest.add_discovery do
3
- ARGV.first || 'railssqlserver'
4
- end
@@ -1,16 +0,0 @@
1
- require 'autotest/sqlserver'
2
-
3
- class Autotest::Railssqlserver < Autotest::Sqlserver
4
-
5
- def initialize
6
- super
7
- self.libs << "#{File::PATH_SEPARATOR}../../../rails/activerecord/test/"
8
- self.extra_files = ['../../../rails/activerecord/test/']
9
- self.add_mapping %r%../../../rails/activerecord/test/.*/.*_test.rb$% do |filename, _|
10
- filename
11
- end
12
- end
13
-
14
-
15
- end
16
-
@@ -1,54 +0,0 @@
1
- require 'autotest'
2
- require 'active_support'
3
-
4
- class Autotest::Sqlserver < Autotest
5
-
6
- def initialize
7
- super
8
-
9
- odbc_mode = true
10
-
11
- clear_mappings
12
-
13
- self.libs = [
14
- "lib",
15
- "test",
16
- "test/connections/native_sqlserver#{odbc_mode ? '_odbc' : ''}",
17
- "../../../rails/activerecord/test/"
18
- ].join(File::PATH_SEPARATOR)
19
-
20
- @test_sqlserver_file_match = %r%^test/cases/.*_test_sqlserver\.rb$%
21
-
22
- add_exception %r%^\./(?:autotest)%
23
- add_exception %r%^\./(.*LICENSE|debug.log|README.*|CHANGELOG.*)$%i
24
-
25
- # Any *_test_sqlserver file saved runs that file
26
- self.add_mapping @test_sqlserver_file_match do |filename, matchs|
27
- filename
28
- end
29
-
30
- # If any the adapter changes
31
- # the test directory, ofcourse having _test.rb at the end, will run that test.
32
- self.add_mapping(%r%^lib/(.*)\.rb$%) do |filename, matchs|
33
- files_matching @test_sqlserver_file_match
34
- end
35
-
36
- # If any core file like the test helper, connections, fixtures, migratinos,
37
- # and other support files, then run all matching *_test_sqlserver files.
38
- add_mapping %r%^test/(cases/(sqlserver_helper)\.rb|connections|fixtures|migrations|schema/.*)% do
39
- files_matching @test_sqlserver_file_match
40
- end
41
-
42
- end
43
-
44
- # Have to use a custom reorder method since the normal :alpha for Autotest would put the
45
- # files with ../ in the path before others.
46
- def reorder(files_to_test)
47
- ar_tests, sqlsvr_tests = files_to_test.partition { |k,v| k.starts_with?('../../../') }
48
- ar_tests.sort! { |a,b| a[0] <=> b[0] }
49
- sqlsvr_tests.sort! { |a,b| a[0] <=> b[0] }
50
- sqlsvr_tests + ar_tests
51
- end
52
-
53
- end
54
-
@@ -1,151 +0,0 @@
1
- require 'active_record/version'
2
-
3
- module ActiveRecord
4
- module ConnectionAdapters
5
- module SQLServerCoreExtensions
6
-
7
-
8
- module ActiveRecord
9
-
10
- def self.included(klass)
11
- klass.extend ClassMethods
12
- class << klass
13
- alias_method_chain :reset_column_information, :sqlserver_cache_support
14
- alias_method_chain :add_order!, :sqlserver_unique_checking
15
- alias_method_chain :add_limit!, :sqlserver_order_checking
16
- end
17
- end
18
-
19
- module ClassMethods
20
-
21
- def execute_procedure(proc_name, *variables)
22
- if connection.respond_to?(:execute_procedure)
23
- connection.execute_procedure(proc_name,*variables)
24
- else
25
- []
26
- end
27
- end
28
-
29
- def coerce_sqlserver_date(*attributes)
30
- write_inheritable_attribute :coerced_sqlserver_date_columns, Set.new(attributes.map(&:to_s))
31
- end
32
-
33
- def coerce_sqlserver_time(*attributes)
34
- write_inheritable_attribute :coerced_sqlserver_time_columns, Set.new(attributes.map(&:to_s))
35
- end
36
-
37
- def coerced_sqlserver_date_columns
38
- read_inheritable_attribute(:coerced_sqlserver_date_columns) || []
39
- end
40
-
41
- def coerced_sqlserver_time_columns
42
- read_inheritable_attribute(:coerced_sqlserver_time_columns) || []
43
- end
44
-
45
- def reset_column_information_with_sqlserver_cache_support
46
- connection.send(:initialize_sqlserver_caches) if connection.respond_to?(:sqlserver?)
47
- reset_column_information_without_sqlserver_cache_support
48
- end
49
-
50
- private
51
-
52
- def add_limit_with_sqlserver_order_checking!(sql, options, scope = :auto)
53
- if connection.respond_to?(:sqlserver?)
54
- scope = scope(:find) if :auto == scope
55
- if scope
56
- options = options.dup
57
- scoped_order = scope[:order]
58
- order = options[:order]
59
- if order && scoped_order
60
- options[:order] = add_order_with_sqlserver_unique_checking!('', order, scope).gsub(/^ ORDER BY /,'')
61
- elsif scoped_order
62
- options[:order] = scoped_order
63
- end
64
- end
65
- end
66
- add_limit_without_sqlserver_order_checking!(sql, options, scope)
67
- end
68
-
69
- def add_order_with_sqlserver_unique_checking!(sql, order, scope = :auto)
70
- if connection.respond_to?(:sqlserver?)
71
- order_sql = ''
72
- add_order_without_sqlserver_unique_checking!(order_sql, order, scope)
73
- unless order_sql.blank?
74
- unique_order_hash = {}
75
- select_table_name = connection.send(:get_table_name,sql)
76
- select_table_name.tr!('[]','') if select_table_name
77
- orders_and_dirs_set = connection.send(:orders_and_dirs_set,order_sql)
78
- unique_order_sql = orders_and_dirs_set.inject([]) do |array,order_dir|
79
- ord, dir = order_dir
80
- ord_tn_and_cn = ord.to_s.split('.').map{|o|o.tr('[]','')}
81
- ord_table_name, ord_column_name = if ord_tn_and_cn.size > 1
82
- ord_tn_and_cn
83
- else
84
- [nil, ord_tn_and_cn.first]
85
- end
86
- unique_key = [(ord_table_name || select_table_name), ord_column_name]
87
- if unique_order_hash[unique_key]
88
- array
89
- else
90
- unique_order_hash[unique_key] = true
91
- array << "#{ord} #{dir}".strip
92
- end
93
- end.join(', ')
94
- sql << " ORDER BY #{unique_order_sql}"
95
- end
96
- else
97
- add_order_without_sqlserver_unique_checking!(sql, order, scope)
98
- end
99
- end
100
-
101
- end
102
-
103
- module JoinAssociationChanges
104
-
105
- def self.included(klass)
106
- klass.class_eval do
107
- include InstanceMethods
108
- alias_method_chain :aliased_table_name_for, :sqlserver_support
109
- end
110
- end
111
-
112
- module InstanceMethods
113
-
114
- protected
115
-
116
- # An exact copy, except this method has a Regexp escape on the quoted table name.
117
- def aliased_table_name_for_with_sqlserver_support(name,suffix=nil)
118
- if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{Regexp.escape(active_record.connection.quote_table_name(name.downcase))}\son}i
119
- @join_dependency.table_aliases[name] += 1
120
- end
121
- unless @join_dependency.table_aliases[name].zero?
122
- # if the table name has been used, then use an alias
123
- name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}"
124
- table_index = @join_dependency.table_aliases[name]
125
- @join_dependency.table_aliases[name] += 1
126
- name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
127
- else
128
- @join_dependency.table_aliases[name] += 1
129
- end
130
- name
131
- end
132
-
133
- end
134
-
135
- end
136
-
137
- end
138
-
139
-
140
- end
141
- end
142
- end
143
-
144
-
145
- ActiveRecord::Base.send :include, ActiveRecord::ConnectionAdapters::SQLServerCoreExtensions::ActiveRecord
146
-
147
- if ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR >= 3
148
- require 'active_record/associations'
149
- ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.send :include, ActiveRecord::ConnectionAdapters::SQLServerCoreExtensions::ActiveRecord::JoinAssociationChanges
150
- end
151
-
@@ -1,40 +0,0 @@
1
-
2
- module ActiveRecord
3
- module ConnectionAdapters
4
- module SQLServerCoreExtensions
5
- module ODBC
6
-
7
- module TimeStamp
8
- def to_sqlserver_string
9
- date, time, nanoseconds = to_s.split(' ')
10
- "#{date} #{time}.#{sprintf("%03d",nanoseconds.to_i/1000000)}"
11
- end
12
- end
13
-
14
- module Statement
15
- def finished?
16
- begin
17
- connected?
18
- false
19
- rescue ::ODBC::Error => e
20
- true
21
- end
22
- end
23
- end
24
-
25
- module Database
26
- def run_block(*args)
27
- yield sth = run(*args)
28
- sth.drop
29
- end
30
- end
31
-
32
- end
33
- end
34
- end
35
- end
36
-
37
- ODBC::TimeStamp.send :include, ActiveRecord::ConnectionAdapters::SQLServerCoreExtensions::ODBC::TimeStamp if defined?(ODBC::TimeStamp)
38
- ODBC::Statement.send :include, ActiveRecord::ConnectionAdapters::SQLServerCoreExtensions::ODBC::Statement if defined?(ODBC::Statement)
39
- ODBC::Database.send :include, ActiveRecord::ConnectionAdapters::SQLServerCoreExtensions::ODBC::Database if defined?(ODBC::Database)
40
-
@@ -1,19 +0,0 @@
1
- # The filename begins with "aaaa" to ensure this is the first test.
2
- require 'cases/sqlserver_helper'
3
-
4
- class AAAACreateTablesTestSqlserver < ActiveRecord::TestCase
5
- self.use_transactional_fixtures = false
6
-
7
- should 'load activerecord schema' do
8
- schema_file = "#{ACTIVERECORD_TEST_ROOT}/schema/schema.rb"
9
- eval(File.read(schema_file))
10
- assert true
11
- end
12
-
13
- should 'load sqlserver specific schema' do
14
- sqlserver_specific_schema_file = "#{SQLSERVER_SCHEMA_ROOT}/sqlserver_specific_schema.rb"
15
- eval(File.read(sqlserver_specific_schema_file))
16
- assert true
17
- end
18
-
19
- end