Browse code

Moved mysql result from the connection to db_res_t to allow nested queries.

Andreas Granig authored on 20/09/2005 13:28:34
Showing 6 changed files
... ...
@@ -330,8 +330,8 @@ static int store_result(db_con_t* _h, db_res_t** _r)
330 330
 		return -2;
331 331
 	}
332 332
 
333
-	CON_RESULT(_h) = mysql_store_result(CON_CONNECTION(_h));
334
-	if (!CON_RESULT(_h)) {
333
+	MYRES_RESULT(*_r) = mysql_store_result(CON_CONNECTION(_h));
334
+	if (!MYRES_RESULT(*_r)) {
335 335
 		if (mysql_field_count(CON_CONNECTION(_h)) == 0) {
336 336
 			(*_r)->col.n = 0;
337 337
 			(*_r)->n = 0;
... ...
@@ -344,15 +344,16 @@ static int store_result(db_con_t* _h, db_res_t** _r)
344 344
 		}
345 345
 	}
346 346
 
347
-        if (convert_result(_h, *_r) < 0) {
347
+	if (convert_result(_h, *_r) < 0) {
348 348
 		LOG(L_ERR, "store_result: Error while converting result\n");
349
+		pkg_free((*_r)->data);
349 350
 		pkg_free(*_r);
350 351
 
351
-		     /* This cannot be used because if convert_result fails,
352
-		      * free_result will try to free rows and columns too 
353
-		      * and free will be called two times
354
-		      */
355
-		     /* free_result(*_r); */
352
+		/* This cannot be used because if convert_result fails,
353
+		 * free_result will try to free rows and columns too 
354
+		 * and free will be called two times
355
+		 */
356
+		/* free_result(*_r); */
356 357
 		return -4;
357 358
 	}
358 359
 	
... ...
@@ -374,8 +375,6 @@ int db_free_result(db_con_t* _h, db_res_t* _r)
374 375
 	     LOG(L_ERR, "db_free_result: Unable to free result structure\n");
375 376
 	     return -1;
376 377
      }
377
-     mysql_free_result(CON_RESULT(_h));
378
-     CON_RESULT(_h) = 0;
379 378
      return 0;
380 379
 }
381 380
 
... ...
@@ -109,7 +109,6 @@ struct my_con* new_connection(struct db_id* id)
109 109
 void free_connection(struct my_con* con)
