--- dbd_sqlite3.c 2008-07-01 19:32:52.000000000 -0300 +++ dbd_sqlite3.c-ok 2008-07-01 19:29:02.000000000 -0300 @@ -632,7 +632,7 @@ char* table; char* my_statement = NULL; char curr_table[MAX_IDENT_LENGTH] = ""; - char curr_field_name[MAX_IDENT_LENGTH]; + char curr_field_name[MAX_IDENT_LENGTH] = ""; char curr_field_name_up[MAX_IDENT_LENGTH]; char sql_command[MAX_IDENT_LENGTH+80]; char **table_result_table; @@ -645,9 +645,45 @@ dbi_error_flag errflag = 0; /* printf("%s\n", statement); */ + + /* replaces field aliases for field names */ + if(curr_field_name[0]=='\0'){ + my_statement = strdup(statement); + if (!my_statement) { + return 0; + } + if (!(table = strstr(my_statement, " from "))) { + table = strstr(my_statement, " FROM "); + } + if(table!=NULL){ + char *token,*saveptr; + *table = '\0'; + token = strtok_r(my_statement, " ;", &saveptr); + while(token!=NULL) { + if(strcmp(token,"as")==0 || strcmp(token,"AS")==0){ + token = strtok_r(NULL, " ;", &saveptr); + if(token==NULL){ + break; + }//if + if(strcmp(token,field)==0){ + strcpy(curr_field_name, table); + }//if + } + else{ + table = token; + } + token = strtok_r(NULL, " ;", &saveptr); + }//while + }//if + free(my_statement); + }//if + if(curr_field_name[0]=='\0'){ + strcpy(curr_field_name, field); + }//if + /* check whether field contains the table info. It does if the notation "table.field" is used */ - item = strchr(field, (int)'.'); + item = strchr(curr_field_name, (int)'.'); if (!item) { /* the field does not contain the table info. However, the latter may be available in the original statement, so let's look @@ -668,7 +704,15 @@ *table = '\0'; /* terminate string, leaves only field names */ - if ((table = strstr(my_statement, field)) != NULL + table = my_statement; + while((table=strstr(table, curr_field_name))!=NULL){ + if(table != my_statement && *(table-1) == '.'){ + break; + } + table++; + }//while + + if (table!=NULL && (table = strstr(table, curr_field_name)) != NULL && table != my_statement && *(table-1) == '.') { /* the field name is there, isolate preceding table */ @@ -727,7 +771,7 @@ values for the field types */ if (!strcmp(curr_table, "sqlite_master") || !strcmp(curr_table, "sqlite_temp_master")) { - if (!strcmp(field, "rootpage")) { + if (!strcmp(curr_field_name, "rootpage")) { return FIELD_TYPE_LONG; } else { @@ -736,14 +780,45 @@ } } free(my_statement); - strcpy(curr_field_name, field); + //strcpy(curr_field_name, field); } else { /* each field contains table info */ - strncpy(curr_table, field, item-field); - curr_table[item-field] = '\0'; - strcpy(curr_field_name, item+1); + strncpy(curr_table, curr_field_name, item-curr_field_name); + curr_table[item-curr_field_name] = '\0'; + //strcpy(curr_field_name, item+1); } + /* replaces table aliases for table names */ + if(curr_table[0]!='\0'){ + my_statement = strdup(statement); + if (!my_statement) { + return 0; + } + if (!(table = strstr(my_statement, " from "))) { + table = strstr(my_statement, " FROM "); + } + if(table!=NULL){ + char *token,*saveptr; + token = strtok_r(table+6, " ;", &saveptr); + while(token!=NULL) { + if(strcmp(token,"as")==0 || strcmp(token,"AS")==0){ + token = strtok_r(NULL, " ;", &saveptr); + if(token==NULL){ + break; + }//if + if(strcmp(token,curr_table)==0){ + strcpy(curr_table, table); + }//if + } + else{ + table = token; + } + token = strtok_r(NULL, " ;", &saveptr); + }//while + }//if + free(my_statement); + }//if + printf("-----------> 0 %s %s \n", curr_field_name, curr_table); /* printf("field went to %s<<\ncurr_table went to %s<<\ncurr_field_name went to %s<<\n", field, curr_table, curr_field_name); */ /* check for known functions which may appear here instead @@ -790,6 +865,7 @@ Look up the field type using the table_info pragma */ snprintf(sql_command, MAX_IDENT_LENGTH+80, "PRAGMA table_info(%s)", curr_table); + printf("-----------> %s %s %s\n", curr_field_name, curr_table, sql_command); query_res = sqlite3_get_table((sqlite3*)conn->connection, sql_command, &table_result_table,