Просмотр исходного кода

- Database connection related structures

Jan Janak 18 лет назад
Родитель
Сommit
18cb22d01a
2 измененных файлов с 135 добавлено и 16 удалено
  1. 106 0
      db/db_con.c
  2. 29 16
      db/db_con.h

+ 106 - 0
db/db_con.c

@@ -0,0 +1,106 @@
+/* 
+ * $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 <stdlib.h>
+#include "../mem/mem.h"
+#include "../dprint.h"
+#include "db_con.h"
+
+
+/*
+ * Default implementation of the connect function is noop,
+ * db drivers can override the function pointer in db_con
+ * structures
+ */
+static int db_con_connect(db_con_t* con)
+{
+	return 0;
+}
+
+
+/*
+ * Default implementation of the disconnect function is noop,
+ * db drivers can override the function pointer in db_con
+ * structures
+ */
+static void db_con_disconnect(db_con_t* con)
+{
+}
+
+
+/*
+ * Create a new generic db_con structure representing a
+ * database connection and call the driver specific function
+ * in the driver that is associated with the structure based
+ * on the scheme of uri parameter
+ */
+db_con_t* db_con(db_ctx_t* ctx, db_uri_t* uri)
+{
+    db_con_t* r;
+
+    r = (db_con_t*)pkg_malloc(sizeof(db_con_t));
+    if (r == NULL) {
+		ERR("db_con: No memory left\n");
+		goto error;
+    }
+
+    memset(r, '\0', sizeof(db_con_t));
+	if (db_gen_init(&r->gen) < 0) goto error;
+
+    r->uri = uri;
+	r->ctx = ctx;
+	r->connect = db_con_connect;
+	r->disconnect = db_con_disconnect;
+
+	/* Call db_ctx function if the driver has it */
+	if (db_drv_call(&uri->scheme, "db_con", r, ctx->con_n) < 0) {
+		goto error;
+	}
+
+	return r;
+
+ error:
+	if (r) {
+		db_gen_free(&r->gen);
+		pkg_free(r);
+	}
+	return NULL;
+}
+
+
+/*
+ * Releaase all memory used by the structure
+ */
+void db_con_free(db_con_t* con)
+{
+    if (con == NULL) return;
+	db_gen_free(&con->gen);
+	if (con->uri) db_uri_free(con->uri);
+    pkg_free(con);
+}

+ 29 - 16
db/db_con.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,26 +26,38 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#ifndef _DB_CON_H
+#define _DB_CON_H  1
 
+#include "db_gen.h"
+#include "db_ctx.h"
+#include "db_uri.h"
 
-#ifndef DB_CON_H
-#define DB_CON_H
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
 
+struct db_con;
+struct db_ctx;
+
+typedef int (db_con_connect_t)(struct db_con* con);
+typedef void (db_con_disconnect_t)(struct db_con* con);
 
-/*
- * This structure represents a database connection
- * and pointer to this structure is used as a connection
- * handle
- */
-typedef struct {
-	const char* table;     /* Default table to use */
-	unsigned long tail;    /* Variable length tail
-	                        * database module specific */    
-} db_con_t;
 
+typedef struct db_con {
+	db_gen_t gen;            /* Generic part of the structure */
+	db_con_connect_t* connect;
+	db_con_disconnect_t* disconnect;
+
+	struct db_ctx* ctx;
+    db_uri_t* uri;
+} db_con_t;
 
-#define CON_TABLE(cn)      ((cn)->table)
-#define CON_TAIL(cn)       ((cn)->tail)
+struct db_con* db_con(struct db_ctx* ctx, db_uri_t* uri);
+void db_con_free(struct db_con* con);
 
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 
-#endif /* DB_CON_H */
+#endif /* _DB_CON_H */