Skip to content

Commit

Permalink
General cleanup/cosmetics
Browse files Browse the repository at this point in the history
C# 6 and so on
  • Loading branch information
roji committed May 26, 2016
1 parent 043f767 commit 7689c80
Show file tree
Hide file tree
Showing 12 changed files with 1,551 additions and 1,910 deletions.
3 changes: 1 addition & 2 deletions src/EntityFramework5.Npgsql/EntityFramework5.Npgsql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<LangVersion>5</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -80,4 +79,4 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
513 changes: 214 additions & 299 deletions src/EntityFramework6.Npgsql/NpgsqlMigrationSqlGenerator.cs

Large diffs are not rendered by default.

508 changes: 240 additions & 268 deletions src/EntityFramework6.Npgsql/NpgsqlProviderManifest.cs

Large diffs are not rendered by default.

89 changes: 36 additions & 53 deletions src/EntityFramework6.Npgsql/NpgsqlServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using JetBrains.Annotations;
#if ENTITIES6
using System.Data.Entity.Core.Common;
using System.Data.Entity.Core.Common.CommandTrees;
Expand All @@ -39,6 +39,8 @@
using DbConnection = System.Data.Common.DbConnection;
using DbCommand = System.Data.Common.DbCommand;

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

namespace Npgsql
{
#if ENTITIES6
Expand All @@ -47,38 +49,33 @@ public class NpgsqlServices : DbProviderServices
internal class NpgsqlServices : DbProviderServices
#endif
{
private static readonly NpgsqlServices _instance = new NpgsqlServices();
public static NpgsqlServices Instance { get; } = new NpgsqlServices();

#if ENTITIES6
public NpgsqlServices()
{
AddDependencyResolver(new SingletonDependencyResolver<Func<MigrationSqlGenerator>>(
() => new NpgsqlMigrationSqlGenerator(), "Npgsql"));
() => new NpgsqlMigrationSqlGenerator(), nameof(Npgsql)));
}
#endif

public static NpgsqlServices Instance
{
get { return _instance; }
}

protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
{
return CreateCommandDefinition(CreateDbCommand(((NpgsqlProviderManifest)providerManifest).Version, commandTree));
}
protected override DbCommandDefinition CreateDbCommandDefinition([NotNull] DbProviderManifest providerManifest, [NotNull] DbCommandTree commandTree)
=> CreateCommandDefinition(CreateDbCommand(((NpgsqlProviderManifest)providerManifest).Version, commandTree));

