Remove the memory database for the cache
[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 "cache_flag.hh"
22 #include "db_handle_provider.hh"
23 #include "utils/logging.hh"
24
25 #include "pkgmgrinfo_debug.h"
26 #include "pkgmgrinfo_internal.h"
27
28 namespace pkgmgr_server {
29 namespace database {
30
31 PkgSetDBHandler::PkgSetDBHandler(uid_t uid, int pid, bool is_offline)
32     : AbstractDBHandler(uid, pid), uid_(uid), is_offline_(is_offline) {}
33
34 PkgSetDBHandler::~PkgSetDBHandler() {}
35
36 void PkgSetDBHandler::SetPkgInfo(package_x* package) {
37   package_ = package;
38 }
39
40 void PkgSetDBHandler::SetPkgID(std::string pkgid) {
41   pkgid = std::move(pkgid);
42 }
43
44 void PkgSetDBHandler::SetWriteType(pkgmgr_common::PkgWriteType write_type) {
45   write_type_ = write_type;
46 }
47
48 std::vector<std::vector<std::string>> PkgSetDBHandler::GetResult() {
49   return std::move(result_);
50 }
51
52 int PkgSetDBHandler::Execute() {
53   std::unique_lock<std::shared_mutex> u(lock_);
54   SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
55   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
56
57   if (!Connect())
58     return PMINFO_R_ERROR;
59
60   const auto& db = GetConnection().front().first;
61   int ret = 0;
62
63   if (write_type_ == pkgmgr_common::PkgWriteType::Insert)
64     ret = pkgmgr_server::internal::InsertPkgInfo(db, package_, uid_);
65   else if (write_type_ == pkgmgr_common::PkgWriteType::Update)
66     ret = pkgmgr_server::internal::UpdatePkgInfo(db, package_, uid_);
67   else if (write_type_ == pkgmgr_common::PkgWriteType::Delete)
68     ret = pkgmgr_server::internal::DeletePkgInfo(db, package_->package, uid_);
69   else
70     LOG(ERROR) << "Unknown db write type";
71
72   if (is_offline_ || ret != PMINFO_R_OK)
73     return ret;
74
75   auto lock = CacheFlag::GetWriterLock();
76   if (CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
77     DBHandleProvider::GetInst(uid_).RegisterPendingPackageInfo(package_);
78
79   return ret;
80 }
81
82 }  // namespace database
83 }  // namespace pkgmgr_server