File: ogrsdedatasource.cpp

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (1509 lines) | stat: -rw-r--r-- 53,814 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
/******************************************************************************
 * $Id: ogrsdedatasource.cpp 22470 2011-05-31 18:18:26Z warmerdam $
 *
 * Project:  OpenGIS Simple Features Reference Implementation
 * Purpose:  Implements OGRSDEDataSource class.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
 * Copyright (c) 2008, Shawn Gervais <project10@project10.net> 
 * Copyright (c) 2008, Howard Butler <hobu.inc@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#include "ogr_sde.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "gdal.h"

CPL_CVSID("$Id: ogrsdedatasource.cpp 22470 2011-05-31 18:18:26Z warmerdam $");

/************************************************************************/
/*                          OGRSDEDataSource()                           */
/************************************************************************/

OGRSDEDataSource::OGRSDEDataSource()

{
    pszName = NULL;
    papoLayers = NULL;
    nLayers = 0;

    bDSVersionLocked = TRUE;
    bDSUpdate = FALSE;
    bDSUseVersionEdits = FALSE;
    
    nState = SE_DEFAULT_STATE_ID;
    nNextState = -2;
    hConnection = NULL;
    hVersion = NULL;
}

/************************************************************************/
/*                          ~OGRSDEDataSource()                          */
/************************************************************************/

OGRSDEDataSource::~OGRSDEDataSource()

{
    int         i;
    LONG        nSDEErr;
    char       pszVersionName[SE_MAX_VERSION_LEN];
    
    
    // Commit our transactions if we were opened for update
    if (bDSUpdate && bDSUseVersionEdits && (nNextState != -2 && nState != SE_DEFAULT_STATE_ID )  ) {
        CPLDebug("OGR_SDE", "Moving states from %ld to %ld", 
                 (long) nState, (long) nNextState);

        SE_connection_commit_transaction(hConnection);
        nSDEErr = SE_state_close(hConnection, nNextState);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_state_close" );
        }
        nSDEErr = SE_versioninfo_get_name(hVersion, pszVersionName);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_versioninfo_get_name" );
        }  
        nSDEErr = SE_version_free_lock(hConnection, pszVersionName);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_version_free_lock" );
        }  
        nSDEErr = SE_version_change_state(hConnection, hVersion, nNextState);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_version_change_state" );
        }
        nSDEErr = SE_state_trim_tree(hConnection, nState, nNextState);
        if( nSDEErr != SE_SUCCESS && nSDEErr != SE_STATE_INUSE && nSDEErr != SE_STATE_USED_BY_VERSION)
        {

            IssueSDEError( nSDEErr, "SE_state_trim_tree" );
        }

        bDSVersionLocked = TRUE;      

    }

    CPLFree( pszName );

    for( i = 0; i < nLayers; i++ )
        delete papoLayers[i];

    CPLFree( papoLayers );

    if (hVersion != NULL) 
    {
        SE_versioninfo_free(hVersion); 
    }   
    
    if( hConnection != NULL )
    {
        SE_connection_free( hConnection );
    }

 

}

/************************************************************************/
/*                           IssueSDEError()                            */
/************************************************************************/

void OGRSDEDataSource::IssueSDEError( int nErrorCode, 
                                      const char *pszFunction )

{
    char szErrorMsg[SE_MAX_MESSAGE_LENGTH+1];
    LONG nSDEErr;
    char pszVersionName[SE_MAX_VERSION_LEN];
    
    if( pszFunction == NULL )
        pszFunction = "SDE";
       
    if (bDSUpdate && bDSUseVersionEdits && !bDSVersionLocked) {
        // try to clean up our state/transaction mess if we can
        nSDEErr = SE_state_delete(hConnection, nNextState);
        if (nSDEErr && nSDEErr != SE_STATE_INUSE) {
            SE_error_get_string( nSDEErr, szErrorMsg );
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "SE_state_delete could not complete in IssueSDEError %d/%s", 
                      nErrorCode, szErrorMsg );
        }
        nSDEErr = SE_versioninfo_get_name(hVersion, pszVersionName);
        if( nSDEErr != SE_SUCCESS )
        {
            SE_error_get_string( nSDEErr, szErrorMsg );
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "SE_versioninfo_get_name could not complete in IssueSDEError %d/%s", 
                      nErrorCode, szErrorMsg );
        }  
        nSDEErr = SE_version_free_lock(hConnection, pszVersionName);
        if( nSDEErr != SE_SUCCESS )
        {
            SE_error_get_string( nSDEErr, szErrorMsg );
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "SE_version_free_lock could not complete in IssueSDEError %d/%s", 
                      nErrorCode, szErrorMsg );
        }  
        nSDEErr = SE_connection_rollback_transaction(hConnection);
        if (nSDEErr) {
            SE_error_get_string( nSDEErr, szErrorMsg );
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "SE_connection_rollback_transaction could not complete in IssueSDEError %d/%s", 
                      nErrorCode, szErrorMsg );
        }
    }
    bDSVersionLocked = TRUE;
    SE_error_get_string( nErrorCode, szErrorMsg );

    CPLError( CE_Failure, CPLE_AppDefined, 
              "%s: %d/%s", 
              pszFunction, nErrorCode, szErrorMsg );
}

/************************************************************************/
/*                                Open()                                */
/************************************************************************/

int OGRSDEDataSource::Open( const char * pszNewName, int bUpdate )

