Skip to content

Commit 5c8d80b

Browse files
authored
Refactor parameter processing to reduce duplication etc. (#4333)
1 parent ff22adc commit 5c8d80b

File tree

2 files changed

+31
-71
lines changed

2 files changed

+31
-71
lines changed

src/Npgsql/NpgsqlCommand.cs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -633,15 +633,7 @@ Task Prepare(bool async, CancellationToken cancellationToken = default)
633633
{
634634
foreach (var batchCommand in InternalBatchCommands)
635635
{
636-
batchCommand.Parameters.HasOutputParameters = false;
637-
batchCommand.Parameters.PlaceholderType = PlaceholderType.NoParameters;
638-
639-
foreach (var p in batchCommand.Parameters.InternalList)
640-
{
641-
batchCommand.Parameters.CalculatePlaceholderType(p);
642-
p.Bind(connector.TypeMapper);
643-
}
644-
636+
batchCommand.Parameters.ProcessParameters(connector.TypeMapper, validate: false);
645637
ProcessRawQuery(connector.SqlQueryParser, connector.UseConformingStrings, batchCommand);
646638

647639
needToPrepare = batchCommand.ExplicitPrepare(connector) || needToPrepare;
@@ -652,15 +644,7 @@ Task Prepare(bool async, CancellationToken cancellationToken = default)
652644
}
653645
else
654646
{
655-
Parameters.HasOutputParameters = false;
656-
Parameters.PlaceholderType = PlaceholderType.NoParameters;
657-
658-
foreach (var p in Parameters.InternalList)
659-
{
660-
Parameters.CalculatePlaceholderType(p);
661-
p.Bind(connector.TypeMapper);
662-
}
663-
647+
Parameters.ProcessParameters(connector.TypeMapper, validate: false);
664648
ProcessRawQuery(connector.SqlQueryParser, connector.UseConformingStrings, batchCommand: null);
665649

666650
foreach (var batchCommand in InternalBatchCommands)
@@ -1335,9 +1319,9 @@ internal async ValueTask<NpgsqlDataReader> ExecuteReader(CommandBehavior behavio
13351319

13361320
if (IsWrappedByBatch)
13371321
foreach (var batchCommand in InternalBatchCommands)
1338-
batchCommand.Parameters.ValidateAndBind(connector.TypeMapper);
1322+
batchCommand.Parameters.ProcessParameters(connector.TypeMapper, validate: true);
13391323
else
1340-
Parameters.ValidateAndBind(connector.TypeMapper);
1324+
Parameters.ProcessParameters(connector.TypeMapper, validate: true);
13411325

13421326
NpgsqlEventSource.Log.CommandStartPrepared();
13431327
break;
@@ -1351,7 +1335,7 @@ internal async ValueTask<NpgsqlDataReader> ExecuteReader(CommandBehavior behavio
13511335
{
13521336
var batchCommand = InternalBatchCommands[i];
13531337

1354-
batchCommand.Parameters.ValidateAndBind(connector.TypeMapper);
1338+
batchCommand.Parameters.ProcessParameters(connector.TypeMapper, validate: true);
13551339
ProcessRawQuery(connector.SqlQueryParser, connector.UseConformingStrings, batchCommand);
13561340

13571341
if (connector.Settings.MaxAutoPrepare > 0 && batchCommand.TryAutoPrepare(connector))
@@ -1360,7 +1344,7 @@ internal async ValueTask<NpgsqlDataReader> ExecuteReader(CommandBehavior behavio
13601344
}
13611345
else
13621346
{
1363-
Parameters.ValidateAndBind(connector.TypeMapper);
1347+
Parameters.ProcessParameters(connector.TypeMapper, validate: true);
13641348
ProcessRawQuery(connector.SqlQueryParser, connector.UseConformingStrings, batchCommand: null);
13651349

13661350
if (connector.Settings.MaxAutoPrepare > 0)
@@ -1452,13 +1436,13 @@ internal async ValueTask<NpgsqlDataReader> ExecuteReader(CommandBehavior behavio
14521436
{
14531437
foreach (var batchCommand in InternalBatchCommands)
14541438
{
1455-
batchCommand.Parameters.ValidateAndBind(pool.MultiplexingTypeMapper!);
1439+
batchCommand.Parameters.ProcessParameters(pool.MultiplexingTypeMapper!, validate: true);
14561440
ProcessRawQuery(null, standardConformingStrings: true, batchCommand);
14571441
}
14581442
}
14591443
else
14601444
{
1461-
Parameters.ValidateAndBind(pool.MultiplexingTypeMapper!);
1445+
Parameters.ProcessParameters(pool.MultiplexingTypeMapper!, validate: true);
14621446
ProcessRawQuery(null, standardConformingStrings: true, batchCommand: null);
14631447
}
14641448

src/Npgsql/NpgsqlParameterCollection.cs

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ internal void CloneTo(NpgsqlParameterCollection other)
680680
}
681681
}
682682

683-
internal void ValidateAndBind(ConnectorTypeMapper typeMapper)
683+
internal void ProcessParameters(ConnectorTypeMapper typeMapper, bool validate)
684684
{
685685
HasOutputParameters = false;
686686
PlaceholderType = PlaceholderType.NoParameters;
@@ -689,7 +689,25 @@ internal void ValidateAndBind(ConnectorTypeMapper typeMapper)
689689
{
690690
var p = InternalList[i];
691691

692-
CalculatePlaceholderType(p);
692+
switch (PlaceholderType)
693+
{
694+
case PlaceholderType.NoParameters:
695+
PlaceholderType = p.IsPositional ? PlaceholderType.Positional : PlaceholderType.Named;
696+
break;
697+
case PlaceholderType.Named:
698+
if (p.IsPositional)
699+
PlaceholderType = PlaceholderType.Mixed;
700+
break;
701+
case PlaceholderType.Positional:
702+
if (!p.IsPositional)
703+
PlaceholderType = PlaceholderType.Mixed;
704+
break;
705+
case PlaceholderType.Mixed:
706+
break;
707+
default:
708+
throw new ArgumentOutOfRangeException(
709+
nameof(PlaceholderType), $"Unknown {nameof(PlaceholderType)} value: {PlaceholderType}");
710+
}
693711

694712
switch (p.Direction)
695713
{
@@ -718,53 +736,11 @@ internal void ValidateAndBind(ConnectorTypeMapper typeMapper)
718736
}
719737

720738
p.Bind(typeMapper);
721-
p.LengthCache?.Clear();
722-
p.ValidateAndGetLength();
723-
}
724-
}
725-
726-
internal void CalculatePlaceholderType(NpgsqlParameter p)
727-
{
728-
if (p.IsPositional)
729-
{
730-
switch (PlaceholderType)
731-
{
732-
case PlaceholderType.NoParameters:
733-
PlaceholderType = PlaceholderType.Positional;
734-
break;
735-
736-
case PlaceholderType.Named:
737-
PlaceholderType = PlaceholderType.Mixed;
738-
break;
739739

740-
case PlaceholderType.Positional:
741-
case PlaceholderType.Mixed:
742-
break;
743-
744-
default:
745-
throw new ArgumentOutOfRangeException(
746-
nameof(PlaceholderType), $"Unknown {nameof(PlaceholderType)} value: {PlaceholderType}");
747-
}
748-
}
749-
else
750-
{
751-
switch (PlaceholderType)
740+
if (validate)
752741
{
753-
case PlaceholderType.NoParameters:
754-
PlaceholderType = PlaceholderType.Named;
755-
break;
756-
757-
case PlaceholderType.Positional:
758-
PlaceholderType = PlaceholderType.Mixed;
759-
break;
760-
761-
case PlaceholderType.Named:
762-
case PlaceholderType.Mixed:
763-
break;
764-
765-
default:
766-
throw new ArgumentOutOfRangeException(
767-
nameof(PlaceholderType), $"Unknown {nameof(PlaceholderType)} value: {PlaceholderType}");
742+
p.LengthCache?.Clear();
743+
p.ValidateAndGetLength();
768744
}
769745
}
770746
}

0 commit comments

Comments
 (0)