Prepare and execute the SQL statement for this layer */
| 498 | |
| 499 | /* Prepare and execute the SQL statement for this layer */ |
| 500 | static int prepare_database(layerObj *layer, rectObj rect, char **query_string) |
| 501 | { |
| 502 | msMSSQL2008LayerInfo *layerinfo; |
| 503 | char *columns_wanted = 0; |
| 504 | char *data_source = 0; |
| 505 | char *f_table_name = 0; |
| 506 | char *geom_table = 0; |
| 507 | /* |
| 508 | "Geometry::STGeomFromText('POLYGON(())',)" + terminator = 40 chars |
| 509 | Plus 10 formatted doubles (15 digits of precision, a decimal point, a space/comma delimiter each = 17 chars each) |
| 510 | Plus SRID + comma - if SRID is a long...we'll be safe with 10 chars |
| 511 | */ |
| 512 | char box3d[40 + 10 * 22 + 11]; |
| 513 | char query_string_temp[10000]; /* Should be big enough */ |
| 514 | int t; |
| 515 | |
| 516 | char *pos_from, *pos_ftab, *pos_space, *pos_paren; |
| 517 | |
| 518 | layerinfo = getMSSQL2008LayerInfo(layer); |
| 519 | |
| 520 | /* Extract the proper f_table_name from the geom_table string. |
| 521 | * We are expecting the geom_table to be either a single word |
| 522 | * or a sub-select clause that possibly includes a join -- |
| 523 | * |
| 524 | * (select column[,column[,...]] from ftab[ natural join table2]) as foo |
| 525 | * |
| 526 | * We are expecting whitespace or a ')' after the ftab name. |
| 527 | * |
| 528 | */ |
| 529 | |
| 530 | geom_table = msStrdup(layerinfo->geom_table); |
| 531 | pos_from = strstrIgnoreCase(geom_table, " from "); |
| 532 | |
| 533 | if(!pos_from) { |
| 534 | f_table_name = (char *) msSmallMalloc(strlen(geom_table) + 1); |
| 535 | strcpy(f_table_name, geom_table); |
| 536 | } else { /* geom_table is a sub-select clause */ |
| 537 | pos_ftab = pos_from + 6; /* This should be the start of the ftab name */ |
| 538 | pos_space = strstr(pos_ftab, " "); /* First space */ |
| 539 | |
| 540 | /* TODO strrchr is POSIX and C99, rindex is neither */ |
| 541 | #if defined(_WIN32) && !defined(__CYGWIN__) |
| 542 | pos_paren = strrchr(pos_ftab, ')'); |
| 543 | #else |
| 544 | pos_paren = rindex(pos_ftab, ')'); |
| 545 | #endif |
| 546 | |
| 547 | if(!pos_space || !pos_paren) { |
| 548 | msSetError(MS_QUERYERR, DATA_ERROR_MESSAGE, "prepare_database()", geom_table, "Error parsing MSSQL2008 data variable: Something is wrong with your subselect statement.<br><br>\n\nMore Help:<br><br>\n\n"); |
| 549 | |
| 550 | return MS_FAILURE; |
| 551 | } |
| 552 | |
| 553 | if (pos_paren < pos_space) { /* closing parenthesis preceeds any space */ |
| 554 | f_table_name = (char *) msSmallMalloc(pos_paren - pos_ftab + 1); |
| 555 | strlcpy(f_table_name, pos_ftab, pos_paren - pos_ftab + 1); |
| 556 | } else { |
| 557 | f_table_name = (char *) msSmallMalloc(pos_space - pos_ftab + 1); |
no test coverage detected