{
    CPLAssert( nLayers == 0 );

/* -------------------------------------------------------------------- */
/*      If we aren't prefixed with SDE: then ignore this datasource.    */
/* -------------------------------------------------------------------- */
    if( !EQUALN(pszNewName,"SDE:",4) )
        return FALSE;

/* -------------------------------------------------------------------- */
/*      Parse arguments on comma.  We expect (layer is optional):       */
/*        SDE:server,instance,database,username,password,layer          */
/* -------------------------------------------------------------------- */
    char **papszTokens = CSLTokenizeStringComplex( pszNewName+4, ",",
                                                   TRUE, TRUE );

    CPLDebug( "OGR_SDE", "Open(\"%s\") revealed %d tokens.", pszNewName,
              CSLCount( papszTokens ) );

    if( CSLCount( papszTokens ) < 5 || CSLCount( papszTokens ) > 8 )
    {
        CPLError( CE_Failure, CPLE_OpenFailed, 
                  "SDE connect string had wrong number of arguments.\n"
                  "Expected 'SDE:server,instance,database,username,password,layer'\n"
		          "The layer name value is optional.\n"
                  "Got '%s'", 
                  pszNewName );
        return FALSE;
    }

/* -------------------------------------------------------------------- */
/*      Try to establish connection.                                    */
/* -------------------------------------------------------------------- */
    int 	nSDEErr;
    SE_ERROR    sSDEErrorInfo;

    nSDEErr = SE_connection_create( papszTokens[0], 
                                    papszTokens[1], 
                                    papszTokens[2], 
                                    papszTokens[3],
                                    papszTokens[4],
                                    &sSDEErrorInfo, &hConnection );

    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_connection_create" );
        CSLDestroy( papszTokens );
        return FALSE;
    }
    
    pszName = CPLStrdup( pszNewName );
    
    bDSUpdate = bUpdate;

    // Use SDE Versioned edits by default
    const char* pszVersionEdits;
    pszVersionEdits = CPLGetConfigOption( "SDE_VERSIONEDITS", "TRUE" );
    if (EQUAL(pszVersionEdits,"TRUE")) {
        bDSUseVersionEdits = TRUE;
    } else {
        bDSUseVersionEdits = FALSE;
    }

    
/* -------------------------------------------------------------------- */
/*      Set unprotected concurrency policy, suitable for single         */
/*      threaded access.                                                */
/* -------------------------------------------------------------------- */
    nSDEErr = SE_connection_set_concurrency( hConnection,
                                             SE_UNPROTECTED_POLICY);

    if( nSDEErr != SE_SUCCESS) {
        IssueSDEError( nSDEErr, NULL );
        CSLDestroy( papszTokens );
        return FALSE;
    }

/* -------------------------------------------------------------------- */
/*      Open a selected layer only, or else treat all known spatial     */
/*      tables as layers.                                               */
/* -------------------------------------------------------------------- */
    if ( CSLCount( papszTokens ) == 6 && *papszTokens[5] != '\0' )
    {
        OpenSpatialTable( papszTokens[5] );
    }


/* -------------------------------------------------------------------- */
/*      Create a new version from the parent version if we were given   */
/*      both the child and parent version values                        */
/* -------------------------------------------------------------------- */

    if ( CSLCount( papszTokens ) == 8 && *papszTokens[7] != '\0' )
    {
        CPLDebug("OGR_SDE", "Creating child version %s from parent version  %s", papszTokens[7],papszTokens[6]);
        CPLDebug("OGR_SDE", "Opening layer %s", papszTokens[5]);
        OpenSpatialTable( papszTokens[5] );
        nSDEErr = CreateVersion(papszTokens[6],papszTokens[7]);
        bDSVersionLocked = FALSE;
        if (!nSDEErr)
        {
            // We've already set the error
            CSLDestroy( papszTokens );
            return FALSE;
        }        
    }
    
/* -------------------------------------------------------------------- */
/*      Fetch the specified version or use SDE.DEFAULT if none is       */
/*      specified.                                                      */
/* -------------------------------------------------------------------- */

    if ( CSLCount( papszTokens ) == 7 && *papszTokens[6] != '\0' )
    {
        CPLDebug("OGR_SDE", "Setting version to %s", papszTokens[6]);
        CPLDebug("OGR_SDE", "Opening layer %s", papszTokens[5]);
        OpenSpatialTable( papszTokens[5] );
        nSDEErr = SetVersionState(papszTokens[6]);
        if (!nSDEErr)
        {
            // We've already set the error
            CSLDestroy( papszTokens );
            return FALSE;
        }        
    }
    else if ( CSLCount( papszTokens ) == 8 && *papszTokens[7] != '\0' )
    {
        // For user-specified version names, the input is not fully qualified
        // We have to append the connection's username to the version name for 
        // SDE to be able to find it.
        char username[SE_MAX_OWNER_LEN];
        nSDEErr = SE_connection_get_user_name(hConnection, username);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_connection_get_user_name" );
            CSLDestroy( papszTokens );
            return FALSE;
        }

        const char* pszVersionName= CPLSPrintf( "%s.%s", username, papszTokens[7]);

        
        CPLDebug("OGR_SDE", "Setting version to %s", pszVersionName);
        CPLDebug("OGR_SDE", "Opening layer %s", papszTokens[5]);
        OpenSpatialTable( papszTokens[5] );
        nSDEErr = SetVersionState(pszVersionName);
        if (!nSDEErr)
        {
            // We've already set the error
            CSLDestroy( papszTokens );
            return FALSE;
        }        
        
    }

    else
    {
        CPLDebug("OGR_SDE", "Setting version to SDE.DEFAULT");
        nSDEErr = SetVersionState("SDE.DEFAULT");
        EnumerateSpatialTables();
        if (!nSDEErr)
        {
            // We've already set the error
            CSLDestroy( papszTokens );
            return FALSE;
        }        

    }
    CSLDestroy( papszTokens );
 
    return TRUE;
}


