-
Notifications
You must be signed in to change notification settings - Fork 0
/
stardust_db.h
355 lines (306 loc) · 11.2 KB
/
stardust_db.h
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#ifndef STARDUST_DB_H
#define STARDUST_DB_H
/* Warning, this file is generated automatically. Do not modify. */
#include <stdint.h>
/**
* Returned on success.
*/
#define STARDUST_DB_OK 0
/**
* Returned if the provided database path is not UTF-8.
*/
#define STARDUST_DB_INVALID_PATH_UTF_8 1
/**
* Returned if the database cannot be opened at the specified location.
*/
#define STARDUST_DB_INVALID_PATH_LOCATION 2
/**
* Returned if the RowSet was not initialised.
*/
#define STARDUST_DB_NULL_ROW_SET 3
/**
* Returned if the database was not opened.
*/
#define STARDUST_DB_NULL_DB 4
/**
* Returned if the query was not valid UTF-8.
*/
#define STARDUST_DB_INVALID_QUERY_UTF_8 5
/**
* Returned if the query returned no result.
*/
#define STARDUST_DB_NO_RESULT 6
/**
* Returned if the query resulted in an execution error.
*/
#define STARDUST_DB_EXECUTION_ERROR 7
/**
* Returned if the current row is past the end of the RowSet.
*/
#define STARDUST_DB_END 8
/**
* Returned if the column with the specified key could not be found.
*/
#define STARDUST_DB_NO_COLUMN 9
/**
* Returned if the provided string buffer is too small for the value.
*/
#define STARDUST_DB_BUFFER_TOO_SMALL 10
/**
* Returned if the specified value is the wrong type.
*/
#define STARDUST_DB_VALUE_WRONG_TYPE 11
/**
* Returned if the specified value is null.
*/
#define STARDUST_DB_VALUE_NULL 12
/**
* Returned if there was an error creating the temporary database.
*/
#define STARDUST_DB_TEMP_DB_ERROR 13
/**
* Contains a connection to a database.
*/
typedef struct Database Database;
/**
* Stores a list of rows returned by a query.
*/
typedef struct Relation Relation;
/**
* A `Database` that deletes its data when dropped.
*/
typedef struct TemporaryDatabase TemporaryDatabase;
/**
* Stores a database connection for the C interface.
*/
typedef enum Db_Tag {
Ordinary,
Temporary,
} Db_Tag;
typedef struct Db {
Db_Tag tag;
union {
struct {
struct Database *ordinary;
};
struct {
struct TemporaryDatabase *temporary;
};
};
} Db;
/**
* Stores a list of rows returned from a query execution for the C interface.
*/
typedef struct RowSet {
struct Relation *relation;
uintptr_t current_row;
} RowSet;
typedef int64_t IntegerStorage;
/**
* Used to zero-initialise the RowSet before using as an argument in `execute_query`.
*/
#define ROW_SET_INIT (RowSet){ .relation = (Relation*)0, .current_row = 0 }
/**
* Opens the database at the specified path. Returns `STARDUST_DB_OK` on success.
* # Safety
* `path` must be a null-terminated string.
* `db` must point to a valid piece of memory.
*/
int open_database(const char *path, struct Db *db);
/**
* Opens a temporary database. Returns `STARDUST_DB_OK` on success.
* # Safety
* `db` must point to a valid piece of memory.
*/
int temp_db(struct Db *db);
/**
* Closes the database. This function should always succeed.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query` or `INIT_ROW_SET`.
*/
void close_db(struct Db *db);
/**
* Frees the memory from the `RowSet`.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query` or `INIT_ROW_SET`.
*/
void close_row_set(struct RowSet *row_set);
/**
* Executes the query in `query` and places the result in `row_set`.
* Errors will be placed in the buffer at `err_buf`, which must be no smaller than `err_buff_len`.
* # Safety
* `db` must point to a Db initialised by `open_database` or `temp_db`.
* `query` must be a null-terminated string.
* `row_set` must point to a RowSet initialised by `ROW_SET_INIT`, or a previous invocation of `execute_query`.
* `err_buff` must point to a valid piece of memory, no shorter than `err_buff_len`.
*/
int execute_query(struct Db *db,
const char *query,
struct RowSet *row_set,
char *err_buff,
uintptr_t err_buff_len);
/**
* Move to the next row in the `RowSet`. Returns `STARDUST_DB_END` if the row is past the end of the `RowSet`.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
*/
int next_row(struct RowSet *row_set);
/**
* Set the current row of the `RowSet` to the specified value. Returns `STARDUST_DB_END` if the row is past the end of the `RowSet`.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
*/
int set_row(struct RowSet *row_set,
uintptr_t row);
/**
* Sets the value in `is_end` to 1 if the current row is past the end of the `RowSet`, otherwise the value is set to 0.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `is_end` must point to a valid piece of memory.
*/
int is_end(const struct RowSet *row_set,
int *is_end);
/**
* Sets the value in `num_columns` to be the number of columns in the `RowSet`.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `num_columns` must point to a valid piece of memory.
*/
int num_columns(const struct RowSet *row_set, uintptr_t *num_columns);
/**
* Sets the value in `num_rows` to be the number of rows in the `RowSet`.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `num_rows` must point to a valid piece of memory.
*/
int num_rows(const struct RowSet *row_set, uintptr_t *num_rows);
/**
* Sets the value in `is_null` to 1 if the value at the specified column is Null, otherwise 0.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `is_null` must point to a valid piece of memory.
*/
int is_null_index(const struct RowSet *row_set, uintptr_t column, int *is_null);
/**
* Sets the value in `is_string` to 1 if the value at the specified column is a string, otherwise 0.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `is_string` must point to a valid piece of memory.
*/
int is_string_index(const struct RowSet *row_set, uintptr_t column, int *is_string);
/**
* Sets the value in `is_int` to 1 if the value at the specified column is an integer, otherwise 0.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `is_int` must point to a valid piece of memory.
*/
int is_int_index(const struct RowSet *row_set, uintptr_t column, int *is_int);
/**
* If the value at the specified column is a string, copy the value to the buffer, otherwise a type error is returned.
* `STARDUST_DB_BUFFER_TOO_SMALL` is returned if the string buffer is too small.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `string_buffer` must point to a valid piece of memory, no shorter than `buffer_len`.
*/
int get_string_index(const struct RowSet *row_set,
uintptr_t column,
char *string_buffer,
uintptr_t buffer_len);
/**
* If the value at the specified column is an integer, copy the value to the buffer, otherwise a type error is returned.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `int_buffer` must point to a valid piece of memory.
*/
int get_int_index(const struct RowSet *row_set,
uintptr_t column,
IntegerStorage *int_buffer);
/**
* Cast the value to a string and copy the value to the buffer. An error will be returned if the value is null.
* `STARDUST_DB_BUFFER_TOO_SMALL` is returned if the string buffer is too small.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `string_buffer` must point to a valid piece of memory, no shorter than `buffer_len`.
*/
int get_string_index_cast(const struct RowSet *row_set,
uintptr_t column,
char *string_buffer,
uintptr_t buffer_len);
/**
* Cast the value to an integer and copy the value to the buffer. An error will be returned if the value is null.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `int_buffer` must point to a valid piece of memory.
*/
int get_int_index_cast(const struct RowSet *row_set,
uintptr_t column,
IntegerStorage *int_buffer);
/**
* Sets the value in `is_null` to 1 if the value at the specified column is null, otherwise 0.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `column` must be a null-terminated string.
* `is_string` must point to a valid piece of memory.
*/
int is_null_named(const struct RowSet *row_set, const char *column, int *is_null);
/**
* Sets the value in `is_string` to 1 if the value at the specified column is a string, otherwise 0.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `column` must be a null-terminated string.
* `is_string` must point to a valid piece of memory.
*/
int is_string_named(const struct RowSet *row_set, const char *column, int *is_string);
/**
* Sets the value in `is_int` to 1 if the value at the specified column is an integer, otherwise 0.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `column` must be a null-terminated string.
* `is_int` must point to a valid piece of memory.
*/
int is_int_named(const struct RowSet *row_set, const char *column, int *is_int);
/**
* If the value at the specified column is a string, copy the value to the buffer, otherwise a type error is returned.
* `STARDUST_DB_BUFFER_TOO_SMALL` is returned if the string buffer is too small.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `column` must be a null-terminated string.
* `string_buffer` must point to a valid piece of memory, no smaller than `buffer_len`.
*/
int get_string_named(const struct RowSet *row_set,
const char *column,
char *string_buffer,
uintptr_t buffer_len);
/**
* If the value at the specified column is an integer, copy the value to the buffer, otherwise a type error is returned.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `column` must be a null-terminated string.
* `int_buffer` must point to a valid piece of memory.
*/
int get_int_named(const struct RowSet *row_set,
const char *column,
IntegerStorage *int_buffer);
/**
* Cast the value to a string and copy the value to the buffer. An error will be returned if the value is null.
* `STARDUST_DB_BUFFER_TOO_SMALL` is returned if the string buffer is too small.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `column` must be a null-terminated string.
* `string_buffer` must point to a valid piece of memory, no smaller than `buffer_len`.
*/
int get_string_named_cast(const struct RowSet *row_set,
const char *column,
char *string_buffer,
uintptr_t buffer_len);
/**
* Cast the value to an integer and copy the value to the buffer. An error will be returned if the value is null.
* # Safety
* `row_set` must point to a RowSet initialised by `execute_query`.
* `column` must be a null-terminated string.
* `int_buffer` must point to a valid piece of memory.
*/
int get_int_named_cast(const struct RowSet *row_set,
const char *column,
IntegerStorage *int_buffer);
#endif /* STARDUST_DB_H */