Fix Metadata Plugin Parser
[platform/core/appfw/aul-1.git] / parser / metadata / common / database.hh
1 /*
2  * Copyright (c) 2020 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 COMMON_DATABASE_HH_
18 #define COMMON_DATABASE_HH_
19
20 #include <sqlite3.h>
21
22 #include <string>
23
24 #include "common/exception.hh"
25
26 #define __PREPARE_V2(db, query, length, stmt) do {                             \
27   int ret = sqlite3_prepare_v2(db, query, length, &stmt, nullptr);             \
28   if (ret != SQLITE_OK) {                                                      \
29     _E("sqlite3_prepare_v2() is failed. error(%s:%d)",                         \
30         sqlite3_errmsg(db), ret);                                              \
31     THROW(-ret);                                                               \
32   }                                                                            \
33 } while (0)
34
35
36 #define __BIND_TEXT(db, stmt, i, text) do {                                    \
37   int ret = sqlite3_bind_text(stmt, i, text, -1, SQLITE_TRANSIENT);            \
38   if (ret != SQLITE_OK) {                                                      \
39     _E("sqlite3_bind_text() is failed. index(%d), error(%s:%d)",               \
40         i, sqlite3_errmsg(db), ret);                                           \
41     THROW(-ret);                                                               \
42   }                                                                            \
43 } while (0)
44
45 #define __STEP(db, stmt) do {                                                  \
46   int ret = sqlite3_step(stmt);                                                \
47   if (ret != SQLITE_DONE) {                                                    \
48     _E("sqlite3_step() is failed. error(%s:%d)", sqlite3_errmsg(db), ret);     \
49     THROW(-ret);                                                               \
50   }                                                                            \
51 } while (0)
52
53 namespace plugin {
54
55 class Database {
56  public:
57   Database(std::string path);
58   virtual ~Database();
59
60   void Open();
61   void Close();
62   bool IntegrityCheck();
63   void BeginTransaction();
64   void EndTransaction();
65   void Rollback();
66   sqlite3* GetHandle();
67   std::string ColumnText(sqlite3_stmt* stmt, int index);
68
69  private:
70   static int BusyHandler(void* data, int count);
71
72  private:
73   std::string path_;
74   sqlite3* db_ = nullptr;
75 };
76
77 }  // namespace plugin
78
79 #endif  // COMMON_DATABASE_HH_