Fix the use of cache if not the requested uid default
[platform/core/appfw/pkgmgr-info.git] / src / server / database / abstract_db_handler.hh
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 #ifndef ABSTRACT_DB_HANDLER_HH_
18 #define ABSTRACT_DB_HANDLER_HH_
19
20 #include <shared_mutex>
21 #include <string>
22 #include <vector>
23
24 #include <sys/types.h>
25 #include <sqlite3.h>
26
27 #include "db_type.hh"
28 #include "query_parcelable.hh"
29
30 namespace pkgmgr_server {
31 namespace database {
32
33 #ifndef EXPORT_API
34 #define EXPORT_API __attribute__((visibility("default")))
35 #endif
36
37 class EXPORT_API AbstractDBHandler {
38  public:
39   AbstractDBHandler(uid_t uid, pid_t pid)
40       : db_type_(pkgmgr_common::DBType::DB_TYPE_NONE),
41         op_type_(pkgmgr_common::DBOperationType::OPERATION_TYPE_NONE),
42         uid_(uid), pid_(pid) {};
43   virtual ~AbstractDBHandler();
44   virtual int Execute() = 0;
45   void SetLocale(std::string locale);
46   void SetDBType(pkgmgr_common::DBType type);
47   void SetOpType(pkgmgr_common::DBOperationType type);
48   pkgmgr_common::DBOperationType GetOpType();
49
50  protected:
51   virtual bool Connect();
52   int GetPID();
53   uid_t GetUID();
54   std::vector<std::pair<std::string, uid_t>> GetDBPath();
55   virtual std::vector<std::pair<sqlite3*, uid_t>> GetConnection();
56   void ClearDBHandle();
57   const std::string& GetLocale();
58   static uid_t GetDefaultUser();
59
60   static std::shared_timed_mutex lock_;
61
62  private:
63   pkgmgr_common::DBType db_type_;
64   pkgmgr_common::DBOperationType op_type_;
65   uid_t uid_;
66   pid_t pid_;
67   std::string locale_;
68   std::vector<std::pair<sqlite3*, uid_t>> db_handle_list_;
69 };
70
71 }  // namespace database
72 }  // namespace pkgmgr_server
73
74 #endif  // ABSTRACT_DB_HANDLER_HH_