Navigation Menu

Skip to content

Commit

Permalink
convert the primary key type name to a PG valid one,
Browse files Browse the repository at this point in the history
add a new options param to importVector
  • Loading branch information
brushtyler committed Aug 23, 2011
1 parent 00d7498 commit 24b73e6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 97 deletions.
3 changes: 2 additions & 1 deletion python/core/qgsproviderregistry.sip
Expand Up @@ -70,7 +70,8 @@ class QgsProviderRegistry
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = FALSE,
QString *errorMessage /Out/ = 0,
bool skipAttributeCreation = FALSE
bool skipAttributeCreation = FALSE,
const QMap<QString, QVariant> *options = 0
) const;


Expand Down
19 changes: 11 additions & 8 deletions src/core/qgsproviderregistry.cpp
Expand Up @@ -43,12 +43,14 @@ typedef QString directoryDrivers_t();
typedef QString protocolDrivers_t();
//typedef int dataCapabilities_t();
//typedef QgsDataItem * dataItem_t(QString);
typedef int importVector_t(QgsVectorLayer* layer,
const QString& uri,
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = false,
QString *errorMessage = 0,
bool skipAttributeCreation = false);
typedef int importVector_t( QgsVectorLayer* layer,
const QString& uri,
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = false,
QString *errorMessage = 0,
bool skipAttributeCreation = false,
const QMap<QString, QVariant> *options = 0
);

QgsProviderRegistry *QgsProviderRegistry::_instance = 0;

Expand Down Expand Up @@ -546,7 +548,8 @@ int QgsProviderRegistry::importVector( QgsVectorLayer* layer,
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected,
QString *errorMessage,
bool skipAttributeCreation
bool skipAttributeCreation,
const QMap<QString, QVariant> *options
) const
{
QLibrary *myLib = providerLibrary( providerKey );
Expand All @@ -567,5 +570,5 @@ int QgsProviderRegistry::importVector( QgsVectorLayer* layer,
}

delete myLib;
return pImport( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation );
return pImport( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation, options );
}
3 changes: 2 additions & 1 deletion src/core/qgsproviderregistry.h
Expand Up @@ -147,7 +147,8 @@ class CORE_EXPORT QgsProviderRegistry
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = false,
QString *errorMessage = 0,
bool skipAttributeCreation = false
bool skipAttributeCreation = false,
const QMap<QString, QVariant> *options = 0
) const;

private:
Expand Down
158 changes: 77 additions & 81 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -56,6 +56,52 @@ QMap<QString, QString> QgsPostgresProvider::Conn::passwordCache;
int QgsPostgresProvider::providerIds = 0;


bool QgsPostgresProvider::convertField( QgsField &field )
{
QString fieldType = "varchar"; //default to string
int fieldSize = field.length();
int fieldPrec = field.precision();
switch ( field.type() )
{
case QVariant::LongLong:
fieldType = "int8";
fieldSize = -1;
fieldPrec = 0;
break;

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

case QVariant::Int:
fieldType = "int";
fieldSize = -1;
fieldPrec = 0;
break;

case QVariant::Double:
if ( fieldSize <= 0 || fieldPrec < 0)
{
fieldType = "real8";
fieldSize = -1;
fieldPrec = -1;
}
else
{
fieldType = "decimal";
}
break;

default:
return false;
}

field.setTypeName( fieldType );
field.setLength( fieldSize );
field.setPrecision( fieldPrec );
return true;
}

