Skip to content

Commit

Permalink
[FEATURE] postgres provider: allow copying tables from one schema to …
Browse files Browse the repository at this point in the history
…an other
  • Loading branch information
jef-n committed Sep 11, 2015
1 parent 69e53df commit fd2ad13
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/core/qgsdatasourceuri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ void QgsDataSourceURI::clearSchema()
mSchema = "";
}

void QgsDataSourceURI::setSchema( QString schema )
{
mSchema = schema;
}

QString QgsDataSourceURI::escape( const QString &theVal, QChar delim = '\'' ) const
{
QString val = theVal;
Expand Down Expand Up @@ -547,9 +552,9 @@ QString QgsDataSourceURI::uri() const
QByteArray QgsDataSourceURI::encodedUri() const
{
QUrl url;
Q_FOREACH ( const QString& key, mParams.uniqueKeys() )
Q_FOREACH( const QString& key, mParams.uniqueKeys() )
{
Q_FOREACH ( const QString& value, mParams.values( key ) )
Q_FOREACH( const QString& value, mParams.values( key ) )
{
url.addQueryItem( key, value );
}
Expand All @@ -563,7 +568,7 @@ void QgsDataSourceURI::setEncodedUri( const QByteArray & uri )
QUrl url;
url.setEncodedQuery( uri );
QPair<QString, QString> item;
Q_FOREACH ( item, url.queryItems() )
Q_FOREACH( item, url.queryItems() )
{
mParams.insertMulti( item.first, item.second );
}
Expand Down Expand Up @@ -659,7 +664,7 @@ void QgsDataSourceURI::setParam( const QString &key, const QString &value )

void QgsDataSourceURI::setParam( const QString &key, const QStringList &value )
{
Q_FOREACH ( const QString& val, value )
Q_FOREACH( const QString& val, value )
{
mParams.insertMulti( key, val );
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsdatasourceuri.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ class CORE_EXPORT QgsDataSourceURI
bool selectAtIdDisabled() const;

void clearSchema();

//! set the table schema
// @note added in 2.11
void setSchema( QString schema );

void setSql( QString sql );

QString host() const;
Expand Down
28 changes: 24 additions & 4 deletions src/providers/postgres/qgspostgresdataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ QVector<QgsDataItem*> QgsPGConnectionItem::createChildren()
return items;
}

Q_FOREACH ( const QgsPostgresSchemaProperty& schema, schemas )
Q_FOREACH( const QgsPostgresSchemaProperty& schema, schemas )
{
QgsPGSchemaItem * schemaItem = new QgsPGSchemaItem( this, mName, schema.name, mPath + "/" + schema.name );
if ( !schema.description.isEmpty() )
Expand Down Expand Up @@ -187,6 +187,11 @@ void QgsPGConnectionItem::createSchema()
}

bool QgsPGConnectionItem::handleDrop( const QMimeData * data, Qt::DropAction )
{
return handleDrop( data, QString::null );
}

bool QgsPGConnectionItem::handleDrop( const QMimeData * data, QString toSchema )
{
if ( !QgsMimeDataUtils::isUriList( data ) )
return false;
Expand All @@ -204,7 +209,7 @@ bool QgsPGConnectionItem::handleDrop( const QMimeData * data, Qt::DropAction )
QStringList importResults;
bool hasError = false;
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data );
Q_FOREACH ( const QgsMimeDataUtils::Uri& u, lst )
Q_FOREACH( const QgsMimeDataUtils::Uri& u, lst )
{
if ( u.layerType != "vector" )
{
Expand All @@ -220,6 +225,12 @@ bool QgsPGConnectionItem::handleDrop( const QMimeData * data, Qt::DropAction )
{
uri.setDataSource( QString(), u.name, "geom" );
QgsDebugMsg( "URI " + uri.uri() );

if ( !toSchema.isNull() )
{
uri.setSchema( toSchema );
}

QgsVectorLayerImport::ImportError err;
QString importError;
err = QgsVectorLayerImport::importLayer( srcLayer, uri.uri(), "postgres", &srcLayer->crs(), false, &importError, false, 0, progress );
Expand Down Expand Up @@ -473,7 +484,7 @@ QVector<QgsDataItem*> QgsPGSchemaItem::createChildren()
}

bool dontResolveType = QgsPostgresConn::dontResolveType( mConnectionName );
Q_FOREACH ( QgsPostgresLayerProperty layerProperty, layerProperties )
Q_FOREACH( QgsPostgresLayerProperty layerProperty, layerProperties )
{
if ( layerProperty.schemaName != mName )
continue;
Expand Down Expand Up @@ -661,6 +672,15 @@ QgsPGLayerItem *QgsPGSchemaItem::createLayer( QgsPostgresLayerProperty layerProp
return layerItem;
}

bool QgsPGSchemaItem::handleDrop( const QMimeData * data, Qt::DropAction )
{
QgsPGConnectionItem *conn = qobject_cast<QgsPGConnectionItem *>( parent() );
if ( !conn )
return 0;

return conn->handleDrop( data, mName );
}

// ---------------------------------------------------------------------------
QgsPGRootItem::QgsPGRootItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
Expand All @@ -677,7 +697,7 @@ QgsPGRootItem::~QgsPGRootItem()
QVector<QgsDataItem*> QgsPGRootItem::createChildren()
{
QVector<QgsDataItem*> connections;
Q_FOREACH ( const QString& connName, QgsPostgresConn::connectionList() )
Q_FOREACH( const QString& connName, QgsPostgresConn::connectionList() )
{
connections << new QgsPGConnectionItem( this, connName, mPath + "/" + connName );
}
Expand Down
5 changes: 5 additions & 0 deletions src/providers/postgres/qgspostgresdataitems.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class QgsPGConnectionItem : public QgsDataCollectionItem
virtual bool acceptDrop() override { return true; }
virtual bool handleDrop( const QMimeData * data, Qt::DropAction action ) override;

bool handleDrop( const QMimeData * data, QString toSchema );

signals:
void addGeometryColumn( QgsPostgresLayerProperty );

Expand All @@ -84,6 +86,9 @@ class QgsPGSchemaItem : public QgsDataCollectionItem
QVector<QgsDataItem*> createChildren() override;
virtual QList<QAction*> actions() override;

virtual bool acceptDrop() override { return true; }
virtual bool handleDrop( const QMimeData * data, Qt::DropAction action ) override;

public slots:
void deleteSchema();
void renameSchema();
Expand Down

0 comments on commit fd2ad13

Please sign in to comment.