1 #ifndef DB_H
    2 #define DB_H
    3 
    4 /*
    5  * WARNING: automatically generated by ort-c-header 0.8.5.
    6  * DO NOT EDIT!
    7  */
    8 
    9 #ifndef KWBP_VERSION
   10 # define KWBP_VERSION "0.8.5"
   11 #endif
   12 #ifndef KWBP_VSTAMP
   13 # define KWBP_VSTAMP 10906
   14 #endif
   15 
   16 struct	user {
   17 	char	*email;
   18 	char	*hash;
   19 	int64_t	 id;
   20 };
   21 
   22 struct	session {
   23 	struct user user;
   24 	/* User associated with session. */
   25 	int64_t	 userid;
   26 	int64_t	 id;
   27 };
   28 
   29 /*
   30  * All of the fields we validate.
   31  * These are as VALID_XXX_YYY, where XXX is the structure and YYY is the 
   32  * field.
   33  * Only native types are listed.
   34  */
   35 enum	valid_keys {
   36 	VALID_USER_EMAIL,
   37 	VALID_USER_HASH,
   38 	VALID_USER_ID,
   39 	VALID_SESSION_USERID,
   40 	VALID_SESSION_ID,
   41 	VALID__MAX
   42 };
   43 
   44 /*
   45  * Validation fields.
   46  * Pass this directly into khttp_parse(3) to use them as-is.
   47  * The functions are "valid_xxx_yyy", where "xxx" is the struct and "yyy" 
   48  * the field, and can be used standalone.
   49  * The form inputs are named "xxx-yyy".
   50  */
   51 extern const struct kvalid valid_keys[VALID__MAX];
   52 
   53 __BEGIN_DECLS
   54 
   55 /*
   56  * Forward declaration of opaque pointer.
   57  */
   58 struct ort;
   59 
   60 /*
   61  * Set the argument given to the logging function specified to 
   62  * db_open_logging().
   63  * Has no effect if no logging function has been set.
   64  * The buffer is copied into a child process, so serialised objects may 
   65  * not have any pointers in the current address space or they will fail 
   66  * (at best).
   67  * Set length to zero to unset the logging function callback argument.
   68  */
   69 void db_logging_data(struct ort *ort, const void *arg, size_t sz);
   70 
   71 /*
   72  * Allocate and open the database in "file".
   73  * Returns an opaque pointer or NULL on memory exhaustion.
   74  * The returned pointer must be closed with db_close().
   75  * See db_open_logging() for the equivalent function that accepts logging 
   76  * callbacks.
   77  * This function starts a child with fork(), the child of which opens the 
   78  * database, so a constraint environment (e.g., with pledge) must take 
   79  * this into account.
   80  * Subsequent this function, all database operations take place over IPC.
   81  */
   82 struct ort *db_open(const char *file);
   83 
   84 /*
   85  * Like db_open() but accepts a function for logging.
   86  * If both are provided, the "long" form overrides the "short" form.
   87  * The logging function is run both in a child and parent process, so it 
   88  * must not have side effects.
   89  * The optional pointer is passed to the long form logging function and 
   90  * is inherited by the child process as-is, without being copied by 
   91  * value.
   92  * See db_logging_data() to set the pointer after initialisation.
   93  */
   94 struct ort *db_open_logging(const char *file,
   95 	void (*log)(const char *, void *),
   96 	void (*log_short)(const char *, ...), void *log_arg);
   97 
   98 /*
   99  * Open a transaction with identifier "id".
  100  * If "mode" is 0, the transaction is opened in "deferred" mode, meaning 
  101  * that the database is read-locked (no writes allowed) on the first read 
  102  * operation, and write-locked on the first write (only the current 
  103  * process can write).
  104  * If "mode" is >0, the transaction immediately starts a write-lock.
  105  * If "mode" is <0, the transaction starts in a write-pending, where no 
  106  * other locks can be held at the same time.
  107  * The DB_TRANS_OPEN_IMMEDIATE, DB_TRANS_OPEN_DEFERRED, and 
  108  * DB_TRANS_OPEN_EXCLUSIVE macros accomplish the same but with the "mode" 
  109  * being explicit in the name and not needing to be specified.
  110  */
  111 void db_trans_open(struct ort *ctx, size_t id, int mode);
  112 
  113 #define DB_TRANS_OPEN_IMMEDIATE(_ctx, _id) \
  114 	db_trans_open((_ctx), (_id), 1)
  115 #define DB_TRANS_OPEN_DEFERRED(_ctx, _id)\
  116 	db_trans_open((_ctx), (_id), 0)
  117 #define DB_TRANS_OPEN_EXCLUSIVE(_ctx, _id)\
  118 	db_trans_open((_ctx), (_id), -1)
  119 
  120 /*
  121  * Roll-back an open transaction.
  122  */
  123 void db_trans_rollback(struct ort *ctx, size_t id);
  124 
  125 /*
  126  * Commit an open transaction.
  127  */
  128 void db_trans_commit(struct ort *ctx, size_t id);
  129 
  130 /*
  131  * Close the context opened by db_open().
  132  * Has no effect if "p" is NULL.
  133  */
  134 void db_close(struct ort *p);
  135 
  136 /*
  137  * Clear resources and free "p".
  138  * Has no effect if "p" is NULL.
  139  */
  140 void db_user_free(struct user *p);
  141 
  142 /*
  143  * Lookup by credentials.
  144  * Queries on the following fields in struct user:
  145  * 	v1: email (equals)
  146  * 	v2: hash (pre-hashed password, equals)
  147  * Returns a pointer or NULL on fail.
  148  * Free the pointer with db_user_free().
  149  */
  150 struct user *db_user_get_creds(struct ort *ctx, const char *v1, const char *v2);
  151 
  152 /*
  153  * Clear resources and free "p".
  154  * Has no effect if "p" is NULL.
  155  */
  156 void db_session_free(struct session *p);
  157 
  158 /*
  159  * Insert a new row into the database.
  160  * Only native (and non-rowid) fields may be set.
  161  * 	v1: userid
  162  * Returns the new row's identifier on success or <0 otherwise.
  163  */
  164 int64_t db_session_insert(struct ort *ctx, int64_t v1);
  165 
  166 /*
  167  * Lookup by unique identifier.
  168  * Queries on the following fields in struct session:
  169  * 	v1: id (equals)
  170  * Returns a pointer or NULL on fail.
  171  * Free the pointer with db_session_free().
  172  */
  173 struct session *db_session_get_id(struct ort *ctx, int64_t v1);
  174 
  175 /*
  176  * Constrains the deleted records to:
  177  * 	v1: id (equals)
  178  * Returns zero on constraint violation, non-zero on success.
  179  */
  180 int db_session_delete_by_id_eq(struct ort *ctx, int64_t v1);
  181 
  182 /*
  183  * Print out the fields of a user in JSON including nested structures.
  184  * Omits any password entries or those marked "noexport".
  185  * See json_user_obj() for the full object.
  186  */
  187 void json_user_data(struct kjsonreq *r, const struct user *p);
  188 
  189 /*
  190  * Emit the JSON key-value pair for the object:
  191  * 	"user" : { [data]+ }
  192  * See json_user_data() for the data.
  193  */
  194 void json_user_obj(struct kjsonreq *r, const struct user *p);
  195 
  196 /*
  197  * Print out the fields of a session in JSON including nested structures.
  198  * Omits any password entries or those marked "noexport".
  199  * See json_session_obj() for the full object.
  200  */
  201 void json_session_data(struct kjsonreq *r, const struct session *p);
  202 
  203 /*
  204  * Emit the JSON key-value pair for the object:
  205  * 	"session" : { [data]+ }
  206  * See json_session_data() for the data.
  207  */
  208 void json_session_obj(struct kjsonreq *r, const struct session *p);
  209 
  210 /*
  211  * Validation routines for the email field in struct user.
  212  */
  213 int valid_user_email(struct kpair *p);
  214 
  215 /*
  216  * Validation routines for the hash field in struct user.
  217  */
  218 int valid_user_hash(struct kpair *p);
  219 
  220 /*
  221  * Validation routines for the id field in struct user.
  222  */
  223 int valid_user_id(struct kpair *p);
  224 
  225 /*
  226  * Validation routines for the user field in struct session.
  227  */
  228 int valid_session_user(struct kpair *p);
  229 
  230 /*
  231  * Validation routines for the userid field in struct session.
  232  */
  233 int valid_session_userid(struct kpair *p);
  234 
  235 /*
  236  * Validation routines for the id field in struct session.
  237  */
  238 int valid_session_id(struct kpair *p);
  239 
  240 __END_DECLS
  241 
  242 #endif