~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to ogr/ogrsf_frmts/generic/ogrdatasource.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
 
 * $Id: ogrdatasource.cpp 20154 2010-07-28 08:31:53Z rouault $
 
2
 * $Id: ogrdatasource.cpp 23403 2011-11-20 21:01:21Z ajolma $
3
3
 *
4
4
 * Project:  OpenGIS Simple Features Reference Implementation
5
5
 * Purpose:  The generic portions of the OGRDataSource class.
27
27
 * DEALINGS IN THE SOFTWARE.
28
28
 ****************************************************************************/
29
29
 
 
30
#include "swq.h"
30
31
#include "ogrsf_frmts.h"
31
32
#include "ogr_api.h"
32
33
#include "ogr_p.h"
34
35
#include "ogr_attrind.h"
35
36
#include "cpl_multiproc.h"
36
37
 
37
 
CPL_CVSID("$Id: ogrdatasource.cpp 20154 2010-07-28 08:31:53Z rouault $");
 
38
CPL_CVSID("$Id: ogrdatasource.cpp 23403 2011-11-20 21:01:21Z ajolma $");
38
39
 
39
40
/************************************************************************/
40
41
/*                           ~OGRDataSource()                           */
307
308
 
308
309
        if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE )
309
310
        {
310
 
            delete poFeature;
311
311
            CPLError( CE_Failure, CPLE_AppDefined,
312
312
                      "Unable to translate feature %ld from layer %s.\n",
313
313
                      poFeature->GetFID(), poSrcDefn->GetName() );
 
314
            OGRFeature::DestroyFeature( poFeature );
314
315
            return poDstLayer;
315
316
        }
316
317
 
