Browse code

db_text - add affected rows capability

Luis Azedo authored on 16/02/2015 13:48:58
Showing 4 changed files
... ...
@@ -336,6 +336,20 @@ int dbt_raw_query(db1_con_t* _h, char* _s, db1_res_t** _r)
336 336
 }
337 337
 
338 338
 /*
339
+ * Affected Rows
340
+ */
341
+int dbt_affected_rows(db1_con_t* _h)
342
+{
343
+    if (!_h || !CON_TABLE(_h))
344
+    {
345
+        LM_ERR("invalid parameter\n");
346
+        return -1;
347
+    }
348
+
349
+    return ((dbt_con_p)_h->tail)->affected;
350
+}
351
+
352
+/*
339 353
  * Insert a row into table
340 354
  */
341 355
 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)
350 350
 		LM_ERR("invalid parameter\n");
351 351
 		return -1;
352 352
 	}
353
+ 
354
+    ((dbt_con_p)_h->tail)->affected = 0;
355
+    
353 356
 	if(!_k || !_v || _n<=0)
354 357
 	{
355 358
 		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)
407 407
 		goto clean;
408 408
 	}
409 409
 
410
+    ((dbt_con_p)_h->tail)->affected = 1;
411
+    
410 412
 	/* dbt_print_table(_tbc, NULL); */
411 413
 	
412 414
 	/* 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)
452 452
 		return -1;
453 453
 	}
454 454
 
455
+    ((dbt_con_p)_h->tail)->affected = 0;
456
+
455 457
 	/* lock database */
456 458
 	_tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
457 459
 	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)
464 464
 	if(!_k || !_v || _n<=0)
465 465
 	{
466 466
 		LM_DBG("deleting all records\n");
467
+        ((dbt_con_p)_h->tail)->affected = _tbc->nrrows;
467 468
 		dbt_table_free_rows(_tbc);
468 469
 		/* unlock databse */
469 470
 		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)
490 490
 			_tbc->nrrows--;
491 491
 			// free row
492 492
 			dbt_row_free(_tbc, _drp);
493
+
494
+            ((dbt_con_p)_h->tail)->affected++;
495
+
493 496
 		}
494 497
 		_drp = _drp0;
495 498
 	}
... ...
@@ -530,7 +555,9 @@ int dbt_update(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
530 530
 		LM_ERR("invalid parameters\n");
531 531
 		return -1;
532 532
 	}
533
-	
533
+
534
+    ((dbt_con_p)_h->tail)->affected = 0;
535
+    
534 536
 	/* lock database */
535 537
 	_tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
536 538
 	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,
569 569
 					goto error;
570 570
 				}
571 571
 			}
572
+
573
+            ((dbt_con_p)_h->tail)->affected++;
574
+
572 575
 		}
573 576
 		_drp = _drp->next;
574 577
 	}
... ...
@@ -47,6 +47,7 @@ typedef struct _dbt_result
47 47
 typedef struct _dbt_con
48 48
 {
49 49
 	dbt_cache_p con;
50
+    int affected;
50 51
 } dbt_con_t, *dbt_con_p;
51 52
 
52 53
 #define DBT_CON_CONNECTION(db_con) (((dbt_con_p)((db_con)->tail))->con)
... ...
@@ -134,6 +134,8 @@ int dbt_bind_api(db_func_t *dbb)
134 134
 	dbb->insert      = (db_insert_f)dbt_insert;
135 135
 	dbb->delete      = (db_delete_f)dbt_delete; 
136 136
 	dbb->update      = (db_update_f)dbt_update;
137
+    dbb->affected_rows = (db_affected_rows_f) dbt_affected_rows;
138
+    dbb->cap         = DB_CAP_AFFECTED_ROWS;
137 139
 
138 140
 	return 0;
139 141
 }
... ...
@@ -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);
87 87
 int dbt_update(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
88 88
 	      db_key_t* _uk, db_val_t* _uv, int _n, int _un);
89 89
 
90
+/*
91
+ * Affected rows
92
+ */
93
+int dbt_affected_rows(db1_con_t* _h);
94
+
90 95
 #endif
91 96