[sr-dev] git:master: srdb1: insert delayed support in DB API v1

Daniel-Constantin Mierla miconda at gmail.com
Thu Aug 18 00:39:09 CEST 2011


Module: sip-router
Branch: master
Commit: 13e07c7875d6f62aa46068a4daaf01ecdf8fa216
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=13e07c7875d6f62aa46068a4daaf01ecdf8fa216

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Thu Aug 18 00:30:59 2011 +0200

srdb1: insert delayed support in DB API v1

- new field in DB API struct for insert delayed implementations
- sql insert command wrapped to allow 'delayed' flag
- DB_CAP_INSERT_DELAYED flag added to show db insert delayed capability

---

 lib/srdb1/db.c       |    6 ++++++
 lib/srdb1/db.h       |   17 +++++++++++++++++
 lib/srdb1/db_cap.h   |    3 ++-
 lib/srdb1/db_query.c |   25 ++++++++++++++++++++++---
 lib/srdb1/db_query.h |   21 +++++++++++++++++++++
 5 files changed, 68 insertions(+), 4 deletions(-)

diff --git a/lib/srdb1/db.c b/lib/srdb1/db.c
index 6f85b22..0c4cb58 100644
--- a/lib/srdb1/db.c
+++ b/lib/srdb1/db.c
@@ -135,6 +135,10 @@ int db_check_api(db_func_t* dbf, char *mname)
 	if (dbf->insert_update) {
 		dbf->cap |= DB_CAP_INSERT_UPDATE;
 	}
+
+	if (dbf->insert_delayed) {
+		dbf->cap |= DB_CAP_INSERT_UPDATE;
+	}
 	return 0;
 error:
 	return -1;
@@ -227,6 +231,8 @@ int db_bind_mod(const str* mod, db_func_t* mydbf)
 			"db_last_inserted_id", 1, 0);
 		dbf.insert_update = (db_insert_update_f)find_mod_export(tmp,
 			"db_insert_update", 2, 0);
+		dbf.insert_delayed = (db_insert_delayed_f)find_mod_export(tmp,
+			"db_insert_delayed", 2, 0);
 	}
 	if(db_check_api(&dbf, tmp)!=0)
 		goto error;
diff --git a/lib/srdb1/db.h b/lib/srdb1/db.h
index 7da401b..21b9110 100644
--- a/lib/srdb1/db.h
+++ b/lib/srdb1/db.h
@@ -282,6 +282,22 @@ typedef int (*db_insert_update_f) (const db1_con_t* _h, const db_key_t* _k,
 
 
 /**
+ * \brief Insert delayed a row into the specified table.
+ *
+ * This function implements INSERT DELAYED SQL directive. It is possible to
+ * insert one or more rows in a table with delay using this function.
+ * \param _h database connection handle
+ * \param _k array of keys (column names)
+ * \param _v array of values for keys specified in _k parameter
+ * \param _n number of keys-value pairs int _k and _v parameters
+ * \return returns 0 if everything is OK, otherwise returns value < 0
+ */
+typedef int (*db_insert_delayed_f) (const db1_con_t* _h, const db_key_t* _k,
+				const db_val_t* _v, const int _n);
+
+
+
+/**
  * \brief Database module callbacks
  * 
  * This structure holds function pointer to all database functions. Before this
@@ -304,6 +320,7 @@ typedef struct db_func {
 	db_last_inserted_id_f  last_inserted_id;  /* Retrieve the last inserted ID
 	                                            in a table */
 	db_insert_update_f insert_update; /* Insert into table, update on duplicate key */ 
+	db_insert_delayed_f insert_delayed;           /* Insert delayed into table */
 } db_func_t;
 
 
