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