Skip to content
Permalink
Browse files

Fix: field precision is not set in HANA

  • Loading branch information
mrylov authored and nyalldawson committed Apr 7, 2021
1 parent 7601bbe commit 59a365a8dd53b3923fd9a7ad902680cf50613978
Showing with 33 additions and 2 deletions.
  1. +2 −2 src/providers/hana/qgshanaconnection.cpp
  2. +31 −0 tests/src/python/test_provider_hana.py
@@ -716,7 +716,7 @@ void QgsHanaConnection::readQueryFields( const QString &schemaName, const QStrin
field.isAutoIncrement = rsmd->isAutoIncrement( i );
field.isUnique = isColumnUnique( schema, field.tableName, field.name );
field.size = static_cast<int>( rsmd->getColumnLength( i ) );
field.precision = -1;
field.precision = static_cast<int>( rsmd->getScale( i ) );
if ( field.isGeometry() )
field.srid = getColumnSrid( schema, field.tableName, field.name );
// As field comments cannot be retrieved via ODBC, we get it from SYS.TABLE_COLUMNS.
@@ -778,14 +778,14 @@ void QgsHanaConnection::readTableFields( const QString &schemaName, const QStrin
field.type = rsColumns->getShort( 5/*DATA_TYPE*/ );
field.typeName = rsColumns->getString( 6/*TYPE_NAME*/ );
field.size = rsColumns->getInt( 7/*COLUMN_SIZE*/ );
field.precision = static_cast<int>( rsColumns->getShort( 9/*DECIMAL_DIGITS*/ ) );
field.isSigned = field.type == SQLDataTypes::SmallInt || field.type == SQLDataTypes::Integer ||
field.type == SQLDataTypes::BigInt || field.type == SQLDataTypes::Decimal ||
field.type == SQLDataTypes::Numeric || field.type == SQLDataTypes::Real ||
field.type == SQLDataTypes::Float || field.type == SQLDataTypes::Double;
field.isNullable = rsColumns->getString( 18/*IS_NULLABLE*/ ) == QLatin1String( "TRUE" );
field.isAutoIncrement = isColumnAutoIncrement( field.name );
field.isUnique = isColumnUnique( field.name );
field.precision = -1;
if ( field.isGeometry() )
field.srid = getColumnSrid( schemaName, tableName, field.name );
field.comment = rsColumns->getString( 12/*REMARKS*/ );
@@ -216,6 +216,37 @@ def testBooleanType(self):
expected = {1: True, 2: False, 3: NULL}
self.assertEqual(values, expected)

def testDecimalAndFloatTypes(self):
create_sql = f'CREATE TABLE "{self.schemaName}"."decimal_and_float_type" ( ' \
'"id" INTEGER NOT NULL PRIMARY KEY,' \
'"decimal_field" DECIMAL(15,4),' \
'"float_field" FLOAT(12))'
insert_sql = f'INSERT INTO "{self.schemaName}"."decimal_and_float_type" ("id", "decimal_field", ' \
f'"float_field") VALUES (?, ?, ?) '
insert_args = [[1, 1.1234, 1.76543]]
self.prepareTestTable('decimal_and_float_type', create_sql, insert_sql, insert_args)

vl = self.createVectorLayer(f'table="{self.schemaName}"."decimal_and_float_type" sql=', 'testdecimalfloat')

fields = vl.dataProvider().fields()
decimal_field = fields.at(fields.indexFromName('decimal_field'))
self.assertEqual(decimal_field.type(), QVariant.Double)
self.assertEqual(decimal_field.length(), 15)
self.assertEqual(decimal_field.precision(), 4)
float_field = fields.at(fields.indexFromName('float_field'))
self.assertEqual(float_field.type(), QVariant.Double)
self.assertEqual(float_field.length(), 7)
self.assertEqual(float_field.precision(), 0)

feat = next(vl.getFeatures(QgsFeatureRequest()))

decimal_idx = vl.fields().lookupField('decimal_field')
self.assertIsInstance(feat.attributes()[decimal_idx], float)
self.assertEqual(feat.attributes()[decimal_idx], 1.1234)
float_idx = vl.fields().lookupField('float_field')
self.assertIsInstance(feat.attributes()[float_idx], float)
self.assertAlmostEqual(feat.attributes()[float_idx], 1.76543, 5)

def testDateTimeTypes(self):
create_sql = f'CREATE TABLE "{self.schemaName}"."date_time_type" ( ' \
'"id" INTEGER NOT NULL PRIMARY KEY,' \

0 comments on commit 59a365a

Please sign in to comment.