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.
18 #include <types_internal.h>
19 #include <context_mgr.h>
21 #include <system_info.h>
22 #include "media_stats_types.h"
23 #include "db_handle.h"
25 ctx::media_db_handle::media_db_handle(const char* zone)
26 : stats_db_handle_base(zone)
30 ctx::media_db_handle::~media_db_handle()
34 int ctx::media_db_handle::read(const char* subject, ctx::json filter)
36 //TODO: filter validation (in the API side?)
39 if (STR_EQ(subject, MEDIA_SUBJ_PEAK_TIME_FOR_MUSIC)) {
40 query = create_sql_peak_time(MEDIA_TYPE_MUSIC, filter);
42 } else if (STR_EQ(subject, MEDIA_SUBJ_PEAK_TIME_FOR_VIDEO)) {
43 query = create_sql_peak_time(MEDIA_TYPE_VIDEO, filter);
45 } else if (STR_EQ(subject, MEDIA_SUBJ_COMMON_SETTING_FOR_MUSIC)) {
46 query = create_sql_common_setting(MEDIA_TYPE_MUSIC, filter);
48 } else if (STR_EQ(subject, MEDIA_SUBJ_COMMON_SETTING_FOR_VIDEO)) {
49 query = create_sql_common_setting(MEDIA_TYPE_VIDEO, filter);
51 } else if (STR_EQ(subject, MEDIA_SUBJ_MUSIC_FREQUENCY)) {
52 is_trigger_item = true;
53 query = create_sql_frequency(MEDIA_TYPE_MUSIC, filter);
55 } else if (STR_EQ(subject, MEDIA_SUBJ_VIDEO_FREQUENCY)) {
56 is_trigger_item = true;
57 query = create_sql_frequency(MEDIA_TYPE_VIDEO, filter);
60 IF_FAIL_RETURN(!query.empty(), ERR_OPERATION_FAILED);
62 bool ret = execute_query(subject, filter, query.c_str());
63 IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
68 std::string ctx::media_db_handle::create_where_clause(int media_type, ctx::json filter)
70 std::stringstream where_clause;
72 where_clause << CX_MEDIA_TYPE " = " << media_type << " AND ";
73 where_clause << stats_db_handle_base::create_where_clause(filter);
75 return where_clause.str();
78 std::string ctx::media_db_handle::create_sql_peak_time(int media_type, ctx::json filter)
80 std::string where = create_where_clause(media_type, filter);
81 return stats_db_handle_base::create_sql_peak_time(filter, MEDIA_TABLE_NAME, where);
84 std::string ctx::media_db_handle::create_sql_common_setting(int media_type, ctx::json filter)
86 std::string where = create_where_clause(media_type, filter);
87 return stats_db_handle_base::create_sql_common_setting(filter, MEDIA_TABLE_NAME, where);
90 std::string ctx::media_db_handle::create_sql_frequency(int media_type, ctx::json filter)
92 ctx::json filter_cleaned;
94 std::string time_of_day;
96 if (filter.get(NULL, STATS_DAY_OF_WEEK, &week_str))
97 filter_cleaned.set(NULL, STATS_DAY_OF_WEEK, week_str);
99 if (filter.get(NULL, STATS_TIME_OF_DAY, &time_of_day))
100 filter_cleaned.set(NULL, STATS_TIME_OF_DAY, time_of_day);
102 std::string where_clause = create_where_clause(media_type, filter_cleaned);
104 std::stringstream query;
106 "SELECT IFNULL(COUNT(*),0) AS " STATS_TOTAL_COUNT \
107 " FROM " MEDIA_TABLE_NAME \
108 " WHERE " << where_clause;