Module: kamailio
Branch: master
Commit: b93b7b39537f8858b34052f77e3ce0512cdc72c0
URL:
https://github.com/kamailio/kamailio/commit/b93b7b39537f8858b34052f77e3ce05…
Author: Luis Azedo <luis(a)2600hz.com>
Committer: Luis Azedo <luis(a)2600hz.com>
Date: 2015-02-16T13:48:58Z
db_text - add affected rows capability
---
Modified: modules/db_text/dbt_base.c
Modified: modules/db_text/dbt_res.h
Modified: modules/db_text/dbtext.c
Modified: modules/db_text/dbtext.h
---
Diff:
https://github.com/kamailio/kamailio/commit/b93b7b39537f8858b34052f77e3ce05…
Patch:
https://github.com/kamailio/kamailio/commit/b93b7b39537f8858b34052f77e3ce05…
---
diff --git a/modules/db_text/dbt_base.c b/modules/db_text/dbt_base.c
index c29a5e0..e862e86 100644
--- a/modules/db_text/dbt_base.c
+++ b/modules/db_text/dbt_base.c
@@ -336,6 +336,20 @@ int dbt_raw_query(db1_con_t* _h, char* _s, db1_res_t** _r)
}
/*
+ * Affected Rows
+ */
+int dbt_affected_rows(db1_con_t* _h)
+{
+ if (!_h || !CON_TABLE(_h))
+ {
+ LM_ERR("invalid parameter\n");
+ return -1;
+ }
+
+ return ((dbt_con_p)_h->tail)->affected;
+}
+
+/*
* Insert a row into table
*/
int dbt_insert(db1_con_t* _h, db_key_t* _k, db_val_t* _v, int _n)
@@ -350,6 +364,9 @@ int dbt_insert(db1_con_t* _h, db_key_t* _k, db_val_t* _v, int _n)
LM_ERR("invalid parameter\n");
return -1;
}
+
+ ((dbt_con_p)_h->tail)->affected = 0;
+
if(!_k || !_v || _n<=0)
{
LM_ERR("no key-value to insert\n");
@@ -407,6 +424,8 @@ int dbt_insert(db1_con_t* _h, db_key_t* _k, db_val_t* _v, int _n)
goto clean;
}
+ ((dbt_con_p)_h->tail)->affected = 1;
+
/* dbt_print_table(_tbc, NULL); */
/* unlock databse */
@@ -452,6 +471,8 @@ int dbt_delete(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
int _n)
return -1;
}
+ ((dbt_con_p)_h->tail)->affected = 0;
+
/* lock database */
_tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
if(!_tbc)
@@ -464,6 +485,7 @@ int dbt_delete(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
int _n)
if(!_k || !_v || _n<=0)
{
LM_DBG("deleting all records\n");
+ ((dbt_con_p)_h->tail)->affected = _tbc->nrrows;
dbt_table_free_rows(_tbc);
/* unlock databse */
dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
@@ -490,6 +512,9 @@ int dbt_delete(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
int _n)
_tbc->nrrows--;
// free row
dbt_row_free(_tbc, _drp);
+
+ ((dbt_con_p)_h->tail)->affected++;
+
}
_drp = _drp0;
}
@@ -530,7 +555,9 @@ int dbt_update(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t*
_v,
LM_ERR("invalid parameters\n");
return -1;
}
-
+
+ ((dbt_con_p)_h->tail)->affected = 0;
+
/* lock database */
_tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
if(!_tbc)
@@ -569,6 +596,9 @@ int dbt_update(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t*
_v,
goto error;
}
}
+
+ ((dbt_con_p)_h->tail)->affected++;
+
}
_drp = _drp->next;
}
diff --git a/modules/db_text/dbt_res.h b/modules/db_text/dbt_res.h
index a143097..00bdb40 100644
--- a/modules/db_text/dbt_res.h
+++ b/modules/db_text/dbt_res.h
@@ -47,6 +47,7 @@ typedef struct _dbt_result
typedef struct _dbt_con
{
dbt_cache_p con;
+ int affected;
} dbt_con_t, *dbt_con_p;
#define DBT_CON_CONNECTION(db_con) (((dbt_con_p)((db_con)->tail))->con)
diff --git a/modules/db_text/dbtext.c b/modules/db_text/dbtext.c
index 47cf295..cde16af 100644
--- a/modules/db_text/dbtext.c
+++ b/modules/db_text/dbtext.c
@@ -134,6 +134,8 @@ int dbt_bind_api(db_func_t *dbb)
dbb->insert = (db_insert_f)dbt_insert;
dbb->delete = (db_delete_f)dbt_delete;
dbb->update = (db_update_f)dbt_update;
+ dbb->affected_rows = (db_affected_rows_f) dbt_affected_rows;
+ dbb->cap = DB_CAP_AFFECTED_ROWS;
return 0;
}
diff --git a/modules/db_text/dbtext.h b/modules/db_text/dbtext.h
index 936bb3d..48f62cd 100644
--- a/modules/db_text/dbtext.h
+++ b/modules/db_text/dbtext.h
@@ -87,5 +87,10 @@ int dbt_delete(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
int _n);
int dbt_update(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
db_key_t* _uk, db_val_t* _uv, int _n, int _un);
+/*
+ * Affected rows
+ */
+int dbt_affected_rows(db1_con_t* _h);
+
#endif