Each data provider has its URI and its provider key. Once added via User Interface, it's easy to collect them and use them from the console.
Enter this statement in your python console, to collect the URI and key for all map layers:
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
uri = layer.dataProvider().dataSourceUri()
providerKey = layer.dataProvider().name()
print("URI:\n%s\nKey:\n%s\n" % (uri, providerKey))
to create a new map layer based on this information:
Vector Layer
vl = QgsVectorLayer( uri, 'MyLayerName', providerKey )
QgsMapLayerRegistry.instance().addMapLayers( [vl] )
Raster Layer
rl = QgsRasterLayer( uri, 'MyLayerName', providerKey )
QgsMapLayerRegistry.instance().addMapLayers( [rl] )
The key for MS SQL is 'mssql'
The uri follow the QgsDataSourceURI standard, which has an example in the PyQGIS cookbook chapter Loading Layers, Vector Layers. MS SQL works analogue to postgres.
The above example duplicates just one layer, but prints the necessary information to add each of them and gives an idea about the principle.
You are able to access any plugin data by using the qgis.utils.plugins
from qgis import utils
mypluginInstance = utils.plugins['myplugin']
print(mypluginInstance.myData)
Best Answer
Renaud, there are a couple of ways to do this:
Solution #1 is pretty straightforward. OpenLayers plugin offers a good solution to #2, which will help you understand working with other plugins as well. Here is how both are accomplished.
Trigger OpenLayers plugin menu action
Work directly with OpenLayers plugin
This latter solution probably needs a little more explaining.
Get the OpenLayers plugin instance from qgis.utils's plugin registry.
Get the 'Google Physical' layer type object from OpenLayers's layer type registry. Open [path-to-user-plugins]/openlayers/openlayers_plugin.py and starting at line #111 you will see the order the layer types are assigned to the registry. They are given IDs starting with 0 (see rest of module for how that's done).
Loads the layer into QGIS's map canvas.
There may be other solutions as well, but those are the basics that I could find.