/************************************************************************/
/*                             CreateVersion()                          */
/************************************************************************/
int OGRSDEDataSource::CreateVersion( const char* pszParentVersion, const char* pszChildVersion) {
    SE_VERSIONINFO hParentVersion = NULL;
    SE_VERSIONINFO hChildVersion = NULL;
    SE_VERSIONINFO hDummyVersion = NULL;
    
    LONG nSDEErr;
    nSDEErr = SE_versioninfo_create(&hParentVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_create" );
        return FALSE;
    }

    nSDEErr = SE_versioninfo_create(&hChildVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_create" );
        return FALSE;
    }

    const char* pszOverwriteVersion =  CPLGetConfigOption( "SDE_VERSIONOVERWRITE", "FALSE" );
    if( EQUAL(pszOverwriteVersion, "TRUE") && bDSUpdate ) {
        nSDEErr = SE_version_delete(hConnection, pszChildVersion);
        
        // if the version didn't exist in the first place, just continue on.
        if( nSDEErr != SE_SUCCESS && nSDEErr != SE_VERSION_NOEXIST)
        {
            IssueSDEError( nSDEErr, "SE_version_delete" );
            return FALSE;
        }
    }

    // Attempt to use the child version if it is there.
    nSDEErr = SE_version_get_info(hConnection, pszChildVersion, hChildVersion);
    if( nSDEErr != SE_SUCCESS)
    {   
        if (nSDEErr != SE_VERSION_NOEXIST) {
            IssueSDEError( nSDEErr, "SE_version_get_info child" );
            return FALSE;
        } 
    } else { 
        SE_versioninfo_free(hParentVersion);
        SE_versioninfo_free(hChildVersion);
        return TRUE; 
    }

    if (!bDSUpdate) {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "The version %s does not exist and cannot be created because the datasource is not in update mode", 
                  pszChildVersion);
        return FALSE;
    }
    
    nSDEErr = SE_version_get_info(hConnection, pszParentVersion, hParentVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        // this usually denotes incongruent versions of the client 
        // and server.  If this is the case, we're going to attempt to 
        // not do versioned queries at all.
        if ( nSDEErr == SE_INVALID_RELEASE ) {
            CPLDebug("OGR_SDE", "nState was set to SE_INVALID_RELEASE\n\n\n");
            // leave nState set to SE_DEFAULT_STATE_ID
            SE_versioninfo_free(hParentVersion);
            hParentVersion = NULL;
            IssueSDEError( nSDEErr, "SE_INVALID_RELEASE."
                           "  Your client/server versions must not match or " 
                           "you have some other major configuration problem");
            return FALSE;
            
        } else {
            IssueSDEError( nSDEErr, "SE_version_get_info parent" );
            return FALSE;
        }
    } 

    // Fill in details of our child version from our parent version
    nSDEErr = SE_versioninfo_set_name(hChildVersion, pszChildVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_set_name "
                                "Version names must be in the form \"MYVERSION\""
                                "not \"SDE.MYVERSION\"" );
        return FALSE;
    }    

    nSDEErr = SE_versioninfo_set_access(hChildVersion, SE_VERSION_ACCESS_PUBLIC);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_set_access" );
        return FALSE;
    }    

    const char* pszDescription =  CPLGetConfigOption( "SDE_DESCRIPTION", "Created by OGR" );
    nSDEErr = SE_versioninfo_set_description(hChildVersion, pszDescription);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_set_description" );
        return FALSE;
    }    

    nSDEErr = SE_versioninfo_set_parent_name(hChildVersion, pszParentVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_set_parent_name" );
        return FALSE;
    }
    
    LONG nStateID;
    nSDEErr = SE_versioninfo_get_state_id(hParentVersion, &nStateID);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_get_state_id" );
        return FALSE;
    }
    nSDEErr = SE_versioninfo_set_state_id(hChildVersion, nStateID);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_set_parent_name" );
        return FALSE;
    }

    nSDEErr = SE_versioninfo_create(&hDummyVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_create" );
        return FALSE;
    }
    nSDEErr = SE_version_create(hConnection, hChildVersion, FALSE, hDummyVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_version_create" );
        return FALSE;
    }
    
    SE_versioninfo_free(hParentVersion);
    SE_versioninfo_free(hChildVersion);
    SE_versioninfo_free(hDummyVersion);
    
    return TRUE;
}

