Skip to content
Permalink
Browse files

Revise primary key determination

  • Loading branch information
stefanuhrig committed Jan 9, 2021
1 parent e53c9b9 commit 6a6c8969d7fcf27fa8ab8f6abd15fbae69cbd9b2
@@ -584,35 +584,23 @@ QVector<QgsHanaSchemaProperty> QgsHanaConnection::getSchemas( const QString &own
return list;
}

QPair<QString, QMap<QString, short>> QgsHanaConnection::getLayerPrimaryeKey( const QString &schemaName, const QString &tableName )
QStringList QgsHanaConnection::getLayerPrimaryeKey( const QString &schemaName, const QString &tableName )
{
try
{
DatabaseMetaDataUnicodeRef dbmd = mConnection->getDatabaseMetaDataUnicode();
ResultSetRef rsPrimaryKeys = dbmd->getPrimaryKeys( nullptr,
QgsHanaUtils::toUtf16( schemaName ),
QgsHanaUtils::toUtf16( tableName ) );
QString keyName;
QMap<QString, short> keyColumns;
QMap<int, QString> pos2Name;
while ( rsPrimaryKeys->next() )
{
QString clmName = QgsHanaUtils::toQString( rsPrimaryKeys->getNString( 4 /*COLUMN_NAME*/ ) );
if ( keyName.isEmpty() )
keyName = QgsHanaUtils::toQString( rsPrimaryKeys->getNString( 6 /*PK_NAME*/ ) );
ResultSetRef rsColumns = dbmd->getColumns( nullptr,
QgsHanaUtils::toUtf16( schemaName ),
QgsHanaUtils::toUtf16( tableName ),
QgsHanaUtils::toUtf16( clmName ) );
while ( rsColumns->next() )
{
Short dataType = rsColumns->getShort( 5 /*DATA_TYPE*/ );
keyColumns.insert( clmName, *dataType );
}
rsColumns->close();
int pos = *rsPrimaryKeys->getInt( 5 /*KEY_SEQ*/ );
pos2Name.insert(pos, clmName );
}
rsPrimaryKeys->close();

return qMakePair( keyName, keyColumns );
return pos2Name.values();
}
catch ( const Exception &ex )
{
@@ -66,7 +66,7 @@ class QgsHanaConnection : public QObject
bool userTablesOnly = true );
void readLayerInfo( QgsHanaLayerProperty &layerProperty );
QVector<QgsHanaSchemaProperty> getSchemas( const QString &ownerName );
QPair<QString, QMap<QString, short>> getLayerPrimaryeKey( const QString &schemaName, const QString &tableName );
QStringList getLayerPrimaryeKey( const QString &schemaName, const QString &tableName );
QgsWkbTypes::Type getColumnGeometryType( const QString &schemaName, const QString &tableName, const QString &columnName );
QString getColumnDataType( const QString &schemaName, const QString &tableName, const QString &columnName );
int getColumnSrid( const QString &schemaName, const QString &tableName, const QString &columnName );
@@ -1406,9 +1406,8 @@ void QgsHanaProvider::determinePrimaryKey()
if ( !mIsQuery )
{
QgsHanaConnectionRef conn( mUri );
auto layerPrimaryKey = conn->getLayerPrimaryeKey( mSchemaName, mTableName );
const QMap<QString, short> &columns = layerPrimaryKey.second;
auto primaryKey = QgsHanaPrimaryKeyUtils::determinePrimaryKeyFromColumns( columns.keys(), mAttributeFields );
QStringList layerPrimaryKey = conn->getLayerPrimaryeKey( mSchemaName, mTableName );
auto primaryKey = QgsHanaPrimaryKeyUtils::determinePrimaryKeyFromColumns( layerPrimaryKey, mAttributeFields );
mPrimaryKeyType = primaryKey.first;
mPrimaryKeyAttrs = primaryKey.second;
}
@@ -328,9 +328,19 @@ QList<QgsHanaProviderConnection::TableProperty> QgsHanaProviderConnection::table
property.setTableName( layerInfo.tableName );
property.setSchema( layerInfo.schemaName );
property.setGeometryColumn( layerInfo.geometryColName );
property.setPrimaryKeyColumns( layerInfo.pkCols );
property.setGeometryColumnCount( layerInfo.geometryColName.isEmpty() ? 0 : 1 );
property.setComment( layerInfo.tableComment );

if (layerInfo.isView)
{
// Set the candidates
property.setPrimaryKeyColumns( layerInfo.pkCols );
}
else // Fetch and set the real pks
{
QStringList pks = conn->getLayerPrimaryeKey( layerInfo.schemaName, layerInfo.tableName );
property.setPrimaryKeyColumns( pks );
}
tables.push_back( property );
}
}

0 comments on commit 6a6c896

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