110 110
 {
111 111
 	if (!con) return;
112
-	if (con->res) mysql_free_result(con->res);
113 112
 	if (con->id) free_db_id(con->id);
114 113
 	if (con->con) {
115 114
 		mysql_close(con->con);
... ...
@@ -40,9 +40,7 @@ struct my_con {
40 40
 	unsigned int ref;        /* Reference count */
41 41
 	struct pool_con* next;   /* Next connection in the pool */
42 42
 
43
-	MYSQL_RES* res;          /* Actual result */
44 43
 	MYSQL* con;              /* Connection representation */
45
-	MYSQL_ROW row;           /* Actual row in the result */
46 44
 	time_t timestamp;        /* Timestamp of last query */
47 45
 };
48 46
 
... ...
@@ -50,9 +48,7 @@ struct my_con {
50 48
 /*
51 49
  * Some convenience wrappers
52 50
  */
53
-#define CON_RESULT(db_con)     (((struct my_con*)((db_con)->tail))->res)
54 51
 #define CON_CONNECTION(db_con) (((struct my_con*)((db_con)->tail))->con)
55
-#define CON_ROW(db_con)        (((struct my_con*)((db_con)->tail))->row)
56 52
 #define CON_TIMESTAMP(db_con)  (((struct my_con*)((db_con)->tail))->timestamp)
57 53
 
58 54
 
... ...
@@ -70,7 +70,7 @@ static inline int get_columns(db_con_t* _h, db_res_t* _r)
70 70
 
71 71
 	RES_COL_N(_r) = n;
72 72
 
73
-	fields = mysql_fetch_fields(CON_RESULT(_h));
73
+	fields = mysql_fetch_fields(MYRES_RESULT(_r));
74 74
 	for(i = 0; i < n; i++) {
75 75
 		RES_NAMES(_r)[i] = fields[i].name;
76 76
 		switch(fields[i].type) {
... ...
@@ -143,7 +143,7 @@ static inline int convert_rows(db_con_t* _h, db_res_t* _r)
143 143
 		return -1;
144 144
 	}
145 145
 
146
-	n = mysql_num_rows(CON_RESULT(_h));
146
+	n = mysql_num_rows(MYRES_RESULT(_r));
147 147
 	RES_ROW_N(_r) = n;
148 148
 	if (!n) {
149 149
 		RES_ROWS(_r) = 0;
... ...
@@ -156,8 +156,8 @@ static inline int convert_rows(db_con_t* _h, db_res_t* _r)
156 156
 	}
157 157
 
158 158
 	for(i = 0; i < n; i++) {
159
-		CON_ROW(_h) = mysql_fetch_row(CON_RESULT(_h));
160
-		if (!CON_ROW(_h)) {
159
+		MYRES_ROW(_r) = mysql_fetch_row(MYRES_RESULT(_r));
160
+		if (!MYRES_ROW(_r)) {
161 161
 			LOG(L_ERR, "convert_rows: %s\n", mysql_error(CON_CONNECTION(_h)));
162 162
 			RES_ROW_N(_r) = i;
163 163
 			free_rows(_r);
... ...
@@ -201,6 +201,14 @@ db_res_t* new_result(void)
201 201
 		LOG(L_ERR, "new_result: No memory left\n");
202 202
 		return 0;
203 203
 	}
204
+	r->data = pkg_malloc(sizeof(struct my_res));
205
+	if(!r->data) {
206
+		pkg_free(r);
207
+		LOG(L_ERR, "store_result(): No memory left 2\n");
208
+		return 0;
209
+	}
210
+	MYRES_RESULT(r) = 0;
211
+	MYRES_ROW(r) = 0;
204 212
 	RES_NAMES(r) = 0;
205 213
 	RES_TYPES(r) = 0;
206 214
 	RES_COL_N(r) = 0;
... ...
@@ -246,6 +254,8 @@ int free_result(db_res_t* _r)
246 254
 
247 255
 	free_columns(_r);
248 256
 	free_rows(_r);
257
+	mysql_free_result(MYRES_RESULT(_r));
258
+	pkg_free(_r->data);
249 259
 	pkg_free(_r);
250 260
 	return 0;
251 261
 }
... ...
@@ -33,6 +33,13 @@
33 33
 #include "../../db/db_res.h"
34 34
 #include "../../db/db_con.h"
35 35
 
36
+struct my_res {
37
+	MYSQL_RES* res; /* The mysql result */
38
+	MYSQL_ROW  row; /* The current row */
39
+};
40
+
41
+#define MYRES_RESULT(db_res)   (((struct my_res*)(db_res)->data)->res)
42
+#define MYRES_ROW(db_res)      (((struct my_res*)(db_res)->data)->row)
36 43
 
37 44
 /*
38 45
  * Create a new result structure and initialize it
... ...
@@ -33,6 +33,7 @@
33 33
 #include <mysql/mysql.h>
34 34
 #include "val.h"
35 35
 #include "my_con.h"
36
+#include "res.h"
36 37
 #include "row.h"
37 38
 
38 39
 
... ...
@@ -56,11 +57,11 @@ int convert_row(db_con_t* _h, db_res_t* _res, db_row_t* _r)
56 57
 		return -1;
57 58
 	}
58 59
 
59
-	lengths = mysql_fetch_lengths(CON_RESULT(_h));
60
+	lengths = mysql_fetch_lengths(MYRES_RESULT(_res));
60 61
 
61 62
 	for(i = 0; i < RES_COL_N(_res); i++) {
62 63
 		if (str2val(RES_TYPES(_res)[i], &(ROW_VALUES(_r)[i]), 
63
-			    ((MYSQL_ROW)CON_ROW(_h))[i], lengths[i]) < 0) {
64
+				((MYSQL_ROW)MYRES_ROW(_res))[i], lengths[i]) < 0) {
64 65
 			LOG(L_ERR, "convert_row: Error while converting value\n");
65 66
 			free_row(_r);
66 67
 			return -3;