Fix service database queries 46/208846/1
authorAbhay agarwal <ay.agarwal@samsung.com>
Wed, 12 Jun 2019 12:04:22 +0000 (17:34 +0530)
committersaerome kim <saerome.kim@samsung.com>
Mon, 1 Jul 2019 02:02:37 +0000 (11:02 +0900)
Change-Id: I215f37f7b3825de58ff939c6f086b11d8b81916d
Signed-off-by: Abhay agarwal <ay.agarwal@samsung.com>
ua-daemon/data/ua_db.sql
ua-daemon/include/ua-manager-database.h
ua-daemon/src/ua-manager-device-db.c
ua-daemon/src/ua-manager-device-service-db.c
ua-daemon/src/ua-manager-service-db.c
ua-daemon/src/ua-manager-user-db.c

index 42c3778..d571135 100644 (file)
@@ -5,12 +5,12 @@ PRAGMA foreign_keys = ON;
 
 CREATE TABLE IF NOT EXISTS userdata (
   name TEXT,
-  user_id INTEGER PRIMARY KEY AUTOINCREMENT,
-  account TEXT
+  user_id INTEGER,
+  account TEXT PRIMARY KEY
 );
 
 CREATE TABLE IF NOT EXISTS devices (
-  device_number INTEGER PRIMARY KEY AUTOINCREMENT,
+  device_number INTEGER,
   device_id TEXT,
   user_id INTEGER,
   tech_type INTEGER,
@@ -19,12 +19,13 @@ CREATE TABLE IF NOT EXISTS devices (
   timestamp LONG,
   presence_state INTEGER,
   os_type INTEGER,
-  FOREIGN KEY(user_id) REFERENCES userdata(user_id)
+  FOREIGN KEY(user_id) REFERENCES userdata(user_id),
+  PRIMARY KEY(device_id, tech_type, address)
 );
 
 CREATE TABLE IF NOT EXISTS services (
-  service_number INTEGER PRIMARY KEY AUTOINCREMENT,
-  service_name TEXT,
+  service_number INTEGER,
+  service_name TEXT PRIMARY KEY,
   cycle INTEGER
 );
 
index a9a2067..fec78e3 100644 (file)
@@ -124,7 +124,7 @@ GSList *_uam_db_get_device_services(const char *device_id, int tech_type, const
 int _ua_device_service_db_clear(void);
 int _uam_db_delete_service_number(int service_number);
 int _uam_db_delete_device_service_number(int device_number, int service_number);
-int _ua_device_service_db_delete_by_service(const char *service_name);
+int _uam_db_delete_device_service(const char *service_name);
 int _uam_db_delete_device_service_info(const char *device_id, int tech_type,
                const char *address, const char *service_name);
 
index 5767f0c..06abc35 100644 (file)
        "user_id FROM devices WHERE device_id = ? AND tech_type = ? AND address = ?"
 #define SELECT_DEVICE_NUMBER "SELECT device_number FROM devices " \
        "WHERE device_id = ? AND tech_type = ? AND address = ?"
+#define SELECT_MAX_DEVICE_NUMBER "SELECT MAX(device_number) FROM devices"
 
 #define INSERT_DEVICE "insert into devices " \
        "(device_id, tech_type, address, ip_address, timestamp, " \
-       "presence_state, os_type, user_id)" \
-       "values (?, ?, ?, ?, ?, ?, ?, ?)"
+       "presence_state, os_type, user_id, device_number)" \
+       "values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
 
 #define UPDATE_TIMESTAMP "UPDATE devices " \
        "SET timestamp = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
@@ -138,6 +139,7 @@ static sqlite3_stmt *delete_device_info;
 static sqlite3_stmt *select_all_devices;
 static sqlite3_stmt *select_device;
 static sqlite3_stmt *select_device_number;
+static sqlite3_stmt *select_max_device_number;
 
 /* UPDATE statements */
 static sqlite3_stmt *update_timestamp;
@@ -148,6 +150,7 @@ static sqlite3_stmt *update_ip_address;
 static sqlite3_stmt *insert_device_info;
 
 static sqlite3 *database;
+static int max_device_number;
 
 static int __ua_device_db_busy(void *user, int attempts)
 {
@@ -234,6 +237,7 @@ static int __ua_device_prepare_select(sqlite3 *db)
        PREPARE_SELECT(select_all_devices, SELECT_ALL_DEVICES);
        PREPARE_SELECT(select_device, SELECT_DEVICE);
        PREPARE_SELECT(select_device_number, SELECT_DEVICE_NUMBER);
+       PREPARE_SELECT(select_max_device_number, SELECT_MAX_DEVICE_NUMBER);
 
        initialized = 1;
        FUNC_EXIT;
@@ -317,6 +321,7 @@ static void __ua_device_finalize_select(void)
        FINALIZE(select_all_devices);
        FINALIZE(select_device);
        FINALIZE(select_device_number);
+       FINALIZE(select_max_device_number);
 
        FUNC_EXIT;
 }
@@ -350,6 +355,43 @@ static void __ua_device_table_devicesinfo_finalize(void)
        FUNC_EXIT;
 }
 
+static sqlite3 *__ua_device_db_get_database(void)
+{
+       if (database == NULL)
+               __ua_device_db_initialize_once();
+
+       return database;
+}
+
+int _uam_db_get_max_device_number(void)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       int rc;
+       sqlite3_stmt *stmt = select_max_device_number;
+
+       do {
+               rc = sqlite3_step(stmt);
+
+               switch (rc) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       max_device_number = sqlite3_column_int(stmt, 0);
+                       UAM_INFO("max_device_number %d", max_device_number);
+                       break;
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate device info: %s\n",
+                                       sqlite3_errmsg(__ua_device_db_get_database()));
+               }
+       } while (rc == SQLITE_ROW);
+
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
 int _ua_device_db_deinitialize(void)
 {
        FUNC_ENTRY;
@@ -371,6 +413,8 @@ int _ua_device_db_initialize(void)
        __ua_device_db_initialize_once();
 
        EXEC(UAM_ERROR_NONE, __ua_device_table_devicesinfo_prepare(database));
+       if (_uam_db_get_max_device_number() != UAM_ERROR_NONE)
+               goto handle_error;
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -381,14 +425,6 @@ handle_error:
        return UAM_ERROR_DB_FAILED;
 }
 
