]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/include/openils/oils_buildq.h
Implement .execute method of qstore server.
[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 Expression_;
25 typedef struct Expression_ Expression;
26
27 struct QSeq_;
28 typedef struct QSeq_ QSeq;
29
30 struct OrderItem_;
31 typedef struct OrderItem_ OrderItem;
32
33 struct BuildSQLState_;
34 typedef struct BuildSQLState_ BuildSQLState;
35
36 struct IdNode_;
37 typedef struct IdNode_ IdNode;
38
39 /**
40         @brief Stores various things related to the construction of an SQL query.
41         
42         This struct carries around various bits and scraps of context for constructing and
43         executing an SQL query.  It also provides a way for buildSQLQuery() to return more than
44         one kind of thing to its caller.  In particular it can return a status code, a list of
45         error messages, and (if there is no error) an SQL string.
46 */
47 struct BuildSQLState_ {
48         dbi_conn dbhandle;            /**< Handle for the database connection */
49         dbi_result result;            /**< Reference to current row or result set */
50         int error;                    /**< Boolean; true if an error has occurred */
51         osrfStringArray* error_msgs;  /**< Descriptions of errors, if any */
52         growing_buffer* sql;          /**< To hold the constructed query */
53         IdNode* query_stack;          /**< For avoiding infinite recursion of nested queries */
54         IdNode* expr_stack;           /**< For avoiding infinite recursion of nested expressions */
55         IdNode* from_stack;           /**< For avoiding infinite recursion of from clauses */
56         int indent;                   /**< For prettifying SQL output: level of indentation */
57 };
58
59 typedef enum {
60         QT_SELECT,
61         QT_UNION,
62         QT_INTERSECT,
63         QT_EXCEPT
64 } QueryType;
65
66 struct StoredQ_ {
67         StoredQ*      next;
68         int           id;
69         QueryType     type;
70         int           use_all;        /**< Boolean */
71         int           use_distinct;   /**< Boolean */
72         FromRelation* from_clause;
73         Expression*   where_clause;
74         SelectItem*   select_list;
75         QSeq*         child_list;
76         OrderItem*    order_by_list;
77 };
78
79 typedef enum {
80         FRT_RELATION,
81         FRT_SUBQUERY,
82         FRT_FUNCTION
83 } FromRelationType;
84
85 typedef enum {
86         JT_NONE,
87         JT_INNER,
88         JT_LEFT,
89         JT_RIGHT,
90         JT_FULL
91 } JoinType;
92
93 struct FromRelation_ {
94         FromRelation*    next;
95         int              id;
96         FromRelationType type;
97         char*            table_name;
98         char*            class_name;
99         int              subquery_id;
100         StoredQ*         subquery;
101         int              function_call_id;
102         char*            table_alias;
103         int              parent_relation_id;
104         int              seq_no;
105         JoinType         join_type;
106         Expression*      on_clause;
107         FromRelation*    join_list;
108 };
109
110 struct SelectItem_ {
111         SelectItem* next;
112         int         id;
113         int         stored_query_id;
114         int         seq_no;
115         Expression* expression;
116         char*       column_alias;
117         int         grouped_by;        // Boolean
118 };
119
120 typedef enum {
121         EXP_BETWEEN,
122         EXP_BOOL,
123         EXP_CASE,
124         EXP_CAST,
125         EXP_COLUMN,
126         EXP_EXIST,
127         EXP_FIELD,
128         EXP_FUNCTION,
129         EXP_IN,
130         EXP_NOT_BETWEEN,
131         EXP_NOT_EXIST,
132         EXP_NOT_IN,
133         EXP_NULL,
134         EXP_NUMBER,
135         EXP_OPERATOR,
136         EXP_STRING,
137         EXP_SUBQUERY
138 } ExprType;
139
140 struct Expression_ {
141         Expression* next;
142         int         id;
143         ExprType    type;
144         int         parenthesize;       // Boolean
145         int         parent_expr_id;
146         int         seq_no;
147         char*       literal;
148         char*       table_alias;
149         char*       column_name;
150         Expression* left_operand;
151         char*       op;
152         Expression* right_operand;
153         int         function_id;
154         int         subquery_id;
155         StoredQ*    subquery;
156         int         cast_type_id;
157 };
158
159 struct QSeq_ {
160         QSeq*    next;
161         int      id;
162         int      parent_query_id;
163         int      seq_no;
164         StoredQ* child_query;
165 };
166
167 struct OrderItem_ {
168         OrderItem* next;
169         int        id;
170         int        stored_query_id;
171         int        seq_no;
172         Expression* expression;
173 };
174
175 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
176
177 void buildSQLStateFree( BuildSQLState* state );
178
179 void buildSQLCleanup( void );
180
181 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
182
183 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
184
185 void pop_id( IdNode** stack );
186
187 void storedQFree( StoredQ* sq );
188
189 void storedQCleanup( void );
190
191 int buildSQL( BuildSQLState* state, StoredQ* query );
192
193 void oilsStoredQSetVerbose( void );
194
195 jsonObject* oilsExecSql( BuildSQLState* state );
196
197 jsonObject* oilsFirstRow( BuildSQLState* state );
198
199 jsonObject* oilsNextRow( BuildSQLState* state );
200
201 #ifdef __cplusplus
202 }
203 #endif
204
205 #endif