diff --git a/query_classifier/test/canonical_tests/canonizer.c b/query_classifier/test/canonical_tests/canonizer.c index 0a9346f93..5116aa4d4 100644 --- a/query_classifier/test/canonical_tests/canonizer.c +++ b/query_classifier/test/canonical_tests/canonizer.c @@ -19,104 +19,60 @@ static char* server_options[] = { const int num_elements = (sizeof(server_options) / sizeof(char *)) - 1; static char* server_groups[] = { - "embedded", - "server", - "server", - NULL + "embedded", + "server", + "server", + NULL }; int main(int argc, char** argv) { - int fdin,fdout,i=0,fnamelen,fsz,lines = 0; - unsigned int psize; - GWBUF** qbuff; - char *qin, *outnm, *buffer, *tok; + unsigned int psize; + GWBUF* qbuff; + char *tok; + char readbuff[4092]; + FILE* infile; + FILE* outfile; + + if(argc != 3){ + printf("Usage: canonizer \n"); + return 1; + } - if(argc != 3){ - printf("Usage: canonizer \n"); - return 1; - } + if(mysql_library_init(num_elements, server_options, server_groups)){ + printf("Embedded server init failed.\n"); + return 1; + } + + infile = fopen(argv[1],"rb"); + outfile = fopen(argv[2],"wb"); + + if(infile == NULL || outfile == NULL){ + printf("Opening files failed.\n"); + return 1; + } - - - bool failed = mysql_library_init(num_elements, server_options, server_groups); - - if(failed){ - printf("Embedded server init failed.\n"); - return 1; - } - - fnamelen = strlen(argv[1]) + 16; - fdin = open(argv[1],O_RDONLY); - fsz = lseek(fdin,0,SEEK_END); - lseek(fdin,0,SEEK_SET); - - if(!(buffer = malloc(sizeof(char)*fsz + 1))){ - printf("Error: Failed to allocate memory."); - return 1; - } - - read(fdin,buffer,fsz); - buffer[fsz] = '\0'; - - - - i = 0; - int bsz = 4,z=0; - qbuff = calloc(bsz,sizeof(GWBUF*)); - tok = strtok(buffer,"\n"); - - while(tok){ - - if(i>=bsz){ - GWBUF** tmp = calloc(bsz*2,sizeof(GWBUF*)); - if(!tmp){ - printf("Error: Failed to allocate memory."); - return 1; - } - - for(z=0;z 0){ - qin = strdup(tok); - psize = strlen(qin); - qbuff[i] = gwbuf_alloc(psize + 6); - *(qbuff[i]->sbuf->data + 0) = (unsigned char)psize; - *(qbuff[i]->sbuf->data + 1) = (unsigned char)(psize>>8); - *(qbuff[i]->sbuf->data + 2) = (unsigned char)(psize>>16); - *(qbuff[i]->sbuf->data + 4) = 0x03; - memcpy(qbuff[i]->sbuf->data + 5,qin,psize); - *(qbuff[i]->sbuf->data + 5 + psize) = 0x00; - tok = strtok(NULL,"\n\0"); - free(qin); - i++; - } - } - - fdout = open(argv[2],O_TRUNC|O_CREAT|O_WRONLY,S_IRWXU|S_IXGRP|S_IXOTH); - - for(i = 0;isbuf->data + 0) = (unsigned char)psize; + *(qbuff->sbuf->data + 1) = (unsigned char)(psize>>8); + *(qbuff->sbuf->data + 2) = (unsigned char)(psize>>16); + *(qbuff->sbuf->data + 4) = 0x03; + strcpy((char*)(qbuff->start + 5),readbuff); + parse_query(qbuff); + tok = skygw_get_canonical(qbuff); + fprintf(outfile,"%s\n",tok); + free(tok); + gwbuf_free(qbuff); + + } + fclose(infile); + fclose(outfile); + mysql_library_end(); + + return 0; } diff --git a/query_classifier/test/canonical_tests/expected.sql b/query_classifier/test/canonical_tests/expected.sql index 1126f6016..7303d585e 100755 --- a/query_classifier/test/canonical_tests/expected.sql +++ b/query_classifier/test/canonical_tests/expected.sql @@ -1,17 +1,18 @@ -select md5("?") =?, sleep(?), rand(?) -select * from my1 where md5("?") =? -select md5("?") =? -select * from my1 where md5("?") =? -select sleep(?) -select * from tst where lname='?' -select ?,?,?,?,?,? from tst -select * from tst where fname like '?' -select * from tst where lname like '?' order by fname -insert into tst values ("?","?"),("?",?),("?","?") -drop table if exists tst -create table tst(fname varchar(30), lname varchar(30)) -update tst set lname="?" where fname like '?' or lname like '?' -delete from tst where lname like '?' and fname like '?' -select ? from tst where fname='?' or lname like '?' -select ?,?,?,? from tst where name='?' or name='?' or name='?' or name='?' -select count(?),count(?),count(?),count(?),count (?),count(?) from tst +select md5("?") =?, sleep(?), rand(?); +select * from my1 where md5("?") =?; +select md5("?") =?; +select * from my1 where md5("?") =?; +select sleep(?); +select * from tst where lname='?'; +select ?,?,?,?,?,? from tst; +select * from tst where fname like '?'; +select * from tst where lname like '?' order by fname; +insert into tst values ("?","?"),("?",?),("?","?"); +drop table if exists tst; +create table tst(fname varchar(30), lname varchar(30)); +update tst set lname="?" where fname like '?' or lname like '?'; +delete from tst where lname like '?' and fname like '?'; +select ? from tst where fname='?' or lname like '?'; +select ?,?,?,? from tst where name='?' or name='?' or name='?' or name='?'; +select count(?),count(?),count(?),count(?),count (?),count(?) from tst; +select count(?),count(?),count(?),count(?),count (?),count(?) from tst;