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