forked from fogleman/Craft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.c
164 lines (149 loc) · 4.77 KB
/
db.c
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
#include <stdlib.h>
#include "db.h"
#include "sqlite3.h"
static int db_enabled = 1;
static sqlite3 *db;
static sqlite3_stmt *insert_block_stmt;
static sqlite3_stmt *select_block_stmt;
static sqlite3_stmt *update_chunk_stmt;
void db_enable() {
db_enabled = 1;
}
void db_disable() {
db_enabled = 0;
}
int get_db_enabled() {
return db_enabled;
}
int db_init() {
if (!db_enabled) {
return 0;
}
static const char *create_query =
"create table if not exists state ("
" x float not null,"
" y float not null,"
" z float not null,"
" rx float not null,"
" ry float not null"
");"
"create table if not exists block ("
" p int not null,"
" q int not null,"
" x int not null,"
" y int not null,"
" z int not null,"
" w int not null"
");"
"create index if not exists block_pq_idx on block(p, q);"
"create index if not exists block_xyz_idx on block (x, y, z);"
"create unique index if not exists block_pqxyz_idx on block (p, q, x, y, z);";
static const char *insert_block_query =
"insert or replace into block (p, q, x, y, z, w) "
"values (?, ?, ?, ?, ?, ?);";
static const char *select_block_query =
"select w from block where x = ? and y = ? and z = ?;";
static const char *update_chunk_query =
"select x, y, z, w from block where p = ? and q = ?;";
int rc;
rc = sqlite3_open(DB_NAME, &db);
if (rc) return rc;
rc = sqlite3_exec(db, create_query, NULL, NULL, NULL);
if (rc) return rc;
rc = sqlite3_prepare_v2(db, insert_block_query, -1, &insert_block_stmt, NULL);
if (rc) return rc;
rc = sqlite3_prepare_v2(db, select_block_query, -1, &select_block_stmt, NULL);
if (rc) return rc;
rc = sqlite3_prepare_v2(db, update_chunk_query, -1, &update_chunk_stmt, NULL);
if (rc) return rc;
return 0;
}
void db_close() {
if (!db_enabled) {
return;
}
sqlite3_finalize(insert_block_stmt);
sqlite3_finalize(select_block_stmt);
sqlite3_finalize(update_chunk_stmt);
sqlite3_close(db);
}
void db_save_state(float x, float y, float z, float rx, float ry) {
if (!db_enabled) {
return;
}
static const char *query =
"insert into state (x, y, z, rx, ry) values (?, ?, ?, ?, ?);";
sqlite3_stmt *stmt;
sqlite3_exec(db, "delete from state;", NULL, NULL, NULL);
sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
sqlite3_bind_double(stmt, 1, x);
sqlite3_bind_double(stmt, 2, y);
sqlite3_bind_double(stmt, 3, z);
sqlite3_bind_double(stmt, 4, rx);
sqlite3_bind_double(stmt, 5, ry);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
int db_load_state(float *x, float *y, float *z, float *rx, float *ry) {
if (!db_enabled) {
return 0;
}
static const char *query =
"select x, y, z, rx, ry from state;";
int result = 0;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
if (sqlite3_step(stmt) == SQLITE_ROW) {
*x = sqlite3_column_double(stmt, 0);
*y = sqlite3_column_double(stmt, 1);
*z = sqlite3_column_double(stmt, 2);
*rx = sqlite3_column_double(stmt, 3);
*ry = sqlite3_column_double(stmt, 4);
result = 1;
}
sqlite3_finalize(stmt);
return result;
}
void db_insert_block(int p, int q, int x, int y, int z, int w) {
if (!db_enabled) {
return;
}
sqlite3_reset(insert_block_stmt);
sqlite3_bind_int(insert_block_stmt, 1, p);
sqlite3_bind_int(insert_block_stmt, 2, q);
sqlite3_bind_int(insert_block_stmt, 3, x);
sqlite3_bind_int(insert_block_stmt, 4, y);
sqlite3_bind_int(insert_block_stmt, 5, z);
sqlite3_bind_int(insert_block_stmt, 6, w);
sqlite3_step(insert_block_stmt);
}
int db_select_block(int x, int y, int z) {
if (!db_enabled) {
return -1;
}
sqlite3_reset(select_block_stmt);
sqlite3_bind_int(select_block_stmt, 1, x);
sqlite3_bind_int(select_block_stmt, 2, y);
sqlite3_bind_int(select_block_stmt, 3, z);
if (sqlite3_step(select_block_stmt) == SQLITE_ROW) {
return sqlite3_column_int(select_block_stmt, 0);
}
else {
return -1;
}
}
void db_update_chunk(Map *map, int p, int q) {
if (!db_enabled) {
return;
}
sqlite3_reset(update_chunk_stmt);
sqlite3_bind_int(update_chunk_stmt, 1, p);
sqlite3_bind_int(update_chunk_stmt, 2, q);
while (sqlite3_step(update_chunk_stmt) == SQLITE_ROW) {
int x = sqlite3_column_int(update_chunk_stmt, 0);
int y = sqlite3_column_int(update_chunk_stmt, 1);
int z = sqlite3_column_int(update_chunk_stmt, 2);
int w = sqlite3_column_int(update_chunk_stmt, 3);
map_set(map, x, y, z, w);
}
}