Skip to content

Commit 9225974

Browse files
author
jef
committedAug 21, 2009
postgres & ogr provider update:
- postgres provider: report field width -1 for everything except strings (fixes #1131) - in vector file writer, ogr provider choose field width: - 255 for strings, when given width is negative or larger. (was 80 in ogr provider and OGR determined-default for vector file writer). - 10 for integers, when given width is negative or larger. - 32 for reals, when given width is negative or larger. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11464 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed
 

‎src/core/qgsvectorfilewriter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,19 @@ QgsVectorFileWriter::QgsVectorFileWriter( const QString& shapefileName,
129129
{
130130
case QVariant::LongLong:
131131
ogrType = OFTString;
132-
ogrWidth = ogrWidth <= 21 ? ogrWidth : 21;
132+
ogrWidth = ogrWidth > 0 && ogrWidth <= 21 ? ogrWidth : 21;
133133
ogrPrecision = -1;
134134
break;
135135

136136
case QVariant::String:
137137
ogrType = OFTString;
138+
if ( ogrWidth < 0 || ogrWidth > 255 )
139+
ogrWidth = 255;
138140
break;
139141

140142
case QVariant::Int:
141143
ogrType = OFTInteger;
142-
ogrWidth = ogrWidth <= 10 ? ogrWidth : 10;
144+
ogrWidth = ogrWidth > 0 && ogrWidth <= 10 ? ogrWidth : 10;
143145
ogrPrecision = 0;
144146
break;
145147

@@ -388,11 +390,11 @@ QgsVectorFileWriter::writeAsShapefile( QgsVectorLayer* layer,
388390
// The 'CT-params' (e.g. +towgs84) does not get stripped in this way
389391
QRegExp regExp( ".shp$" );
390392
QString prjName = shapefileName;
391-
prjName.replace( regExp, QString( "" ));
393+
prjName.replace( regExp, QString( "" ) );
392394
prjName.append( QString( ".prj" ) );
393395
QFile prjFile( prjName );
394396

395-
if( !prjFile.open( QIODevice::WriteOnly ) )
397+
if ( !prjFile.open( QIODevice::WriteOnly ) )
396398
{
397399
QgsDebugMsg( "Couldn't open file " + prjName );
398400
return NoError; // For now

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,9 +1458,9 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
14581458
OGRFieldDefnH field;
14591459
if ( fields[0] == "Real" )
14601460
{
1461-
if ( width == -1 )
1461+
if ( width < 0 )
14621462
width = 32;
1463-
if ( precision == -1 )
1463+
if ( precision < 0 )
14641464
precision = 3;
14651465

14661466
field = OGR_Fld_Create( codec->fromUnicode( it->first ).data(), OFTReal );
@@ -1469,7 +1469,7 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
14691469
}
14701470
else if ( fields[0] == "Integer" )
14711471
{
1472-
if ( width == -1 || width > 10 )
1472+
if ( width < 0 || width > 10 )
14731473
width = 10;
14741474

14751475
field = OGR_Fld_Create( codec->fromUnicode( it->first ).data(), OFTInteger );
@@ -1478,8 +1478,8 @@ QGISEXTERN bool createEmptyDataSource( const QString& uri,
14781478
}
14791479
else if ( fields[0] == "String" )
14801480
{
1481-
if ( width == -1 )
1482-
width = 80;
1481+
if ( width < 0 || width > 255 )
1482+
width = 255;
14831483

14841484
field = OGR_Fld_Create( codec->fromUnicode( it->first ).data(), OFTString );
14851485
OGR_Fld_SetWidth( field, width );

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ void QgsPostgresProvider::loadFields()
861861
QString fieldTypeName = QString::fromUtf8( PQgetvalue( oidResult, 0, 0 ) );
862862
QString fieldTType = QString::fromUtf8( PQgetvalue( oidResult, 0, 1 ) );
863863
QString fieldElem = QString::fromUtf8( PQgetvalue( oidResult, 0, 2 ) );
864-
QString fieldSize = QString::fromUtf8( PQgetvalue( oidResult, 0, 3 ) );
864+
int fieldSize = QString::fromUtf8( PQgetvalue( oidResult, 0, 3 ) ).toInt();
865865

866866
sql = QString( "SELECT attnum FROM pg_attribute WHERE attrelid=%1 AND attname=%2" )
867867
.arg( tableoid ).arg( quotedValue( fieldName ) );
@@ -888,15 +888,24 @@ void QgsPostgresProvider::loadFields()
888888
fieldTypeName = fieldTypeName.mid( 1 );
889889

890890
if ( fieldTypeName == "int8" )
891+
{
891892
fieldType = QVariant::LongLong;
893+
fieldSize = -1;
894+
}
892895
else if ( fieldTypeName.startsWith( "int" ) ||
893896
fieldTypeName == "serial" )
897+
{
894898
fieldType = QVariant::Int;
899+
fieldSize = -1;
900+
}
895901
else if ( fieldTypeName == "real" ||
896902
fieldTypeName == "double precision" ||
897903
fieldTypeName.startsWith( "float" ) ||
898904
fieldTypeName == "numeric" )
905+
{
899906
fieldType = QVariant::Double;
907+
fieldSize = -1;
908+
}
900909
else if ( fieldTypeName == "text" ||
901910
fieldTypeName == "char" ||
902911
fieldTypeName == "bpchar" ||
@@ -906,7 +915,9 @@ void QgsPostgresProvider::loadFields()
906915
fieldTypeName == "money" ||
907916
fieldTypeName.startsWith( "time" ) ||
908917
fieldTypeName.startsWith( "date" ) )
918+
{
909919
fieldType = QVariant::String;
920+
}
910921
else
911922
{
912923
QgsDebugMsg( "Field " + fieldName + " ignored, because of unsupported type " + fieldTypeName );
@@ -917,20 +928,22 @@ void QgsPostgresProvider::loadFields()
917928
{
918929
fieldTypeName = "_" + fieldTypeName;
919930
fieldType = QVariant::String;
931+
fieldSize = -1;
920932
}
921933
}
922934
else if ( fieldTType == "e" )
923935
{
924936
// enum
925937
fieldType = QVariant::String;
938+
fieldSize = -1;
926939
}
927940
else
928941
{
929942
QgsDebugMsg( "Field " + fieldName + " ignored, because of unsupported type type " + fieldTType );
930943
continue;
931944
}
932945

933-
attributeFields.insert( i, QgsField( fieldName, fieldType, fieldTypeName, fieldSize.toInt(), fieldModifier, fieldComment ) );
946+
attributeFields.insert( i, QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldModifier, fieldComment ) );
934947
}
935948
}
936949
}

0 commit comments

Comments
 (0)
Please sign in to comment.