src/evsql.h
branchnew-evsql
changeset 44 9e76ee9729b6
parent 43 5776ace903b5
child 45 424ce5ab82fd
equal deleted inserted replaced
43:5776ace903b5 44:9e76ee9729b6
     7 
     7 
     8 // XXX: remove libpq?
     8 // XXX: remove libpq?
     9 #include <stdint.h>
     9 #include <stdint.h>
    10 #include <postgresql/libpq-fe.h>
    10 #include <postgresql/libpq-fe.h>
    11 #include <event2/event.h>
    11 #include <event2/event.h>
       
    12 
       
    13 #include <lib/err.h>
    12 
    14 
    13 /*
    15 /*
    14  * The generic context handle
    16  * The generic context handle
    15  */
    17  */
    16 struct evsql;
    18 struct evsql;
    60     EVSQL_TYPE_UINT16,
    62     EVSQL_TYPE_UINT16,
    61     EVSQL_TYPE_UINT32,
    63     EVSQL_TYPE_UINT32,
    62     EVSQL_TYPE_UINT64,
    64     EVSQL_TYPE_UINT64,
    63 };
    65 };
    64 
    66 
    65 struct evsql_item {
    67 struct evsql_item_binary {
       
    68     const char *ptr;
       
    69     size_t len;
       
    70 };
       
    71 
       
    72 /*
       
    73  * Metadata about the type of an item
       
    74  */
       
    75 struct evsql_item_info {
    66     // format
    76     // format
    67     enum evsql_item_format format;
    77     enum evsql_item_format format;
    68     
    78     
    69     // type
    79     // type
    70     enum evsql_item_type type;
    80     enum evsql_item_type type;
       
    81 
       
    82     // flags
       
    83     struct evsql_item_flags {
       
    84         uint8_t null_ok : 1;
       
    85     } flags;
       
    86 };
       
    87 
       
    88 /*
       
    89  * The type and value of an item
       
    90  */
       
    91 struct evsql_item {
       
    92     // "header"
       
    93     struct evsql_item_info info;
    71 
    94 
    72     // pointer to the raw databytes. Set to NULL to indicate SQL-NULL
    95     // pointer to the raw databytes. Set to NULL to indicate SQL-NULL
    73     const char *bytes;
    96     const char *bytes;
    74 
    97 
    75     // size of byte array pointed to by bytes, zero for text
    98     // size of byte array pointed to by bytes, zero for text
    83     } value;
   106     } value;
    84 };
   107 };
    85 
   108 
    86 /*
   109 /*
    87  * Query info, similar to prepared statements
   110  * Query info, similar to prepared statements
       
   111  *
       
   112  * Contains the literal SQL query and the types of the arguments
    88  */
   113  */
    89 struct evsql_query_info {
   114 struct evsql_query_info {
    90     // the SQL query itself
   115     // the SQL query itself
    91     const char *sql;
   116     const char *sql;
    92 
   117 
    93     // the list of items
   118     // the list of items
    94     struct evsql_item params[];
   119     struct evsql_item_info params[];
       
   120 };
       
   121 
       
   122 /*
       
   123  * Contains the query parameter types and their values
       
   124  */
       
   125 struct evsql_query_params {
       
   126     // result format
       
   127     enum evsql_item_format result_format;
       
   128     
       
   129     // list of params
       
   130     struct evsql_item list[];
    95 };
   131 };
    96 
   132 
    97 /*
   133 /*
    98  * Result info
   134  * Result info
       
   135  *
       
   136  * Contains the types of the result columns
    99  */
   137  */
   100 struct evsql_result_info {
   138 struct evsql_result_info {
   101     int padding;
   139     // XXX: put something useful here?
       
   140     int _unused;
   102 
   141 
   103     // the list of fields
   142     // the list of fields
   104     struct evsql_item columns[];
   143     struct evsql_item_info columns[];
   105 };
       
   106 
       
   107 
       
   108 /*
       
   109  * Result type
       
   110  */
       
   111 struct evsql_result_info {
       
   112     struct evsql *evsql;
       
   113     struct evsql_trans *trans;
       
   114     
       
   115     int error;
       
   116 
       
   117     union evsql_result {
       
   118         // libpq
       
   119         PGresult *pq;
       
   120     } result;
       
   121 };
   144 };
   122 
   145 
   123 /*
   146 /*
   124  * Callback for handling query results.
   147  * Callback for handling query results.
   125  *
   148  *
   126  * The query has completed, either succesfully or unsuccesfully (nonzero .error).
   149  * The query has completed, either succesfully or unsuccesfully (nonzero .error).
   127  *
   150  *
   128  * Use the evsql_result_* functions to manipulate the results.
   151  * Use the evsql_result_* functions to manipulate the results.
   129  */
   152  */
   130 typedef void (*evsql_query_cb)(const struct evsql_result_info *res, void *arg);
   153 typedef void (*evsql_query_cb)(struct evsql_result *res, void *arg);
   131 
   154 
   132 /*
   155 /*
   133  * Callback for handling global-level errors.
   156  * Callback for handling global-level errors.
   134  *
   157  *
   135  * The evsql is not useable anymore.
   158  * The evsql is not useable anymore.
   185 struct evsql_query *evsql_query (struct evsql *evsql, struct evsql_trans *trans, const char *command, evsql_query_cb query_fn, void *cb_arg);
   208 struct evsql_query *evsql_query (struct evsql *evsql, struct evsql_trans *trans, const char *command, evsql_query_cb query_fn, void *cb_arg);
   186 
   209 
   187 /*
   210 /*
   188  * Same as evsql_query, but uses the SQL-level support for binding parameters.
   211  * Same as evsql_query, but uses the SQL-level support for binding parameters.
   189  */
   212  */
   190 struct evsql_query *evsql_query_params (struct evsql *evsql, struct evsql_trans *trans, const char *command, const struct evsql_query_params *params, evsql_query_cb query_fn, void *cb_arg);
   213 struct evsql_query *evsql_query_params (struct evsql *evsql, struct evsql_trans *trans, 
       
   214     const char *command, const struct evsql_query_params *params, 
       
   215     evsql_query_cb query_fn, void *cb_arg
       
   216 );
       
   217 
       
   218 /*
       
   219  * Execute the given query_info, using the parameter list in query_info to resolve the given variable arugments
       
   220  */
       
   221 struct evsql_query *evsql_query_exec (struct evsql *evsql, struct evsql_trans *trans, 
       
   222     const struct evsql_query_info *query_info,
       
   223     evsql_query_cb query_fn, void *cb_arg,
       
   224     ...
       
   225 );
   191 
   226 
   192 /*
   227 /*
   193  * Abort a query, the query callback will not be called, the query and any possible results will be discarded.
   228  * Abort a query, the query callback will not be called, the query and any possible results will be discarded.
   194  *
   229  *
   195  * This does not garuntee that the query will not execute, simply that you won't get the results.
   230  * This does not garuntee that the query will not execute, simply that you won't get the results.
   244 /*
   279 /*
   245  * Result-handling functions
   280  * Result-handling functions
   246  */
   281  */
   247 
   282 
   248 // get error message associated with function
   283 // get error message associated with function
   249 const char *evsql_result_error (const struct evsql_result_info *res);
   284 const char *evsql_result_error (const struct evsql_result *res);
       
   285 
       
   286 /*
       
   287  * Iterator-based interface.
       
   288  *
       
   289  * Call result_begin to check for errors, then result_next to fetch rows, and finally result_end to release.
       
   290  */
       
   291 err_t evsql_result_begin (struct evsql_result_info *info, struct evsql_result *res);
       
   292 int evsql_result_next (struct evsql_result *res, ...);
       
   293 void evsql_result_end (struct evsql_result *res);
   250 
   294 
   251 // number of rows in the result
   295 // number of rows in the result
   252 size_t evsql_result_rows (const struct evsql_result_info *res);
   296 size_t evsql_result_rows (const struct evsql_result *res);
   253 
   297 
   254 // number of columns in the result
   298 // number of columns in the result
   255 size_t evsql_result_cols (const struct evsql_result_info *res);
   299 size_t evsql_result_cols (const struct evsql_result *res);
   256 
   300 
   257 // number of affected rows for UPDATE/INSERT
   301 // number of affected rows for UPDATE/INSERT
   258 size_t evsql_result_affected (const struct evsql_result_info *res);
   302 size_t evsql_result_affected (const struct evsql_result *res);
   259 
   303 
   260 // fetch the raw binary value from a result set, and return it via ptr
   304 // fetch the raw binary value from a result set, and return it via ptr
   261 // if size is nonzero, check that the size of the field data matches
   305 // if size is nonzero, check that the size of the field data matches
   262 int evsql_result_binary (const struct evsql_result_info *res, size_t row, size_t col, const char **ptr, size_t *size, int nullok);
   306 int evsql_result_binary (const struct evsql_result *res, size_t row, size_t col, const char **ptr, size_t *size, int nullok);
   263 int evsql_result_string (const struct evsql_result_info *res, size_t row, size_t col, const char **ptr, int nullok);
   307 int evsql_result_string (const struct evsql_result *res, size_t row, size_t col, const char **ptr, int nullok);
   264 
   308 
   265 // fetch certain kinds of values from a binary result set
   309 // fetch certain kinds of values from a binary result set
   266 int evsql_result_uint16 (const struct evsql_result_info *res, size_t row, size_t col, uint16_t *uval, int nullok);
   310 int evsql_result_uint16 (const struct evsql_result *res, size_t row, size_t col, uint16_t *uval, int nullok);
   267 int evsql_result_uint32 (const struct evsql_result_info *res, size_t row, size_t col, uint32_t *uval, int nullok);
   311 int evsql_result_uint32 (const struct evsql_result *res, size_t row, size_t col, uint32_t *uval, int nullok);
   268 int evsql_result_uint64 (const struct evsql_result_info *res, size_t row, size_t col, uint64_t *uval, int nullok);
   312 int evsql_result_uint64 (const struct evsql_result *res, size_t row, size_t col, uint64_t *uval, int nullok);
   269 
   313 
   270 // release the result set, freeing its memory
   314 // release the result set, freeing its memory
   271 void evsql_result_free (const struct evsql_result_info *res);
   315 void evsql_result_free (struct evsql_result *res);
   272 
   316 
   273 /*
   317 /*
   274  * Close a connection. Callbacks for waiting queries will not be run.
   318  * Close a connection. Callbacks for waiting queries will not be run.
   275  *
   319  *
   276  * XXX: not implemented yet.
   320  * XXX: not implemented yet.