Shaka Player Embedded
sqlite.h
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_JS_IDB_SQLITE_H
16 #define SHAKA_EMBEDDED_JS_IDB_SQLITE_H
17 
18 #include <atomic>
19 #include <string>
20 #include <vector>
21 
22 #include "shaka/optional.h"
23 #include "src/util/macros.h"
24 
25 struct sqlite3;
26 
27 namespace shaka {
28 namespace js {
29 namespace idb {
30 
31 enum class DatabaseStatus {
32  Success,
33 
35  NotFound,
39  Busy,
46 };
47 
55  public:
59 
61 
63 
64  bool valid() const {
65  return db_;
66  }
67 
68  DatabaseStatus CreateDb(const std::string& db_name, int64_t version);
69  DatabaseStatus UpdateDbVersion(const std::string& db_name, int64_t version);
70  DatabaseStatus DeleteDb(const std::string& db_name);
71  DatabaseStatus GetDbVersion(const std::string& db_name, int64_t* version);
72 
73  DatabaseStatus CreateObjectStore(const std::string& db_name,
74  const std::string& store_name);
75  DatabaseStatus DeleteObjectStore(const std::string& db_name,
76  const std::string& store_name);
77  DatabaseStatus ListObjectStores(const std::string& db_name,
78  std::vector<std::string>* names);
79 
81  DatabaseStatus AddData(const std::string& db_name,
82  const std::string& store_name,
83  const std::vector<uint8_t>& data, int64_t* key);
85  DatabaseStatus GetData(const std::string& db_name,
86  const std::string& store_name, int64_t key,
87  std::vector<uint8_t>* data);
92  DatabaseStatus UpdateData(const std::string& db_name,
93  const std::string& store_name, int64_t key,
94  const std::vector<uint8_t>& data);
96  DatabaseStatus DeleteData(const std::string& db_name,
97  const std::string& store_name, int64_t key);
99  DatabaseStatus FindData(const std::string& db_name,
100  const std::string& store_name, optional<int64_t> key,
101  bool ascending, int64_t* found_key);
102 
103  DatabaseStatus Commit();
104  DatabaseStatus Rollback();
105 
106  private:
107  DatabaseStatus GetStoreId(const std::string& db_name,
108  const std::string& store_name, int64_t* store_id);
109 
110  friend class SqliteConnection;
111  sqlite3* db_;
112 };
113 
119  public:
126  explicit SqliteConnection(const std::string& file_path);
127  ~SqliteConnection();
128 
130 
135  DatabaseStatus Init();
136 
142  DatabaseStatus BeginTransaction(SqliteTransaction* transaction);
143 
144 
155  DatabaseStatus Flush();
156 
157  private:
158  const std::string path_;
159  // Use an atomic variable so it can be accessed from different threads without
160  // a lock. Sqlite is internally thread-safe.
161  std::atomic<sqlite3*> db_;
162 };
163 
164 } // namespace idb
165 } // namespace js
166 } // namespace shaka
167 
168 #endif // SHAKA_EMBEDDED_JS_IDB_SQLITE_H
#define SHAKA_NON_COPYABLE_OR_MOVABLE_TYPE(Type)
Definition: macros.h:51
#define SHAKA_NON_COPYABLE_TYPE(Type)
Definition: macros.h:43