@@ -76,132 +76,67 @@ function my_mysqli_connect(
76
76
int $ port ,
77
77
?string $ socket = null ,
78
78
bool $ enable_env_flags = true
79
- ): \mysqli |false {
79
+ ): \mysqli {
80
+ // Because the tests are meant to test both error modes, they can set the report_mode to a different value,
81
+ // which we do not want to override. However, we want to make sure that if a connection cannot be made,
82
+ // the constuctor will throw an exception. We store current report_mode in variable and restore it later.
83
+ $ driver = new mysqli_driver ;
84
+ $ report_mode = $ driver ->report_mode ;
85
+ $ driver ->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT ;
80
86
81
- $ flags = $ enable_env_flags ? get_environment_connection_flags (): 0 ;
87
+ $ flags = $ enable_env_flags ? get_environment_connection_flags () : 0 ;
82
88
if ($ flags !== 0 ) {
83
89
$ link = mysqli_init ();
84
- if (!mysqli_real_connect ($ link , $ host , $ user , $ password , $ db , $ port , $ socket , $ flags )) {
85
- $ link = false ;
86
- }
90
+ mysqli_real_connect ($ link , $ host , $ user , $ password , $ db , $ port , $ socket , $ flags );
87
91
} else {
88
92
$ link = mysqli_connect ($ host , $ user , $ password , $ db , $ port , $ socket );
89
93
}
90
94
91
- return $ link ;
92
- }
93
-
94
- /**
95
- * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
96
- *
97
- * @param bool $enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
98
- */
99
- function my_mysqli_real_connect (
100
- mysqli $ link ,
101
- string $ host ,
102
- string $ user ,
103
- string $ password ,
104
- string $ db ,
105
- int $ port ,
106
- ?string $ socket = null ,
107
- int $ flags = 0 ,
108
- bool $ enable_env_flags = true
109
- ): \mysqli |false {
110
- if ($ enable_env_flags ) {
111
- $ flags = $ flags | get_environment_connection_flags ();
112
- }
95
+ // Restore error mode
96
+ $ driver ->report_mode = $ report_mode ;
113
97
114
- return mysqli_real_connect ( $ link, $ host , $ user , $ password , $ db , $ port , $ socket , $ flags ) ;
98
+ return $ link ;
115
99
}
116
100
117
- function default_mysqli_connect_ex (): \mysqli |false {
118
- return my_mysqli_connect (
119
- get_default_host (),
120
- get_default_user (),
121
- get_default_password (),
122
- get_default_database (),
123
- get_default_port (),
124
- null ,
125
- );
126
- }
127
101
function default_mysqli_connect (): \mysqli {
128
- $ link = default_mysqli_connect_ex ();
129
- if (!$ link ) {
130
- throw new Error ("Cannot connect to default connection " );
131
- }
132
- return $ link ;
133
- }
134
- function default_mysqli_real_connect (mysqli $ link , int $ flags = 0 ): bool {
135
- return my_mysqli_real_connect (
136
- $ link ,
102
+ return my_mysqli_connect (
137
103
get_default_host (),
138
104
get_default_user (),
139
105
get_default_password (),
140
106
get_default_database (),
141
107
get_default_port (),
142
108
null ,
143
- $ flags ,
144
109
);
145
110
}
146
111
147
112
function mysqli_check_skip_test (): void {
148
- /* Disable exceptions */
149
- mysqli_report (MYSQLI_REPORT_OFF );
150
- $ link = default_mysqli_connect_ex ();
151
- // Re-enable exceptions
152
- mysqli_report (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
153
- if (!is_object ($ link )) {
113
+ try {
114
+ $ link = default_mysqli_connect ();
115
+ } catch (\mysqli_sql_exception ) {
154
116
die (sprintf ("skip Can't connect to MySQL Server - [%d] %s " , mysqli_connect_errno (), mysqli_connect_error ()));
155
117
}
156
- mysqli_close ($ link );
157
118
}
158
119
159
120
function have_innodb (mysqli $ link ): bool {
160
- if (($ res = $ link ->query ("SHOW VARIABLES LIKE 'have_innodb' " ))
161
- && ($ row = $ res ->fetch_row ())
162
- && !empty ($ row )
163
- ) {
164
- return !($ row [1 ] == 'DISABLED ' || $ row [1 ] == 'NO ' );
165
- }
166
- // MySQL 5.6.1+
167
- if ($ res = $ link ->query ('SHOW ENGINES ' )) {
168
- while ($ row = $ res ->fetch_assoc ()) {
169
- if (!isset ($ row ['Engine ' ]) || !isset ($ row ['Support ' ])) {
170
- return false ;
171
- }
172
-
173
- if (($ row ['Engine ' ] == 'InnoDB ' )
174
- && (($ row ['Support ' ] == 'YES ' ) || ($ row ['Support ' ] == 'DEFAULT ' ))
175
- ) {
176
- return true ;
177
- }
178
- }
179
- }
180
- return false ;
121
+ $ res = $ link ->query ("SELECT SUPPORT FROM INFORMATION_SCHEMA.ENGINES WHERE ENGINE = 'InnoDB' " );
122
+ $ supported = $ res ->fetch_column ();
123
+ return $ supported === 'YES ' || $ supported === 'DEFAULT ' ;
181
124
}
125
+
182
126
function mysqli_check_innodb_support_skip_test (): void {
183
- /* Disable exceptions */
184
- mysqli_report (MYSQLI_REPORT_OFF );
185
- $ link = default_mysqli_connect_ex ();
186
- if (!is_object ($ link )) {
187
- // Re-enable exceptions
188
- mysqli_report (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
127
+ try {
128
+ $ link = default_mysqli_connect ();
129
+ } catch (\mysqli_sql_exception ) {
189
130
die (sprintf ("skip Can't connect to MySQL Server - [%d] %s " , mysqli_connect_errno (), mysqli_connect_error ()));
190
131
}
191
- $ status = have_innodb ($ link );
192
- // Re-enable exceptions
193
- mysqli_report (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
194
- mysqli_close ($ link );
195
- if (!$ status ) {
196
- die (sprintf ("skip Needs InnoDB support, [%d] %s " , $ link ->errno , $ link ->error ));
132
+ if (! have_innodb ($ link )) {
133
+ die (sprintf ("skip Needs InnoDB support " ));
197
134
}
198
135
}
199
136
200
137
function tear_down_table_on_default_connection (string $ table ) {
201
138
$ link = default_mysqli_connect ();
202
- if (!mysqli_query ($ link , 'DROP TABLE IF EXISTS ' . $ table )) {
203
- printf ("[clean] Failed to drop \"$ table \" table: [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
204
- }
139
+ mysqli_query ($ link , 'DROP TABLE IF EXISTS ' . $ table );
205
140
}
206
141
207
142
function setup_table_with_data_on_default_connection (string $ table ): mysqli {
0 commit comments