Skip to content

Commit bda3d75

Browse files
author
jef
committed
postgres fixes:
- allow editing of where clause in database selection dialog - don't open query builder with invalid layer - reallow oid type as primary key type - preset where clause with current one in query builder - actually show error message when QgsPostgresProvider::getGeometryDetails() fails git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11767 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 27d4369 commit bda3d75

File tree

6 files changed

+57
-28
lines changed

6 files changed

+57
-28
lines changed

i18n/qgis_de.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17347,7 +17347,7 @@ p, li { white-space: pre-wrap; }
1734717347
<message>
1734817348
<location filename="../src/providers/postgres/qgspostgresprovider.cpp" line="2899"/>
1734917349
<source>Unable to get feature type and srid</source>
17350-
<translation>Kann den Fearture-Typ und die SRID nicht ermitteln</translation>
17350+
<translation>Kann den Objekttyp und die SRID nicht ermitteln</translation>
1735117351
</message>
1735217352
<message>
1735317353
<location filename="../src/providers/postgres/qgspostgresprovider.cpp" line="2188"/>

src/app/qgsdbtablemodel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void QgsDbTableModel::addTableEntry( QString type, QString schemaName, QString t
7373
pkItem->setData( pkCols );
7474
pkItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
7575
QStandardItem* sqlItem = new QStandardItem( sql );
76-
sqlItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
76+
sqlItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
7777

7878
childItemList.push_back( schemaNameItem );
7979
childItemList.push_back( tableItem );

src/app/qgspgsourceselect.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ QgsPgSourceSelect::QgsPgSourceSelect( QWidget *parent, Qt::WFlags fl )
6767

6868
mTablesTreeView->setEditTriggers( QAbstractItemView::CurrentChanged );
6969

70-
mTablesTreeView->setItemDelegateForColumn( QgsDbTableModel::dbtmPkCol, new QgsPgSourceSelectDelegate( this ) );
70+
mTablesTreeView->setItemDelegate( new QgsPgSourceSelectDelegate( this ) );
7171

7272
QSettings settings;
7373
mTablesTreeView->setSelectionMode( settings.value( "/qgis/addPostgisDC", false ).toBool() ?
@@ -325,9 +325,9 @@ QString QgsPgSourceSelect::layerURI( const QModelIndex &index )
325325
}
326326

327327
uri += QString( " table=\"%1\".\"%2\" (%3) sql=%4" )
328-
.arg( schemaName ).arg( tableName )
329-
.arg( geomColumnName )
330-
.arg( sql );
328+
.arg( schemaName ).arg( tableName )
329+
.arg( geomColumnName )
330+
.arg( sql );
331331

332332
return uri;
333333
}
@@ -341,7 +341,7 @@ void QgsPgSourceSelect::addTables()
341341
QModelIndexList::const_iterator selected_it = selectedIndices.constBegin();
342342
for ( ; selected_it != selectedIndices.constEnd(); ++selected_it )
343343
{
344-
if ( !selected_it->parent().isValid() || selected_it->column()>0 )
344+
if ( !selected_it->parent().isValid() || selected_it->column() > 0 )
345345
{
346346
//top level items only contain the schema names
347347
continue;
@@ -501,18 +501,23 @@ QString QgsPgSourceSelect::connectionInfo()
501501

502502
void QgsPgSourceSelect::setSql( const QModelIndex &index )
503503
{
504-
QgsDebugMsg( QString("%1,%2").arg( index.row() ).arg( index.column() ) );
505504
if ( !index.parent().isValid() )
506505
{
507506
QgsDebugMsg( "schema item found" );
508507
return;
509508
}
510-
509+
511510
QgsVectorLayer *vlayer = new QgsVectorLayer( layerURI( mProxyModel.mapToSource( index ) ), "querybuilder", "postgres" );
512511

512+
if ( !vlayer->isValid() )
513+
{
514+
delete vlayer;
515+
return;
516+
}
517+
513518
// create a query builder object
514519
QgsQueryBuilder *gb = new QgsQueryBuilder( vlayer, this );
515-
if( gb->exec() )
520+
if ( gb->exec() )
516521
{
517522
mTableModel.setSql( mProxyModel.mapToSource( index ), gb->sql() );
518523
}
@@ -537,7 +542,7 @@ QStringList QgsPgSourceSelect::pkCandidates( PGconn *pg, QString schemaName, QSt
537542
QStringList cols;
538543
cols << QString::null;
539544

540-
QString sql = QString( "select attname from pg_attribute join pg_type on atttypid=pg_type.oid WHERE pg_type.typname='int4' AND attrelid=regclass('\"%1\".\"%2\"')" ).arg( schemaName ).arg( viewName );
545+
QString sql = QString( "select attname from pg_attribute join pg_type on atttypid=pg_type.oid WHERE pg_type.typname IN ('int4','oid') AND attrelid=regclass('\"%1\".\"%2\"')" ).arg( schemaName ).arg( viewName );
541546
QgsDebugMsg( sql );
542547
PGresult *colRes = PQexec( pg, sql.toUtf8() );
543548

@@ -614,7 +619,7 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
614619
as = type = "WAITING";
615620
}
616621

617-
mTableModel.addTableEntry( type, schemaName, tableName, column, relkind=="v" ? pkCandidates( pg, schemaName, tableName ) : QStringList(), "" );
622+
mTableModel.addTableEntry( type, schemaName, tableName, column, relkind == "v" ? pkCandidates( pg, schemaName, tableName ) : QStringList(), "" );
618623
n++;
619624
}
620625
}
@@ -694,7 +699,7 @@ bool QgsPgSourceSelect::getTableInfo( PGconn *pg, bool searchGeometryColumnsOnly
694699

695700
addSearchGeometryColumn( schema, table, column );
696701
//details.push_back(geomPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
697-
mTableModel.addTableEntry( "Waiting", schema, table, column, relkind=="v" ? pkCandidates( pg, schema, table ) : QStringList(), "" );
702+
mTableModel.addTableEntry( "Waiting", schema, table, column, relkind == "v" ? pkCandidates( pg, schema, table ) : QStringList(), "" );
698703
n++;
699704
}
700705
}

