Ticket #2134: gdal-1.5.0-ogr2ogr-sql.patch

File gdal-1.5.0-ogr2ogr-sql.patch, 3.2 kB (added by dgrichard, 4 months ago)

ogr2ogr sql option to support local file

  • gdal-1.5.0/apps/ogr2ogr.cpp

    old new  
    7777    OGRGeometry *poSpatialFilter = NULL; 
    7878    const char  *pszSelect; 
    7979    char        **papszSelFields = NULL; 
    80     const char  *pszSQLStatement = NULL; 
     80    char        *pszSQLStatement = NULL; 
    8181    int         eGType = -2; 
    8282 
    8383/* -------------------------------------------------------------------- */ 
     
    376376    if( pszSQLStatement != NULL ) 
    377377    { 
    378378        OGRLayer *poResultSet; 
     379        char *sql_pos; 
     380        char *sql_statement; 
     381        int  isFile= FALSE; 
     382 
     383        /* allow passing a local file instead of text for    */ 
     384        /* SQL statement. This hack is due to the            */ 
     385        /* limitation of the command line (See ARG_MAX)      */ 
     386        if( (sql_pos= strstr(pszSQLStatement,"file://")) != NULL ) 
     387        { 
     388#define kSQLSTATEMENT_BUFLEN 10240 
     389             char *sql_file; 
     390             FILE *fp; 
     391             char buf[kSQLSTATEMENT_BUFLEN]; 
     392             sql_file= sql_pos+7; 
     393             size_t size_S= kSQLSTATEMENT_BUFLEN; 
     394 
     395             isFile= TRUE; 
     396             memset(buf,kSQLSTATEMENT_BUFLEN,'\0') ; 
     397             sql_statement= (char*)malloc(kSQLSTATEMENT_BUFLEN) ; 
     398             if ( (fp= fopen(sql_file,"r")) != NULL ) 
     399             { 
     400                 fgets(buf,kSQLSTATEMENT_BUFLEN-1,fp) ; 
     401                 strcpy(sql_statement,buf) ; 
     402                 while( (fgets(buf,kSQLSTATEMENT_BUFLEN-1,fp)) != NULL ) 
     403                 { 
     404                     sql_statement= (char*)realloc(sql_statement,size_S+kSQLSTATEMENT_BUFLEN) ; 
     405                     size_S+= kSQLSTATEMENT_BUFLEN ; 
     406                     strcat(sql_statement,buf) ; 
     407                 } 
     408                 fclose(fp); 
     409             } 
     410             else 
     411             { 
     412                 CPLError( CE_Failure, CPLE_AppDefined, 
     413                           "--sql option : file %s is unreadable.", 
     414                           sql_file); 
     415                 exit( 1 ); 
     416             } 
     417             pszSQLStatement= sql_statement ; 
     418#undef kSQLSTATEMENT_BUFLEN 
     419        } 
    379420 
    380421        if( pszWHERE != NULL ) 
    381422            printf( "-where clause ignored in combination with -sql.\n" ); 
     
    385426        poResultSet = poDS->ExecuteSQL( pszSQLStatement, poSpatialFilter,  
    386427                                        NULL ); 
    387428 
     429        if( isFile ) 
     430        { 
     431          free(pszSQLStatement) ; 
     432        } 
     433 
    388434        if( poResultSet != NULL ) 
    389435        { 
    390436            if( !TranslateLayer( poDS, poResultSet, poODS, papszLCO,  
     
    502548            "                     copy to the new layer (defaults to all)\n"  
    503549            " -where restricted_where: Attribute query (like SQL WHERE)\n"  
    504550            " -sql statement: Execute given SQL statement and save result.\n" 
     551            "                 The statement can be a local file with the\n" 
     552            "                 file://path_to_sql_file syntax.\n" 
    505553            " -skipfailures: skip features or layers that fail to convert\n" 
    506554            " -spat xmin ymin xmax ymax: spatial query extents\n" 
    507555            " -dsco NAME=VALUE: Dataset creation option (format specific)\n"