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 Expression_ Expression;
34 typedef struct QSeq_ QSeq;
37 typedef struct OrderItem_ OrderItem;
39 struct BuildSQLState_;
40 typedef struct BuildSQLState_ BuildSQLState;
43 typedef struct IdNode_ IdNode;
46 @brief Stores various things related to the construction of an SQL query.
48 This struct carries around various bits and scraps of context for constructing and
49 executing an SQL query. It also provides a way for buildSQLQuery() to return more than
50 one kind of thing to its caller. In particular it can return a status code, a list of
51 error messages, and (if there is no error) an SQL string.
53 struct BuildSQLState_ {
54 dbi_conn dbhandle; /**< Handle for the database connection */
55 dbi_result result; /**< Reference to current row or result set */
56 int error; /**< Boolean; true if an error has occurred */
57 osrfStringArray* error_msgs; /**< Descriptions of errors, if any */
58 growing_buffer* sql; /**< To hold the constructed query */
59 osrfHash* bindvar_list; /**< List of bind variables used by this query, each with
60 a pointer to the corresponding BindVar. */
61 IdNode* query_stack; /**< For avoiding infinite recursion of nested queries */
62 IdNode* expr_stack; /**< For avoiding infinite recursion of nested expressions */
63 IdNode* from_stack; /**< For avoiding infinite recursion of from clauses */
64 int indent; /**< For prettifying SQL output: level of indentation */
65 int defaults_usable; /**< Boolean; if true, we can use unconfirmed default
66 values for bind variables */
67 int values_required; /**< Boolean: if true, we need values for a bind variables */
81 int use_all; /**< Boolean */
82 int use_distinct; /**< Boolean */
83 FromRelation* from_clause;
84 Expression* where_clause;
85 SelectItem* select_list;
87 Expression* having_clause;
88 OrderItem* order_by_list;
105 struct FromRelation_ {
108 FromRelationType type;
113 int function_call_id;
114 Expression* function_call;
116 int parent_relation_id;
119 Expression* on_clause;
120 FromRelation* join_list;
128 Expression* expression;
130 int grouped_by; // Boolean
146 jsonObject* default_value;
147 jsonObject* actual_value;
153 Expression* condition;
181 int parenthesize; // Boolean
187 Expression* left_operand;
189 Expression* right_operand;
193 int negate; // Boolean
195 Expression* subexp_list; // Linked list of subexpressions
196 CaseBranch* branch_list; // Linked list of CASE branches
197 // The next column comes, not from query.expression,
198 // but from query.function_sig:
207 StoredQ* child_query;
215 Expression* expression;
218 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
220 void buildSQLStateFree( BuildSQLState* state );
222 void buildSQLCleanup( void );
224 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
226 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
228 jsonObject* oilsGetColNames( BuildSQLState* state, StoredQ* query );
230 void pop_id( IdNode** stack );
232 void storedQFree( StoredQ* sq );
234 void storedQCleanup( void );
236 int buildSQL( BuildSQLState* state, const StoredQ* query );
238 void oilsStoredQSetVerbose( void );
240 jsonObject* oilsFirstRow( BuildSQLState* state );
242 jsonObject* oilsNextRow( BuildSQLState* state );
244 jsonObject* oilsBindVarList( osrfHash* bindvar_list );
246 int oilsApplyBindValues( BuildSQLState* state, const jsonObject* bindings );