e260c7084680bdd5ba8fce519e765745337fcadf
[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::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   int ret = get_query_result(GetConnection(), query_.c_str(), &list, &row, &col);
48   if (ret != PMINFO_R_OK) {
49     // TODO: error log
50     return false;
51   }
52
53   result_.clear();
54   result_.resize(row);
55   GList* tmp = list;
56   for (int i = 0; i < row; ++i) {
57     for (int j = 0; j < col; ++j) {
58       result_[i].emplace_back(reinterpret_cast<char *>(tmp->data));
59       tmp = tmp->next;
60     }
61   }
62
63   g_list_free(list);
64   return true;
65 }
66
67 }  // namespace database
68 }  // namespace pkgmgr_common