1 #ifndef DB_H
    2 #define DB_H
    3 
    4 /*
    5  * WARNING: automatically generated by kwebapp 0.3.1.
    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, combined with the 
   36  * 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 
   52  * field.
   53  * Only native types are listed.
   54  */
   55 enum	valid_keys {
   56 	VALID_USER_EMAIL,
   57 	VALID_USER_HASH,
   58 	VALID_USER_ID,
   59 	VALID_SESS_USERID,
   60 	VALID_SESS_TOKEN,
   61 	VALID_SESS_ID,
   62 	VALID__MAX
   63 };
   64 
   65 /*
   66  * Validation fields.
   67  * Pass this directly into khttp_parse(3) to use them as-is.
   68  * The functions are "valid_xxx_yyy", where "xxx" is the struct and "yyy" 
   69  * the field, and can be used standalone.
   70  * The form inputs are named "xxx-yyy".
   71  */
   72 extern const struct kvalid valid_keys[VALID__MAX];
   73 
   74 __BEGIN_DECLS
   75 
   76 /*
   77  * Allocate and open the database in "file". This opens the database in 
   78  * "safe exit" mode (see ksql(3)).
   79  * Note: if you're using a sandbox, you must accommodate for the SQLite 
   80  * database within process memory.
   81  * Returns a pointer to the database or NULL on memory exhaustion.
   82  * The returned pointer must be closed with db_close().
   83  */
   84 struct ksql *db_open(const char *file);
   85 
   86 /*
   87  * Close the context opened by db_open().
   88  * Has no effect if "p" is NULL.
   89  */
   90 void db_close(struct ksql *p);
   91 
   92 /*
   93  * Unfill resources and free "p".
   94  * Has no effect if "p" is NULL.
   95  */
   96 void db_user_free(struct user *p);
   97 
   98 /*
   99  * Fill in a user from an open statement "stmt".
  100  * This starts grabbing results from "pos", which may be NULL to start 
  101  * from zero.
  102  * This follows DB_SCHEMA_USER's order for columns.
  103  */
  104 void db_user_fill(struct user *p, struct ksqlstmt *stmt, size_t *pos);
  105 
  106 /*
  107  * Free memory allocated by db_user_fill().
  108  * Has not effect if "p" is NULL.
  109  */
  110 void db_user_unfill(struct user *p);
  111 
  112 /*
  113  * Search for a user by email/password.
  114  * Uses the given fields in struct user:
  115  * 	v1: email
  116  * 	v2: hash (pre-hashed password)
  117  * Returns a pointer or NULL on fail.
  118  * Free the pointer with db_user_free().
  119  */
  120 struct user *db_user_get_creds(struct ksql *db, const char *v1, const char *v2);
  121 
  122 /*
  123  * Print out the fields of a user in JSON including nested 
  124  * structures.
  125  * Omits any password entries or those marked "noexport".
  126  * See json_user_obj() for the full object.
  127  */
  128 void json_user_data(struct kjsonreq *r, const struct user *p);
  129 
  130 /*
  131  * Emit the JSON key-value pair for the object:
  132  * 	"user" : { [data]+ }
  133  * See json_user_data() for the data.
  134  */
  135 void json_user_obj(struct kjsonreq *r, const struct user *p);
  136 
  137 /*
  138  * Validation routines for the email field in struct user.
  139  */
  140 int valid_user_email(struct kpair *p);
  141 
  142 /*
  143  * Validation routines for the hash field in struct user.
  144  */
  145 int valid_user_hash(struct kpair *p);
  146 
  147 /*
  148  * Validation routines for the id field in struct user.
  149  */
  150 int valid_user_id(struct kpair *p);
  151 
  152 /*
  153  * Unfill resources and free "p".
  154  * Has no effect if "p" is NULL.
  155  */
  156 void db_sess_free(struct sess *p);
  157 
  158 /*
  159  * Fill in a sess from an open statement "stmt".
  160  * This starts grabbing results from "pos", which may be NULL to start 
  161  * from zero.
  162  * This follows DB_SCHEMA_SESS's order for columns.
  163  */
  164 void db_sess_fill(struct sess *p, struct ksqlstmt *stmt, size_t *pos);
  165 
  166 /*
  167  * Free memory allocated by db_sess_fill().
  168  * Has not effect if "p" is NULL.
  169  */
  170 void db_sess_unfill(struct sess *p);
  171 
  172 /*
  173  * Search by token and identifier.
  174  * Uses the given fields in struct sess:
  175  * 	v1: token
  176  * 	v2: id
  177  * Returns a pointer or NULL on fail.
  178  * Free the pointer with db_sess_free().
  179  */
  180 struct sess *db_sess_get_creds(struct ksql *db, int64_t v1, int64_t v2);
  181 
  182 /*
  183  * Delete session.
  184  * 
  185  * Constrains the deleted records to:
  186  * 	v1: id
  187  * Returns zero on failure, non-zero on constraint errors.
  188  */
  189 int db_sess_delete_id(struct ksql *db, int64_t v1);
  190 
  191 /*
  192  * Print out the fields of a sess in JSON including nested 
  193  * structures.
  194  * Omits any password entries or those marked "noexport".
  195  * See json_sess_obj() for the full object.
  196  */
  197 void json_sess_data(struct kjsonreq *r, const struct sess *p);
  198 
  199 /*
  200  * Emit the JSON key-value pair for the object:
  201  * 	"sess" : { [data]+ }
  202  * See json_sess_data() for the data.
  203  */
  204 void json_sess_obj(struct kjsonreq *r, const struct sess *p);
  205 
  206 /*
  207  * Validation routines for the user field in struct sess.
  208  */
  209 int valid_sess_user(struct kpair *p);
  210 
  211 /*
  212  * Validation routines for the userid field in struct sess.
  213  */
  214 int valid_sess_userid(struct kpair *p);
  215 
  216 /*
  217  * Validation routines for the token field in struct sess.
  218  */
  219 int valid_sess_token(struct kpair *p);
  220 
  221 /*
  222  * Validation routines for the id field in struct sess.
  223  */
  224 int valid_sess_id(struct kpair *p);
  225 
  226 __END_DECLS
  227 
  228 #endif