3 @brief Header for routines for building database queries.
9 #include "opensrf/osrf_json.h"
16 typedef struct StoredQ_ StoredQ;
19 typedef struct FromRelation_ FromRelation;
22 typedef struct SelectItem_ SelectItem;
25 typedef struct BindVar_ BindVar;
28 typedef struct CaseBranch_ CaseBranch;
31 typedef struct Datatype_ Datatype;
34 typedef struct Expression_ Expression;
37 typedef struct QSeq_ QSeq;
40 typedef struct OrderItem_ OrderItem;
42 struct BuildSQLState_;
43 typedef struct BuildSQLState_ BuildSQLState;
46 typedef struct IdNode_ IdNode;
49 @brief Stores various things related to the construction of an SQL query.
51 This struct carries around various bits and scraps of context for constructing and
52 executing an SQL query. It also provides a way for buildSQLQuery() to return more than
53 one kind of thing to its caller. In particular it can return a status code, a list of
54 error messages, and (if there is no error) an SQL string.
56 struct BuildSQLState_ {
57 dbi_conn dbhandle; /**< Handle for the database connection */
58 dbi_result result; /**< Reference to current row or result set */
59 int error; /**< Boolean; true if an error has occurred */
60 osrfStringArray* error_msgs; /**< Descriptions of errors, if any */
61 growing_buffer* sql; /**< To hold the constructed query */
62 osrfHash* bindvar_list; /**< List of bind variables used by this query, each with
63 a pointer to the corresponding BindVar. */
64 IdNode* query_stack; /**< For avoiding infinite recursion of nested queries */
65 IdNode* expr_stack; /**< For avoiding infinite recursion of nested expressions */
66 IdNode* from_stack; /**< For avoiding infinite recursion of from clauses */
67 int indent; /**< For prettifying SQL output: level of indentation */
68 int defaults_usable; /**< Boolean; if true, we can use unconfirmed default
69 values for bind variables */
70 int values_required; /**< Boolean: if true, we need values for a bind variables */
84 int use_all; /**< Boolean */
85 int use_distinct; /**< Boolean */
86 FromRelation* from_clause;
87 Expression* where_clause;
88 SelectItem* select_list;
90 Expression* having_clause;
91 OrderItem* order_by_list;
108 struct FromRelation_ {
111 FromRelationType type;
116 int function_call_id;
117 Expression* function_call;
119 int parent_relation_id;
122 Expression* on_clause;
123 FromRelation* join_list;
131 Expression* expression;
133 int grouped_by; // Boolean
149 jsonObject* default_value;
150 jsonObject* actual_value;
156 Expression* condition;
164 int is_numeric; // Boolean
165 int is_composite; // Boolean
192 int parenthesize; // Boolean
198 Expression* left_operand;
200 Expression* right_operand;
204 int negate; // Boolean
206 Expression* subexp_list; // Linked list of subexpressions
207 CaseBranch* branch_list; // Linked list of CASE branches
208 // The next column comes, not from query.expression,
209 // but from query.function_sig:
218 StoredQ* child_query;
226 Expression* expression;
229 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
231 void buildSQLStateFree( BuildSQLState* state );
233 void buildSQLCleanup( void );
235 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
237 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
239 jsonObject* oilsGetColNames( BuildSQLState* state, StoredQ* query );
241 void pop_id( IdNode** stack );
243 void storedQFree( StoredQ* sq );
245 void storedQCleanup( void );
247 int buildSQL( BuildSQLState* state, const StoredQ* query );
249 void oilsStoredQSetVerbose( void );
251 jsonObject* oilsFirstRow( BuildSQLState* state );
253 jsonObject* oilsNextRow( BuildSQLState* state );
255 jsonObject* oilsBindVarList( osrfHash* bindvar_list );
257 int oilsApplyBindValues( BuildSQLState* state, const jsonObject* bindings );