Skip to content
Permalink
Browse files

Rework handling of primary keys in HANA (part 4)

  • Loading branch information
mrylov committed Dec 29, 2020
1 parent 61fbdcf commit 76ba06ce0c05dfd6fc63cebdfd7c349d050058b6
@@ -148,7 +148,7 @@ bool QgsHanaFeatureIterator::fetchFeature( QgsFeature &feature )
unsigned short paramIndex = 1;

// Read feature id
QgsFeatureId fid = 0;
QgsFeatureId fid = FID_NULL;
bool subsetOfAttributes = mRequest.flags() & QgsFeatureRequest::SubsetOfAttributes;
QgsAttributeList fetchAttributes = mRequest.subsetOfAttributes();

@@ -158,9 +158,10 @@ bool QgsHanaFeatureIterator::fetchFeature( QgsFeature &feature )
{
case QgsHanaPrimaryKeyType::PktInt:
{
int idx = mSource->mPrimaryKeyAttrs.at( 0 );
QVariant v = mResultSet->getValue( paramIndex );
if ( !subsetOfAttributes || fetchAttributes.contains( mSource->mPrimaryKeyAttrs[ 0 ] ) )
feature.setAttribute( 0, v );
if ( !subsetOfAttributes || fetchAttributes.contains( idx ) )
feature.setAttribute( idx, v );
fid = QgsHanaPrimaryKeyUtils::intToFid( v.toInt() );
++paramIndex;
}
@@ -324,20 +325,13 @@ QString QgsHanaFeatureIterator::buildSqlQuery( const QgsFeatureRequest &request

if ( subsetOfAttributes )
{
auto addAttributeIds = [&attrIds]( const QSet<int> &ids )
{
for ( int id : ids )
if ( !attrIds.contains( id ) )
attrIds << id;
};

if ( mRequest.filterType() == QgsFeatureRequest::FilterExpression )
// Ensure that all attributes required for expression filter are fetched
addAttributeIds( request.filterExpression()->referencedAttributeIndexes( mSource->mFields ) );
attrIds.unite( request.filterExpression()->referencedAttributeIndexes( mSource->mFields ) );

if ( !mRequest.orderBy().isEmpty() )
// Ensure that all attributes required for order by are fetched
addAttributeIds( mRequest.orderBy().usedAttributeIndices( mSource->mFields ) );
attrIds.unite( mRequest.orderBy().usedAttributeIndices( mSource->mFields ) );
}

QStringList sqlFields;
@@ -237,8 +237,8 @@ QString QgsHanaPrimaryKeyUtils::buildWhereClause( QgsFeatureId featureId, const
if ( pkValues.isEmpty() )
return QString();

const QgsField &fld = fields.at( pkAttrs[0] );
return QStringLiteral( "%1=%2" ).arg( QgsHanaUtils::quotedIdentifier( fld.name() ), pkValues[0].toString() );
const QgsField &field = fields.at( pkAttrs[0] );
return QStringLiteral( "%1=%2" ).arg( QgsHanaUtils::quotedIdentifier( field.name() ), QgsHanaUtils::toConstant( pkValues[0], field.type() ) );
}
case PktFidMap:
{
@@ -250,8 +250,8 @@ QString QgsHanaPrimaryKeyUtils::buildWhereClause( QgsFeatureId featureId, const
QStringList conditions;
for ( int i = 0; i < pkAttrs.size(); i++ )
{
const QgsField &fld = fields.at( pkAttrs[i] );
conditions << QStringLiteral( "%1=%2" ).arg( QgsHanaUtils::quotedIdentifier( fld.name() ), pkValues[i].toString() );
const QgsField &field = fields.at( pkAttrs[i] );
conditions << QStringLiteral( "%1=%2" ).arg( QgsHanaUtils::quotedIdentifier( field.name() ), QgsHanaUtils::toConstant( pkValues[i], field.type() ) );
}
return conditions.join( QStringLiteral( " AND " ) );
}
@@ -283,13 +283,12 @@ QString QgsHanaPrimaryKeyUtils::buildWhereClause( const QgsFeatureIds &featureId
QVariantList pkValues = primaryKeyCntx.lookupKey( featureId );
if ( pkValues.isEmpty() )
return QString();

fids << pkValues[0].toString();
}
}

const QgsField &fld = fields.at( pkAttrs[0] );
return QStringLiteral( "%1 IN (%2)" ).arg( QgsHanaUtils::quotedIdentifier( fld.name() ), fids.join( ',' ) );
const QgsField &field = fields.at( pkAttrs[0] );
return QStringLiteral( "%1 IN (%2)" ).arg( QgsHanaUtils::quotedIdentifier( field.name() ), fids.join( ',' ) );
}
case PktFidMap:
{
@@ -115,6 +115,37 @@ QString QgsHanaUtils::quotedValue( const QVariant &value )
}
}

QString QgsHanaUtils::toConstant( const QVariant &value, QVariant::Type type )
{
if ( value.isNull() )
return QStringLiteral( "NULL" );

switch ( type )
{
case QVariant::Bool:
return value.toBool() ? QStringLiteral( "TRUE" ) : QStringLiteral( "FALSE" );
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
case QVariant::Double:
return value.toString();
case QVariant::Char:
case QVariant::String:
return QgsHanaUtils::quotedString( value.toString() );
case QVariant::Date:
return QStringLiteral( "date'%1'" ).arg( value.toDate().toString( "yyyy-MM-dd" ) );
case QVariant::DateTime:
return QStringLiteral( "timestamp'%1'" ).arg( value.toDateTime().toString( "yyyy-MM-dd hh:mm:ss.zzz" ) );
case QVariant::Time:
return QStringLiteral( "time'%1'" ).arg( value.toTime().toString( "hh:mm:ss.zzz" ) );
case QVariant::ByteArray:
return QStringLiteral( "x'%1'" ).arg( QString( value.toByteArray().toHex() ) );
default:
return value.toString();
}
}

QString QgsHanaUtils::toString( QgsUnitTypes::DistanceUnit unit )
{
switch ( unit )
@@ -41,6 +41,8 @@ class QgsHanaUtils
static QString quotedString( const QString &str );
static QString quotedValue( const QVariant &value );

static QString toConstant( const QVariant &value, QVariant::Type type );

static QString toString( QgsUnitTypes::DistanceUnit unit );

static QString toQString( const odbc::NString &str );

0 comments on commit 76ba06c

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