Skip to content

Commit 00d7498

Browse files
committedAug 23, 2011
[FEATURE] import a layer from canvas into a PG database
1 parent 7a2a328 commit 00d7498

File tree

5 files changed

+787
-17
lines changed

5 files changed

+787
-17
lines changed
 

‎python/core/qgsproviderregistry.sip

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ class QgsProviderRegistry
6060
* @note this method was added in QGIS 1.1
6161
*/
6262
virtual QString protocolDrivers() const;
63+
64+
/** allows to import a vector layer using the provider
65+
* @note this method was added in QGIS 1.8
66+
*/
67+
int importVector( QgsVectorLayer* layer,
68+
const QString& providerKey,
69+
const QString& uri,
70+
const QgsCoordinateReferenceSystem *destCRS,
71+
bool onlySelected = FALSE,
72+
QString *errorMessage /Out/ = 0,
73+
bool skipAttributeCreation = FALSE
74+
) const;
6375

6476

6577

‎src/core/qgsproviderregistry.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgslogger.h"
3131
#include "qgsmessageoutput.h"
3232
#include "qgsprovidermetadata.h"
33+
#include "qgsvectorlayer.h"
3334

3435

3536
// typedefs for provider plugin functions of interest
@@ -42,6 +43,12 @@ typedef QString directoryDrivers_t();
4243
typedef QString protocolDrivers_t();
4344
//typedef int dataCapabilities_t();
4445
//typedef QgsDataItem * dataItem_t(QString);
46+
typedef int importVector_t(QgsVectorLayer* layer,
47+
const QString& uri,
48+
const QgsCoordinateReferenceSystem *destCRS,
49+
bool onlySelected = false,
50+
QString *errorMessage = 0,
51+
bool skipAttributeCreation = false);
4552

4653
QgsProviderRegistry *QgsProviderRegistry::_instance = 0;
4754

@@ -468,7 +475,7 @@ void * QgsProviderRegistry::function( QString const & providerKey,
468475
return 0;
469476
}
470477

