Skip to content

Commit 5e30f4e

Browse files
committed
Cleanup
1 parent 5a6bb45 commit 5e30f4e

File tree

2 files changed

+7
-90
lines changed

2 files changed

+7
-90
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 5 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ module DatabaseStatements
77
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :commit, :dbcc, :explain, :save, :select, :set, :rollback, :waitfor, :use) # :nodoc:
88
private_constant :READ_QUERY
99

10-
# TODO: replace `internal_exec_query` by `perform_query`.
10+
def write_query?(sql) # :nodoc:
11+
!READ_QUERY.match?(sql)
12+
rescue ArgumentError # Invalid encoding
13+
!READ_QUERY.match?(sql.b)
14+
end
1115

1216
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch:)
13-
# unless binds.nil? || binds.empty?
14-
# types, params = sp_executesql_types_and_parameters(binds)
15-
#
16-
# # TODO: `name` parameter does not exist.
17-
# sql = sp_executesql_sql(sql, types, params)
18-
# end
19-
20-
2117
result = if id_insert_table_name = query_requires_identity_insert?(sql)
2218
with_identity_insert_enabled(id_insert_table_name, raw_connection) do
2319
internal_exec_sql_query(sql, raw_connection)
@@ -31,103 +27,27 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
3127
result
3228
end
3329

34-
#
35-
# def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false)
36-
# sql = transform_query(sql)
37-
#
38-
# check_if_write_query(sql)
39-
# mark_transaction_written_if_write(sql)
40-
#
41-
# unless without_prepared_statement?(binds)
42-
# types, params = sp_executesql_types_and_parameters(binds)
43-
# sql = sp_executesql_sql(sql, types, params, name)
44-
# end
45-
#
46-
# log(sql, name, binds, async: async) do |notification_payload|
47-
# with_raw_connection do |conn|
48-
# result = if id_insert_table_name = query_requires_identity_insert?(sql)
49-
# with_identity_insert_enabled(id_insert_table_name, conn) do
50-
# internal_exec_sql_query(sql, conn)
51-
# end
52-
# else
53-
# internal_exec_sql_query(sql, conn)
54-
# end
55-
#
56-
# verified!
57-
# notification_payload[:row_count] = result.count
58-
# result
59-
# end
60-
# end
61-
# end
62-
63-
64-
# Receive a native adapter result object and returns an ActiveRecord::Result object.
6530
def cast_result(raw_result)
6631
if raw_result.columns.empty?
6732
ActiveRecord::Result.empty
6833
else
6934
ActiveRecord::Result.new(raw_result.columns, raw_result.rows)
7035
end
71-
72-
# rescue => e
73-
# binding.pry
7436
end
7537

76-
77-
7838
def affected_rows(raw_result)
79-
8039
raw_result.first['AffectedRows']
81-
82-
# if raw_result.count == 1 && raw_result.first.key?('AffectedRows')
83-
# raw_result.first['AffectedRows']
84-
# else
85-
# raw_result.count
86-
# end
87-
88-
# rescue => e
89-
# binding.pry
90-
end
91-
92-
93-
94-
def write_query?(sql) # :nodoc:
95-
!READ_QUERY.match?(sql)
96-
rescue ArgumentError # Invalid encoding
97-
!READ_QUERY.match?(sql.b)
9840
end
9941

100-
# TODO: This method implemented in Rails.
101-
# def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
102-
# log(sql, name, async: async) do |notification_payload|
103-
# with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
104-
# result = if id_insert_table_name = query_requires_identity_insert?(sql)
105-
# with_identity_insert_enabled(id_insert_table_name, conn) { internal_raw_execute(sql, conn, perform_do: true) }
106-
# else
107-
# internal_raw_execute(sql, conn, perform_do: true)
108-
# end
109-
# verified!
110-
# notification_payload[:row_count] = result
111-
# result
112-
# end
113-
# end
114-
# end
115-
116-
11742
def raw_execute(sql, name = nil, binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true, batch: false)
118-
11943
unless binds.nil? || binds.empty?
12044
types, params = sp_executesql_types_and_parameters(binds)
121-
122-
12345
sql = sp_executesql_sql(sql, types, params, name)
12446
end
12547

126-
12748
super
12849
end
12950

130-
13151
def internal_exec_sql_query(sql, conn)
13252
handle = internal_raw_execute(sql, conn)
13353
handle_to_names_and_values(handle, ar_result: true)
@@ -138,13 +58,11 @@ def internal_exec_sql_query(sql, conn)
13858
def exec_delete(sql, name = nil, binds = [])
13959
sql = sql.dup << "; SELECT @@ROWCOUNT AS AffectedRows"
14060
super(sql, name, binds)
141-
# internal_execute(sql, name, binds).first['AffectedRows']
14261
end
14362

14463
def exec_update(sql, name = nil, binds = [])
14564
sql = sql.dup << "; SELECT @@ROWCOUNT AS AffectedRows"
14665
super(sql, name, binds)
147-
# internal_execute(sql, name, binds).first['AffectedRows']
14866
end
14967

15068
def begin_db_transaction
@@ -531,7 +449,6 @@ def finish_statement_handle(handle)
531449
# TinyTDS returns false instead of raising an exception if connection fails.
532450
# Getting around this by raising an exception ourselves while PR
533451
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
534-
# TODO: Check if `perform_do` is needed.
535452
def internal_raw_execute(sql, raw_connection, perform_do: false)
536453
result = raw_connection.execute(sql)
537454
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)

lib/active_record/connection_adapters/sqlserver/showplan.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ module Showplan
1313
OPTIONS = [OPTION_ALL, OPTION_TEXT, OPTION_XML]
1414

1515
def explain(arel, binds = [], options = [])
16-
sql = to_sql(arel)
17-
result = with_showplan_on { internal_exec_query(sql, "EXPLAIN", binds) }
16+
sql = to_sql(arel)
17+
result = with_showplan_on { internal_exec_query(sql, "EXPLAIN", binds) }
1818
printer = showplan_printer.new(result)
1919

2020
printer.pp

0 commit comments

Comments
 (0)