libidav/davqlparser.c

changeset 87
ed21d95984bb
parent 86
ecba8bdf9741
child 88
4d6b03bd7034
equal deleted inserted replaced
86:ecba8bdf9741 87:ed21d95984bb
85 default: return "unknown"; 85 default: return "unknown";
86 } 86 }
87 } 87 }
88 88
89 static void dav_debug_ql_stmt_print(DavQLStatement *stmt) { 89 static void dav_debug_ql_stmt_print(DavQLStatement *stmt) {
90 sstr_t empty = ST("(empty)");
91
92 // Basic information 90 // Basic information
93 size_t fieldcount = ucx_list_size(stmt->fields); 91 size_t fieldcount = ucx_list_size(stmt->fields);
94 int specialfield = 0; 92 int specialfield = 0;
95 UCX_FOREACH(elm, stmt->fields) { 93 UCX_FOREACH(elm, stmt->fields) {
96 DavQLExpression* expr = (DavQLExpression*)elm->data; 94 DavQLExpression* expr = (DavQLExpression*)elm->data;
109 "Has where clause: %s\n", 107 "Has where clause: %s\n",
110 sfmtarg(stmt->srctext), 108 sfmtarg(stmt->srctext),
111 _map_querytype(stmt->type), 109 _map_querytype(stmt->type),
112 fieldcount, 110 fieldcount,
113 _map_specialfield(specialfield), 111 _map_specialfield(specialfield),
114 sfmtarg(stmt->path ? stmt->path->srctext : empty), 112 sfmtarg(stmt->path),
115 stmt->where ? "yes" : "no"); 113 stmt->where ? "yes" : "no");
116 if (stmt->type == DAVQL_SET) { 114 if (stmt->type == DAVQL_SET) {
117 printf("Value list size matches: %s", 115 printf("Value list size matches: %s",
118 ucx_list_size(stmt->fields) == ucx_list_size(stmt->setvalues) 116 ucx_list_size(stmt->fields) == ucx_list_size(stmt->setvalues)
119 ? "yes" : "no"); 117 ? "yes" : "no");
171 return DQLD_CMD_Q; 169 return DQLD_CMD_Q;
172 } else if (!strcmp(buffer, "ps\n")) { 170 } else if (!strcmp(buffer, "ps\n")) {
173 return DQLD_CMD_PS; 171 return DQLD_CMD_PS;
174 } else if (!strcmp(buffer, "pe\n")) { 172 } else if (!strcmp(buffer, "pe\n")) {
175 return DQLD_CMD_PE; 173 return DQLD_CMD_PE;
176 } else if (!strcmp(buffer, "p\n")) {
177 return DQLD_CMD_P;
178 } else if (!strcmp(buffer, "l\n")) { 174 } else if (!strcmp(buffer, "l\n")) {
179 return DQLD_CMD_L; 175 return DQLD_CMD_L;
180 } else if (!strcmp(buffer, "r\n")) { 176 } else if (!strcmp(buffer, "r\n")) {
181 return DQLD_CMD_R; 177 return DQLD_CMD_R;
182 } else if (!strcmp(buffer, "h\n")) { 178 } else if (!strcmp(buffer, "h\n")) {
205 int cmd = dav_debug_ql_command(); 201 int cmd = dav_debug_ql_command();
206 switch (cmd) { 202 switch (cmd) {
207 case DQLD_CMD_Q: return; 203 case DQLD_CMD_Q: return;
208 case DQLD_CMD_PS: dav_debug_ql_stmt_print(stmt); break; 204 case DQLD_CMD_PS: dav_debug_ql_stmt_print(stmt); break;
209 case DQLD_CMD_PE: dav_debug_ql_expr_print(examineexpr); break; 205 case DQLD_CMD_PE: dav_debug_ql_expr_print(examineexpr); break;
210 case DQLD_CMD_P:
211 examineexpr = stmt->path;
212 dav_debug_ql_expr_print(examineexpr);
213 break;
214 case DQLD_CMD_L: 206 case DQLD_CMD_L:
215 if (dav_debug_ql_expr_selected(examineexpr)) { 207 if (dav_debug_ql_expr_selected(examineexpr)) {
216 if (examineexpr->left) { 208 if (examineexpr->left) {
217 examineexpr = examineexpr->left; 209 examineexpr = examineexpr->left;
218 dav_debug_ql_expr_print(examineexpr); 210 dav_debug_ql_expr_print(examineexpr);
232 } 224 }
233 break; 225 break;
234 case DQLD_CMD_H: 226 case DQLD_CMD_H:
235 printf( 227 printf(
236 "\nCommands:\n" 228 "\nCommands:\n"
237 "p: examine path\n"
238 "ps: print statement information\n" 229 "ps: print statement information\n"
239 "q: quit\n\n" 230 "q: quit\n\n"
240 "\nExpression examination:\n" 231 "\nExpression examination:\n"
241 "pe: print expression information\n" 232 "pe: print expression information\n"
242 "l: enter left subtree\n" 233 "l: enter left subtree\n"
351 expr->srctext.length = token_sstr(token)->ptr + 342 expr->srctext.length = token_sstr(token)->ptr +
352 token_sstr(token)->length - expr->srctext.ptr; 343 token_sstr(token)->length - expr->srctext.ptr;
353 } 344 }
354 345
355 return expr; 346 return expr;
347 }
348
349 static void dav_free_expression(DavQLExpression *expr) {
350 if (expr->left) {
351 dav_free_expression(expr->left);
352 }
353 if (expr->right) {
354 dav_free_expression(expr->right);
355 }
356 free(expr);
356 } 357 }
357 358
358 static void dav_parse_unexpected_token(DavQLStatement *stmt, UcxList *token) { 359 static void dav_parse_unexpected_token(DavQLStatement *stmt, UcxList *token) {
359 sstr_t emptystring = ST(""); 360 sstr_t emptystring = ST("");
360 stmt->errorcode = DAVQL_ERROR_UNEXPECTED_TOKEN; 361 stmt->errorcode = DAVQL_ERROR_UNEXPECTED_TOKEN;
430 } 431 }
431 } 432 }
432 break; 433 break;
433 } 434 }
434 // from clause 435 // from clause
435 case 20: 436 case 20: {
436 stmt->path = dav_parse_expression(token, 1); 437 DavQLExpression *expr = dav_parse_expression(token, 1);
438 stmt->path = expr->srctext;
439 dav_free_expression(expr);
437 step = 520; 440 step = 520;
438 break; 441 break;
442 }
439 // where clause 443 // where clause
440 case 30: 444 case 30:
441 step = 530; 445 step = 530;
442 break; 446 break;
443 // with clause 447 // with clause
503 } 507 }
504 508
505 return stmt; 509 return stmt;
506 } 510 }
507 511
508 static void dav_free_expression(DavQLExpression *expr) {
509 if (expr->left) {
510 dav_free_expression(expr->left);
511 }
512 if (expr->right) {
513 dav_free_expression(expr->right);
514 }
515 free(expr);
516 }
517
518 void dav_free_statement(DavQLStatement *stmt) { 512 void dav_free_statement(DavQLStatement *stmt) {
519 UCX_FOREACH(expr, stmt->fields) { 513 UCX_FOREACH(expr, stmt->fields) {
520 dav_free_expression(expr->data); 514 dav_free_expression(expr->data);
521 } 515 }
522 ucx_list_free(stmt->fields); 516 ucx_list_free(stmt->fields);
523 UCX_FOREACH(expr, stmt->setvalues) { 517 UCX_FOREACH(expr, stmt->setvalues) {
524 dav_free_expression(expr->data); 518 dav_free_expression(expr->data);
525 } 519 }
526 ucx_list_free(stmt->setvalues); 520 ucx_list_free(stmt->setvalues);
527 521
528 if (stmt->path) {
529 dav_free_expression(stmt->path);
530 }
531 if (stmt->where) { 522 if (stmt->where) {
532 dav_free_expression(stmt->where); 523 dav_free_expression(stmt->where);
533 } 524 }
534 if (stmt->errormessage) { 525 if (stmt->errormessage) {
535 free(stmt->errormessage); 526 free(stmt->errormessage);

mercurial