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,
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
);
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);
"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 = ?"
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;
static sqlite3_stmt *insert_device_info;
static sqlite3 *database;
+static int max_device_number;
static int __ua_device_db_busy(void *user, int attempts)
{
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;
FINALIZE(select_all_devices);
FINALIZE(select_device);
FINALIZE(select_device_number);
+ FINALIZE(select_max_device_number);
FUNC_EXIT;
}
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;
__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;
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)
{
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",
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);
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;
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);
#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 " \
/* SELECT statements */
static sqlite3_stmt *select_service;
-static sqlite3_stmt *select_service_info;
-static sqlite3_stmt *select_device_info;
/* UPDATE statements */
//TO-DO
}
PREPARE_SELECT(select_service, SELECT_SERVICE);
- //PREPARE_SELECT(select_service_info, SELECT_SERVICE_INFO);
initialized = 1;
FUNC_EXIT;
FUNC_ENTRY;
FINALIZE(select_service);
- FINALIZE(select_service_info);
- FINALIZE(select_device_info);
FUNC_EXIT;
}
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;
#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
/* 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;
static sqlite3_stmt *insert_service_info;
static sqlite3 *database;
+static int max_service_number;
static int __ua_service_db_busy(void *user, int attempts)
{
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;
FINALIZE(select_all_services);
FINALIZE(select_service);
+ FINALIZE(select_max_service_number);
FUNC_EXIT;
}
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;
__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;
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;
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",
}
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);
FUNC_ENTRY;
int error_code = UAM_ERROR_NONE;
sqlite3_stmt *stmt = select_service;
+ int rc;
retv_if(!info, UAM_ERROR_INVALID_PARAM);
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);
#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 "
/* 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
static sqlite3_stmt *insert_user_info;
static sqlite3 *database;
+static int max_user_id;
static int __ua_db_busy(void *user, int attempts)
{
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;
FINALIZE(select_all_users_data);
FINALIZE(select_user_data);
+ FINALIZE(select_max_user_id);
FUNC_EXIT;
}
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;
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;
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;
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()));
}
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);