Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit c6bbd98

Browse files
committed
[[ Bug 20570 ]] Fix MySQL errors from multiple result sets
All result sets must be processed to avoid errors.
1 parent fe3b22c commit c6bbd98

File tree

1 file changed

+75
-38
lines changed

1 file changed

+75
-38
lines changed

revdb/src/mysql_connection.cpp

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -153,55 +153,62 @@ Output: False on error
153153
*/
154154
Bool DBConnection_MYSQL::sqlExecute(char *p_query, DBString *p_arguments, int p_argument_count, unsigned int &p_affected_rows)
155155
{
156-
int t_affected_rows;
157-
t_affected_rows = 0;
158-
159-
Bool t_result;
160-
t_result = False;
161-
162156
if (!ExecuteQuery(p_query, p_arguments, p_argument_count) || mysql_errno(getMySQL()))
163157
{
164158
// OK-2007-09-10 : Bug 5360
165159
errorMessageSet(mysql_error(getMySQL()));
166-
return t_result;
160+
return False;
167161
}
168162

169-
// First we need to establish if the query returned a result set. This will be the case if it was
170-
// a select query. If there is a result set, we need to store it temporarily and free it to avoid
171-
// errors ocurring in subsequent queries.
172-
unsigned int t_field_count;
173-
t_field_count = mysql_field_count(getMySQL());
174-
175-
MYSQL_RES *t_result_set;
176-
t_result_set = NULL;
177-
if (t_field_count != 0)
163+
bool t_success = true;
164+
bool t_first = true;
165+
166+
// we need to iterate through all result sets to avoid errors
167+
int t_status = 0;
168+
169+
while (t_success && (t_status == 0))
178170
{
179-
t_result_set = mysql_store_result(getMySQL());
180-
if (t_result_set == NULL)
181-
return False;
171+
MYSQL_RES *t_result_set;
172+
t_result_set = NULL;
182173

174+
int t_affected_rows;
183175
t_affected_rows = 0;
184-
}
185-
else
186-
t_affected_rows = (int)mysql_affected_rows(&mysql);
187176

188-
if (t_affected_rows != -1)
189-
t_result = True;
190-
191-
if (t_result_set != NULL)
192-
{
193-
mysql_free_result(t_result_set);
194-
}
177+
// First we need to establish if the query returned a result set. This will be the case if it was
178+
// a select query. If there is a result set, we need to store it temporarily and free it to avoid
179+
// errors ocurring in subsequent queries.
180+
t_result_set = mysql_store_result(getMySQL());
181+
t_success = (t_result_set != NULL) || (mysql_field_count(getMySQL()) == 0);
182+
183+
if (t_success && (t_result_set == NULL))
184+
{
185+
t_affected_rows = mysql_affected_rows(getMySQL());
186+
t_success = -1 != t_affected_rows;
187+
}
188+
189+
if (t_result_set != NULL)
190+
mysql_free_result(t_result_set);
195191

196-
p_affected_rows = t_affected_rows;
192+
if (t_success && t_first)
193+
{
194+
p_affected_rows = t_affected_rows;
195+
t_first = false;
196+
}
197+
198+
if (t_success)
199+
{
200+
t_status = mysql_next_result(getMySQL());
201+
t_success = t_status <= 0;
202+
}
203+
}
197204

198205
// OK-2007-09-10 : Bug 5360
199-
if (t_result)
206+
if (t_success)
200207
errorMessageSet(NULL);
201208
else
202209
errorMessageSet(mysql_error(getMySQL()));
203210

204-
return t_result;
211+
return t_success;
205212
}
206213

207214

@@ -216,20 +223,50 @@ DBCursor *DBConnection_MYSQL::sqlQuery(char *p_query, DBString *p_arguments, int
216223
DBCursor_MYSQL *t_cursor;
217224
t_cursor = NULL;
218225

219-
if (ExecuteQuery(p_query, p_arguments, p_argument_count))
226+
bool t_success = true;
227+
228+
if (t_success)
229+
t_success = ExecuteQuery(p_query, p_arguments, p_argument_count);
230+
231+
int t_status = 0;
232+
while (t_success && (t_status == 0))
233+
{
234+
if (t_cursor == NULL)
235+
{
236+
t_cursor = new (nothrow) DBCursor_MYSQL();
237+
t_success = (t_cursor != NULL) && t_cursor->open((DBConnection*)this);
238+
}
239+
else
240+
{
241+
// we need to fetch all available result sets, even if we do nothing with them.
242+
MYSQL_RES *t_result_set;
243+
t_result_set = mysql_store_result(getMySQL());
244+
245+
t_success = (t_result_set != NULL) || (mysql_field_count(getMySQL()) == 0);
246+
if (t_result_set != NULL)
247+
mysql_free_result(t_result_set);
248+
}
249+
250+
if (t_success)
251+
{
252+
t_status = mysql_next_result(getMySQL());
253+
t_success = t_status <= 0;
254+
}
255+
}
256+
257+
if (t_cursor != NULL)
220258
{
221-
t_cursor = new (nothrow) DBCursor_MYSQL();
222-
if (!t_cursor -> open((DBConnection *)this))
259+
if (t_success)
260+
addCursor(t_cursor)
261+
else
223262
{
224263
delete t_cursor;
225264
t_cursor = NULL;
226265
}
227-
else
228-
addCursor(t_cursor);
229266
}
230267

231268
// OK-2007-09-10 : Bug 5360
232-
if (t_cursor != NULL)
269+
if (t_success)
233270
errorMessageSet(NULL);
234271
else
235272
errorMessageSet(mysql_error(getMySQL()));

0 commit comments

Comments
 (0)