-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
60 lines (52 loc) · 1.43 KB
/
database.cpp
File metadata and controls
60 lines (52 loc) · 1.43 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
#include "database.h"
#include "godot_vfs.h"
void Database::_bind_methods()
{
ObjectTypeDB::bind_method(_MD("open", "database_file", "mode_flags"), &Database::open, DEFVAL(MODE_OPEN_DEFAULT));
ObjectTypeDB::bind_method(_MD("close"), &Database::close);
ObjectTypeDB::bind_method(_MD("opened"), &Database::opened);
ObjectTypeDB::bind_method(_MD("getErrorMessage"), &Database::getErrorMessage);
BIND_CONSTANT(MODE_OPEN_DEFAULT);
BIND_CONSTANT(MODE_OPEN_READONLY);
BIND_CONSTANT(MODE_OPEN_READWRITE);
BIND_CONSTANT(MODE_OPEN_CREATE);
}
Error Database::open(const String& database_file, Mode mode_flags)
{
if (!mpDatabase) {
int err = sqlite3_open_v2(
database_file.utf8().get_data(), // SQLite requires UTF-8 on windows
&mpDatabase,
mode_flags,
NULL); // TODO: Add in VFS support for godot filesystem
return err == SQLITE_OK ? OK : FAILED; // TODO: Better error return codes
}
else {
// Database connection already open
return ERR_ALREADY_IN_USE;
}
}
void Database::close()
{
if (mpDatabase) {
sqlite3_close_v2(mpDatabase);
mpDatabase = nullptr;
}
}
bool Database::opened() const
{
return mpDatabase;
}
String Database::getErrorMessage() const
{
return String(static_cast<const CharType*>(sqlite3_errmsg16(mpDatabase)));
}
Database::Database()
: mpDatabase(nullptr)
{
sqlite3_vfs_register(sqlite3_godot_vfs(), 1);
}
Database::~Database()
{
close();
}