Skip to content

Commit 5d79452

Browse files
author
rouault
committedFeb 13, 2015
RFC 53: OGR not-null constraints and default values
From: Even Rouault <[email protected]> git-svn-id: https://svn.osgeo.org/gdal/trunk/gdal@28481 f0d54148-0727-0410-94bb-9a71ac55c965
1 parent 81f78e4 commit 5d79452

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+3286
-507
lines changed
 

‎MIGRATION_GUIDE.TXT

+9
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ Behaviour changes:
112112
it would have been interpreted as WHERE "a_column_name" = 'a_value" if
113113
a_column_name was indeed a column name.
114114

115+
F) RFC 53:
116+
117+
http://trac.osgeo.org/gdal/wiki/rfc53_ogr_notnull_default
118+
119+
API changes:
120+
* OGRFieldDefn::SetDefault() now takes a const char* as argument.
121+
OGRFieldDefn::GetDefaultRef() removed and replaced by GetDefault() that
122+
returns a const char*
123+
115124
MIGRATION GUIDE FROM GDAL 1.10 to GDAL 1.11
116125
-------------------------------------------
117126

‎apps/ogr2ogr.cpp

+69-11
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ static TargetLayerInfo* SetupTargetLayer( GDALDataset *poSrcDS,
106106
char **papszFieldMap,
107107
const char* pszWHERE,
108108
int bExactFieldNameMatch,
109-
int bQuiet );
109+
int bQuiet,
110+
int bForceNullable,
111+
int bUnsetDefault);
110112

111113
static void FreeTargetLayerInfo(TargetLayerInfo* psInfo);
112114

@@ -932,6 +934,8 @@ int main( int nArgc, char ** papszArgv )
932934
int nCoordDim = -1;
933935
char **papszOpenOptions = NULL;
934936
char **papszDestOpenOptions = NULL;
937+
int bForceNullable = FALSE;
938+
int bUnsetDefault = FALSE;
935939

936940
int nGCPCount = 0;
937941
GDAL_GCP *pasGCPs = NULL;
@@ -1465,6 +1469,14 @@ int main( int nArgc, char ** papszArgv )
14651469
papszFieldMap = CSLTokenizeStringComplex(pszFieldMap, ",",
14661470
FALSE, FALSE );
14671471
}
1472+
else if( EQUAL(papszArgv[iArg],"-forceNullable") )
1473+
{
1474+
bForceNullable = TRUE;
1475+
}
1476+
else if( EQUAL(papszArgv[iArg],"-unsetDefault") )
1477+
{
1478+
bUnsetDefault = TRUE;
1479+
}
14681480
else if( papszArgv[iArg][0] == '-' )
14691481
{
14701482
Usage(CPLSPrintf("Unknown option name '%s'", papszArgv[iArg]));
@@ -1882,7 +1894,9 @@ int main( int nArgc, char ** papszArgv )
18821894
papszFieldMap,
18831895
pszWHERE,
18841896
bExactFieldNameMatch,
1885-
bQuiet);
1897+
bQuiet,
1898+
bForceNullable,
1899+
bUnsetDefault);
18861900

18871901
poPassedLayer->ResetReading();
18881902

@@ -2040,7 +2054,9 @@ int main( int nArgc, char ** papszArgv )
20402054
papszFieldMap,
20412055
pszWHERE,
20422056
bExactFieldNameMatch,
2043-
bQuiet);
2057+
bQuiet,
2058+
bForceNullable,
2059+
bUnsetDefault);
20442060

20452061
if( psInfo == NULL && !bSkipFailures )
20462062
exit(1);
@@ -2318,7 +2334,9 @@ int main( int nArgc, char ** papszArgv )
23182334
papszFieldMap,
23192335
pszWHERE,
23202336
bExactFieldNameMatch,
2321-
bQuiet);
2337+
bQuiet,
2338+
bForceNullable,
2339+
bUnsetDefault);
23222340

23232341
poPassedLayer->ResetReading();
23242342

