Explorar o código

- Data structures for database results, records, and fields
- db_row structure removed, obsoleted by db_rec

Jan Janak %!s(int64=18) %!d(string=hai) anos
pai
achega
e7b9f9db1d
Modificáronse 6 ficheiros con 360 adicións e 23 borrados
  1. 79 0
      db/db_fld.c
  2. 93 0
      db/db_fld.h
  3. 61 0
      db/db_rec.c
  4. 49 0
      db/db_rec.h
  5. 61 0
      db/db_res.c
  6. 17 23
      db/db_res.h

+ 79 - 0
db/db_fld.c

@@ -0,0 +1,79 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2005 FhG FOKUS
+ * Copyright (C) 2006-2007 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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 <string.h>
+#include "../mem/mem.h"
+#include "../dprint.h"
+#include "db_fld.h"
+
+
+int db_fld_init(db_fld_t* fld, size_t n)
+{
+	int i;
+
+	memset(fld, '\0', sizeof(db_fld_t) * n);
+	for(i = 0; i < n; i++) {
+		if (db_gen_init(&fld[i].gen) < 0) return -1;
+	}
+	return 0;
+}
+
+
+db_fld_t* db_fld(size_t n)
+{
+	db_fld_t* r;
+
+	r = (db_fld_t*)pkg_malloc(sizeof(db_fld_t) * n);
+	if (r == NULL) {
+		ERR("db_fld: No memory left\n");
+		return NULL;
+	}
+	if (db_fld_init(r, n) < 0) goto error;
+	return r;
+
+ error:
+	if (r) {
+		db_gen_free(&r->gen);
+		pkg_free(r);
+	}
+	return NULL;
+}
+
+
+void db_fld_free(db_fld_t* fld, size_t n)
+{
+    int i;
+    if (!fld || !n) return;
+
+	for(i = 0; i < n; i++) {
+		db_gen_free(&fld[i].gen);
+		if (fld[i].name.s) pkg_free(fld[i].name.s);
+	}
+	pkg_free(fld);
+}
+

+ 93 - 0
db/db_fld.h

@@ -0,0 +1,93 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 FhG Fokus
+ * Copyright (C) 2006-2007 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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
+ */
+
+#ifndef _DB_FLD_H
+#define _DB_FLD_H  1
+
+#include <time.h>
+#include "../str.h"
+#include "db_gen.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+
+enum db_fld_type {
+    DB_NONE = 0,   /* Bumper */
+    DB_INT,        /* 32-bit integer */
+    DB_FLOAT,      /* 32-bit fixed-precision number */
+    DB_DOUBLE,     /* double data type */
+    DB_CSTR,       /* Zero-terminated string */
+    DB_STR,        /* str structure */
+    DB_DATETIME,   /* Date and time in number of seconds since 1-Jan-1970 */
+    DB_BLOB,       /* Generic binary object*/
+    DB_BITMAP      /* Bitmap of flags */
+};
+
+enum db_fld_op {
+	DB_EQ = 0, /* The value of the field must be equal */
+	DB_LT,     /* The value of the field must be less than */
+	DB_GT,     /* The value of the field must be greater than */
+	DB_LEQ,    /* The value of the field must be let than or equal */
+	DB_GEQ     /* The value of the field must be greater than or equal */
+};
+
+enum db_flags {
+    DB_NULL = (1 << 0)
+};
+
+typedef struct db_fld {
+	db_gen_t gen;  /* Generic part of the structure */
+    str name;
+    enum db_fld_type type;
+    unsigned int flags;
+    union {
+		int          int4;   /* integer value */
+		float        flt;    /* float value */
+		double       dbl;    /* double value */
+		time_t       time;   /* unix time value */
+		const char*  cstr;   /* NULL terminated string */
+		str          str;    /* str string value */
+		str          blob;   /* Blob data */
+		unsigned int bitmap; /* Bitmap data type, 32 flags, should be enough */ 
+		long long    int8;   /* 8-byte integer */
+    } v;                     /* union of all possible types */
+	enum db_fld_op op;
+} db_fld_t;
+
+
+struct db_fld* db_fld(size_t n);
+int db_fld_init(struct db_fld* fld, size_t n);
+void db_fld_free(struct db_fld* fld, size_t n);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* _DB_FLD_H */

+ 61 - 0
db/db_rec.c

