Browse Source

- more database interface cleanup
- move the free_rows function to db/db_res.c
- move the free_row function to db/db_row.c
- change mysql and unixodbc to use this implementation
- the postgresql uses a different implementation, this still needs some
investigation
- move common used print_columns function to db_ut.c
- change the mysql, postgresql and unixodbc modules to use this implementation
- use the right mysql and postgresql escape function for BLOBs instead of the
deprecated function which could produce errors in certain corner cases
- extends comments in db dir and database modules


git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@2185 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt 18 years ago
parent
commit
0eece1d296
6 changed files with 140 additions and 4 deletions
  1. 53 0
      lib/srdb1/db_res.c
  2. 4 0
      lib/srdb1/db_res.h
  3. 41 0
      lib/srdb1/db_row.c
  4. 4 0
      lib/srdb1/db_row.h
  5. 34 4
      lib/srdb1/db_ut.c
  6. 4 0
      lib/srdb1/db_ut.h

+ 53 - 0
lib/srdb1/db_res.c

@@ -0,0 +1,53 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 FhG Fokus
+ * Copyright (C) 2007 1und1 Internet AG
+ *
+ * This file is part of openser, a free SIP server.
+ *
+ * openser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * openser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "db_res.h"
+
+#include "db_row.h"
+#include "../dprint.h"
+#include "../mem/mem.h"
+
+/*
+ * Release memory used by rows
+ */
+inline int free_rows(db_res_t* _r)
+{
+	int i;
+
+	if (!_r) {
+		LOG(L_ERR, "ERROR:free_rows: Invalid parameter value\n");
+		return -1;
+	}
+	LOG(L_DBG, "DEBUG:free_rows: Freeing %d rows\n", RES_ROW_N(_r));
+
+	for(i = 0; i < RES_ROW_N(_r); i++) {
+		LOG(L_DBG, "DEBUG:free_row: Row[%d]=%p\n", i, &(RES_ROWS(_r)[i]));
+		free_row(&(RES_ROWS(_r)[i]));
+	}
+	if (RES_ROWS(_r)) {
+		LOG(L_DBG, "DEBUG:free_rows: %p=pkg_free() RES_ROWS\n", RES_ROWS(_r));
+		pkg_free(RES_ROWS(_r));
+		RES_ROWS(_r) = NULL;
+	}
+	return 0;
+}

+ 4 - 0
lib/srdb1/db_res.h

@@ -51,5 +51,9 @@ typedef struct db_res {
 #define RES_LAST_ROW(re)  ((re)->last_row)
 #define RES_NUM_ROWS(re) ((re)->res_rows)
 
+/*
+ * Release memory used by rows in a result structure
+ */
+int free_rows(db_res_t* _r);
 
 #endif /* DB_RES_H */

+ 41 - 0
lib/srdb1/db_row.c

@@ -0,0 +1,41 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 FhG Fokus
+ * Copyright (C) 2007 1und1 Internet AG
+ *
+ * This file is part of openser, a free SIP server.
+ *
+ * openser is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * openser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "db_row.h"
+
+#include "../dprint.h"
+#include "../mem/mem.h"
+
+/*
+ * Release memory used by row
+ */
+inline int free_row(db_row_t* _r)
+{
+	if (!_r) {
+		LOG(L_ERR, "ERROR:free_row: Invalid parameter value\n");
+		return -1;
+	}
+
+	if (ROW_VALUES(_r)) pkg_free(ROW_VALUES(_r));
+	return 0;
+}

+ 4 - 0
lib/srdb1/db_row.h

@@ -39,5 +39,9 @@ typedef struct db_row {
 #define ROW_VALUES(rw) ((rw)->values)
 #define ROW_N(rw)      ((rw)->n)
 
+/*
+ * Release memory used by row
+ */
+int free_row(db_row_t* _r);
 
 #endif /* DB_ROW_H */

+ 34 - 4
lib/srdb1/db_ut.c

@@ -124,8 +124,7 @@ inline int db_double2str(double _v, char* _s, int* _l)
  */
 inline int db_str2time(const char* _s, time_t* _v)
 {
-	if ((!_s) || (!_v))
-	{
+	if ((!_s) || (!_v)) {
 		LOG(L_ERR, "ERROR:db_str2time: Invalid parameter value\n");
 		return -1;
 	}
@@ -155,8 +154,7 @@ inline int db_time2str(time_t _v, char* _s, int* _l)
 {
 	int l;
 
-	if ((!_s) || (!_l) || (*_l < 2))
-	{
+	if ((!_s) || (!_l) || (*_l < 2)) {
 		LOG(L_ERR, "ERROR:db_time2str: Invalid parameter value\n");
 		return -1;
 	}
@@ -172,3 +170,35 @@ inline int db_time2str(time_t _v, char* _s, int* _l)
 	*_l = l + 2;
 	return 0;
 }
+
+
+/*
+ * Print list of columns separated by comma
+ */
+inline int db_print_columns(char* _b, int _l, db_key_t* _c, int _n)
+{
+	int i, ret;
+	int len = 0;
+
+	if ((!_c) || (!_n) || (!_b) || (!_l)) {
+		LOG(L_ERR, "ERROR:print_columns: Invalid parameter value\n");
+		return -1;
+	}
+
+	for(i = 0; i < _n; i++)	{
+		if (i == (_n - 1)) {
+			ret = snprintf(_b + len, _l - len, "%s ", _c[i]);
+			if (ret < 0 || ret >= (_l - len)) goto error;
+			len += ret;
+		} else {
+			ret = snprintf(_b + len, _l - len, "%s,", _c[i]);
+			if (ret < 0 || ret >= (_l - len)) goto error;
+			len += ret;
+		}
+	}
+	return len;
+
+	error:
+	LOG(L_ERR, "ERROR:print_columns: Error in snprintf\n");
+	return -1;
+}

+ 4 - 0
lib/srdb1/db_ut.h

@@ -35,6 +35,7 @@
 #undef _XOPEN_SOURCE
 #undef _XOPEN_SOURCE_EXTENDED
 
+#include "db_key.h"
 
 int db_str2int(const char* _s, int* _v);
 
@@ -47,4 +48,7 @@ int db_double2str(double _v, char* _s, int* _l);
 int db_time2str(time_t _v, char* _s, int* _l);
 
 int db_str2time(const char* _s, time_t* _v);
+
+int db_print_columns(char* _b, int _l, db_key_t* _c, int _n);
+
 #endif