@@ -604,7 +604,10 @@ void QgsMssqlProvider::select( QgsAttributeList fetchAttributes,
604
604
605
605
if ( !mSqlWhereClause .isEmpty () )
606
606
{
607
- mStatement += " and (" + mSqlWhereClause + " )" ;
607
+ if ( rect.isEmpty () )
608
+ mStatement += " where (" + mSqlWhereClause + " )" ;
609
+ else
610
+ mStatement += " and (" + mSqlWhereClause + " )" ;
608
611
}
609
612
610
613
mFetchGeom = fetchGeometry;
@@ -626,6 +629,129 @@ void QgsMssqlProvider::select( QgsAttributeList fetchAttributes,
626
629
}
627
630
}
628
631
632
+ // Returns the minimum value of an attribute
633
+ QVariant QgsMssqlProvider::minimumValue ( int index )
634
+ {
635
+ // get the field name
636
+ QgsField fld = mAttributeFields [ index ];
637
+ QString sql = QString ( " select min([%1]) from " )
638
+ .arg ( fld.name () );
639
+
640
+ if ( !mSchemaName .isEmpty () )
641
+ sql += " [" + mSchemaName + " ]." ;
642
+
643
+ sql += " [" + mTableName + " ]" ;
644
+
645
+ if ( !mSqlWhereClause .isEmpty () )
646
+ {
647
+ sql += QString ( " where (%1)" ).arg ( mSqlWhereClause );
648
+ }
649
+
650
+ mQuery = QSqlQuery ( mDatabase );
651
+ mQuery .setForwardOnly ( true );
652
+
653
+ if ( !mQuery .exec ( sql ) )
654
+ {
655
+ QString msg = mQuery .lastError ().text ();
656
+ QgsDebugMsg ( msg );
657
+ }
658
+
659
+ if ( mQuery .isActive () )
660
+ {
661
+ if ( mQuery .next () )
662
+ {
663
+ return mQuery .value ( 0 );
664
+ }
665
+ }
666
+
667
+ return QVariant ( QString::null );
668
+ }
669
+
670
+ // Returns the maximum value of an attribute
671
+ QVariant QgsMssqlProvider::maximumValue ( int index )
672
+ {
673
+ // get the field name
674
+ QgsField fld = mAttributeFields [ index ];
675
+ QString sql = QString ( " select max([%1]) from " )
676
+ .arg ( fld.name () );
677
+
678
+ if ( !mSchemaName .isEmpty () )
679
+ sql += " [" + mSchemaName + " ]." ;
680
+
681
+ sql += " [" + mTableName + " ]" ;
682
+
683
+ if ( !mSqlWhereClause .isEmpty () )
684
+ {
685
+ sql += QString ( " where (%1)" ).arg ( mSqlWhereClause );
686
+ }
687
+
688
+ mQuery = QSqlQuery ( mDatabase );
689
+ mQuery .setForwardOnly ( true );
690
+
691
+ if ( !mQuery .exec ( sql ) )
692
+ {
693
+ QString msg = mQuery .lastError ().text ();
694
+ QgsDebugMsg ( msg );
695
+ }
696
+
697
+ if ( mQuery .isActive () )
698
+ {
699
+ if ( mQuery .next () )
700
+ {
701
+ return mQuery .value ( 0 );
702
+ }
703
+ }
704
+
705
+ return QVariant ( QString::null );
706
+ }
707
+
708
+ // Returns the list of unique values of an attribute
709
+ void QgsMssqlProvider::uniqueValues ( int index, QList<QVariant> &uniqueValues, int limit )
710
+ {
711
+ uniqueValues.clear ();
712
+
713
+ // get the field name
714
+ QgsField fld = mAttributeFields [ index ];
715
+ QString sql = QString ( " select distinct " );
716
+
717
+ if (limit > 0 )
718
+ {
719
+ sql += QString ( " top %1 " ).arg ( limit );
720
+ }
721
+
722
+ sql += QString ( " [%1] from " )
723
+ .arg ( fld.name () );
724
+
725
+ if ( !mSchemaName .isEmpty () )
726
+ sql += " [" + mSchemaName + " ]." ;
727
+
728
+ sql += " [" + mTableName + " ]" ;
729
+
730
+ if ( !mSqlWhereClause .isEmpty () )
731
+ {
732
+ sql += QString ( " where (%1)" ).arg ( mSqlWhereClause );
733
+ }
734
+
735
+ mQuery = QSqlQuery ( mDatabase );
736
+ mQuery .setForwardOnly ( true );
737
+
738
+ if ( !mQuery .exec ( sql ) )
739
+ {
740
+ QString msg = mQuery .lastError ().text ();
741
+ QgsDebugMsg ( msg );
742
+ }
743
+
744
+ if ( mQuery .isActive () )
745
+ {
746
+ // read all features
747
+ while ( mQuery .next () )
748
+ {
749
+ uniqueValues.append ( mQuery .value ( 0 ) );
750
+ }
751
+ }
752
+ }
753
+
754
+
629
755
// update the extent, feature count, wkb type and srid for this layer
630
756
void QgsMssqlProvider::UpdateStatistics ( bool estimate )
631
757
{
0 commit comments