QgsPostgresProvider::WriterError
QgsPostgresProvider::importVector(
Expand All @@ -64,7 +110,8 @@ QgsPostgresProvider::importVector(
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected,
QString *errorMessage,
bool skipAttributeCreation )
bool skipAttributeCreation,
const QMap<QString,QVariant> *options )
{
const QgsCoordinateReferenceSystem* outputCRS;
QgsCoordinateTransform* ct = 0;
Expand Down Expand Up @@ -142,12 +189,16 @@ QgsPostgresProvider::importVector(
else
{
// search for the passed field
for ( QgsFieldMap::const_iterator fldIt = fields.begin(); fldIt != fields.end(); ++fldIt )
for ( QgsFieldMap::iterator fldIt = fields.begin(); fldIt != fields.end(); ++fldIt )
{
if ( fldIt.value().name() == primaryKey )
{
// found, get the field type
primaryKeyType = fldIt.value().typeName();
QgsField &fld = fldIt.value();
if ( convertField( fld ) )
{
primaryKeyType = fld.typeName();
}
}
}
}
Expand All @@ -157,7 +208,7 @@ QgsPostgresProvider::importVector(
{
primaryKeyType = "serial";
// check the feature count to choose if create a serial8 pk field
if ( layer->featureCount() > 0x10000 - 0x00FF )
if ( layer->featureCount() > 0xFFFFFF )
{
primaryKeyType = "serial8";
}
Expand Down Expand Up @@ -305,6 +356,7 @@ QgsPostgresProvider::importVector(
QgsDebugMsg( "layer loaded" );

// add fields to the layer
if ( fields.size() > 0 )
{
// get the list of fields
QList<QgsField> flist;
Expand All @@ -325,54 +377,16 @@ QgsPostgresProvider::importVector(
// the field type should be natively supported
if ( layer->dataProvider()->name() != provider->name() )
{
QString fieldType = "varchar"; //default to string
int fieldSize = fld.length();
int fieldPrec = fld.precision();
switch ( fld.type() )
if ( !convertField( fld ) )
{
case QVariant::LongLong:
fieldType = "int8";
fieldSize = -1;
fieldPrec = 0;
break;

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

case QVariant::Int:
fieldType = "int";
fieldSize = -1;
fieldPrec = 0;
break;

case QVariant::Double:
if ( fieldSize <= 0 || fieldPrec < 0)
{
fieldType = "real8";
fieldSize = -1;
fieldPrec = -1;
}
else
{
fieldType = "decimal";
}
break;

default:
QgsDebugMsg( "error creating field " + fld.name() + ": unsupported type" );
if ( errorMessage )
*errorMessage = QObject::tr( "unsupported type for field %1" )
QgsDebugMsg( "error creating field " + fld.name() + ": unsupported type" );
if ( errorMessage )
*errorMessage = QObject::tr( "unsupported type for field %1" )
.arg( fld.name() );

delete provider;
return ErrAttributeTypeUnsupported;
delete provider;
return ErrAttributeTypeUnsupported;
}

fld.setTypeName( fieldType );
fld.setLength( fieldSize );
fld.setPrecision( fieldPrec );
}

flist.append( fld );
Expand Down Expand Up @@ -414,9 +428,7 @@ QgsPostgresProvider::importVector(
shallTransform = false;
}

int n = 0;
QStringList errors;

int n = 0, errors = 0;

// make all the changes using one transaction only
if ( provider->connectRW() )
Expand Down Expand Up @@ -454,7 +466,6 @@ QgsPostgresProvider::importVector(
catch ( QgsCsException &e )
{
delete ct;
delete provider;

QString msg = QObject::tr( "Failed to transform a point while drawing a feature of type '%1'. Writing stopped. (Exception: %2)" )
.arg( fet.typeName() ).arg( e.what() );
Expand Down Expand Up @@ -505,9 +516,10 @@ QgsPostgresProvider::importVector(
.arg( flist.first().id() )
.arg( flist.last().id() )
);
errors << QObject::tr( "failed while adding features from %1 to %2" )
.arg( flist.first().id() )
.arg( flist.last().id() );
if ( errorMessage )
*errorMessage += QObject::tr( "failed while adding features from %1 to %2\n" )
.arg( flist.first().id() )
.arg( flist.last().id() );
}
n += flist.size();

Expand All @@ -524,19 +536,15 @@ QgsPostgresProvider::importVector(
delete ct;
}

if ( errors.size() > 0 )
if ( errors > 0 && n > 0 )
{
if ( errorMessage && n > 0 )
if ( errorMessage )
{
errors << QObject::tr( "Only %1 of %2 features written." ).arg( n - errors.size() ).arg( n );
*errorMessage = errors.join( "\n" );
*errorMessage += QObject::tr( "Only %1 of %2 features written." ).arg( n - errors ).arg( n );
}
return ErrFeatureWriteFailed;
}

if ( errorMessage )
errorMessage->clear();

return NoError;
}

Expand Down Expand Up @@ -3257,28 +3265,18 @@ bool QgsPostgresProvider::deleteFeatures( const QgsFeatureIds & id )
}

bool QgsPostgresProvider::addAttributes( const QList<QgsField> &attributes )
{
return addAttributes( attributes, true );
}

bool QgsPostgresProvider::addAttributes( const QList<QgsField> &attributes, bool useNewTransaction )
{
bool returnvalue = true;

if ( isQuery )
return false;

// if there's no rw connection opened then create a new transaction
if ( !connectionRW )
useNewTransaction = true;

if ( !connectRW() )
return false;

try
{
if ( useNewTransaction )
connectionRW->PQexecNR( "BEGIN" );
connectionRW->PQexecNR( "BEGIN" );

for ( QList<QgsField>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter )
{
Expand Down Expand Up @@ -3319,19 +3317,16 @@ bool QgsPostgresProvider::addAttributes( const QList<QgsField> &attributes, bool
}
}

if ( useNewTransaction )
connectionRW->PQexecNR( "COMMIT" );
connectionRW->PQexecNR( "COMMIT" );
}
catch ( PGException &e )
{
e.showErrorMessage( tr( "Error while adding attributes" ) );
if ( useNewTransaction )
connectionRW->PQexecNR( "ROLLBACK" );
connectionRW->PQexecNR( "ROLLBACK" );
returnvalue = false;
}

if ( useNewTransaction )
rewind();
rewind();
return returnvalue;
}

Expand Down Expand Up @@ -4329,7 +4324,8 @@ QGISEXTERN int importVector(
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected,
QString *errorMessage,
bool skipAttributeCreation )
bool skipAttributeCreation,
const QMap<QString,QVariant> *options )
{
return QgsPostgresProvider::importVector( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation );
return QgsPostgresProvider::importVector( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation, options );
}
11 changes: 5 additions & 6 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -70,7 +70,8 @@ class QgsPostgresProvider : public QgsVectorDataProvider
const QgsCoordinateReferenceSystem *destCRS,
bool onlySelected = false,
QString *errorMessage = 0,
bool skipAttributeCreation = false
bool skipAttributeCreation = false,
const QMap<QString,QVariant> *options = 0
);

/**
Expand Down Expand Up @@ -378,11 +379,9 @@ class QgsPostgresProvider : public QgsVectorDataProvider
*/
bool loadFields();

/**Adds new attributes
@param name map with attribute name as key and type as value
@param useNewTransaction create a new transaction
@return true in case of success and false in case of failure*/
bool addAttributes( const QList<QgsField> &attributes, bool useNewTransaction );
/** convert a QgsField to work with PG
*/
static bool convertField( QgsField &field );

/**Adds a list of features
@param useNewTransaction create a new transaction
Expand Down

0 comments on commit 24b73e6

Please sign in to comment.