1 #ifndef DB_H
    2 #define DB_H
    3 
    4 /*
    5  * WARNING: automatically generated by ort_lang_c_header 0.12.10.
    6  * DO NOT EDIT!
    7  */
    8 
    9 #ifndef KWBP_VERSION
   10 # define KWBP_VERSION "0.12.10"
   11 #endif
   12 #ifndef KWBP_VSTAMP
   13 # define KWBP_VSTAMP 11311
   14 #endif
   15 
   16 /*
   17  * Birthsex of individual
   18  */
   19 enum	sex {
   20 	/* Male */
   21 	SEX_male = 0,
   22 	/* Female */
   23 	SEX_female = 1,
   24 	/* Other */
   25 	SEX_other = 2
   26 };
   27 
   28 /*
   29  * Controlling organisation.
   30  */
   31 struct	company {
   32 	/* Name of the organisation. */
   33 	char	*name;
   34 	int64_t	 id;
   35 	/* Simply a check for null values. */
   36 	int64_t	 somenum;
   37 	/* Non-zero if "somenum" field is null/unset. */
   38 	int has_somenum;
   39 	TAILQ_ENTRY(company) _entries;
   40 };
   41 
   42 /*
   43  * Queue of company for listings.
   44  */
   45 TAILQ_HEAD(company_q, company);
   46 
   47 /*
   48  * A regular user.
   49  */
   50 struct	user {
   51 	/*
   52 	 * This struct will be filled in from an inner join
   53 	 *     on the "cid" variable.
   54 	 */
   55 	struct company company;
   56 	/* A foreign key reference. */
   57 	int64_t	 cid;
   58 	/* User's birth sex. */
   59 	enum sex sex;
   60 	/*
   61 	 * Password hash.
   62 	 *     This is passed to inserts and updates as a password,
   63 	 *     then hashed within the implementation and extracted
   64 	 *     (in listings and searches) as the hash value.
   65 	 */
   66 	char	*hash;
   67 	/* Unique e-mail address. */
   68 	char	*email;
   69 	/* A PNG image or something. */
   70 	void	*image;
   71 	size_t	 image_sz;
   72 	/* User's full name. */
   73 	char	*name;
   74 	int64_t	 uid;
   75 	/* Non-zero if "image" field is null/unset. */
   76 	int has_image;
   77 };
   78 
   79 /*
   80  * Callback of user for iteration.
   81  * The arg parameter is the opaque pointer passed into the iterate 
   82  * function.
   83  */
   84 typedef void (*user_cb)(const struct user *v, void *arg);
   85 
   86 /*
   87  * Authenticated session.
   88  */
   89 struct	session {
   90 	struct user user;
   91 	/* Associated user. */
   92 	int64_t	 userid;
   93 	/* Random cookie. */
   94 	int64_t	 token;
   95 	time_t	 mtime;
   96 	int64_t	 id;
   97 };
   98 
   99 /*
  100  * Callback of session for iteration.
  101  * The arg parameter is the opaque pointer passed into the iterate 
  102  * function.
  103  */
  104 typedef void (*session_cb)(const struct session *v, void *arg);
  105 
  106 __BEGIN_DECLS
  107 
  108 /*
  109  * Forward declaration of opaque pointer.
  110  */
  111 struct ort;
  112 
  113 /*
  114  * Set the argument given to the logging function specified to 
  115  * db_open_logging().
  116  * Has no effect if no logging function has been set.
  117  * The buffer is copied into a child process, so serialised objects may 
  118  * not have any pointers in the current address space or they will fail 
  119  * (at best).
  120  * Set length to zero to unset the logging function callback argument.
  121  */
  122 void db_logging_data(struct ort *ort, const void *arg, size_t sz);
  123 
  124 /*
  125  * Allocate and open the database in "file".
  126  * Returns an opaque pointer or NULL on memory exhaustion.
  127  * The returned pointer must be closed with db_close().
  128  * See db_open_logging() for the equivalent function that accepts logging 
  129  * callbacks.
  130  * This function starts a child with fork(), the child of which opens the 
  131  * database, so a constraint environment (e.g., with pledge) must take 
  132  * this into account.
  133  * Subsequent this function, all database operations take place over IPC.
  134  */
  135 struct ort *db_open(const char *file);
  136 
  137 /*
  138  * Like db_open() but accepts a function for logging.
  139  * If both are provided, the "long" form overrides the "short" form.
  140  * The logging function is run both in a child and parent process, so it 
  141  * must not have side effects.
  142  * The optional pointer is passed to the long form logging function and 
  143  * is inherited by the child process as-is, without being copied by 
  144  * value.
  145  * See db_logging_data() to set the pointer after initialisation.
  146  */
  147 struct ort *db_open_logging(const char *file,
  148 	void (*log)(const char *, void *),
  149 	void (*log_short)(const char *, ...), void *log_arg);
  150 
  151 /*
  152  * Open a transaction with identifier "id".
  153  * If "mode" is 0, the transaction is opened in "deferred" mode, meaning 
  154  * that the database is read-locked (no writes allowed) on the first read 
  155  * operation, and write-locked on the first write (only the current 
  156  * process can write).
  157  * If "mode" is >0, the transaction immediately starts a write-lock.
  158  * If "mode" is <0, the transaction starts in a write-pending, where no 
  159  * other locks can be held at the same time.
  160  * The DB_TRANS_OPEN_IMMEDIATE, DB_TRANS_OPEN_DEFERRED, and 
  161  * DB_TRANS_OPEN_EXCLUSIVE macros accomplish the same but with the "mode" 
  162  * being explicit in the name and not needing to be specified.
  163  */
  164 void db_trans_open(struct ort *ctx, size_t id, int mode);
  165 
  166 #define DB_TRANS_OPEN_IMMEDIATE(_ctx, _id) \
  167 	db_trans_open((_ctx), (_id), 1)
  168 #define DB_TRANS_OPEN_DEFERRED(_ctx, _id)\
  169 	db_trans_open((_ctx), (_id), 0)
  170 #define DB_TRANS_OPEN_EXCLUSIVE(_ctx, _id)\
  171 	db_trans_open((_ctx), (_id), -1)
  172 
  173 /*
  174  * Roll-back an open transaction.
  175  */
  176 void db_trans_rollback(struct ort *ctx, size_t id);
  177 
  178 /*
  179  * Commit an open transaction.
  180  */
  181 void db_trans_commit(struct ort *ctx, size_t id);
  182 
  183 /*
  184  * Close the context opened by db_open().
  185  * Has no effect if "p" is NULL.
  186  */
  187 void db_close(struct ort *p);
  188 
  189 /*
  190  * Clear resources and free "p".
  191  * Has no effect if "p" is NULL.
  192  */
  193 void db_company_free(struct company *p);
  194 
  195 /*
  196  * Unfill and free all queue members.
  197  * Has no effect if "q" is NULL.
  198  */
  199 void db_company_freeq(struct company_q *q);
  200 
  201 /*
  202  * Insert a new row into the database.
  203  * Only native (and non-rowid) fields may be set.
  204  * 	v1: name
  205  * 	v2: somenum
  206  * Returns the new row's identifier on success or <0 otherwise.
  207  */
  208 int64_t db_company_insert(struct ort *ctx, const char *v1, int64_t *v2);
  209 
  210 /*
  211  * Search for a set of company.
  212  * Queries on the following fields in struct company:
  213  * 	somenum (not an argument: checked is null)
  214  * Always returns a queue pointer.
  215  * Free this with db_company_freeq().
  216  */
  217 struct company_q *db_company_list_by_somenum_isnull(struct ort *ctx);
  218 /*
  219  * Delete fields in struct company.
  220  *  * Constraint fields:
  221  * Returns zero on constraint violation, non-zero on success.
  222  */
  223 void db_company_delete(struct ort *ctx);
  224 /*
  225  * Clear resources and free "p".
  226  * Has no effect if "p" is NULL.
  227  */
  228 void db_user_free(struct user *p);
  229 
  230 /*
  231  * Insert a new row into the database.
  232  * Only native (and non-rowid) fields may be set.
  233  * 	v1: cid
  234  * 	v2: sex
  235  * 	v3: hash (pre-hashed password)
  236  * 	v4: email
  237  * 	v5: image
  238  * 	v6: name
  239  * Returns the new row's identifier on success or <0 otherwise.
  240  */
  241 int64_t db_user_insert(struct ort *ctx, int64_t v1, enum sex v2, const char *v3,
  242      const char *v4,
  243      size_t v5_sz, const void **v5,
  244      const char *v6);
  245 
  246 /*
  247  * Create a function that searches for users by a given
  248  *     name; and, when found, invokes a callback function
  249  *     provided the user structure.
  250  * This callback function is called during an implicit transaction: thus, 
  251  * it should not invoke any database modifications or risk deadlock.
  252  * Queries on the following fields in struct user:
  253  * 	v1: name (equals)
  254  * Invokes the given callback with retrieved data.
  255  */
  256 void db_user_iterate_by_name_eq(struct ort *ctx, user_cb cb, void *arg, const char *v1);
  257 /*
  258  * Search for a unique user with their e-mail and
  259  *     password.
  260  *     This is a quick way to verify that a user has entered
  261  *     the correct password for logging in.
  262  * Queries on the following fields in struct user:
  263  * 	v1: email (equals)
  264  * 	v2: hash (pre-hashed password, equals)
  265  * Returns a pointer or NULL on fail.
  266  * Free the pointer with db_user_free().
  267  */
  268 struct user *db_user_get_creds(struct ort *ctx, const char *v1, const char *v2);
  269 /*
  270  * Lookup by unique identifier.
  271  * Queries on the following fields in struct user:
  272  * 	v1: uid (equals)
  273  * Returns a pointer or NULL on fail.
  274  * Free the pointer with db_user_free().
  275  */
  276 struct user *db_user_get_by_uid_eq(struct ort *ctx, int64_t v1);
  277 /*
  278  * Update fields in struct user.
  279  * Updated fields:
  280  * 	v1: hash (password)
  281  * Constraint fields:
  282  * 	v2: uid (equals)
  283  * Returns zero on constraint violation, non-zero on success.
  284  */
  285 int db_user_update_hash_set_by_uid_eq(struct ort *ctx, const char *v1, int64_t v2);
  286 /*
  287  * Update fields in struct user.
  288  * Updated fields:
  289  * 	v1: email
  290  * Constraint fields:
  291  * 	v2: uid (equals)
  292  * Returns zero on constraint violation, non-zero on success.
  293  */
  294 int db_user_update_email_set_by_uid_eq(struct ort *ctx, const char *v1, int64_t v2);
  295 /*
  296  * Delete fields in struct user.
  297  *  * Constraint fields:
  298  * Returns zero on constraint violation, non-zero on success.
  299  */
  300 void db_user_delete(struct ort *ctx);
  301 /*
  302  * Clear resources and free "p".
  303  * Has no effect if "p" is NULL.
  304  */
  305 void db_session_free(struct session *p);
  306 
  307 /*
  308  * Insert a new row into the database.
  309  * Only native (and non-rowid) fields may be set.
  310  * 	v1: userid
  311  * 	v2: token
  312  * 	v3: mtime
  313  * Returns the new row's identifier on success or <0 otherwise.
  314  */
  315 int64_t db_session_insert(struct ort *ctx, int64_t v1, int64_t v2, time_t v3);
  316 
  317 /*
  318  * Search for company's logged-in users.
  319  * This callback function is called during an implicit transaction: thus, 
  320  * it should not invoke any database modifications or risk deadlock.
  321  * Queries on the following fields in struct session:
  322  * 	v1: user.company.name (equals)
  323  * 	v2: mtime (equals)
  324  * Invokes the given callback with retrieved data.
  325  */
  326 void db_session_iterate_foo(struct ort *ctx, session_cb cb, void *arg, const char *v1,
  327      time_t v2);
  328 /*
  329  * Delete fields in struct session.
  330  *  * Constraint fields:
  331  * 	v1: id (equals)
  332  * Returns zero on constraint violation, non-zero on success.
  333  */
  334 void db_session_delete_by_id_eq(struct ort *ctx, int64_t v1);
  335 __END_DECLS
  336 
  337 #endif