-static sqlite3 *__ua_device_db_get_database(void)
-{
-       if (database == NULL)
-               __ua_device_db_initialize_once();
-
-       return database;
-}
-
 int _ua_device_db_update_device_ip_address(char *device_id, int tech_type, char *address,
                char *ip_address)
 {
@@ -498,6 +534,7 @@ int _ua_device_db_insert_device_info(int user_id, const uam_device_info_t *dev_i
        DB_ACTION(sqlite3_bind_int(stmt, 6, presence_state));
        DB_ACTION(sqlite3_bind_int(stmt, 7, dev_info->operating_system));
        DB_ACTION(sqlite3_bind_int(stmt, 8, user_id));
+       DB_ACTION(sqlite3_bind_int(stmt, 9, max_device_number + 1));
 
        if (sqlite3_step(stmt) != SQLITE_DONE) {
                UAM_ERR("Failed to insert device info: %s\n",
@@ -506,7 +543,8 @@ int _ua_device_db_insert_device_info(int user_id, const uam_device_info_t *dev_i
                goto handle_error;
        }
 
-       UAM_DBG("User info inserted [%d]", user_id);
+       UAM_DBG("Device info inserted device_id [%s] user id [%d]", dev_info->device_id, user_id);
+       max_device_number = max_device_number + 1;
 
 handle_error:
        sqlite3_reset(stmt);
@@ -584,6 +622,7 @@ handle_error:
 int _uam_db_get_device_number(const char *device_id, int tech_type, const char *address, int *device_number)
 {
        FUNC_ENTRY;
+       int rc;
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = select_device_number;
 
@@ -596,16 +635,24 @@ int _uam_db_get_device_number(const char *device_id, int tech_type, const char *
        DB_ACTION(sqlite3_bind_int(stmt, 2, tech_type));
        DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC));
 
-       if (sqlite3_step(stmt) != SQLITE_DONE) {
-               UAM_ERR("Failed to select device info %s\n",
-                               sqlite3_errmsg(__ua_device_db_get_database()));
-               error_code = UAM_ERROR_DB_FAILED;
-       } else {
-               UAM_DBG("Device number info found");
-               *device_number = sqlite3_column_int(stmt, 0);
+       do {
+               rc = sqlite3_step(stmt);
 
-               UAM_INFO("%d", *device_number);
-       }
+               switch (rc) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       UAM_DBG("Device number info found");
+                       *device_number = sqlite3_column_int(stmt, 0);
+
+                       UAM_INFO("device_number %d", *device_number);
+                       break;
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate device info: %s\n",
+                                       sqlite3_errmsg(__ua_device_db_get_database()));
+               }
+       } while (rc == SQLITE_ROW);
 
 handle_error:
        sqlite3_reset(stmt);
index b4d108f..19f2210 100644 (file)
 #include "ua-manager-common.h"
 #include "ua-manager-database.h"
 
-#define SELECT_SERVICE "SELECT service_number, service_name, cycle" \
-       "FROM device_services JOIN services where service_number = ?"
+#define SELECT_SERVICE "SELECT S.service_number, S.service_name, S.cycle " \
+       "FROM device_services as D JOIN services as S where D.device_number = ?"
 
 #define INSERT_DEVICE_SERVICE "insert into device_services " \
-       "(device_number, service_number, values (?, ?)"
+       "(device_number, service_number) values (?, ?)"
 
-#define UPDATE ""
-
-#define DELETE_ALL_DEVICE_SERVICES "delete from devices "
+#define DELETE_ALL_DEVICE_SERVICES "delete from device_services "
 #define DELETE_DEVICE_SERVICE "delete from device_services " \
        "WHERE device_number = ? and service_number = ?"
 #define DELETE_SERVICE "delete from device_services " \
@@ -125,8 +123,6 @@ static sqlite3_stmt *delete_device_service;
 
 /* SELECT statements */
 static sqlite3_stmt *select_service;
-static sqlite3_stmt *select_service_info;
-static sqlite3_stmt *select_device_info;
 
 /* UPDATE statements */
 //TO-DO
@@ -220,7 +216,6 @@ static int __ua_device_service_prepare_select(sqlite3 *db)
        }
 
        PREPARE_SELECT(select_service, SELECT_SERVICE);
-       //PREPARE_SELECT(select_service_info, SELECT_SERVICE_INFO);
 
        initialized = 1;
        FUNC_EXIT;
@@ -291,8 +286,6 @@ static void __ua_device_service_finalize_select(void)
        FUNC_ENTRY;
 
        FINALIZE(select_service);
-       FINALIZE(select_service_info);
-       FINALIZE(select_device_info);
 
        FUNC_EXIT;
 }
