Implement pkgmgrinfo_pkginfo_set_installed_storage
[platform/core/appfw/pkgmgr-info.git] / src / common / database / query_handler.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "query_handler.hh"
18
19 #include "pkgmgrinfo_internal.h"
20
21 namespace pkgmgr_common {
22 namespace database {
23
24 QueryHandler::QueryHandler(uid_t uid) : AbstractDBHandler(uid), uid_(uid) {}
25
26 QueryHandler::~QueryHandler() {}
27
28 void QueryHandler::SetQuery(std::vector<std::string> query) {
29   query_ = query;
30 }
31
32 std::string QueryHandler::GetString() { return std::string(); }
33 int QueryHandler::GetInt() { return 0; }
34 int QueryHandler::GetRecordCount() { return 0; }
35
36 std::vector<std::vector<std::string>>&& QueryHandler::GetResult() {
37   return std::move(result_);
38 }
39
40 bool QueryHandler::Execute() {
41   if (!Connect()) return false;
42
43   // TODO: db handle should be delivered
44   GList* list =nullptr;
45   int row = 0;
46   int col = 0;
47   if (query_.size() == 0)
48     return false;
49
50   if (GetOpType() == OPERATION_TYPE_READ) {
51     int ret = get_query_result(GetConnection(), query_[0].c_str(), &list, &row, &col);
52     if (ret != PMINFO_R_OK) {
53       // TODO: error log
54       return false;
55     }
56
57     result_.clear();
58     result_.resize(row);
59     GList* tmp = list;
60     for (int i = 0; i < row; ++i) {
61       for (int j = 0; j < col; ++j) {
62         result_[i].emplace_back(reinterpret_cast<char *>(tmp->data));
63         tmp = tmp->next;
64       }
65     }
66
67     g_list_free(list);
68     return true;
69   } else {
70     result_.clear();
71     result_.resize(1);
72     const char **queries = (const char **)calloc(query_.size(), sizeof(char *));
73     int i = 0;
74     for (const auto& query : query_)
75       queries[i++] = query.c_str();
76
77     int ret = execute_write_queries(GetConnection(), queries, query_.size());
78     if (ret != 0)
79       result_[0].emplace_back("FAIL");
80     else
81       result_[0].emplace_back("SUCCESS");
82     free(queries);
83     return true;
84   }
85 }
86
87 }  // namespace database
88 }  // namespace pkgmgr_common