-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.c
241 lines (174 loc) · 6.02 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
MIT License
Copyright (c) 2021 Preston Smith
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
(including the next paragraph) 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 <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include "sidgo.h"
/* Disconnect */
void db_disconnect(PGconn *conn, PGresult *res){
printf("Disconnecting from Database.");
PQclear(res);
PQfinish(conn);
exit(1);
}
/* Get latest row id from db */
int db_row_id(void){
PGconn *conn;
conn = PQconnectdb("");
PGresult *res;
int retRowId;
res = PQexec(conn, "SELECT MAX(id) FROM sid");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
printf("No data retrieved\n");
PQclear(res);
db_disconnect(conn, res);
}
retRowId = atoi(PQgetvalue(res,0,0));
printf("ROW ID: %d\n", retRowId);
PQclear(res);
PQfinish(conn);
return retRowId;
}
/* Checks if the sid table in the database exists, if it doesn't, it create it.*/
int db_table_exist(void){
PGconn *conn;
conn = PQconnectdb("");
PGresult *res;
/* Buffering SQL commands */
char buffer[128];
unsigned long tAccount = snprintf(buffer, sizeof(buffer), "CREATE TABLE IF NOT EXISTS sid"\
"(id BIGSERIAL, flake NUMERIC, serial BIGINT);");
printf("Running accountability for primary table.\n");
if (tAccount > sizeof(buffer)) {
printf("Error, buffer too small.");
}
res = PQexec(conn, buffer);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printf("Table accountability failed.\n");
printf("%s\n", PQresultErrorMessage(res));
printf("%s\n", PQerrorMessage(conn));
PQclear(res);
db_disconnect(conn, res);
}
PQclear(res);
PQfinish(conn);
printf("Continuing.\n");
return 0;
}
/* seed transaction */
int db_init_seed(void){
PGconn *conn;
conn = PQconnectdb("");
char buffer[128];
unsigned long seedFlakeId = gen_seed_id();
PGresult *res;
unsigned long sFlake = snprintf(buffer, sizeof(buffer), "INSERT INTO sid (flake, serial) VALUES (%lu, %d)", seedFlakeId, 1000);
if (sFlake > sizeof(buffer)) {
printf("Error, buffer too small.\n");
}
res = PQexec(conn, buffer);
printf("Seeding database...\n");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printf("INSERT command failed\n");
printf("%s\n", PQresultErrorMessage(res));
printf("%s\n", PQerrorMessage(conn));
PQclear(res);
db_disconnect(conn, res);
}
PQclear(res);
PQfinish(conn);
printf("Done.\n");
return 0;
}
/* Snowflake ID transaction */
int db_transact_flake(void){
PGconn *conn;
conn = PQconnectdb("");
char buffer[70];
unsigned long flakeId = gen_id();
PGresult *res;
unsigned long tFlake = snprintf(buffer, sizeof(buffer), "INSERT INTO sid (flake, serial) VALUES (%lu, %d)", flakeId, 1000);
if (tFlake > sizeof(buffer)) {
printf("Error, buffer too small.");
}
res = PQexec(conn, buffer);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printf("INSERT command failed\n");
printf("%s\n", PQresultErrorMessage(res));
printf("%s\n", PQerrorMessage(conn));
PQclear(res);
db_disconnect(conn, res);
}
PQclear(res);
PQfinish(conn);
return 0;
}
/* Database connection status functions. */
int db_connections(void){
PGconn *conn;
conn = PQconnectdb("");
switch(PQstatus(conn)){
case CONNECTION_STARTED:
printf("Waiting for connection to be made.\n");
break;
case CONNECTION_OK:
printf("Connect OK.\n");
break;
case CONNECTION_BAD:
printf("Connection failed.\n");
PQerrorMessage(conn);
PQfinish(conn);
exit(1);
break;
case CONNECTION_NEEDED:
printf("Internal state: connect() needed.\n");
break;
case CONNECTION_CHECK_WRITABLE:
printf("Checking if session is read-write.\n");
break;
case CONNECTION_CONSUME:
printf("Consuming any extra messages.\n");
break;
case CONNECTION_GSS_STARTUP:
printf("Negotiating GSSAPI.\n");
break;
case CONNECTION_CHECK_TARGET:
printf("Checking target server properties.\n");
break;
case CONNECTION_MADE:
printf("Connection OK; waiting to send.\n");
break;
case CONNECTION_AWAITING_RESPONSE:
printf("Waiting for a response from the server.\n");
break;
case CONNECTION_AUTH_OK:
printf("Received authentication; waiting for backend start-up to finish.\n");
break;
case CONNECTION_SSL_STARTUP:
printf("Negotiating SSL encryption.\n");
break;
case CONNECTION_SETENV:
printf("Negotiating environment-driven parameter settings.\n");
break;
}
return 0;
}