-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.c
150 lines (127 loc) · 3.5 KB
/
database.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
#include <sqlite3.h>
#include <stdint.h>
#include <stdio.h>
#include "database.h"
static const char *DATABASE_FILE = 0;
int initialize_database(const char *database_file)
{
DATABASE_FILE = database_file;
sqlite3 *db;
int rc = sqlite3_open(database_file, &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return -1;
}
const char *sql =
// guilds table
"CREATE TABLE IF NOT EXISTS guilds ("
"id INTEGER PRIMARY KEY, voice INTEGER, "
"reject_voice INTEGER, logs_channel INTEGER);"
// temp_voices table
"CREATE TABLE IF NOT EXISTS temp_voices ("
"id INTEGER PRIMARY KEY, guild_id INTEGER, owner_id INTEGER);";
char *errmsg = 0;
rc = sqlite3_exec(db, sql, 0, 0, &errmsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return -1;
}
sqlite3_close(db);
return 0;
}
int db_guild_insert(guild_data data)
{
sqlite3 *db;
int rc = sqlite3_open(DATABASE_FILE, &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return -1;
}
const char *sql = "INSERT INTO guilds (id, voice, reject_voice, "
"logs_channel) VALUES (?, ?, ?, ?);";
sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return -1;
}
sqlite3_bind_int64(stmt, 1, data.id);
sqlite3_bind_int64(stmt, 2, data.voice);
sqlite3_bind_int64(stmt, 3, data.reject_voice);
sqlite3_bind_int64(stmt, 4, data.logs_channel);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
return -1;
}
return 0;
}
int db_guild_read(uint64_t guild_id, guild_data *dest)
{
sqlite3 *db;
int rc = sqlite3_open(DATABASE_FILE, &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return -1;
}
const char *sql = "SELECT id, voice, reject_voice, logs_channel FROM "
"guilds WHERE id = ?";
sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return -1;
}
sqlite3_bind_int64(stmt, 1, guild_id);
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
dest->id = guild_id;
dest->voice = sqlite3_column_int64(stmt, 1);
dest->reject_voice = sqlite3_column_int64(stmt, 2);
dest->logs_channel = sqlite3_column_int64(stmt, 3);
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0; // row found
} else if (rc == SQLITE_DONE) {
sqlite3_finalize(stmt);
sqlite3_close(db);
return 1; // row not found
} else {
fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
sqlite3_close(db);
return -1; // err
}
}
int db_guild_delete(uint64_t guild_id)
{
sqlite3 *db;
int rc = sqlite3_open(DATABASE_FILE, &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return -1;
}
const char *sql = "DELETE FROM guilds WHERE id = ?";
sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return -1;
}
sqlite3_bind_int64(stmt, 1, guild_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
if (rc != SQLITE_DONE) {
fprintf(stderr, "Failed to delete: %s\n", sqlite3_errmsg(db));
return -1;
}
return 0;
}