** msPostGISBuildSQLSRID() ** ** Returns malloc'ed char* that must be freed by caller. */
| 1661 | ** Returns malloc'ed char* that must be freed by caller. |
| 1662 | */ |
| 1663 | char *msPostGISBuildSQLSRID(layerObj *layer) { |
| 1664 | |
| 1665 | char *strSRID = NULL; |
| 1666 | msPostGISLayerInfo *layerinfo = NULL; |
| 1667 | |
| 1668 | if (layer->debug) { |
| 1669 | msDebug("msPostGISBuildSQLSRID called.\n"); |
| 1670 | } |
| 1671 | |
| 1672 | assert( layer->layerinfo != NULL); |
| 1673 | |
| 1674 | layerinfo = (msPostGISLayerInfo *)layer->layerinfo; |
| 1675 | |
| 1676 | /* An SRID was already provided in the DATA line. */ |
| 1677 | if ( layerinfo->srid && (strlen(layerinfo->srid) > 0) ) { |
| 1678 | strSRID = msStrdup(layerinfo->srid); |
| 1679 | if( layer->debug > 1 ) { |
| 1680 | msDebug("msPostGISBuildSQLSRID: SRID provided (%s)\n", strSRID); |
| 1681 | } |
| 1682 | } |
| 1683 | /* |
| 1684 | ** No SRID in data line, so extract target table from the 'fromsource'. |
| 1685 | ** Either of form "thetable" (one word) or "(select ... from thetable)" |
| 1686 | ** or "(select ... from thetable where ...)". |
| 1687 | */ |
| 1688 | else { |
| 1689 | char *f_table_name; |
| 1690 | char *strSRIDTemplate = "find_srid('','%s','%s')"; |
| 1691 | char *pos = strstr(layerinfo->fromsource, " "); |
| 1692 | if( layer->debug > 1 ) { |
| 1693 | msDebug("msPostGISBuildSQLSRID: Building find_srid line.\n", strSRID); |
| 1694 | } |
| 1695 | |
| 1696 | if ( ! pos ) { |
| 1697 | /* target table is one word */ |
| 1698 | f_table_name = msStrdup(layerinfo->fromsource); |
| 1699 | if( layer->debug > 1 ) { |
| 1700 | msDebug("msPostGISBuildSQLSRID: Found table (%s)\n", f_table_name); |
| 1701 | } |
| 1702 | } else { |
| 1703 | /* target table is hiding in sub-select clause */ |
| 1704 | pos = strcasestr(layerinfo->fromsource, " from "); |
| 1705 | if ( pos ) { |
| 1706 | char *pos_paren; |
| 1707 | char *pos_space; |
| 1708 | pos += 6; /* should be start of table name */ |
| 1709 | pos_paren = strstr(pos, ")"); /* first ) after table name */ |
| 1710 | pos_space = strstr(pos, " "); /* first space after table name */ |
| 1711 | if ( pos_space < pos_paren ) { |
| 1712 | /* found space first */ |
| 1713 | f_table_name = (char*)msSmallMalloc(pos_space - pos + 1); |
| 1714 | strlcpy(f_table_name, pos, pos_space - pos+1); |
| 1715 | } else { |
| 1716 | /* found ) first */ |
| 1717 | f_table_name = (char*)msSmallMalloc(pos_paren - pos + 1); |
| 1718 | strlcpy(f_table_name, pos, pos_paren - pos+1); |
| 1719 | } |
| 1720 | } else { |
no test coverage detected