410
411
{
411
412
    VALIDATE_POINTER1( hDS, "OGR_DS_CopyLayer", NULL );
412
413
    VALIDATE_POINTER1( hSrcLayer, "OGR_DS_CopyLayer", NULL );
 
414
    VALIDATE_POINTER1( pszNewName, "OGR_DS_CopyLayer", NULL );
413
415
 
414
416
    return (OGRLayerH) 
415
417
        ((OGRDataSource *) hDS)->CopyLayer( (OGRLayer *) hSrcLayer, 
461
463
    {
462
464
        OGRLayer *poLayer = GetLayer(i);
463
465
 
464
 
        if( strcmp( pszName, poLayer->GetLayerDefn()->GetName() ) == 0 )
 
466
        if( strcmp( pszName, poLayer->GetName() ) == 0 )
465
467
            return poLayer;
466
468
    }
467
469
 
470
472
    {
471
473
        OGRLayer *poLayer = GetLayer(i);
472
474
 
473
 
        if( EQUAL( pszName, poLayer->GetLayerDefn()->GetName() ) )
 
475
        if( EQUAL( pszName, poLayer->GetName() ) )
474
476
            return poLayer;
475
477
    }
476
478
 
534
536
        {
535
537
            poLayer = GetLayer(i);
536
538
            
537
 
            if( EQUAL(poLayer->GetLayerDefn()->GetName(),papszTokens[3]) )
 
539
            if( EQUAL(poLayer->GetName(),papszTokens[3]) )
538
540
                break;
539
541
        }
540
542
        
642
644
        {
643
645
            poLayer = GetLayer(i);
644
646
        
645
 
            if( EQUAL(poLayer->GetLayerDefn()->GetName(),papszTokens[3]) )
 
647
            if( EQUAL(poLayer->GetName(),papszTokens[3]) )
646
648
                break;
647
649
        }
648
650
 
720
722
}
721
723
 
722
724
/************************************************************************/
 
725
/*                        ProcessSQLDropTable()                         */
 
726
/*                                                                      */
 
727
/*      The correct syntax for dropping a table (layer) in the OGR SQL  */
 
728
/*      dialect is:                                                     */
 
729
/*                                                                      */
 
730
/*          DROP TABLE <layername>                                      */
 
731
/************************************************************************/
 
732
 
 
733
OGRErr OGRDataSource::ProcessSQLDropTable( const char *pszSQLCommand )
 
734
 
 
735
{
 
736
    char **papszTokens = CSLTokenizeString( pszSQLCommand );
 
737
 
 
738
/* -------------------------------------------------------------------- */
 
739
/*      Do some general syntax checking.                                */
 
740
/* -------------------------------------------------------------------- */
 
741
    if( CSLCount(papszTokens) != 3
 
742
        || !EQUAL(papszTokens[0],"DROP")
 
743
        || !EQUAL(papszTokens[1],"TABLE") )
 
744
    {
 
745
        CSLDestroy( papszTokens );
 
746
        CPLError( CE_Failure, CPLE_AppDefined, 
 
747
                  "Syntax error in DROP TABLE command.\n"
 
748
                  "Was '%s'\n"
 
749
                  "Should be of form 'DROP TABLE <table>'",
 
750
                  pszSQLCommand );
 
751
        return OGRERR_FAILURE;
 
752
    }
 
753
 
 
754
/* -------------------------------------------------------------------- */
 
755
/*      Find the named layer.                                           */
 
756
/* -------------------------------------------------------------------- */
 
757
    int  i;
 
758
    OGRLayer *poLayer=NULL;
 
759
 
 
760
    for( i = 0; i < GetLayerCount(); i++ )
 
761
    {
 
762
        poLayer = GetLayer(i);
 
763
        
 
764
        if( EQUAL(poLayer->GetName(),papszTokens[2]) )
 
765
            break;
 
766
    }
 
767
    
 
768
    if( i >= GetLayerCount() )
 
769
    {
 
770
        CPLError( CE_Failure, CPLE_AppDefined, 
 
771
                  "DROP TABLE failed, no such layer as `%s'.",
 
772
                  papszTokens[2] );
 
773
        CSLDestroy( papszTokens );
 
774
        return OGRERR_FAILURE;
 
775
    }
 
776
 
 
777
    CSLDestroy( papszTokens );
 
778
 
 
779
/* -------------------------------------------------------------------- */
 
780
/*      Delete it.                                                      */
 
781
/* -------------------------------------------------------------------- */
 
782
 
 
783
    return DeleteLayer( i );
 
784
}
 
785
 
 
786
/************************************************************************/
 
787
/*                    OGRDataSourceParseSQLType()                       */
 
788
/************************************************************************/
 
789
 
 
790
/* All arguments will be altered */
 
791
static OGRFieldType OGRDataSourceParseSQLType(char* pszType, int& nWidth, int &nPrecision)
 
792
{
 
793
    char* pszParenthesis = strchr(pszType, '(');
 
794
    if (pszParenthesis)
 
795
    {
 
796
        nWidth = atoi(pszParenthesis + 1);
 
797
        *pszParenthesis = '\0';
 
798
        char* pszComma = strchr(pszParenthesis + 1, ',');
 
799
        if (pszComma)
 
800
            nPrecision = atoi(pszComma + 1);
 
801
    }
 
802
 
 
803
    OGRFieldType eType = OFTString;
 
804
    if (EQUAL(pszType, "INTEGER"))
 
805
        eType = OFTInteger;
 
806
    else if (EQUAL(pszType, "INTEGER[]"))
 
807
        eType = OFTIntegerList;
 
808
    else if (EQUAL(pszType, "FLOAT") ||
 
809
             EQUAL(pszType, "NUMERIC") ||
 
810
             EQUAL(pszType, "DOUBLE") /* unofficial alias */ ||
 
811
             EQUAL(pszType, "REAL") /* unofficial alias */)
 
812
        eType = OFTReal;
 
813
    else if (EQUAL(pszType, "FLOAT[]") ||
 
814
             EQUAL(pszType, "NUMERIC[]") ||
 
815
             EQUAL(pszType, "DOUBLE[]") /* unofficial alias */ ||
 
816
             EQUAL(pszType, "REAL[]") /* unofficial alias */)
 
817
        eType = OFTRealList;
 
818
    else if (EQUAL(pszType, "CHARACTER") ||
 
819
             EQUAL(pszType, "TEXT") /* unofficial alias */ ||
 
820
             EQUAL(pszType, "STRING") /* unofficial alias */ ||
 
821
             EQUAL(pszType, "VARCHAR") /* unofficial alias */)
 
822
        eType = OFTString;
 
823
    else if (EQUAL(pszType, "TEXT[]") ||
 
824
             EQUAL(pszType, "STRING[]") /* unofficial alias */||
 
825
             EQUAL(pszType, "VARCHAR[]") /* unofficial alias */)
 
826
        eType = OFTStringList;
 
827
    else if (EQUAL(pszType, "DATE"))
 
828
        eType = OFTDate;
 
829
    else if (EQUAL(pszType, "TIME"))
 
830
        eType = OFTTime;
 
831
    else if (EQUAL(pszType, "TIMESTAMP") ||
 
832
             EQUAL(pszType, "DATETIME") /* unofficial alias */ )
 
833
        eType = OFTDateTime;
 
834
    else
 
835
    {
 
836
        CPLError(CE_Warning, CPLE_NotSupported,
 
837
                 "Unsupported column type '%s'. Defaulting to VARCHAR",
 
838
                 pszType);
 
839
    }
 
840
    return eType;
 
841
}
 
842
 
 
843
/************************************************************************/
 
844
/*                    ProcessSQLAlterTableAddColumn()                   */
 
845
/*                                                                      */
 
846
/*      The correct syntax for adding a column in the OGR SQL           */
 
847
/*      dialect is:                                                     */
 
848
/*                                                                      */
 
849
/*          ALTER TABLE <layername> ADD [COLUMN] <columnname> <columntype>*/
 
850
/************************************************************************/
 
851
 
 
852
OGRErr OGRDataSource::ProcessSQLAlterTableAddColumn( const char *pszSQLCommand )
 
853
 
 
854
{
 
855
    char **papszTokens = CSLTokenizeString( pszSQLCommand );
 
856
 
 
857
/* -------------------------------------------------------------------- */
 
858
/*      Do some general syntax checking.                                */
 
859
/* -------------------------------------------------------------------- */
 
860
    const char* pszLayerName = NULL;
 
861
    const char* pszColumnName = NULL;
 
862
    char* pszType = NULL;
 
863
    int iTypeIndex = 0;
 
864
    int nTokens = CSLCount(papszTokens);
 
865
 
 
866
    if( nTokens >= 7
 
867
        && EQUAL(papszTokens[0],"ALTER")
 
868
        && EQUAL(papszTokens[1],"TABLE")
 
869
        && EQUAL(papszTokens[3],"ADD")
 
870
        && EQUAL(papszTokens[4],"COLUMN"))
 
871
    {
 
872
        pszLayerName = papszTokens[2];
 
873
        pszColumnName = papszTokens[5];
 
874
        iTypeIndex = 6;
 
875
    }
 
876
    else if( nTokens >= 6
 
877
             && EQUAL(papszTokens[0],"ALTER")
 
878
             && EQUAL(papszTokens[1],"TABLE")
 
879
             && EQUAL(papszTokens[3],"ADD"))
 
880
    {
 
881
        pszLayerName = papszTokens[2];
 
882
        pszColumnName = papszTokens[4];
 
883
        iTypeIndex = 5;
 
884
    }
 
885
    else
 
886
    {
 
887
        CSLDestroy( papszTokens );
 
888
        CPLError( CE_Failure, CPLE_AppDefined,
 
889
                  "Syntax error in ALTER TABLE ADD COLUMN command.\n"
 
890
                  "Was '%s'\n"
 
891
                  "Should be of form 'ALTER TABLE <layername> ADD [COLUMN] <columnname> <columntype>'",
 
892
                  pszSQLCommand );
 
893
        return OGRERR_FAILURE;
 
894
    }
 
895
 
 
896
/* -------------------------------------------------------------------- */
 
897
/*      Merge type components into a single string if there were split  */
 
898
/*      with spaces                                                     */
 
899
/* -------------------------------------------------------------------- */
 
900
    CPLString osType;
 
901
    for(int i=iTypeIndex;i<nTokens;i++)
 
902
    {
 
903
        osType += papszTokens[i];
 
904
        CPLFree(papszTokens[i]);
 
905
    }
 
906
    pszType = papszTokens[iTypeIndex] = CPLStrdup(osType);
 
907
    papszTokens[iTypeIndex + 1] = NULL;
 
908
 
 
909
/* -------------------------------------------------------------------- */
 
910
/*      Find the named layer.                                           */
 
911
/* -------------------------------------------------------------------- */
 
912
    OGRLayer *poLayer = GetLayerByName(pszLayerName);
 
913
    if( poLayer == NULL )
 
914
    {
 
915
        CPLError( CE_Failure, CPLE_AppDefined,
 
916
                  "%s failed, no such layer as `%s'.",
 
917
                  pszSQLCommand,
 
918
                  pszLayerName );
 
919
        CSLDestroy( papszTokens );
 
920
        return OGRERR_FAILURE;
 
921
    }
 
922
 
 
923
/* -------------------------------------------------------------------- */
 
924
/*      Add column.                                                     */
 
925
/* -------------------------------------------------------------------- */
 
926
 
 
927
    int nWidth = 0, nPrecision = 0;
 
928
    OGRFieldType eType = OGRDataSourceParseSQLType(pszType, nWidth, nPrecision);
 
929
    OGRFieldDefn oFieldDefn(pszColumnName, eType);
 
930
    oFieldDefn.SetWidth(nWidth);
 
931
    oFieldDefn.SetPrecision(nPrecision);
 
932
 
 
933
    CSLDestroy( papszTokens );
 
934
 
 
935
    return poLayer->CreateField( &oFieldDefn );
 
936
}
 
937
 
 
938
/************************************************************************/
 
939
/*                    ProcessSQLAlterTableDropColumn()                  */
 
940
/*                                                                      */
 
941
/*      The correct syntax for droping a column in the OGR SQL          */
 
942
/*      dialect is:                                                     */
 
943
/*                                                                      */
 
944
/*          ALTER TABLE <layername> DROP [COLUMN] <columnname>          */
 
945
/************************************************************************/
 
946
 
 
947
OGRErr OGRDataSource::ProcessSQLAlterTableDropColumn( const char *pszSQLCommand )
 
948
 
 
949
{
 
950
    char **papszTokens = CSLTokenizeString( pszSQLCommand );
 
951
 
 
952
/* -------------------------------------------------------------------- */
 
953
/*      Do some general syntax checking.                                */
 
954
/* -------------------------------------------------------------------- */
 
955
    const char* pszLayerName = NULL;
 
956
    const char* pszColumnName = NULL;
 
957
    if( CSLCount(papszTokens) == 6
 
958
        && EQUAL(papszTokens[0],"ALTER")
 
959
        && EQUAL(papszTokens[1],"TABLE")
 
960
        && EQUAL(papszTokens[3],"DROP")
 
961
        && EQUAL(papszTokens[4],"COLUMN"))
 
962
    {
 
963
        pszLayerName = papszTokens[2];
 
964
        pszColumnName = papszTokens[5];
 
965
    }
 
966
    else if( CSLCount(papszTokens) == 5
 
967
             && EQUAL(papszTokens[0],"ALTER")
 
968
             && EQUAL(papszTokens[1],"TABLE")
 
969
             && EQUAL(papszTokens[3],"DROP"))
 
970
    {
 
971
        pszLayerName = papszTokens[2];
 
972
        pszColumnName = papszTokens[4];
 
973
    }
 
974
    else
 
975
    {
 
976
        CSLDestroy( papszTokens );
 
977
        CPLError( CE_Failure, CPLE_AppDefined,
 
978
                  "Syntax error in ALTER TABLE DROP COLUMN command.\n"
 
979
                  "Was '%s'\n"
 
980
                  "Should be of form 'ALTER TABLE <layername> DROP [COLUMN] <columnname>'",
 
981
                  pszSQLCommand );
 
982
        return OGRERR_FAILURE;
 
983
    }
 
984
 
 
985
/* -------------------------------------------------------------------- */
 
986
/*      Find the named layer.                                           */
 
987
/* -------------------------------------------------------------------- */
 
988
    OGRLayer *poLayer = GetLayerByName(pszLayerName);
 
989
    if( poLayer == NULL )
 
990
    {
 
991
        CPLError( CE_Failure, CPLE_AppDefined,
 
992
                  "%s failed, no such layer as `%s'.",
 
993
                  pszSQLCommand,
 
994
                  pszLayerName );
 
995
        CSLDestroy( papszTokens );
 
996
        return OGRERR_FAILURE;
 
997
    }
 
998
 
 
999
/* -------------------------------------------------------------------- */
 
1000
/*      Find the field.                                                 */
 
1001
/* -------------------------------------------------------------------- */
 
1002
 
 
1003
    int nFieldIndex = poLayer->GetLayerDefn()->GetFieldIndex(pszColumnName);
 
1004
    if( nFieldIndex < 0 )
 
1005
    {
 
1006
        CPLError( CE_Failure, CPLE_AppDefined,
 
1007
                  "%s failed, no such field as `%s'.",
 
1008
                  pszSQLCommand,
 
1009
                  pszColumnName );
 
1010
        CSLDestroy( papszTokens );
 
1011
        return OGRERR_FAILURE;
 
1012
    }
 
1013
 
 
1014
 
 
1015
/* -------------------------------------------------------------------- */
 
1016
/*      Remove it.                                                      */
 
1017
/* -------------------------------------------------------------------- */
 
1018
 
 
1019
    CSLDestroy( papszTokens );
 
1020
 
 
1021
    return poLayer->DeleteField( nFieldIndex );
 
1022
}
 
1023
 
 
1024
/************************************************************************/
 
1025
/*                 ProcessSQLAlterTableRenameColumn()                   */
 
1026
/*                                                                      */
 
1027
/*      The correct syntax for renaming a column in the OGR SQL         */
 
1028
/*      dialect is:                                                     */
 
1029
/*                                                                      */
 
1030
/*       ALTER TABLE <layername> RENAME [COLUMN] <oldname> TO <newname> */
 
1031
/************************************************************************/
 
1032
 
 
1033
OGRErr OGRDataSource::ProcessSQLAlterTableRenameColumn( const char *pszSQLCommand )
 
1034
 
 
1035
{
 
1036
    char **papszTokens = CSLTokenizeString( pszSQLCommand );
 
1037
 
 
1038
/* -------------------------------------------------------------------- */
 
1039
/*      Do some general syntax checking.                                */
 
1040
/* -------------------------------------------------------------------- */
 
1041
    const char* pszLayerName = NULL;
 
1042
    const char* pszOldColName = NULL;
 
1043
    const char* pszNewColName = NULL;
 
1044
    if( CSLCount(papszTokens) == 8
 
1045
        && EQUAL(papszTokens[0],"ALTER")
 
1046
        && EQUAL(papszTokens[1],"TABLE")
 
1047
        && EQUAL(papszTokens[3],"RENAME")
 
1048
        && EQUAL(papszTokens[4],"COLUMN")
 
1049
        && EQUAL(papszTokens[6],"TO"))
 
1050
    {
 
1051
        pszLayerName = papszTokens[2];
 
1052
        pszOldColName = papszTokens[5];
 
1053
        pszNewColName = papszTokens[7];
 
1054
    }
 
1055
    else if( CSLCount(papszTokens) == 7
 
1056
             && EQUAL(papszTokens[0],"ALTER")
 
1057
             && EQUAL(papszTokens[1],"TABLE")
 
1058
             && EQUAL(papszTokens[3],"RENAME")
 
1059
             && EQUAL(papszTokens[5],"TO"))
 
1060
    {
 
1061
        pszLayerName = papszTokens[2];
 
1062
        pszOldColName = papszTokens[4];
 
1063
        pszNewColName = papszTokens[6];
 
1064
    }
 
1065
    else
 
1066
    {
 
1067
        CSLDestroy( papszTokens );
 
1068
        CPLError( CE_Failure, CPLE_AppDefined,
 
1069
                  "Syntax error in ALTER TABLE RENAME COLUMN command.\n"
 
1070
                  "Was '%s'\n"
 
1071
                  "Should be of form 'ALTER TABLE <layername> RENAME [COLUMN] <columnname> <columntype>'",
 
1072
                  pszSQLCommand );
 
1073
        return OGRERR_FAILURE;
 
1074
    }
 
1075
 
 
1076
/* -------------------------------------------------------------------- */
 
1077
/*      Find the named layer.                                           */
 
1078
/* -------------------------------------------------------------------- */
 
1079
    OGRLayer *poLayer = GetLayerByName(pszLayerName);
 
1080
    if( poLayer == NULL )
 
1081
    {
 
1082
        CPLError( CE_Failure, CPLE_AppDefined,
 
1083
                  "%s failed, no such layer as `%s'.",
 
1084
                  pszSQLCommand,
 
1085
                  pszLayerName );
 
1086
        CSLDestroy( papszTokens );
 
1087
        return OGRERR_FAILURE;
 
1088
    }
 
1089
 
 
1090
/* -------------------------------------------------------------------- */
 
1091
/*      Find the field.                                                 */
 
1092
/* -------------------------------------------------------------------- */
 
1093
 
 
1094
    int nFieldIndex = poLayer->GetLayerDefn()->GetFieldIndex(pszOldColName);
 
1095
    if( nFieldIndex < 0 )
 
1096
    {
 
1097
        CPLError( CE_Failure, CPLE_AppDefined,
 
1098
                  "%s failed, no such field as `%s'.",
 
1099
                  pszSQLCommand,
 
1100
                  pszOldColName );
 
1101
        CSLDestroy( papszTokens );
 
1102
        return OGRERR_FAILURE;
 
1103
    }
 
1104
 
 
1105
/* -------------------------------------------------------------------- */
 
1106
/*      Rename column.                                                  */
 
1107
/* -------------------------------------------------------------------- */
 
1108
    OGRFieldDefn* poOldFieldDefn = poLayer->GetLayerDefn()->GetFieldDefn(nFieldIndex);
 
1109
    OGRFieldDefn oNewFieldDefn(poOldFieldDefn);
 
1110
    oNewFieldDefn.SetName(pszNewColName);
 
1111
 
 
1112
    CSLDestroy( papszTokens );
 
1113
 
 
1114
    return poLayer->AlterFieldDefn( nFieldIndex, &oNewFieldDefn, ALTER_NAME_FLAG );
 
1115
}
 
1116
 
 
1117
/************************************************************************/
 
1118
/*                 ProcessSQLAlterTableAlterColumn()                    */
 
1119
/*                                                                      */
 
1120
/*      The correct syntax for altering the type of a column in the     */
 
1121
/*      OGR SQL dialect is:                                             */
 
1122
/*                                                                      */
 
1123
/*   ALTER TABLE <layername> ALTER [COLUMN] <columnname> TYPE <newtype> */
 
1124
/************************************************************************/
 
1125
 
 
1126
OGRErr OGRDataSource::ProcessSQLAlterTableAlterColumn( const char *pszSQLCommand )
 
1127
 
 
1128
{
 
1129
    char **papszTokens = CSLTokenizeString( pszSQLCommand );
 
1130
 
 
1131
/* -------------------------------------------------------------------- */
 
1132
/*      Do some general syntax checking.                                */
 
1133
/* -------------------------------------------------------------------- */
 
1134
    const char* pszLayerName = NULL;
 
1135
    const char* pszColumnName = NULL;
 
1136
    char* pszType = NULL;
 
1137
    int iTypeIndex = 0;
 
1138
    int nTokens = CSLCount(papszTokens);
 
1139
 
 
1140
    if( nTokens >= 8
 
1141
        && EQUAL(papszTokens[0],"ALTER")
 
1142
        && EQUAL(papszTokens[1],"TABLE")
 
1143
        && EQUAL(papszTokens[3],"ALTER")
 
1144
        && EQUAL(papszTokens[4],"COLUMN")
 
1145
        && EQUAL(papszTokens[6],"TYPE"))
 
1146
    {
 
1147
        pszLayerName = papszTokens[2];
 
1148
        pszColumnName = papszTokens[5];
 
1149
        iTypeIndex = 7;
 
1150
    }
 
1151
    else if( nTokens >= 7
 
1152
             && EQUAL(papszTokens[0],"ALTER")
 
1153
             && EQUAL(papszTokens[1],"TABLE")
 
1154
             && EQUAL(papszTokens[3],"ALTER")
 
1155
             && EQUAL(papszTokens[5],"TYPE"))
 
1156
    {
 
1157
        pszLayerName = papszTokens[2];
 
1158
        pszColumnName = papszTokens[4];
 
1159
        iTypeIndex = 6;
 
1160
    }
 
1161
    else
 
1162
    {
 
1163
        CSLDestroy( papszTokens );
 
1164
        CPLError( CE_Failure, CPLE_AppDefined,
 
1165
                  "Syntax error in ALTER TABLE ALTER COLUMN command.\n"
 
1166
                  "Was '%s'\n"
 
1167
                  "Should be of form 'ALTER TABLE <layername> ALTER [COLUMN] <columnname> TYPE <columntype>'",
 
1168
                  pszSQLCommand );
 
1169
        return OGRERR_FAILURE;
 
1170
    }
 
1171
 
 
1172
/* -------------------------------------------------------------------- */
 
1173
/*      Merge type components into a single string if there were split  */
 
1174
/*      with spaces                                                     */
 
1175
/* -------------------------------------------------------------------- */
 
1176
    CPLString osType;
 
1177
    for(int i=iTypeIndex;i<nTokens;i++)
 
1178
    {
 
1179
        osType += papszTokens[i];
 
1180
        CPLFree(papszTokens[i]);
 
1181
    }
 
1182
    pszType = papszTokens[iTypeIndex] = CPLStrdup(osType);
 
1183
    papszTokens[iTypeIndex + 1] = NULL;
 
1184
 
 
1185
/* -------------------------------------------------------------------- */
 
1186
/*      Find the named layer.                                           */
 
1187
/* -------------------------------------------------------------------- */
 
1188
    OGRLayer *poLayer = GetLayerByName(pszLayerName);
 
1189
    if( poLayer == NULL )
 
1190
    {
 
1191
        CPLError( CE_Failure, CPLE_AppDefined,
 
1192
                  "%s failed, no such layer as `%s'.",
 
1193
                  pszSQLCommand,
 
1194
                  pszLayerName );
 
1195
        CSLDestroy( papszTokens );
 
1196
        return OGRERR_FAILURE;
 
1197
    }
 
1198
 
 
1199
/* -------------------------------------------------------------------- */
 
1200
/*      Find the field.                                                 */
 
1201
/* -------------------------------------------------------------------- */
 
1202
 
 
1203
    int nFieldIndex = poLayer->GetLayerDefn()->GetFieldIndex(pszColumnName);
 
1204
    if( nFieldIndex < 0 )
 
1205
    {
 
1206
        CPLError( CE_Failure, CPLE_AppDefined,
 
1207
                  "%s failed, no such field as `%s'.",
 
1208
                  pszSQLCommand,
 
1209
                  pszColumnName );
 
1210
        CSLDestroy( papszTokens );
 
1211
        return OGRERR_FAILURE;
 
1212
    }
 
1213
 
 
1214
/* -------------------------------------------------------------------- */
 
1215
/*      Alter column.                                                   */
 
1216
/* -------------------------------------------------------------------- */
 
1217
 
 
1218
    OGRFieldDefn* poOldFieldDefn = poLayer->GetLayerDefn()->GetFieldDefn(nFieldIndex);
 
1219
    OGRFieldDefn oNewFieldDefn(poOldFieldDefn);
 
1220
 
 
1221
    int nWidth = 0, nPrecision = 0;
 
1222
    OGRFieldType eType = OGRDataSourceParseSQLType(pszType, nWidth, nPrecision);
 
1223
    oNewFieldDefn.SetType(eType);
 
1224
    oNewFieldDefn.SetWidth(nWidth);
 
1225
    oNewFieldDefn.SetPrecision(nPrecision);
 
1226
 
 
1227
    int nFlags = 0;
 
1228
    if (poOldFieldDefn->GetType() != oNewFieldDefn.GetType())
 
1229
        nFlags |= ALTER_TYPE_FLAG;
 
1230
    if (poOldFieldDefn->GetWidth() != oNewFieldDefn.GetWidth() ||
 
1231
        poOldFieldDefn->GetPrecision() != oNewFieldDefn.GetPrecision())
 
1232
        nFlags |= ALTER_WIDTH_PRECISION_FLAG;
 
1233
 
 
1234
    CSLDestroy( papszTokens );
 
1235
 
 
1236
    if (nFlags == 0)
 
1237
        return OGRERR_NONE;
 
1238
    else
 
1239
        return poLayer->AlterFieldDefn( nFieldIndex, &oNewFieldDefn, nFlags );
 
1240
}
 
1241
 
 
1242
/************************************************************************/
723
1243
/*                             ExecuteSQL()                             */
724
1244
/************************************************************************/
725
1245
 
728
1248
                                      const char *pszDialect )
