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