/************************************************************************/
/*                             SetVersionState()                        */
/************************************************************************/
int OGRSDEDataSource::SetVersionState( const char* pszVersionName ) {

    int nSDEErr;
    SE_STATEINFO hCurrentStateInfo= NULL;
    SE_STATEINFO hNextStateInfo= NULL;    
    SE_STATEINFO hDummyStateInfo = NULL;

    nSDEErr = SE_versioninfo_create(&hVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_create" );
        return FALSE;
    }
    nSDEErr = SE_version_get_info(hConnection, pszVersionName, hVersion);
    if( nSDEErr != SE_SUCCESS )
    {
        // this usually denotes incongruent versions of the client 
        // and server.  If this is the case, we're going to attempt to 
        // not do versioned queries at all.
        if ( nSDEErr == SE_INVALID_RELEASE ) {
            CPLDebug("OGR_SDE", "nState was set to SE_INVALID_RELEASE\n\n\n");
            // leave nState set to SE_DEFAULT_STATE_ID
            SE_versioninfo_free(hVersion);
            hVersion = NULL;
            IssueSDEError( nSDEErr, "SE_INVALID_RELEASE."
                           "  Your client/server versions must not match or " 
                           "you have some other major configuration problem");
            return FALSE;
            
        } else {
            IssueSDEError( nSDEErr, "SE_version_get_info" );
            return FALSE;
        }
    } 
    
    nSDEErr = SE_versioninfo_get_state_id(hVersion, &nState);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_versioninfo_get_state_id" );
        return FALSE;
    }


    
    if (bDSUpdate && bDSUseVersionEdits) {
        LONG nLockCount = 0;
        SE_VERSION_LOCK* pahLocks = NULL;
        nSDEErr = SE_version_get_locks(hConnection, pszVersionName, &nLockCount, &pahLocks);
    
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_version_get_locks" );
            return FALSE;
        }
    
        if (nLockCount > 0) 
        {
            // This version is already locked for edit.  We can't edit this 
            // version right now until the lock is released.  All we can do is issue
            // an error.

            SE_version_free_locks(pahLocks, nLockCount);
            bDSVersionLocked = TRUE;
            CPLError( CE_Failure, CPLE_AppDefined, 
                      "The %s version is already locked and open for edit", pszVersionName);            
            return FALSE;
        }

        // So we're in update mode.  We need to get the state id 
        // of the active version, create a child state of it to 
        // push our edits onto, and close the state and move the 
        // version to it when we're done. 

        nSDEErr = SE_connection_start_transaction(hConnection);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_connection_start_transaction" );
            return FALSE;
        } 
        
        // Lock the version we're editing on so no one can change the state 
        // of it underneath us.  SHARED_LOCK is the same lock mode that ArcGIS uses,
        // and it means the state of the version cannot be moved until until we 
        // release the lock and move it.
        nSDEErr = SE_version_lock(hConnection, pszVersionName, SE_VERSION_SHARED_LOCK);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_version_lock" );
            return FALSE;
        } 
        nSDEErr = SE_stateinfo_create(&hCurrentStateInfo);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_stateinfo_create" );
            return FALSE;
        }
        nSDEErr = SE_state_get_info(hConnection, nState, hCurrentStateInfo);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_state_get_info" );
            return FALSE;
        }
        if (SE_stateinfo_is_open(hCurrentStateInfo)) {
                CPLError( CE_Failure, CPLE_AppDefined, 
                          "The editing state for this version is currently open.  "
                          "It must be closed for edits before it can be opened by OGR for update. ");
                return FALSE;            
        }
        nSDEErr = SE_stateinfo_create(&hNextStateInfo);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_stateinfo_create" );
            return FALSE;
        }
        
        nSDEErr = SE_stateinfo_create(&hDummyStateInfo);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_stateinfo_create" );
            return FALSE;
        }
        nSDEErr = SE_state_create(hConnection, hDummyStateInfo, nState, hNextStateInfo);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_state_create" );
            return FALSE;
        }

        nSDEErr = SE_stateinfo_get_id(hNextStateInfo, &nNextState);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_stateinfo_get_id" );
            return FALSE;
        }
        nSDEErr = SE_state_open(hConnection, nNextState);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_state_open" );
            return FALSE;
        }
        SE_stateinfo_free(hDummyStateInfo);
        SE_stateinfo_free(hCurrentStateInfo);
        SE_stateinfo_free(hNextStateInfo);

    }
    return TRUE; 


}
/************************************************************************/
/*                             OpenTable()                              */
/************************************************************************/

int OGRSDEDataSource::OpenTable( const char *pszTableName, 
                                 const char *pszFIDColumn,
                                 const char *pszShapeColumn,
                                 LONG nFIDColType )

{
/* -------------------------------------------------------------------- */
/*      Create the layer object.                                        */
/* -------------------------------------------------------------------- */
    OGRSDELayer  *poLayer;

    poLayer = new OGRSDELayer( this, bDSUpdate );

    if( !poLayer->Initialize( pszTableName, pszFIDColumn, pszShapeColumn ) )
    {
        delete poLayer;
        return FALSE;
    }
    
    poLayer->SetFIDColType( nFIDColType );

/* -------------------------------------------------------------------- */
/*      Add layer to data source layer list.                            */
/* -------------------------------------------------------------------- */
    papoLayers = (OGRSDELayer **)
        CPLRealloc( papoLayers,  sizeof(OGRSDELayer *) * (nLayers+1) );
    papoLayers[nLayers++] = poLayer;

    return TRUE;
}

/************************************************************************/
/*                            DeleteLayer()                             */
/************************************************************************/

OGRErr OGRSDEDataSource::DeleteLayer( int iLayer )

{
    if( iLayer < 0 || iLayer >= nLayers )
        return OGRERR_FAILURE;
    
/* -------------------------------------------------------------------- */
/*      Blow away our OGR structures related to the layer.  This is     */
/*      pretty dangerous if anything has a reference to this layer!     */
/* -------------------------------------------------------------------- */
    
    
    OGRSDELayer* poLayer = (OGRSDELayer*) papoLayers[iLayer];
    
    CPLString osGeometryName = poLayer->osShapeColumnName;
    CPLString osLayerName = poLayer->GetLayerDefn()->GetName();
    
    CPLDebug( "OGR_SDE", 
              "DeleteLayer(%s,%s)", 
              osLayerName.c_str(),
              osGeometryName.c_str() );

    delete papoLayers[iLayer];
    memmove( papoLayers + iLayer, papoLayers + iLayer + 1, 
             sizeof(void *) * (nLayers - iLayer - 1) );
    nLayers--;

/* -------------------------------------------------------------------- */
/*      Remove from the database.                                       */
/* -------------------------------------------------------------------- */
    LONG nSDEErr;
    char** paszTables;
    LONG nCount;
    char pszVersionName[SE_MAX_VERSION_LEN];
    
    nSDEErr = SE_layer_delete( hConnection, osLayerName.c_str(), osGeometryName.c_str());
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_layer_delete" );
        return OGRERR_FAILURE;
    }

    nSDEErr = SE_registration_get_dependent_tables(hConnection, osLayerName.c_str(), &paszTables, &nCount);
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_registration_get_dependent_tables" );
        return OGRERR_FAILURE;
    }

    for (int i=0; i<nCount; i++) {
        CPLDebug("OGR_SDE", "Dependent multiversion table: %s", paszTables[i]);
    }
    
    // if we still have dependent tables after deleting the layer, it is because the 
    // table is multiversion.  We need to smash the table to single version before 
    // deleting its registration.  If the user deletes the table from this version, 
    // all other versions are gone too.
    if (nCount) {
        nSDEErr = SE_versioninfo_get_name(hVersion, pszVersionName);
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_versioninfo_get_name" );
            return OGRERR_FAILURE;
        }        
        nSDEErr = SE_registration_make_single_version(hConnection, pszVersionName, osLayerName.c_str());
        if( nSDEErr != SE_SUCCESS )
        {
            IssueSDEError( nSDEErr, "SE_registration_make_single_version" );
            return OGRERR_FAILURE;
        }
    }

    SE_registration_free_dependent_tables(paszTables, &nCount);

    nSDEErr = SE_registration_delete( hConnection, osLayerName.c_str() );
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_registration_delete" );
        return OGRERR_FAILURE;
    }
    
    nSDEErr = SE_table_delete( hConnection, osLayerName.c_str() );
    
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_table_delete" );
        return OGRERR_FAILURE;
    }
    
    CPLDebug( "OGR_SDE", "DeleteLayer(%s) successful", osLayerName.c_str() );

    return OGRERR_NONE;
}


