Skip to content

Commit 4a9e8cc

Browse files
szekerestjef-n
authored andcommitted
MSSQL:Improve handling of datetime types (fixes #5283)
1 parent 985e453 commit 4a9e8cc

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

src/providers/mssql/qgsmssqlprovider.cpp

+62-8
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ QgsMssqlProvider::QgsMssqlProvider( QString uri )
6969
mValid = true;
7070

7171
mUseWkb = false;
72-
mSkipFailures = true;
72+
mSkipFailures = false;
7373

7474
mUseEstimatedMetadata = anUri.useEstimatedMetadata();
7575

@@ -274,18 +274,18 @@ QVariant::Type QgsMssqlProvider::DecodeSqlType( QString sqlTypeName )
274274
}
275275
else if ( sqlTypeName.startsWith( "date", Qt::CaseInsensitive ) )
276276
{
277-
type = QVariant::Date;
277+
type = QVariant::String;
278278
}
279279
else if ( sqlTypeName.startsWith( "datetime", Qt::CaseInsensitive ) ||
280280
sqlTypeName.startsWith( "smalldatetime", Qt::CaseInsensitive ) ||
281281
sqlTypeName.startsWith( "datetime2", Qt::CaseInsensitive ) )
282282
{
283-
type = QVariant::DateTime;
283+
type = QVariant::String;
284284
}
285285
else if ( sqlTypeName.startsWith( "time", Qt::CaseInsensitive ) ||
286286
sqlTypeName.startsWith( "timestamp", Qt::CaseInsensitive ) )
287287
{
288-
type = QVariant::Time;
288+
type = QVariant::String;
289289
}
290290
else
291291
{
@@ -755,6 +755,7 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList & flist )
755755

756756
statement += ") VALUES (" + values + ")";
757757

758+
// use prepared statement to prevent from sql injection
758759
if ( !mQuery.prepare( statement ) )
759760
{
760761
QString msg = mQuery.lastError().text();
@@ -775,8 +776,34 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList & flist )
775776
if ( fld.name().isEmpty() )
776777
continue; // invalid
777778

778-
// use prepared statement to prevent from sql injection
779-
mQuery.addBindValue( *it2 );
779+
QVariant::Type type = fld.type();
780+
if ( it2->isNull() || !it2->isValid() )
781+
{
782+
// binding null values
783+
if ( type == QVariant::Date || type == QVariant::DateTime )
784+
mQuery.addBindValue( QVariant( QVariant::String ) );
785+
else
786+
mQuery.addBindValue( QVariant( type ) );
787+
}
788+
else if ( type == QVariant::Int )
789+
{
790+
// binding an INTEGER value
791+
mQuery.addBindValue( it2->toInt() );
792+
}
793+
else if ( type == QVariant::Double )
794+
{
795+
// binding a DOUBLE value
796+
mQuery.addBindValue( it2->toDouble() );
797+
}
798+
else if ( type == QVariant::String )
799+
{
800+
// binding a TEXT value
801+
mQuery.addBindValue( it2->toString() );
802+
}
803+
else
804+
{
805+
mQuery.addBindValue( *it2 );
806+
}
780807
}
781808

782809
if ( !mGeometryColName.isEmpty() )
@@ -932,6 +959,7 @@ bool QgsMssqlProvider::changeAttributeValues( const QgsChangedAttributesMap & at
932959
// set attribute filter
933960
statement += QString( " WHERE [%1]=%2" ).arg( mFidColName, FID_TO_STRING( fid ) );
934961

962+
// use prepared statement to prevent from sql injection
935963
if ( !mQuery.prepare( statement ) )
936964
{
937965
QString msg = mQuery.lastError().text();
@@ -949,8 +977,34 @@ bool QgsMssqlProvider::changeAttributeValues( const QgsChangedAttributesMap & at
949977
if ( fld.name().isEmpty() )
950978
continue; // invalid
951979

952-
// use prepared statement to prevent from sql injection
953-
mQuery.addBindValue( *it2 );
980+
QVariant::Type type = fld.type();
981+
if ( it2->isNull() || !it2->isValid() )
982+
{
983+
// binding null values
984+
if ( type == QVariant::Date || type == QVariant::DateTime )
985+
mQuery.addBindValue( QVariant( QVariant::String ) );
986+
else
987+
mQuery.addBindValue( QVariant( type ) );
988+
}
989+
else if ( type == QVariant::Int )
990+
{
991+
// binding an INTEGER value
992+
mQuery.addBindValue( it2->toInt() );
993+
}
994+
else if ( type == QVariant::Double )
995+
{
996+
// binding a DOUBLE value
997+
mQuery.addBindValue( it2->toDouble() );
998+
}
999+
else if ( type == QVariant::String )
1000+
{
1001+
// binding a TEXT value
1002+
mQuery.addBindValue( it2->toString() );
1003+
}
1004+
else
1005+
{
1006+
mQuery.addBindValue( *it2 );
1007+
}
9541008
}
9551009

9561010
if ( !mQuery.exec() )

0 commit comments

Comments
 (0)