-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cc
More file actions
146 lines (127 loc) · 5.59 KB
/
Copy pathdatabase.cc
File metadata and controls
146 lines (127 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// MIT License
//
// Copyright (c) 2026 Ioannis Kaliakatsos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "database.h"
#include <memory>
#include <mutex>
#include <stdexcept>
#include "internal/sqlite3.h"
#include "queries.h"
namespace sqlite_reflection {
std::shared_ptr<Database> Database::instance_ = nullptr;
std::weak_ptr<Database> Database::retired_;
std::mutex Database::instance_mutex_;
const ReflectionRegister& GetReflectionRegister() {
return *GetReflectionRegisterInstance();
}
void Database::Initialize(const std::string& path) {
std::lock_guard<std::mutex> lock(instance_mutex_);
if (instance_ != nullptr) {
throw std::invalid_argument("Database has already been initialized");
}
if (!retired_.expired()) {
// A previous database is still kept alive by outstanding Instance() handles.
// Creating a new one now would leave two live databases/connections that do not
// share the same db_mutex_; refuse until those handles are released.
throw std::runtime_error(
"Database cannot be reinitialized while handles to the previous database are still in use");
}
const auto effective_path = !path.empty() ? path : ":memory:";
instance_ = std::shared_ptr<Database>(new Database(effective_path.data()));
}
void Database::Finalize() {
std::lock_guard<std::mutex> lock(instance_mutex_);
if (instance_ == nullptr) {
// Nothing to retire. Crucially, do not overwrite retired_ here: a previous Finalize()
// may have retired an instance that outstanding handles still keep alive, and that
// guard must remain in effect until those handles are released.
return;
}
// Drop the singleton's own reference. The connection is closed by ~Database once the
// last outstanding Instance() handle is released, so an in-flight operation on another
// thread keeps the database alive until it finishes. Track the retiring instance so a
// subsequent Initialize() is rejected until those handles are gone.
retired_ = instance_;
instance_.reset();
}
Database::~Database() {
if (db_ != nullptr) {
sqlite3_close(db_);
}
}
Database::Database(const char* path) : db_(nullptr) {
// Open in serialized threading mode so the shared connection is safe to use from
// multiple threads; access is additionally serialized through db_mutex_.
const int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
if (sqlite3_open_v2(path, &db_, flags, nullptr)) {
throw std::invalid_argument("Database could not be initialized");
}
auto& reg = GetReflectionRegister();
for (const auto& contents : reg.records) {
const auto& record = contents.second;
CreateTableQuery query(db_, record);
query.Execute();
}
}
std::shared_ptr<const Database> Database::Instance() {
std::lock_guard<std::mutex> lock(instance_mutex_);
if (instance_ == nullptr) {
throw std::runtime_error("Database has not been initialized; call Database::Initialize() first");
}
return instance_;
}
FetchQueryResults Database::Fetch(const Reflection& record, const QueryPredicateBase* predicate) const {
std::lock_guard<std::mutex> lock(db_mutex_);
FetchRecordsQuery query(db_, record, predicate);
return query.GetResults();
}
const Reflection& Database::GetRecord(const std::string& type_id) {
return GetReflectionRegister().records.at(type_id);
}
void Database::Save(void* p, const Reflection& record) const {
std::lock_guard<std::mutex> lock(db_mutex_);
InsertQuery query(db_, record, p);
query.Execute();
}
int64_t Database::SaveAutoIncrement(void* p, const Reflection& record) const {
// The lock spans both the insert and the rowid read so that, on the shared connection,
// sqlite3_last_insert_rowid() reflects this insert and not one from another thread.
std::lock_guard<std::mutex> lock(db_mutex_);
InsertQuery query(db_, record, p, true);
query.Execute();
return sqlite3_last_insert_rowid(db_);
}
void Database::Update(void* p, const Reflection& record) const {
std::lock_guard<std::mutex> lock(db_mutex_);
UpdateQuery query(db_, record, p);
query.Execute();
}
void Database::Delete(const Reflection& record, const QueryPredicateBase* predicate) const {
std::lock_guard<std::mutex> lock(db_mutex_);
DeleteQuery query(db_, record, predicate);
query.Execute();
}
void Database::UnsafeSql(const std::string& raw_sql_query) const {
std::lock_guard<std::mutex> lock(db_mutex_);
SqlQuery sql(db_, raw_sql_query);
sql.Execute();
}
} // namespace sqlite_reflection