Implement new param_list method, which returns a list of
[working/Evergreen.git] / Open-ILS / include / openils / oils_buildq.h
1 /**
2         @file buildquery.h
3         @brief Header for routines for building database queries.
4 */
5
6 #ifndef OILS_BUILDQ_H
7 #define OILS_BUILDQ_H
8
9 #include "opensrf/osrf_json.h"
10
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
15 struct StoredQ_;
16 typedef struct StoredQ_ StoredQ;
17
18 struct FromRelation_;
19 typedef struct FromRelation_ FromRelation;
20
21 struct SelectItem_;
22 typedef struct SelectItem_ SelectItem;
23
24 struct BindVar_;
25 typedef struct BindVar_ BindVar;
26
27 struct Expression_;
28 typedef struct Expression_ Expression;
29
30 struct QSeq_;
31 typedef struct QSeq_ QSeq;
32
33 struct OrderItem_;
34 typedef struct OrderItem_ OrderItem;
35
36 struct BuildSQLState_;
37 typedef struct BuildSQLState_ BuildSQLState;
38
39 struct IdNode_;
40 typedef struct IdNode_ IdNode;
41
42 /**
43         @brief Stores various things related to the construction of an SQL query.
44         
45         This struct carries around various bits and scraps of context for constructing and
46         executing an SQL query.  It also provides a way for buildSQLQuery() to return more than
47         one kind of thing to its caller.  In particular it can return a status code, a list of
48         error messages, and (if there is no error) an SQL string.
49 */
50 struct BuildSQLState_ {
51         dbi_conn dbhandle;            /**< Handle for the database connection */
52         dbi_result result;            /**< Reference to current row or result set */
53         int error;                    /**< Boolean; true if an error has occurred */
54         osrfStringArray* error_msgs;  /**< Descriptions of errors, if any */
55         growing_buffer* sql;          /**< To hold the constructed query */
56         osrfHash* bindvar_list;       /**< List of bind variables used by this query, each with
57                                            a pointer to the corresponding BindVar. */
58         IdNode* query_stack;          /**< For avoiding infinite recursion of nested queries */
59         IdNode* expr_stack;           /**< For avoiding infinite recursion of nested expressions */
60         IdNode* from_stack;           /**< For avoiding infinite recursion of from clauses */
61         int indent;                   /**< For prettifying SQL output: level of indentation */
62         int defaults_usable;          /**< Boolean; if true, we can use unconfirmed default
63                                            values for bind variables */
64         int values_required;          /**< Boolean: if true, we need values for a bind variables */
65 };
66
67 typedef enum {
68         QT_SELECT,
69         QT_UNION,
70         QT_INTERSECT,
71         QT_EXCEPT
72 } QueryType;
73
74 struct StoredQ_ {
75         StoredQ*      next;
76         int           id;
77         QueryType     type;
78         int           use_all;        /**< Boolean */
79         int           use_distinct;   /**< Boolean */
80         FromRelation* from_clause;
81         Expression*   where_clause;
82         SelectItem*   select_list;
83         QSeq*         child_list;
84         Expression*   having_clause;
85         OrderItem*    order_by_list;
86 };
87
88 typedef enum {
89         FRT_RELATION,
90         FRT_SUBQUERY,
91         FRT_FUNCTION
92 } FromRelationType;
93
94 typedef enum {
95         JT_NONE,
96         JT_INNER,
97         JT_LEFT,
98         JT_RIGHT,
99         JT_FULL
100 } JoinType;
101
102 struct FromRelation_ {
103         FromRelation*    next;
104         int              id;
105         FromRelationType type;
106         char*            table_name;
107         char*            class_name;
108         int              subquery_id;
109         StoredQ*         subquery;
110         int              function_call_id;
111         char*            table_alias;
112         int              parent_relation_id;
113         int              seq_no;
114         JoinType         join_type;
115         Expression*      on_clause;
116         FromRelation*    join_list;
117 };
118
119 struct SelectItem_ {
120         SelectItem* next;
121         int         id;
122         int         stored_query_id;
123         int         seq_no;
124         Expression* expression;
125         char*       column_alias;
126         int         grouped_by;        // Boolean
127 };
128
129 typedef enum {
130         BIND_STR,
131         BIND_NUM,
132         BIND_STR_LIST,
133         BIND_NUM_LIST
134 } BindVarType;
135
136 struct BindVar_ {
137         BindVar*    next;
138         char*       name;
139         char*       label;
140         BindVarType type;
141         char*       description;
142         jsonObject* default_value;
143         jsonObject* actual_value;
144 };
145
146 typedef enum {
147         EXP_BETWEEN,
148         EXP_BIND,
149         EXP_BOOL,
150         EXP_CASE,
151         EXP_CAST,
152         EXP_COLUMN,
153         EXP_EXIST,
154         EXP_FIELD,
155         EXP_FUNCTION,
156         EXP_IN,
157         EXP_NULL,
158         EXP_NUMBER,
159         EXP_OPERATOR,
160         EXP_STRING,
161         EXP_SUBQUERY
162 } ExprType;
163
164 struct Expression_ {
165         Expression* next;
166         int         id;
167         ExprType    type;
168         int         parenthesize;       // Boolean
169         int         parent_expr_id;
170         int         seq_no;
171         char*       literal;
172         char*       table_alias;
173         char*       column_name;
174         Expression* left_operand;
175         char*       op;
176         Expression* right_operand;
177         int         function_id;
178         int         subquery_id;
179         StoredQ*    subquery;
180         int         cast_type_id;
181         int         negate;             // Boolean
182         BindVar*    bind;
183 };
184
185 struct QSeq_ {
186         QSeq*    next;
187         int      id;
188         int      parent_query_id;
189         int      seq_no;
190         StoredQ* child_query;
191 };
192
193 struct OrderItem_ {
194         OrderItem* next;
195         int        id;
196         int        stored_query_id;
197         int        seq_no;
198         Expression* expression;
199 };
200
201 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
202
203 void buildSQLStateFree( BuildSQLState* state );
204
205 void buildSQLCleanup( void );
206
207 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
208
209 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
210
211 jsonObject* oilsGetColNames( BuildSQLState* state, StoredQ* query );
212
213 void pop_id( IdNode** stack );
214
215 void storedQFree( StoredQ* sq );
216
217 void storedQCleanup( void );
218
219 int buildSQL( BuildSQLState* state, StoredQ* query );
220
221 void oilsStoredQSetVerbose( void );
222
223 jsonObject* oilsFirstRow( BuildSQLState* state );
224
225 jsonObject* oilsNextRow( BuildSQLState* state );
226
227 jsonObject* oilsBindVarList( osrfHash* bindvar_list );
228
229 int oilsApplyBindValues( BuildSQLState* state, jsonObject* bindings );
230
231 #ifdef __cplusplus
232 }
233 #endif
234
235 #endif