1 #ifndef SQL_PARSER_H 2 #define SQL_PARSER_H 3 4 #include <glib.h> 5 #define YY_NO_UNISTD_H 6 7 8 typedef struct sql_statement sql_statement; 9 typedef struct sql_select_statement sql_select_statement; 10 typedef struct sql_insert_statement sql_insert_statement; 11 typedef struct sql_update_statement sql_update_statement; 12 typedef struct sql_delete_statement sql_delete_statement; 13 14 typedef struct sql_table sql_table; 15 typedef struct sql_where sql_where; 16 typedef struct sql_condition sql_condition; 17 typedef struct sql_order_field sql_order_field; 18 19 typedef struct sql_field sql_field; 20 typedef struct sql_field_item sql_field_item; 21 typedef struct param_spec param_spec; 22 23 /* 24 * global SQL statement structures 25 */ 26 typedef enum 27 { 28 SQL_select, 29 SQL_insert, 30 SQL_delete, 31 SQL_update 32 } 33 sql_statement_type; 34 35 36 struct sql_statement 37 { 38 sql_statement_type type; 39 char *full_query; 40 void *statement; 41 }; 42 43 struct sql_select_statement 44 { 45 int distinct; 46 GList *fields; /* list of sql_field */ 47 GList *from; /* list of sql_table */ 48 sql_where *where; 49 GList *order; /* list of sql_order_field */ 50 GList *group; 51 }; 52 53 struct sql_insert_statement 54 { 55 sql_table *table; 56 GList *fields; 57 GList *values; 58 }; 59 60 struct sql_update_statement 61 { 62 sql_table *table; 63 GList *set; 64 sql_where *where; 65 }; 66 67 struct sql_delete_statement 68 { 69 sql_table *table; 70 sql_where *where; 71 }; 72 73 74 75 /* 76 * Fields structures 77 */ 78 typedef enum 79 { 80 SQL_name, 81 SQL_equation, 82 SQL_inlineselect, 83 SQL_function 84 } 85 sql_field_type; 86 87 typedef enum 88 { 89 SQL_plus, 90 SQL_minus, 91 SQL_times, 92 SQL_div 93 } 94 sql_field_operator; 95 96 struct sql_field_item 97 { 98 sql_field_type type; 99 100 union 101 { 102 GList *name; 103 struct 104 { 105 sql_field_item *left; 106 sql_field_item *right; 107 sql_field_operator op; 108 } 109 equation; 110 sql_select_statement *select; 111 struct 112 { 113 gchar *funcname; 114 GList *funcarglist; 115 } function; 116 } d; 117 }; 118 119 struct sql_field 120 { 121 sql_field_item *item; 122 char *as; 123 GList *param_spec; 124 }; 125 126 typedef enum 127 { 128 PARAM_name, 129 PARAM_descr, 130 PARAM_type, 131 PARAM_isparam, 132 PARAM_nullok 133 } param_spec_type; 134 135 struct param_spec 136 { 137 param_spec_type type; 138 char *content; 139 }; 140 141 142 143 /* 144 * Tables structures 145 */ 146 typedef enum 147 { 148 SQL_simple, 149 SQL_nestedselect 150 } 151 sql_table_type; 152 153 typedef enum 154 { 155 SQL_cross_join, /* default */ 156 SQL_inner_join, 157 SQL_left_join, 158 SQL_right_join, 159 SQL_full_join 160 } 161 sql_join_type; 162 163 struct sql_table 164 { 165 sql_table_type type; 166 union 167 { 168 char *simple; 169 sql_select_statement *select; 170 } 171 d; 172 char *as; 173 sql_join_type join_type; 174 sql_where *join_cond; 175 }; 176 177 178 /* 179 * Conditions structures 180 */ 181 typedef enum 182 { 183 SQL_eq, 184 SQL_is, 185 SQL_isnot, 186 SQL_in, 187 SQL_like, 188 SQL_notin, 189 SQL_between, 190 SQL_gt, 191 SQL_lt, 192 SQL_geq, 193 SQL_leq, 194 SQL_diff 195 } 196 sql_condition_operator; 197 198 struct sql_condition 199 { 200 sql_condition_operator op; 201 202 union 203 { 204 struct 205 { 206 sql_field *left; 207 sql_field *right; 208 } 209 pair; 210 struct 211 { 212 sql_field *field; 213 sql_field *lower; 214 sql_field *upper; 215 } 216 between; 217 } 218 d; 219 }; 220 221 typedef enum 222 { 223 SQL_and, 224 SQL_or 225 } 226 sql_logic_operator; 227 228 typedef enum 229 { 230 SQL_single, 231 SQL_negated, 232 SQL_pair 233 } 234 sql_where_type; 235 236 struct sql_where 237 { 238 sql_where_type type; 239 240 union 241 { 242 sql_condition *single; 243 sql_where *negated; 244 struct 245 { 246 sql_where *left; 247 sql_where *right; 248 sql_logic_operator op; 249 } 250 pair; 251 } 252 d; 253 }; 254 255 /* 256 * ORDER BY structure 257 */ 258 typedef enum 259 { 260 SQL_asc, 261 SQL_desc 262 } 263 sql_ordertype; 264 265 struct sql_order_field 266 { 267 sql_ordertype order_type; 268 GList *name; 269 }; 270 271 272 273 274 275 276 int sql_display (sql_statement * statement); 277 int sql_destroy (sql_statement * statement); 278 char *sql_stringify (sql_statement * statement); 279 280 sql_statement *sql_parse (char *sql_statement); 281 sql_statement *sql_parse_with_error (char *sql_statement, GError ** error); 282 283 int sql_statement_append_field (sql_statement * statement, char *tablename, 284 char *fieldname, char *as); 285 int sql_statement_append_tablejoin (sql_statement * statement, 286 char *lefttable, char *righttable, 287 char *leftfield, char *rightfield); 288 int sql_statement_append_where (sql_statement * statement, char *leftfield, 289 char *rightfield, sql_logic_operator logicopr, 290 sql_condition_operator condopr); 291 292 GList *sql_statement_get_fields (sql_statement * statement); 293 GList *sql_statement_get_tables (sql_statement * statement); 294 void sql_statement_free_tables (GList * tables); 295 void sql_statement_free_fields (GList * fields); 296 297 char *sql_statement_get_first_table (sql_statement * statement); 298 299 gint sql_statement_get_wherejoin_ontable (sql_statement * statement, 300 gchar * ontable, GList ** leftfield, 301 GList ** rightfield, 302 sql_condition_operator * condopr); 303 #endif /* SQL_PARSER_H */ 304