/************************************************************************/
/*                            CleanupLayerCreation()                    */
/************************************************************************/
void OGRSDEDataSource::CleanupLayerCreation(const char* pszLayerName)
{
    
    LONG nSDEErr;


    nSDEErr = SE_registration_delete( hConnection, pszLayerName );
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_registration_delete" );
    }
    
    nSDEErr = SE_table_delete( hConnection, pszLayerName);
    
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_table_delete" );
    }
    
    CPLDebug( "OGR_SDE", "CleanupLayerCreation(%s) successful", pszLayerName );

}

/************************************************************************/
/*                            CreateLayer()                             */
/************************************************************************/

OGRLayer *
OGRSDEDataSource::CreateLayer( const char * pszLayerName,
                               OGRSpatialReference *poSRS,
                               OGRwkbGeometryType eType,
                               char ** papszOptions )

{
    LONG                nSDEErr;

/* -------------------------------------------------------------------- */
/*      Do we already have this layer?  If so, should we blow it        */
/*      away?                                                           */
/* -------------------------------------------------------------------- */
    int iLayer;
    CPLString osFullName = pszLayerName;

    if( strchr(pszLayerName,'.') == NULL )
        osFullName = "SDE." + osFullName;

    for( iLayer = 0; iLayer < nLayers; iLayer++ )
    {
        // We look for an exact match or for SDE.layername which is how
        // the layer will be known after reading back. 
        if( EQUAL(osFullName,
                  papoLayers[iLayer]->GetLayerDefn()->GetName())
            || EQUAL(pszLayerName, 
                     papoLayers[iLayer]->GetLayerDefn()->GetName()) )
        {
            if( CSLFetchBoolean( papszOptions, "OVERWRITE", FALSE ) )
            {
                DeleteLayer( iLayer );
            }
            else
            {
                CPLError( CE_Failure, CPLE_AppDefined, 
                          "Layer %s already exists, CreateLayer failed.\n"
                          "Use the layer creation option OVERWRITE=YES to "
                          "replace it.",
                          pszLayerName );
                return NULL;
            }
        }
    }

/* -------------------------------------------------------------------- */
/*      Sometimes there are residual layers left around and we need     */
/*      to blow them away.                                              */
/* -------------------------------------------------------------------- */
    SE_REGINFO *ahTableList;
    LONG nTableListCount;
    int iTable;

    nSDEErr = SE_registration_get_info_list( hConnection, &ahTableList,
                                             &nTableListCount );
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_registration_get_info_list" );
        return NULL;
    }

    for( iTable = 0; iTable < nTableListCount; iTable++ )
    {
        char szTableName[SE_QUALIFIED_TABLE_NAME+1];
        szTableName[0] = '\0';

        SE_reginfo_get_table_name( ahTableList[iTable], szTableName );
        if( EQUAL(szTableName,pszLayerName) 
            || EQUAL(szTableName,osFullName) )
        {
            if( !CSLFetchBoolean( papszOptions, "OVERWRITE", FALSE ) )
            {
                CPLError( CE_Failure, CPLE_AppDefined, 
                          "Registration informatin for  %s already exists, CreateLayer failed.\n"
                          "Use the layer creation option OVERWRITE=YES to pre-clear it.",
                          pszLayerName );
                return NULL;
            }

            CPLDebug( "SDE", "sde_layer_delete(%s) - hidden/residual layer.",
                      osFullName.c_str() );

            SE_layer_delete( hConnection, szTableName, "SHAPE");
            SE_registration_delete( hConnection, szTableName );
            SE_table_delete( hConnection, szTableName );
        }
    }

    SE_registration_free_info_list( nTableListCount, ahTableList );

/* -------------------------------------------------------------------- */
/*      Get various layer creation options.                             */
/* -------------------------------------------------------------------- */
    const char         *pszExpectedFIDName;
    const char         *pszGeometryName;
    const char         *pszDbtuneKeyword;
    const char         *pszLayerDescription;


    pszGeometryName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME" );
    if( pszGeometryName == NULL )
        pszGeometryName = "SHAPE";

    
    pszExpectedFIDName = CPLGetConfigOption( "SDE_FID", "OBJECTID" );

    pszDbtuneKeyword = CSLFetchNameValue( papszOptions, "SDE_KEYWORD" );
    if( pszDbtuneKeyword == NULL )
        pszDbtuneKeyword = "DEFAULTS";
    
    // Set layer description
    pszLayerDescription = CSLFetchNameValue( papszOptions, "SDE_DESCRIPTION" );
    if( pszLayerDescription == NULL )
        pszLayerDescription = CPLSPrintf( "Created by GDAL/OGR %s", 
                                      GDALVersionInfo( "RELEASE_NAME" ) );

/* -------------------------------------------------------------------- */
/*      Create a basic table with the FID column                        */
/* -------------------------------------------------------------------- */
    SE_COLUMN_DEF       sColumnDef;
 
    /*
     * Setting the size and decimal_digits to 0 instructs SDE to
     * use default values for the SE_INTEGER_TYPE - these might be specific
     * to the underlying RDBMS.
     */
    strcpy( sColumnDef.column_name, pszExpectedFIDName );
    sColumnDef.sde_type       = SE_INTEGER_TYPE;
    sColumnDef.size           = 0;
    sColumnDef.decimal_digits = 0;
    sColumnDef.nulls_allowed  = FALSE;
    
    nSDEErr = SE_table_create( hConnection, pszLayerName, 1, &sColumnDef, 
                               pszDbtuneKeyword );
    
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_table_create" );
        return NULL;
    }


