Change to use dlopen for direct access
[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
23 #include "pkgmgrinfo_debug.h"
24 #include "pkgmgrinfo_internal.h"
25
26 namespace pkgmgr_server {
27 namespace database {
28
29 PkgSetDBHandler::PkgSetDBHandler(uid_t uid, int pid)
30     : AbstractDBHandler(uid, pid), uid_(uid) {}
31
32 PkgSetDBHandler::~PkgSetDBHandler() {}
33
34 void PkgSetDBHandler::SetPkgInfo(package_x* package) {
35   package_ = package;
36 }
37
38 void PkgSetDBHandler::SetPkgID(std::string pkgid) {
39   pkgid = std::move(pkgid);
40 }
41
42 void PkgSetDBHandler::SetWriteType(pkgmgr_common::PkgWriteType write_type) {
43   write_type_ = write_type;
44 }
45
46 std::vector<std::vector<std::string>> PkgSetDBHandler::GetResult() {
47   return std::move(result_);
48 }
49
50 int PkgSetDBHandler::Execute() {
51   std::unique_lock<std::shared_timed_mutex> u(lock_);
52   SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
53   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
54
55   if (!Connect())
56     return PMINFO_R_ERROR;
57
58   DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID(), true);
59   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
60   sqlite3* conn = conn_list.front().first;
61   int ret = 0;
62   if (write_type_ == pkgmgr_common::PkgWriteType::Insert)
63     ret = pkgmgr_parser_insert_pkg_info(conn, package_, uid_);
64   else if (write_type_ == pkgmgr_common::PkgWriteType::Update)
65     ret = pkgmgr_parser_update_pkg_info(conn, package_, uid_);
66   else if (write_type_ == pkgmgr_common::PkgWriteType::Delete)
67     ret = pkgmgr_parser_delete_pkg_info(conn, package_->package, uid_);
68   else
69     _LOGE("Unknown db write type");
70
71   if (ret != PM_PARSER_R_OK)
72     return ret;
73
74   return ret;
75 }
76
77 }  // namespace database
78 }  // namespace pkgmgr_server