Skip to content
Permalink
Browse files

Add model for database tables from a connection

Optionally limited to a specific schema, if desired
  • Loading branch information
nyalldawson committed Mar 7, 2020
1 parent e0ca4d4 commit f7731bc28f46b7894e8cf6e87d5c27261ba3864d
@@ -210,6 +210,7 @@ This information is calculated from the geometry columns types.
.. seealso:: :py:func:`geometryColumnTypes`
%End

bool operator==( const QgsAbstractDatabaseProviderConnection::TableProperty &other ) const;

};

@@ -0,0 +1,88 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsdatabasetablemodel.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsDatabaseTableModel : QAbstractItemModel
{
%Docstring
A model containing tables from a database connection.

This class does not automatically subscribe to database updates. Tables are queried
from the database initially upon model construction. In order
to update the listed tbales, QgsDatabaseTableModel.refresh() must be manually
called.

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsdatabasetablemodel.h"
%End
public:

enum Role
{
RoleTableName,
RoleSchema,
RoleTableFlags,
RoleComment,
RoleCustomInfo,
RoleWkbType,
RoleCrs,
};

explicit QgsDatabaseTableModel( const QString &provider, const QString &connection, const QString &schema = QString(), QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsDatabaseTableModel, for the specified ``provider`` and ``connection`` name.

The optional ``schema`` argument can be used to restrict the tables to those from a specific schema.

.. warning::

The ``provider`` must support the connection API methods in its QgsProviderMetadata implementation
in order for the model to work correctly.
%End

explicit QgsDatabaseTableModel( QgsAbstractDatabaseProviderConnection *connection /Transfer/, const QString &schema = QString(), QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsDatabaseTableModel, for the specified ``connection``.

The optional ``schema`` argument can be used to restrict the tables to those from a specific schema.

Ownership of ``connection`` is transferred to the model.
%End

virtual QModelIndex parent( const QModelIndex &child ) const;

virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;

virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const;

virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;

virtual QModelIndex index( int row, int column, const QModelIndex &parent ) const;


public slots:

void refresh();
%Docstring
Refreshes the table list by querying the underlying connection.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsdatabasetablemodel.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -37,6 +37,7 @@
%Include auto_generated/qgscredentials.sip
%Include auto_generated/qgsdartmeasurement.sip
%Include auto_generated/qgsdatabaseschemamodel.sip
%Include auto_generated/qgsdatabasetablemodel.sip
%Include auto_generated/qgsdatadefinedsizelegend.sip
%Include auto_generated/qgsdataitem.sip
%Include auto_generated/qgsdataitemprovider.sip
@@ -228,6 +228,7 @@ SET(QGIS_CORE_SRCS
qgscredentials.cpp
qgsdartmeasurement.cpp
qgsdatabaseschemamodel.cpp
qgsdatabasetablemodel.cpp
qgsdatadefinedsizelegend.cpp
qgsdataitem.cpp
qgsdataitemprovider.cpp
@@ -729,6 +730,7 @@ SET(QGIS_CORE_HDRS
qgscredentials.h
qgsdartmeasurement.h
qgsdatabaseschemamodel.h
qgsdatabasetablemodel.h
qgsdatadefinedsizelegend.h
qgsdataitem.h
qgsdataitemprovider.h
@@ -237,6 +237,18 @@ int QgsAbstractDatabaseProviderConnection::TableProperty::maxCoordinateDimension
return res;
}

bool QgsAbstractDatabaseProviderConnection::TableProperty::operator==( const QgsAbstractDatabaseProviderConnection::TableProperty &other ) const
{
return mSchema == other.mSchema &&
mTableName == other.mTableName &&
mGeometryColumn == other.mGeometryColumn &&
mGeometryColumnCount == other.mGeometryColumnCount &&
mPkColumns == other.mPkColumns &&
mFlags == other.mFlags &&
mComment == other.mComment &&
mInfo == other.mInfo;
}


void QgsAbstractDatabaseProviderConnection::TableProperty::setGeometryColumnTypes( const QList<QgsAbstractDatabaseProviderConnection::TableProperty::GeometryColumnType> &columnTypes )
{
@@ -248,6 +248,7 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
*/
int maxCoordinateDimensions() const;

bool operator==( const QgsAbstractDatabaseProviderConnection::TableProperty &other ) const;

private:

@@ -0,0 +1,186 @@
/***************************************************************************
qgsdatabasetablemodel.cpp
------------------------
Date : March 2020
Copyright : (C) 2020 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsdatabasetablemodel.h"
#include "qgsproviderregistry.h"
#include "qgsprovidermetadata.h"
#include "qgsabstractdatabaseproviderconnection.h"
#include "qgsdataitem.h"

QgsDatabaseTableModel::QgsDatabaseTableModel( const QString &provider, const QString &connection, const QString &schema, QObject *parent )
: QAbstractItemModel( parent )
, mSchema( schema )
{
QgsProviderMetadata *metadata = QgsProviderRegistry::instance()->providerMetadata( provider );
Q_ASSERT( metadata );

mConnection.reset( dynamic_cast<QgsAbstractDatabaseProviderConnection *>( metadata->createConnection( connection ) ) );
Q_ASSERT( mConnection );
init();
}

QgsDatabaseTableModel::QgsDatabaseTableModel( QgsAbstractDatabaseProviderConnection *connection, const QString &schema, QObject *parent )
: QAbstractItemModel( parent )
, mConnection( connection )
, mSchema( schema )
{
Q_ASSERT( mConnection );
init();
}

void QgsDatabaseTableModel::init()
{
Q_ASSERT( mConnection->capabilities() & QgsAbstractDatabaseProviderConnection::Capability::Tables );
mTables = mConnection->tables( mSchema );
}

QModelIndex QgsDatabaseTableModel::parent( const QModelIndex &child ) const
{
Q_UNUSED( child )
return QModelIndex();
}


int QgsDatabaseTableModel::rowCount( const QModelIndex &parent ) const
{
if ( parent.isValid() )
return 0;

return mTables.count();
}

int QgsDatabaseTableModel::columnCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
return 1;
}


QVariant QgsDatabaseTableModel::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() )
return QVariant();

if ( index.row() >= mTables.count() )
return QVariant();

const QgsAbstractDatabaseProviderConnection::TableProperty &table = mTables[ index.row() ];
switch ( role )
{
case Qt::DisplayRole:
case Qt::ToolTipRole:
{
return mSchema.isEmpty() && !table.schema().isEmpty() ? QStringLiteral( "%1.%2" ).arg( table.schema(), table.tableName() ) : table.tableName();
}

case RoleTableName:
{
return table.tableName();
}

case Qt::DecorationRole:
case RoleWkbType:
case RoleCrs:
{
if ( table.geometryColumnTypes().empty() )
{
if ( role == Qt::DecorationRole )
return QgsLayerItem::iconTable();
else
return QVariant();
}

if ( role == Qt::DecorationRole )
{
const QgsWkbTypes::GeometryType geomType = QgsWkbTypes::geometryType( table.geometryColumnTypes().at( 0 ).wkbType );
switch ( geomType )
{
case QgsWkbTypes::PointGeometry:
{
return QgsLayerItem::iconPoint();
}
case QgsWkbTypes::PolygonGeometry :
{
return QgsLayerItem::iconPolygon();
}
case QgsWkbTypes::LineGeometry :
{
return QgsLayerItem::iconLine();
}
default:
return QgsLayerItem::iconTable();
}
return QVariant();
}
else if ( role == RoleWkbType )
return table.geometryColumnTypes().at( 0 ).wkbType;
else if ( role == RoleCrs )
return table.geometryColumnTypes().at( 0 ).crs;

return QVariant();
}

case RoleSchema:
return table.schema();

case RoleTableFlags:
return static_cast< int >( table.flags() );

case RoleComment:
return table.comment();

case RoleCustomInfo:
return table.info();

}

return QVariant();
}

QModelIndex QgsDatabaseTableModel::index( int row, int column, const QModelIndex &parent ) const
{
if ( hasIndex( row, column, parent ) )
{
return createIndex( row, column, row );
}

return QModelIndex();
}

void QgsDatabaseTableModel::refresh()
{
const QList< QgsAbstractDatabaseProviderConnection::TableProperty > newTables = mConnection->tables( mSchema );
const QList< QgsAbstractDatabaseProviderConnection::TableProperty > oldTables = mTables;

for ( const QgsAbstractDatabaseProviderConnection::TableProperty &oldTable : oldTables )
{
if ( !newTables.contains( oldTable ) )
{
int r = mTables.indexOf( oldTable );
beginRemoveRows( QModelIndex(), r, r );
mTables.removeAt( r );
endRemoveRows();
}
}

for ( const QgsAbstractDatabaseProviderConnection::TableProperty &newTable : newTables )
{
if ( !mTables.contains( newTable ) )
{
beginInsertRows( QModelIndex(), mTables.count(), mTables.count() );
mTables.append( newTable );
endInsertRows();
}
}
}

0 comments on commit f7731bc

Please sign in to comment.
You can’t perform that action at this time.