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 Expression_ Expression;
31 typedef struct QSeq_ QSeq;
34 typedef struct OrderItem_ OrderItem;
36 struct BuildSQLState_;
37 typedef struct BuildSQLState_ BuildSQLState;
40 typedef struct IdNode_ IdNode;
43 @brief Stores various things related to the construction of an SQL query.
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.
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 */
78 int use_all; /**< Boolean */
79 int use_distinct; /**< Boolean */
80 FromRelation* from_clause;
81 Expression* where_clause;
82 SelectItem* select_list;
84 Expression* having_clause;
85 OrderItem* order_by_list;
102 struct FromRelation_ {
105 FromRelationType type;
110 int function_call_id;
112 int parent_relation_id;
115 Expression* on_clause;
116 FromRelation* join_list;
124 Expression* expression;
126 int grouped_by; // Boolean
142 jsonObject* default_value;
143 jsonObject* actual_value;
170 int parenthesize; // Boolean
176 Expression* left_operand;
178 Expression* right_operand;
182 int negate; // Boolean
184 Expression* subexp_list; // Linked list of subexpressions
185 // The next two columns come, not from query.expression,
186 // but from query.function_sig:
188 int is_aggregate; // Boolean
196 StoredQ* child_query;
204 Expression* expression;
207 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
209 void buildSQLStateFree( BuildSQLState* state );
211 void buildSQLCleanup( void );
213 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
215 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
217 jsonObject* oilsGetColNames( BuildSQLState* state, StoredQ* query );
219 void pop_id( IdNode** stack );
221 void storedQFree( StoredQ* sq );
223 void storedQCleanup( void );
225 int buildSQL( BuildSQLState* state, const StoredQ* query );
227 void oilsStoredQSetVerbose( void );
229 jsonObject* oilsFirstRow( BuildSQLState* state );
231 jsonObject* oilsNextRow( BuildSQLState* state );
233 jsonObject* oilsBindVarList( osrfHash* bindvar_list );
235 int oilsApplyBindValues( BuildSQLState* state, const jsonObject* bindings );