Skip to content

Commit a3cf2d4

Browse files
authored
Merge pull request #261 from servicetitan/upstream/covariantReturnTypes
Use C#9 Covariant return types feature for Clone() implementation for type safety and reduce number of casts
2 parents b9842e8 + c4d28de commit a3cf2d4

113 files changed

Lines changed: 482 additions & 637 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Orm/Xtensive.Orm.Tests.Sql/CloneTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void SetUp()
3737
public void SqlExpressionCloneTest()
3838
{
3939
SqlExpression e = SqlDml.Literal(1);
40-
SqlExpression eClone = (SqlExpression) e.Clone();
40+
SqlExpression eClone = e.Clone();
4141
Assert.AreNotEqual(e, eClone);
4242
Assert.AreEqual(e.NodeType, eClone.NodeType);
4343
}

Orm/Xtensive.Orm/Orm/Upgrade/Internals/CatalogCloner.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private void CloneSchemas(Catalog newCatalog, Catalog source, Dictionary<string,
8484
private void CloneAssertions(Schema newSchema, Schema sourceSchema)
8585
{
8686
foreach (var assertion in sourceSchema.Assertions) {
87-
var newAssertion = newSchema.CreateAssertion(assertion.Name, (SqlExpression)assertion.Condition.Clone(), assertion.IsDeferrable, assertion.IsInitiallyDeferred);
87+
var newAssertion = newSchema.CreateAssertion(assertion.Name, assertion.Condition.Clone(), assertion.IsDeferrable, assertion.IsInitiallyDeferred);
8888
CopyDbName(newAssertion, assertion);
8989
}
9090
}
@@ -113,9 +113,9 @@ private void CloneDomains(Schema newSchema, Schema sourceSchema, Dictionary<Coll
113113
if (sourceDomain.Collation!=null)
114114
newDomain.Collation = collationsMap[sourceDomain.Collation];
115115
if (sourceDomain.DefaultValue!=null)
116-
newDomain.DefaultValue = (SqlExpression)sourceDomain.DefaultValue.Clone();
116+
newDomain.DefaultValue = sourceDomain.DefaultValue.Clone();
117117
foreach (var domainConstraint in sourceDomain.DomainConstraints) {
118-
var newConstraint = newDomain.CreateConstraint(domainConstraint.Name, (SqlExpression) domainConstraint.Condition.Clone());
118+
var newConstraint = newDomain.CreateConstraint(domainConstraint.Name, domainConstraint.Condition.Clone());
119119
CopyDbName(newConstraint, domainConstraint);
120120
}
121121
}
@@ -181,7 +181,7 @@ private void CloneTableColumns(Table newTable, Table sourceTable, Dictionary<Col
181181
CopyDbName(newColumn, sourceTableColumn);
182182

183183
if (sourceTableColumn.DefaultValue!=null)
184-
newColumn.DefaultValue = (SqlExpression) sourceTableColumn.DefaultValue.Clone();
184+
newColumn.DefaultValue = sourceTableColumn.DefaultValue.Clone();
185185

186186
var schema = newTable.Schema;
187187
if (sourceTableColumn.Collation!=null) {
@@ -196,7 +196,7 @@ private void CloneTableColumns(Table newTable, Table sourceTable, Dictionary<Col
196196
if (sourceTableColumn.Domain!=null)
197197
newColumn.Domain = schema.Domains[sourceTableColumn.Domain.Name];
198198
if (sourceTableColumn.Expression!=null)
199-
newColumn.Expression = (SqlExpression) sourceTableColumn.Expression.Clone();
199+
newColumn.Expression = sourceTableColumn.Expression.Clone();
200200
newColumn.IsNullable = sourceTableColumn.IsNullable;
201201
newColumn.IsPersisted = sourceTableColumn.IsPersisted;
202202
if (sourceTableColumn.SequenceDescriptor!=null)
@@ -251,7 +251,7 @@ private void CloneIndex(DataTable newTable, Index sourceIndex)
251251
ft.IsUnique = ftIndex.IsUnique;
252252
ft.UnderlyingUniqueIndex = ftIndex.UnderlyingUniqueIndex;
253253
if (ftIndex.Where!=null)
254-
ft.Where = (SqlExpression) ftIndex.Where.Clone();
254+
ft.Where = ftIndex.Where.Clone();
255255
ClonePartitionDescriptor(ft, sourceIndex);
256256
return;
257257
}
@@ -269,7 +269,7 @@ private void CloneIndex(DataTable newTable, Index sourceIndex)
269269
spatial.IsClustered = spatialIndex.IsClustered;
270270
spatial.IsUnique = spatialIndex.IsUnique;
271271
if (spatialIndex.Where!=null)
272-
spatial.Where = (SqlExpression) spatialIndex.Where.Clone();
272+
spatial.Where = spatialIndex.Where.Clone();
273273
ClonePartitionDescriptor(spatialIndex, sourceIndex);
274274
return;
275275
}
@@ -283,7 +283,7 @@ private void CloneIndex(DataTable newTable, Index sourceIndex)
283283
index.IsUnique = sourceIndex.IsUnique;
284284
index.IsClustered = sourceIndex.IsClustered;
285285
if (sourceIndex.Where!=null)
286-
index.Where = (SqlExpression) sourceIndex.Where.Clone();
286+
index.Where = sourceIndex.Where.Clone();
287287
index.NonkeyColumns.AddRange(GetNonKeyColumns(newTable, sourceIndex));
288288
index.IsBitmap = sourceIndex.IsBitmap;
289289
ClonePartitionDescriptor(index, sourceIndex);
@@ -319,7 +319,7 @@ private void CloneTableConstraint(Table newTable, TableConstraint sourceConstrai
319319
{
320320
var checkConstraint = sourceConstraint as CheckConstraint;
321321
if (checkConstraint!=null) {
322-
var c = newTable.CreateCheckConstraint(checkConstraint.Name, (SqlExpression) checkConstraint.Condition.Clone());
322+
var c = newTable.CreateCheckConstraint(checkConstraint.Name, checkConstraint.Condition.Clone());
323323
CopyDbName(c, checkConstraint);
324324
return;
325325
}

Orm/Xtensive.Orm/Sql/Ddl/Actions/SqlAddColumn.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ public class SqlAddColumn : SqlAction
1212
{
1313
public TableColumn Column { get; private set; }
1414

15-
internal override object Clone(SqlNodeCloneContext context) =>
16-
context.NodeMapping.TryGetValue(this, out var clone)
17-
? clone
18-
: context.NodeMapping[this] = new SqlAddColumn(Column);
15+
internal override SqlAddColumn Clone(SqlNodeCloneContext context) =>
16+
context.GetOrAdd(this, static (t, c) =>
17+
new SqlAddColumn(t.Column));
1918

2019
// Constructors
2120

Orm/Xtensive.Orm/Sql/Ddl/Actions/SqlAddConstraint.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ public class SqlAddConstraint : SqlAction
1212
{
1313
public Constraint Constraint { get; private set; }
1414

15-
internal override object Clone(SqlNodeCloneContext context) =>
16-
context.NodeMapping.TryGetValue(this, out var clone)
17-
? clone
18-
: context.NodeMapping[this] = new SqlAddConstraint(Constraint);
15+
internal override SqlAddConstraint Clone(SqlNodeCloneContext context) =>
16+
context.GetOrAdd(this, static (t, c) =>
17+
new SqlAddConstraint(t.Constraint));
1918

2019
// Constructors
2120

Orm/Xtensive.Orm/Sql/Ddl/Actions/SqlAlterIdentityInfo.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ public class SqlAlterIdentityInfo : SqlAction
1414
public SequenceDescriptor SequenceDescriptor { get; set; }
1515
public SqlAlterIdentityInfoOptions InfoOption { get; set; }
1616

17-
internal override object Clone(SqlNodeCloneContext context) =>
18-
context.NodeMapping.TryGetValue(this, out var clone)
19-
? clone
20-
: context.NodeMapping[this] = new SqlAlterIdentityInfo(Column, (SequenceDescriptor) SequenceDescriptor.Clone(), InfoOption);
17+
internal override SqlAlterIdentityInfo Clone(SqlNodeCloneContext context) =>
18+
context.GetOrAdd(this, static (t, c) =>
19+
new SqlAlterIdentityInfo(t.Column, (SequenceDescriptor) t.SequenceDescriptor.Clone(), t.InfoOption));
2120

2221
internal SqlAlterIdentityInfo(TableColumn column, SequenceDescriptor sequenceDescriptor, SqlAlterIdentityInfoOptions infoOption)
2322
{

Orm/Xtensive.Orm/Sql/Ddl/Actions/SqlDropColumn.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ public class SqlDropColumn : SqlCascadableAction
1212
{
1313
public TableColumn Column { get; private set; }
1414

15-
internal override object Clone(SqlNodeCloneContext context) =>
16-
context.NodeMapping.TryGetValue(this, out var clone)
17-
? clone
18-
: context.NodeMapping[this] = new SqlDropColumn(Column);
15+
internal override SqlDropColumn Clone(SqlNodeCloneContext context) =>
16+
context.GetOrAdd(this, static (t, c) =>
17+
new SqlDropColumn(t.Column));
1918

2019
// Constructors
2120

Orm/Xtensive.Orm/Sql/Ddl/Actions/SqlDropConstraint.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ public class SqlDropConstraint : SqlCascadableAction
1212
{
1313
public Constraint Constraint { get; private set; }
1414

15-
internal override object Clone(SqlNodeCloneContext context) =>
16-
context.NodeMapping.TryGetValue(this, out var clone)
17-
? clone
18-
: context.NodeMapping[this] = new SqlDropConstraint(Constraint, Cascade);
15+
internal override SqlDropConstraint Clone(SqlNodeCloneContext context) =>
16+
context.GetOrAdd(this, static (t, c) =>
17+
new SqlDropConstraint(t.Constraint, t.Cascade));
1918

2019
// Constructors
2120

Orm/Xtensive.Orm/Sql/Ddl/Actions/SqlDropDefault.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ public class SqlDropDefault : SqlAction
1212
{
1313
public TableColumn Column { get; private set; }
1414

15-
internal override object Clone(SqlNodeCloneContext context) =>
16-
context.NodeMapping.TryGetValue(this, out var clone)
17-
? clone
18-
: context.NodeMapping[this] = new SqlDropDefault(Column);
15+
internal override SqlDropDefault Clone(SqlNodeCloneContext context) =>
16+
context.GetOrAdd(this, static (t, c) =>
17+
new SqlDropDefault(t.Column));
1918

2019
// Constructors
2120

Orm/Xtensive.Orm/Sql/Ddl/Actions/SqlRenameColumn.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ public class SqlRenameColumn : SqlAction
1515
public TableColumn Column { get; private set; }
1616
public string NewName { get; private set; }
1717

18-
internal override object Clone(SqlNodeCloneContext context) =>
19-
context.NodeMapping.TryGetValue(this, out var clone)
20-
? clone
21-
: context.NodeMapping[this] = new SqlRenameColumn(Column, NewName);
18+
internal override SqlRenameColumn Clone(SqlNodeCloneContext context) =>
19+
context.GetOrAdd(this, static (t, c) =>
20+
new SqlRenameColumn(t.Column, t.NewName));
2221

2322

2423
// Constructors

Orm/Xtensive.Orm/Sql/Ddl/Actions/SqlSetDefault.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ public class SqlSetDefault : SqlAction
1414
public TableColumn Column { get; private set; }
1515
public SqlExpression DefaultValue { get; private set; }
1616

17-
internal override object Clone(SqlNodeCloneContext context) =>
18-
context.NodeMapping.TryGetValue(this, out var clone)
19-
? clone
20-
: context.NodeMapping[this] = new SqlSetDefault((SqlExpression) DefaultValue.Clone(context), Column);
17+
internal override SqlSetDefault Clone(SqlNodeCloneContext context) =>
18+
context.GetOrAdd(this, static (t, c) =>
19+
new SqlSetDefault(t.DefaultValue.Clone(c), t.Column));
2120

2221
internal SqlSetDefault(SqlExpression defaultValue, TableColumn column)
2322
{

0 commit comments

Comments
 (0)