Add MAPI - select with query 37/62937/2
authorSangkoo Kim <sangkoo.kim@samsung.com>
Mon, 21 Mar 2016 07:20:07 +0000 (16:20 +0900)
committerSangkoo Kim <sangkoo.kim@samsung.com>
Mon, 21 Mar 2016 07:42:02 +0000 (16:42 +0900)
Change-Id: I4232534c407e79b46fe11653b0dc9a476a79e661
Signed-off-by: Sangkoo Kim <sangkoo.kim@samsung.com>
include/mapi/msg_storage.h
include/proxy/MsgHandle.h
include/utils/MsgSqliteWrapper.h
include/utils/MsgUtilStorage.h
mapi/msg_storage.cpp
proxy/MsgHandleStorage.cpp
utils/MsgSqliteWrapper.cpp
utils/MsgUtilStorage.cpp

index 2cd864fbd4659a74709e16eb68a229a06de4fe77..99671372687929e98f780a03298ffd0ca811d8af 100755 (executable)
@@ -737,7 +737,7 @@ int msg_get_mem_size(msg_handle_t handle, unsigned int* memsize);
  * @retval MSG_ERR_NOT_SUPPORTED     Not supported
  */
 
-int msg_backup_message(msg_handle_t handle, msg_message_backup_type_t type, const char *backup_filepath);
+int msg_backup_message(msg_handle_t handle, msg_message_backup_type_t type, const char *backup_filepath) DEPRECATED;
 
 
 /**
@@ -760,7 +760,7 @@ int msg_backup_message(msg_handle_t handle, msg_message_backup_type_t type, cons
  * @retval MSG_ERR_NOT_SUPPORTED     Not supported
  */
 
-int msg_restore_message(msg_handle_t handle, const char *backup_filepath);
+int msg_restore_message(msg_handle_t handle, const char *backup_filepath) DEPRECATED;
 
 
 /**
@@ -1124,6 +1124,59 @@ int msg_delete_msgs_by_list(msg_handle_t handle, msg_id_list_s *msg_id_list);
  */
 
 int msg_set_conversation_to_read(msg_handle_t handle,  msg_thread_id_t thread_id);
+
+
+/**
+ * @brief Gets a DB records with specified query.
+ *
+ * @since_tizen 3.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/message.read
+ *
+ * @remarks You must release @a db_res using msg_db_free().
+ * @remarks You should set @a query with SQL query string after 'SELECT'.
+ *
+ * @param[in] handle     The Message handle
+ * @param[in] query      The SQL SELECT query
+ * @param[out] db_res    The result of SQL SELECT query
+ * @param[out] row_count The row count of result
+ * @param[out] col_count The column count of result
+ *
+ * @return  @c 0 on success,
+ *        otherwise a negative error value
+ *
+ * @retval MSG_SUCCESS               Success in operation
+ * @retval MSG_ERR_INVALID_PARAMETER Parameter is invalid
+ * @retval MSG_ERR_STORAGE_ERROR     Storage error
+ * @retval MSG_ERR_DB_BUSY           DB operation is busy
+ * @retval MSG_ERR_DB_GETTABLE       DB get table operation is failed
+ * @retval MSG_ERR_PERMISSION_DENIED The application does not have the privilege to call this method
+ * @retval MSG_ERR_NOT_SUPPORTED     Not supported
+ *
+ * @see msg_db_free()
+ */
+int msg_db_select_with_query(msg_handle_t handle, const char *query, char ***db_res, int *row_count, int *col_count);
+
+/**
+ * @brief Release memory for result of SQL query.
+ *
+ * @since_tizen 3.0
+ *
+ * @param[in] handle     The Message handle
+ * @param[in] db_res     The result of SQL SELECT query
+ *
+ * @return  @c 0 on success,
+ *        otherwise a negative error value
+ *
+ * @retval MSG_SUCCESS               Success in operation
+ * @retval MSG_ERR_INVALID_PARAMETER Parameter is invalid
+ * @retval MSG_ERR_STORAGE_ERROR     Storage error
+ * @retval MSG_ERR_PERMISSION_DENIED The application does not have the privilege to call this method
+ * @retval MSG_ERR_NOT_SUPPORTED     Not supported
+ *
+ * @see msg_db_select_with_query()
+ */
+int msg_db_free(msg_handle_t handle, char **db_res);
 /**
  *     @}
  */
index 4764f6ebefe3dec23a09bd3299da7e24eb328cf3..edd472f59e3421fb1ffc335e5982323bf8ecfe0b 100755 (executable)
@@ -129,6 +129,10 @@ class MsgHandle
                /* ETC */
                msg_error_t searchMessage(const char *pSearchString, msg_struct_list_s *pThreadViewList);
 
+
+               msg_error_t dbSelectWithQuery(const char *query, char ***db_res, int *row_count, int *col_count);
+               void dbFree(char **db_res);
+
                msg_error_t getRejectMsgList(const char *pNumber, msg_struct_list_s *pRejectMsgList);
                msg_error_t regStorageChangeCallback(msg_storage_change_cb onStorageChange, void *pUserParam);
                msg_error_t getReportStatus(msg_message_id_t msg_id, msg_struct_list_s *report_list);
