Skip to content

Commit

Permalink
[mssql] Fix incorrect precision detection for double/float fields
Browse files Browse the repository at this point in the history
Fixes #15124
  • Loading branch information
nyalldawson committed Oct 8, 2018
1 parent f6b8020 commit 71c5051
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -378,8 +378,8 @@ void QgsMssqlProvider::loadFields()
QgsField(
query.value( 3 ).toString(), sqlType,
sqlTypeName,
query.value( 7 ).toInt(),
query.value( 8 ).toInt() ) );
query.value( 6 ).toInt(),
sqlTypeName == QLatin1String( "decimal" ) ? query.value( 8 ).toInt() : -1 ) );
}
else if ( sqlType == QVariant::Date || sqlType == QVariant::DateTime || sqlType == QVariant::Time )
{
Expand Down
29 changes: 29 additions & 0 deletions tests/src/python/test_provider_mssql.py
Expand Up @@ -176,6 +176,35 @@ def testDateTimeTypes(self):
self.assertEqual(f.attributes()[datetime_idx], QDateTime(
QDate(2004, 3, 4), QTime(13, 41, 52)))

def testFloatDecimalFields(self):
vl = QgsVectorLayer('%s table="qgis_test"."float_dec" sql=' %
(self.dbconn), "testprec", "mssql")
self.assertTrue(vl.isValid())

fields = vl.dataProvider().fields()
self.assertEqual(fields.at(fields.indexFromName(
'float_field')).type(), QVariant.Double)
self.assertEqual(fields.at(fields.indexFromName(
'float_field')).length(), 15)
self.assertEqual(fields.at(fields.indexFromName(
'float_field')).precision(), -1)

self.assertEqual(fields.at(fields.indexFromName(
'dec_field')).type(), QVariant.Double)
self.assertEqual(fields.at(fields.indexFromName(
'dec_field')).length(), 7)
self.assertEqual(fields.at(fields.indexFromName(
'dec_field')).precision(), 3)

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

float_idx = vl.fields().lookupField('float_field')
self.assertIsInstance(f.attributes()[float_idx], float)
self.assertAlmostEqual(f.attributes()[float_idx], 1.1111111111, 5)
dec_idx = vl.fields().lookupField('dec_field')
self.assertIsInstance(f.attributes()[dec_idx], float)
self.assertEqual(f.attributes()[dec_idx], 1.123)

def testCreateLayer(self):
layer = QgsVectorLayer("Point?field=id:integer&field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
Expand Down
13 changes: 13 additions & 0 deletions tests/testdata/provider/testdata_mssql.sql
Expand Up @@ -19,6 +19,9 @@ GO
DROP TABLE IF EXISTS qgis_test.[new_table_multipolygon];
GO

DROP TABLE IF EXISTS qgis_test.[float_dec];
GO

DROP SCHEMA qgis_test;
GO

Expand Down Expand Up @@ -49,6 +52,13 @@ CREATE TABLE qgis_test.[date_times] (
);
GO

CREATE TABLE qgis_test.[float_dec] (
id integer PRIMARY KEY,
float_field float,
dec_field decimal(7,3)
);
GO

INSERT INTO qgis_test.[someData] (pk, cnt, name, name2, num_char, geom) VALUES
(5, -200, NULL, 'NuLl', '5', geometry::STGeomFromText( 'Point(-71.123 78.23)', 4326 )),
(3, 300, 'Pear', 'PEaR', '3', NULL),
Expand All @@ -69,6 +79,9 @@ GO
INSERT INTO qgis_test.[date_times] (id, date_field, time_field, datetime_field ) VALUES
(1, '2004-03-04', '13:41:52', '2004-03-04 13:41:52' );

INSERT INTO qgis_test.[float_dec] (id, float_field, dec_field ) VALUES
(1, 1.1111111111, 1.123 );
GO



Expand Down

0 comments on commit 71c5051

Please sign in to comment.