729
1249
 
730
1250
{
731
 
    const char *pszError;
732
1251
    swq_select *psSelectInfo = NULL;
733
1252
 
734
1253
    (void) pszDialect;
736
1255
    swq_field_list sFieldList;
737
1256
    int            nFIDIndex = 0;
738
1257
    OGRGenSQLResultsLayer *poResults = NULL;
 
1258
    char *pszWHERE = NULL;
739
1259
 
740
1260
    memset( &sFieldList, 0, sizeof(sFieldList) );
741
1261
 
758
1278
    }
759
1279
    
760
1280
/* -------------------------------------------------------------------- */
 
1281
/*      Handle DROP TABLE statements specially.                         */
 
1282
/* -------------------------------------------------------------------- */
 
1283
    if( EQUALN(pszStatement,"DROP TABLE",10) )
 
1284
    {
 
1285
        ProcessSQLDropTable( pszStatement );
 
1286
        return NULL;
 
1287
    }
 
1288
 
 
1289
/* -------------------------------------------------------------------- */
 
1290
/*      Handle ALTER TABLE statements specially.                        */
 
1291
/* -------------------------------------------------------------------- */
 
1292
    if( EQUALN(pszStatement,"ALTER TABLE",11) )
 
1293
    {
 
1294
        char **papszTokens = CSLTokenizeString( pszStatement );
 
1295
        if( CSLCount(papszTokens) >= 4 &&
 
1296
            EQUAL(papszTokens[3],"ADD") )
 
1297
        {
 
1298
            ProcessSQLAlterTableAddColumn( pszStatement );
 
1299
            CSLDestroy(papszTokens);
 
1300
            return NULL;
 
1301
        }
 
1302
        else if( CSLCount(papszTokens) >= 4 &&
 
1303
                 EQUAL(papszTokens[3],"DROP") )
 
1304
        {
 
1305
            ProcessSQLAlterTableDropColumn( pszStatement );
 
1306
            CSLDestroy(papszTokens);
 
1307
            return NULL;
 
1308
        }
 
1309
        else if( CSLCount(papszTokens) >= 4 &&
 
1310
                 EQUAL(papszTokens[3],"RENAME") )
 
1311
        {
 
1312
            ProcessSQLAlterTableRenameColumn( pszStatement );
 
1313
            CSLDestroy(papszTokens);
 
1314
            return NULL;
 
1315
        }
 
1316
        else if( CSLCount(papszTokens) >= 4 &&
 
1317
                 EQUAL(papszTokens[3],"ALTER") )
 
1318
        {
 
1319
            ProcessSQLAlterTableAlterColumn( pszStatement );
 
1320
            CSLDestroy(papszTokens);
 
1321
            return NULL;
 
1322
        }
 
1323
        else
 
1324
        {
 
1325
            CPLError( CE_Failure, CPLE_AppDefined,
 
1326
                      "Unsupported ALTER TABLE command : %s",
 
1327
                      pszStatement );
 
1328
            CSLDestroy(papszTokens);
 
1329
            return NULL;
 
1330
        }
 
1331
    }
 