index a9696daf3bcf657e475b53c4f6b4d77b8c3dd033..27a05f2ff8bda2cd1b80128306a90c08e60a9679 100755 (executable)
@@ -95,7 +95,9 @@ public:
        bool checkTableExist(const char *pTableName);
        msg_error_t execQuery(const char *pQuery);
        msg_error_t getTable(const char *pQuery, int *pRowCnt, int *pColumnCnt);
+       msg_error_t getTableWithResult(const char *pQuery, char ***res, int *pRowCnt, int *pColumnCnt);
        void freeTable();
+       void freeTable(char **db_res);
        msg_error_t bindText(const char *pBindStr, int index);
        msg_error_t bindInt(const int pBindint, int index);
        msg_error_t bindBlob(const void * pBindBlob, int size, int index);
index 4255fd91c1007bdb2a9b5cc245d94854de1fc67a..07ed6250a0c840736c634f89aaa99432ca2e9c8e 100755 (executable)
@@ -81,6 +81,9 @@ msg_error_t MsgStoGetAddressList(const msg_thread_id_t threadId, msg_struct_list
 msg_error_t MsgStoGetMessageList(const MSG_LIST_CONDITION_S *pListCond, msg_struct_list_s *pMsgList, int contactCount);
 msg_error_t MsgStoGetMediaList(const msg_thread_id_t threadId, msg_list_handle_t *pMediaList);
 
+msg_error_t MsgStoDbSelectWithQuery(const char *query, char ***db_res, int *row_count, int *col_count);
+void MsgStoDbFree(char **db_res);
+
 #ifdef FEATURE_SMS_CDMA
 msg_error_t MsgStoClearUniquenessTable();
 #endif
index b289a8e5e0a13627eb2d83f90c5ab62c92763ac3..4017d3a135235c0099cbc5ede84f85d12bbe8b7d 100755 (executable)
@@ -861,6 +861,49 @@ EXPORT_API int msg_search_message_for_thread_view(msg_handle_t handle, const cha
        return err;
 }
 
+
+EXPORT_API int msg_db_select_with_query(msg_handle_t handle, const char *query, char ***db_res, int *row_count, int *col_count)
+{
+       CHECK_MSG_SUPPORTED(MSG_TELEPHONY_SMS_FEATURE);
+       msg_error_t err = MSG_SUCCESS;
+
+       if (handle == NULL || query == NULL)
+               return MSG_ERR_INVALID_PARAMETER;
+
+       MsgHandle* pHandle = (MsgHandle*)handle;
+
+       try {
+               err = pHandle->dbSelectWithQuery(query, db_res, row_count, col_count);
+       } catch (MsgException& e) {
+               MSG_FATAL("%s", e.what());
+               return MSG_ERR_STORAGE_ERROR;
+       }
+
+       return err;
+}
+
+
+EXPORT_API int msg_db_free(msg_handle_t handle, char **db_res)
+{
+       CHECK_MSG_SUPPORTED(MSG_TELEPHONY_SMS_FEATURE);
+
+       if (handle == NULL || db_res == NULL)
+               return MSG_ERR_INVALID_PARAMETER;
+
+       MsgHandle* pHandle = (MsgHandle*)handle;
+
+       try {
+               pHandle->dbFree(db_res);
+       } catch (MsgException& e) {
+               MSG_FATAL("%s", e.what());
+               return MSG_ERR_STORAGE_ERROR;
+       }
+
+       return MSG_SUCCESS;
+}
+
+
+
 EXPORT_API int msg_get_reject_msg_list(msg_handle_t handle, const char *phone_num, msg_struct_list_s *msg_reject_msg_list)
 {
        CHECK_MSG_SUPPORTED(MSG_TELEPHONY_SMS_FEATURE);
index 3602578e4ef4fee0b3976593f04e51f320f857ea..85ec53c1704c4b414a0d25767646b464d10ff108 100755 (executable)
@@ -1883,3 +1883,25 @@ msg_error_t MsgHandle::getVobject(msg_message_id_t MsgId, void** encodedData)
 
        return MSG_SUCCESS;
 }
+
+
+msg_error_t MsgHandle::dbSelectWithQuery(const char *query, char ***db_res, int *row_count, int *col_count)
+{
+       msg_error_t err = MSG_SUCCESS;
+
+       err = MsgStoDbSelectWithQuery(query, db_res, row_count, col_count);
+
+       if (err != MSG_SUCCESS)
+               return err;
+
+       MSG_END();
+
+       return err;
+}
+
+
+void MsgHandle::dbFree(char **db_res)
+{
+       MsgStoDbFree(db_res);
+       MSG_END();
+}
index 9143e354e6993b3eac2d0484ed666959a8e0fc0f..2cd42066a89838c6093131e3e8e8b872eb10a638 100755 (executable)
@@ -78,6 +78,9 @@ msg_error_t MsgDbHandler::connect()
                if (ret == SQLITE_OK) {
                        MSG_DEBUG("DB Connect Success : [%p]", handle);
                        return MSG_SUCCESS;
+               } else if (ret == SQLITE_PERM){
+                       MSG_DEBUG("DB Connect Fail [%d]", ret);
+                       return MSG_ERR_PERMISSION_DENIED;
                } else {
                        MSG_DEBUG("DB Connect Fail [%d]", ret);
                        return MSG_ERR_DB_CONNECT;
@@ -106,6 +109,9 @@ msg_error_t MsgDbHandler::connectReadOnly()
                if (ret == SQLITE_OK) {
                        MSG_DEBUG("DB Connect Success : [%p]", handle);
                        return MSG_SUCCESS;
+               } else if (ret == SQLITE_PERM){
+                       MSG_DEBUG("DB Connect Fail [%d]", ret);
+                       return MSG_ERR_PERMISSION_DENIED;
                } else {
                        MSG_DEBUG("DB Connect Fail [%d]", ret);
                        return MSG_ERR_DB_CONNECT;
@@ -225,6 +231,36 @@ msg_error_t MsgDbHandler::getTable(const char *pQuery, int *pRowCnt, int *pColum
 }
 
 
+msg_error_t MsgDbHandler::getTableWithResult(const char *pQuery, char ***res, int *pRowCnt, int *pColumnCnt)
+{
+       int ret = 0;
+
+       *pRowCnt = 0;
+       if (pColumnCnt)
+               *pColumnCnt = 0;
+
+       MSG_DEBUG("[%s]", pQuery);
+       ret = sqlite3_get_table(handle, pQuery, res, pRowCnt, pColumnCnt, NULL);
+
+       if (ret == SQLITE_OK) {
+               if (*pRowCnt == 0) { /* when the no record return 'MSG_ERR_DB_NORECORD' */
+                       MSG_DEBUG("No Query Result");
+                       return MSG_ERR_DB_NORECORD;
+               }
+
+               MSG_DEBUG("Get Table Success");
+               return MSG_SUCCESS;
+       } else if (ret == SQLITE_BUSY) {
+               MSG_DEBUG("The database file is locked [%d]", ret);
+               return MSG_ERR_DB_BUSY;
+       } else {
+               MSG_DEBUG("Get Table Fail [%d]", ret);
+               return MSG_ERR_DB_GETTABLE;
+       }
+
+       return MSG_SUCCESS;
+}
+
 void MsgDbHandler::freeTable()
 {
        if (result) {
@@ -233,6 +269,13 @@ void MsgDbHandler::freeTable()
        }
 }
 
+void MsgDbHandler::freeTable(char **db_res)
+{
+       if (db_res) {
+               sqlite3_free_table(db_res);
+               db_res = NULL;
+       }
+}
 
 msg_error_t MsgDbHandler::bindText(const char *pBindStr, int index)
 {
index f56820ee95c48503bf4166c620734d973072db46..ad2e9aebe55dfb1d3bd30a24828f9632267036f4 100755 (executable)
@@ -28,6 +28,7 @@
 #include "MsgGconfWrapper.h"
 #include "MsgUtilFunction.h"
 #include "MsgUtilStorage.h"
+#include "MsgException.h"
 
 #include <storage.h>
 
@@ -2864,6 +2865,51 @@ msg_error_t MsgStoGetMediaList(const msg_thread_id_t threadId, msg_list_handle_t
        return MSG_SUCCESS;
 }
 
+
+msg_error_t MsgStoDbSelectWithQuery(const char *szQuery, char ***db_res, int *row_count, int *col_count)
+{
+       MSG_BEGIN();
+
+       msg_error_t err = MSG_SUCCESS;
+
+       MsgDbHandler *dbHandle = getDbHandle();
+       err = dbHandle->connectReadOnly();
+       if (err != MSG_SUCCESS) {
+               MSG_ERR("db connect (read only) is failed [%d]", err);
+               return err;
+       }
+
+       char *zSQL = sqlite3_mprintf("SELECT %q;", szQuery);
+
+       if (zSQL) {
+               err = dbHandle->getTableWithResult((const char *)zSQL, db_res, row_count, col_count);
+               sqlite3_free(zSQL);
+               zSQL = NULL;
+       } else {
+               THROW(MsgException::INVALID_RESULT, "sqlite3_mprintf() is failed");
+       }
+
+       MSG_DEBUG("getTableWithResult :: row_count=[%d], col_count=[%d]", *row_count, *col_count);
+
+       if (err == MSG_ERR_DB_NORECORD) {
+               dbHandle->freeTable(*db_res);
+               err = MSG_SUCCESS;
+       } else if (err != MSG_SUCCESS) {
+               MSG_DEBUG("Fail to getTable().");
+               dbHandle->freeTable(*db_res);
+       }
+
+       return err;
+}
+
+
+void MsgStoDbFree(char **db_res)
+{
+       MsgDbHandler *dbHandle = getDbHandle();
+       dbHandle->freeTable(db_res);
+}
+
+
 #ifdef FEATURE_SMS_CDMA
 msg_error_t MsgStoClearUniquenessTable()
 {