Skip to content

Commit

Permalink
[mssql] Fix nvarchar length #58809
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Oct 4, 2024
1 parent ff1abe7 commit b2a3278
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/providers/mssql/qgsmssqlprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ bool QgsMssqlProvider::addAttributes( const QList<QgsField> &attributes )
for ( QList<QgsField>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
{
QString type = it->typeName();
if ( type == QLatin1String( "char" ) || type == QLatin1String( "varchar" ) )
if ( type == QLatin1String( "char" ) || type == QLatin1String( "varchar" ) || type == QLatin1String( "nvarchar" ) )
{
if ( it->length() > 0 )
type = QStringLiteral( "%1(%2)" ).arg( type ).arg( it->length() );
Expand Down
17 changes: 17 additions & 0 deletions tests/src/python/test_provider_mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,23 @@ def test_insert_pk_escaping(self):
features = list(vl.getFeatures())
self.assertEqual([f['test-field'] for f in features], [1])

def test_nvarchar_length(self):
"""
Test that nvarchar length is correctly set
"""
md = QgsProviderRegistry.instance().providerMetadata('mssql')
conn = md.createConnection(self.dbconn, {})

conn.execSql('DROP TABLE IF EXISTS qgis_test.test_nvarchar_length')
conn.execSql('CREATE TABLE qgis_test.test_nvarchar_length (id integer PRIMARY KEY)')

uri = f'{self.dbconn} table="qgis_test"."test_nvarchar_length" sql='
vl = QgsVectorLayer(uri, '', 'mssql')

self.assertTrue(vl.isValid())
self.assertTrue(vl.dataProvider().addAttributes([QgsField('name', QMetaType.Type.QString, 'nvarchar', 12)]))
self.assertEqual(vl.dataProvider().fields().at(1).length(), 12)


if __name__ == '__main__':
unittest.main()

0 comments on commit b2a3278

Please sign in to comment.