|
| 1 | +#ifndef TURSO_H |
| 2 | +#define TURSO_H |
| 3 | + |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stddef.h> |
| 6 | +#include <stdint.h> |
| 7 | + |
| 8 | +/// SAFETY: slice with non-null ptr must points to the valid memory range [ptr..ptr + len) |
| 9 | +/// ownership of the slice is not transferred - so its either caller owns the data or turso |
| 10 | +/// as the owner doesn't change - there is no method to free the slice reference - because: |
| 11 | +/// 1. if tursodb owns it - it will clean it in appropriate time |
| 12 | +/// 2. if caller owns it - it must clean it in appropriate time with appropriate method and tursodb doesn't know how to properly free the data |
| 13 | +typedef struct |
| 14 | +{ |
| 15 | + const void *ptr; |
| 16 | + size_t len; |
| 17 | +} turso_slice_ref_t; |
| 18 | + |
| 19 | +typedef enum |
| 20 | +{ |
| 21 | + TURSO_OK = 0, |
| 22 | + TURSO_DONE = 1, |
| 23 | + TURSO_ROW = 2, |
| 24 | + TURSO_IO = 3, |
| 25 | + TURSO_BUSY = 4, |
| 26 | + TURSO_INTERRUPT = 5, |
| 27 | + TURSO_BUSY_SNAPSHOT = 6, |
| 28 | + TURSO_ERROR = 127, |
| 29 | + TURSO_MISUSE = 128, |
| 30 | + TURSO_CONSTRAINT = 129, |
| 31 | + TURSO_READONLY = 130, |
| 32 | + TURSO_DATABASE_FULL = 131, |
| 33 | + TURSO_NOTADB = 132, |
| 34 | + TURSO_CORRUPT = 133, |
| 35 | + TURSO_IOERR = 134, |
| 36 | +} turso_status_code_t; |
| 37 | + |
| 38 | +// enumeration of value types supported by the database |
| 39 | +typedef enum |
| 40 | +{ |
| 41 | + TURSO_TYPE_UNKNOWN = 0, |
| 42 | + TURSO_TYPE_INTEGER = 1, |
| 43 | + TURSO_TYPE_REAL = 2, |
| 44 | + TURSO_TYPE_TEXT = 3, |
| 45 | + TURSO_TYPE_BLOB = 4, |
| 46 | + TURSO_TYPE_NULL = 5, |
| 47 | +} turso_type_t; |
| 48 | + |
| 49 | +typedef enum |
| 50 | +{ |
| 51 | + TURSO_TRACING_LEVEL_ERROR = 1, |
| 52 | + TURSO_TRACING_LEVEL_WARN, |
| 53 | + TURSO_TRACING_LEVEL_INFO, |
| 54 | + TURSO_TRACING_LEVEL_DEBUG, |
| 55 | + TURSO_TRACING_LEVEL_TRACE, |
| 56 | +} turso_tracing_level_t; |
| 57 | + |
| 58 | +/// opaque pointer to the TursoDatabase instance |
| 59 | +/// SAFETY: the database must be opened and closed only once but can be used concurrently |
| 60 | +typedef struct turso_database turso_database_t; |
| 61 | + |
| 62 | +/// opaque pointer to the TursoConnection instance |
| 63 | +/// SAFETY: the connection must be used exclusive and can't be accessed concurrently |
| 64 | +typedef struct turso_connection turso_connection_t; |
| 65 | + |
| 66 | +/// opaque pointer to the TursoStatement instance |
| 67 | +/// SAFETY: the statement must be used exclusive and can't be accessed concurrently |
| 68 | +typedef struct turso_statement turso_statement_t; |
| 69 | + |
| 70 | +// return STATIC zero-terminated C-string with turso version (sem-ver string e.g. x.y.z-...) |
| 71 | +// (this string DO NOT need to be deallocated as it static) |
| 72 | +const char *turso_version(); |
| 73 | + |
| 74 | +typedef struct |
| 75 | +{ |
| 76 | + /* zero-terminated C string */ |
| 77 | + const char *message; |
| 78 | + /* zero-terminated C string */ |
| 79 | + const char *target; |
| 80 | + /* zero-terminated C string */ |
| 81 | + const char *file; |
| 82 | + uint64_t timestamp; |
| 83 | + size_t line; |
| 84 | + turso_tracing_level_t level; |
| 85 | +} turso_log_t; |
| 86 | + |
| 87 | +typedef struct |
| 88 | +{ |
| 89 | + /// SAFETY: turso_log_t log argument fields have lifetime scoped to the logger invocation |
| 90 | + /// caller must ensure that data is properly copied if it wants it to have longer lifetime |
| 91 | + void (*logger)(const turso_log_t *log); |
| 92 | + /* zero-terminated C string */ |
| 93 | + const char *log_level; |
| 94 | +} turso_config_t; |
| 95 | + |
| 96 | +/** |
| 97 | + * Database description. |
| 98 | + */ |
| 99 | +typedef struct |
| 100 | +{ |
| 101 | + /** Parameter which defines who drives the IO - callee or the caller (non-zero parameter value interpreted as async IO) */ |
| 102 | + uint64_t async_io; |
| 103 | + /** Path to the database file or `:memory:` |
| 104 | + * zero-terminated C string |
| 105 | + */ |
| 106 | + const char *path; |
| 107 | + /** Optional comma separated list of experimental features to enable |
| 108 | + * zero-terminated C string or null pointer |
| 109 | + */ |
| 110 | + const char *experimental_features; |
| 111 | + /** optional VFS parameter explicitly specifying FS backend for the database. |
| 112 | + * Available options are: |
| 113 | + * - "memory": in-memory backend |
| 114 | + * - "syscall": generic syscall backend |
| 115 | + * - "io_uring": IO uring (supported only on Linux) |
| 116 | + * - "experimental_win_iocp": Windows IOCP [experimental](supported only on Windows) |
| 117 | + */ |
| 118 | + const char *vfs; |
| 119 | + /** optional encryption cipher |
| 120 | + * as encryption is experimental - experimental_features must have "encryption" in the list |
| 121 | + */ |
| 122 | + const char *encryption_cipher; |
| 123 | + /** optional encryption hexkey |
| 124 | + * as encryption is experimental - experimental_features must have "encryption" in the list |
| 125 | + */ |
| 126 | + const char *encryption_hexkey; |
| 127 | +} turso_database_config_t; |
| 128 | + |
| 129 | +/** Setup global database info */ |
| 130 | +turso_status_code_t turso_setup( |
| 131 | + const turso_config_t *config, |
| 132 | + /** Optional return error parameter (can be null) */ |
| 133 | + const char **error_opt_out); |
| 134 | + |
| 135 | +/** Create database holder but do not open it */ |
| 136 | +turso_status_code_t turso_database_new( |
| 137 | + const turso_database_config_t *config, |
| 138 | + /** reference to pointer which will be set to database instance in case of TURSO_OK result */ |
| 139 | + const turso_database_t **database, |
| 140 | + /** Optional return error parameter (can be null) */ |
| 141 | + const char **error_opt_out); |
| 142 | + |
| 143 | +/** Open database |
| 144 | + * Can return TURSO_IO result if async_io=true is set |
| 145 | + */ |
| 146 | +turso_status_code_t turso_database_open( |
| 147 | + const turso_database_t *database, |
| 148 | + /** Optional return error parameter (can be null) */ |
| 149 | + const char **error_opt_out); |
| 150 | + |
| 151 | +/** Connect to the database */ |
| 152 | +turso_status_code_t turso_database_connect( |
| 153 | + const turso_database_t *self, |
| 154 | + /** reference to pointer which will be set to connection instance in case of TURSO_OK result */ |
| 155 | + turso_connection_t **connection, |
| 156 | + /** Optional return error parameter (can be null) */ |
| 157 | + const char **error_opt_out); |
| 158 | + |
| 159 | +/** Set busy timeout for the connection */ |
| 160 | +void turso_connection_set_busy_timeout_ms(const turso_connection_t *self, int64_t timeout_ms); |
| 161 | + |
| 162 | +/** Get autocommit state of the connection */ |
| 163 | +bool turso_connection_get_autocommit(const turso_connection_t *self); |
| 164 | + |
| 165 | +/** Get last insert rowid for the connection or 0 if no inserts happened before */ |
| 166 | +int64_t turso_connection_last_insert_rowid(const turso_connection_t *self); |
| 167 | + |
| 168 | +/** Prepare single statement in a connection */ |
| 169 | +turso_status_code_t |
| 170 | +turso_connection_prepare_single( |
| 171 | + const turso_connection_t *self, |
| 172 | + /* zero-terminated C string */ |
| 173 | + const char *sql, |
| 174 | + /** reference to pointer which will be set to statement instance in case of TURSO_OK result */ |
| 175 | + turso_statement_t **statement, |
| 176 | + /** Optional return error parameter (can be null) */ |
| 177 | + const char **error_opt_out); |
| 178 | + |
| 179 | +/** Prepare first statement in a string containing multiple statements in a connection */ |
| 180 | +turso_status_code_t |
| 181 | +turso_connection_prepare_first( |
| 182 | + const turso_connection_t *self, |
| 183 | + /* zero-terminated C string */ |
| 184 | + const char *sql, |
| 185 | + /** reference to pointer which will be set to statement instance in case of TURSO_OK result; can be null if no statements can be parsed from the input string */ |
| 186 | + turso_statement_t **statement, |
| 187 | + /** offset in the sql string right after the parsed statement */ |
| 188 | + size_t *tail_idx, |
| 189 | + /** Optional return error parameter (can be null) */ |
| 190 | + const char **error_opt_out); |
| 191 | + |
| 192 | +/** close the connection preventing any further operations executed over it |
| 193 | + * caller still need to call deinit method to reclaim memory from the instance holding connection |
| 194 | + * SAFETY: caller must guarantee that no ongoing operations are running over connection before calling turso_connection_close(...) method |
| 195 | + */ |
| 196 | +turso_status_code_t turso_connection_close( |
| 197 | + const turso_connection_t *self, |
| 198 | + /** Optional return error parameter (can be null) */ |
| 199 | + const char **error_opt_out); |
| 200 | + |
| 201 | +/** Execute single statement |
| 202 | + * execute returns TURSO_DONE if execution completed |
| 203 | + * execute returns TURSO_IO if async_io was set and execution needs IO in order to make progress |
| 204 | + */ |
| 205 | +turso_status_code_t turso_statement_execute( |
| 206 | + const turso_statement_t *self, |
| 207 | + uint64_t *rows_changes, |
| 208 | + /** Optional return error parameter (can be null) */ |
| 209 | + const char **error_opt_out); |
| 210 | + |
| 211 | +/** Step statement execution once |
| 212 | + * Returns TURSO_DONE if execution finished |
| 213 | + * Returns TURSO_ROW if execution generated the row (row values can be inspected with corresponding statement methods) |
| 214 | + * Returns TURSO_IO if async_io was set and statement needs to execute IO to make progress |
| 215 | + */ |
| 216 | +turso_status_code_t turso_statement_step(const turso_statement_t *self, const char **error_opt_out); |
| 217 | + |
| 218 | +/** Execute one iteration of underlying IO backend after TURSO_IO status code |
| 219 | + * This function either return some ERROR status or TURSO_OK |
| 220 | + */ |
| 221 | +turso_status_code_t turso_statement_run_io(const turso_statement_t *self, const char **error_opt_out); |
| 222 | + |
| 223 | +/** Reset a statement |
| 224 | + * This method must be called in order to cleanup statement resources and prepare it for re-execution |
| 225 | + * Any pending execution will be aborted - be careful and in certain cases ensure that turso_statement_finalize called before turso_statement_reset |
| 226 | + */ |
| 227 | +turso_status_code_t turso_statement_reset(const turso_statement_t *self, const char **error_opt_out); |
| 228 | + |
| 229 | +/** Finalize a statement |
| 230 | + * finalize returns TURSO_DONE if finalization completed |
| 231 | + * This method must be called in the end of statement execution (either successfull or not) |
| 232 | + */ |
| 233 | +turso_status_code_t turso_statement_finalize(const turso_statement_t *self, const char **error_opt_out); |
| 234 | + |
| 235 | +/** return amount of row modifications (insert/delete operations) made by the most recent executed statement */ |
| 236 | +int64_t turso_statement_n_change(const turso_statement_t *self); |
| 237 | + |
| 238 | +/** Get column count */ |
| 239 | +int64_t turso_statement_column_count(const turso_statement_t *self); |
| 240 | + |
| 241 | +/** Get the column name at the index |
| 242 | + * C string allocated by Turso must be freed after the usage with corresponding turso_str_deinit(...) method |
| 243 | + */ |
| 244 | +const char *turso_statement_column_name(const turso_statement_t *self, size_t index); |
| 245 | + |
| 246 | +/** Get the column declared type at the index (e.g. "INTEGER", "TEXT", "DATETIME", etc.) |
| 247 | + * Returns NULL if the column type is not available. |
| 248 | + * C string allocated by Turso must be freed after the usage with corresponding turso_str_deinit(...) method |
| 249 | + */ |
| 250 | +const char *turso_statement_column_decltype(const turso_statement_t *self, size_t index); |
| 251 | + |
| 252 | +/** Get the row value at the the index for a current statement state |
| 253 | + * SAFETY: returned pointers will be valid only until next invocation of statement operation (step, finalize, reset, etc) |
| 254 | + * Caller must make sure that any non-owning memory is copied appropriated if it will be used for longer lifetime |
| 255 | + */ |
| 256 | +turso_type_t turso_statement_row_value_kind(const turso_statement_t *self, size_t index); |
| 257 | +/* Get amount of bytes in the BLOB or TEXT values |
| 258 | + * Return -1 for other kinds |
| 259 | + */ |
| 260 | +int64_t turso_statement_row_value_bytes_count(const turso_statement_t *self, size_t index); |
| 261 | +/* Get pointer to the start of the slice for BLOB or TEXT values |
| 262 | + * Return NULL for other kinds |
| 263 | + */ |
| 264 | +const char *turso_statement_row_value_bytes_ptr(const turso_statement_t *self, size_t index); |
| 265 | +/* Return value of INTEGER kind |
| 266 | + * Return 0 for other kinds |
| 267 | + */ |
| 268 | +int64_t turso_statement_row_value_int(const turso_statement_t *self, size_t index); |
| 269 | +/* Return value of REAL kind |
| 270 | + * Return 0 for other kinds |
| 271 | + */ |
| 272 | +double turso_statement_row_value_double(const turso_statement_t *self, size_t index); |
| 273 | + |
| 274 | +/** Return named argument position in a statement |
| 275 | + Return positive integer with 1-indexed position if named parameter was found |
| 276 | + Return -1 if parameter was not found |
| 277 | +*/ |
| 278 | +int64_t turso_statement_named_position( |
| 279 | + const turso_statement_t *self, |
| 280 | + /* zero-terminated C string */ |
| 281 | + const char *name); |
| 282 | + |
| 283 | +/** Return parameters count for the statement |
| 284 | + * -1 if pointer is invalid |
| 285 | + */ |
| 286 | +int64_t |
| 287 | +turso_statement_parameters_count(const turso_statement_t *self); |
| 288 | + |
| 289 | +/** Bind a positional argument to a statement */ |
| 290 | +turso_status_code_t |
| 291 | +turso_statement_bind_positional_null(const turso_statement_t *self, size_t position); |
| 292 | +turso_status_code_t |
| 293 | +turso_statement_bind_positional_int(const turso_statement_t *self, size_t position, int64_t value); |
| 294 | +turso_status_code_t |
| 295 | +turso_statement_bind_positional_double(const turso_statement_t *self, size_t position, double value); |
| 296 | +turso_status_code_t |
| 297 | +turso_statement_bind_positional_blob( |
| 298 | + const turso_statement_t *self, |
| 299 | + size_t position, |
| 300 | + /* pointer to the start of BLOB slice */ |
| 301 | + const char *ptr, |
| 302 | + /* length of BLOB slice */ |
| 303 | + size_t len); |
| 304 | +turso_status_code_t |
| 305 | +turso_statement_bind_positional_text( |
| 306 | + const turso_statement_t *self, |
| 307 | + size_t position, |
| 308 | + /* pointer to the start of TEXT slice */ |
| 309 | + const char *ptr, |
| 310 | + /* length of TEXT slice */ |
| 311 | + size_t len); |
| 312 | + |
| 313 | +/** Deallocate C string allocated by Turso */ |
| 314 | +void turso_str_deinit(const char *self); |
| 315 | +/** Deallocate and close a database |
| 316 | + * SAFETY: caller must ensure that no other code can concurrently or later call methods over deinited database |
| 317 | + */ |
| 318 | +void turso_database_deinit(const turso_database_t *self); |
| 319 | +/** Deallocate and close a connection |
| 320 | + * SAFETY: caller must ensure that no other code can concurrently or later call methods over deinited connection |
| 321 | + */ |
| 322 | +void turso_connection_deinit(const turso_connection_t *self); |
| 323 | +/** Deallocate and close a statement |
| 324 | + * SAFETY: caller must ensure that no other code can concurrently or later call methods over deinited statement |
| 325 | + */ |
| 326 | +void turso_statement_deinit(const turso_statement_t *self); |
| 327 | + |
| 328 | +#endif /* TURSO_H */ |
0 commit comments