#include #include #include /** * Placeholder struct representing the [rorm_db::Database] struct. */ typedef struct Database_ Database; /** * Placeholder struct representing the stream result from a query. */ typedef struct Stream_ Stream; /** * Placeholder struct representing the row gathered from a stream. */ typedef struct Row_ Row; /** * Placeholder struct representing a transaction */ typedef struct Transaction_ Transaction; /** * Representation of the database backend. * *This is used to determine the correct driver and the correct dialect to use. */ enum DBBackend { /** * This exists to forbid default initializations with 0 on C side. * Using this type will result in an [crate::errors::Error::ConfigurationError] */ Invalid, /** * SQLite backend */ SQLite, /** * MySQL / MariaDB backend */ MySQL, /** * Postgres backend */ Postgres, }; typedef int32_t DBBackend; /** * Representation of a join type. */ typedef enum FFIJoinType { /** * Normal join operation. * * Equivalent to INNER JOIN */ Join, /** * Cartesian product of the tables */ CrossJoin, /** * Given: * T1 LEFT JOIN T2 ON .. * * First, an inner join is performed. * Then, for each row in T1 that does not satisfy the join condition with any row in T2, * a joined row is added with null values in columns of T2. */ LeftJoin, /** * Given: * T1 RIGHT JOIN T2 ON .. * * First, an inner join is performed. * Then, for each row in T2 that does not satisfy the join condition with any row in T1, * a joined row is added with null values in columns of T1. */ RightJoin, /** * Given: * T1 FULL JOIN T2 ON .. * * First, an inner join is performed. * Then, for each row in T2 that does not satisfy the join condition with any row in T1, * a joined row is added with null values in columns of T1. * Also, for each row in T1 that does not satisfy the join condition with any row in T2, * a joined row is added with null values in columns of T2. */ FullJoin, } FFIJoinType; /** * Representation of the [Ordering] */ typedef enum FFIOrdering { /** * Ascending */ Asc, /** * Descending */ Desc, } FFIOrdering; /** * Representation of a string. */ typedef struct FFIString { const uint8_t *content; size_t size; } FFIString; /** * Configuration operation to connect to a database. * *Will be converted into [rorm_db::DatabaseConfiguration]. * *`min_connections` and `max_connections` must not be 0. */ typedef struct DBConnectOptions { DBBackend backend; struct FFIString name; struct FFIString host; uint16_t port; struct FFIString user; struct FFIString password; uint32_t min_connections; uint32_t max_connections; } DBConnectOptions; /** * Representation of all error codes. */ typedef enum Error_Tag { /** * Everything's fine, nothing to worry about. */ NoError, /** * Runtime was destroyed or never created and can therefore not be accessed. */ MissingRuntimeError, /** * An error occurred while getting or accessing the runtime. */ RuntimeError, /** * An error occurred while trying to convert a FFIString into a &str due to invalid content */ InvalidStringError, /** * Configuration error */ ConfigurationError, /** * Database error */ DatabaseError, /** * There are no rows left in the stream */ NoRowsLeftInStream, /** * Column could not be converted in the given type */ ColumnDecodeError, /** * Column was not found in row */ ColumnNotFoundError, /** * The index in the row was out of bounds */ ColumnIndexOutOfBoundsError, /** * The provided date could not be parsed */ InvalidDateError, /** * The provided time could not be parsed */ InvalidTimeError, /** * The provided datetime could not be parsed */ InvalidDateTimeError, } Error_Tag; typedef struct Error { Error_Tag tag; union { struct { struct FFIString runtime_error; }; struct { struct FFIString configuration_error; }; struct { struct FFIString database_error; }; }; } Error; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_FFICondition { const struct FFICondition *content; size_t size; } FFISlice_FFICondition; /** * This enum represents all available unary conditions. */ typedef enum FFIUnaryCondition_Tag { /** * Representation of SQL's "{} IS NULL" */ IsNull, /** * Representation of SQL's "{} IS NOT NULL" */ IsNotNull, /** * Representation of SQL's "EXISTS {}" */ Exists, /** * Representation of SQL's "NOT EXISTS {}" */ NotExists, /** * Representation of SQL's "NOT {}" */ Not, } FFIUnaryCondition_Tag; typedef struct FFIUnaryCondition { FFIUnaryCondition_Tag tag; union { struct { const struct FFICondition *is_null; }; struct { const struct FFICondition *is_not_null; }; struct { const struct FFICondition *exists; }; struct { const struct FFICondition *not_exists; }; struct { const struct FFICondition *not; }; }; } FFIUnaryCondition; /** * This enum represents a binary expression. */ typedef enum FFIBinaryCondition_Tag { /** * Representation of "{} = {}" in SQL */ Equals, /** * Representation of "{} <> {}" in SQL */ NotEquals, /** * Representation of "{} > {}" in SQL */ Greater, /** * Representation of "{} >= {}" in SQL */ GreaterOrEquals, /** * Representation of "{} < {}" in SQL */ Less, /** * Representation of "{} <= {}" in SQL */ LessOrEquals, /** * Representation of "{} LIKE {}" in SQL */ Like, /** * Representation of "{} NOT LIKE {}" in SQL */ NotLike, /** * Representation of "{} REGEXP {}" in SQL */ Regexp, /** * Representation of "{} NOT REGEXP {}" in SQL */ NotRegexp, /** * Representation of "{} IN {}" in SQL */ In, /** * Representation of "{} NOT IN {}" in SQL */ NotIn, } FFIBinaryCondition_Tag; typedef struct FFIBinaryCondition { FFIBinaryCondition_Tag tag; union { struct { const struct FFICondition *equals[2]; }; struct { const struct FFICondition *not_equals[2]; }; struct { const struct FFICondition *greater[2]; }; struct { const struct FFICondition *greater_or_equals[2]; }; struct { const struct FFICondition *less[2]; }; struct { const struct FFICondition *less_or_equals[2]; }; struct { const struct FFICondition *like[2]; }; struct { const struct FFICondition *not_like[2]; }; struct { const struct FFICondition *regexp[2]; }; struct { const struct FFICondition *not_regexp[2]; }; struct { const struct FFICondition *in[2]; }; struct { const struct FFICondition *not_in[2]; }; }; } FFIBinaryCondition; /** * This enum represents all available ternary expression. */ typedef enum FFITernaryCondition_Tag { /** * Between represents "{} BETWEEN {} AND {}" from SQL */ Between, /** * Between represents "{} NOT BETWEEN {} AND {}" from SQL */ NotBetween, } FFITernaryCondition_Tag; typedef struct FFITernaryCondition { FFITernaryCondition_Tag tag; union { struct { const struct FFICondition *between[3]; }; struct { const struct FFICondition *not_between[3]; }; }; } FFITernaryCondition; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_FFIString_Tag { /** * None value */ None_FFIString, /** * Some value */ Some_FFIString, } FFIOption_FFIString_Tag; typedef struct FFIOption_FFIString { FFIOption_FFIString_Tag tag; union { struct { struct FFIString some; }; }; } FFIOption_FFIString; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_u8 { const uint8_t *content; size_t size; } FFISlice_u8; /** * Representation of a [chrono::NaiveTime]. */ typedef struct FFITime { uint32_t hour; uint32_t min; uint32_t sec; } FFITime; /** * Representation of a [chrono::NaiveDate] */ typedef struct FFIDate { int32_t year; uint32_t month; uint32_t day; } FFIDate; /** * Representation of a [chrono::DateTime]. */ typedef struct FFIDateTime { int32_t year; uint32_t month; uint32_t day; uint32_t hour; uint32_t min; uint32_t sec; } FFIDateTime; /** * This enum represents a value */ typedef enum FFIValue_Tag { /** * null representation */ Null, /** * Representation of an identifier. * This variant will not be escaped, so do not * pass unchecked data to it. */ Ident, /** * Representation of a column. */ Column, /** * String representation */ String, /** * i64 representation */ I64, /** * i32 representation */ I32, /** * i16 representation */ I16, /** * Bool representation */ Bool, /** * f64 representation */ F64, /** * f32 representation */ F32, /** * Binary representation */ Binary, /** * Representation of time without timezones */ NaiveTime, /** * Representation of dates without timezones */ NaiveDate, /** * Representation of datetimes without timezones */ NaiveDateTime, } FFIValue_Tag; typedef struct Column_Body { /** * Optional table name */ struct FFIOption_FFIString table_name; /** * Name of the column */ struct FFIString column_name; } Column_Body; typedef struct FFIValue { FFIValue_Tag tag; union { struct { struct FFIString ident; }; Column_Body column; struct { struct FFIString string; }; struct { int64_t i64; }; struct { int32_t i32; }; struct { int16_t i16; }; struct { bool bool_; }; struct { double f64; }; struct { float f32; }; struct { struct FFISlice_u8 binary; }; struct { struct FFITime naive_time; }; struct { struct FFIDate naive_date; }; struct { struct FFIDateTime naive_date_time; }; }; } FFIValue; /** * This enum represents a condition tree. */ typedef enum FFICondition_Tag { /** * A list of [Condition]s, that get expanded to "{} AND {} ..." */ Conjunction, /** * A list of [Condition]s, that get expanded to "{} OR {} ..." */ Disjunction, /** * Representation of a unary condition. */ UnaryCondition, /** * Representation of a binary condition. */ BinaryCondition, /** * Representation of a ternary condition. */ TernaryCondition, /** * Representation of a value. */ Value, } FFICondition_Tag; typedef struct FFICondition { FFICondition_Tag tag; union { struct { struct FFISlice_FFICondition conjunction; }; struct { struct FFISlice_FFICondition disjunction; }; struct { struct FFIUnaryCondition unary_condition; }; struct { struct FFIBinaryCondition binary_condition; }; struct { struct FFITernaryCondition ternary_condition; }; struct { struct FFIValue value; }; }; } FFICondition; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_FFIString { const struct FFIString *content; size_t size; } FFISlice_FFIString; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_FFIValue { const struct FFIValue *content; size_t size; } FFISlice_FFIValue; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_FFISlice_FFIValue { const struct FFISlice_FFIValue *content; size_t size; } FFISlice_FFISlice_FFIValue; /** * FFI representation of a [SelectColumnImpl] */ typedef struct FFIColumnSelector { struct FFIOption_FFIString table_name; struct FFIString column_name; struct FFIOption_FFIString select_alias; } FFIColumnSelector; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_FFIColumnSelector { const struct FFIColumnSelector *content; size_t size; } FFISlice_FFIColumnSelector; /** * FFI representation of a Join expression. */ typedef struct FFIJoin { /** * Type of the join operation */ enum FFIJoinType join_type; /** * Name of the join table */ struct FFIString table_name; /** * Alias for the join table */ struct FFIString join_alias; /** * Condition to apply the join on */ const struct FFICondition *join_condition; } FFIJoin; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_FFIJoin { const struct FFIJoin *content; size_t size; } FFISlice_FFIJoin; /** * Representation of a [OrderByEntry] */ typedef struct FFIOrderByEntry { enum FFIOrdering ordering; struct FFIOption_FFIString table_name; struct FFIString column_name; } FFIOrderByEntry; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_FFIOrderByEntry { const struct FFIOrderByEntry *content; size_t size; } FFISlice_FFIOrderByEntry; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_u64_Tag { /** * None value */ None_u64, /** * Some value */ Some_u64, } FFIOption_u64_Tag; typedef struct FFIOption_u64 { FFIOption_u64_Tag tag; union { struct { uint64_t some; }; }; } FFIOption_u64; /** * FFI representation of a Limit clause. */ typedef struct FFILimitClause { uint64_t limit; struct FFIOption_u64 offset; } FFILimitClause; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_FFILimitClause_Tag { /** * None value */ None_FFILimitClause, /** * Some value */ Some_FFILimitClause, } FFIOption_FFILimitClause_Tag; typedef struct FFIOption_FFILimitClause { FFIOption_FFILimitClause_Tag tag; union { struct { struct FFILimitClause some; }; }; } FFIOption_FFILimitClause; /** * Representation of an FFI safe slice. */ typedef struct FFISlice______Row { const Row *const *content; size_t size; } FFISlice______Row; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_Row { const Row *content; size_t size; } FFISlice_Row; /** * Representation of an update. * *Consists of a column and the value to set to this column. */ typedef struct FFIUpdate { struct FFIString column; struct FFIValue value; } FFIUpdate; /** * Representation of an FFI safe slice. */ typedef struct FFISlice_FFIUpdate { const struct FFIUpdate *content; size_t size; } FFISlice_FFIUpdate; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_FFISlice_u8_Tag { /** * None value */ None_FFISlice_u8, /** * Some value */ Some_FFISlice_u8, } FFIOption_FFISlice_u8_Tag; typedef struct FFIOption_FFISlice_u8 { FFIOption_FFISlice_u8_Tag tag; union { struct { struct FFISlice_u8 some; }; }; } FFIOption_FFISlice_u8; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_bool_Tag { /** * None value */ None_bool, /** * Some value */ Some_bool, } FFIOption_bool_Tag; typedef struct FFIOption_bool { FFIOption_bool_Tag tag; union { struct { bool some; }; }; } FFIOption_bool; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_FFIDate_Tag { /** * None value */ None_FFIDate, /** * Some value */ Some_FFIDate, } FFIOption_FFIDate_Tag; typedef struct FFIOption_FFIDate { FFIOption_FFIDate_Tag tag; union { struct { struct FFIDate some; }; }; } FFIOption_FFIDate; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_FFIDateTime_Tag { /** * None value */ None_FFIDateTime, /** * Some value */ Some_FFIDateTime, } FFIOption_FFIDateTime_Tag; typedef struct FFIOption_FFIDateTime { FFIOption_FFIDateTime_Tag tag; union { struct { struct FFIDateTime some; }; }; } FFIOption_FFIDateTime; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_f32_Tag { /** * None value */ None_f32, /** * Some value */ Some_f32, } FFIOption_f32_Tag; typedef struct FFIOption_f32 { FFIOption_f32_Tag tag; union { struct { float some; }; }; } FFIOption_f32; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_f64_Tag { /** * None value */ None_f64, /** * Some value */ Some_f64, } FFIOption_f64_Tag; typedef struct FFIOption_f64 { FFIOption_f64_Tag tag; union { struct { double some; }; }; } FFIOption_f64; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_i16_Tag { /** * None value */ None_i16, /** * Some value */ Some_i16, } FFIOption_i16_Tag; typedef struct FFIOption_i16 { FFIOption_i16_Tag tag; union { struct { int16_t some; }; }; } FFIOption_i16; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_i32_Tag { /** * None value */ None_i32, /** * Some value */ Some_i32, } FFIOption_i32_Tag; typedef struct FFIOption_i32 { FFIOption_i32_Tag tag; union { struct { int32_t some; }; }; } FFIOption_i32; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_i64_Tag { /** * None value */ None_i64, /** * Some value */ Some_i64, } FFIOption_i64_Tag; typedef struct FFIOption_i64 { FFIOption_i64_Tag tag; union { struct { int64_t some; }; }; } FFIOption_i64; /** * Helper type to wrap [Option] ffi safe. */ typedef enum FFIOption_FFITime_Tag { /** * None value */ None_FFITime, /** * Some value */ Some_FFITime, } FFIOption_FFITime_Tag; typedef struct FFIOption_FFITime { FFIOption_FFITime_Tag tag; union { struct { struct FFITime some; }; }; } FFIOption_FFITime; /** * Connect to the database using the provided [DBConnectOptions]. * *You must provide a callback with the following parameters: * *The first parameter is the `context` pointer. The second parameter will be a pointer to the Database connection. It will be needed to make queries. The last parameter holds an [Error] enum. * ***Important**: Rust does not manage the memory of the database. To properly free it, use [rorm_db_free]. * *This function is called from an asynchronous context. */ void rorm_db_connect(struct DBConnectOptions options, void (*callback)(void*, Database*, struct Error), void* context); /** * This function deletes rows from the database based on the given conditions. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `model`: Name of the table to query. - `condition`: Pointer to a [Condition]. - `callback`: callback function. Takes the `context`, a pointer to a vec of rows and an [Error]. - `context`: Pass through void pointer. * *Returns the rows affected of the delete statement. Note that this also includes relations, etc. * ***Important**: - Make sure that `db`, `model` and `condition` are allocated until the callback is executed. * *This function is called from an asynchronous context. */ void rorm_db_delete(const Database *db, Transaction *transaction, struct FFIString model, const struct FFICondition *condition, void (*callback)(void*, uint64_t, struct Error), void* context); /** * Free the connection to the database. * *Takes the pointer to the database instance. * ***Important**: Do not call this function more than once! * *This function is called completely synchronously. */ void rorm_db_free(Database*); /** * This function inserts a row into the database. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `model`: Name of the table to query. - `columns`: Array of columns to insert to the table. - `row`: List of values to insert. Must be of the same length as `columns`. - `callback`: callback function. Takes the `context` and an [Error]. - `context`: Pass through void pointer. * ***Important**: - Make sure that `db`, `model`, `columns` and `row` are allocated until the callback is executed. * *This function is called from an asynchronous context. */ void rorm_db_insert(const Database *db, Transaction *transaction, struct FFIString model, struct FFISlice_FFIString columns, struct FFISlice_FFIValue row, void (*callback)(void*, struct Error), void* context); /** * This function inserts multiple rows into the database. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `model`: Name of the table to query. - `columns`: Array of columns to insert to the table. - `rows`: List of list of values to insert. The inner lists must be of the same length as `columns`. - `callback`: callback function. Takes the `context` and an [Error]. - `context`: Pass through void pointer. * ***Important**: - Make sure that `db`, `model`, `columns` and `rows` are allocated until the callback is executed. * *This function is called from an asynchronous context. */ void rorm_db_insert_bulk(const Database *db, Transaction *transaction, struct FFIString model, struct FFISlice_FFIString columns, struct FFISlice_FFISlice_FFIValue rows, void (*callback)(void*, struct Error), void* context); /** * This function queries the database given the provided parameter and returns all matched rows. * *To include the statement in a transaction specify `transaction` as a valid Transaction. As the Transaction needs to be mutable, it is important to not use the Transaction anywhere else until the callback is finished. * *If the statement should be executed **not** in a Transaction, specify a null pointer. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect] - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `model`: Name of the table to query. - `columns`: Array of columns to retrieve from the database. - `joins`: Array of joins to add to the query. - `condition`: Pointer to a [Condition]. - `order_by`: Array of [FFIOrderByEntry]. - `limit`: Optional limit / offset to set to the query. - `callback`: callback function. Takes the `context`, a FFISlice of rows and an [Error]. - `context`: Pass through void pointer. * ***Important**: - Make sure that `db`, `model`, `columns`, `joins` and `condition` are allocated until the callback is executed. - The FFISlice returned in the callback is freed after the callback has ended. * *This function is called from an asynchronous context. */ void rorm_db_query_all(const Database *db, Transaction *transaction, struct FFIString model, struct FFISlice_FFIColumnSelector columns, struct FFISlice_FFIJoin joins, const struct FFICondition *condition, struct FFISlice_FFIOrderByEntry order_by, struct FFIOption_FFILimitClause limit, void (*callback)(void*, struct FFISlice______Row, struct Error), void* context); /** * This function queries the database given the provided parameter and returns one matched row. * *To include the statement in a transaction specify `transaction` as a valid Transaction. As the Transaction needs to be mutable, it is important to not use the Transaction anywhere else until the callback is finished. * *If the statement should be executed **not** in a Transaction, specify a null pointer. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `model`: Name of the table to query. - `columns`: Array of columns to retrieve from the database. - `joins`: Array of joins to add to the query. - `condition`: Pointer to a [Condition]. - `order_by`: Array of [FFIOrderByEntry]. - `offset`: Optional offset to set to the query. - `callback`: callback function. Takes the `context`, a pointer to a row and an [Error]. - `context`: Pass through void pointer. * ***Important**: - Make sure that `db`, `model`, `columns`, `joins` and `condition` are allocated until the callback is executed. * *This function is called from an asynchronous context. */ void rorm_db_query_one(const Database *db, Transaction *transaction, struct FFIString model, struct FFISlice_FFIColumnSelector columns, struct FFISlice_FFIJoin joins, const struct FFICondition *condition, struct FFISlice_FFIOrderByEntry order_by, struct FFIOption_u64 offset, void (*callback)(void*, Row*, struct Error), void* context); /** * This function queries the database given the provided parameter and returns one optional matched row. If no results could be retrieved, None is returned. * *To include the statement in a transaction specify `transaction` as a valid Transaction. As the Transaction needs to be mutable, it is important to not use the Transaction anywhere else until the callback is finished. * *If the statement should be executed **not** in a Transaction, specify a null pointer. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `model`: Name of the table to query. - `columns`: Array of columns to retrieve from the database. - `joins`: Array of joins to add to the query. - `condition`: Pointer to a [Condition]. - `order_by`: Array of [FFIOrderByEntry]. - `offset`: Optional offset to set to the query. - `callback`: callback function. Takes the `context`, a pointer to a row and an [Error]. - `context`: Pass through void pointer. * ***Important**: - Make sure that `db`, `model`, `columns`, `joins` and `condition` are allocated until the callback is executed. * *This function is called from an asynchronous context. */ void rorm_db_query_optional(const Database *db, Transaction *transaction, struct FFIString model, struct FFISlice_FFIColumnSelector columns, struct FFISlice_FFIJoin joins, const struct FFICondition *condition, struct FFISlice_FFIOrderByEntry order_by, struct FFIOption_u64 offset, void (*callback)(void*, Row*, struct Error), void* context); /** * This function queries the database given the provided parameter. * *Returns a pointer to the created stream. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `model`: Name of the table to query. - `columns`: Array of columns to retrieve from the database. - `joins`: Array of joins to add to the query. - `condition`: Pointer to a [Condition]. - `order_by`: Array of [FFIOrderByEntry]. - `limit`: Optional limit / offset to set to the query. - `callback`: callback function. Takes the `context`, a stream pointer and an [Error]. - `context`: Pass through void pointer. * *This function is called completely synchronously. */ void rorm_db_query_stream(const Database *db, Transaction *transaction, struct FFIString model, struct FFISlice_FFIColumnSelector columns, struct FFISlice_FFIJoin joins, const struct FFICondition *condition, struct FFISlice_FFIOrderByEntry order_by, struct FFIOption_FFILimitClause limit, void (*callback)(void*, Stream*, struct Error), void* context); /** * This function executes a raw SQL statement. * *Statements are executed as prepared statements, if possible. * *To define placeholders, use `?` in SQLite and MySQL and $1, $n in Postgres. The corresponding parameters are bound in order to the query. * *The number of placeholder must match with the number of provided bind parameters. * *To include the statement in a transaction specify `transaction` as a valid Transaction. As the Transaction needs to be mutable, it is important to not use the Transaction anywhere else until the callback is finished. * *If the statement should be executed **not** in a Transaction, specify a null pointer. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `query_string`: SQL statement to execute. - `bind_params`: Slice of FFIValues to bind to the query. - `callback`: callback function. Takes the `context`, a pointer to a slice of rows and an [Error]. - `context`: Pass through void pointer. * ***Important**: - Make sure `db`, `query_string` and `bind_params` are allocated until the callback was executed. - The FFISlice returned in the callback is freed after the callback has ended. * *This function is called from an asynchronous context. */ void rorm_db_raw_sql(const Database *db, Transaction *transaction, struct FFIString query_string, struct FFISlice_FFIValue bind_params, void (*callback)(void*, struct FFISlice_Row, struct Error), void* context); /** * Starts a transaction on the current database connection. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `callback`: callback function. Takes the `context`, a pointer to a transaction and an [Error]. - `context`: Pass through void pointer. * ***Important**: Rust does not manage the memory of the transaction. To properly free it, use [rorm_transaction_commit] or [rorm_transaction_abort]. * *This function is called from an asynchronous context. */ void rorm_db_start_transaction(const Database *db, void (*callback)(void*, Transaction*, struct Error), void* context); /** * This function updates rows in the database. * ***Parameter**: - `db`: Reference to the Database, provided by [rorm_db_connect]. - `transaction`: Mutable pointer to a Transaction. Can be a null pointer to ignore this parameter. - `model`: Name of the table to query. - `updates`: List of [FFIUpdate] to apply. - `condition`: Pointer to a [Condition]. - `callback`: callback function. Takes the `context`, the rows affected and an [Error]. - `context`: Pass through void pointer. * ***Important**: - Make sure that `db`, `model`, `updates` and `condition` are allocated until the callback is executed. * *This function is called from an asynchronous context. */ void rorm_db_update(const Database *db, Transaction *transaction, struct FFIString model, struct FFISlice_FFIUpdate updates, const struct FFICondition *condition, void (*callback)(void*, uint64_t, struct Error), void* context); /** * Frees the row given as parameter. * *The function panics if the provided pointer is invalid. * ***Important**: Do not call this function on the same pointer more than once! * *This function is called completely synchronously. */ void rorm_row_free(Row*); /** * Tries to retrieve an FFISlice of a u8 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFISlice_u8 rorm_row_get_binary(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a bool from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ bool rorm_row_get_bool(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a FFIDate from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIDate rorm_row_get_date(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a FFIDateTime from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIDateTime rorm_row_get_datetime(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve an f32 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ float rorm_row_get_f32(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve an f64 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ double rorm_row_get_f64(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve an i16 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ int16_t rorm_row_get_i16(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve an i32 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ int32_t rorm_row_get_i32(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve an i64 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ int64_t rorm_row_get_i64(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable FFISlice of a u8 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_FFISlice_u8 rorm_row_get_null_binary(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable bool from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_bool rorm_row_get_null_bool(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable FFIDate from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_FFIDate rorm_row_get_null_date(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable FFIDate from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_FFIDateTime rorm_row_get_null_datetime(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable f32 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_f32 rorm_row_get_null_f32(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable f64 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_f64 rorm_row_get_null_f64(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable i16 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_i16 rorm_row_get_null_i16(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable i32 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_i32 rorm_row_get_null_i32(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable i64 from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_i64 rorm_row_get_null_i64(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable FFIString from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_FFIString rorm_row_get_null_str(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a nullable FFITime from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIOption_FFITime rorm_row_get_null_time(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve an FFIString from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFIString rorm_row_get_str(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Tries to retrieve a FFITime from the given row pointer. * ***Parameter**: - `row_ptr`: Pointer to a row. - `index`: Name of the column to retrieve from the row. - `error_ptr`: Pointer to an [Error]. Gets only written to if an error occurs. * *This function is called completely synchronously. */ struct FFITime rorm_row_get_time(const Row *row_ptr, struct FFIString index, struct Error *error_ptr); /** * Shutdown the runtime. * *Specify the amount of time to wait in milliseconds. * *If no runtime is currently existing, a [Error::MissingRuntimeError] will be returned. If the runtime could not be locked, a [Error::RuntimeError] containing further information will be returned. * *This function is called completely synchronously. */ void rorm_runtime_shutdown(uint64_t duration, void (*callback)(void*, struct Error), void* context); /** * This function is used to initialize and start the async runtime. * *It is needed as rorm is completely written asynchronously. * ***Important**: Do not forget to stop the runtime using [rorm_runtime_shutdown]! * *This function is called completely synchronously. */ void rorm_runtime_start(void (*callback)(void*, struct Error), void* context); /** * Frees the stream given as parameter. * *This function panics if the pointer to the stream is invalid. * ***Important**: Do not call this function more than once! * *This function is called completely synchronously. */ void rorm_stream_free(Stream*); /** * Use this function to retrieve a pointer to a row on a stream. * ***Parameter**: - `stream_ptr`: Mutable pointer to the stream that is obtained from [rorm_db_query_stream]. - `callback`: callback function. Takes the `context`, a row pointer and a [Error]. - `context`: Pass through void pointer * ***Important**: - Do not call this function multiple times on the same stream, unless all given callbacks have returned successfully. Calling the function multiple times on the same stream will result in undefined behaviour! - Do not call this function on the same stream if the previous call returned a [Error::NoRowsLeftInStream]. - Do not use pass the stream to another function unless the callback of the current call is finished * *This function is called from an asynchronous context. */ void rorm_stream_get_row(Stream *stream_ptr, void (*callback)(void*, Row*, struct Error), void* context); /** * Commits a transaction. * *All previous operations will be applied to the database. * ***Parameter**: - `transaction`: Pointer to a valid transaction, provided by [rorm_db_start_transaction]. - `callback`: callback function. Takes the `context` and an [Error]. - `context`: Pass through void pointer. * ***Important**: Rust takes ownership of `transaction` and frees it after using. Don't use it anywhere else after calling this function! * *This function is called from an asynchronous context. */ void rorm_transaction_commit(Transaction *transaction, void (*callback)(void*, struct Error), void* context); /** * Rollback a transaction and abort it. * *All previous operations will be discarded. * ***Parameter**: - `transaction`: Pointer to a valid transaction, provided by [rorm_db_start_transaction]. - `callback`: callback function. Takes the `context` and an [Error]. - `context`: Pass through void pointer. * ***Important**: Rust takes ownership of `transaction` and frees it after using. Don't use it anywhere else after calling this function! * *This function is called from an asynchronous context. */ void rorm_transaction_rollback(Transaction *transaction, void (*callback)(void*, struct Error), void* context);