-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgdsqlite.cpp
191 lines (164 loc) · 5.11 KB
/
gdsqlite.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* gdsqlite.cpp */
#include "gdsqlite.h"
#include "globals.h"
#include "os/os.h"
SQLite::SQLite() {
}
int SQLite::open(String path) {
if (path.begins_with("res://")) {
if (GlobalConfig::get_singleton()) {
String resource_path = GlobalConfig::get_singleton()->get_resource_path();
if (resource_path != "") path = path.replace("res:/",resource_path);
else path = path.replace("res://", "");
}
}
else if (path.begins_with("user://")) {
String data_dir=OS::get_singleton()->get_data_dir();
if (data_dir != "") path = path.replace("user:/",data_dir);
else path = path.replace("user://", "");
}
return sqlite3_open(path.utf8().get_data(), &db);
}
void SQLite::prepare(String query) {
sqlite3_prepare_v2(db, query.utf8().get_data(), -1, &stmt, NULL);
}
int SQLite::step() {
return sqlite3_step(stmt);
}
int SQLite::step_assoc() {
int ret = sqlite3_step(stmt);
if (_row_names.size() == 0) {
for(unsigned int i = 0; i < sqlite3_column_count(stmt); ++i)
_row_names[sqlite3_column_name(stmt,i)] = i;
}
return ret;
}
Array SQLite::fetch_assoc() {
Array result;
while (1) {
int ret = sqlite3_step(stmt);
if (ret == SQLITE_ROW) {
Dictionary row;
for(unsigned int i = 0; i < sqlite3_column_count(stmt); ++i) {
int type = sqlite3_column_type(stmt, i);
switch (type) {
case SQLITE_INTEGER: row[sqlite3_column_name(stmt,i)] = sqlite3_column_int(stmt, i); break;
case SQLITE_FLOAT: row[sqlite3_column_name(stmt,i)] = sqlite3_column_double(stmt, i); break;
case SQLITE_TEXT: row[sqlite3_column_name(stmt,i)] = String::utf8((char *)sqlite3_column_text(stmt, i)); break;
default: break;
}
}
result.push_back(row);
}
else if (ret == SQLITE_DONE) {
break;
}
else {
break;
}
}
return result;
}
Array SQLite::fetch_one() {
Array result = fetch_assoc();
if (result.size() <= 0) { return NULL; }
return result[0];
}
Array SQLite::fetch_array(String query) {
prepare(query);
Array result = fetch_assoc();
finalize();
return result;
}
int SQLite::query(String query) {
prepare(query);
int ret = step();
finalize();
return ret;
}
int SQLite::get_data_count() {
return sqlite3_data_count(stmt);
}
int SQLite::get_column_count() {
return sqlite3_column_count(stmt);
}
int SQLite::get_column_int(int col) {
return sqlite3_column_int(stmt, col);
}
double SQLite::get_column_double(int col) {
return sqlite3_column_double(stmt, col);
}
String SQLite::get_column_text(int col) {
return String::utf8((char *)sqlite3_column_text(stmt, col));
}
int SQLite::get_column_int_assoc(String col) {
return sqlite3_column_int(stmt, _row_names[col]);
}
double SQLite::get_column_double_assoc(String col) {
return sqlite3_column_double(stmt, _row_names[col]);
}
String SQLite::get_column_text_assoc(String col) {
return String::utf8((char *)sqlite3_column_text(stmt, _row_names[col]));
}
void SQLite::finalize() {
sqlite3_finalize(stmt);
_row_names.clear();
}
String SQLite::get_errormsg() {
return sqlite3_errmsg(db);
}
void SQLite::close() {
sqlite3_close(db);
}
void SQLite::_bind_methods() {
ClassDB::bind_method("open", &SQLite::open);
ClassDB::bind_method("prepare", &SQLite::prepare);
ClassDB::bind_method("step", &SQLite::step);
ClassDB::bind_method("step_assoc", &SQLite::step_assoc);
ClassDB::bind_method("fetch_assoc", &SQLite::fetch_assoc);
ClassDB::bind_method("fetch_one", &SQLite::fetch_one);
ClassDB::bind_method("fetch_array", &SQLite::fetch_array);
ClassDB::bind_method("query", &SQLite::query);
ClassDB::bind_method("get_data_count", &SQLite::get_data_count);
ClassDB::bind_method("get_column_count", &SQLite::get_column_count);
ClassDB::bind_method("get_column_int", &SQLite::get_column_int);
ClassDB::bind_method("get_column_double", &SQLite::get_column_double);
ClassDB::bind_method("get_column_text", &SQLite::get_column_text);
ClassDB::bind_method("finalize", &SQLite::finalize);
ClassDB::bind_method("get_errormsg", &SQLite::get_errormsg);
ClassDB::bind_method("close", &SQLite::close);
ClassDB::bind_method("get_column_int_assoc", &SQLite::get_column_int_assoc);
ClassDB::bind_method("get_column_double_assoc", &SQLite::get_column_double_assoc);
ClassDB::bind_method("get_column_text_assoc", &SQLite::get_column_text_assoc);
BIND_CONSTANT(SQLITE_OK);
BIND_CONSTANT(SQLITE_ERROR);
BIND_CONSTANT(SQLITE_INTERNAL);
BIND_CONSTANT(SQLITE_PERM);
BIND_CONSTANT(SQLITE_ABORT);
BIND_CONSTANT(SQLITE_BUSY);
BIND_CONSTANT(SQLITE_LOCKED);
BIND_CONSTANT(SQLITE_NOMEM);
BIND_CONSTANT(SQLITE_READONLY);
BIND_CONSTANT(SQLITE_INTERRUPT);
BIND_CONSTANT(SQLITE_IOERR);
BIND_CONSTANT(SQLITE_CORRUPT);
BIND_CONSTANT(SQLITE_NOTFOUND);
BIND_CONSTANT(SQLITE_FULL);
BIND_CONSTANT(SQLITE_CANTOPEN);
BIND_CONSTANT(SQLITE_PROTOCOL);
BIND_CONSTANT(SQLITE_EMPTY);
BIND_CONSTANT(SQLITE_SCHEMA);
BIND_CONSTANT(SQLITE_TOOBIG);
BIND_CONSTANT(SQLITE_CONSTRAINT);
BIND_CONSTANT(SQLITE_MISMATCH);
BIND_CONSTANT(SQLITE_MISUSE);
BIND_CONSTANT(SQLITE_NOLFS);
BIND_CONSTANT(SQLITE_AUTH);
BIND_CONSTANT(SQLITE_FORMAT);
BIND_CONSTANT(SQLITE_RANGE);
BIND_CONSTANT(SQLITE_NOTADB);
BIND_CONSTANT(SQLITE_NOTICE);
BIND_CONSTANT(SQLITE_WARNING);
BIND_CONSTANT(SQLITE_ROW);
BIND_CONSTANT(SQLITE_DONE);
}