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;
168 int parenthesize; // Boolean
174 Expression* left_operand;
176 Expression* right_operand;
181 int negate; // Boolean
190 StoredQ* child_query;
198 Expression* expression;
201 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
203 void buildSQLStateFree( BuildSQLState* state );
205 void buildSQLCleanup( void );
207 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
209 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
211 jsonObject* oilsGetColNames( BuildSQLState* state, StoredQ* query );
213 void pop_id( IdNode** stack );
215 void storedQFree( StoredQ* sq );
217 void storedQCleanup( void );
219 int buildSQL( BuildSQLState* state, StoredQ* query );
221 void oilsStoredQSetVerbose( void );
223 jsonObject* oilsFirstRow( BuildSQLState* state );
225 jsonObject* oilsNextRow( BuildSQLState* state );