Skip to content

Commit b63d003

Browse files
committed
Code quality improvements
C# 7 etc.
1 parent 9a737da commit b63d003

6 files changed

Lines changed: 433 additions & 1219 deletions

File tree

src/EntityFramework6.Npgsql/NpgsqlMigrationSqlGenerator.cs

Lines changed: 58 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,42 @@ protected virtual void Convert([NotNull] IEnumerable<MigrationOperation> operati
6969
{
7070
foreach (var migrationOperation in operations)
7171
{
72-
if (migrationOperation is AddColumnOperation)
73-
Convert(migrationOperation as AddColumnOperation);
74-
else if (migrationOperation is AlterColumnOperation)
75-
Convert(migrationOperation as AlterColumnOperation);
76-
else if (migrationOperation is CreateTableOperation)
77-
Convert(migrationOperation as CreateTableOperation);
78-
else if (migrationOperation is DropForeignKeyOperation)
79-
Convert(migrationOperation as DropForeignKeyOperation);
80-
else if (migrationOperation is DropTableOperation)
81-
Convert(migrationOperation as DropTableOperation);
82-
else if (migrationOperation is MoveTableOperation)
83-
Convert(migrationOperation as MoveTableOperation);
84-
else if (migrationOperation is RenameTableOperation)
85-
Convert(migrationOperation as RenameTableOperation);
86-
else if (migrationOperation is AddForeignKeyOperation)
87-
Convert(migrationOperation as AddForeignKeyOperation);
88-
else if (migrationOperation is DropIndexOperation)
89-
Convert(migrationOperation as DropIndexOperation);
90-
else if (migrationOperation is SqlOperation)
91-
AddStatment((migrationOperation as SqlOperation).Sql, (migrationOperation as SqlOperation).SuppressTransaction);
92-
else if (migrationOperation is AddPrimaryKeyOperation)
93-
Convert(migrationOperation as AddPrimaryKeyOperation);
94-
else if (migrationOperation is CreateIndexOperation)
95-
Convert(migrationOperation as CreateIndexOperation);
96-
else if (migrationOperation is RenameIndexOperation)
97-
Convert(migrationOperation as RenameIndexOperation);
98-
else if (migrationOperation is DropColumnOperation)
99-
Convert(migrationOperation as DropColumnOperation);
100-
else if (migrationOperation is DropPrimaryKeyOperation)
101-
Convert(migrationOperation as DropPrimaryKeyOperation);
102-
else if (migrationOperation is HistoryOperation)
103-
Convert(migrationOperation as HistoryOperation);
104-
else if (migrationOperation is RenameColumnOperation)
105-
Convert(migrationOperation as RenameColumnOperation);
106-
else if (migrationOperation is UpdateDatabaseOperation)
107-
Convert((migrationOperation as UpdateDatabaseOperation).Migrations as IEnumerable<MigrationOperation>);
72+
if (migrationOperation is AddColumnOperation operation)
73+
Convert(operation);
74+
else if (migrationOperation is AlterColumnOperation columnOperation)
75+
Convert(columnOperation);
76+
else if (migrationOperation is CreateTableOperation tableOperation)
77+
Convert(tableOperation);
78+
else if (migrationOperation is DropForeignKeyOperation keyOperation)
79+
Convert(keyOperation);
80+
else if (migrationOperation is DropTableOperation dropTableOperation)
81+
Convert(dropTableOperation);
82+
else if (migrationOperation is MoveTableOperation moveTableOperation)
83+
Convert(moveTableOperation);
84+
else if (migrationOperation is RenameTableOperation renameTableOperation)
85+
Convert(renameTableOperation);
86+
else if (migrationOperation is AddForeignKeyOperation foreignKeyOperation)
87+
Convert(foreignKeyOperation);
88+
else if (migrationOperation is DropIndexOperation indexOperation)
89+
Convert(indexOperation);
90+
else if (migrationOperation is SqlOperation sqlOperation)
91+
AddStatment(sqlOperation.Sql, sqlOperation.SuppressTransaction);
92+
else if (migrationOperation is AddPrimaryKeyOperation primaryKeyOperation)
93+
Convert(primaryKeyOperation);
94+
else if (migrationOperation is CreateIndexOperation createIndexOperation)
95+
Convert(createIndexOperation);
96+
else if (migrationOperation is RenameIndexOperation renameIndexOperation)
97+
Convert(renameIndexOperation);
98+
else if (migrationOperation is DropColumnOperation dropColumnOperation)
99+
Convert(dropColumnOperation);
100+
else if (migrationOperation is DropPrimaryKeyOperation dropPrimaryKeyOperation)
101+
Convert(dropPrimaryKeyOperation);
102+
else if (migrationOperation is HistoryOperation historyOperation)
103+
Convert(historyOperation);
104+
else if (migrationOperation is RenameColumnOperation renameColumnOperation)
105+
Convert(renameColumnOperation);
106+
else if (migrationOperation is UpdateDatabaseOperation databaseOperation)
107+
Convert(databaseOperation.Migrations as IEnumerable<MigrationOperation>);
108108
else
109109
throw new NotImplementedException("Unhandled MigrationOperation " + migrationOperation.GetType().Name + " in " + GetType().Name);
110110
}
@@ -237,6 +237,7 @@ protected virtual void Convert(MoveTableOperation moveTableOperation)
237237
#endregion
238238

239239
#region Columns
240+
240241
protected virtual void Convert(AddColumnOperation addColumnOperation)
241242
{
242243
var sql = new StringBuilder();
@@ -507,15 +508,11 @@ protected virtual void Convert(DropPrimaryKeyOperation dropPrimaryKeyOperation)
507508
/// <returns>The quoted identifier.</returns>
508509
void AppendQuotedIdentifier(string identifier, StringBuilder builder)
509510
{
510-
if (String.IsNullOrEmpty(identifier))
511-
{
511+
if (string.IsNullOrEmpty(identifier))
512512
throw new ArgumentException("Value cannot be null or empty", nameof(identifier));
513-
}
514513

515514
if (identifier[identifier.Length - 1] == '"' && identifier[0] == '"')
516-
{
517515
builder.Append(identifier);
518-
}
519516
else
520517
{
521518
builder.Append('"');
@@ -531,19 +528,11 @@ void AppendQuotedIdentifier(string identifier, StringBuilder builder)
531528
/// <returns>The quoted identifier.</returns>
532529
string QuoteIdentifier(string identifier)
533530
{
534-
if (String.IsNullOrEmpty(identifier))
535-
{
531+
if (string.IsNullOrEmpty(identifier))
536532
throw new ArgumentException("Value cannot be null or empty", nameof(identifier));
537-
}
538533

539-
if (identifier[identifier.Length - 1] == '"' && identifier[0] == '"')
540-
{
541-
return identifier;
542-
}
543-
else
544-
{
545-
return '"' + identifier + '"';
546-
}
534+
return identifier[identifier.Length - 1] == '"' && identifier[0] == '"'
535+
? identifier : $"\"{identifier}\"";
547536
}
548537

549538
void AppendColumn(ColumnModel column, StringBuilder sql)
@@ -727,9 +716,7 @@ void AppendTableName(string tableName, StringBuilder sql)
727716
{
728717
var dotIndex = tableName.IndexOf('.');
729718
if (dotIndex == -1)
730-
{
731719
AppendQuotedIdentifier(tableName, sql);
732-
}
733720
else
734721
{
735722
AppendQuotedIdentifier(tableName.Remove(dotIndex), sql);
@@ -756,9 +743,7 @@ void AppendValue(byte[] values, StringBuilder sql)
756743
}
757744

758745
void AppendValue(bool value, StringBuilder sql)
759-
{
760-
sql.Append(value ? "TRUE" : "FALSE");
761-
}
746+
=> sql.Append(value ? "TRUE" : "FALSE");
762747

763748
void AppendValue(DateTime value, StringBuilder sql)
764749
{
@@ -804,22 +789,22 @@ void AppendValue(DbGeometry value, StringBuilder sql)
804789

805790
void AppendValue(object value, StringBuilder sql)
806791
{
807-
if (value is byte[])
808-
AppendValue((byte[])value, sql);
809-
else if (value is bool)
810-
AppendValue((bool)value, sql);
811-
else if (value is DateTime)
812-
AppendValue((DateTime)value, sql);
813-
else if (value is DateTimeOffset)
814-
AppendValue((DateTimeOffset)value, sql);
815-
else if (value is Guid)
816-
AppendValue((Guid)value, sql);
817-
else if (value is string)
818-
AppendValue((string)value, sql);
819-
else if (value is TimeSpan)
820-
AppendValue((TimeSpan)value, sql);
821-
else if (value is DbGeometry)
822-
AppendValue((DbGeometry)value, sql);
792+
if (value is byte[] bytes)
793+
AppendValue(bytes, sql);
794+
else if (value is bool b)
795+
AppendValue(b, sql);
796+
else if (value is DateTime time)
797+
AppendValue(time, sql);
798+
else if (value is DateTimeOffset offset)
799+
AppendValue(offset, sql);
800+
else if (value is Guid guid)
801+
AppendValue(guid, sql);
802+
else if (value is string s)
803+
AppendValue(s, sql);
804+
else if (value is TimeSpan timeSpan)
805+
AppendValue(timeSpan, sql);
806+
else if (value is DbGeometry geometry)
807+
AppendValue(geometry, sql);
823808
else
824809
sql.Append(string.Format(CultureInfo.InvariantCulture, "{0}", value));
825810
}

src/EntityFramework6.Npgsql/NpgsqlProviderManifest.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323

2424
using System;
2525
using System.Collections.Generic;
26-
using System.Text;
2726
using System.Data.Entity;
2827
using System.Data.Entity.Core.Common;
2928
using System.Data.Entity.Core.Metadata.Edm;
3029
using System.Collections.ObjectModel;
3130
using System.Linq;
3231
using System.Reflection;
3332
using System.Xml;
34-
using System.Data;
3533
using JetBrains.Annotations;
3634
using NpgsqlTypes;
3735

@@ -127,16 +125,14 @@ public override TypeUsage GetEdmType([NotNull] TypeUsage storeType)
127125
return TypeUsage.CreateDefaultTypeUsage(primitiveType);
128126
case "numeric":
129127
{
130-
byte scale;
131-
byte precision;
132128
if (storeType.Facets.TryGetValue(ScaleFacet, false, out facet) &&
133129
!facet.IsUnbounded && facet.Value != null)
134130
{
135-
scale = (byte)facet.Value;
131+
var scale = (byte)facet.Value;
136132
if (storeType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
137133
!facet.IsUnbounded && facet.Value != null)
138134
{
139-
precision = (byte)facet.Value;
135+
var precision = (byte)facet.Value;
140136
return TypeUsage.CreateDecimalTypeUsage(primitiveType, precision, scale);
141137
}
142138
}
@@ -242,16 +238,14 @@ public override TypeUsage GetStoreType([NotNull] TypeUsage edmType)
242238
return TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float8"]);
243239
case PrimitiveTypeKind.Decimal:
244240
{
245-
byte scale;
246-
byte precision;
247241
if (edmType.Facets.TryGetValue(ScaleFacet, false, out facet) &&
248242
!facet.IsUnbounded && facet.Value != null)
249243
{
250-
scale = (byte)facet.Value;
244+
var scale = (byte)facet.Value;
251245
if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
252246
!facet.IsUnbounded && facet.Value != null)
253247
{
254-
precision = (byte)facet.Value;
248+
var precision = (byte)facet.Value;
255249
return TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["numeric"], precision, scale);
256250
}
257251
}

0 commit comments

Comments
 (0)