@@ -488,7 +481,7 @@ handle_error:
        return error_code;
 }
 
-int _ua_device_service_db_delete_by_service(const char *service_name)
+int _uam_db_delete_device_service(const char *service_name)
 {
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
index 5a2fa64..cb4c3b6 100644 (file)
 
 #define SELECT_ALL_SERVICES "SELECT service_number, service_name, " \
        "cycle FROM services"
-
 #define SELECT_SERVICE "SELECT service_number, service_name, cycle " \
        "FROM services where service_name = ?"
+#define SELECT_MAX_SERVICE_NUMBER "SELECT MAX(service_number) FROM services"
 
-#define INSERT_SERVICE "insert into services (service_name, cycle) values (?, ?)"
+#define INSERT_SERVICE "insert into services (service_number, service_name, cycle) values (?, ?, ?)"
 
 #define UPDATE_CYCLE "UPDATE services SET cycle = ? WHERE service_name = ?"
 
 #define DELETE_ALL_SERVICES "delete from services"
-
 #define DELETE_SERVICE_INFO "delete from services WHERE service_name = ?"
 
 #ifndef DATABASE_FULL_PATH
@@ -125,6 +124,7 @@ static sqlite3_stmt *delete_service_info;
 /* SELECT statements */
 static sqlite3_stmt *select_all_services;
 static sqlite3_stmt *select_service;
+static sqlite3_stmt *select_max_service_number;
 
 /* UPDATE statements */
 static sqlite3_stmt *update_cycle;
@@ -133,6 +133,7 @@ static sqlite3_stmt *update_cycle;
 static sqlite3_stmt *insert_service_info;
 
 static sqlite3 *database;
+static int max_service_number;
 
 static int __ua_service_db_busy(void *user, int attempts)
 {
@@ -218,6 +219,7 @@ static int __ua_service_prepare_select(sqlite3 *db)
 
        PREPARE_SELECT(select_all_services, SELECT_ALL_SERVICES);
        PREPARE_SELECT(select_service, SELECT_SERVICE);
+       PREPARE_SELECT(select_max_service_number, SELECT_MAX_SERVICE_NUMBER);
 
        initialized = 1;
        FUNC_EXIT;
@@ -298,6 +300,7 @@ static void __ua_service_finalize_select(void)
 
        FINALIZE(select_all_services);
        FINALIZE(select_service);
+       FINALIZE(select_max_service_number);
 
        FUNC_EXIT;
 }
@@ -329,6 +332,44 @@ static void __ua_service_table_servicesinfo_finalize(void)
        FUNC_EXIT;
 }
 