src/app/qgspgsourceselect.h

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
qgdbsourceselect.h - description
2+
qgpgsourceselect.h - description
33
-------------------
44
begin : Sat Jun 22 2002
55
copyright : (C) 2002 by Gary E.Sherman
@@ -53,13 +53,25 @@ class QgsPgSourceSelectDelegate : public QItemDelegate
5353
const QStyleOptionViewItem &option,
5454
const QModelIndex &index ) const
5555
{
56-
QStringList values = index.data( Qt::UserRole + 1 ).toStringList();
56+
if ( index.column() == QgsDbTableModel::dbtmSql )
57+
{
58+
QLineEdit *le = new QLineEdit( parent );
59+
le->setText( index.data( Qt::DisplayRole ).toString() );
60+
return le;
61+
}
5762

58-
if( values.size()> 0 )
63+
64+
if ( index.column() == QgsDbTableModel::dbtmPkCol )
5965
{
60-
QComboBox *cb = new QComboBox( parent );
61-
cb->addItems( values );
62-
return cb;
66+
QStringList values = index.data( Qt::UserRole + 1 ).toStringList();
67+
68+
if ( values.size() > 0 )
69+
{
70+
QComboBox *cb = new QComboBox( parent );
71+
cb->addItems( values );
72+
cb->setCurrentIndex( cb->findText( index.data( Qt::DisplayRole ).toString() ) );
73+
return cb;
74+
}
6375
}
6476

6577
return NULL;
@@ -68,9 +80,12 @@ class QgsPgSourceSelectDelegate : public QItemDelegate
6880
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
6981
{
7082
QComboBox *cb = dynamic_cast<QComboBox *>( editor );
71-
if ( !cb )
72-
return;
73-
model->setData( index, cb->currentText() );
83+
if ( cb )
84+
model->setData( index, cb->currentText() );
85+
86+
QLineEdit *le = dynamic_cast<QLineEdit *>( editor );
87+
if ( le )
88+
model->setData( index, le->text() );
7489
}
7590
};
7691

src/app/qgsquerybuilder.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ QgsQueryBuilder::QgsQueryBuilder( QgsVectorLayer *layer,
3939
mOrigSubsetString = layer->subsetString();
4040

4141
lblDataUri->setText( layer->publicSource() );
42+
txtSQL->setText( mOrigSubsetString );
43+
4244
populateFields();
4345
}
4446

src/providers/postgres/qgspostgresprovider.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -1054,10 +1054,15 @@ QString QgsPostgresProvider::getPrimaryKey()
10541054

10551055
Result result = connectionRO->PQexec( sql );
10561056

1057-
if ( PQresultStatus( result ) != PGRES_TUPLES_OK ||
1058-
PQntuples( result ) != 1 ||
1059-
QString( PQgetvalue( result, 0, 0 ) ) != "int4" ||
1060-
!uniqueData( mSchemaName, mTableName, primaryKey ) )
1057+
QString type;
1058+
if ( PQresultStatus( result ) == PGRES_TUPLES_OK &&
1059+
PQntuples( result ) == 1 )
1060+
{
1061+
type = PQgetvalue( result, 0, 0 );
1062+
}
1063+
1064+
if (( type != "int4" && type != "oid" ) ||
1065+
!uniqueData( mSchemaName, mTableName, primaryKey ) )
10611066
{
10621067
primaryKey = "";
10631068
}
@@ -2901,8 +2906,10 @@ bool QgsPostgresProvider::getGeometryDetails()
29012906
}
29022907
else // something went wrong...
29032908
{
2904-
log.prepend( tr( "Qgis was unable to determine the type and srid of column %1 in %2. The database communication log was:\n" )
2905-
.arg( geometryColumn ).arg( mSchemaTableName ) );
2909+
log.prepend( tr( "Qgis was unable to determine the type and srid of column %1 in %2. The database communication log was:\n%3" )
2910+
.arg( geometryColumn )
2911+
.arg( mSchemaTableName )
2912+
.arg( QString::fromUtf8( PQresultErrorMessage( result ) ) ) );
29062913
showMessageBox( tr( "Unable to get feature type and srid" ), log );
29072914
}
29082915

0 commit comments

Comments
 (0)