Skip to content

Commit fa613e7

Browse files
committed
[Rails3] Abstract adapter organization.
1 parent 086fcde commit fa613e7

File tree

4 files changed

+107
-89
lines changed

4 files changed

+107
-89
lines changed

RAILS3_NOTES

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ this project root. From here you can start testing, but it is best practice to u
5858
end.join(', ')
5959
end
6060

61+
6162
= Adapter Todo
6263

63-
* Move all sqlserver_adapter/core_ext files to sqlserver/core_ext
64+
* Move all sqlserver_adapter/core_ext files to sqlserver/core_ext.
65+
* Make sure the repair special columns was good to go.
66+
* Possibly remove 2000 support and query methods.
67+
68+
69+
70+
6471

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,25 @@ def zip_fields_and_rows(fields, rows)
198198
results << row_hash
199199
end
200200
end
201+
202+
# === SQLServer Specific (Executing) ============================ #
201203

202204
def do_execute(sql,name=nil)
203205
log(sql, name || 'EXECUTE') do
204206
with_auto_reconnect { raw_connection_do(sql) }
205207
end
206208
end
209+
210+
def raw_connection_do(sql)
211+
case connection_mode
212+
when :odbc
213+
raw_connection.do(sql)
214+
else :adonet
215+
raw_connection.create_command.tap{ |cmd| cmd.command_text = sql }.execute_non_query
216+
end
217+
end
218+
219+
# === SQLServer Specific (Selecting) ============================ #
207220

208221
def raw_select(sql, name = nil)
209222
fields_and_row_sets = []
@@ -226,7 +239,18 @@ def raw_select(sql, name = nil)
226239
end
227240
fields_and_row_sets
228241
end
229-
242+
243+
def raw_connection_run(sql)
244+
with_auto_reconnect do
245+
case connection_mode
246+
when :odbc
247+
block_given? ? raw_connection.run_block(sql) { |handle| yield(handle) } : raw_connection.run(sql)
248+
else :adonet
249+
raw_connection.create_command.tap{ |cmd| cmd.command_text = sql }.execute_reader
250+
end
251+
end
252+
end
253+
230254
def handle_more_results?(handle)
231255
case connection_mode
232256
when :odbc
@@ -282,6 +306,19 @@ def handle_to_fields_and_rows_adonet(handle)
282306
[fields,rows]
283307
end
284308

309+
def finish_statement_handle(handle)
310+
case connection_mode
311+
when :odbc
312+
handle.drop if handle && handle.respond_to?(:drop) && !handle.finished?
313+
when :adonet
314+
handle.close if handle && handle.respond_to?(:close) && !handle.is_closed
315+
handle.dispose if handle && handle.respond_to?(:dispose)
316+
end
317+
handle
318+
end
319+
320+
# === SQLServer Specific (Misc, Doomed?) ======================== #
321+
285322
def add_lock!(sql, options)
286323
# http://blog.sqlauthority.com/2007/04/27/sql-server-2005-locking-hints-and-examples/
287324
return unless options[:lock]

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,6 @@ def change_column_null(table_name, column_name, null, default = nil)
152152
sql << " NOT NULL" unless null
153153
do_execute sql
154154
end
155-
156-
def pk_and_sequence_for(table_name)
157-
idcol = identity_column(table_name)
158-
idcol ? [idcol.name,nil] : nil
159-
end
160-
161-
def primary_key(table_name)
162-
identity_column(table_name).try(:name)
163-
end
164155

165156
# === SQLServer Specific ======================================== #
166157

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 61 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def initialize(logger,config)
192192
end
193193
end
194194

195-
# ABSTRACT ADAPTER =========================================#
195+
# === Abstract Adapter ========================================== #
196196

197197
def adapter_name
198198
ADAPTER_NAME
@@ -214,6 +214,54 @@ def supports_savepoints?
214214
true
215215
end
216216

