Merge branch 'tizen' of ssh://review.tizen.org:29418/platform/core/appfw/pkgmgr-info
[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   virtual ~AbstractDBHandler();
41   virtual int Execute() = 0;
42   void SetLocale(std::string locale);
43   void SetDBType(pkgmgr_common::DBType type);
44   void SetOpType(pkgmgr_common::DBOperationType type);
45   pkgmgr_common::DBOperationType GetOpType();
46
47  protected:
48   virtual bool Connect();
49   int GetPID();
50   uid_t GetUID();
51   virtual std::vector<std::pair<sqlite3*, uid_t>> GetConnection();
52   void ClearDBHandle();
53   const std::string& GetLocale();
54   static std::shared_timed_mutex lock_;
55  private:
56   std::vector<std::pair<std::string, uid_t>> GetDBPath();
57   pkgmgr_common::DBType db_type_ = pkgmgr_common::DBType::DB_TYPE_NONE;
58   pkgmgr_common::DBOperationType op_type_ =
59       pkgmgr_common::DBOperationType::OPERATION_TYPE_NONE;
60   uid_t uid_;
61   pid_t pid_;
62   std::string locale_;
63   std::vector<std::pair<sqlite3*, uid_t>> db_handle_list_;
64 };
65
66 }  // namespace database
67 }  // namespace pkgmgr_server
68
69 #endif  // ABSTRACT_DB_HANDLER_HH_
70