Support "SELECT TOP n [PERCENT]... " queries.

Updated the SQL parser to support "SELECT TOP n [PERCENT]... " queries,
matching the Mocrosoft Access SQL language.

Export these queries from databases with mdb-queries.
This commit is contained in:
James Woodcock
2020-09-26 11:28:34 +01:00
parent 8219e4ef7a
commit eb5dd4d0b7
5 changed files with 64 additions and 7 deletions

View File

@@ -62,9 +62,11 @@ int main (int argc, char **argv) {
//variables for the generation of sql
char *sql_tables = (char *) malloc(bind_size);
char *sql_predicate = (char *) malloc(bind_size);
char *sql_columns = (char *) malloc(bind_size);
char *sql_where = (char *) malloc(bind_size);
char *sql_sorting = (char *) malloc(bind_size);
int flagint;
/* see getopt(3) for more information on getopt and this will become clear */
while ((opt=getopt(argc, argv, "L1d:"))!=-1) {
@@ -148,8 +150,22 @@ int main (int argc, char **argv) {
while (mdb_fetch_row(table)) {
if(strcmp(query_id,objectid) == 0) {
flagint = atoi(flag);
//we have a row for our query
switch(atoi(attribute)) {
case 3: // predicate
if (flagint & 0x30) {
strcpy(sql_predicate, " TOP ");
strcat(sql_predicate, name1);
if (flagint & 0x20) {
strcat(sql_predicate, " PERCENT");
}
} else if (flagint & 0x8) {
strcpy(sql_predicate, " DISTINCTROW");
} else if (flagint & 0x2) {
strcpy(sql_predicate, " DISTINCT");
}
break;
case 5: // table name
if(strcmp(sql_tables,"") == 0) {
strcpy(sql_tables,name1);
@@ -193,9 +209,9 @@ int main (int argc, char **argv) {
/* print out the sql statement */
if(strcmp(sql_where,"") == 0) {
fprintf(stdout,"SELECT %s FROM %s %s\n",sql_columns,sql_tables,sql_sorting);
fprintf(stdout,"SELECT%s %s FROM %s %s\n",sql_predicate,sql_columns,sql_tables,sql_sorting);
} else {
fprintf(stdout,"SELECT %s FROM %s WHERE %s %s\n",sql_columns,sql_tables,sql_where,sql_sorting);
fprintf(stdout,"SELECT%s %s FROM %s WHERE %s %s\n",sql_predicate,sql_columns,sql_tables,sql_where,sql_sorting);
}
mdb_free_tabledef(table);