[Mt-daapd-cvs] mt-daapd/src parser-driver.c,1.2,1.3 smart-parser.c,1.11,1.12 smart-parser.h,1.1,1.2
Status: Beta
Brought to you by:
andrew40
From: Ron P. <rp...@us...> - 2005-10-21 07:48:14
|
Update of /cvsroot/mt-daapd/mt-daapd/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25141 Modified Files: parser-driver.c smart-parser.c smart-parser.h Log Message: first pass of sql generation. Some problems in the production rules, I think, but coming along Index: smart-parser.h =================================================================== RCS file: /cvsroot/mt-daapd/mt-daapd/src/smart-parser.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** smart-parser.h 2 Aug 2005 03:17:22 -0000 1.1 --- smart-parser.h 21 Oct 2005 07:48:07 -0000 1.2 *************** *** 12,15 **** --- 12,16 ---- extern int sp_dispose(PARSETREE tree); extern char *sp_geterror(PARSETREE tree); + char *sp_sql_clause(PARSETREE tree); #endif /* _SMART_PARSER_H_ */ Index: smart-parser.c =================================================================== RCS file: /cvsroot/mt-daapd/mt-daapd/src/smart-parser.c,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** smart-parser.c 20 Oct 2005 07:33:58 -0000 1.11 --- smart-parser.c 21 Oct 2005 07:48:07 -0000 1.12 *************** *** 203,206 **** --- 203,207 ---- SP_NODE *sp_parse_date_criterion(PARSETREE tree); void sp_free_node(SP_NODE *); + int sp_node_size(SP_NODE *); /** *************** *** 844,847 **** --- 845,939 ---- } + /** + * calculate the size required to render the tree as a + * sql query. + * + * @parameter node node/tree to calculate + * @returns size in bytes of sql "where" clause + */ + int sp_node_size(SP_NODE *node) { + int size; + + if(node->op_type == SP_OPTYPE_ANDOR) { + size = sp_node_size(node->left.node); + size += sp_node_size(node->right.node); + size += 7; /* (xxx AND xxx) */ + } else { + size = 4; /* parens, plus spaces around op */ + size += strlen(node->left.field); + if((node->op & 0x0FFF) > T_LAST) { + DPRINTF(E_FATAL,L_PARSE,"Can't determine node size: op %04x\n", + node->op); + } else { + size += strlen(sp_token_descr[node->op & 0x0FFF]); + } + if(node->op_type == SP_OPTYPE_STRING) + size += (2 + strlen(node->right.cvalue)); + if(node->op_type == SP_OPTYPE_INT) + size += ((node->right.ivalue/10) + 1); + } + + return size; + } + + /** + * serialize node into pre-allocated string + * + * @param node node to serialize + * @param string string to generate + */ + void sp_serialize_sql(SP_NODE *node, char *string) { + char buffer[40]; + + if(node->op_type == SP_OPTYPE_ANDOR) { + strcat(string,"("); + sp_serialize_sql(node->left.node,string); + if(node->op == T_AND) strcat(string," and "); + if(node->op == T_OR) strcat(string," or "); + sp_serialize_sql(node->right.node,string); + strcat(string,")"); + } else { + strcat(string,"("); + strcat(string,node->left.field); + strcat(string," "); + strcat(string,sp_token_descr[node->op & 0x0FFF]); + strcat(string," "); + if(node->op_type == SP_OPTYPE_STRING) { + strcat(string,"'"); + strcat(string,node->right.cvalue); + strcat(string,"'"); + } + + if(node->op_type == SP_OPTYPE_INT) { + sprintf(buffer,"%d",node->right.ivalue); + strcat(string,buffer); + } + strcat(string,")"); + + } + } + + + + /** + * generate sql "where" clause + * + * @param node node/tree to calculate + * @returns sql string. Must be freed by caller + */ + char *sp_sql_clause(PARSETREE tree) { + int size; + char *sql; + + size = sp_node_size(tree->tree); + sql = (char*)malloc(size+1); + + memset(sql,0x00,size+1); + sp_serialize_sql(tree->tree,sql); + + return sql; + } + + /** Index: parser-driver.c =================================================================== RCS file: /cvsroot/mt-daapd/mt-daapd/src/parser-driver.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** parser-driver.c 14 Oct 2005 04:11:06 -0000 1.2 --- parser-driver.c 21 Oct 2005 07:48:07 -0000 1.3 *************** *** 36,39 **** --- 36,42 ---- pt=sp_init(); sp_parse(pt,argv[optind]); + + printf("SQL: %s\n",sp_sql_clause(pt)); + sp_dispose(pt); |