Skip to content

Commit 71c5051

Browse files
committedOct 8, 2018
[mssql] Fix incorrect precision detection for double/float fields
Fixes #15124
1 parent f6b8020 commit 71c5051

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed
 

‎src/providers/mssql/qgsmssqlprovider.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ void QgsMssqlProvider::loadFields()
378378
QgsField(
379379
query.value( 3 ).toString(), sqlType,
380380
sqlTypeName,
381-
query.value( 7 ).toInt(),
382-
query.value( 8 ).toInt() ) );
381+
query.value( 6 ).toInt(),
382+
sqlTypeName == QLatin1String( "decimal" ) ? query.value( 8 ).toInt() : -1 ) );
383383
}
384384
else if ( sqlType == QVariant::Date || sqlType == QVariant::DateTime || sqlType == QVariant::Time )
385385
{

‎tests/src/python/test_provider_mssql.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ def testDateTimeTypes(self):
176176
self.assertEqual(f.attributes()[datetime_idx], QDateTime(
177177
QDate(2004, 3, 4), QTime(13, 41, 52)))
178178

179+
def testFloatDecimalFields(self):
180+
vl = QgsVectorLayer('%s table="qgis_test"."float_dec" sql=' %
181+
(self.dbconn), "testprec", "mssql")
182+
self.assertTrue(vl.isValid())
183+
184+
fields = vl.dataProvider().fields()
185+
self.assertEqual(fields.at(fields.indexFromName(
186+
'float_field')).type(), QVariant.Double)
187+
self.assertEqual(fields.at(fields.indexFromName(
188+
'float_field')).length(), 15)
189+
self.assertEqual(fields.at(fields.indexFromName(
190+
'float_field')).precision(), -1)
191+
192+
self.assertEqual(fields.at(fields.indexFromName(
193+
'dec_field')).type(), QVariant.Double)
194+
self.assertEqual(fields.at(fields.indexFromName(
195+
'dec_field')).length(), 7)
196+
self.assertEqual(fields.at(fields.indexFromName(
197+
'dec_field')).precision(), 3)
198+
199+
f = next(vl.getFeatures(QgsFeatureRequest()))
200+
201+
float_idx = vl.fields().lookupField('float_field')
202+
self.assertIsInstance(f.attributes()[float_idx], float)
203+
self.assertAlmostEqual(f.attributes()[float_idx], 1.1111111111, 5)
204+
dec_idx = vl.fields().lookupField('dec_field')
205+
self.assertIsInstance(f.attributes()[dec_idx], float)
206+
self.assertEqual(f.attributes()[dec_idx], 1.123)
207+
179208
def testCreateLayer(self):
180209
layer = QgsVectorLayer("Point?field=id:integer&field=fldtxt:string&field=fldint:integer",
181210
"addfeat", "memory")

‎tests/testdata/provider/testdata_mssql.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ GO
1919
DROP TABLE IF EXISTS qgis_test.[new_table_multipolygon];
2020
GO
2121

22+
DROP TABLE IF EXISTS qgis_test.[float_dec];
23+
GO
24+
2225
DROP SCHEMA qgis_test;
2326
GO
2427

@@ -49,6 +52,13 @@ CREATE TABLE qgis_test.[date_times] (
4952
);
5053
GO
5154

55+
CREATE TABLE qgis_test.[float_dec] (
56+
id integer PRIMARY KEY,
57+
float_field float,
58+
dec_field decimal(7,3)
59+
);
60+
GO
61+
5262
INSERT INTO qgis_test.[someData] (pk, cnt, name, name2, num_char, geom) VALUES
5363
(5, -200, NULL, 'NuLl', '5', geometry::STGeomFromText( 'Point(-71.123 78.23)', 4326 )),
5464
(3, 300, 'Pear', 'PEaR', '3', NULL),
@@ -69,6 +79,9 @@ GO
6979
INSERT INTO qgis_test.[date_times] (id, date_field, time_field, datetime_field ) VALUES
7080
(1, '2004-03-04', '13:41:52', '2004-03-04 13:41:52' );
7181

82+
INSERT INTO qgis_test.[float_dec] (id, float_field, dec_field ) VALUES
83+
(1, 1.1111111111, 1.123 );
84+
GO
7285

7386

7487

0 commit comments

Comments
 (0)
Please sign in to comment.