1332
    
 
1333
/* -------------------------------------------------------------------- */
761
1334
/*      Preparse the SQL statement.                                     */
762
1335
/* -------------------------------------------------------------------- */
763
 
    pszError = swq_select_preparse( pszStatement, &psSelectInfo );
764
 
    if( pszError != NULL )
 
1336
    psSelectInfo = new swq_select();
 
1337
    if( psSelectInfo->preparse( pszStatement ) != CPLE_None )
765
1338
    {
766
 
        CPLError( CE_Failure, CPLE_AppDefined, 
767
 
                  "SQL: %s", pszError );
 
1339
        delete psSelectInfo;
768
1340
        return NULL;
769
1341
    }
770
1342
 
796
1368
                              "`%s' required by JOIN.",
797
1369
                              psTableDef->data_source );
798
1370
 
799
 
                swq_select_free( psSelectInfo );
 
1371
                delete psSelectInfo;
800
1372
                goto end;
801
1373
            }
802
1374
 
813
1385
            CPLError( CE_Failure, CPLE_AppDefined, 
814
1386
                      "SELECT from table %s failed, no such table/featureclass.",
815
1387
                      psTableDef->table_name );
816
 
            swq_select_free( psSelectInfo );
 
1388
            delete psSelectInfo;
817
1389
            goto end;
