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
191 int parenthesize; // Boolean
197 Expression* left_operand;
198 char* op; // Not called "operator" because that's a keyword in C++
199 Expression* right_operand;
203 int negate; // Boolean
205 Expression* subexp_list; // Linked list of subexpressions
206 CaseBranch* branch_list; // Linked list of CASE branches
207 // The next column comes, not from query.expression,
208 // but from query.function_sig:
217 StoredQ* child_query;
225 Expression* expression;
228 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
230 void buildSQLStateFree( BuildSQLState* state );
232 void buildSQLCleanup( void );
234 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
236 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
238 jsonObject* oilsGetColNames( BuildSQLState* state, StoredQ* query );
240 void pop_id( IdNode** stack );
242 void storedQFree( StoredQ* sq );
244 void storedQCleanup( void );
246 int buildSQL( BuildSQLState* state, const StoredQ* query );
248 void oilsStoredQSetVerbose( void );
250 jsonObject* oilsFirstRow( BuildSQLState* state );
252 jsonObject* oilsNextRow( BuildSQLState* state );
254 jsonObject* oilsBindVarList( osrfHash* bindvar_list );
256 int oilsApplyBindValues( BuildSQLState* state, const jsonObject* bindings );