Ver Fonte

- small cleanup

Jan Janak há 22 anos atrás
pai
commit
95f549329d

+ 0 - 3
modules/db_mysql/Makefile

@@ -11,9 +11,6 @@ DEFS +=-I$(LOCALBASE)/include -I$(LOCALBASE)/include/mysql \
 		-I$(LOCALBASE)/mysql/include -I/usr/pkg/include \
 		-I/usr/include/mysql
 
-# Database API, standard includes
-DEFS +=-I../../db -I../.. -I../../mem
-
 # libmysqlclient locations on RH/Suse, Solaris /OpenBSD, FreeBSD
 # (Debian does the right thing and puts it in /usr/lib)
 LIBS=-L/usr/lib/mysql -L$(LOCALBASE)/lib -L$(LOCALBASE)/lib/mysql \

+ 2 - 3
modules/db_mysql/db_con.c

@@ -32,7 +32,6 @@
 #include "../../db/db.h"
 #include "../../dprint.h"
 #include "../../mem/mem.h"
-#include "defs.h"
 
 
 /*
@@ -43,12 +42,12 @@ int use_table(db_con_t* _h, const char* _t)
 {
 	char* ptr;
 	int l;
-#ifdef PARANOID
+
 	if ((!_h) || (!_t)) {
 		LOG(L_ERR, "use_table(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	l = strlen(_t) + 1;
 	ptr = (char*)pkg_malloc(l);
 	if (!ptr) {

+ 3 - 5
modules/db_mysql/db_mod.c

@@ -33,7 +33,7 @@
  *  2003-03-16  flags export parameter added (janakj)
  */
 
-#include <sr_module.h>
+#include "../../sr_module.h"
 #include "dbase.h"
 
 
@@ -43,8 +43,7 @@ MODULE_VERSION
 /*
  * MySQL database module interface
  */