/* -------------------------------------------------------------------- */
/*      Convert the OGRSpatialReference to a SDE coordref object        */
/* -------------------------------------------------------------------- */
    SE_COORDREF         hCoordRef;
    
    if( ConvertOSRtoSDESpatRef( poSRS, &hCoordRef ) != OGRERR_NONE )
    {
        CPLError( CE_Failure, CPLE_AppDefined,
                  "Cannot create layer %s: Unable to convert "
                  "OGRSpatialReference to SDE SE_COORDREF.",
                  pszLayerName );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }


/* -------------------------------------------------------------------- */
/*      Construct the layer info necessary to spatially enable          */
/*      the table.                                                      */
/* -------------------------------------------------------------------- */
    SE_LAYERINFO        hLayerInfo;
    SE_ENVELOPE         sLayerEnvelope;
    LONG                nLayerShapeTypes = 0L;
    
    nSDEErr = SE_layerinfo_create( hCoordRef, &hLayerInfo );
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_layerinfo_create" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    // Determine the type of geometries that this layer will allow
    nLayerShapeTypes |= SE_NIL_TYPE_MASK;
    
    if( wkbFlatten(eType) == wkbPoint || wkbFlatten(eType) == wkbMultiPoint )
        nLayerShapeTypes |= SE_POINT_TYPE_MASK;
    
    else if( wkbFlatten(eType) == wkbLineString
             || wkbFlatten(eType) == wkbMultiLineString )
        nLayerShapeTypes |= ( SE_LINE_TYPE_MASK | SE_SIMPLE_LINE_TYPE_MASK );

    else if( wkbFlatten(eType) == wkbPolygon )
    {
        nLayerShapeTypes |= SE_AREA_TYPE_MASK;
    }
    else if( wkbFlatten(eType) == wkbMultiPolygon )
    {
        nLayerShapeTypes |= SE_AREA_TYPE_MASK;
        nLayerShapeTypes |= SE_MULTIPART_TYPE_MASK;
    }
    else if( eType == wkbUnknown )
    {
        nLayerShapeTypes |= (  SE_POINT_TYPE_MASK
                             | SE_LINE_TYPE_MASK 
                             | SE_SIMPLE_LINE_TYPE_MASK
                             | SE_AREA_TYPE_MASK );
        CPLError( CE_Warning, CPLE_AppDefined,
                  "Warning: Creation of a wkbUnknown layer in ArcSDE will "
                  "result in layers which are not displayable in Arc* "
                  "software" );
    }
    
    else
    {
        CPLError( CE_Failure, CPLE_AppDefined, 
                  "Cannot create SDE layer %s with geometry type %d.",
                   pszLayerName, eType );
        
        SE_layerinfo_free( hLayerInfo );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    nSDEErr = SE_layerinfo_set_shape_types( hLayerInfo, nLayerShapeTypes );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_layerinfo_free( hLayerInfo );
        IssueSDEError( nSDEErr, "SE_layerinfo_set_shape_types" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    
    // Set geometry column name
    nSDEErr = SE_layerinfo_set_spatial_column( hLayerInfo, pszLayerName,
                                               pszGeometryName );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_layerinfo_free( hLayerInfo );
        IssueSDEError( nSDEErr, "SE_layerinfo_set_spatial_column" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    // Set creation keyword
    nSDEErr = SE_layerinfo_set_creation_keyword( hLayerInfo, pszDbtuneKeyword );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_layerinfo_free( hLayerInfo );
        IssueSDEError( nSDEErr, "SE_layerinfo_set_creation_keyword" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    // Set layer extent based on coordinate system envelope
    if( poSRS != NULL && poSRS->IsGeographic() )
    {
        sLayerEnvelope.minx = -180;
        sLayerEnvelope.miny = -90;
        sLayerEnvelope.maxx =  180;
        sLayerEnvelope.maxy =  90;
    }
    else
    {
        nSDEErr = SE_coordref_get_xy_envelope( hCoordRef, &sLayerEnvelope );
        if( nSDEErr != SE_SUCCESS )
        {
            SE_layerinfo_free( hLayerInfo );
            IssueSDEError( nSDEErr, "SE_coordref_get_xy_envelope" );
            CleanupLayerCreation(pszLayerName);
            return NULL;
        }
    }

    CPLDebug( "SDE", "Creating layer with envelope (%g,%g) to (%g,%g)",
              sLayerEnvelope.minx,
              sLayerEnvelope.miny,
              sLayerEnvelope.maxx,
              sLayerEnvelope.maxy );
    nSDEErr = SE_layerinfo_set_envelope( hLayerInfo, &sLayerEnvelope );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_layerinfo_free( hLayerInfo );
        IssueSDEError( nSDEErr, "SE_layerinfo_set_envelope" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }

    
    nSDEErr = SE_layerinfo_set_description( hLayerInfo, pszLayerDescription );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_layerinfo_free( hLayerInfo );
        IssueSDEError( nSDEErr, "SE_layerinfo_set_description" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    
    // Set grid size
    nSDEErr = SE_layerinfo_set_grid_sizes( hLayerInfo,
                                           OGR_SDE_LAYER_CO_GRID1,
                                           OGR_SDE_LAYER_CO_GRID2,
                                           OGR_SDE_LAYER_CO_GRID3 );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_layerinfo_free( hLayerInfo );
        IssueSDEError( nSDEErr, "SE_layerinfo_set_grid_sizes" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    
    // Set layer coordinate reference
    nSDEErr = SE_layerinfo_set_coordref( hLayerInfo, hCoordRef );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_layerinfo_free( hLayerInfo );
        IssueSDEError( nSDEErr, "SE_layerinfo_set_coordref" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    
/* -------------------------------------------------------------------- */
/*      Spatially enable the newly created table                        */
/* -------------------------------------------------------------------- */
    nSDEErr = SE_layer_create( hConnection, hLayerInfo,
                               OGR_SDE_LAYER_CO_INIT_FEATS,
                               OGR_SDE_LAYER_CO_AVG_PTS );

    SE_layerinfo_free( hLayerInfo );
    
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_layer_create" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }

/* -------------------------------------------------------------------- */
/*      Register the newly created table                                */
/* -------------------------------------------------------------------- */
    char                szQualifiedTable[SE_QUALIFIED_TABLE_NAME];
    SE_REGINFO          hRegInfo;
    
    nSDEErr = SE_reginfo_create( &hRegInfo );
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_reginfo_create" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    nSDEErr = SE_registration_get_info( hConnection, pszLayerName,
                                        hRegInfo );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_reginfo_free( hRegInfo );
        IssueSDEError( nSDEErr, "SE_registration_get_info" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    nSDEErr = SE_reginfo_set_creation_keyword( hRegInfo, pszDbtuneKeyword );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_reginfo_free( hRegInfo );
        IssueSDEError( nSDEErr, "SE_reginfo_set_creation_keyword" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }

    nSDEErr = SE_reginfo_set_rowid_column( hRegInfo, pszExpectedFIDName,
                                       SE_REGISTRATION_ROW_ID_COLUMN_TYPE_SDE );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_reginfo_free( hRegInfo );
        IssueSDEError( nSDEErr, "SE_reginfo_set_rowid_column" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    /*
     * If the layer creation option 'MULTIVERSION' is set, enable
     * multi-versioning for this layer
     */
    if( CSLFetchBoolean( papszOptions, "SDE_MULTIVERSION", TRUE ) )
    {
        CPLDebug("OGR_SDE","Setting multiversion to true");
        nSDEErr = SE_reginfo_set_multiversion( hRegInfo, TRUE );
        if( nSDEErr != SE_SUCCESS )
        {
            SE_reginfo_free( hRegInfo );
            IssueSDEError( nSDEErr, "SE_reginfo_set_multiversion" );
            CleanupLayerCreation(pszLayerName);
            return NULL;
        }
    }
    
    nSDEErr = SE_registration_alter( hConnection, hRegInfo );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_reginfo_free( hRegInfo );
        IssueSDEError( nSDEErr, "SE_registration_alter" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }
    
    nSDEErr = SE_reginfo_get_table_name( hRegInfo, szQualifiedTable );
    if( nSDEErr != SE_SUCCESS )
    {
        SE_reginfo_free( hRegInfo );
        IssueSDEError( nSDEErr, "SE_reginfo_get_table_name" );
        CleanupLayerCreation(pszLayerName);
        return NULL;
    }

    SE_reginfo_free( hRegInfo );
    SE_coordref_free( hCoordRef );
    
    
/* -------------------------------------------------------------------- */
/*      Create the layer object.                                        */
/* -------------------------------------------------------------------- */
    OGRSDELayer *poLayer;

    poLayer = new OGRSDELayer( this, bDSUpdate );
    
    if( !poLayer->Initialize( szQualifiedTable, pszExpectedFIDName,
                              pszGeometryName) )
    {
        CPLError( CE_Failure, CPLE_AppDefined,
                  "Cannot initialize newly created layer \"%s\"",
                  pszLayerName );
        CleanupLayerCreation(pszLayerName);
        delete poLayer;
        return NULL;
    }

/* -------------------------------------------------------------------- */
/*      Set various options on the layer.                               */
/* -------------------------------------------------------------------- */
    poLayer->SetFIDColType( SE_REGISTRATION_ROW_ID_COLUMN_TYPE_SDE );

    poLayer->SetUseNSTRING( 
        CSLFetchBoolean( papszOptions, "USE_NSTRING", FALSE ) );

/* -------------------------------------------------------------------- */
/*      Add layer to data source layer list.                            */
/* -------------------------------------------------------------------- */
    papoLayers = (OGRSDELayer **)
        CPLRealloc( papoLayers,  sizeof(OGRSDELayer *) * (nLayers+1) );
    
    papoLayers[nLayers++] = poLayer;

    return poLayer;
}

/************************************************************************/
/*                           TestCapability()                           */
/************************************************************************/

int OGRSDEDataSource::TestCapability( const char * pszCap )

{
    if( EQUAL(pszCap,ODsCCreateLayer) && bDSUpdate )
        return TRUE;
    else if( EQUAL(pszCap,ODsCDeleteLayer) && bDSUpdate )
        return TRUE;
    else
        return FALSE;
}

/************************************************************************/
/*                              GetLayer()                              */
/************************************************************************/

OGRLayer *OGRSDEDataSource::GetLayer( int iLayer )

{
    if( iLayer < 0 || iLayer >= nLayers )
        return NULL;
    else
        return papoLayers[iLayer];
}

/************************************************************************/
/*                       EnumerateSpatialTables()                       */
/************************************************************************/
void OGRSDEDataSource::EnumerateSpatialTables()
{
/* -------------------------------------------------------------------- */
/*      Fetch list of spatial tables from SDE.                          */
/* -------------------------------------------------------------------- */
    SE_REGINFO *ahTableList;
    LONG nTableListCount;
    LONG nSDEErr;

    nSDEErr = SE_registration_get_info_list( hConnection, &ahTableList,
                                             &nTableListCount );
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_registration_get_info_list" );
        return;
    }

    CPLDebug( "OGR_SDE", 
              "SDE::EnumerateSpatialTables() found %d tables.", 
              (int) nTableListCount );

/* -------------------------------------------------------------------- */
/*      Process the tables, turning any appropriate ones into layers.   */
/* -------------------------------------------------------------------- */
    int iTable;

    for( iTable = 0; iTable < nTableListCount; iTable++ )
    {
        CreateLayerFromRegInfo( ahTableList[iTable] );
    }

    SE_registration_free_info_list( nTableListCount, ahTableList );
}

/************************************************************************/
/*                          OpenSpatialTable()                          */
/************************************************************************/

void OGRSDEDataSource::OpenSpatialTable( const char* pszTableName )
{
    SE_REGINFO tableinfo = NULL;
    LONG nSDEErr;

    CPLDebug( "OGR_SDE", "SDE::OpenSpatialTable(\"%s\").", pszTableName );

    nSDEErr = SE_reginfo_create( &tableinfo );
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_reginfo_create" );
    }

    nSDEErr = SE_registration_get_info( hConnection, pszTableName, tableinfo );
    if( nSDEErr != SE_SUCCESS )
    {
        IssueSDEError( nSDEErr, "SE_registration_get_info_list" );
    }
    else
    {
        CreateLayerFromRegInfo( tableinfo );
    }

    SE_reginfo_free( tableinfo );
}

/************************************************************************/
/*                       CreateLayerFromRegInfo()                       */
/************************************************************************/

void OGRSDEDataSource::CreateLayerFromRegInfo( SE_REGINFO& reginfo )
{
    char szTableName[SE_QUALIFIED_TABLE_NAME+1];
    char szIDColName[SE_MAX_COLUMN_LEN+1];
    LONG nFIDColType;
    LONG nSDEErr;

    nSDEErr = SE_reginfo_get_table_name( reginfo, szTableName );
    if( nSDEErr != SE_SUCCESS )
    {
        CPLDebug( "SDE", 
                  "Ignoring reginfo '%p', no table name.",
                  reginfo );
        return;
    }

    // Ignore non-spatial, or hidden tables. 
    if( !SE_reginfo_has_layer( reginfo ) || SE_reginfo_is_hidden( reginfo ) )
    {
        CPLDebug( "SDE", 
                  "Ignoring layer '%s' as it is hidden or does not have a reginfo layer.", 
                  szTableName );
        return;
    }

    CPLDebug( "OGR_SDE", "CreateLayerFromRegInfo() asked to load table \"%s\".", szTableName );

    nSDEErr = SE_reginfo_get_rowid_column( reginfo, szIDColName, &nFIDColType );

    if( nFIDColType == SE_REGISTRATION_ROW_ID_COLUMN_TYPE_NONE
        || strlen(szIDColName) == 0 )
    {
        CPLDebug( "OGR_SDE", "Unable to determine FID column for %s.", 
                  szTableName );
        OpenTable( szTableName, NULL, NULL, nFIDColType );
    }
    else
        OpenTable( szTableName, szIDColName, NULL, nFIDColType );
}

/************************************************************************/
/*                       ConvertOSRtoSDESpatRef                         */
/************************************************************************/
OGRErr OGRSDEDataSource::ConvertOSRtoSDESpatRef( OGRSpatialReference *poSRS,
                                                 SE_COORDREF *psCoordRef )

{
    OGRSpatialReference  *poESRISRS;
    char                 *pszWkt;
    
    if( SE_coordref_create( psCoordRef ) != SE_SUCCESS )
        return OGRERR_FAILURE;
    
    // Construct a generic SE_COORDREF if poSRS is NULL
    if( poSRS == NULL )
    {
        SE_ENVELOPE     sGenericEnvelope;
        
        sGenericEnvelope.minx = -1000000;
        sGenericEnvelope.miny = -1000000;
        sGenericEnvelope.maxx =  1000000;
        sGenericEnvelope.maxy =  1000000;
        
        if( SE_coordref_set_xy_by_envelope( *psCoordRef, &sGenericEnvelope )
                != SE_SUCCESS )
        {
            SE_coordref_free( *psCoordRef );
            return OGRERR_FAILURE;
        }
        
        return OGRERR_NONE;
    }

    
    poESRISRS = poSRS->Clone();
    
    if (poSRS->IsLocal()) {
        CPLDebug("OGR_SDE", "Coordinate reference was local, using UNKNOWN for ESRI SRS description");
        if( SE_coordref_set_by_description( *psCoordRef, "UNKNOWN") != SE_SUCCESS )
            return OGRERR_FAILURE;
        CPLFree(pszWkt);
        return OGRERR_NONE;
    }
    
    if( poESRISRS->morphToESRI() != OGRERR_NONE )
    {
        SE_coordref_free( *psCoordRef );
        delete poESRISRS;
        return OGRERR_FAILURE;
    }
    
    if( poESRISRS->exportToWkt( &pszWkt ) != OGRERR_NONE )
    {
        SE_coordref_free( *psCoordRef );
        delete poESRISRS;
        return OGRERR_FAILURE;
    }
    
    delete poESRISRS;
    
    if( SE_coordref_set_by_description( *psCoordRef, pszWkt ) != SE_SUCCESS )
        return OGRERR_FAILURE;
    
    {
        SE_ENVELOPE     sGenericEnvelope;
        SE_coordref_get_xy_envelope( *psCoordRef, &sGenericEnvelope );

        CPLDebug( "SDE", 
                  "Created coordref '%s' with envelope (%g,%g) to (%g,%g)",
                  pszWkt,
                  sGenericEnvelope.minx,
                  sGenericEnvelope.miny,
                  sGenericEnvelope.maxx,
                  sGenericEnvelope.maxy );
    }

    if( poSRS && poSRS->IsGeographic() )
    {
        LONG nSDEErr;

        // Reset the offset and precision to match the ordinary values
        // for SDE geographic coordinate systems. 
        nSDEErr = SE_coordref_set_xy( *psCoordRef, -400, -400, 1.11195e9 );

        if( nSDEErr != SE_SUCCESS ) 
        {
            IssueSDEError( nSDEErr, "SE_coordref_set_xy()" );
        }
    }

    CPLFree( pszWkt );

    return OGRERR_NONE;
}