src/evsql.h
branchnew-evsql
changeset 53 0d6e07f4c9a1
parent 52 f5037572c326
child 54 7dc8ff496da1
equal deleted inserted replaced
52:f5037572c326 53:0d6e07f4c9a1
    63  */
    63  */
    64 struct evsql_result;
    64 struct evsql_result;
    65 
    65 
    66 /**
    66 /**
    67  * Various transaction isolation levels for conveniance
    67  * Various transaction isolation levels for conveniance
       
    68  *
       
    69  * @see evsql_trans
    68  */
    70  */
    69 enum evsql_trans_type {
    71 enum evsql_trans_type {
    70     EVSQL_TRANS_DEFAULT,
    72     EVSQL_TRANS_DEFAULT,
    71     EVSQL_TRANS_SERIALIZABLE,
    73     EVSQL_TRANS_SERIALIZABLE,
    72     EVSQL_TRANS_REPEATABLE_READ,
    74     EVSQL_TRANS_REPEATABLE_READ,
   145     } flags;
   147     } flags;
   146 };
   148 };
   147 
   149 
   148 /**
   150 /**
   149  * An union to provide storage for the values of small types
   151  * An union to provide storage for the values of small types
       
   152  *
       
   153  * @see evsql_item
   150  */
   154  */
   151 union evsql_item_value {
   155 union evsql_item_value {
   152     /** 16-bit unsigned integer */
   156     /** 16-bit unsigned integer */
   153     uint16_t uint16;
   157     uint16_t uint16;
   154 
   158 
   159     uint64_t uint64;
   163     uint64_t uint64;
   160 };
   164 };
   161 
   165 
   162 /**
   166 /**
   163  * A generic structure containing the type and value of a query parameter or a result field.
   167  * A generic structure containing the type and value of a query parameter or a result field.
       
   168  *
       
   169  * @see evsql_query_info
       
   170  * @see evsql_query_params
       
   171  * @see evsql_result_info
   164  */
   172  */
   165 struct evsql_item {
   173 struct evsql_item {
   166     /** The "header" containing the type and format */
   174     /** The "header" containing the type and format */
   167     struct evsql_item_info info;
   175     struct evsql_item_info info;
   168 
   176 
   193 
   201 
   194 /**
   202 /**
   195  * Query meta-info, similar to a prepared statement.
   203  * Query meta-info, similar to a prepared statement.
   196  *
   204  *
   197  * Contains the literal SQL query and the types of the parameters, but no more.
   205  * Contains the literal SQL query and the types of the parameters, but no more.
       
   206  *
       
   207  * @see evsql_query_exec
   198  */
   208  */
   199 struct evsql_query_info {
   209 struct evsql_query_info {
   200     /** The SQL query itself */
   210     /** The SQL query itself */
   201     const char *sql;
   211     const char *sql;
   202 
   212 
   206     struct evsql_item_info params[];
   216     struct evsql_item_info params[];
   207 };
   217 };
   208 
   218 
   209 /**
   219 /**
   210  * Contains the query parameter types and their actual values
   220  * Contains the query parameter types and their actual values
       
   221  *
       
   222  * @see evsql_query_params
   211  */
   223  */
   212 struct evsql_query_params {
   224 struct evsql_query_params {
   213     /** Requested result format for this query. XXX: move elsewhere */
   225     /** Requested result format for this query. XXX: move elsewhere */
   214     enum evsql_item_format result_format;
   226     enum evsql_item_format result_format;
   215     
   227     
   219     struct evsql_item list[];
   231     struct evsql_item list[];
   220 };
   232 };
   221 
   233 
   222 /**
   234 /**
   223  * Result layout metadata. This contains the stucture needed to decode result rows.
   235  * Result layout metadata. This contains the stucture needed to decode result rows.
       
   236  *
       
   237  * @see evsql_result_begin
   224  */
   238  */
   225 struct evsql_result_info {
   239 struct evsql_result_info {
   226     /** XXX: make up something useful to stick here */
   240     /** XXX: make up something useful to stick here */
   227     int _unused;
   241     int _unused;
   228 
   242 
   232     struct evsql_item_info columns[];
   246     struct evsql_item_info columns[];
   233 };
   247 };
   234 
   248 
   235 /**
   249 /**
   236  * Magic macros for defining param/result info -lists
   250  * Magic macros for defining param/result info -lists
       
   251  *  
       
   252  * @code
       
   253  *  static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
       
   254  *      EVSQL_PARAM( UINT32 ),
       
   255  *      ...,
       
   256  *
       
   257  *      EVSQL_PARAMS_END
       
   258  *  };
       
   259  * @endcode
   237  *
   260  *
   238  * @name EVSQL_TYPE/PARAM_*
   261  * @name EVSQL_TYPE/PARAM_*
   239  * @{
   262  * @{
   240  */
   263  */
   241 
   264 
   242 /**
   265 /**
   243  * A `struct evsql_item_info` initializer, using FMT_BINARY and the given EVSQL_TYPE_ -suffix.
   266  * A `struct evsql_item_info` initializer, using FMT_BINARY and the given EVSQL_TYPE_ -suffix.
   244  *
   267  *
       
   268  * @param typenam the suffix of an evsql_item_type name
       
   269  *
   245  * @see struct evsql_item_info
   270  * @see struct evsql_item_info
   246  * @see enum evsql_item_type
   271  * @see enum evsql_item_type
   247  */
   272  */
   248 #define EVSQL_TYPE(typenam)                     { EVSQL_FMT_BINARY, EVSQL_TYPE_ ## typenam  }
   273 #define EVSQL_TYPE(typenam)                     { EVSQL_FMT_BINARY, EVSQL_TYPE_ ## typenam  }
   249 
   274 
   253  * @see struct evsql_item_info
   278  * @see struct evsql_item_info
   254  */
   279  */
   255 #define EVSQL_TYPE_END                          { EVSQL_FMT_BINARY, EVSQL_TYPE_INVALID      }
   280 #define EVSQL_TYPE_END                          { EVSQL_FMT_BINARY, EVSQL_TYPE_INVALID      }
   256 
   281 
   257 /**
   282 /**
   258  * Initializer block for a `struct evsql_query_params` struct. EVSQL_PARAMS/EVSQL_PARAMS_END should be used as a
   283  * Initializer block for an evsql_query_params struct
   259  * pseudo-block with the following layout:
       
   260  *
       
   261  *  static struct evsql_query_params params = EVSQL_PARAMS(EVSQL_FMT_BINARY) {
       
   262  *      EVSQL_PARAM(...),
       
   263  *      ...,
       
   264  *
       
   265  *      EVSQL_PARAMS_END
       
   266  *  };
       
   267  */
   284  */
   268 #define EVSQL_PARAMS(result_fmt)            { result_fmt, 
   285 #define EVSQL_PARAMS(result_fmt)            { result_fmt, 
       
   286 
       
   287 /**
       
   288  * An evsql_item initializer
       
   289  */
   269 #define EVSQL_PARAM(typenam)                    { EVSQL_TYPE(typenam) }
   290 #define EVSQL_PARAM(typenam)                    { EVSQL_TYPE(typenam) }
       
   291 
       
   292 /**
       
   293  * Include the ending item and terminate the pseudo-block started using #EVSQL_PARAMS
       
   294  */
   270 #define EVSQL_PARAMS_END                        { EVSQL_TYPE_END } \
   295 #define EVSQL_PARAMS_END                        { EVSQL_TYPE_END } \
   271                                               } // <<<
   296                                               } // <<<
   272 
   297 
   273 // @}
   298 // @}
   274 
   299 
   339  * @see evsql_trans_commit
   364  * @see evsql_trans_commit
   340  */
   365  */
   341 typedef void (*evsql_trans_done_cb)(struct evsql_trans *trans, void *arg);
   366 typedef void (*evsql_trans_done_cb)(struct evsql_trans *trans, void *arg);
   342 
   367 
   343 // @}
   368 // @}
       
   369 
       
   370 /**
       
   371  * Session functions
       
   372  *
       
   373  * @name evsql_*
       
   374  * @{
       
   375  */
   344 
   376 
   345 /**
   377 /**
   346  * Create a new PostgreSQL/libpq (evpq) -based evsql using the given conninfo.
   378  * Create a new PostgreSQL/libpq (evpq) -based evsql using the given conninfo.
   347  *
   379  *
   348  * The given conninfo must stay valid for the duration of the evsql's lifetime.
   380  * The given conninfo must stay valid for the duration of the evsql's lifetime.
   357  */
   389  */
   358 struct evsql *evsql_new_pq (struct event_base *ev_base, const char *pq_conninfo, 
   390 struct evsql *evsql_new_pq (struct event_base *ev_base, const char *pq_conninfo, 
   359     evsql_error_cb error_fn, 
   391     evsql_error_cb error_fn, 
   360     void *cb_arg
   392     void *cb_arg
   361 );
   393 );
       
   394 
       
   395 /**
       
   396  * Close a connection. Callbacks for waiting queries will not be run.
       
   397  *
       
   398  * XXX: not implemented yet.
       
   399  *
       
   400  * @ingroup evsql_*
       
   401  * @param evsql the context handle from evsql_new_*
       
   402  */
       
   403 void evsql_close (struct evsql *evsql);
       
   404 
       
   405 // @}
   362 
   406 
   363 /**
   407 /**
   364  * Query API
   408  * Query API
   365  *
   409  *
   366  * @name evsql_query_*
   410  * @name evsql_query_*
   698  */
   742  */
   699 void evsql_result_free (struct evsql_result *res);
   743 void evsql_result_free (struct evsql_result *res);
   700 
   744 
   701 // @}
   745 // @}
   702 
   746 
   703 /**
       
   704  * Close a connection. Callbacks for waiting queries will not be run.
       
   705  *
       
   706  * XXX: not implemented yet.
       
   707  *
       
   708  * @ingroup evsql_*
       
   709  * @param evsql the context handle from evsql_new_*
       
   710  */
       
   711 void evsql_close (struct evsql *evsql);
       
   712 
       
   713 #endif /* EVSQL_H */
   747 #endif /* EVSQL_H */