-
-static cmd_export_t cmds[]={
+static cmd_export_t cmds[] = {
 	{"db_use_table",  (cmd_function)use_table,     2, 0, 0},
 	{"db_init",       (cmd_function)db_init,       1, 0, 0},
 	{"db_close",      (cmd_function)db_close,      2, 0, 0},
@@ -54,7 +53,7 @@ static cmd_export_t cmds[]={
 	{"db_insert",     (cmd_function)db_insert,     2, 0, 0},
 	{"db_delete",     (cmd_function)db_delete,     2, 0, 0},
 	{"db_update",     (cmd_function)db_update,     2, 0, 0},
-	{0,0,0,0,0}
+	{0, 0, 0, 0, 0}
 };
 
 
@@ -62,7 +61,6 @@ struct module_exports exports = {
 	"mysql",
 	cmds,
 	0,   /*  module paramers */
-
 	0,   /* module initialization function */
 	0,   /* response function*/
 	0,   /* destroy function */

+ 35 - 44
modules/db_mysql/dbase.c

@@ -28,40 +28,41 @@
  */
 
 #include <stdio.h>
-#include <mem.h>
-#include <mysql.h>
-#include <dprint.h>
 #include <string.h>
 #include <stdlib.h>
-#include "defs.h"
+#include <mysql/mysql.h>
+#include "../../mem/mem.h"
+#include "../../dprint.h"
 #include "utils.h"
 #include "val.h"
 #include "con_mysql.h"
 #include "res.h"
 #include "dbase.h"
 
+
+#define SQL_BUF_LEN 65536
+
 static char sql_buf[SQL_BUF_LEN];
 
 
 /*
- * Establish database connection,
+ * Establish a database connection,
  * returns 1 on success, 0 otherwise
  * _h is a handle used in communication with database
  *
  * URL is in form mysql://user:password@host:port/database
  */
-static inline int connect_db(db_con_t* _h, const char* _db_url)
+static int connect_db(db_con_t* _h, const char* _db_url)
 {
 	int p, l, res;
 	char* user, *password, *host, *port, *database;
 	char* buf;
 
-#ifdef PARANOID
 	if ((!_h) || (!_db_url)) {
 		LOG(L_ERR, "connect_db(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	CON_CONNECTED(_h) = 0;
 
 	     /* Make a scratch pad copy of given SQL URL */
@@ -115,14 +116,13 @@ static inline int connect_db(db_con_t* _h, const char* _db_url)
  * disconnects database connection represented by _handle
  * returns 1 on success, 0 otherwise
  */
-static inline int disconnect_db(db_con_t* _h)
+static int disconnect_db(db_con_t* _h)
 {
-#ifdef PARANOID
 	if (!_h) {
 		LOG(L_ERR, "disconnect_db(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	if (CON_CONNECTED(_h) == 1) {
 		mysql_close(CON_CONNECTION(_h));
 		CON_CONNECTED(_h) = 0;
@@ -137,14 +137,13 @@ static inline int disconnect_db(db_con_t* _h)
 /*
  * Send an SQL query to the server
  */
-static inline int submit_query(db_con_t* _h, const char* _s)
+static int submit_query(db_con_t* _h, const char* _s)
 {	
-#ifdef PARANOID
 	if ((!_h) || (!_s)) {
 		LOG(L_ERR, "submit_query(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	/* screws up the terminal when the query contains a BLOB :-( (by bogdan)
 	 * DBG("submit_query(): %s\n", _s);
 	 */
@@ -160,16 +159,16 @@ static inline int submit_query(db_con_t* _h, const char* _s)
 /*
  * Print list of columns separated by comma
  */
-static inline int print_columns(char* _b, int _l, db_key_t* _c, int _n)
+static int print_columns(char* _b, int _l, db_key_t* _c, int _n)
 {
 	int i;
 	int res = 0;
-#ifdef PARANOID
+
 	if ((!_c) || (!_n) || (!_b) || (!_l)) {
 		LOG(L_ERR, "print_columns(): Invalid parameter value\n");
 		return 0;
 	}
-#endif
+
 	for(i = 0; i < _n; i++) {
 		if (i == (_n - 1)) {
 			res += snprintf(_b + res, _l - res, "%s ", _c[i]);
@@ -184,15 +183,14 @@ static inline int print_columns(char* _b, int _l, db_key_t* _c, int _n)
 /*
  * Print list of values separated by comma
  */
-static inline int print_values(char* _b, int _l, db_val_t* _v, int _n)
+static int print_values(char* _b, int _l, db_val_t* _v, int _n)
 {
 	int i, res = 0, l;
-#ifdef PARANOID
+
 	if ((!_b) || (!_l) || (!_v) || (!_n)) {
 		LOG(L_ERR, "print_values(): Invalid parameter value\n");
 		return 0;
 	}
-#endif
 
 	for(i = 0; i < _n; i++) {
 		l = _l - res;
@@ -213,17 +211,17 @@ static inline int print_values(char* _b, int _l, db_val_t* _v, int _n)
 /*
  * Print where clause of SQL statement
  */
-static inline int print_where(char* _b, int _l, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n)
+static int print_where(char* _b, int _l, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n)
 {
 	int i;
 	int res = 0;
 	int l;
-#ifdef PARANOID
+
 	if ((!_b) || (!_l) || (!_k) || (!_v) || (!_n)) {
 		LOG(L_ERR, "print_where(): Invalid parameter value\n");
 		return 0;
 	}
-#endif
+
 	for(i = 0; i < _n; i++) {
 		if (_o) {
 			res += snprintf(_b + res, _l - res, "%s%s", _k[i], _o[i]);
@@ -244,17 +242,17 @@ static inline int print_where(char* _b, int _l, db_key_t* _k, db_op_t* _o, db_va
 /*
  * Print set clause of update SQL statement
  */
-static inline int print_set(char* _b, int _l, db_key_t* _k, db_val_t* _v, int _n)
+static int print_set(char* _b, int _l, db_key_t* _k, db_val_t* _v, int _n)
 {
 	int i;
 	int res = 0;
 	int l;
-#ifdef PARANOID
+
 	if ((!_b) || (!_l) || (!_k) || (!_v) || (!_n)) {
 		LOG(L_ERR, "print_set(): Invalid parameter value\n");
 		return 0;
 	}
-#endif
+
 	for(i = 0; i < _n; i++) {
 		res += snprintf(_b + res, _l - res, "%s=", _k[i]);
 		l = _l - res;
@@ -277,12 +275,11 @@ static inline int print_set(char* _b, int _l, db_key_t* _k, db_val_t* _v, int _n
 db_con_t* db_init(const char* _sqlurl)
 {
 	db_con_t* res;
-#ifdef PARANOID
+
 	if (!_sqlurl) {
 		LOG(L_ERR, "db_init(): Invalid parameter value\n");
 		return 0;
 	}
-#endif
 
 	res = pkg_malloc(sizeof(db_con_t) + sizeof(struct con_mysql));
 	if (!res) {
@@ -308,12 +305,11 @@ db_con_t* db_init(const char* _sqlurl)
  */
 void db_close(db_con_t* _h)
 {
-#ifdef PARANOID
 	if (!_h) {
 		LOG(L_ERR, "db_close(): Invalid parameter value\n");
 		return;
 	}
-#endif
+
 	disconnect_db(_h);
 	if (CON_RESULT(_h)) {
 		mysql_free_result(CON_RESULT(_h));
@@ -330,12 +326,10 @@ void db_close(db_con_t* _h)
  */
 int get_result(db_con_t* _h, db_res_t** _r)
 {
-#ifdef PARANOID
 	if ((!_h) || (!_r)) {
 		LOG(L_ERR, "get_result(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
 
 	*_r = new_result();
 	if (*_r == 0) {
@@ -378,12 +372,11 @@ int get_result(db_con_t* _h, db_res_t** _r)
  */
 int db_free_query(db_con_t* _h, db_res_t* _r)
 {
-#ifdef PARANOID
      if ((!_h) || (!_r)) {
 	     LOG(L_ERR, "db_free_query(): Invalid parameter value\n");
 	     return -1;
      }
-#endif
+
      if (free_result(_r) < 0) {
 	     LOG(L_ERR, "free_query(): Unable to free result structure\n");
 	     return -1;
@@ -410,12 +403,12 @@ int db_query(db_con_t* _h, db_key_t* _k, db_op_t* _op,
 	     db_key_t _o, db_res_t** _r)
 {
 	int off;
-#ifdef PARANOID
+
 	if ((!_h) || (!_r)) {
 		LOG(L_ERR, "db_query(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	if (!_c) {
 		off = snprintf(sql_buf, SQL_BUF_LEN, "select * from %s ", CON_TABLE(_h));
 	} else {
@@ -445,12 +438,10 @@ int db_query(db_con_t* _h, db_key_t* _k, db_op_t* _op,
  */
 int db_raw_query(db_con_t* _h, char* _s, db_res_t** _r)
 {
-#ifdef PARANOID
 	if ((!_h) || (!_s)) {
 		LOG(L_ERR, "db_raw_query(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
 
 	if (submit_query(_h, _s) < 0) {
 		LOG(L_ERR, "submit_query(): Error while submitting query\n");
@@ -473,12 +464,12 @@ int db_raw_query(db_con_t* _h, char* _s, db_res_t** _r)
 int db_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n)
 {
 	int off;
-#ifdef PARANOID
+
 	if ((!_h) || (!_k) || (!_v) || (!_n)) {
 		LOG(L_ERR, "db_insert(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	off = snprintf(sql_buf, SQL_BUF_LEN, "insert into %s (", CON_TABLE(_h));
 	off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n);
 	off += snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values (");
@@ -505,12 +496,12 @@ int db_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n)
 int db_delete(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n)
 {
 	int off;
-#ifdef PARANOID
+
 	if (!_h) {
 		LOG(L_ERR, "db_delete(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	off = snprintf(sql_buf, SQL_BUF_LEN, "delete from %s", CON_TABLE(_h));
 	if (_n) {
 		off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where ");
@@ -539,12 +530,12 @@ int db_update(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
 	      db_key_t* _uk, db_val_t* _uv, int _n, int _un)
 {
 	int off;
-#ifdef PARANOID
+
 	if ((!_h) || (!_uk) || (!_uv) || (!_un)) {
 		LOG(L_ERR, "db_update(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	off = snprintf(sql_buf, SQL_BUF_LEN, "update %s set ", CON_TABLE(_h));
 	off += print_set(sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un);
 	if (_n) {

+ 6 - 5
modules/db_mysql/dbase.h

@@ -31,11 +31,12 @@
 #ifndef DBASE_H
 #define DBASE_H
 
-#include <db_con.h>
-#include <db_res.h>
-#include <db_key.h>
-#include <db_op.h>
-#include <db_val.h>
+
+#include "../../db/db_con.h"
+#include "../../db/db_res.h"
+#include "../../db/db_key.h"
+#include "../../db/db_op.h"
+#include "../../db/db_val.h"
 
 
 /*

+ 12 - 16
modules/db_mysql/res.c

@@ -28,11 +28,10 @@
  */
 
 
-#include <mysql.h>
-#include <mem.h>
-#include <dprint.h>
+#include <mysql/mysql.h>
+#include "../../mem/mem.h"
+#include "../../dprint.h"
 #include "row.h"
-#include "defs.h"
 #include "con_mysql.h"
 #include "res.h"
 
@@ -44,12 +43,12 @@ static inline int get_columns(db_con_t* _h, db_res_t* _r)
 {
 	int n, i;
 	MYSQL_FIELD* fields;
-#ifdef PARANOID
+
 	if ((!_h) || (!_r)) {
 		LOG(L_ERR, "get_columns(): Invalid parameter\n");
 		return -1;
 	}
-#endif
+
 	n = mysql_field_count(CON_CONNECTION(_h));
 	if (!n) {
 		LOG(L_ERR, "get_columns(): No columns\n");
@@ -118,12 +117,12 @@ static inline int get_columns(db_con_t* _h, db_res_t* _r)
 static inline int free_rows(db_res_t* _r)
 {
 	int i;
-#ifdef PARANOID
+
 	if (!_r) {
 		LOG(L_ERR, "free_rows(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	for(i = 0; i < RES_ROW_N(_r); i++) {
 		free_row(&(RES_ROWS(_r)[i]));
 	}
@@ -138,12 +137,12 @@ static inline int free_rows(db_res_t* _r)
 static inline int convert_rows(db_con_t* _h, db_res_t* _r)
 {
 	int n, i;
-#ifdef PARANOID
+
 	if ((!_h) || (!_r)) {
 		LOG(L_ERR, "convert_rows(): Invalid parameter\n");
 		return -1;
 	}
-#endif
+
 	n = mysql_num_rows(CON_RESULT(_h));
 	RES_ROW_N(_r) = n;
 	if (!n) {
@@ -180,12 +179,11 @@ static inline int convert_rows(db_con_t* _h, db_res_t* _r)
  */
 static inline int free_columns(db_res_t* _r)
 {
-#ifdef PARANOID
 	if (!_r) {
 		LOG(L_ERR, "free_columns(): Invalid parameter\n");
 		return -1;
 	}
-#endif
+
 	if (RES_NAMES(_r)) pkg_free(RES_NAMES(_r));
 	if (RES_TYPES(_r)) pkg_free(RES_TYPES(_r));
 	return 0;
@@ -217,12 +215,11 @@ db_res_t* new_result(void)
  */
 int convert_result(db_con_t* _h, db_res_t* _r)
 {
-#ifdef PARANOID
 	if ((!_h) || (!_r)) {
 		LOG(L_ERR, "convert_result(): Invalid parameter\n");
 		return -1;
 	}
-#endif
+
 	if (get_columns(_h, _r) < 0) {
 		LOG(L_ERR, "convert_result(): Error while getting column names\n");
 		return -2;
@@ -242,12 +239,11 @@ int convert_result(db_con_t* _h, db_res_t* _r)
  */
 int free_result(db_res_t* _r)
 {
-#ifdef PARANOID
 	if (!_r) {
 		LOG(L_ERR, "free_result(): Invalid parameter\n");
 		return -1;
 	}
-#endif
+
 	free_columns(_r);
 	free_rows(_r);
 	pkg_free(_r);

+ 2 - 2
modules/db_mysql/res.h

@@ -30,8 +30,8 @@
 #ifndef RES_H
 #define RES_H
 
-#include <db_res.h>
-#include <db_con.h>
+#include "../../db/db_res.h"
+#include "../../db/db_con.h"
 
 
 /*

+ 4 - 6
modules/db_mysql/row.c

@@ -28,8 +28,8 @@
  */
 
 
-#include <dprint.h>
-#include <mem.h>
+#include "../../dprint.h"
+#include "../../mem/mem.h"
 #include <mysql/mysql.h>
 #include "val.h"
 #include "con_mysql.h"
@@ -43,12 +43,11 @@ int convert_row(db_con_t* _h, db_res_t* _res, db_row_t* _r)
 {
 	unsigned long* lengths;
 	int i;
-#ifndef PARANOID
+
 	if ((!_h) || (!_res) || (!_r)) {
 		LOG(L_ERR, "convert_row(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
 
 	ROW_VALUES(_r) = (db_val_t*)pkg_malloc(sizeof(db_val_t) * RES_COL_N(_res));
 	ROW_N(_r) = RES_COL_N(_res);
@@ -76,12 +75,11 @@ int convert_row(db_con_t* _h, db_res_t* _res, db_row_t* _r)
  */
 int free_row(db_row_t* _r)
 {
-#ifndef PARANOID
 	if (!_r) {
 		LOG(L_ERR, "free_row(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	if (ROW_VALUES(_r)) pkg_free(ROW_VALUES(_r));
 	return 0;
 }

+ 3 - 3
modules/db_mysql/row.h

@@ -30,9 +30,9 @@
 #ifndef ROW_H
 #define ROW_H
 
-#include <db_con.h>
-#include <db_res.h>
-#include <db_row.h>
+#include "../../db/db_con.h"
+#include "../../db/db_res.h"
+#include "../../db/db_row.h"
 
 
 /*

+ 0 - 1
modules/db_mysql/utils.c

@@ -36,7 +36,6 @@
 
 #include <strings.h>
 #include <string.h>
-#include "defs.h"
 #include "utils.h"
 
 

+ 10 - 19
modules/db_mysql/val.c

@@ -26,12 +26,11 @@
  */
 
 #include <stdio.h>
-#include <dprint.h>
 #include <stdlib.h>
 #include <string.h>
-#include <mysql.h>
+#include "../../dprint.h"
+#include <mysql/mysql.h>
 #include "utils.h"
-#include "defs.h"
 #include "val.h"
 
 
@@ -40,12 +39,11 @@
  */
 static inline int str2int(const char* _s, int* _v)
 {
-#ifdef PARANOID
 	if ((!_s) || (!_v)) {
 		LOG(L_ERR, "str2int(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	*_v = atoi(_s);
 	return 0;
 }
@@ -56,12 +54,11 @@ static inline int str2int(const char* _s, int* _v)
  */
 static inline int str2double(const char* _s, double* _v)
 {
-#ifdef PARANOID
 	if ((!_s) || (!_v)) {
 		LOG(L_ERR, "str2double(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	*_v = atof(_s);
 	return 0;
 }
@@ -72,12 +69,11 @@ static inline int str2double(const char* _s, double* _v)
  */
 static inline int str2time(const char* _s, time_t* _v)
 {
-#ifdef PARANOID
 	if ((!_s) || (!_v)) {
 		LOG(L_ERR, "str2time(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	*_v = mysql2time(_s);
 	return 0;
 }
@@ -88,12 +84,11 @@ static inline int str2time(const char* _s, time_t* _v)
  */
 static inline int int2str(int _v, char* _s, int* _l)
 {
-#ifdef PARANOID
 	if ((!_s) || (!_l) || (!*_l)) {
 		LOG(L_ERR, "int2str(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	*_l = snprintf(_s, *_l, "%-d", _v);
 	return 0;
 }
@@ -104,12 +99,11 @@ static inline int int2str(int _v, char* _s, int* _l)
  */
 static inline int double2str(double _v, char* _s, int* _l)
 {
-#ifdef PARANOID
 	if ((!_s) || (!_l) || (!*_l)) {
 		LOG(L_ERR, "double2str(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	*_l = snprintf(_s, *_l, "%-10.2f", _v);
 	return 0;
 }
@@ -121,12 +115,12 @@ static inline int double2str(double _v, char* _s, int* _l)
 static inline int time2str(time_t _v, char* _s, int* _l)
 {
 	int l;
-#ifdef PARANOID
+
 	if ((!_s) || (!_l) || (*_l < 2))  {
 		LOG(L_ERR, "Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	*_s++ = '\'';
 	l = time2mysql(_v, _s, *_l - 1);
 	*(_s + l) = '\'';
@@ -139,12 +133,10 @@ static inline int time2str(time_t _v, char* _s, int* _l)
  */
 int str2val(db_type_t _t, db_val_t* _v, const char* _s, int _l)
 {
-#ifdef PARANOID
 	if (!_v) {
 		LOG(L_ERR, "str2val(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
 
 	if (!_s) {
 		VAL_TYPE(_v) = _t;
@@ -223,12 +215,11 @@ int val2str(db_val_t* _v, char* _s, int* _len)
 	int l;
 	char* old_s;
 
-#ifdef PARANOID
 	if ((!_v) || (!_s) || (!_len) || (!*_len)) {
 		LOG(L_ERR, "val2str(): Invalid parameter value\n");
 		return -1;
 	}
-#endif
+
 	if (VAL_NULL(_v)) {
 		*_len = snprintf(_s, *_len, "NULL");
 		return 0;

+ 1 - 1
modules/db_mysql/val.h

@@ -28,7 +28,7 @@
 #ifndef VAL_H
 #define VAL_H
 
-#include <db_val.h>
+#include "../../db/db_val.h"
 
 
 /*