Skip to content

Commit 250eb74

Browse files
committed
postgres provider:
* further boost field retrieval * avoid errornous queries, when no tableoid were found * cache default values
1 parent 5c14c21 commit 250eb74

File tree

2 files changed

+38
-74
lines changed

2 files changed

+38
-74
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+36-71
Original file line numberDiff line numberDiff line change
@@ -682,46 +682,45 @@ bool QgsPostgresProvider::loadFields()
682682
typeMap.insert( typeResult.PQgetvalue( i, 0 ).toInt(), typeInfo );
683683
}
684684

685-
// Collect table oids
686-
QSet<int> tableoids;
687-
for ( int i = 0; i < result.PQnfields(); i++ )
685+
686+
QMap<int, QMap<int, QString> > fmtFieldTypeMap, descrMap, defValMap;
687+
if ( result.PQnfields() > 0 )
688688
{
689-
int tableoid = result.PQftable( i );
690-
if ( tableoid > 0 )
689+
// Collect table oids
690+
QSet<int> tableoids;
691+
for ( int i = 0; i < result.PQnfields(); i++ )
691692
{
692-
tableoids.insert( tableoid );
693+
int tableoid = result.PQftable( i );
694+
if ( tableoid > 0 )
695+
{
696+
tableoids.insert( tableoid );
697+
}
698+
}
699+
QStringList tableoidsList;
700+
foreach ( int tableoid, tableoids )
701+
{
702+
tableoidsList.append( QString::number( tableoid ) );
693703
}
694-
}
695-
QStringList tableoidsList;
696-
foreach ( int tableoid, tableoids )
697-
{
698-
tableoidsList.append( QString::number( tableoid ) );
699-
}
700-
701-
QString tableoidsFilter = "(" + tableoidsList.join( "," ) + ")";
702704

703-
// Collect formatted field types
704-
sql = "SELECT attrelid, attnum, pg_catalog.format_type(atttypid,atttypmod) FROM pg_attribute WHERE attrelid IN " + tableoidsFilter;
705-
QgsPostgresResult fmtFieldTypeResult = connectionRO()->PQexec( sql );
706-
QMap<int, QMap<int, QString> > fmtFieldTypeMap;
707-
for ( int i = 0; i < fmtFieldTypeResult.PQntuples(); ++i )
708-
{
709-
int attrelid = fmtFieldTypeResult.PQgetvalue( i, 0 ).toInt();
710-
int attnum = fmtFieldTypeResult.PQgetvalue( i, 1 ).toInt();
711-
QString formatType = fmtFieldTypeResult.PQgetvalue( i, 2 );
712-
fmtFieldTypeMap[attrelid][attnum] = formatType;
713-
}
705+
QString tableoidsFilter = "(" + tableoidsList.join( "," ) + ")";
714706

715-
// Collect descriptions
716-
sql = "SELECT objoid, objsubid, description FROM pg_description WHERE objoid IN " + tableoidsFilter;
717-
QgsPostgresResult descrResult = connectionRO()->PQexec( sql );
718-
QMap<int, QMap<int, QString> > descrMap;
719-
for ( int i = 0; i < descrResult.PQntuples(); ++i )
720-
{
721-
int objoid = descrResult.PQgetvalue( i, 0 ).toInt();
722-
int objsubid = descrResult.PQgetvalue( i, 1 ).toInt();
723-
QString descr = descrResult.PQgetvalue( i, 2 );
724-
descrMap[objoid][objsubid] = descr;
707+
// Collect formatted field types
708+
sql = "SELECT attrelid, attnum, pg_catalog.format_type(atttypid,atttypmod), pg_catalog.col_description(attrelid,attnum), adsrc"
709+
" FROM pg_attribute"
710+
" LEFT OUTER JOIN pg_attrdef ON attrelid=adrelid AND attnum=adnum"
711+
" WHERE attrelid IN " + tableoidsFilter;
712+
QgsPostgresResult fmtFieldTypeResult = connectionRO()->PQexec( sql );
713+
for ( int i = 0; i < fmtFieldTypeResult.PQntuples(); ++i )
714+
{
715+
int attrelid = fmtFieldTypeResult.PQgetvalue( i, 0 ).toInt();
716+
int attnum = fmtFieldTypeResult.PQgetvalue( i, 1 ).toInt();
717+
QString formatType = fmtFieldTypeResult.PQgetvalue( i, 2 );
718+
QString descr = fmtFieldTypeResult.PQgetvalue( i, 3 );
719+
QString defVal = fmtFieldTypeResult.PQgetvalue( i, 4 );
720+
fmtFieldTypeMap[attrelid][attnum] = formatType;
721+
descrMap[attrelid][attnum] = descr;
722+
defValMap[attrelid][attnum] = defVal;
723+
}
725724
}
726725

