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