2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "SocialStatisticsTypes.h"
25 SocialDbHandle::SocialDbHandle(ContextProvider *provider) :
26 StatsDbHandleBase(provider)
30 SocialDbHandle::~SocialDbHandle()
34 int SocialDbHandle::read(CtxJson1 filter)
37 const char *subject = reqProvider->getSubject();
39 if (STR_EQ(subject, SOCIAL_SUBJ_FREQ_ADDRESS)) {
40 query = createSqlFreqAddress(filter);
42 } else if (STR_EQ(subject, SOCIAL_SUBJ_FREQUENCY)) {
44 query = createSqlFrequency(filter);
47 IF_FAIL_RETURN(!query.empty(), ERR_OPERATION_FAILED);
49 bool ret = executeQuery(filter, query.c_str());
50 IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
55 std::string SocialDbHandle::createWhereClause(CtxJson1 filter)
57 std::stringstream whereClause;
60 whereClause << StatsDbHandleBase::createWhereClause(filter);
62 filter.get(NULL, SOCIAL_COMMUNICATION_TYPE, &commType);
65 case SOCIAL_COMMUNICATION_TYPE_CALL:
67 " AND " SOCIAL_PHONE_LOG_TYPE " >= " << CONTACTS_PLOG_TYPE_VOICE_INCOMING <<
68 " AND " SOCIAL_PHONE_LOG_TYPE " <= " << CONTACTS_PLOG_TYPE_VIDEO_BLOCKED;
70 case SOCIAL_COMMUNICATION_TYPE_MESSAGE:
72 " AND " SOCIAL_PHONE_LOG_TYPE " >= " << CONTACTS_PLOG_TYPE_MMS_INCOMING <<
73 " AND " SOCIAL_PHONE_LOG_TYPE " <= " << CONTACTS_PLOG_TYPE_MMS_BLOCKED;
79 return whereClause.str();
82 std::string SocialDbHandle::createSqlFreqAddress(CtxJson1 filter)
84 std::stringstream query;
85 int limit = DEFAULT_LIMIT;
87 filter.get(NULL, KEY_RESULT_SIZE, &limit);
90 "SELECT " SOCIAL_ADDRESS ", " \
91 "COUNT(*) AS " KEY_TOTAL_COUNT ", " \
92 "SUM(" KEY_DURATION ") AS " KEY_TOTAL_DURATION ", " \
93 "MAX(" KEY_UNIV_TIME ") AS " KEY_LAST_TIME \
94 " FROM " SOCIAL_TABLE_CONTACT_LOG \
95 " WHERE " << createWhereClause(filter) <<
96 " GROUP BY " SOCIAL_ADDRESS \
97 " ORDER BY COUNT(*) DESC" \
103 std::string SocialDbHandle::createSqlFrequency(CtxJson1 filter)
105 CtxJson1 filterCleaned;
107 std::string timeOfDay;
110 if (!filter.get(NULL, SOCIAL_ADDRESS, &address)) {
111 _E("Invalid parameter");
115 if (filter.get(NULL, KEY_DAY_OF_WEEK, &weekStr))
116 filterCleaned.set(NULL, KEY_DAY_OF_WEEK, weekStr);
118 if (filter.get(NULL, KEY_TIME_OF_DAY, &timeOfDay))
119 filterCleaned.set(NULL, KEY_TIME_OF_DAY, timeOfDay);
121 std::stringstream query;
124 "DELETE FROM " SOCIAL_TEMP_CONTACT_FREQ ";";
127 "INSERT INTO " SOCIAL_TEMP_CONTACT_FREQ \
128 " SELECT " SOCIAL_ADDRESS ", COUNT(*) AS " KEY_TOTAL_COUNT \
129 " FROM " SOCIAL_TABLE_CONTACT_LOG \
130 " WHERE " << createWhereClause(filterCleaned) <<
131 " GROUP BY " SOCIAL_ADDRESS ";";
134 "INSERT OR IGNORE INTO " SOCIAL_TEMP_CONTACT_FREQ " (" SOCIAL_ADDRESS ")" \
135 " VALUES ('" << address << "');";
138 "SELECT S." SOCIAL_ADDRESS ", S." KEY_TOTAL_COUNT ", 1+COUNT(lesser." KEY_TOTAL_COUNT ") AS " KEY_RANK \
139 " FROM " SOCIAL_TEMP_CONTACT_FREQ " AS S" \
140 " LEFT JOIN " SOCIAL_TEMP_CONTACT_FREQ " AS lesser" \
141 " ON S." KEY_TOTAL_COUNT " < lesser." KEY_TOTAL_COUNT \
142 " WHERE S." SOCIAL_ADDRESS " = '" << address << "'";
148 void SocialDbHandle::replyTriggerItem(int error, CtxJson1 &jsonResult)
150 IF_FAIL_VOID_TAG(STR_EQ(reqProvider->getSubject(), SOCIAL_SUBJ_FREQUENCY), _E, "Invalid subject");
156 jsonResult.get(NULL, SOCIAL_ADDRESS, &valStr);
157 results.set(NULL, SOCIAL_ADDRESS, valStr);
158 jsonResult.get(NULL, KEY_TOTAL_COUNT, &val);
159 results.set(NULL, KEY_TOTAL_COUNT, val);
160 jsonResult.get(NULL, KEY_RANK, &val);
161 results.set(NULL, KEY_RANK, val);
163 reqProvider->replyToRead(reqFilter, error, results);