Fix crash when invalid parameter given
[platform/core/api/capability-manager.git] / src / sqlite_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 SQLITE_CONNECTION_H_
6 #define SQLITE_CONNECTION_H_
7
8 #include <sqlite3.h>
9
10 #include <memory>
11 #include <mutex>
12 #include <string>
13
14 #include "src/sql_connection.h"
15 #include "src/sql_statement.h"
16
17 namespace capmgr {
18
19 class SQLTransaction;
20
21 class SQLiteConnection final : public SQLConnection {
22  public:
23   explicit SQLiteConnection(std::string path, bool readonly);
24   virtual ~SQLiteConnection();
25
26   // non-copyable
27   SQLiteConnection(const SQLiteConnection&) = delete;
28   SQLiteConnection& operator=(const SQLiteConnection&) = delete;
29
30   bool Execute(const std::string& command) override;
31
32   std::shared_ptr<SQLStatement> PrepareStatement(
33       const std::string& query) override;
34
35   bool IsValid() override;
36
37  private:
38   bool Connect(bool readonly);
39   bool Disconnect();
40   std::string GetErrorMessage() const;
41
42   bool BeginTransaction() override;
43   bool CommitTransaction() override;
44   bool RollbackTransaction() override;
45
46   std::string path_;
47   sqlite3* db_;
48 };
49
50 }  // namespace capmgr
51
52 #endif  // SQLITE_CONNECTION_H_