From: Abhay Agarwal Date: Mon, 26 Sep 2022 08:40:57 +0000 (+0530) Subject: Remove code duplicacy in database operations X-Git-Tag: accepted/tizen/unified/20220927.132408^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Ftizen_8.0;p=platform%2Fcore%2Fconnectivity%2Fua-manager.git Remove code duplicacy in database operations Change-Id: Ie2359dd70cb27288f2e48c810406e852d83c267a Signed-off-by: Abhay Agarwal --- diff --git a/ua-daemon/src/ua-manager-device-service-db.c b/ua-daemon/src/ua-manager-device-service-db.c index 2667848..d848534 100644 --- a/ua-daemon/src/ua-manager-device-service-db.c +++ b/ua-daemon/src/ua-manager-device-service-db.c @@ -894,7 +894,7 @@ handle_error: return error_code; } -GSList *_uam_db_get_service_devices_payloads_info(const int app_num) +static GSList *__uam_db_get_service_devices(const int app_num, gboolean have_payload) { FUNC_ENTRY; int error_code = UAM_ERROR_NONE; @@ -902,81 +902,13 @@ GSList *_uam_db_get_service_devices_payloads_info(const int app_num) GSList *svc_dev_list = NULL; db_svc_dev_info_t *info = NULL; int sql_ret = SQLITE_OK; - char *buf = NULL; - int uid_len = 0; - if (app_num == -1) { + if (have_payload && app_num == -1) { stmt = select_device_services_payloads_without_app_num; - } else { + } else if (have_payload) { stmt = select_device_services_payloads; DB_ACTION(sqlite3_bind_int(stmt, 1, app_num), error_code, handle_error); - } - - do { - sql_ret = sqlite3_step(stmt); - - switch (sql_ret) { - case SQLITE_DONE: - break; - case SQLITE_ROW: - UAM_DBG("Device services info found"); - info = g_new0(db_svc_dev_info_t, 1); - g_strlcpy(info->device_id, (char *)sqlite3_column_text(stmt, 0), - UAM_DEVICE_ID_MAX_STRING_LEN); - info->type = sqlite3_column_int(stmt, 1); - g_strlcpy(info->svc, (char *)sqlite3_column_text(stmt, 2), - UAM_SERVICE_MAX_STRING_LEN); - info->discriminant = sqlite3_column_int(stmt, 3); - info->last_seen = sqlite3_column_int64(stmt, 4); - buf = (char *)sqlite3_column_text(stmt, 5); - info->payload_info.primary_key = buf ? *buf : 13; - buf = (char *)sqlite3_column_text(stmt, 6); - info->payload_info.secondary_key = buf ? *buf : 1; - uid_len = sqlite3_column_int(stmt, 7); - info->payload_info.device_uid_len = uid_len >= 0 ? uid_len : 0; - buf = (char *)sqlite3_column_text(stmt, 8); - if (buf) - memcpy(info->payload_info.device_uid, buf, - info->payload_info.device_uid_len); - buf = (char *)sqlite3_column_text(stmt, 9); - info->payload_info.device_type = buf ? *buf : 1; - - UAM_INFO("device id:%s-%d-%s-%d-%llu " \ - "primary key: %d secondary key: %d", - info->device_id, - info->type, - info->svc, - info->discriminant, - info->last_seen, - info->payload_info.primary_key, - info->payload_info.secondary_key); - - svc_dev_list = g_slist_append(svc_dev_list, info); - break; - case SQLITE_ERROR: - default: - UAM_ERR("Failed to enumerate device info [%d:%s]", - sql_ret, sqlite3_errmsg(database_handle)); - } - } while (sql_ret == SQLITE_ROW); -handle_error: - if (error_code != UAM_ERROR_NONE) - UAM_ERR("_uam_db_get_service_devices_payloads_info failed"); - sqlite3_reset(stmt); - FUNC_EXIT; - return svc_dev_list; -} - -GSList *_uam_db_get_service_devices_info(const int app_num) -{ - FUNC_ENTRY; - int error_code = UAM_ERROR_NONE; - sqlite3_stmt *stmt; - GSList *svc_dev_list = NULL; - db_svc_dev_info_t *info = NULL; - int sql_ret = SQLITE_OK; - - if (app_num == -1) { + } else if (app_num == -1) { stmt = select_device_services_without_app_num; } else { stmt = select_device_services; @@ -999,7 +931,26 @@ GSList *_uam_db_get_service_devices_info(const int app_num) UAM_SERVICE_MAX_STRING_LEN); info->discriminant = sqlite3_column_int(stmt, 3); info->last_seen = sqlite3_column_int64(stmt, 4); - info->app_num = sqlite3_column_int(stmt, 5); + + if (!have_payload) { + info->app_num = sqlite3_column_int(stmt, 5); + } else { + char *buf = NULL; + int uid_len = 0; + + buf = (char *)sqlite3_column_text(stmt, 5); + info->payload_info.primary_key = buf ? *buf : 13; + buf = (char *)sqlite3_column_text(stmt, 6); + info->payload_info.secondary_key = buf ? *buf : 1; + uid_len = sqlite3_column_int(stmt, 7); + info->payload_info.device_uid_len = uid_len >= 0 ? uid_len : 0; + buf = (char *)sqlite3_column_text(stmt, 8); + if (buf) + memcpy(info->payload_info.device_uid, buf, + info->payload_info.device_uid_len); + buf = (char *)sqlite3_column_text(stmt, 9); + info->payload_info.device_type = buf ? *buf : 1; + } UAM_INFO("device id:%s-%d-%s-%d-%llu-%d ", info->device_id, @@ -1009,6 +960,11 @@ GSList *_uam_db_get_service_devices_info(const int app_num) info->last_seen, info->app_num); + if (have_payload) + UAM_INFO("DevicePrimary key: %d secondary key: %d", + info->payload_info.primary_key, + info->payload_info.secondary_key); + svc_dev_list = g_slist_append(svc_dev_list, info); break; case SQLITE_ERROR: @@ -1017,13 +973,23 @@ GSList *_uam_db_get_service_devices_info(const int app_num) sql_ret, sqlite3_errmsg(database_handle)); } } while (sql_ret == SQLITE_ROW); - handle_error: if (error_code != UAM_ERROR_NONE) - UAM_ERR("_uam_db_get_service_devices_info failed"); + UAM_ERR("_uam_db_get_service_devices failed"); sqlite3_reset(stmt); FUNC_EXIT; return svc_dev_list; + +} + +GSList *_uam_db_get_service_devices_payloads_info(const int app_num) +{ + return __uam_db_get_service_devices(app_num, TRUE); +} + +GSList *_uam_db_get_service_devices_info(const int app_num) +{ + return __uam_db_get_service_devices(app_num, FALSE); } static int __uam_db_update_dev_svc_payload(int device_number,