forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlVSPackage.cs
More file actions
95 lines (88 loc) · 4.83 KB
/
NpgsqlVSPackage.cs
File metadata and controls
95 lines (88 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//------------------------------------------------------------------------------
// <copyright file="NpgsqlVSPackage.cs" company="Company">
// Copyright (c) Company. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel.Design;
using System.Configuration;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
namespace Npgsql.VSIX
{
/// <summary>
/// This is the class that implements the package exposed by this assembly.
/// </summary>
/// <remarks>
/// <para>
/// The minimum requirement for a class to be considered a valid package for Visual Studio
/// is to implement the IVsPackage interface and register itself with the shell.
/// This package uses the helper classes defined inside the Managed Package Framework (MPF)
/// to do it: it derives from the Package class that provides the implementation of the
/// IVsPackage interface and uses the registration attributes defined in the framework to
/// register itself and its components with the shell. These attributes tell the pkgdef creation
/// utility what data to put into .pkgdef file.
/// </para>
/// <para>
/// To get loaded into VS, the package must be referred by <Asset Type="Microsoft.VisualStudio.VsPackage" ...> in .vsixmanifest file.
/// </para>
/// </remarks>
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideService(typeof(NpgsqlProviderObjectFactory), IsAsyncQueryable = true, ServiceName = "PostgreSQL Provider Object Factory")]
[ProvideBindingPath] // Necessary for loading Npgsql via DbProviderFactories.GetProvider()
[NpgsqlProviderRegistration]
[Guid(PackageGuidString)]
[ProvideAutoLoad(UIContextGuids80.DataSourceWindowAutoVisible, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(UIContextGuids80.DataSourceWindowSupported, PackageAutoLoadFlags.BackgroundLoad)]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")]
public sealed class NpgsqlVSPackage : AsyncPackage
{
/// <summary>
/// NpgsqlVSPackage GUID string.
/// </summary>
public const string PackageGuidString = "ef991dc4-3119-4ed6-bdb3-c160ca562560";
/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
/// </summary>
protected override Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
AddService(typeof(NpgsqlProviderObjectFactory), CreateService, true);
SetupNpgsqlProviderFactory();
return base.InitializeAsync(cancellationToken, progress);
}
Task<object> CreateService(IAsyncServiceContainer container, CancellationToken cancellationtoken, Type servicetype)
=> servicetype == typeof(NpgsqlProviderObjectFactory)
? Task.FromResult<object>(new NpgsqlProviderObjectFactory())
: throw new ArgumentException($"Can't create service of type '{servicetype.Name}'");
void SetupNpgsqlProviderFactory()
{
if (!(ConfigurationManager.GetSection("system.data") is DataSet systemData))
throw new Exception("No system.data section found in configuration manager!");
DataTable factoriesTable;
if (systemData.Tables.IndexOf("DbProviderFactories") == -1)
factoriesTable = systemData.Tables.Add("DbProviderFactories");
else
{
factoriesTable = systemData.Tables[systemData.Tables.IndexOf("DbProviderFactories")];
if (factoriesTable.Rows.Find(Constants.NpgsqlInvariantName) != null)
{
// There's already an entry for Npgsql in the machines.config.
// This should mean there's also a GAC-installed Npgsql - we don't need to do anything.
return;
}
}
// Add an entry for Npgsql
factoriesTable.Rows.Add("Npgsql Data Provider", ".NET Data Provider for PostgreSQL",
Constants.NpgsqlInvariantName, "Npgsql.NpgsqlFactory, Npgsql");
}
}
}