818
1390
        }
819
1391
 
879
1451
/* -------------------------------------------------------------------- */
880
1452
/*      Expand '*' in 'SELECT *' now before we add the pseudo fields    */
881
1453
/* -------------------------------------------------------------------- */
882
 
    pszError = 
883
 
        swq_select_expand_wildcard( psSelectInfo, &sFieldList );
884
 
 
885
 
    if( pszError != NULL )
 
1454
    if( psSelectInfo->expand_wildcard( &sFieldList )  != CE_None )
886
1455
    {
887
 
        swq_select_free( psSelectInfo );
888
 
        CPLError( CE_Failure, CPLE_AppDefined, 
889
 
                  "SQL: %s", pszError );
 
1456
        delete psSelectInfo;
890
1457
        goto end;
891
1458
    }
892
1459
 
902
1469
/* -------------------------------------------------------------------- */
903
1470
/*      Finish the parse operation.                                     */
904
1471
/* -------------------------------------------------------------------- */
905
 
    
906
 
    pszError = swq_select_parse( psSelectInfo, &sFieldList, 0 );
907
 
 
908
 
    if( pszError != NULL )
 
1472
    if( psSelectInfo->parse( &sFieldList, 0 ) != CE_None )
909
1473
    {
910
 
        swq_select_free( psSelectInfo );
911
 
        CPLError( CE_Failure, CPLE_AppDefined, 
912
 
                  "SQL: %s", pszError );
 
1474
        delete psSelectInfo;
913
1475
        goto end;
914
1476
    }
