Browse Source

- added extension for OpenSER-Database-API and the mySQL-Module for
"last_inserted_id" method. This method returns the last inserted ID of an
INSERT-statement, in case the table has an auto-increment field.

Contributed by Carsten Bock, BASIS AudioNet GmbH

The first module to use this functionality will be the usrloc module for
update and delete operations (used the record ID instead of costly AOR and
contact maching)


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

Bogdan-Andrei Iancu 18 years ago
parent
commit
4e8a664563
4 changed files with 39 additions and 0 deletions
  1. 6 0
      lib/srdb1/db.c
  2. 8 0
      lib/srdb1/db.h
  3. 1 0
      lib/srdb1/db_cap.h
  4. 24 0
      lib/srdb1/doc/db-api.txt

+ 6 - 0
lib/srdb1/db.c

@@ -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)
   */
 
 
@@ -141,6 +142,11 @@ int bind_dbmod(char* mod, db_func_t* mydbf)
 		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 */
 	if (tmp != mod) pkg_free(tmp);
 	return 0;

+ 8 - 0
lib/srdb1/db.h

@@ -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)
  */
 
 
@@ -146,6 +147,11 @@ typedef int (*db_update_f) (db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _
  */
 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 */
@@ -160,6 +166,8 @@ typedef struct db_func {
 	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;
 
 

+ 1 - 0
lib/srdb1/db_cap.h

@@ -35,6 +35,7 @@ typedef enum db_cap {
 	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_FETCH   =   1 << 6,  /* Fetch result support */
+	DB_CAP_LAST_INSERTED_ID = 1 << 7,  /* ID of the last insert */
 } db_cap_t;
 
 

+ 24 - 0
lib/srdb1/doc/db-api.txt

@@ -616,3 +616,27 @@ n: number of key=value pairs
 
 The function returns 0 if everything is OK, otherwise the function returns
 value < 0.
+
+
+2.11 Callback dbf.last_inserted_id
+
+2.11.1 Description
+
+The function returns the value generated for an AUTO_INCREMENT column by the 
+previous INSERT or UPDATE  statement. Use this function after you have 
+performed an INSERT statement into a table that contains an AUTO_INCREMENT 
+field.
+
+2.11.2 Prototype
+
+   int (*db_last_inserted_id) (db_con_t* handle);
+
+2.11.3 Parameters
+
+The function takes one parameter:
+handle: structure representing database connection
+
+2.11.4 Return Value
+
+The function returns the ID as integer or returns 0 if the previous statement 
+does not use an AUTO_INCREMENT value.