Skip to content
Permalink
Browse files

[mssql] Fix incorrect precision detection for double/float fields

Fixes #15124
  • Loading branch information
nyalldawson committed Oct 8, 2018
1 parent f6b8020 commit 71c5051356d66a0acbde84f40679ab4ece4bf6bd
@@ -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 )
{
@@ -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")
@@ -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

@@ -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),
@@ -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



0 comments on commit 71c5051

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