internal DbCommand CreateDbCommand(Version serverVersion, DbCommandTree commandTree)
{
if (commandTree == null)
throw new ArgumentNullException("commandTree");
throw new ArgumentNullException(nameof(commandTree));

NpgsqlCommand command = new NpgsqlCommand();
var command = new NpgsqlCommand();

foreach (KeyValuePair<string, TypeUsage> parameter in commandTree.Parameters)
foreach (var parameter in commandTree.Parameters)
{
NpgsqlParameter dbParameter = new NpgsqlParameter();
dbParameter.ParameterName = parameter.Key;
dbParameter.NpgsqlDbType = NpgsqlProviderManifest.GetNpgsqlDbType(((PrimitiveType)parameter.Value.EdmType).PrimitiveTypeKind);
var dbParameter = new NpgsqlParameter
{
ParameterName = parameter.Key,
NpgsqlDbType = NpgsqlProviderManifest.GetNpgsqlDbType(((PrimitiveType)parameter.Value.EdmType).PrimitiveTypeKind)
};
command.Parameters.Add(dbParameter);
}

Expand All @@ -89,76 +86,66 @@ internal DbCommand CreateDbCommand(Version serverVersion, DbCommandTree commandT

internal void TranslateCommandTree(Version serverVersion, DbCommandTree commandTree, DbCommand command, bool createParametersForNonSelect = true)
{
SqlBaseGenerator sqlGenerator = null;
SqlBaseGenerator sqlGenerator;

DbQueryCommandTree select;
DbInsertCommandTree insert;
DbUpdateCommandTree update;
DbDeleteCommandTree delete;
if ((select = commandTree as DbQueryCommandTree) != null)
{
sqlGenerator = new SqlSelectGenerator(select);
}
else if ((insert = commandTree as DbInsertCommandTree) != null)
{
sqlGenerator = new SqlInsertGenerator(insert);
}
else if ((update = commandTree as DbUpdateCommandTree) != null)
{
sqlGenerator = new SqlUpdateGenerator(update);
}
else if ((delete = commandTree as DbDeleteCommandTree) != null)
{
sqlGenerator = new SqlDeleteGenerator(delete);
}
else
{
// TODO: get a message (unsupported DbCommandTree type)
throw new ArgumentException();
}
sqlGenerator._createParametersForConstants = select != null ? false : createParametersForNonSelect;
sqlGenerator._command = (NpgsqlCommand)command;
sqlGenerator.CreateParametersForConstants = select == null && createParametersForNonSelect;
sqlGenerator.Command = (NpgsqlCommand)command;
sqlGenerator.Version = serverVersion;

sqlGenerator.BuildCommand(command);
}

protected override string GetDbProviderManifestToken(DbConnection connection)
protected override string GetDbProviderManifestToken([NotNull] DbConnection connection)
{
if (connection == null)
throw new ArgumentNullException("connection");
string serverVersion = "";
UsingPostgresDBConnection((NpgsqlConnection)connection, conn =>
{
throw new ArgumentNullException(nameof(connection));

var serverVersion = "";
UsingPostgresDbConnection((NpgsqlConnection)connection, conn => {
serverVersion = conn.ServerVersion;
});
return serverVersion;
}

protected override DbProviderManifest GetDbProviderManifest(string versionHint)
protected override DbProviderManifest GetDbProviderManifest([NotNull] string versionHint)
{
if (versionHint == null)
throw new ArgumentNullException("versionHint");
throw new ArgumentNullException(nameof(versionHint));
return new NpgsqlProviderManifest(versionHint);
}

#if ENTITIES6
protected override bool DbDatabaseExists(DbConnection connection, int? commandTimeout, StoreItemCollection storeItemCollection)
protected override bool DbDatabaseExists([NotNull] DbConnection connection, int? commandTimeout, [NotNull] StoreItemCollection storeItemCollection)
{
bool exists = false;
UsingPostgresDBConnection((NpgsqlConnection)connection, conn =>
var exists = false;
UsingPostgresDbConnection((NpgsqlConnection)connection, conn =>
{
using (NpgsqlCommand command = new NpgsqlCommand("select count(*) from pg_catalog.pg_database where datname = '" + connection.Database + "';", conn))
{
using (var command = new NpgsqlCommand("select count(*) from pg_catalog.pg_database where datname = '" + connection.Database + "';", conn))
exists = Convert.ToInt32(command.ExecuteScalar()) > 0;
}
});
return exists;
}

protected override void DbCreateDatabase(DbConnection connection, int? commandTimeout, StoreItemCollection storeItemCollection)
protected override void DbCreateDatabase([NotNull] DbConnection connection, int? commandTimeout, [NotNull] StoreItemCollection storeItemCollection)
{
UsingPostgresDBConnection((NpgsqlConnection)connection, conn =>
UsingPostgresDbConnection((NpgsqlConnection)connection, conn =>
{
var sb = new StringBuilder();
sb.Append("CREATE DATABASE \"");
Expand All @@ -171,28 +158,24 @@ protected override void DbCreateDatabase(DbConnection connection, int? commandTi
sb.Append("\"");
}

using (NpgsqlCommand command = new NpgsqlCommand(sb.ToString(), conn))
{
using (var command = new NpgsqlCommand(sb.ToString(), conn))
command.ExecuteNonQuery();
}
});
}

protected override void DbDeleteDatabase(DbConnection connection, int? commandTimeout, StoreItemCollection storeItemCollection)
protected override void DbDeleteDatabase([NotNull] DbConnection connection, int? commandTimeout, [NotNull] StoreItemCollection storeItemCollection)
{
UsingPostgresDBConnection((NpgsqlConnection)connection, conn =>
UsingPostgresDbConnection((NpgsqlConnection)connection, conn =>
{
//Close all connections in pool or exception "database used by another user appears"
NpgsqlConnection.ClearAllPools();
using (NpgsqlCommand command = new NpgsqlCommand("DROP DATABASE \"" + connection.Database + "\";", conn))
{
using (var command = new NpgsqlCommand("DROP DATABASE \"" + connection.Database + "\";", conn))
command.ExecuteNonQuery();
}
});
}
#endif

private static void UsingPostgresDBConnection(NpgsqlConnection connection, Action<NpgsqlConnection> action)
static void UsingPostgresDbConnection(NpgsqlConnection connection, Action<NpgsqlConnection> action)
{
var connectionBuilder = new NpgsqlConnectionStringBuilder(connection.ConnectionString)
{
Expand Down
14 changes: 3 additions & 11 deletions src/EntityFramework6.Npgsql/SqlGenerators/PendingProjectsNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Npgsql.SqlGenerators
{
Expand All @@ -51,23 +48,18 @@ internal class PendingProjectsNode
{
public readonly List<NameAndInputExpression> Selects = new List<NameAndInputExpression>();
public PendingProjectsNode JoinParent { get; set; }
public string TopName
{
get
{
return Selects[0].AsName;
}
}
public string TopName => Selects[0].AsName;

public PendingProjectsNode(string asName, InputExpression exp)
{
Selects.Add(new NameAndInputExpression(asName, exp));
}

public void Add(string asName, InputExpression exp)
{
Selects.Add(new NameAndInputExpression(asName, exp));
}

public NameAndInputExpression Last { get { return Selects[Selects.Count - 1]; } }
public NameAndInputExpression Last => Selects[Selects.Count - 1];
}
}
Loading

0 comments on commit 7689c80

Please sign in to comment.