Skip to content

Commit 7772042

Browse files
committed
Add option to postgres provider to drop length constraints on character fields
1 parent 72ee2e6 commit 7772042

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -2767,9 +2767,17 @@ bool QgsPostgresProvider::getGeometryDetails()
27672767
return mValid;
27682768
}
27692769

2770-
bool QgsPostgresProvider::convertField( QgsField &field )
2770+
bool QgsPostgresProvider::convertField( QgsField &field , const QMap<QString, QVariant>* options )
27712771
{
2772-
QString fieldType = "varchar"; //default to string
2772+
//determine field type to use for strings
2773+
QString stringFieldType = "varchar";
2774+
if ( options->contains( "dropStringConstraints" ) && options->value( "dropStringConstraints" ).toBool() )
2775+
{
2776+
//drop string length constraints by using PostgreSQL text type for strings
2777+
stringFieldType = "text";
2778+
}
2779+
2780+
QString fieldType = stringFieldType; //default to string
27732781
int fieldSize = field.length();
27742782
int fieldPrec = field.precision();
27752783
switch ( field.type() )
@@ -2788,7 +2796,7 @@ bool QgsPostgresProvider::convertField( QgsField &field )
27882796
break;
27892797

27902798
case QVariant::String:
2791-
fieldType = "varchar";
2799+
fieldType = stringFieldType;
27922800
fieldPrec = -1;
27932801
break;
27942802

@@ -2900,7 +2908,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29002908
{
29012909
// found, get the field type
29022910
QgsField fld = fields[fldIdx];
2903-
if ( convertField( fld ) )
2911+
if ( convertField( fld, options ) )
29042912
{
29052913
primaryKeyType = fld.typeName();
29062914
}
@@ -3050,7 +3058,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
30503058
fld.setName( fld.name().toLower() );
30513059
}
30523060

3053-
if ( !convertField( fld ) )
3061+
if ( !convertField( fld, options ) )
30543062
{
30553063
if ( errorMessage )
30563064
*errorMessage = QObject::tr( "Unsupported type for field %1" ).arg( fld.name() );

src/providers/postgres/qgspostgresprovider.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ class QgsPostgresProvider : public QgsVectorDataProvider
4747

4848
public:
4949

50-
/** Import a vector layer into the database */
50+
/** Import a vector layer into the database
51+
* @param options options for provider, specified via a map of option name
52+
* to value. Valid options are lowercaseFieldNames (set to true to convert
53+
* field names to lowercase), dropStringConstraints (set to true to remove
54+
* length constraints on character fields).
55+
*/
5156
static QgsVectorLayerImport::ImportError createEmptyLayer(
5257
const QString& uri,
5358
const QgsFields &fields,
@@ -324,7 +329,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
324329
bool loadFields();
325330

326331
/** convert a QgsField to work with PG */
327-
static bool convertField( QgsField &field );
332+
static bool convertField( QgsField &field, const QMap<QString, QVariant> *options = 0 );
328333

329334
/**Parses the enum_range of an attribute and inserts the possible values into a stringlist
330335
@param enumValues the stringlist where the values are appended

0 commit comments

Comments
 (0)