Skip SetMemoryMode in offline mode
[platform/core/appfw/pkgmgr-info.git] / src / server / database / pkg_set_db_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 "pkg_set_db_handler.hh"
18
19 #include <vector>
20
21 #include "db_handle_provider.hh"
22 #include "utils/logging.hh"
23
24 #include "pkgmgrinfo_debug.h"
25 #include "pkgmgrinfo_internal.h"
26
27 namespace pkgmgr_server {
28 namespace database {
29
30 PkgSetDBHandler::PkgSetDBHandler(uid_t uid, int pid, bool is_offline)
31     : AbstractDBHandler(uid, pid), uid_(uid), is_offline_(is_offline) {}
32
33 PkgSetDBHandler::~PkgSetDBHandler() {}
34
35 void PkgSetDBHandler::SetPkgInfo(package_x* package) {
36   package_ = package;
37 }
38
39 void PkgSetDBHandler::SetPkgID(std::string pkgid) {
40   pkgid = std::move(pkgid);
41 }
42
43 void PkgSetDBHandler::SetWriteType(pkgmgr_common::PkgWriteType write_type) {
44   write_type_ = write_type;
45 }
46
47 std::vector<std::vector<std::string>> PkgSetDBHandler::GetResult() {
48   return std::move(result_);
49 }
50
51 int PkgSetDBHandler::Execute() {
52   std::unique_lock<std::shared_timed_mutex> u(lock_);
53   SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
54   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
55
56   if (!Connect())
57     return PMINFO_R_ERROR;
58
59   if (!is_offline_)
60     DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID());
61
62   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
63   sqlite3* conn = conn_list.front().first;
64   int ret = 0;
65
66   if (write_type_ == pkgmgr_common::PkgWriteType::Insert)
67     ret = pkgmgr_parser_insert_pkg_info(conn, package_, uid_);
68   else if (write_type_ == pkgmgr_common::PkgWriteType::Update)
69     ret = pkgmgr_parser_update_pkg_info(conn, package_, uid_);
70   else if (write_type_ == pkgmgr_common::PkgWriteType::Delete)
71     ret = pkgmgr_parser_delete_pkg_info(conn, package_->package, uid_);
72   else
73     LOG(ERROR) << "Unknown db write type";
74
75   if (ret != PM_PARSER_R_OK)
76     return ret;
77
78   return ret;
79 }
80
81 }  // namespace database
82 }  // namespace pkgmgr_server