Browse Source

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

Andreas Granig 20 years ago
parent
commit
1b8946fded

+ 9 - 10
modules/db_mysql/dbase.c

@@ -330,8 +330,8 @@ static int store_result(db_con_t* _h, db_res_t** _r)
 		return -2;
 	}
 
-	CON_RESULT(_h) = mysql_store_result(CON_CONNECTION(_h));
-	if (!CON_RESULT(_h)) {
+	MYRES_RESULT(*_r) = mysql_store_result(CON_CONNECTION(_h));
+	if (!MYRES_RESULT(*_r)) {
 		if (mysql_field_count(CON_CONNECTION(_h)) == 0) {
 			(*_r)->col.n = 0;
 			(*_r)->n = 0;
@@ -344,15 +344,16 @@ static int store_result(db_con_t* _h, db_res_t** _r)
 		}
 	}
 
-        if (convert_result(_h, *_r) < 0) {
+	if (convert_result(_h, *_r) < 0) {
 		LOG(L_ERR, "store_result: Error while converting result\n");
+		pkg_free((*_r)->data);
 		pkg_free(*_r);
 
-		     /* This cannot be used because if convert_result fails,
-		      * free_result will try to free rows and columns too 
-		      * and free will be called two times
-		      */
-		     /* free_result(*_r); */
+		/* This cannot be used because if convert_result fails,
+		 * free_result will try to free rows and columns too 
+		 * and free will be called two times
+		 */
+		/* free_result(*_r); */
 		return -4;
 	}
 	
@@ -374,8 +375,6 @@ int db_free_result(db_con_t* _h, db_res_t* _r)
 	     LOG(L_ERR, "db_free_result: Unable to free result structure\n");
 	     return -1;
      }
-     mysql_free_result(CON_RESULT(_h));
-     CON_RESULT(_h) = 0;
      return 0;
 }
 

+ 0 - 1
modules/db_mysql/my_con.c

@@ -109,7 +109,6 @@ struct my_con* new_connection(struct db_id* id)
 void free_connection(struct my_con* con)
 {
 	if (!con) return;
-	if (con->res) mysql_free_result(con->res);
 	if (con->id) free_db_id(con->id);
 	if (con->con) {
 		mysql_close(con->con);

+ 0 - 4
modules/db_mysql/my_con.h

@@ -40,9 +40,7 @@ struct my_con {
 	unsigned int ref;        /* Reference count */
 	struct pool_con* next;   /* Next connection in the pool */
 
-	MYSQL_RES* res;          /* Actual result */
 	MYSQL* con;              /* Connection representation */
-	MYSQL_ROW row;           /* Actual row in the result */
 	time_t timestamp;        /* Timestamp of last query */
 };
 
@@ -50,9 +48,7 @@ struct my_con {
 /*
  * Some convenience wrappers
  */
-#define CON_RESULT(db_con)     (((struct my_con*)((db_con)->tail))->res)
 #define CON_CONNECTION(db_con) (((struct my_con*)((db_con)->tail))->con)
-#define CON_ROW(db_con)        (((struct my_con*)((db_con)->tail))->row)
 #define CON_TIMESTAMP(db_con)  (((struct my_con*)((db_con)->tail))->timestamp)
 
 

+ 14 - 4
modules/db_mysql/res.c

@@ -70,7 +70,7 @@ static inline int get_columns(db_con_t* _h, db_res_t* _r)
 
 	RES_COL_N(_r) = n;
 
-	fields = mysql_fetch_fields(CON_RESULT(_h));
+	fields = mysql_fetch_fields(MYRES_RESULT(_r));
 	for(i = 0; i < n; i++) {
 		RES_NAMES(_r)[i] = fields[i].name;
 		switch(fields[i].type) {
@@ -143,7 +143,7 @@ static inline int convert_rows(db_con_t* _h, db_res_t* _r)
 		return -1;
 	}
 
-	n = mysql_num_rows(CON_RESULT(_h));
+	n = mysql_num_rows(MYRES_RESULT(_r));
 	RES_ROW_N(_r) = n;
 	if (!n) {
 		RES_ROWS(_r) = 0;
@@ -156,8 +156,8 @@ static inline int convert_rows(db_con_t* _h, db_res_t* _r)
 	}
 
 	for(i = 0; i < n; i++) {
-		CON_ROW(_h) = mysql_fetch_row(CON_RESULT(_h));
-		if (!CON_ROW(_h)) {
+		MYRES_ROW(_r) = mysql_fetch_row(MYRES_RESULT(_r));
+		if (!MYRES_ROW(_r)) {
 			LOG(L_ERR, "convert_rows: %s\n", mysql_error(CON_CONNECTION(_h)));
 			RES_ROW_N(_r) = i;
 			free_rows(_r);
@@ -201,6 +201,14 @@ db_res_t* new_result(void)
 		LOG(L_ERR, "new_result: No memory left\n");
 		return 0;
 	}
+	r->data = pkg_malloc(sizeof(struct my_res));
+	if(!r->data) {
+		pkg_free(r);
+		LOG(L_ERR, "store_result(): No memory left 2\n");
+		return 0;
+	}
+	MYRES_RESULT(r) = 0;
+	MYRES_ROW(r) = 0;
 	RES_NAMES(r) = 0;
 	RES_TYPES(r) = 0;
 	RES_COL_N(r) = 0;
@@ -246,6 +254,8 @@ int free_result(db_res_t* _r)
 
 	free_columns(_r);
 	free_rows(_r);
+	mysql_free_result(MYRES_RESULT(_r));
+	pkg_free(_r->data);
 	pkg_free(_r);
 	return 0;
 }

+ 7 - 0
modules/db_mysql/res.h

@@ -33,6 +33,13 @@
 #include "../../db/db_res.h"
 #include "../../db/db_con.h"
 
+struct my_res {
+	MYSQL_RES* res; /* The mysql result */
+	MYSQL_ROW  row; /* The current row */
+};
+
+#define MYRES_RESULT(db_res)   (((struct my_res*)(db_res)->data)->res)
+#define MYRES_ROW(db_res)      (((struct my_res*)(db_res)->data)->row)
 
 /*
  * Create a new result structure and initialize it

+ 3 - 2
modules/db_mysql/row.c

@@ -33,6 +33,7 @@
 #include <mysql/mysql.h>
 #include "val.h"
 #include "my_con.h"
+#include "res.h"
 #include "row.h"
 
 
@@ -56,11 +57,11 @@ int convert_row(db_con_t* _h, db_res_t* _res, db_row_t* _r)
 		return -1;
 	}
 
-	lengths = mysql_fetch_lengths(CON_RESULT(_h));
+	lengths = mysql_fetch_lengths(MYRES_RESULT(_res));
 
 	for(i = 0; i < RES_COL_N(_res); i++) {
 		if (str2val(RES_TYPES(_res)[i], &(ROW_VALUES(_r)[i]), 
-			    ((MYSQL_ROW)CON_ROW(_h))[i], lengths[i]) < 0) {
+				((MYSQL_ROW)MYRES_ROW(_res))[i], lengths[i]) < 0) {
 			LOG(L_ERR, "convert_row: Error while converting value\n");
 			free_row(_r);
 			return -3;