Fix crash when invalid parameter given
[platform/core/api/capability-manager.git] / src / sql_connection.h
1 // Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #ifndef SQL_CONNECTION_H_
6 #define SQL_CONNECTION_H_
7
8 #include <memory>
9 #include <mutex>
10 #include <string>
11 #include <utility>
12
13 #include "src/sql_transaction.h"
14
15 namespace capmgr {
16
17 class SQLStatement;
18 class SQLTransaction;
19
20 class SQLConnection {
21  public:
22   SQLConnection() : error_code_(0), transaction_(this) {}
23   virtual ~SQLConnection() = default;
24
25   virtual bool Execute(const std::string& command) = 0;
26
27   virtual std::shared_ptr<SQLStatement> PrepareStatement(
28       const std::string& query) = 0;
29
30   virtual bool IsValid() = 0;
31
32   virtual void SetErrorCode(int error_code) {
33     error_code_ = error_code;
34   }
35
36   virtual int GetErrorCode() {
37     return error_code_;
38   }
39
40   virtual std::unique_lock<const SQLTransaction> GetTransactionGuard() {
41     return std::move(std::unique_lock<const SQLTransaction>(transaction_));
42   }
43
44  protected:
45   friend class SQLTransaction;
46
47   virtual void UnsetErrorCode() {
48     error_code_ = 0;
49   }
50
51   virtual bool BeginTransaction() = 0;
52   virtual bool CommitTransaction() = 0;
53   virtual bool RollbackTransaction() = 0;
54
55   int error_code_;
56   SQLTransaction transaction_;
57 };
58
59 }  // namespace capmgr
60
61 #endif  // SQL_CONNECTION_H_