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 */
71 int panic; /**< Boolean: set to true if database connection dies */
85 int use_all; /**< Boolean */
86 int use_distinct; /**< Boolean */
87 FromRelation* from_clause;
88 Expression* where_clause;
89 SelectItem* select_list;
91 Expression* having_clause;
92 OrderItem* order_by_list;
93 Expression* limit_count;
94 Expression* offset_count;
111 struct FromRelation_ {
114 FromRelationType type;
119 int function_call_id;
120 Expression* function_call;
122 int parent_relation_id;
125 Expression* on_clause;
126 FromRelation* join_list;
134 Expression* expression;
136 int grouped_by; // Boolean
152 jsonObject* default_value;
153 jsonObject* actual_value;
159 Expression* condition;
167 int is_numeric; // Boolean
168 int is_composite; // Boolean
194 int parenthesize; // Boolean
200 Expression* left_operand;
201 char* op; // Not called "operator" because that's a keyword in C++
202 Expression* right_operand;
206 int negate; // Boolean
208 Expression* subexp_list; // Linked list of subexpressions
209 CaseBranch* branch_list; // Linked list of CASE branches
210 // The next column comes, not from query.expression,
211 // but from query.function_sig:
220 StoredQ* child_query;
228 Expression* expression;
231 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
233 void buildSQLStateFree( BuildSQLState* state );
235 void buildSQLCleanup( void );
237 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
239 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
241 jsonObject* oilsGetColNames( BuildSQLState* state, StoredQ* query );
243 void pop_id( IdNode** stack );
245 void storedQFree( StoredQ* sq );
247 void storedQCleanup( void );
249 int buildSQL( BuildSQLState* state, const StoredQ* query );
251 void oilsStoredQSetVerbose( void );
253 jsonObject* oilsFirstRow( BuildSQLState* state );
255 jsonObject* oilsNextRow( BuildSQLState* state );
257 jsonObject* oilsBindVarList( osrfHash* bindvar_list );
259 int oilsApplyBindValues( BuildSQLState* state, const jsonObject* bindings );