+static sqlite3 *__ua_service_db_get_database(void)
+{
+       if (database == NULL)
+               __ua_service_db_initialize_once();
+
+       return database;
+}
+
+int _uam_db_get_max_service_number(void)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = select_max_service_number;
+       int rc;
+
+       do {
+               rc = sqlite3_step(stmt);
+
+               switch (rc) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       UAM_DBG("Service number info found");
+                       max_service_number = sqlite3_column_int(stmt, 0);
+
+                       UAM_INFO("%d", max_service_number);
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate device info: %s\n",
+                                       sqlite3_errmsg(__ua_service_db_get_database()));
+               }
+       } while (rc == SQLITE_ROW);
+
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
 int _ua_service_db_deinitialize(void)
 {
        FUNC_ENTRY;
@@ -350,6 +391,8 @@ int _ua_service_db_initialize(void)
        __ua_service_db_initialize_once();
 
        EXEC(UAM_ERROR_NONE, __ua_service_table_servicesinfo_prepare(database));
+       if (_uam_db_get_max_service_number() != UAM_ERROR_NONE)
+               goto handle_error;
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -360,14 +403,6 @@ handle_error:
        return UAM_ERROR_DB_FAILED;
 }
 
-static sqlite3 *__ua_service_db_get_database(void)
-{
-       if (database == NULL)
-               __ua_service_db_initialize_once();
-
-       return database;
-}
-
 int _uam_db_insert_service_info(int *service_number, const char *service_name, int cycle)
 {
        FUNC_ENTRY;
@@ -378,10 +413,9 @@ int _uam_db_insert_service_info(int *service_number, const char *service_name, i
 
        UAM_INFO("%s-%d", service_name, cycle);
 
-       DB_ACTION(sqlite3_bind_text(stmt, 1, service_name, -1, SQLITE_TRANSIENT));
-       DB_ACTION(sqlite3_bind_int(stmt, 2, cycle));
-
-       *service_number = sqlite3_last_insert_rowid(database);
+       DB_ACTION(sqlite3_bind_int(stmt, 1, max_service_number + 1));
+       DB_ACTION(sqlite3_bind_text(stmt, 2, service_name, -1, SQLITE_TRANSIENT));
+       DB_ACTION(sqlite3_bind_int(stmt, 3, cycle));
 
        if (sqlite3_step(stmt) != SQLITE_DONE) {
                UAM_ERR("Failed to insert service info: %s\n",
@@ -391,6 +425,8 @@ int _uam_db_insert_service_info(int *service_number, const char *service_name, i
        }
 
        UAM_DBG("Service info inserted service_number:[%d]", *service_number);
+       max_service_number = max_service_number + 1;
+       *service_number = max_service_number;
 
 handle_error:
        sqlite3_reset(stmt);
@@ -471,6 +507,7 @@ int _uam_db_get_service_info(const char *service_name, db_service_info_t *info)
        FUNC_ENTRY;
        int error_code = UAM_ERROR_NONE;
        sqlite3_stmt *stmt = select_service;
+       int rc;
 
        retv_if(!info, UAM_ERROR_INVALID_PARAM);
 
@@ -478,19 +515,26 @@ int _uam_db_get_service_info(const char *service_name, db_service_info_t *info)
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, service_name, -1, SQLITE_STATIC));
 
-       if (sqlite3_step(stmt) != SQLITE_DONE) {
-               UAM_ERR("Failed to select service info %s\n",
-                               sqlite3_errmsg(__ua_service_db_get_database()));
-               error_code = UAM_ERROR_DB_FAILED;
-       } else {
-               UAM_DBG("Service info found");
-               info = g_new0(db_service_info_t, 1);
-               info->service_number = sqlite3_column_int(stmt, 0);
-               g_strlcpy(info->service_name, (char *)sqlite3_column_text(stmt, 1), UAM_SERVICE_MAX_STRING_LEN);
-               info->cycle = sqlite3_column_int(stmt, 2);
-
-               UAM_INFO("%d-%s-%d", info->service_number, info->service_name, info->cycle);
-       }
+       do {
+               rc = sqlite3_step(stmt);
+
+               switch (rc) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       UAM_DBG("Service info found");
+                       info = g_new0(db_service_info_t, 1);
+                       info->service_number = sqlite3_column_int(stmt, 0);
+                       g_strlcpy(info->service_name, (char *)sqlite3_column_text(stmt, 1), UAM_SERVICE_MAX_STRING_LEN);
+                       info->cycle = sqlite3_column_int(stmt, 2);
+
+                       UAM_INFO("%d-%s-%d", info->service_number, info->service_name, info->cycle);
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate device info: %s\n",
+                                       sqlite3_errmsg(__ua_service_db_get_database()));
+               }
+       } while (rc == SQLITE_ROW);
 
 handle_error:
        sqlite3_reset(stmt);
