2222#endregion
2323
2424using System ;
25- using System . Collections . Generic ;
2625using System . Text ;
26+ using JetBrains . Annotations ;
2727#if ENTITIES6
2828using System . Data . Entity . Core . Common ;
2929using System . Data . Entity . Core . Common . CommandTrees ;
3939using DbConnection = System . Data . Common . DbConnection ;
4040using DbCommand = System . Data . Common . DbCommand ;
4141
42+ #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
43+
4244namespace Npgsql
4345{
4446#if ENTITIES6
@@ -47,38 +49,33 @@ public class NpgsqlServices : DbProviderServices
4749 internal class NpgsqlServices : DbProviderServices
4850#endif
4951 {
50- private static readonly NpgsqlServices _instance = new NpgsqlServices ( ) ;
52+ public static NpgsqlServices Instance { get ; } = new NpgsqlServices ( ) ;
5153
5254#if ENTITIES6
5355 public NpgsqlServices ( )
5456 {
5557 AddDependencyResolver ( new SingletonDependencyResolver < Func < MigrationSqlGenerator > > (
56- ( ) => new NpgsqlMigrationSqlGenerator ( ) , " Npgsql" ) ) ;
58+ ( ) => new NpgsqlMigrationSqlGenerator ( ) , nameof ( Npgsql ) ) ) ;
5759 }
5860#endif
5961
60- public static NpgsqlServices Instance
61- {
62- get { return _instance ; }
63- }
64-
65- protected override DbCommandDefinition CreateDbCommandDefinition ( DbProviderManifest providerManifest , DbCommandTree commandTree )
66- {
67- return CreateCommandDefinition ( CreateDbCommand ( ( ( NpgsqlProviderManifest ) providerManifest ) . Version , commandTree ) ) ;
68- }
62+ protected override DbCommandDefinition CreateDbCommandDefinition ( [ NotNull ] DbProviderManifest providerManifest , [ NotNull ] DbCommandTree commandTree )
63+ => CreateCommandDefinition ( CreateDbCommand ( ( ( NpgsqlProviderManifest ) providerManifest ) . Version , commandTree ) ) ;
6964
7065 internal DbCommand CreateDbCommand ( Version serverVersion , DbCommandTree commandTree )
7166 {
7267 if ( commandTree == null )
73- throw new ArgumentNullException ( " commandTree" ) ;
68+ throw new ArgumentNullException ( nameof ( commandTree ) ) ;
7469
75- NpgsqlCommand command = new NpgsqlCommand ( ) ;
70+ var command = new NpgsqlCommand ( ) ;
7671
77- foreach ( KeyValuePair < string , TypeUsage > parameter in commandTree . Parameters )
72+ foreach ( var parameter in commandTree . Parameters )
7873 {
79- NpgsqlParameter dbParameter = new NpgsqlParameter ( ) ;
80- dbParameter . ParameterName = parameter . Key ;
81- dbParameter . NpgsqlDbType = NpgsqlProviderManifest . GetNpgsqlDbType ( ( ( PrimitiveType ) parameter . Value . EdmType ) . PrimitiveTypeKind ) ;
74+ var dbParameter = new NpgsqlParameter
75+ {
76+ ParameterName = parameter . Key ,
77+ NpgsqlDbType = NpgsqlProviderManifest . GetNpgsqlDbType ( ( ( PrimitiveType ) parameter . Value . EdmType ) . PrimitiveTypeKind )
78+ } ;
8279 command . Parameters . Add ( dbParameter ) ;
8380 }
8481
@@ -89,76 +86,66 @@ internal DbCommand CreateDbCommand(Version serverVersion, DbCommandTree commandT
8986
9087 internal void TranslateCommandTree ( Version serverVersion , DbCommandTree commandTree , DbCommand command , bool createParametersForNonSelect = true )
9188 {
92- SqlBaseGenerator sqlGenerator = null ;
89+ SqlBaseGenerator sqlGenerator ;
9390
9491 DbQueryCommandTree select ;
9592 DbInsertCommandTree insert ;
9693 DbUpdateCommandTree update ;
9794 DbDeleteCommandTree delete ;
9895 if ( ( select = commandTree as DbQueryCommandTree ) != null )
99- {
10096 sqlGenerator = new SqlSelectGenerator ( select ) ;
101- }
10297 else if ( ( insert = commandTree as DbInsertCommandTree ) != null )
103- {
10498 sqlGenerator = new SqlInsertGenerator ( insert ) ;
105- }
10699 else if ( ( update = commandTree as DbUpdateCommandTree ) != null )
107- {
108100 sqlGenerator = new SqlUpdateGenerator ( update ) ;
109- }
110101 else if ( ( delete = commandTree as DbDeleteCommandTree ) != null )
111- {
112102 sqlGenerator = new SqlDeleteGenerator ( delete ) ;
113- }
114103 else
115104 {
116105 // TODO: get a message (unsupported DbCommandTree type)
117106 throw new ArgumentException ( ) ;
118107 }
119- sqlGenerator . _createParametersForConstants = select != null ? false : createParametersForNonSelect ;
120- sqlGenerator . _command = ( NpgsqlCommand ) command ;
108+ sqlGenerator . CreateParametersForConstants = select == null && createParametersForNonSelect ;
109+ sqlGenerator . Command = ( NpgsqlCommand ) command ;
121110 sqlGenerator . Version = serverVersion ;
122111
123112 sqlGenerator . BuildCommand ( command ) ;
124113 }
125114
126- protected override string GetDbProviderManifestToken ( DbConnection connection )
115+ protected override string GetDbProviderManifestToken ( [ NotNull ] DbConnection connection )
127116 {
128117 if ( connection == null )
129- throw new ArgumentNullException ( " connection" ) ;
130- string serverVersion = "" ;
131- UsingPostgresDBConnection ( ( NpgsqlConnection ) connection , conn =>
132- {
118+ throw new ArgumentNullException ( nameof ( connection ) ) ;
119+
120+ var serverVersion = "" ;
121+ UsingPostgresDbConnection ( ( NpgsqlConnection ) connection , conn => {
133122 serverVersion = conn . ServerVersion ;
134123 } ) ;
135124 return serverVersion ;
136125 }
137126
138- protected override DbProviderManifest GetDbProviderManifest ( string versionHint )
127+ protected override DbProviderManifest GetDbProviderManifest ( [ NotNull ] string versionHint )
139128 {
140129 if ( versionHint == null )
141- throw new ArgumentNullException ( " versionHint" ) ;
130+ throw new ArgumentNullException ( nameof ( versionHint ) ) ;
142131 return new NpgsqlProviderManifest ( versionHint ) ;
143132 }
144133
145134#if ENTITIES6
146- protected override bool DbDatabaseExists ( DbConnection connection , int ? commandTimeout , StoreItemCollection storeItemCollection )
135+ protected override bool DbDatabaseExists ( [ NotNull ] DbConnection connection , int ? commandTimeout , [ NotNull ] StoreItemCollection storeItemCollection )
147136 {
148- bool exists = false ;
149- UsingPostgresDBConnection ( ( NpgsqlConnection ) connection , conn =>
137+ var exists = false ;
138+ UsingPostgresDbConnection ( ( NpgsqlConnection ) connection , conn =>
150139 {
151- using ( NpgsqlCommand command = new NpgsqlCommand ( "select count(*) from pg_catalog.pg_database where datname = '" + connection . Database + "';" , conn ) )
152- {
140+ using ( var command = new NpgsqlCommand ( "select count(*) from pg_catalog.pg_database where datname = '" + connection . Database + "';" , conn ) )
153141 exists = Convert . ToInt32 ( command . ExecuteScalar ( ) ) > 0 ;
154- }
155142 } ) ;
156143 return exists ;
157144 }
158145
159- protected override void DbCreateDatabase ( DbConnection connection , int ? commandTimeout , StoreItemCollection storeItemCollection )
146+ protected override void DbCreateDatabase ( [ NotNull ] DbConnection connection , int ? commandTimeout , [ NotNull ] StoreItemCollection storeItemCollection )
160147 {
161- UsingPostgresDBConnection ( ( NpgsqlConnection ) connection , conn =>
148+ UsingPostgresDbConnection ( ( NpgsqlConnection ) connection , conn =>
162149 {
163150 var sb = new StringBuilder ( ) ;
164151 sb . Append ( "CREATE DATABASE \" " ) ;
@@ -171,28 +158,24 @@ protected override void DbCreateDatabase(DbConnection connection, int? commandTi
171158 sb . Append ( "\" " ) ;
172159 }
173160
174- using ( NpgsqlCommand command = new NpgsqlCommand ( sb . ToString ( ) , conn ) )
175- {
161+ using ( var command = new NpgsqlCommand ( sb . ToString ( ) , conn ) )
176162 command . ExecuteNonQuery ( ) ;
177- }
178163 } ) ;
179164 }
180165
181- protected override void DbDeleteDatabase ( DbConnection connection , int ? commandTimeout , StoreItemCollection storeItemCollection )
166+ protected override void DbDeleteDatabase ( [ NotNull ] DbConnection connection , int ? commandTimeout , [ NotNull ] StoreItemCollection storeItemCollection )
182167 {
183- UsingPostgresDBConnection ( ( NpgsqlConnection ) connection , conn =>
168+ UsingPostgresDbConnection ( ( NpgsqlConnection ) connection , conn =>
184169 {
185170 //Close all connections in pool or exception "database used by another user appears"
186171 NpgsqlConnection . ClearAllPools ( ) ;
187- using ( NpgsqlCommand command = new NpgsqlCommand ( "DROP DATABASE \" " + connection . Database + "\" ;" , conn ) )
188- {
172+ using ( var command = new NpgsqlCommand ( "DROP DATABASE \" " + connection . Database + "\" ;" , conn ) )
189173 command . ExecuteNonQuery ( ) ;
190- }
191174 } ) ;
192175 }
193176#endif
194177
195- private static void UsingPostgresDBConnection ( NpgsqlConnection connection , Action < NpgsqlConnection > action )
178+ static void UsingPostgresDbConnection ( NpgsqlConnection connection , Action < NpgsqlConnection > action )
196179 {
197180 var connectionBuilder = new NpgsqlConnectionStringBuilder ( connection . ConnectionString )
198181 {
0 commit comments