Browse code

lib/srdb1 (k): add generic helper method for bulk data loading, e.g. for route infos

- add a generic helper method for bulk data loading, e.g. for routing informations
from cr, lcr, htable or other similar tasks
- TODO: adapt cr (patch ready, testing needed), adapt more modules

Henning Westerholt authored on 08/01/2010 17:26:11
Showing 2 changed files
... ...
@@ -425,3 +425,55 @@ int db_use_table(db1_con_t* _h, const str* _t)
425 425
 	CON_TABLE(_h) = _t;
426 426
 	return 0;
427 427
 }
428
+
429
+
430
+/*! \brief Generic query helper for load bulk data
431
+ *
432
+ * Generic query helper method for load bulk data, e.g. lcr tables
433
+ * \param binding database module binding
434
+ * \param handle database connection
435
+ * \param name database table name
436
+ * \param cols queried columns
437
+ * \param count number of queried columns
438
+ * \param strict if set to 1 an error is returned when no data could be loaded,
439
+    otherwise just a warning is logged
440
+ * \param res database result, unchanged on failure and if no data could be found
441
+ * \return 0 if the query was run successful, -1 otherwise
442
+ */
443
+int db_load_bulk_data(db_func_t* binding, db1_con_t* handle, str* name, db_key_t* cols,
444
+		      unsigned int count, unsigned int strict, db1_res_t* res)
445
+{
446
+	if (binding == NULL) {
447
+		LM_ERR("invalid database module binding\n");
448
+		return -1;
449
+	}
450
+
451
+	if(handle == NULL) {
452
+		LM_ERR("invalid database handle\n");
453
+		return -1;
454
+	}
455
+
456
+	if (binding->use_table(handle, name) < 0) {
457
+		LM_ERR("error in use_table for database\n");
458
+		return -1;
459
+	}
460
+
461
+	/* select the whole table and all the columns */
462
+	if(binding->query(handle, 0, 0, 0, cols, 0, count, 0, &res) < 0) {
463
+		LM_ERR("error while querying database\n");
464
+		return -1;
465
+	}
466
+
467
+	if(RES_ROW_N(res) == 0) {
468
+		binding->free_result(handle, res);
469
+		if (strict == 1) {
470
+			LM_ERR("no data in the database table %.*s\n", name->len, name->s);
471
+			return -1;
472
+		} else {
473
+			LM_WARN("no data in the database table %.*s, use an empty set\n", name->len, name->s);
474
+			return 0;
475
+		}
476
+	}
477
+
478
+	return 0;
479
+}
... ...
@@ -398,5 +398,21 @@ int db_use_table(db1_con_t* _h, const str* _t);
398 398
 
399 399
 typedef int (*db_bind_api_f)(db_func_t *dbb);
400 400
 
401
+/**
402
+ * \brief Generic query helper for load bulk data
403
+ *
404
+ * Generic query helper method for load bulk data, e.g. lcr tables
405
+ * \param binding database module binding
406
+ * \param handle database connection
407
+ * \param name database table name
408
+ * \param cols queried columns
409
+ * \param count number of queried columns
410
+ * \param strict if set to 1 an error is returned when no data could be loaded,
411
+    otherwise just a warning is logged
412
+ * \param res database result, unchanged on failure and if no data could be found
413
+ * \return 0 if the query was run successful, -1 otherwise
414
+ */
415
+int db_load_bulk_data(db_func_t* binding, db1_con_t* handle, str* name, db_key_t* cols,
416
+		      unsigned int count, unsigned int strict, db1_res_t* res);
401 417
 
402 418
 #endif /* DB1_H */