@@ -0,0 +1,61 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 FhG FOKUS
+ * Copyright (C) 2006-2007 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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 <stdlib.h>
+#include <string.h>
+#include "../dprint.h"
+#include "../mem/mem.h"
+#include "db_rec.h"
+
+
+db_rec_t* db_rec(void)
+{
+    db_rec_t* r;
+
+    r = (db_rec_t*)pkg_malloc(sizeof(db_rec_t));
+    if (r == NULL) goto err;
+    memset(r, '\0', sizeof(db_rec_t));
+	if (db_gen_init(&r->gen) < 0) goto err;
+    return r;
+
+ err:
+    ERR("db_rec: Cannot create db_rec structure\n");
+	if (r) {
+		db_gen_free(&r->gen);
+		pkg_free(r);
+	}
+    return NULL;
+}
+
+
+void db_rec_free(db_rec_t* r)
+{
+    if (r == NULL) return;
+	db_gen_free(&r->gen);
+    pkg_free(r);
+}

+ 49 - 0
db/db_rec.h

@@ -0,0 +1,49 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 FhG FOKUS
+ * Copyright (C) 2006-2007 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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
+ */
+
+#ifndef _DB_REC_H
+#define _DB_REC_H  1
+
+#include "db_gen.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+typedef struct db_rec {
+	db_gen_t gen; /* Generic part of the structure */
+} db_rec_t;
+
+struct db_rec* db_rec(void);
+void db_rec_free(struct db_rec* rec);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* _DB_REC_H */

+ 61 - 0
db/db_res.c

@@ -0,0 +1,61 @@
+/* 
+ * $Id$ 
+ *
+ * Copyright (C) 2001-2003 FhG FOKUS
+ * Copyright (C) 2006-2007 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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 <string.h>
+#include "../dprint.h"
+#include "../mem/mem.h"
+#include "db_res.h"
+
+
+db_res_t* db_res(db_cmd_t* cmd)
+{
+    db_res_t* r;
+
+    r = (db_res_t*)pkg_malloc(sizeof(db_res_t));
+    if (r == NULL) goto err;
+	memset(r, '\0', sizeof(db_res_t));
+	if (db_gen_init(&r->gen) < 0) goto err;
+    r->cmd = cmd;
+    return r;
+
+ err:
+    ERR("db_res: Cannot create db_res structure\n");
+	if (r) {
+		db_gen_free(&r->gen);
+		pkg_free(r);
+	}
+    return NULL;
+}
+
+
+void db_res_free(db_res_t* r)
+{
+    if (r == NULL) return;
+	db_gen_free(&r->gen);
+    pkg_free(r);
+}

+ 17 - 23
db/db_res.h

@@ -1,7 +1,8 @@
 /* 
  * $Id$ 
  *
- * Copyright (C) 2001-2003 FhG Fokus
+ * Copyright (C) 2001-2003 FhG FOKUS
+ * Copyright (C) 2006-2007 iptelorg GmbH
  *
  * This file is part of ser, a free SIP server.
  *
@@ -25,33 +26,26 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#ifndef _DB_RES_H
+#define _DB_RES_H  1
 
-#ifndef DB_RES_H
-#define DB_RES_H
-
-
-#include "db_row.h"
-#include "db_key.h"
-#include "db_val.h"
+#include "db_gen.h"
+#include "db_cmd.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
 
 typedef struct db_res {
-	struct {
-		db_key_t* names;   /* Column names */
-		db_type_t* types;  /* Column types */
-		int n;             /* Number of columns */
-	} col;
-	struct db_row* rows;       /* Rows */
-	int n;                     /* Number of rows */
-	void* data;                /* Auxiliary data */
+	db_gen_t gen;       /* Generic part of the structure */
+    struct db_cmd* cmd; /* Command that produced the result */
 } db_res_t;
 
+struct db_res* db_res(struct db_cmd* cmd);
+void db_res_free(struct db_res* res);
 
-#define RES_NAMES(re) ((re)->col.names)
-#define RES_TYPES(re) ((re)->col.types)
-#define RES_COL_N(re) ((re)->col.n)
-#define RES_ROWS(re)  ((re)->rows)
-#define RES_ROW_N(re) ((re)->n)
-
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 
-#endif /* DB_RES_H */
+#endif /* _DB_RES_H */