Skip to content
Permalink
Browse files

[mssql] Prevent overwriting existing tables via drag and drop in browser

Fixes #16805
  • Loading branch information
nyalldawson committed Oct 8, 2018
1 parent da21193 commit 745495fe733ae8694adf3e428156594085dd75c0
@@ -1819,6 +1819,26 @@ QgsVectorLayerExporter::ExportError QgsMssqlProvider::createEmptyLayer( const QS
return QgsVectorLayerExporter::ErrCreateLayer;
}
}
else
{
// test for existing
sql = QStringLiteral( "SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[%1].[%2]') AND type in (N'U')" )
.arg( schemaName, tableName );
if ( !q.exec( sql ) )
{
if ( errorMessage )
*errorMessage = q.lastError().text();
return QgsVectorLayerExporter::ErrCreateLayer;
}

// if we got a hit, abort!!
if ( q.next() )
{
if ( errorMessage )
*errorMessage = tr( "Table [%1].[%2] already exists" ).arg( schemaName, tableName );
return QgsVectorLayerExporter::ErrCreateLayer;
}
}

if ( !geometryColumn.isEmpty() )
{
@@ -298,6 +298,25 @@ def testInsertPolygonInMultiPolygon(self):
geom = [f.geometry().asWkt() for f in new_layer.getFeatures()]
self.assertEqual(geom, ['MultiPolygon (((0 0, 1 0, 1 1, 0 1, 0 0)),((10 0, 11 0, 11 1, 10 1, 10 0)))', 'MultiPolygon (((30 0, 31 0, 31 1, 30 1, 30 0)))'])

def testOverwriteExisting(self):
layer = QgsVectorLayer("NoGeometry?field=pk:integer", "addfeat", "memory")
pr = layer.dataProvider()
f = QgsFeature()
f.setAttributes([133])
pr.addFeatures([f])

uri = '{} table="qgis_test"."sacrificalLamb" sql='.format(self.dbconn)
new_layer = QgsVectorLayer(uri, 'new', 'mssql')
self.assertTrue(new_layer.isValid())
self.assertEqual([f.attributes() for f in new_layer.getFeatures()], [[1]])

# try to overwrite
error, message = QgsVectorLayerExporter.exportLayer(layer, uri, 'mssql', QgsCoordinateReferenceSystem())
self.assertEqual(error, QgsVectorLayerExporter.ErrCreateLayer)

# should not have overwritten features
self.assertEqual([f.attributes() for f in new_layer.getFeatures()], [[1]])

def testMultiGeomColumns(self):
uri = '{} table="qgis_test"."multiGeomColumns" (geom1) sql='.format(self.dbconn)
new_layer = QgsVectorLayer(uri, 'new', 'mssql')
@@ -25,6 +25,9 @@ GO
DROP TABLE IF EXISTS qgis_test.[multiGeomColumns];
GO

DROP TABLE IF EXISTS qgis_test.[sacrificalLamb];
GO

DROP SCHEMA qgis_test;
GO

@@ -69,6 +72,11 @@ CREATE TABLE qgis_test.[multiGeomColumns] (
);
GO

CREATE TABLE qgis_test.[sacrificalLamb] (
pk integer PRIMARY KEY
);
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),
@@ -93,6 +101,10 @@ INSERT INTO qgis_test.[float_dec] (id, float_field, dec_field ) VALUES
(1, 1.1111111111, 1.123 );
GO

INSERT INTO qgis_test.[sacrificalLamb] (pk) VALUES
(1);
GO

INSERT INTO qgis_test.[multiGeomColumns] (pk, geom1, geom2) VALUES
(5, geometry::STGeomFromText( 'Point( 1 2 )', 4326 ), NULL),
(3, NULL, geometry::STGeomFromText( 'LineString( 1 2, 3 4 )', 4326 )),

0 comments on commit 745495f

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