Modify search message logic.
authorKeebum Kim <keebum.kim@samsung.com>
Sun, 6 Oct 2013 03:35:19 +0000 (12:35 +0900)
committerKeebum Kim <keebum.kim@samsung.com>
Sun, 6 Oct 2013 03:35:19 +0000 (12:35 +0900)
framework/storage-handler/MsgStorageMessage.cpp

index 1f2a954..ff045a2 100755 (executable)
@@ -2613,8 +2613,25 @@ msg_error_t MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int of
        int rowCnt = 0;
        int index = 26; // numbers of index
 
-       char sqlQuery[MAX_QUERY_LEN+1];
-       char sqlQuerySubset[(MAX_QUERY_LEN/5)+1];
+       unsigned int tmpSize = 0;
+
+       unsigned int searchValSize = 0;
+       unsigned int addrValSize = 0;
+
+       if (pSearchCon->pSearchVal)
+               searchValSize = strlen(pSearchCon->pSearchVal);
+
+       if (pSearchCon->pAddressVal)
+               addrValSize = strlen(pSearchCon->pAddressVal);
+
+       MSG_DEBUG("searchValSize [%d]", searchValSize);
+       MSG_DEBUG("addrValSize [%d]", addrValSize);
+
+       unsigned int maxQuerySize = MAX_QUERY_LEN+(searchValSize*6)+addrValSize+1;
+
+       MSG_DEBUG("MAX_QUERY_LEN+(searchValSize*6)+addrValSize+1 [%d]", maxQuerySize);
+
+       char sqlQuery[maxQuerySize];
 
        char firstName[MAX_DISPLAY_NAME_LEN+1], lastName[MAX_DISPLAY_NAME_LEN+1];
        char displayName[MAX_DISPLAY_NAME_LEN+1];
@@ -2635,41 +2652,36 @@ msg_error_t MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int of
                        "FROM %s A, %s B WHERE A.CONV_ID = B.CONV_ID AND B.ADDRESS_ID <> 0 ",
                        MSGFW_MESSAGE_TABLE_NAME, MSGFW_ADDRESS_TABLE_NAME);
 
-       //// folder
-       memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
-
+       tmpSize = strlen(sqlQuery);
        if (pSearchCon->folderId == MSG_ALLBOX_ID)
-               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID > 0 AND A.FOLDER_ID < %d ", MSG_CBMSGBOX_ID);
+               snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND A.FOLDER_ID > 0 AND A.FOLDER_ID < %d ", MSG_CBMSGBOX_ID);
        else if (pSearchCon->folderId == MSG_IOSBOX_ID)
-               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID > 0 AND A.FOLDER_ID < %d ", MSG_DRAFT_ID);
+               snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND A.FOLDER_ID > 0 AND A.FOLDER_ID < %d ", MSG_DRAFT_ID);
        else
-               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.FOLDER_ID = %d ", pSearchCon->folderId);
-
-       strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
+               snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND A.FOLDER_ID = %d ", pSearchCon->folderId);
 
 
        //// msg type
-       memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
-
+       tmpSize = strlen(sqlQuery);
        switch (pSearchCon->msgType) {
                case MSG_TYPE_SMS:
-                       snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_NORMAL_SMS);
+                       snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_NORMAL_SMS);
                        break;
 
                case MSG_TYPE_MMS:
-                       snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d ", MSG_MMS_TYPE);
+                       snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND A.MAIN_TYPE = %d ", MSG_MMS_TYPE);
                        break;
 
                case MSG_TYPE_MMS_JAVA:
-                       snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_MMS_TYPE, MSG_SENDREQ_JAVA_MMS);
+                       snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_MMS_TYPE, MSG_SENDREQ_JAVA_MMS);
                        break;
 
                case MSG_TYPE_SMS_SYNCML:
-                       snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_SYNCML_CP);
+                       snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_SYNCML_CP);
                        break;
 
                case MSG_TYPE_SMS_REJECT:
-                       snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_REJECT_SMS);
+                       snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND A.MAIN_TYPE = %d AND A.SUB_TYPE = %d ", MSG_SMS_TYPE, MSG_REJECT_SMS);
                        break;
 
                default:
@@ -2677,18 +2689,18 @@ msg_error_t MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int of
                        break;
        }
 
-       strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
 
        /// string
        if (pSearchCon->pSearchVal != NULL) {
 
+               tmpSize = strlen(sqlQuery);
+
                // Replace string for '%' and '_' character
                ext1_str = MsgStoReplaceString(pSearchCon->pSearchVal, "_", "\\_");
                ext2_str = MsgStoReplaceString(ext1_str, "%", "\\%");
                ext3_str = MsgStoReplaceString(ext2_str, "'", "''");
 
-               memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
-               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset),
+               snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize,
                                "AND ( A.MSG_TEXT LIKE '%%%s%%' ESCAPE '\\' "
                                "OR A.SUBJECT LIKE '%%%s%%' ESCAPE '\\' "
                                "OR B.ADDRESS_VAL LIKE '%%%s%%' ESCAPE '\\' "
@@ -2696,7 +2708,6 @@ msg_error_t MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int of
                                "OR B.FIRST_NAME LIKE '%%%s%%' ESCAPE '\\' "
                                "OR B.LAST_NAME LIKE '%%%s%%' ESCAPE '\\') ",
                                ext3_str, ext3_str, ext3_str, ext3_str, ext3_str, ext3_str);
-               strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
 
                if (ext1_str) {
                        free(ext1_str);
@@ -2717,14 +2728,14 @@ msg_error_t MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int of
        /// address
        if (pSearchCon->pAddressVal != NULL) {
 
+               tmpSize = strlen(sqlQuery);
+
                // Replace string for '%' and '_' character
                ext1_str = MsgStoReplaceString(pSearchCon->pAddressVal, "_", "\\_");
                ext2_str = MsgStoReplaceString(ext1_str, "%", "\\%");
                ext3_str = MsgStoReplaceString(ext2_str, "'", "''");
 
-               memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
-               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "AND B.ADDRESS_VAL LIKE '%%%s%%' ESCAPE '\\' ", ext3_str);
-               strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
+               snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "AND B.ADDRESS_VAL LIKE '%%%s%%' ESCAPE '\\' ", ext3_str);
 
                if (ext1_str) {
                        free(ext1_str);
@@ -2743,14 +2754,11 @@ msg_error_t MsgStoSearchMessage(const MSG_SEARCH_CONDITION_S *pSearchCon, int of
        }
 
        /// limit, offset
-       memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
-
+       tmpSize = strlen(sqlQuery);
        if (offset >= 0 && limit > 0)
-               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "GROUP BY A.MSG_ID ORDER BY A.DISPLAY_TIME DESC LIMIT %d OFFSET %d;", limit, offset);
+               snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "GROUP BY A.MSG_ID ORDER BY A.DISPLAY_TIME DESC LIMIT %d OFFSET %d;", limit, offset);
        else
-               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "GROUP BY A.MSG_ID ORDER BY A.DISPLAY_TIME DESC;");
-
-       strncat(sqlQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(sqlQuery));
+               snprintf(sqlQuery+tmpSize, maxQuerySize-tmpSize, "GROUP BY A.MSG_ID ORDER BY A.DISPLAY_TIME DESC;");
 
        msg_error_t err = dbHandle.getTable(sqlQuery, &rowCnt);