@@ -4,31 +4,26 @@ module ActiveRecord
4
4
module ConnectionAdapters
5
5
module MySQL
6
6
module DatabaseStatements
7
- # Returns an ActiveRecord::Result instance.
8
- def select_all ( *, **) # :nodoc:
9
- result = nil
10
- with_raw_connection do |conn |
11
- result = if ExplainRegistry . collect? && prepared_statements
12
- unprepared_statement { super }
13
- else
14
- super
15
- end
16
- conn . abandon_results!
17
- end
18
- result
19
- end
20
-
21
- READ_QUERY = ActiveRecord ::ConnectionAdapters ::AbstractAdapter . build_read_query_regexp (
7
+ READ_QUERY = AbstractAdapter . build_read_query_regexp (
22
8
:desc , :describe , :set , :show , :use
23
9
) # :nodoc:
24
10
private_constant :READ_QUERY
25
11
12
+ # https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_current-timestamp
13
+ # https://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-syntax.html
14
+ HIGH_PRECISION_CURRENT_TIMESTAMP = Arel . sql ( "CURRENT_TIMESTAMP(6)" ) . freeze # :nodoc:
15
+ private_constant :HIGH_PRECISION_CURRENT_TIMESTAMP
16
+
26
17
def write_query? ( sql ) # :nodoc:
27
18
!READ_QUERY . match? ( sql )
28
19
rescue ArgumentError # Invalid encoding
29
20
!READ_QUERY . match? ( sql . b )
30
21
end
31
22
23
+ def high_precision_current_timestamp
24
+ HIGH_PRECISION_CURRENT_TIMESTAMP
25
+ end
26
+
32
27
def explain ( arel , binds = [ ] , options = [ ] )
33
28
sql = build_explain_clause ( options ) + " " + to_sql ( arel , binds )
34
29
start = Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
@@ -38,47 +33,6 @@ def explain(arel, binds = [], options = [])
38
33
MySQL ::ExplainPrettyPrinter . new . pp ( result , elapsed )
39
34
end
40
35
41
- def internal_exec_query ( sql , name = "SQL" , binds = [ ] , prepare : false , async : false ) # :nodoc:
42
- if without_prepared_statement? ( binds )
43
- execute_and_free ( sql , name , async : async ) do |result |
44
- if result
45
- build_result ( columns : result . fields , rows : result . to_a )
46
- else
47
- build_result ( columns : [ ] , rows : [ ] )
48
- end
49
- end
50
- else
51
- exec_stmt_and_free ( sql , name , binds , cache_stmt : prepare , async : async ) do |_ , result |
52
- if result
53
- build_result ( columns : result . fields , rows : result . to_a )
54
- else
55
- build_result ( columns : [ ] , rows : [ ] )
56
- end
57
- end
58
- end
59
- end
60
-
61
- def exec_delete ( sql , name = nil , binds = [ ] ) # :nodoc:
62
- if without_prepared_statement? ( binds )
63
- with_raw_connection do |conn |
64
- @affected_rows_before_warnings = nil
65
- execute_and_free ( sql , name ) { @affected_rows_before_warnings || conn . affected_rows }
66
- end
67
- else
68
- exec_stmt_and_free ( sql , name , binds ) { |stmt | stmt . affected_rows }
69
- end
70
- end
71
- alias :exec_update :exec_delete
72
-
73
- # https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_current-timestamp
74
- # https://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-syntax.html
75
- HIGH_PRECISION_CURRENT_TIMESTAMP = Arel . sql ( "CURRENT_TIMESTAMP(6)" ) . freeze # :nodoc:
76
- private_constant :HIGH_PRECISION_CURRENT_TIMESTAMP
77
-
78
- def high_precision_current_timestamp
79
- HIGH_PRECISION_CURRENT_TIMESTAMP
80
- end
81
-
82
36
def build_explain_clause ( options = [ ] )
83
37
return "EXPLAIN" if options . empty?
84
38
@@ -92,52 +46,15 @@ def build_explain_clause(options = [])
92
46
end
93
47
94
48
private
95
- def sync_timezone_changes ( raw_connection )
96
- raw_connection . query_options [ :database_timezone ] = default_timezone
97
- end
98
-
99
- def execute_batch ( statements , name = nil )
100
- statements = statements . map { |sql | transform_query ( sql ) }
101
- combine_multi_statements ( statements ) . each do |statement |
102
- with_raw_connection do |conn |
103
- raw_execute ( statement , name )
104
- conn . abandon_results!
105
- end
106
- end
49
+ # https://mariadb.com/kb/en/analyze-statement/
50
+ def analyze_without_explain?
51
+ mariadb? && database_version >= "10.1.0"
107
52
end
108
53
109
54
def default_insert_value ( column )
110
55
super unless column . auto_increment?
111
56
end
112
57
113
- def last_inserted_id ( result )
114
- @raw_connection &.last_id
115
- end
116
-
117
- def multi_statements_enabled?
118
- flags = @config [ :flags ]
119
-
120
- if flags . is_a? ( Array )
121
- flags . include? ( "MULTI_STATEMENTS" )
122
- else
123
- flags . anybits? ( Mysql2 ::Client ::MULTI_STATEMENTS )
124
- end
125
- end
126
-
127
- def with_multi_statements
128
- if multi_statements_enabled?
129
- return yield
130
- end
131
-
132
- with_raw_connection do |conn |
133
- conn . set_server_option ( Mysql2 ::Client ::OPTION_MULTI_STATEMENTS_ON )
134
-
135
- yield
136
- ensure
137
- conn . set_server_option ( Mysql2 ::Client ::OPTION_MULTI_STATEMENTS_OFF )
138
- end
139
- end
140
-
141
58
def combine_multi_statements ( total_sql )
142
59
total_sql . each_with_object ( [ ] ) do |sql , total_sql_chunks |
143
60
previous_packet = total_sql_chunks . last
@@ -164,61 +81,6 @@ def max_allowed_packet_reached?(current_packet, previous_packet)
164
81
def max_allowed_packet
165
82
@max_allowed_packet ||= show_variable ( "max_allowed_packet" )
166
83
end
167
-
168
- def raw_execute ( sql , name , async : false , allow_retry : false , materialize_transactions : true )
169
- log ( sql , name , async : async ) do
170
- with_raw_connection ( allow_retry : allow_retry , materialize_transactions : materialize_transactions ) do |conn |
171
- sync_timezone_changes ( conn )
172
- result = conn . query ( sql )
173
- handle_warnings ( sql )
174
- result
175
- end
176
- end
177
- end
178
-
179
- def exec_stmt_and_free ( sql , name , binds , cache_stmt : false , async : false )
180
- sql = transform_query ( sql )
181
- check_if_write_query ( sql )
182
-
183
- mark_transaction_written_if_write ( sql )
184
-
185
- type_casted_binds = type_casted_binds ( binds )
186
-
187
- log ( sql , name , binds , type_casted_binds , async : async ) do
188
- with_raw_connection do |conn |
189
- sync_timezone_changes ( conn )
190
-
191
- if cache_stmt
192
- stmt = @statements [ sql ] ||= conn . prepare ( sql )
193
- else
194
- stmt = conn . prepare ( sql )
195
- end
196
-
197
- begin
198
- result = ActiveSupport ::Dependencies . interlock . permit_concurrent_loads do
199
- stmt . execute ( *type_casted_binds )
200
- end
201
- rescue Mysql2 ::Error => e
202
- if cache_stmt
203
- @statements . delete ( sql )
204
- else
205
- stmt . close
206
- end
207
- raise e
208
- end
209
-
210
- ret = yield stmt , result
211
- result . free if result
212
- stmt . close unless cache_stmt
213
- ret
214
- end
215
- end
216
- end
217
-
218
- # https://mariadb.com/kb/en/analyze-statement/
219
- def analyze_without_explain?
220
- mariadb? && database_version >= "10.1.0"
221
- end
222
84
end
223
85
end
224
86
end
0 commit comments