diff --git a/lib/srdb1/db_cap.h b/lib/srdb1/db_cap.h
index e87ef66..1bc4704 100644
--- a/lib/srdb1/db_cap.h
+++ b/lib/srdb1/db_cap.h
@@ -47,7 +47,8 @@ typedef enum db_cap {
 	DB_CAP_REPLACE =   1 << 5,  /*!< driver can replace (also known as INSERT OR UPDATE) data       */
 	DB_CAP_FETCH   =   1 << 6,  /*!< driver supports fetch result queries                           */
 	DB_CAP_LAST_INSERTED_ID = 1 << 7,  /*!< driver can return the ID of the last insert operation   */
- 	DB_CAP_INSERT_UPDATE = 1 << 8 /*!< driver can insert data into database and update on duplicate */
+	DB_CAP_INSERT_UPDATE = 1 << 8, /*!< driver can insert data into database & update on duplicate  */
+	DB_CAP_INSERT_DELAYED = 1 << 9 /*!< driver can do insert delayed                                */
 
 } db_cap_t;
 
diff --git a/lib/srdb1/db_query.c b/lib/srdb1/db_query.c
index d2869f9..dbde735 100644
--- a/lib/srdb1/db_query.c
+++ b/lib/srdb1/db_query.c
@@ -33,6 +33,7 @@
 */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include "../../dprint.h"
 #include "db_ut.h"
 #include "db_query.h"
@@ -143,9 +144,9 @@ int db_do_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r,
 }
 
 
-int db_do_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
+int db_do_insert_cmd(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
 	const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*),
-	int (*submit_query)(const db1_con_t* _h, const str* _c))
+	int (*submit_query)(const db1_con_t* _h, const str* _c), int mode)
 {
 	int off, ret;
 
@@ -154,7 +155,12 @@ int db_do_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
 		return -1;
 	}
 
-	ret = snprintf(sql_buf, sql_buffer_size, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+	if(mode==1)
+		ret = snprintf(sql_buf, sql_buffer_size, "insert delayed into %.*s (",
+				CON_TABLE(_h)->len, CON_TABLE(_h)->s);
+	else
+		ret = snprintf(sql_buf, sql_buffer_size, "insert into %.*s (",
+				CON_TABLE(_h)->len, CON_TABLE(_h)->s);
 	if (ret < 0 || ret >= sql_buffer_size) goto error;
 	off = ret;
 
@@ -187,6 +193,19 @@ error:
 	return -1;
 }
 
+int db_do_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
+	const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*),
+	int (*submit_query)(const db1_con_t* _h, const str* _c))
+{
+	return db_do_insert_cmd(_h, _k, _v, _n, val2str, submit_query, 0);
+}
+
+int db_do_insert_delayed(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
+	const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*),
+	int (*submit_query)(const db1_con_t* _h, const str* _c))
+{
+	return db_do_insert_cmd(_h, _k, _v, _n, val2str, submit_query, 1);
+}
 
 int db_do_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
 	const db_val_t* _v, const int _n, int (*val2str) (const db1_con_t*,
diff --git a/lib/srdb1/db_query.h b/lib/srdb1/db_query.h
index 13645d8..416de3f 100644
--- a/lib/srdb1/db_query.h
+++ b/lib/srdb1/db_query.h
@@ -185,6 +185,27 @@ int db_do_replace(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
 
 
 /**
+ * \brief Helper function for db insert delayed operations
+ *
+ * This method evaluates the actual arguments for the database operation
+ * and setups the string that is used for the insert delayed operation in the db
+ * module. Then its submit the query for the operation. It uses for its work
+ * the implementation in the concrete database module.
+ *
+ * \param _h structure representing database connection
+ * \param _k key names
+ * \param _v values of the keys
+ * \param _n number of key/value pairs
+ * \param (*val2str) function pointer to the db specific val conversion function
+ * \param (*submit_query) function pointer to the db specific query submit function
+ * \return zero on success, negative on errors
+ */
+int db_do_insert_delayed(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
+	const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*),
+	int (*submit_query)(const db1_con_t* _h, const str* _c));
+
+
+/**
  * \brief Initialisation function - should be called from db.c at start-up
  *
  * This initialises the db_query module, and should be called before any functions in db_query are called.




More information about the sr-dev mailing list