Skip to content
Permalink
Browse files

Rework handling of primary keys in HANA (part 3)

  • Loading branch information
mrylov committed Dec 28, 2020
1 parent 531998f commit 44fec96c0daa7fc4c1e7f00f4d70e07f3351f1a2
@@ -552,7 +552,7 @@ void QgsHanaConnection::readLayerInfo( QgsHanaLayerProperty &layerProperty )
{
layerProperty.srid = getColumnSrid( layerProperty.schemaName, layerProperty.tableName, layerProperty.geometryColName );
layerProperty.type = getColumnGeometryType( layerProperty.schemaName, layerProperty.tableName, layerProperty.geometryColName );
layerProperty.pkCols = getPrimaryeKeys( layerProperty );
layerProperty.pkCols = getPrimaryeKeyCandidates( layerProperty );
}

QVector<QgsHanaSchemaProperty> QgsHanaConnection::getSchemas( const QString &ownerName )
@@ -620,39 +620,18 @@ QPair<QString, QMap<QString, short>> QgsHanaConnection::getLayerPrimaryeKey( con
}
}

// The current implementation allows returning only primary keys that contain
// only one column of integer type.
QStringList QgsHanaConnection::getPrimaryeKeys( const QgsHanaLayerProperty &layerProperty )
QStringList QgsHanaConnection::getPrimaryeKeyCandidates( const QgsHanaLayerProperty &layerProperty )
{
QStringList ret;
size_t numColumns = 0;
QStringList intColumns;
const QPair<QString, QMap<QString, short>> primaryKeys = getLayerPrimaryeKey( layerProperty.schemaName, layerProperty.tableName );
const QMap<QString, short> &columns = primaryKeys.second;
for ( auto it = columns.constBegin(); it != columns.constEnd(); ++it )
{
const QString &clmName = it.key();
short clmType = it.value();

if ( clmType == SQLDataTypes::TinyInt || clmType == SQLDataTypes::SmallInt ||
clmType == SQLDataTypes::Integer || clmType == SQLDataTypes::BigInt )
intColumns << clmName;
++numColumns;
}
if ( !layerProperty.isView )
return QStringList();

if ( numColumns == 1 )
{
if ( !intColumns.empty() )
ret << intColumns[0];
}
else if ( numColumns > 0 )
QStringList ret;
QgsHanaResultSetRef rsColumns = getColumns( layerProperty.schemaName, layerProperty.tableName, QStringLiteral( "%" ) );
while ( rsColumns->next() )
{
QgsDebugMsg( QStringLiteral( "Table %1.%2 has %3 columns in its primary key %4" ).arg(
layerProperty.schemaName,
layerProperty.tableName,
QString::number( numColumns ),
primaryKeys.first ) );
ret << rsColumns->getValue( 4/*COLUMN_NAME */ ).toString();
}
rsColumns->close();
return ret;
}

@@ -81,7 +81,7 @@ class QgsHanaConnection : public QObject
private:
QgsHanaConnection( odbc::ConnectionRef connection, const QgsDataSourceUri &uri );

QStringList getPrimaryeKeys( const QgsHanaLayerProperty &layerProperty );
QStringList getPrimaryeKeyCandidates( const QgsHanaLayerProperty &layerProperty );

odbc::PreparedStatementRef createPreparedStatement( const QString &sql, const QVariantList &args );

@@ -210,7 +210,7 @@ QString QgsHanaPrimaryKeyUtils::buildWhereClause( const QgsFields &fields, QgsHa
QList<QString> conditions;
for ( int idx : pkAttrs )
conditions << QStringLiteral( "%1=?" ).arg( QgsHanaUtils::quotedIdentifier( fields[idx].name() ) );
return conditions.join( QStringLiteral( ") AND (" ) );
return conditions.join( QStringLiteral( " AND " ) );
}
case PktUnknown:
return QString();
@@ -253,7 +253,7 @@ QString QgsHanaPrimaryKeyUtils::buildWhereClause( QgsFeatureId featureId, const
const QgsField &fld = fields.at( pkAttrs[i] );
conditions << QStringLiteral( "%1=%2" ).arg( QgsHanaUtils::quotedIdentifier( fld.name() ), pkValues[i].toString() );
}
return conditions.join( QStringLiteral( ") AND (" ) );
return conditions.join( QStringLiteral( " AND " ) );
}
case PktUnknown:
return QString();
@@ -1447,20 +1447,17 @@ void QgsHanaProvider::updateFeatureIdMap( QgsFeatureId fid, const QgsAttributeMa
if ( !( mPrimaryKeyType == PktFidMap || mPrimaryKeyType == PktInt64 ) )
return;

QVariantList k = mPrimaryKeyCntx->removeFid( fid );

int keyCount = std::min( mPrimaryKeyAttrs.size(), k.size() );

QVariantList values = mPrimaryKeyCntx->removeFid( fid );
int keyCount = std::min( mPrimaryKeyAttrs.size(), values.size() );
for ( int i = 0; i < keyCount; i++ )
{
int idx = mPrimaryKeyAttrs.at( i );
if ( !attributes.contains( idx ) )
continue;

k[i] = attributes[ idx ];
values[i] = attributes[ idx ];
}

mPrimaryKeyCntx->insertFid( fid, k );
mPrimaryKeyCntx->insertFid( fid, values );
}

QgsCoordinateReferenceSystem QgsHanaProvider::crs() const

0 comments on commit 44fec96

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