@@ -7,17 +7,13 @@ module DatabaseStatements
7
7
READ_QUERY = ActiveRecord ::ConnectionAdapters ::AbstractAdapter . build_read_query_regexp ( :begin , :commit , :dbcc , :explain , :save , :select , :set , :rollback , :waitfor , :use ) # :nodoc:
8
8
private_constant :READ_QUERY
9
9
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
11
15
12
16
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
-
21
17
result = if id_insert_table_name = query_requires_identity_insert? ( sql )
22
18
with_identity_insert_enabled ( id_insert_table_name , raw_connection ) do
23
19
internal_exec_sql_query ( sql , raw_connection )
@@ -31,103 +27,27 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
31
27
result
32
28
end
33
29
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.
65
30
def cast_result ( raw_result )
66
31
if raw_result . columns . empty?
67
32
ActiveRecord ::Result . empty
68
33
else
69
34
ActiveRecord ::Result . new ( raw_result . columns , raw_result . rows )
70
35
end
71
-
72
- # rescue => e
73
- # binding.pry
74
36
end
75
37
76
-
77
-
78
38
def affected_rows ( raw_result )
79
-
80
39
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 )
98
40
end
99
41
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
-
117
42
def raw_execute ( sql , name = nil , binds = [ ] , prepare : false , async : false , allow_retry : false , materialize_transactions : true , batch : false )
118
-
119
43
unless binds . nil? || binds . empty?
120
44
types , params = sp_executesql_types_and_parameters ( binds )
121
-
122
-
123
45
sql = sp_executesql_sql ( sql , types , params , name )
124
46
end
125
47
126
-
127
48
super
128
49
end
129
50
130
-
131
51
def internal_exec_sql_query ( sql , conn )
132
52
handle = internal_raw_execute ( sql , conn )
133
53
handle_to_names_and_values ( handle , ar_result : true )
@@ -138,13 +58,11 @@ def internal_exec_sql_query(sql, conn)
138
58
def exec_delete ( sql , name = nil , binds = [ ] )
139
59
sql = sql . dup << "; SELECT @@ROWCOUNT AS AffectedRows"
140
60
super ( sql , name , binds )
141
- # internal_execute(sql, name, binds).first['AffectedRows']
142
61
end
143
62
144
63
def exec_update ( sql , name = nil , binds = [ ] )
145
64
sql = sql . dup << "; SELECT @@ROWCOUNT AS AffectedRows"
146
65
super ( sql , name , binds )
147
- # internal_execute(sql, name, binds).first['AffectedRows']
148
66
end
149
67
150
68
def begin_db_transaction
@@ -531,7 +449,6 @@ def finish_statement_handle(handle)
531
449
# TinyTDS returns false instead of raising an exception if connection fails.
532
450
# Getting around this by raising an exception ourselves while PR
533
451
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
534
- # TODO: Check if `perform_do` is needed.
535
452
def internal_raw_execute ( sql , raw_connection , perform_do : false )
536
453
result = raw_connection . execute ( sql )
537
454
raise TinyTds ::Error , "failed to execute statement" if result . is_a? ( FalseClass )
0 commit comments