1 #ifndef DB_H
    2 #define DB_H
    3 
    4 /*
    5  * WARNING: automatically generated by kwebapp 0.2.12.
    6  * DO NOT EDIT!
    7  */
    8 
    9 /*
   10  * A user within our system.
   11  */
   12 struct	user {
   13 	/* E-mail address. */
   14 	char	*email;
   15 	/* Hashed password. */
   16 	char	*hash;
   17 	int64_t	 id;
   18 };
   19 
   20 /*
   21  * A logged-in user.
   22  */
   23 struct	sess {
   24 	/* User attached to session. */
   25 	struct user user;
   26 	/* User identifier attached to session. */
   27 	int64_t	 userid;
   28 	/* Random token for authentication. */
   29 	int64_t	 token;
   30 	int64_t	 id;
   31 };
   32 
   33 /*
   34  * Define our table columns.
   35  * Use these when creating your own SQL statements,
   36  * combined with the db_xxxx_fill functions.
   37  * Each macro must be given a unique alias name.
   38  * This allows for doing multiple inner joins on the same table.
   39  */
   40 #define DB_SCHEMA_USER(_x) \
   41 	#_x ".email" "," \
   42 	#_x ".hash" "," \
   43 	#_x ".id"
   44 #define DB_SCHEMA_SESS(_x) \
   45 	#_x ".userid" "," \
   46 	#_x ".token" "," \
   47 	#_x ".id"
   48 
   49 /*
   50  * All of the fields we validate.
   51  * These are as VALID_XXX_YYY, where XXX is the structure and YYY is the field.
   52  * Only native types are listed.
   53  */
   54 enum	valid_keys {
   55 	VALID_USER_EMAIL,
   56 	VALID_USER_HASH,
   57 	VALID_USER_ID,
   58 	VALID_SESS_USERID,
   59 	VALID_SESS_TOKEN,
   60 	VALID_SESS_ID,
   61 	VALID__MAX
   62 };
   63 
   64 /*
   65  * Validation fields.
   66  * Pass this directly into khttp_parse(3) to use them as-is.
   67  * The functions are "valid_xxx_yyy", where "xxx" is the struct and "yyy" the field, and can be used standalone.
   68  * The form inputs are named "xxx-yyy".
   69  */
   70 extern const struct kvalid valid_keys[VALID__MAX];
   71 
   72 __BEGIN_DECLS
   73 
   74 /*
   75  * Allocate and open the database in "file".
   76  * This returns a pointer to the database in "safe exit" mode (see ksql(3)).
   77  * It returns NULL on memory allocation failure.
   78  * The returned pointer must be closed with db_close().
   79  * Note: if you're using a sandbox, you must
   80  * accommodate for the SQLite database within
   81  * process memory.
   82  */
   83 struct ksql *db_open(const char *file);
   84 
   85 /*
   86  * Close the database opened by db_open().
   87  * Has no effect if "p" is NULL.
   88  */
   89 void db_close(struct ksql *p);
   90 
   91 /*
   92  * Unfill resources and free "p".
   93  * Has no effect if "p" is NULL.
   94  */
   95 void db_user_free(struct user *p);
   96 
   97 /*
   98  * Fill in a user from an open statement "stmt".
   99  * This starts grabbing results from "pos", which may be NULL to start from zero.
  100  * This follows DB_SCHEMA_USER's order for columns.
  101  */
  102 void db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos);
  103 
  104 /*
  105  * Insert a new row into the database.
  106  * Only native (and non-rowid) fields may be set.
  107  * 	v1: email
  108  * 	v2: hash (pre-hashed password)
  109  * Returns the new row's identifier on success or <0 otherwise.
  110  */
  111 int64_t db_user_insert(struct ksql *db, const char *v1, const char *v2);
  112 
  113 /*
  114  * Free memory allocated by db_user_fill().
  115  * Has not effect if "p" is NULL.
  116  */
  117 void db_user_unfill(struct user *p);
  118 
  119 /*
  120  * Search for a user by email/password.
  121  * 
  122  * Uses the given fields in struct user:
  123  * 	v1: email
  124  * 	v2: hash (pre-hashed password)
  125  * Returns a pointer or NULL on fail.
  126  * Free the pointer with db_user_free().
  127  */
  128 struct user *db_user_get_creds(struct ksql *db, const char *v1, const char *v2);
  129 
  130 /*
  131  * Print out the fields of a user in JSON including nested structures.
  132  * Omits any password entries or those marked "noexport".
  133  * See json_user_obj() for the full object.
  134  */
  135 void json_user_data(struct kjsonreq *r, const struct user *p);
  136 
  137 /*
  138  * Emit the JSON key-value pair for the object:
  139  * 	"user" : { [data]+ }
  140  * See json_user_data() for the data.
  141  */
  142 void json_user_obj(struct kjsonreq *r, const struct user *p);
  143 
  144 /*
  145  * Validation routines for the email field in struct user.
  146  */
  147 int valid_user_email(struct kpair *p);
  148 
  149 /*
  150  * Validation routines for the hash field in struct user.
  151  */
  152 int valid_user_hash(struct kpair *p);
  153 
  154 /*
  155  * Validation routines for the id field in struct user.
  156  */
  157 int valid_user_id(struct kpair *p);
  158 
  159 /*
  160  * Unfill resources and free "p".
  161  * Has no effect if "p" is NULL.
  162  */
  163 void db_sess_free(struct sess *p);
  164 
  165 /*
  166  * Fill in a sess from an open statement "stmt".
  167  * This starts grabbing results from "pos", which may be NULL to start from zero.
  168  * This follows DB_SCHEMA_SESS's order for columns.
  169  */
  170 void db_sess_fill(struct sess *p, struct ksqlstmt *stmt, size_t *pos);
  171 
  172 /*
  173  * Insert a new row into the database.
  174  * Only native (and non-rowid) fields may be set.
  175  * 	v1: userid
  176  * 	v2: token
  177  * Returns the new row's identifier on success or <0 otherwise.
  178  */
  179 int64_t db_sess_insert(struct ksql *db, int64_t v1, int64_t v2);
  180 
  181 /*
  182  * Free memory allocated by db_sess_fill().
  183  * Has not effect if "p" is NULL.
  184  */
  185 void db_sess_unfill(struct sess *p);
  186 
  187 /*
  188  * Search by token and identifier.
  189  * 
  190  * Uses the given fields in struct sess:
  191  * 	v1: token
  192  * 	v2: id
  193  * Returns a pointer or NULL on fail.
  194  * Free the pointer with db_sess_free().
  195  */
  196 struct sess *db_sess_get_creds(struct ksql *db, int64_t v1, int64_t v2);
  197 
  198 /*
  199  * Delete session.
  200  * 
  201  * Constrains the deleted records to:
  202  * 	v1: id
  203  * Returns zero on failure, non-zero on constraint errors.
  204  */
  205 int db_sess_delete_id(struct ksql *db, int64_t v1);
  206 
  207 /*
  208  * Print out the fields of a sess in JSON including nested structures.
  209  * Omits any password entries or those marked "noexport".
  210  * See json_sess_obj() for the full object.
  211  */
  212 void json_sess_data(struct kjsonreq *r, const struct sess *p);
  213 
  214 /*
  215  * Emit the JSON key-value pair for the object:
  216  * 	"sess" : { [data]+ }
  217  * See json_sess_data() for the data.
  218  */
  219 void json_sess_obj(struct kjsonreq *r, const struct sess *p);
  220 
  221 /*
  222  * Validation routines for the user field in struct sess.
  223  */
  224 int valid_sess_user(struct kpair *p);
  225 
  226 /*
  227  * Validation routines for the userid field in struct sess.
  228  */
  229 int valid_sess_userid(struct kpair *p);
  230 
  231 /*
  232  * Validation routines for the token field in struct sess.
  233  */
  234 int valid_sess_token(struct kpair *p);
  235 
  236 /*
  237  * Validation routines for the id field in struct sess.
  238  */
  239 int valid_sess_id(struct kpair *p);
  240 
  241 __END_DECLS
  242 
  243 #endif