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 Expression_ Expression;
28 typedef struct QSeq_ QSeq;
31 typedef struct OrderItem_ OrderItem;
33 struct BuildSQLState_;
34 typedef struct BuildSQLState_ BuildSQLState;
37 typedef struct IdNode_ IdNode;
40 @brief Stores various things related to the construction of an SQL query.
42 This struct carries around various bits and scraps of context for constructing and
43 executing an SQL query. It also provides a way for buildSQLQuery() to return more than
44 one kind of thing to its caller. In particular it can return a status code, a list of
45 error messages, and (if there is no error) an SQL string.
47 struct BuildSQLState_ {
48 dbi_conn dbhandle; /**< Handle for the database connection */
49 dbi_result result; /**< Reference to current row or result set */
50 int error; /**< Boolean; true if an error has occurred */
51 osrfStringArray* error_msgs; /**< Descriptions of errors, if any */
52 growing_buffer* sql; /**< To hold the constructed query */
53 IdNode* query_stack; /**< For avoiding infinite recursion of nested queries */
54 IdNode* expr_stack; /**< For avoiding infinite recursion of nested expressions */
55 IdNode* from_stack; /**< For avoiding infinite recursion of from clauses */
56 int indent; /**< For prettifying SQL output: level of indentation */
70 int use_all; /**< Boolean */
71 int use_distinct; /**< Boolean */
72 FromRelation* from_clause;
73 Expression* where_clause;
74 SelectItem* select_list;
76 Expression* having_clause;
77 OrderItem* order_by_list;
94 struct FromRelation_ {
97 FromRelationType type;
102 int function_call_id;
104 int parent_relation_id;
107 Expression* on_clause;
108 FromRelation* join_list;
116 Expression* expression;
118 int grouped_by; // Boolean
142 int parenthesize; // Boolean
148 Expression* left_operand;
150 Expression* right_operand;
155 int negate; // Boolean
163 StoredQ* child_query;
171 Expression* expression;
174 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
176 void buildSQLStateFree( BuildSQLState* state );
178 void buildSQLCleanup( void );
180 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
182 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
184 jsonObject* oilsGetColNames( BuildSQLState* state, StoredQ* query );
186 void pop_id( IdNode** stack );
188 void storedQFree( StoredQ* sq );
190 void storedQCleanup( void );
192 int buildSQL( BuildSQLState* state, StoredQ* query );
194 void oilsStoredQSetVerbose( void );
196 jsonObject* oilsFirstRow( BuildSQLState* state );
198 jsonObject* oilsNextRow( BuildSQLState* state );