-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspringql.h
451 lines (418 loc) · 11.6 KB
/
springql.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// This file is part of https://github.com/SpringQL/SpringQL-client-c which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details.
#ifndef _SPRINGQL_H_
#define _SPRINGQL_H_
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* Errno (error number) to be returned erroneous functions.
*/
typedef enum SpringErrno {
Ok = 0,
/**
* Panic
*/
Unknown = -1,
ForeignIo = -2,
ForeignSourceTimeout = -3,
InputTimeout = -4,
SpringQlCoreIo = -5,
ThreadPoisoned = -6,
InvalidOption = -7,
InvalidFormat = -8,
Unavailable = -9,
Sql = -10,
InvalidConfig = -11,
Null = -12,
Time = -13,
/**
* Insufficient buffer size
*/
CInsufficient = -126,
/**
* Invalid null pointer
*/
CNull = -127,
} SpringErrno;
/**
* Configuration.
*/
typedef struct SpringConfig SpringConfig;
/**
* Pipeline (dataflow definition) in SpringQL.
*/
typedef struct SpringPipeline SpringPipeline;
/**
* Row object to pop from an in memory queue.
*/
typedef struct SpringSinkRow SpringSinkRow;
/**
* Row object to push into an in memory queue.
*/
typedef struct SpringSourceRow SpringSourceRow;
/**
* Builder of SpringSourceRow
*/
typedef struct SpringSourceRowBuilder SpringSourceRowBuilder;
/**
* Returns default configuration.
*
* Returned value is not modifiable (it is just a void pointer).
* If you would like to change the default configuration, use `spring_config_toml()` instead.
*/
struct SpringConfig *spring_config_default(void);
/**
* Configuration by TOML format string.
*
* Returned value is not modifiable (it is just a void pointer).
*
* # Parameters
*
* - `overwrite_config_toml`: TOML format configuration to overwrite default.
* See <https://springql.github.io/deployment/configuration> for TOML format and configuration values.
*
* # Panics
*
* Currently, the process aborts when:
*
* - `overwrite_config_toml` includes invalid key and/or value.
* - `overwrite_config_toml` is not valid as TOML.
*/
struct SpringConfig *spring_config_toml(const char *overwrite_config_toml);
/**
* Frees heap occupied by a `SpringConfig`.
*
* # Returns
*
* - `Ok`: on success.
* - `CNull`: `config` is a NULL pointer.
*/
enum SpringErrno spring_config_close(struct SpringConfig *config);
/**
* Creates and open an in-process stream pipeline.
*
* # Returns
*
* - non-NULL: on success
* - NULL: on failure. Check spring_last_err() for details.
*
* # Errors
*
* No errors are expected currently.
*/
struct SpringPipeline *spring_open(const struct SpringConfig *config);
/**
* Frees heap occupied by a `SpringPipeline`.
*
* # Returns
*
* - `Ok`: on success.
* - `CNull`: `pipeline` is a NULL pointer.
*/
enum SpringErrno spring_close(struct SpringPipeline *pipeline);
/**
* Execute commands (DDL) to modify the pipeline.
*
* # Returns
*
* - `Ok`: on success.
* - `Sql`:
* - Invalid SQL syntax.
* - Refers to undefined objects (streams, pumps, etc)
* - Other semantic errors.
* - `InvalidOption`:
* - `OPTIONS` in `CREATE` statement includes invalid key or value.
*/
enum SpringErrno spring_command(const struct SpringPipeline *pipeline, const char *sql);
/**
* Pop a row from an in memory queue. This is a blocking function.
*
* Do not call this function from threads.
* If you need to pop from multiple in-memory queues using threads, use `spring_pop_non_blocking()`.
* See: <https://github.com/SpringQL/SpringQL/issues/125>
*
* # Returns
*
* - non-NULL: on success
* - NULL: on failure. Check spring_last_err() for details.
*
* # Errors
*
* - `Unavailable`: queue named `queue` does not exist.
*/
struct SpringSinkRow *spring_pop(const struct SpringPipeline *pipeline, const char *queue);
/**
* Pop a row from an in memory queue. This is a non-blocking function.
*
* # Returns
*
* - non-NULL: Successfully get a row.
* - NULL: Error occurred if `is_err` is true (check spring_last_err() for details). Otherwise, any row is not in the queue.
*
* # Errors
*
* - `Unavailable`: queue named `queue` does not exist.
*/
struct SpringSinkRow *spring_pop_non_blocking(const struct SpringPipeline *pipeline,
const char *queue,
bool *is_err);
/**
* Push a row into an in memory queue. This is a non-blocking function.
*
* `row` is freed internally.
*
* # Returns
*
* - `Ok`: on success.
* - `Unavailable`: queue named `queue` does not exist.
*/
enum SpringErrno spring_push(const struct SpringPipeline *pipeline,
const char *queue,
struct SpringSourceRow *row);
/**
* Create a source row from JSON string
*
* # Returns
*
* - non-NULL: Successfully created a row.
* - NULL: Error occurred.
*
* # Errors
*
* - `InvalidFormat`: JSON string is invalid.
*/
struct SpringSourceRow *spring_source_row_from_json(const char *json);
/**
* Start creating a source row using a builder.
*
* # Returns
*
* Pointer to the builder
*/
struct SpringSourceRowBuilder *spring_source_row_builder(void);
/**
* Add a BLOB column to the builder and return the new one.
*
* `builder` is freed internally.
*
* # Parameters
*
* - `builder`: Pointer to the builder created via spring_source_row_builder().
* - `column_name`: Column name to add.
* - `v`: BLOB value to add. The byte sequence is copied internally.
* - `v_len`: `v`'s length.
*
* # Returns
*
* - non-NULL: Successfully created a row.
* - NULL: Error occurred.
*
* # Errors
*
* - `Sql`: `column_name` is already added to the builder.
*/
struct SpringSourceRowBuilder *spring_source_row_add_column_blob(struct SpringSourceRowBuilder *builder,
const char *column_name,
const void *v,
int v_len);
/**
* Finish creating a source row using a builder.
*
* The heap space for the `builder` is internally freed.
*
* # Returns
*
* SpringSourceRow
*/
struct SpringSourceRow *spring_source_row_build(struct SpringSourceRowBuilder *builder);
/**
* Frees heap occupied by a `SpringSinkRow`.
*
* # Returns
*
* - `Ok`: on success.
* - `CNull`: `pipeline` is a NULL pointer.
*/
enum SpringErrno spring_sink_row_close(struct SpringSinkRow *row);
/**
* Get a 2-byte integer column.
*
* # Parameters
*
* - `row`: A `SpringRow` pointer to get a column value from.
* - `i_col`: The column index to get a value from.
* - `out`: A pointer to a buffer to store the column value.
*
* # Returns
*
* - `Ok`: On success.
* - `Unavailable`:
* - Column pointed by `i_col` is already fetched.
* - `i_col` is out of range.
* - `CNull`: Column value is NULL.
*/
enum SpringErrno spring_column_short(const struct SpringSinkRow *row, uint16_t i_col, short *out);
/**
* Get a 4-byte integer column.
*
* # Parameters
*
* - `row`: A `SpringRow` pointer to get a column value from.
* - `i_col`: The column index to get a value from.
* - `out`: A pointer to a buffer to store the column value.
*
* # Returns
*
* - `Ok`: On success.
* - `Unavailable`:
* - Column pointed by `i_col` is already fetched.
* - `i_col` is out of range.
* - `CNull`: Column value is NULL.
*/
enum SpringErrno spring_column_int(const struct SpringSinkRow *row, uint16_t i_col, int *out);
/**
* Get an 8-byte integer column.
*
* # Parameters
*
* - `row`: A `SpringRow` pointer to get a column value from.
* - `i_col`: The column index to get a value from.
* - `out`: A pointer to a buffer to store the column value.
*
* # Returns
*
* - `Ok`: On success.
* - `Unavailable`:
* - Column pointed by `i_col` is already fetched.
* - `i_col` is out of range.
* - `CNull`: Column value is NULL.
*/
enum SpringErrno spring_column_long(const struct SpringSinkRow *row, uint16_t i_col, long *out);
/**
* Get a 4-byte unsigned integer column.
*
* # Parameters
*
* - `row`: A `SpringRow` pointer to get a column value from.
* - `i_col`: The column index to get a value from.
* - `out`: A pointer to a buffer to store the column value.
*
* # Returns
*
* - `Ok`: On success.
* - `Unavailable`:
* - Column pointed by `i_col` is already fetched.
* - `i_col` is out of range.
* - `CNull`: Column value is NULL.
*/
enum SpringErrno spring_column_unsigned_int(const struct SpringSinkRow *row,
uint16_t i_col,
unsigned int *out);
/**
* Get a text column.
*
* # Parameters
*
* - `row`: A `SpringRow` pointer to get a column value from.
* - `i_col`: The column index to get a value from.
* - `out`: A pointer to a buffer to store the column value.
* - `out_len`: The length of the buffer pointed by `out`.
*
* # Returns
*
* - `> 0`: Length of the text.
* - `Unavailable`:
* - Column pointed by `i_col` is already fetched.
* - `i_col` is out of range.
* - `CNull`: Column value is NULL.
*/
int spring_column_text(const struct SpringSinkRow *row, uint16_t i_col, char *out, int out_len);
/**
* Get a BLOB column.
*
* # Parameters
*
* - `row`: A `SpringRow` pointer to get a column value from.
* - `i_col`: The column index to get a value from.
* - `out`: A pointer to a buffer to store the column value.
* - `out_len`: The length of the buffer pointed by `out`.
*
* # Returns
*
* - `> 0`: Length of the text.
* - `Unavailable`:
* - Column pointed by `i_col` is already fetched.
* - `i_col` is out of range.
* - `CNull`: Column value is NULL.
*/
int spring_column_blob(const struct SpringSinkRow *row, uint16_t i_col, void *out, int out_len);
/**
* Get a bool column.
*
* # Parameters
*
* - `row`: A `SpringRow` pointer to get a column value from.
* - `i_col`: The column index to get a value from.
* - `out`: A pointer to a buffer to store the column value.
*
* # Returns
*
* - `Ok`: On success.
* - `Unavailable`:
* - Column pointed by `i_col` is already fetched.
* - `i_col` is out of range.
* - `CNull`: Column value is NULL.
*/
enum SpringErrno spring_column_bool(const struct SpringSinkRow *row, uint16_t i_col, bool *out);
/**
* Get a 4-byte floating point column.
*
* # Parameters
*
* - `row`: A `SpringRow` pointer to get a column value from.
* - `i_col`: The column index to get a value from.
* - `out`: A pointer to a buffer to store the column value.
*
* # Returns
*
* - `Ok`: On success.
* - `Unavailable`:
* - Column pointed by `i_col` is already fetched.
* - `i_col` is out of range.
* - `CNull`: Column value is NULL.
*/
enum SpringErrno spring_column_float(const struct SpringSinkRow *row, uint16_t i_col, float *out);
/**
* Write the most recent error number into `errno_` and message into a caller-provided buffer as a UTF-8
* string, returning the number of bytes written.
*
* # Note
*
* This writes a **UTF-8** string into the buffer. Windows users may need to
* convert it to a UTF-16 "unicode" afterwards.
*
* If there are no recent errors then this returns `0` (because we wrote 0
* bytes). `-1` is returned if there are any errors, for example when passed a
* null pointer or a buffer of insufficient size.
*
* # Returns
*
* - `0`: if there are no recent errors.
* - `> 0`: the length of the recent error message.
* - `< 0`: SpringErrno
*/
int spring_last_err(enum SpringErrno *errno_,
char *errmsg,
int errmsg_len);
/**
* Calculate the number of bytes in the last error's error message **not**
* including any trailing `null` characters.
*
* # Returns
*
* - `0`: if there are no recent errors.
* - `> 0`: the length of the recent error message.
*/
int spring_last_errmsg_len(void);
#endif /* _SPRINGQL_H_ */