[Devel] Patch for DB-API / mysql Module

Carsten Bock openser-list at qbiz.de
Mon Oct 16 16:09:24 CEST 2006


Hi everybody,

I am not really sure, if anyone excepts me needs this functionality, but 
however:
I extended the OpenSER-Database-API and the mySQL-Module with an 
"last_inserted_id" method. This method returns the last inserted ID of 
an INSERT-statement, in case the table has an auto-increment field.
I did only implement this functionality in the mySQL module; i took a 
look in the PostgreSQL-Docs and i did not find an according 
functionality in the postgres-C-API.... (to be honest: since we do not 
use postgres in this project, i did not spend too much time searching...)
What do you think?

Kind regards,
Carsten


-------------- next part --------------
--- /usr/src/orig/openser-1.1.0-tls/modules/mysql/dbase.c	2006-07-02 20:01:08.000000000 +0200
+++ dbase.c	2006-10-13 17:53:28.000000000 +0200
@@ -1,5 +1,5 @@
 /* 
- * $Id: dbase.c,v 1.4 2006/07/02 18:01:08 miconda Exp $ 
+ * $Id: dbase.c,v 1.1 2006/10/10 18:47:39 carsten Exp $ 
  *
  * MySQL module core functions
  *
@@ -649,3 +649,13 @@
 	LOG(L_ERR, "db_replace: Error in snprintf\n");
 	return -1;
 }
+
+
+/*
+ * Returns the last inserted ID
+ */
+int db_last_inserted_id(db_con_t* _h)
+{	
+	return mysql_insert_id(CON_CONNECTION(_h));
+}
+
--- /usr/src/orig/openser-1.1.0-tls/modules/mysql/dbase.h	2005-06-16 14:41:51.000000000 +0200
+++ dbase.h	2006-10-13 17:53:28.000000000 +0200
@@ -1,5 +1,5 @@
 /*
- * $Id: dbase.h,v 1.2 2005/06/16 12:41:51 bogdan_iancu Exp $
+ * $Id: dbase.h,v 1.1 2006/10/10 18:47:39 carsten Exp $
  *
  * MySQL module core functions
  *
@@ -96,5 +96,9 @@
  */
 int use_table(db_con_t* _h, const char* _t);
 
+/*
+ * Returns the last inserted ID
+ */
+int db_last_inserted_id(db_con_t* _h);
 
 #endif /* DBASE_H */
--- /usr/src/orig/openser-1.1.0-tls/modules/mysql/db_mod.c	2006-01-24 21:56:25.000000000 +0100
+++ db_mod.c	2006-10-11 12:23:24.000000000 +0200
@@ -1,5 +1,5 @@
 /* 
- * $Id: db_mod.c,v 1.4 2006/01/24 20:56:25 bogdan_iancu Exp $ 
+ * $Id: db_mod.c,v 1.1 2006/10/10 18:47:39 carsten Exp $ 
  *
  * MySQL module interface
  *
@@ -46,16 +46,17 @@
  * MySQL database module interface
  */
 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},
-	{"db_query",       (cmd_function)db_query,       2, 0, 0},
-	{"db_raw_query",   (cmd_function)db_raw_query,   2, 0, 0},
-	{"db_free_result", (cmd_function)db_free_result, 2, 0, 0},
-	{"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},
-	{"db_replace",     (cmd_function)db_replace,     2, 0, 0},
+	{"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},
+	{"db_query",       		(cmd_function)db_query,       2, 0, 0},
+	{"db_raw_query",   		(cmd_function)db_raw_query,   2, 0, 0},
+	{"db_free_result", 		(cmd_function)db_free_result, 2, 0, 0},
+	{"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},
+	{"db_replace",     		(cmd_function)db_replace,        2, 0, 0},
+	{"db_last_inserted_id",	(cmd_function)db_last_inserted_id, 1, 0, 0},
 	{0, 0, 0, 0, 0}
 };
 
-------------- next part --------------
--- /usr/src/orig/openser-1.1.0-tls/db/db.c	2006-07-05 10:52:15.000000000 +0200
+++ db.c	2006-10-10 17:41:34.000000000 +0200
@@ -23,6 +23,7 @@
   * History:
   * --------
   *  2004-06-06  bind_dbmod takes dbf as parameter (andrei)
+  *  2006-10-10  Added support for retrieving the last inserted ID (Carsten Bock, BASIS AudioNet GmbH)
   */
 
 
@@ -135,6 +136,11 @@
 		dbf.cap |= DB_CAP_REPLACE;
 	}
 
+	dbf.last_inserted_id= (db_last_inserted_id_f)find_mod_export(tmp, "db_last_inserted_id", 1, 0);
+	if (dbf.last_inserted_id) {
+		dbf.cap |= DB_CAP_LAST_INSERTED_ID;
+	}
+
 	*mydbf=dbf; /* copy */
 	return 0;
 
--- /usr/src/orig/openser-1.1.0-tls/db/db_cap.h	2005-06-16 14:07:08.000000000 +0200
+++ db_cap.h	2006-10-16 15:43:49.000000000 +0200
@@ -34,6 +34,7 @@
 	DB_CAP_DELETE =    1 << 3,  /* Database driver can delete data from database */
 	DB_CAP_UPDATE =    1 << 4,  /* Database driver can update data in the database */
 	DB_CAP_REPLACE =   1 << 5,  /* Replace (also known as INSERT OR UPDATE) support */
+	DB_CAP_LAST_INSERTED_ID =   1 << 6,  /* ID of the last insert */
 } db_cap_t;
 
 
--- /usr/src/orig/openser-1.1.0-tls/db/db.h	2005-06-16 14:07:08.000000000 +0200
+++ db.h	2006-10-10 17:38:55.000000000 +0200
@@ -28,6 +28,7 @@
  * History:
  * --------
  *  2004-06-06  removed db_* macros and global dbf (andrei)
+ *  2006-10-10  Added support for retrieving the last inserted ID (Carsten Bock, BASIS AudioNet GmbH)
  */
 
 
@@ -137,6 +138,11 @@
  */
 typedef int (*db_replace_f) (db_con_t* handle, db_key_t* keys, db_val_t* vals, int n);
 
+/*
+ * Retrieve the last inserted ID in a table
+ */
+typedef int (*db_last_inserted_id_f) (db_con_t* handle);
+
 
 typedef struct db_func {
 	unsigned int     cap;          /* Capability vector of the database transport */
@@ -150,6 +156,7 @@
 	db_delete_f      delete;       /* Delete from table */ 
 	db_update_f      update;       /* Update table */
 	db_replace_f     replace;      /* Replace row in a table */
+	db_last_inserted_id_f     last_inserted_id;      /* Retrieve the last inserted ID in a table */
 } db_func_t;
 
 


More information about the Devel mailing list