@@ -56,6 +56,52 @@ QMap<QString, QString> QgsPostgresProvider::Conn::passwordCache;
5656int QgsPostgresProvider::providerIds = 0 ;
5757
5858
59+ bool QgsPostgresProvider::convertField ( QgsField &field )
60+ {
61+ QString fieldType = " varchar" ; // default to string
62+ int fieldSize = field.length ();
63+ int fieldPrec = field.precision ();
64+ switch ( field.type () )
65+ {
66+ case QVariant::LongLong:
67+ fieldType = " int8" ;
68+ fieldSize = -1 ;
69+ fieldPrec = 0 ;
70+ break ;
71+
72+ case QVariant::String:
73+ fieldType = " varchar" ;
74+ fieldPrec = -1 ;
75+ break ;
76+
77+ case QVariant::Int:
78+ fieldType = " int" ;
79+ fieldSize = -1 ;
80+ fieldPrec = 0 ;
81+ break ;
82+
83+ case QVariant::Double:
84+ if ( fieldSize <= 0 || fieldPrec < 0 )
85+ {
86+ fieldType = " real8" ;
87+ fieldSize = -1 ;
88+ fieldPrec = -1 ;
89+ }
90+ else
91+ {
92+ fieldType = " decimal" ;
93+ }
94+ break ;
95+
96+ default :
97+ return false ;
98+ }
99+
100+ field.setTypeName ( fieldType );
101+ field.setLength ( fieldSize );
102+ field.setPrecision ( fieldPrec );
103+ return true ;
104+ }
59105
60106QgsPostgresProvider::WriterError
61107QgsPostgresProvider::importVector (
@@ -64,7 +110,8 @@ QgsPostgresProvider::importVector(
64110 const QgsCoordinateReferenceSystem *destCRS,
65111 bool onlySelected,
66112 QString *errorMessage,
67- bool skipAttributeCreation )
113+ bool skipAttributeCreation,
114+ const QMap<QString,QVariant> *options )
68115{
69116 const QgsCoordinateReferenceSystem* outputCRS;
70117 QgsCoordinateTransform* ct = 0 ;
@@ -142,12 +189,16 @@ QgsPostgresProvider::importVector(
142189 else
143190 {
144191 // search for the passed field
145- for ( QgsFieldMap::const_iterator fldIt = fields.begin (); fldIt != fields.end (); ++fldIt )
192+ for ( QgsFieldMap::iterator fldIt = fields.begin (); fldIt != fields.end (); ++fldIt )
146193 {
147194 if ( fldIt.value ().name () == primaryKey )
148195 {
149196 // found, get the field type
150- primaryKeyType = fldIt.value ().typeName ();
197+ QgsField &fld = fldIt.value ();
198+ if ( convertField ( fld ) )
199+ {
200+ primaryKeyType = fld.typeName ();
201+ }
151202 }
152203 }
153204 }
@@ -157,7 +208,7 @@ QgsPostgresProvider::importVector(
157208 {
158209 primaryKeyType = " serial" ;
159210 // check the feature count to choose if create a serial8 pk field
160- if ( layer->featureCount () > 0x10000 - 0x00FF )
211+ if ( layer->featureCount () > 0xFFFFFF )
161212 {
162213 primaryKeyType = " serial8" ;
163214 }
@@ -305,6 +356,7 @@ QgsPostgresProvider::importVector(
305356 QgsDebugMsg ( " layer loaded" );
306357
307358 // add fields to the layer
359+ if ( fields.size () > 0 )
308360 {
309361 // get the list of fields
310362 QList<QgsField> flist;
@@ -325,54 +377,16 @@ QgsPostgresProvider::importVector(
325377 // the field type should be natively supported
326378 if ( layer->dataProvider ()->name () != provider->name () )
327379 {
328- QString fieldType = " varchar" ; // default to string
329- int fieldSize = fld.length ();
330- int fieldPrec = fld.precision ();
331- switch ( fld.type () )
380+ if ( !convertField ( fld ) )
332381 {
333- case QVariant::LongLong:
334- fieldType = " int8" ;
335- fieldSize = -1 ;
336- fieldPrec = 0 ;
337- break ;
338-
339- case QVariant::String:
340- fieldType = " varchar" ;
341- fieldPrec = -1 ;
342- break ;
343-
344- case QVariant::Int:
345- fieldType = " int" ;
346- fieldSize = -1 ;
347- fieldPrec = 0 ;
348- break ;
349-
350- case QVariant::Double:
351- if ( fieldSize <= 0 || fieldPrec < 0 )
352- {
353- fieldType = " real8" ;
354- fieldSize = -1 ;
355- fieldPrec = -1 ;
356- }
357- else
358- {
359- fieldType = " decimal" ;
360- }
361- break ;
362-
363- default :
364- QgsDebugMsg ( " error creating field " + fld.name () + " : unsupported type" );
365- if ( errorMessage )
366- *errorMessage = QObject::tr ( " unsupported type for field %1" )
382+ QgsDebugMsg ( " error creating field " + fld.name () + " : unsupported type" );
383+ if ( errorMessage )
384+ *errorMessage = QObject::tr ( " unsupported type for field %1" )
367385 .arg ( fld.name () );
368386
369- delete provider;
370- return ErrAttributeTypeUnsupported;
387+ delete provider;
388+ return ErrAttributeTypeUnsupported;
371389 }
372-
373- fld.setTypeName ( fieldType );
374- fld.setLength ( fieldSize );
375- fld.setPrecision ( fieldPrec );
376390 }
377391
378392 flist.append ( fld );
@@ -414,9 +428,7 @@ QgsPostgresProvider::importVector(
414428 shallTransform = false ;
415429 }
416430
417- int n = 0 ;
418- QStringList errors;
419-
431+ int n = 0 , errors = 0 ;
420432
421433 // make all the changes using one transaction only
422434 if ( provider->connectRW () )
@@ -454,7 +466,6 @@ QgsPostgresProvider::importVector(
454466 catch ( QgsCsException &e )
455467 {
456468 delete ct;
457- delete provider;
458469
459470 QString msg = QObject::tr ( " Failed to transform a point while drawing a feature of type '%1'. Writing stopped. (Exception: %2)" )
460471 .arg ( fet.typeName () ).arg ( e.what () );
@@ -505,9 +516,10 @@ QgsPostgresProvider::importVector(
505516 .arg ( flist.first ().id () )
506517 .arg ( flist.last ().id () )
507518 );
508- errors << QObject::tr ( " failed while adding features from %1 to %2" )
509- .arg ( flist.first ().id () )
510- .arg ( flist.last ().id () );
519+ if ( errorMessage )
520+ *errorMessage += QObject::tr ( " failed while adding features from %1 to %2\n " )
521+ .arg ( flist.first ().id () )
522+ .arg ( flist.last ().id () );
511523 }
512524 n += flist.size ();
513525
@@ -524,19 +536,15 @@ QgsPostgresProvider::importVector(
524536 delete ct;
525537 }
526538
527- if ( errors. size () > 0 )
539+ if ( errors > 0 && n > 0 )
528540 {
529- if ( errorMessage && n > 0 )
541+ if ( errorMessage )
530542 {
531- errors << QObject::tr ( " Only %1 of %2 features written." ).arg ( n - errors.size () ).arg ( n );
532- *errorMessage = errors.join ( " \n " );
543+ *errorMessage += QObject::tr ( " Only %1 of %2 features written." ).arg ( n - errors ).arg ( n );
533544 }
534545 return ErrFeatureWriteFailed;
535546 }
536547
537- if ( errorMessage )
538- errorMessage->clear ();
539-
540548 return NoError;
541549}
542550
@@ -3257,28 +3265,18 @@ bool QgsPostgresProvider::deleteFeatures( const QgsFeatureIds & id )
32573265}
32583266
32593267bool QgsPostgresProvider::addAttributes ( const QList<QgsField> &attributes )
3260- {
3261- return addAttributes ( attributes, true );
3262- }
3263-
3264- bool QgsPostgresProvider::addAttributes ( const QList<QgsField> &attributes, bool useNewTransaction )
32653268{
32663269 bool returnvalue = true ;
32673270
32683271 if ( isQuery )
32693272 return false ;
32703273
3271- // if there's no rw connection opened then create a new transaction
3272- if ( !connectionRW )
3273- useNewTransaction = true ;
3274-
32753274 if ( !connectRW () )
32763275 return false ;
32773276
32783277 try
32793278 {
3280- if ( useNewTransaction )
3281- connectionRW->PQexecNR ( " BEGIN" );
3279+ connectionRW->PQexecNR ( " BEGIN" );
32823280
32833281 for ( QList<QgsField>::const_iterator iter = attributes.begin (); iter != attributes.end (); ++iter )
32843282 {
@@ -3319,19 +3317,16 @@ bool QgsPostgresProvider::addAttributes( const QList<QgsField> &attributes, bool
33193317 }
33203318 }
33213319
3322- if ( useNewTransaction )
3323- connectionRW->PQexecNR ( " COMMIT" );
3320+ connectionRW->PQexecNR ( " COMMIT" );
33243321 }
33253322 catch ( PGException &e )
33263323 {
33273324 e.showErrorMessage ( tr ( " Error while adding attributes" ) );
3328- if ( useNewTransaction )
3329- connectionRW->PQexecNR ( " ROLLBACK" );
3325+ connectionRW->PQexecNR ( " ROLLBACK" );
33303326 returnvalue = false ;
33313327 }
33323328
3333- if ( useNewTransaction )
3334- rewind ();
3329+ rewind ();
33353330 return returnvalue;
33363331}
33373332
@@ -4329,7 +4324,8 @@ QGISEXTERN int importVector(
43294324 const QgsCoordinateReferenceSystem *destCRS,
43304325 bool onlySelected,
43314326 QString *errorMessage,
4332- bool skipAttributeCreation )
4327+ bool skipAttributeCreation,
4328+ const QMap<QString,QVariant> *options )
43334329{
4334- return QgsPostgresProvider::importVector ( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation );
4330+ return QgsPostgresProvider::importVector ( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation, options );
43354331}
0 commit comments