915
1477
 
916
1478
/* -------------------------------------------------------------------- */
 
1479
/*      Extract the WHERE expression to use separately.                 */
 
1480
/* -------------------------------------------------------------------- */
 
1481
    if( psSelectInfo->where_expr != NULL )
 
1482
    {
 
1483
        if (m_poDriver && (
 
1484
                EQUAL(m_poDriver->GetName(), "PostgreSQL") ||
 
1485
                EQUAL(m_poDriver->GetName(), "FileGDB" )) )
 
1486
            pszWHERE = psSelectInfo->where_expr->Unparse( &sFieldList, '"' );
 
1487
        else
 
1488
            pszWHERE = psSelectInfo->where_expr->Unparse( &sFieldList, '\'' );
 
1489
        //CPLDebug( "OGR", "Unparse() -> %s", pszWHERE );
 
1490
    }
 
1491
 
 
1492
/* -------------------------------------------------------------------- */
917
1493
/*      Everything seems OK, try to instantiate a results layer.        */
918
1494
/* -------------------------------------------------------------------- */
919
1495
 
920
1496
    poResults = new OGRGenSQLResultsLayer( this, psSelectInfo, 
921
 
                                           poSpatialFilter );
 
1497
                                           poSpatialFilter,
 
1498
                                           pszWHERE,
 