727726
QSet<QString> fields;
@@ -907,6 +906,7 @@ bool QgsPostgresProvider::loadFields()
907906

908907
mAttrPalIndexName.insert( i, fieldName );
909908
mAttributeFields.append( QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldPrec, fieldComment ) );
909+
mDefaultValues.insert( i, defValMap[tableoid][attnum] );
910910
}
911911

912912
return true;
@@ -1475,44 +1475,9 @@ bool QgsPostgresProvider::isValid()
14751475
return mValid;
14761476
}
14771477

1478-
QVariant QgsPostgresProvider::defaultValue( QString fieldName, QString tableName, QString schemaName )
1479-
{
1480-
if ( schemaName.isNull() )
1481-
schemaName = mSchemaName;
1482-
if ( tableName.isNull() )
1483-
tableName = mTableName;
1484-
1485-
// Get the default column value from the Postgres information
1486-
// schema. If there is no default we return an empty string.
1487-
1488-
// Maintaining a cache of the results of this query would be quite
1489-
// simple and if this query is called lots, could save some time.
1490-
1491-
QString sql = QString( "SELECT column_default FROM information_schema.columns WHERE column_default IS NOT NULL AND table_schema=%1 AND table_name=%2 AND column_name=%3 " )
1492-
.arg( quotedValue( schemaName ) )
1493-
.arg( quotedValue( tableName ) )
1494-
.arg( quotedValue( fieldName ) );
1495-
1496-
QVariant defaultValue( QString::null );
1497-
1498-
QgsPostgresResult result = connectionRO()->PQexec( sql );
1499-
1500-
if ( result.PQntuples() == 1 )
1501-
defaultValue = result.PQgetvalue( 0, 0 );
1502-
1503-
return defaultValue;
1504-
}
1505-
15061478
QVariant QgsPostgresProvider::defaultValue( int fieldId )
15071479
{
1508-
try
1509-
{
1510-
return defaultValue( field( fieldId ).name() );
1511-
}
1512-
catch ( PGFieldNotFound )
1513-
{
1514-
return QVariant( QString::null );
1515-
}
1480+
return mDefaultValues.value( fieldId, QString::null );
15161481
}
15171482

15181483
QString QgsPostgresProvider::paramValue( QString fieldValue, const QString &defaultValue ) const

src/providers/postgres/qgspostgresprovider.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ class QgsPostgresProvider : public QgsVectorDataProvider
181181

182182
QgsAttributeList pkAttributeIndexes() override { return mPrimaryKeyAttrs; }
183183

184-
/**Returns the default value for field specified by @c fieldName */
185-
QVariant defaultValue( QString fieldName, QString tableName = QString::null, QString schemaName = QString::null );
186-
187184
/**Returns the default value for field specified by @c fieldId */
188185
QVariant defaultValue( int fieldId ) override;
189186

@@ -482,6 +479,8 @@ class QgsPostgresProvider : public QgsVectorDataProvider
482479
QgsPostgresTransaction* mTransaction;
483480

484481
void setTransaction( QgsTransaction* transaction ) override;
482+
483+
QHash<int, QString> mDefaultValues;
485484
};
486485

487486

0 commit comments

Comments
 (0)