1 #ifndef DB_H
    2 #define DB_H
    3 
    4 /*
    5  * WARNING: automatically generated by kwebapp 0.2.2.
    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  */
   80 struct ksql *db_open(const char *file);
   81 
   82 /*
   83  * Close the database opened by db_open().
   84  * Has no effect if "p" is NULL.
   85  */
   86 void db_close(struct ksql *p);
   87 
   88 /*
   89  * Unfill resources and free "p".
   90  * Has no effect if "p" is NULL.
   91  */
   92 void db_user_free(struct user *p);
   93 
   94 /*
   95  * Fill in a user from an open statement "stmt".
   96  * This starts grabbing results from "pos", which may be NULL to start from zero.
   97  * This follows DB_SCHEMA_USER's order for columns.
   98  */
   99 void db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos);
  100 
  101 /*
  102  * Insert a new row into the database.
  103  * Only native (and non-rowid) fields may be set.
  104  * 	v1: email
  105  * 	v2: hash (pre-hashed password)
  106  * Returns the new row's identifier on success or <0 otherwise.
  107  */
  108 int64_t db_user_insert(struct ksql *db, const char *v1, const char *v2);
  109 
  110 /*
  111  * Free memory allocated by db_user_fill().
  112  * Has not effect if "p" is NULL.
  113  */
  114 void db_user_unfill(struct user *p);
  115 
  116 /*
  117  * Search for a user by email/password.
  118  * 
  119  * Uses the given fields in struct user:
  120  * 	v1: email
  121  * 	v2: hash (pre-hashed password)
  122  * Returns a pointer or NULL on fail.
  123  * Free the pointer with db_user_free().
  124  */
  125 struct user *db_user_get_creds(struct ksql *db, const char *v1, const char *v2);
  126 
  127 /*
  128  * Print out the fields of a user in JSON including nested structures.
  129  * Omits any password entries or those marked "noexport".
  130  * See json_user_obj() for the full object.
  131  */
  132 void json_user_data(struct kjsonreq *r, const struct user *p);
  133 
  134 /*
  135  * Emit the JSON key-value pair for the object:
  136  * 	"user" : { [data]+ }
  137  * See json_user_data() for the data.
  138  */
  139 void json_user_obj(struct kjsonreq *r, const struct user *p);
  140 
  141 /*
  142  * Validation routines for the email field in struct user.
  143  */
  144 int valid_user_email(struct kpair *p);
  145 
  146 /*
  147  * Validation routines for the hash field in struct user.
  148  */
  149 int valid_user_hash(struct kpair *p);
  150 
  151 /*
  152  * Validation routines for the id field in struct user.
  153  */
  154 int valid_user_id(struct kpair *p);
  155 
  156 /*
  157  * Unfill resources and free "p".
  158  * Has no effect if "p" is NULL.
  159  */
  160 void db_sess_free(struct sess *p);
  161 
  162 /*
  163  * Fill in a sess from an open statement "stmt".
  164  * This starts grabbing results from "pos", which may be NULL to start from zero.
  165  * This follows DB_SCHEMA_SESS's order for columns.
  166  */
  167 void db_sess_fill(struct sess *p, struct ksqlstmt *stmt, size_t *pos);
  168 
  169 /*
  170  * Insert a new row into the database.
  171  * Only native (and non-rowid) fields may be set.
  172  * 	v1: userid
  173  * 	v2: token
  174  * Returns the new row's identifier on success or <0 otherwise.
  175  */
  176 int64_t db_sess_insert(struct ksql *db, int64_t v1, int64_t v2);
  177 
  178 /*
  179  * Free memory allocated by db_sess_fill().
  180  * Has not effect if "p" is NULL.
  181  */
  182 void db_sess_unfill(struct sess *p);
  183 
  184 /*
  185  * Search by token and identifier.
  186  * 
  187  * Uses the given fields in struct sess:
  188  * 	v1: token
  189  * 	v2: id
  190  * Returns a pointer or NULL on fail.
  191  * Free the pointer with db_sess_free().
  192  */
  193 struct sess *db_sess_get_creds(struct ksql *db, int64_t v1, int64_t v2);
  194 
  195 /*
  196  * Delete session.
  197  * 
  198  * Constrains the deleted records to:
  199  * 	v1: id
  200  * Returns zero on failure, non-zero on constraint errors.
  201  */
  202 int db_sess_delete_id(struct ksql *db, int64_t v1);
  203 
  204 /*
  205  * Print out the fields of a sess in JSON including nested structures.
  206  * Omits any password entries or those marked "noexport".
  207  * See json_sess_obj() for the full object.
  208  */
  209 void json_sess_data(struct kjsonreq *r, const struct sess *p);
  210 
  211 /*
  212  * Emit the JSON key-value pair for the object:
  213  * 	"sess" : { [data]+ }
  214  * See json_sess_data() for the data.
  215  */
  216 void json_sess_obj(struct kjsonreq *r, const struct sess *p);
  217 
  218 /*
  219  * Validation routines for the user field in struct sess.
  220  */
  221 int valid_sess_user(struct kpair *p);
  222 
  223 /*
  224  * Validation routines for the userid field in struct sess.
  225  */
  226 int valid_sess_userid(struct kpair *p);
  227 
  228 /*
  229  * Validation routines for the token field in struct sess.
  230  */
  231 int valid_sess_token(struct kpair *p);
  232 
  233 /*
  234  * Validation routines for the id field in struct sess.
  235  */
  236 int valid_sess_id(struct kpair *p);
  237 
  238 __END_DECLS
  239 
  240 #endif