index d2b5512..807fba2 100644 (file)
 
 #define SELECT_USER_DATA "SELECT user_id, " \
        "name, account FROM userdata WHERE user_id = ?"
+#define SELECT_MAX_USER_ID "SELECT MAX(user_id) FROM userdata"
 
 #define INSERT_USER_INFO "insert into userdata " \
-       "(name, account)" \
-       "values (?, ?)"
+       "(user_id, name, account)" \
+       "values (?, ?, ?)"
 
 #define DELETE_ALL_USERS_DATA "delete from userdata "
 
@@ -129,6 +130,7 @@ static sqlite3_stmt *delete_user_info;
 /* SELECT statements */
 static sqlite3_stmt *select_all_users_data;
 static sqlite3_stmt *select_user_data;
+static sqlite3_stmt *select_max_user_id;
 
 /* UPDATE statements */
 //TODO
@@ -137,6 +139,7 @@ static sqlite3_stmt *select_user_data;
 static sqlite3_stmt *insert_user_info;
 
 static sqlite3 *database;
+static int max_user_id;
 
 static int __ua_db_busy(void *user, int attempts)
 {
@@ -222,6 +225,7 @@ static int __ua_prepare_select(sqlite3 *db)
 
        PREPARE_SELECT(select_all_users_data, SELECT_ALL_USERS_DATA);
        PREPARE_SELECT(select_user_data, SELECT_USER_DATA);
+       PREPARE_SELECT(select_max_user_id, SELECT_MAX_USER_ID);
 
        initialized = 1;
        FUNC_EXIT;
@@ -292,6 +296,7 @@ static void __ua_finalize_select(void)
 
        FINALIZE(select_all_users_data);
        FINALIZE(select_user_data);
+       FINALIZE(select_max_user_id);
 
        FUNC_EXIT;
 }