@@ -2444,7 +2462,7 @@ static void Usage(const char* pszAdditionalMsg, int bShort)
24442462
" [-wrapdateline][-datelineoffset val]\n"
24452463
" [[-simplify tolerance] | [-segmentize max_dist]]\n"
24462464
" [-addfields]\n"
2447-
" [-relaxedFieldNameMatch]\n"
2465+
" [-relaxedFieldNameMatch] [-forceNullable] [-unsetDefault]\n"
24482466
" [-fieldTypeToString All|(type1[,type2]*)] [-unsetFieldWidth]\n"
24492467
" [-mapFieldType srctype|All=dsttype[,srctype2=dsttype2]*]\n"
24502468
" [-fieldmap identity | index1[,index2]*]\n"
@@ -2677,7 +2695,9 @@ void DoFieldTypeConversion(GDALDataset* poDstDS, OGRFieldDefn& oFieldDefn,
26772695
char** papszFieldTypesToString,
26782696
char** papszMapFieldType,
26792697
int bUnsetFieldWidth,
2680-
int bQuiet)
2698+
int bQuiet,
2699+
int bForceNullable,
2700+
int bUnsetDefault)
26812701
{
26822702
if (papszFieldTypesToString != NULL &&
26832703
(CSLFindString(papszFieldTypesToString, "All") != -1 ||
@@ -2710,6 +2730,10 @@ void DoFieldTypeConversion(GDALDataset* poDstDS, OGRFieldDefn& oFieldDefn,
27102730
oFieldDefn.SetWidth(0);
27112731
oFieldDefn.SetPrecision(0);
27122732
}
2733+
if( bForceNullable )
2734+
oFieldDefn.SetNullable(TRUE);
2735+
if( bUnsetDefault )
2736+
oFieldDefn.SetDefault(NULL);
27132737

27142738
if( poDstDS->GetDriver() != NULL &&
27152739
poDstDS->GetDriver()->GetMetadataItem(GDAL_DMD_CREATIONFIELDDATATYPES) != NULL &&
@@ -2782,7 +2806,9 @@ static TargetLayerInfo* SetupTargetLayer( CPL_UNUSED GDALDataset *poSrcDS,
27822806
char **papszFieldMap,
27832807
const char* pszWHERE,
27842808
int bExactFieldNameMatch,
2785-
int bQuiet)
2809+
int bQuiet,
2810+
int bForceNullable,
2811+
int bUnsetDefault)
27862812
{
27872813
OGRLayer *poDstLayer;
27882814
OGRFeatureDefn *poSrcFDefn;
@@ -2927,6 +2953,8 @@ static TargetLayerInfo* SetupTargetLayer( CPL_UNUSED GDALDataset *poSrcDS,
29272953

29282954
CPLErrorReset();
29292955

2956+
char** papszLCOTemp = CSLDuplicate(papszLCO);
2957+
29302958
int eGCreateLayerType = eGType;
29312959
if( anRequestedGeomFields.size() == 0 &&
29322960
nSrcGeomFieldCount > 1 &&
@@ -2939,8 +2967,32 @@ static TargetLayerInfo* SetupTargetLayer( CPL_UNUSED GDALDataset *poSrcDS,
29392967
{
29402968
eGCreateLayerType = wkbNone;
29412969
}
2942-
2943-
char** papszLCOTemp = CSLDuplicate(papszLCO);
2970+
// If the source feature has a single geometry column that is not nullable
2971+
// and that ODsCCreateGeomFieldAfterCreateLayer is available, use it
2972+
// so as to be able to set the not null constraint (if the driver supports it)
2973+
else if( anRequestedGeomFields.size() == 0 &&
2974+
nSrcGeomFieldCount == 1 &&
2975+
poDstDS->TestCapability(ODsCCreateGeomFieldAfterCreateLayer) &&
2976+
!poSrcFDefn->GetGeomFieldDefn(0)->IsNullable() &&
2977+
!bForceNullable)
2978+
{
2979+
anRequestedGeomFields.push_back(0);
2980+
eGCreateLayerType = wkbNone;
2981+
}
2982+
// If the source feature first geometry column is not nullable
2983+
// and that GEOMETRY_NULLABLE creation option is available, use it
2984+
// so as to be able to set the not null constraint (if the driver supports it)
2985+
else if( anRequestedGeomFields.size() == 0 &&
2986+
nSrcGeomFieldCount >= 1 &&
2987+
!poSrcFDefn->GetGeomFieldDefn(0)->IsNullable() &&
2988+
poDstDS->GetDriver()->GetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST) != NULL &&
2989+
strstr(poDstDS->GetDriver()->GetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST), "GEOMETRY_NULLABLE") != NULL &&
2990+
CSLFetchNameValue(papszLCO, "GEOMETRY_NULLABLE") == NULL&&
2991+
!bForceNullable )
2992+
{
2993+
papszLCOTemp = CSLSetNameValue(papszLCOTemp, "GEOMETRY_NULLABLE", "NO");
2994+
}
2995+
29442996
// Force FID column as 64 bit if the source feature has a 64 bit FID,
29452997
// the target driver supports 64 bit FID and the user didn't set it
29462998
// manually.
@@ -2991,6 +3043,8 @@ static TargetLayerInfo* SetupTargetLayer( CPL_UNUSED GDALDataset *poSrcDS,
29913043
eGType = ForceCoordDimension(eGType, nCoordDim);
29923044
oGFldDefn.SetType((OGRwkbGeometryType) eGType);
29933045
}
3046+
if( bForceNullable )
3047+
oGFldDefn.SetNullable(TRUE);
29943048
poDstLayer->CreateGeomField(&oGFldDefn);
29953049
}
29963050
}
@@ -3082,7 +3136,9 @@ static TargetLayerInfo* SetupTargetLayer( CPL_UNUSED GDALDataset *poSrcDS,
30823136
papszFieldTypesToString,
30833137
papszMapFieldType,
30843138
bUnsetFieldWidth,
3085-
bQuiet);
3139+
bQuiet,
3140+
bForceNullable,
3141+
bUnsetDefault);
30863142

30873143
/* The field may have been already created at layer creation */
30883144
int iDstField = -1;
@@ -3196,7 +3252,9 @@ static TargetLayerInfo* SetupTargetLayer( CPL_UNUSED GDALDataset *poSrcDS,
31963252
papszFieldTypesToString,
31973253
papszMapFieldType,
31983254
bUnsetFieldWidth,
3199-
bQuiet);
3255+
bQuiet,
3256+
bForceNullable,
3257+
bUnsetDefault);
32003258

32013259
/* The field may have been already created at layer creation */
32023260
std::map<CPLString, int>::iterator oIter =

0 commit comments

Comments
 (0)
Please sign in to comment.