@@ -321,14 +321,11 @@ void QgsMssqlProvider::loadMetadata()
321
321
QString msg = query.lastError ().text ();
322
322
QgsDebugMsg ( msg );
323
323
}
324
- if ( query.isActive () )
324
+ if ( query.isActive () && query. next () )
325
325
{
326
- if ( query.next () )
327
- {
328
- mGeometryColName = query.value ( 0 ).toString ();
329
- mSRId = query.value ( 2 ).toInt ();
330
- mWkbType = getWkbType ( query.value ( 3 ).toString (), query.value ( 1 ).toInt () );
331
- }
326
+ mGeometryColName = query.value ( 0 ).toString ();
327
+ mSRId = query.value ( 2 ).toInt ();
328
+ mWkbType = getWkbType ( query.value ( 3 ).toString (), query.value ( 1 ).toInt () );
332
329
}
333
330
}
334
331
@@ -400,13 +397,10 @@ void QgsMssqlProvider::loadFields()
400
397
QString msg = query.lastError ().text ();
401
398
QgsDebugMsg ( msg );
402
399
}
403
- if ( query.isActive () )
400
+ if ( query.isActive () && query. next () )
404
401
{
405
- if ( query.next () )
406
- {
407
- mFidColName = query.value ( 3 ).toString ();
408
- return ;
409
- }
402
+ mFidColName = query.value ( 3 ).toString ();
403
+ return ;
410
404
}
411
405
foreach ( QString pk, pkCandidates )
412
406
{
@@ -420,16 +414,10 @@ void QgsMssqlProvider::loadFields()
420
414
QString msg = query.lastError ().text ();
421
415
QgsDebugMsg ( msg );
422
416
}
423
- if ( query.isActive () )
417
+ if ( query.isActive () && query. next () && query. value ( 0 ). toInt () == query. value ( 1 ). toInt () )
424
418
{
425
- if ( query.next () )
426
- {
427
- if ( query.value ( 0 ).toInt () == query.value ( 1 ).toInt () )
428
- {
429
- mFidColName = pk;
430
- return ;
431
- }
432
- }
419
+ mFidColName = pk;
420
+ return ;
433
421
}
434
422
}
435
423
}
@@ -476,10 +464,9 @@ QVariant QgsMssqlProvider::minimumValue( int index )
476
464
QgsDebugMsg ( msg );
477
465
}
478
466
479
- if ( query.isActive () )
467
+ if ( query.isActive () && query. next () )
480
468
{
481
- if ( query.next () )
482
- return query.value ( 0 );
469
+ return query.value ( 0 );
483
470
}
484
471
485
472
return QVariant ( QString::null );
@@ -512,10 +499,9 @@ QVariant QgsMssqlProvider::maximumValue( int index )
512
499
QgsDebugMsg ( msg );
513
500
}
514
501
515
- if ( query.isActive () )
502
+ if ( query.isActive () && query. next () )
516
503
{
517
- if ( query.next () )
518
- return query.value ( 0 );
504
+ return query.value ( 0 );
519
505
}
520
506
521
507
return QVariant ( QString::null );
@@ -582,23 +568,22 @@ void QgsMssqlProvider::UpdateStatistics( bool estimate )
582
568
QString sql = " SELECT min(bounding_box_xmin), min(bounding_box_ymin), max(bounding_box_xmax), max(bounding_box_ymax)"
583
569
" FROM sys.spatial_index_tessellations WHERE object_id = OBJECT_ID('[%1].[%2]')" ;
584
570
585
- statement = QString (sql).arg ( mSchemaName )
586
- .arg ( mTableName );
571
+ statement = QString ( sql ).arg ( mSchemaName ).arg ( mTableName );
587
572
588
573
if ( query.exec ( statement ) )
589
574
{
590
- QgsDebugMsg (" Found extents in spatial index" );
575
+ QgsDebugMsg ( " Found extents in spatial index" );
591
576
if ( query.next () )
592
577
{
593
- mExtent .setXMinimum ( query.value ( 0 ).toDouble () );
594
- mExtent .setYMinimum ( query.value ( 1 ).toDouble () );
595
- mExtent .setXMaximum ( query.value ( 2 ).toDouble () );
596
- mExtent .setYMaximum ( query.value ( 3 ).toDouble () );
597
- return ;
578
+ mExtent .setXMinimum ( query.value ( 0 ).toDouble () );
579
+ mExtent .setYMinimum ( query.value ( 1 ).toDouble () );
580
+ mExtent .setXMaximum ( query.value ( 2 ).toDouble () );
581
+ mExtent .setYMaximum ( query.value ( 3 ).toDouble () );
582
+ return ;
598
583
}
599
584
}
600
585
601
- QgsDebugMsg (query.lastError ().text ());
586
+ QgsDebugMsg ( query.lastError ().text () );
602
587
603
588
// If we can't find the extents in the spatial index table just do what we normally do.
604
589
bool readAllGeography = false ;
@@ -638,20 +623,17 @@ void QgsMssqlProvider::UpdateStatistics( bool estimate )
638
623
639
624
if ( !query.isActive () )
640
625
{
641
- return ;
626
+ return ;
642
627
}
643
628
644
629
QgsGeometry geom;
645
- if ( !readAllGeography )
630
+ if ( !readAllGeography && query. next () )
646
631
{
647
- if ( query.next () )
648
- {
649
- mExtent .setXMinimum ( query.value ( 0 ).toDouble () );
650
- mExtent .setYMinimum ( query.value ( 1 ).toDouble () );
651
- mExtent .setXMaximum ( query.value ( 2 ).toDouble () );
652
- mExtent .setYMaximum ( query.value ( 3 ).toDouble () );
653
- return ;
654
- }
632
+ mExtent .setXMinimum ( query.value ( 0 ).toDouble () );
633
+ mExtent .setYMinimum ( query.value ( 1 ).toDouble () );
634
+ mExtent .setXMaximum ( query.value ( 2 ).toDouble () );
635
+ mExtent .setYMaximum ( query.value ( 3 ).toDouble () );
636
+ return ;
655
637
}
656
638
657
639
// We have to read all the geometry if readAllGeography is true.
@@ -714,15 +696,11 @@ long QgsMssqlProvider::featureCount() const
714
696
" JOIN sys.partitions p ON t.object_id = p.object_id AND p.index_id IN (0,1)"
715
697
" WHERE SCHEMA_NAME(t.schema_id) = '%1' AND OBJECT_NAME(t.OBJECT_ID) = '%2'" ;
716
698
717
- QString statement = QString (sql).arg ( mSchemaName )
718
- .arg ( mTableName );
699
+ QString statement = QString ( sql ).arg ( mSchemaName ).arg ( mTableName );
719
700
720
- if ( query.exec ( statement ) )
701
+ if ( query.exec ( statement ) && query. next () )
721
702
{
722
- if ( query.next () )
723
- {
724
- return query.value (0 ).toInt ();
725
- }
703
+ return query.value ( 0 ).toInt ();
726
704
}
727
705
else
728
706
{
@@ -1311,23 +1289,15 @@ QgsCoordinateReferenceSystem QgsMssqlProvider::crs()
1311
1289
query.exec ( QString ( " select srtext from spatial_ref_sys where srid = %1" ).arg ( QString::number ( mSRId ) ) );
1312
1290
if ( query.isActive () )
1313
1291
{
1314
- if ( query.next () )
1315
- {
1316
- if ( mCrs .createFromWkt ( query.value ( 0 ).toString () ) )
1317
- return mCrs ;
1318
- }
1292
+ if ( query.next () && mCrs .createFromWkt ( query.value ( 0 ).toString () ) )
1293
+ return mCrs ;
1294
+
1319
1295
query.finish ();
1320
1296
}
1321
1297
query.clear ();
1322
1298
query.exec ( QString ( " select well_known_text from sys.spatial_reference_systems where spatial_reference_id = %1" ).arg ( QString::number ( mSRId ) ) );
1323
- if ( query.isActive () )
1324
- {
1325
- if ( query.next () )
1326
- {
1327
- if ( mCrs .createFromWkt ( query.value ( 0 ).toString () ) )
1328
- return mCrs ;
1329
- }
1330
- }
1299
+ if ( query.isActive () && query.next () && mCrs .createFromWkt ( query.value ( 0 ).toString () ) )
1300
+ return mCrs ;
1331
1301
}
1332
1302
return mCrs ;
1333
1303
}
@@ -1369,11 +1339,8 @@ bool QgsMssqlProvider::setSubsetString( QString theSQL, bool )
1369
1339
return false ;
1370
1340
}
1371
1341
1372
- if ( query.isActive () )
1373
- {
1374
- if ( query.next () )
1375
- mNumberFeatures = query.value ( 0 ).toInt ();
1376
- }
1342
+ if ( query.isActive () && query.next () )
1343
+ mNumberFeatures = query.value ( 0 ).toInt ();
1377
1344
1378
1345
QgsDataSourceURI anUri = QgsDataSourceURI ( dataSourceUri () );
1379
1346
anUri.setSql ( mSqlWhereClause );
0 commit comments