Skip to content
Permalink
Browse files
Add option to postgres provider to drop length constraints on charact…
…er fields
  • Loading branch information
nyalldawson committed May 20, 2014
1 parent 72ee2e6 commit 7772042fffed2019375adac2afa938964e9cd728
Showing with 20 additions and 7 deletions.
  1. +13 −5 src/providers/postgres/qgspostgresprovider.cpp
  2. +7 −2 src/providers/postgres/qgspostgresprovider.h
@@ -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() )
@@ -2788,7 +2796,7 @@ bool QgsPostgresProvider::convertField( QgsField &field )
break;

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

@@ -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();
}
@@ -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() );
@@ -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,
@@ -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

0 comments on commit 7772042

Please sign in to comment.