217+
def disable_referential_integrity
218+
do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'"
219+
yield
220+
ensure
221+
do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'"
222+
end
223+
224+
# === Abstract Adapter (Connection Management) ================== #
225+
226+
def active?
227+
raw_connection_do("SELECT 1")
228+
true
229+
rescue *lost_connection_exceptions
230+
false
231+
end
232+
233+
def reconnect!
234+
disconnect!
235+
connect
236+
active?
237+
end
238+
239+
def disconnect!
240+
case connection_mode
241+
when :odbc
242+
raw_connection.disconnect rescue nil
243+
else :adonet
244+
raw_connection.close rescue nil
245+
end
246+
end
247+
248+
def reset!
249+
remove_database_connections_and_rollback { }
250+
end
251+
252+
# === Abstract Adapter (Misc Support) =========================== #
253+
254+
def pk_and_sequence_for(table_name)
255+
idcol = identity_column(table_name)
256+
idcol ? [idcol.name,nil] : nil
257+
end
258+
259+
def primary_key(table_name)
260+
identity_column(table_name).try(:name)
261+
end
262+
263+
# === SQLServer Specific (DB Reflection) ======================== #
264+
217265
def database_version
218266
@database_version ||= info_schema_query { select_value('SELECT @@version') }
219267
end
@@ -275,39 +323,6 @@ def native_binary_database_type
275323
@@native_binary_database_type || ((sqlserver_2005? || sqlserver_2008?) ? 'varbinary(max)' : 'image')
276324
end
277325

278-
# REFERENTIAL INTEGRITY ====================================#
279-
280-
def disable_referential_integrity
281-
do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'"
282-
yield
283-
ensure
284-
do_execute "EXEC sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'"
285-
end
286-
287-
# CONNECTION MANAGEMENT ====================================#
288-
289-
def active?
290-
raw_connection_do("SELECT 1")
291-
true
292-
rescue *lost_connection_exceptions
293-
false
294-
end
295-
296-
def reconnect!
297-
disconnect!
298-
connect
299-
active?
300-
end
301-
302-
def disconnect!
303-
case connection_mode
304-
when :odbc
305-
raw_connection.disconnect rescue nil
306-
else :adonet
307-
raw_connection.close rescue nil
308-
end
309-
end
310-
311326
# RAKE UTILITY METHODS =====================================#
312327

313328
def recreate_database
@@ -357,24 +372,13 @@ def current_database
357372
def charset
358373
select_value "SELECT SERVERPROPERTY('SqlCharSetName')"
359374
end
360-
361-
# This should disconnect all other users and rollback any transactions for SQL 2000 and 2005
362-
# http://sqlserver2000.databases.aspfaq.com/how-do-i-drop-a-sql-server-database.html
363-
def remove_database_connections_and_rollback(database=nil)
364-
database ||= current_database
365-
do_execute "ALTER DATABASE #{quote_table_name(database)} SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
366-
begin
367-
yield
368-
ensure
369-
do_execute "ALTER DATABASE #{quote_table_name(database)} SET MULTI_USER"
370-
end if block_given?
371-
end
375+
372376

373377

374378

375379
protected
376380

377-
# CONNECTION MANAGEMENT ====================================#
381+
# === SQLServer Specific (Connection Management) ================ #
378382

379383
def connect
380384
config = @connection_options
@@ -402,8 +406,14 @@ def connect
402406
raise unless @auto_connecting
403407
end
404408

405-
def connection_mode
406-
@connection_options[:mode]
409+
def remove_database_connections_and_rollback(database=nil)
410+
database ||= current_database
411+
do_execute "ALTER DATABASE #{quote_table_name(database)} SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
412+
begin
413+
yield
414+
ensure
415+
do_execute "ALTER DATABASE #{quote_table_name(database)} SET MULTI_USER"
416+
end if block_given?
407417
end
408418

409419
def lost_connection_exceptions
@@ -442,35 +452,8 @@ def auto_reconnected?
442452
@auto_connecting = false
443453
end
444454

445-
def raw_connection_run(sql)
446-
with_auto_reconnect do
447-
case connection_mode
448-
when :odbc
449-
block_given? ? raw_connection.run_block(sql) { |handle| yield(handle) } : raw_connection.run(sql)
450-
else :adonet
451-
raw_connection.create_command.tap{ |cmd| cmd.command_text = sql }.execute_reader
452-
end
453-
end
454-
end
455-
456-
def raw_connection_do(sql)
457-
case connection_mode
458-
when :odbc
459-
raw_connection.do(sql)
460-
else :adonet
461-
raw_connection.create_command.tap{ |cmd| cmd.command_text = sql }.execute_non_query
462-
end
463-
end
464-
465-
def finish_statement_handle(handle)
466-
case connection_mode
467-
when :odbc
468-
handle.drop if handle && handle.respond_to?(:drop) && !handle.finished?
469-
when :adonet
470-
handle.close if handle && handle.respond_to?(:close) && !handle.is_closed
471-
handle.dispose if handle && handle.respond_to?(:dispose)
472-
end
473-
handle
455+
def connection_mode
456+
@connection_options[:mode]
474457
end
475458

476459
end #class SQLServerAdapter < AbstractAdapter

0 commit comments

Comments
 (0)