1499
                                           pszDialect );
 
1500
 
 
1501
    CPLFree( pszWHERE );
922
1502
 
923
1503
    // Eventually, we should keep track of layers to cleanup.
924
1504
 
1099
1679
}
1100
1680
 
1101
1681
/************************************************************************/
 
1682
/*                            GetStyleTable()                           */
 
1683
/************************************************************************/
 
1684
 
 
1685
OGRStyleTable *OGRDataSource::GetStyleTable()
 
1686
{
 
1687
    return m_poStyleTable;
 
1688
}
 
1689
 
 
1690
/************************************************************************/
 
1691
/*                         SetStyleTableDirectly()                      */
 
1692
/************************************************************************/
 
1693
 
 
1694
void OGRDataSource::SetStyleTableDirectly( OGRStyleTable *poStyleTable )
 
1695
{
 
1696
    if ( m_poStyleTable )
 
1697
        delete m_poStyleTable;
 
1698
    m_poStyleTable = poStyleTable;
 
1699
}
 
1700
 
 
1701
/************************************************************************/
 
1702
/*                            SetStyleTable()                           */
 
1703
/************************************************************************/
 
1704
 
 
1705
void OGRDataSource::SetStyleTable(OGRStyleTable *poStyleTable)
 
1706
{
 
1707
    if ( m_poStyleTable )
 
1708
        delete m_poStyleTable;
 
1709
    if ( poStyleTable )
 
1710
        m_poStyleTable = poStyleTable->Clone();
 
1711
}
 
1712
 
 
1713
/************************************************************************/
1102
1714
/*                         OGR_DS_GetStyleTable()                       */
1103
1715
/************************************************************************/
1104
1716