Browse code

db_text - add affected rows capability

Luis Azedo authored on 16/02/2015 13:48:58
Showing 4 changed files
... ...
@@ -335,6 +335,20 @@ int dbt_raw_query(db1_con_t* _h, char* _s, db1_res_t** _r)
335 335
     return -1;
336 336
 }
337 337
 
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
+
338 352
 /*
339 353
  * Insert a row into table
340 354
  */
... ...
@@ -350,6 +364,9 @@ int dbt_insert(db1_con_t* _h, db_key_t* _k, db_val_t* _v, int _n)
350 364
 		LM_ERR("invalid parameter\n");
351 365
 		return -1;
352 366
 	}
367
+ 
368
+    ((dbt_con_p)_h->tail)->affected = 0;
369
+    
353 370
 	if(!_k || !_v || _n<=0)
354 371
 	{
355 372
 		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 424
 		goto clean;
408 425
 	}
409 426
 
427
+    ((dbt_con_p)_h->tail)->affected = 1;
428
+    
410 429
 	/* dbt_print_table(_tbc, NULL); */
411 430
 	
412 431
 	/* 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 471
 		return -1;
453 472
 	}
454 473
 
474
+    ((dbt_con_p)_h->tail)->affected = 0;
475
+
455 476
 	/* lock database */
456 477
 	_tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
457 478
 	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 485
 	if(!_k || !_v || _n<=0)
465 486
 	{
466 487
 		LM_DBG("deleting all records\n");
488
+        ((dbt_con_p)_h->tail)->affected = _tbc->nrrows;
467 489
 		dbt_table_free_rows(_tbc);
468 490
 		/* unlock databse */
469 491
 		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 512
 			_tbc->nrrows--;
491 513
 			// free row
492 514
 			dbt_row_free(_tbc, _drp);
515
+
516
+            ((dbt_con_p)_h->tail)->affected++;
517
+
493 518
 		}
494 519
 		_drp = _drp0;
495 520
 	}
... ...
@@ -530,7 +555,9 @@ int dbt_update(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v,
530 555
 		LM_ERR("invalid parameters\n");
531 556
 		return -1;
532 557
 	}
533
-	
558
+
559
+    ((dbt_con_p)_h->tail)->affected = 0;
560
+    
534 561
 	/* lock database */
535 562
 	_tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h));
536 563
 	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 596
 					goto error;
570 597
 				}
571 598
 			}
599
+
600
+            ((dbt_con_p)_h->tail)->affected++;
601
+
572 602
 		}
573 603
 		_drp = _drp->next;
574 604
 	}
... ...
@@ -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