Skip to content

Commit

Permalink
Add option to postgres provider to drop length constraints on charact…
Browse files Browse the repository at this point in the history
…er fields
  • Loading branch information
nyalldawson committed May 20, 2014
1 parent 72ee2e6 commit 7772042
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2767,9 +2767,17 @@ bool QgsPostgresProvider::getGeometryDetails()
return mValid;
}

bool QgsPostgresProvider::convertField( QgsField &field )
bool QgsPostgresProvider::convertField( QgsField &field , const QMap<QString, QVariant>* options )
{
QString fieldType = "varchar"; //default to string
//determine field type to use for strings
QString stringFieldType = "varchar";
if ( options->contains( "dropStringConstraints" ) && options->value( "dropStringConstraints" ).toBool() )
{
//drop string length constraints by using PostgreSQL text type for strings
stringFieldType = "text";
}

QString fieldType = stringFieldType; //default to string
int fieldSize = field.length();
int fieldPrec = field.precision();
switch ( field.type() )
Expand All @@ -2788,7 +2796,7 @@ bool QgsPostgresProvider::convertField( QgsField &field )
break;

case QVariant::String:
fieldType = "varchar";
fieldType = stringFieldType;
fieldPrec = -1;
break;

Expand Down Expand Up @@ -2900,7 +2908,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
{
// found, get the field type
QgsField fld = fields[fldIdx];
if ( convertField( fld ) )
if ( convertField( fld, options ) )
{
primaryKeyType = fld.typeName();
}
Expand Down Expand Up @@ -3050,7 +3058,7 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
fld.setName( fld.name().toLower() );
}

if ( !convertField( fld ) )
if ( !convertField( fld, options ) )
{
if ( errorMessage )
*errorMessage = QObject::tr( "Unsupported type for field %1" ).arg( fld.name() );
Expand Down
9 changes: 7 additions & 2 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -47,7 +47,12 @@ class QgsPostgresProvider : public QgsVectorDataProvider

public:

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

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

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

0 comments on commit 7772042

Please sign in to comment.