@@ -325,6 +330,43 @@ static void __ua_table_usersinfo_finalize(void)
        FUNC_EXIT;
 }
 
+static sqlite3 *__ua_db_get_database(void)
+{
+       if (database == NULL)
+               __ua_db_initialize_once();
+
+       return database;
+}
+
+int _uam_db_get_max_user_id(void)
+{
+       FUNC_ENTRY;
+       int error_code = UAM_ERROR_NONE;
+       sqlite3_stmt *stmt = select_max_user_id;
+       int rc;
+
+       do {
+               rc = sqlite3_step(stmt);
+
+               switch (rc) {
+               case SQLITE_DONE:
+                       break;
+               case SQLITE_ROW:
+                       max_user_id = sqlite3_column_int(stmt, 0);
+                       UAM_INFO("%d", max_user_id);
+                       break;
+               case SQLITE_ERROR:
+               default:
+                       UAM_ERR("Failed to enumerate device info: %s\n",
+                                       sqlite3_errmsg(__ua_db_get_database()));
+               }
+       } while (rc == SQLITE_ROW);
+
+       sqlite3_reset(stmt);
+       FUNC_EXIT;
+       return error_code;
+}
+
 int _ua_db_initialize(void)
 {
        FUNC_ENTRY;
@@ -341,6 +383,9 @@ int _ua_db_initialize(void)
        if (_ua_device_service_db_initialize() != UAM_ERROR_NONE)
                goto handle_error;
 
+       if (_uam_db_get_max_user_id() != UAM_ERROR_NONE)
+               goto handle_error;
+
        FUNC_EXIT;
        return UAM_ERROR_NONE;
 
@@ -370,14 +415,6 @@ int _ua_db_deinitialize(void)
        return UAM_ERROR_NONE;
 }
 
-static sqlite3 *__ua_db_get_database(void)
-{
-       if (database == NULL)
-               __ua_db_initialize_once();
-
-       return database;
-}
-
 int _ua_db_insert_user_info(int *user_id, const char *name, const char *account)
 {
        FUNC_ENTRY;
@@ -388,11 +425,11 @@ int _ua_db_insert_user_info(int *user_id, const char *name, const char *account)
 
        UAM_INFO("%s-%s", name, account);
 
-       DB_ACTION(sqlite3_bind_text(stmt, 1, name ? name : "",
+       DB_ACTION(sqlite3_bind_int(stmt, 1, max_user_id + 1));
+       DB_ACTION(sqlite3_bind_text(stmt, 2, name ? name : "",
                                -1, SQLITE_TRANSIENT));
-       DB_ACTION(sqlite3_bind_text(stmt, 2, account, -1, SQLITE_TRANSIENT));
+       DB_ACTION(sqlite3_bind_text(stmt, 3, account, -1, SQLITE_TRANSIENT));
 
-       *user_id = sqlite3_last_insert_rowid(database);
        if (sqlite3_step(stmt) != SQLITE_DONE) {
                UAM_ERR("Failed to insert user info: %s\n",
                                sqlite3_errmsg(__ua_db_get_database()));
@@ -401,6 +438,8 @@ int _ua_db_insert_user_info(int *user_id, const char *name, const char *account)
        }
 
        UAM_DBG("User info inserted [%d]", *user_id);
+       max_user_id = max_user_id + 1;
+       *user_id = max_user_id;
 
 handle_error:
        sqlite3_reset(stmt);