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 OrderItem* order_by_list;
93 struct FromRelation_ {
96 FromRelationType type;
101 int function_call_id;
103 int parent_relation_id;
106 Expression* on_clause;
107 FromRelation* join_list;
115 Expression* expression;
117 int grouped_by; // Boolean
144 int parenthesize; // Boolean
150 Expression* left_operand;
152 Expression* right_operand;
164 StoredQ* child_query;
172 Expression* expression;
175 BuildSQLState* buildSQLStateNew( dbi_conn dbhandle );
177 void buildSQLStateFree( BuildSQLState* state );
179 void buildSQLCleanup( void );
181 const char* sqlAddMsg( BuildSQLState* state, const char* msg, ... );
183 StoredQ* getStoredQuery( BuildSQLState* state, int query_id );
185 void pop_id( IdNode** stack );
187 void storedQFree( StoredQ* sq );
189 void storedQCleanup( void );
191 int buildSQL( BuildSQLState* state, StoredQ* query );
193 void oilsStoredQSetVerbose( void );
195 jsonObject* oilsFirstRow( BuildSQLState* state );
197 jsonObject* oilsNextRow( BuildSQLState* state );