src/evsql/evsql.h
changeset 29 5de62ca9a5aa
child 40 03017f5f0087
equal deleted inserted replaced
28:e944453ca924 29:5de62ca9a5aa
       
     1 #ifndef EVSQL_INTERNAL_H
       
     2 #define EVSQL_INTERNAL_H
       
     3 
       
     4 /*
       
     5  * Internal interfaces
       
     6  */
       
     7 
       
     8 #include <sys/queue.h>
       
     9 
       
    10 #include <event2/event.h>
       
    11 
       
    12 #include "../evsql.h"
       
    13 #include "../evpq.h"
       
    14 
       
    15 /*
       
    16  * The engine type
       
    17  */
       
    18 enum evsql_type {
       
    19     EVSQL_EVPQ,     // evpq
       
    20 };
       
    21 
       
    22 /*
       
    23  * Contains the type, engine configuration, list of connections and waiting query queue.
       
    24  */
       
    25 struct evsql {
       
    26     // what event_base to use
       
    27     struct event_base *ev_base;
       
    28 
       
    29     // what engine we use
       
    30     enum evsql_type type;
       
    31 
       
    32     // callbacks
       
    33     evsql_error_cb error_fn;
       
    34     void *cb_arg;
       
    35     
       
    36     // engine-specific connection configuration
       
    37     union {
       
    38         const char *evpq;
       
    39     } engine_conf;
       
    40 
       
    41     // list of connections that are open
       
    42     LIST_HEAD(evsql_conn_list, evsql_conn) conn_list;
       
    43    
       
    44     // list of queries running or waiting to run
       
    45     TAILQ_HEAD(evsql_query_queue, evsql_query) query_queue;
       
    46 };
       
    47 
       
    48 /*
       
    49  * A single connection to the server.
       
    50  *
       
    51  * Contains the engine connection, may have a transaction associated, and may have a query associated.
       
    52  */
       
    53 struct evsql_conn {
       
    54     // evsql we belong to
       
    55     struct evsql *evsql;
       
    56 
       
    57     // engine-specific connection info
       
    58     union {
       
    59         struct evpq_conn *evpq;
       
    60     } engine;
       
    61 
       
    62     // our position in the conn list
       
    63     LIST_ENTRY(evsql_conn) entry;
       
    64 
       
    65     // are we running a transaction?
       
    66     struct evsql_trans *trans;
       
    67 
       
    68     // are we running a transactionless query?
       
    69     struct evsql_query *query;
       
    70 };
       
    71 
       
    72 /*
       
    73  * A single transaction.
       
    74  *
       
    75  * Has a connection associated and possibly a query (which will also be associated with the connection)
       
    76  */
       
    77 struct evsql_trans {
       
    78     // our evsql_conn/evsql
       
    79     struct evsql *evsql;
       
    80     struct evsql_conn *conn;
       
    81     
       
    82     // callbacks
       
    83     evsql_trans_error_cb error_fn;
       
    84     evsql_trans_ready_cb ready_fn;
       
    85     evsql_trans_done_cb done_fn;
       
    86     void *cb_arg;
       
    87 
       
    88     // the transaction type
       
    89     enum evsql_trans_type type;
       
    90 
       
    91     // has evsql_trans_commit be called?
       
    92     int has_commit : 1;
       
    93 
       
    94     // our current query
       
    95     struct evsql_query *query;
       
    96 
       
    97 };
       
    98 
       
    99 /*
       
   100  * A single query.
       
   101  *
       
   102  * Has the info needed to exec the query (as these may be queued), and the callback/result info.
       
   103  */
       
   104 struct evsql_query {
       
   105     // the actual SQL query, this may or may not be ours, see _evsql_query_exec
       
   106     char *command;
       
   107     
       
   108     // possible query params
       
   109     struct evsql_query_param_info {
       
   110         int count;
       
   111 
       
   112         Oid *types;
       
   113         const char **values;
       
   114         int *lengths;
       
   115         int *formats;
       
   116 
       
   117         int result_format;
       
   118     } params;
       
   119 
       
   120     // our callback
       
   121     evsql_query_cb cb_fn;
       
   122     void *cb_arg;
       
   123 
       
   124     // our position in the query list
       
   125     TAILQ_ENTRY(evsql_query) entry;
       
   126 
       
   127     // the result
       
   128     union {
       
   129         PGresult *evpq;
       
   130     } result;
       
   131 };
       
   132 
       
   133 // maximum length for a 'BEGIN TRANSACTION ...' query
       
   134 #define EVSQL_QUERY_BEGIN_BUF 512
       
   135 
       
   136 #endif /* EVSQL_INTERNAL_H */