Skip to content
Permalink
Browse files

[OGR provider] Advertize correct supported data types, length and wit…

…h for GPKG; and advertize Date, Time, DateTime based on driver capabilities
  • Loading branch information
rouault committed Jun 1, 2018
1 parent bad1ab4 commit a3e527d8ffff81981d9f570dc092b46c7b44869f
Showing with 52 additions and 12 deletions.
  1. +52 −12 src/providers/ogr/qgsogrprovider.cpp
@@ -449,20 +449,60 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
open( OpenModeInitial );
int nMaxIntLen = 11;
int nMaxInt64Len = 21;
int nMaxDoubleLen = 20;
int nMaxDoublePrec = 15;
int nDateLen = 8;
if ( mGDALDriverName == QLatin1String( "GPKG" ) )
{
// GPKG only supports field length for text (and binary)
nMaxIntLen = 0;
nMaxInt64Len = 0;
nMaxDoubleLen = 0;
nMaxDoublePrec = 0;
nDateLen = 0;
}
QList<NativeType> nativeTypes;
nativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer)" ), QStringLiteral( "integer" ), QVariant::Int, 0, 11 )
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer 64 bit)" ), QStringLiteral( "integer64" ), QVariant::LongLong, 0, 21 )
<< QgsVectorDataProvider::NativeType( tr( "Decimal number (real)" ), QStringLiteral( "double" ), QVariant::Double, 0, 20, 0, 15 )
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), QStringLiteral( "string" ), QVariant::String, 0, 65535 )
<< QgsVectorDataProvider::NativeType( tr( "Date" ), QStringLiteral( "date" ), QVariant::Date, 8, 8 );
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer)" ), QStringLiteral( "integer" ), QVariant::Int, 0, nMaxIntLen )
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer 64 bit)" ), QStringLiteral( "integer64" ), QVariant::LongLong, 0, nMaxInt64Len )
<< QgsVectorDataProvider::NativeType( tr( "Decimal number (real)" ), QStringLiteral( "double" ), QVariant::Double, 0, nMaxDoubleLen, 0, nMaxDoublePrec )
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), QStringLiteral( "string" ), QVariant::String, 0, 65535 );

bool supportsDate = true;
bool supportsTime = mGDALDriverName != QLatin1String( "ESRI Shapefile" ) && mGDALDriverName != QLatin1String( "GPKG" );
bool supportsDateTime = mGDALDriverName != QLatin1String( "ESRI Shapefile" );
const char *pszDataTypes = nullptr;
if ( mOgrOrigLayer )
{
pszDataTypes = GDALGetMetadataItem( mOgrOrigLayer->driver(), GDAL_DMD_CREATIONFIELDDATATYPES, nullptr );
}
// For drivers that advertize their data type, use that instead of the
// above hardcoded defaults.
if ( pszDataTypes )
{
char **papszTokens = CSLTokenizeString2( pszDataTypes, " ", 0 );
supportsDate = CSLFindString( papszTokens, "Date" ) >= 0;
supportsTime = CSLFindString( papszTokens, "Time" ) >= 0;
supportsDateTime = CSLFindString( papszTokens, "DateTime" ) >= 0;
CSLDestroy( papszTokens );
}

// Some drivers do not support datetime type
// Please help to fill this list
if ( mGDALDriverName != QLatin1String( "ESRI Shapefile" ) )
if ( supportsDate )
{
nativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Date" ), QStringLiteral( "date" ), QVariant::Date, nDateLen, nDateLen );
}
if ( supportsTime )
{
nativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Time" ), QStringLiteral( "time" ), QVariant::Time );
}
if ( supportsDateTime )
{
nativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Time" ), QStringLiteral( "time" ), QVariant::Time, -1, -1 )
<< QgsVectorDataProvider::NativeType( tr( "Date & Time" ), QStringLiteral( "datetime" ), QVariant::DateTime );
}

@@ -471,8 +511,8 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,3,0)
if ( mOgrOrigLayer )
{
const char *pszDataTypes = GDALGetMetadataItem( mOgrOrigLayer->driver(), GDAL_DMD_CREATIONFIELDDATASUBTYPES, nullptr );
if ( pszDataTypes && strstr( pszDataTypes, "Boolean" ) )
const char *pszDataSubTypes = GDALGetMetadataItem( mOgrOrigLayer->driver(), GDAL_DMD_CREATIONFIELDDATASUBTYPES, nullptr );
if ( pszDataSubTypes && strstr( pszDataSubTypes, "Boolean" ) )
supportsBoolean = true;
}
#else
@@ -492,7 +532,7 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
{
// boolean data type
nativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Boolean" ), QStringLiteral( "bool" ), QVariant::Bool, -1, -1, -1, -1 );
<< QgsVectorDataProvider::NativeType( tr( "Boolean" ), QStringLiteral( "bool" ), QVariant::Bool );
}

setNativeTypes( nativeTypes );

0 comments on commit a3e527d

Please sign in to comment.
You can’t perform that action at this time.