471-
QLibrary *QgsProviderRegistry::providerLibrary( QString const & providerKey )
478+
QLibrary *QgsProviderRegistry::providerLibrary( QString const & providerKey ) const
472479
{
473480
QString lib = library( providerKey );
474481

@@ -531,3 +538,34 @@ QgsProviderRegistry::openVector( QString const & dataSource, QString const & pro
531538
return getProvider( providerKey, dataSource );
532539
} // QgsProviderRegistry::openVector
533540
*/
541+
542+
543+
int QgsProviderRegistry::importVector( QgsVectorLayer* layer,
544+
const QString& providerKey,
545+
const QString& uri,
546+
const QgsCoordinateReferenceSystem *destCRS,
547+
bool onlySelected,
548+
QString *errorMessage,
549+
bool skipAttributeCreation
550+
) const
551+
{
552+
QLibrary *myLib = providerLibrary( providerKey );
553+
if ( !myLib )
554+
{
555+
if ( errorMessage )
556+
*errorMessage = QObject::tr( "unable to load %1 provider" ).arg( providerKey );
557+
return -1;
558+
}
559+
560+
importVector_t * pImport = ( importVector_t * ) cast_to_fptr( myLib->resolve( "importVector" ) );
561+
if ( !pImport )
562+
{
563+
delete myLib;
564+
if ( errorMessage )
565+
*errorMessage = QObject::tr( "provider %1 has no importVector feature" ).arg( providerKey );
566+
return -2;
567+
}
568+
569+
delete myLib;
570+
return pImport( layer, uri, destCRS, onlySelected, errorMessage, skipAttributeCreation );
571+
}

‎src/core/qgsproviderregistry.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929
class QgsDataProvider;
3030
class QgsProviderMetadata;
31-
31+
class QgsVectorLayer;
32+
class QgsCoordinateReferenceSystem;
3233

3334

3435
/** \ingroup core
@@ -78,7 +79,7 @@ class CORE_EXPORT QgsProviderRegistry
7879
void *function( const QString & providerKey,
7980
const QString & functionName );
8081

81-
QLibrary *providerLibrary( const QString & providerKey );
82+
QLibrary *providerLibrary( const QString & providerKey ) const;
8283

8384
/** Return list of available providers by their keys */
8485
QStringList providerList() const;
@@ -139,6 +140,16 @@ class CORE_EXPORT QgsProviderRegistry
139140
/** type for data provider metadata associative container */
140141
typedef std::map<QString, QgsProviderMetadata*> Providers;
141142

143+
/** allows to import a vector layer using the provider */
144+
int importVector( QgsVectorLayer* layer,
145+
const QString& providerKey,
146+
const QString& uri,
147+
const QgsCoordinateReferenceSystem *destCRS,
148+
bool onlySelected = false,
149+
QString *errorMessage = 0,
150+
bool skipAttributeCreation = false
151+
) const;
152+
142153
private:
143154

144155
/** ctor private since instance() creates it */

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 676 additions & 12 deletions
Large diffs are not rendered by default.

‎src/providers/postgres/qgspostgresprovider.h

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern "C"
3232
class QgsFeature;
3333
class QgsField;
3434
class QgsGeometry;
35+
class QgsVectorLayer;
3536

3637
#include "qgsdatasourceuri.h"
3738

@@ -48,6 +49,30 @@ class QgsPostgresProvider : public QgsVectorDataProvider
4849
Q_OBJECT
4950

5051
public:
52+
53+
enum WriterError
54+
{
55+
NoError = 0,
56+
ErrDriverNotFound,
57+
ErrCreateDataSource,
58+
ErrCreateLayer,
59+
ErrAttributeTypeUnsupported,
60+
ErrAttributeCreationFailed,
61+
ErrProjection,
62+
ErrFeatureWriteFailed,
63+
ErrInvalidLayer,
64+
ErrConnectionFailed,
65+
};
66+
67+
/** Import a vector layer into the database */
68+
static WriterError importVector( QgsVectorLayer* layer,
69+
const QString& uri,
70+
const QgsCoordinateReferenceSystem *destCRS,
71+
bool onlySelected = false,
72+
QString *errorMessage = 0,
73+
bool skipAttributeCreation = false
74+
);
75+
5176
/**
5277
* Constructor for the provider. The uri must be in the following format:
5378
* host=localhost user=gsherman dbname=test password=xxx table=test.alaska (the_geom)
@@ -295,6 +320,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
295320
*/
296321
QString description() const;
297322

323+
298324
signals:
299325
/**
300326
* This is emitted whenever the worker thread has fully calculated the
@@ -316,6 +342,7 @@ class QgsPostgresProvider : public QgsVectorDataProvider
316342
void repaintRequested();
317343

318344
private:
345+
319346
int providerId; // id to append to provider specific identified (like cursors)
320347

321348
bool declareCursor( const QString &cursorName,
@@ -337,11 +364,11 @@ class QgsPostgresProvider : public QgsVectorDataProvider
337364

338365
/** Double quote a PostgreSQL identifier for placement in a SQL string.
339366
*/
340-
QString quotedIdentifier( QString ident ) const;
367+
static QString quotedIdentifier( QString ident );
341368

342369
/** Quote a value for placement in a SQL string.
343370
*/
344-
QString quotedValue( QString value ) const;
371+
static QString quotedValue( QString value );
345372

346373
/** expression to retrieve value
347374
*/
@@ -351,6 +378,17 @@ class QgsPostgresProvider : public QgsVectorDataProvider
351378
*/
352379
bool loadFields();
353380

381+
/**Adds new attributes
382+
@param name map with attribute name as key and type as value
383+
@param useNewTransaction create a new transaction
384+
@return true in case of success and false in case of failure*/
385+
bool addAttributes( const QList<QgsField> &attributes, bool useNewTransaction );
386+
387+
/**Adds a list of features
388+
@param useNewTransaction create a new transaction
389+
@return true in case of success and false in case of failure*/
390+
bool addFeatures( QgsFeatureList & flist, bool useNewTransaction );
391+
354392
/**Parses the enum_range of an attribute and inserts the possible values into a stringlist
355393
@param enumValues the stringlist where the values are appended
356394
@param attributeName the name of the enum attribute
@@ -712,6 +750,13 @@ class QgsPostgresProvider : public QgsVectorDataProvider
712750
* Default value for primary key
713751
*/
714752
QString mPrimaryKeyDefault;
753+
754+
#if 0
755+
/** used to cache the lastest fetched features */
756+
QHash<QgsFeatureId, QgsFeature> mFeatureMap;
757+
QList<QgsFeatureId> mPriorityIds;
758+
#endif
759+
715760
};
716761

717762
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.