Skip to content

Commit

Permalink
Store all parameter values as INullable
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed May 16, 2024
1 parent 72a28f8 commit 5ec2bb2
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 58 deletions.
14 changes: 7 additions & 7 deletions MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3714,7 +3714,7 @@ GROUP BY firstname
},
};

var result = select.Execute(new NodeExecutionContext(_localDataSources, this, new Dictionary<string, DataTypeReference>(), new Dictionary<string, object>(), null), CommandBehavior.Default);
var result = select.Execute(new NodeExecutionContext(_localDataSources, this, new Dictionary<string, DataTypeReference>(), new Dictionary<string, INullable>(), null), CommandBehavior.Default);
var dataTable = new DataTable();
dataTable.Load(result);

Expand Down Expand Up @@ -4192,7 +4192,7 @@ DECLARE @test int
Assert.AreEqual(1, selectConstantScan.Values.Count);

var parameterTypes = new Dictionary<string, DataTypeReference>();
var parameterValues = new Dictionary<string, object>();
var parameterValues = new Dictionary<string, INullable>();

foreach (var plan in plans)
{
Expand Down Expand Up @@ -4246,7 +4246,7 @@ public void SetVariableInDeclaration()
Assert.AreEqual(1, selectConstantScan.Values.Count);

var parameterTypes = new Dictionary<string, DataTypeReference>();
var parameterValues = new Dictionary<string, object>();
var parameterValues = new Dictionary<string, INullable>();

foreach (var plan in plans)
{
Expand Down Expand Up @@ -4304,7 +4304,7 @@ DECLARE @test varchar(3)
var plans = planBuilder.Build(query, null, out _);

var parameterTypes = new Dictionary<string, DataTypeReference>();
var parameterValues = new Dictionary<string, object>();
var parameterValues = new Dictionary<string, INullable>();

foreach (var plan in plans)
{
Expand Down Expand Up @@ -4338,7 +4338,7 @@ DECLARE @test varchar(3)
var plans = planBuilder.Build(query, null, out _);

var parameterTypes = new Dictionary<string, DataTypeReference>();
var parameterValues = new Dictionary<string, object>();
var parameterValues = new Dictionary<string, INullable>();

foreach (var plan in plans)
{
Expand Down Expand Up @@ -4430,7 +4430,7 @@ DECLARE @test varchar(3)
};

var parameterTypes = new Dictionary<string, DataTypeReference>();
var parameterValues = new Dictionary<string, object>();
var parameterValues = new Dictionary<string, INullable>();

foreach (var plan in plans)
{
Expand Down Expand Up @@ -4464,7 +4464,7 @@ DECLARE @test varchar
var plans = planBuilder.Build(query, null, out _);

var parameterTypes = new Dictionary<string, DataTypeReference>();
var parameterValues = new Dictionary<string, object>();
var parameterValues = new Dictionary<string, INullable>();

foreach (var plan in plans)
{
Expand Down
62 changes: 31 additions & 31 deletions MarkMpn.Sql4Cds.Engine.Tests/Sql2FetchXmlTests.cs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Sql4CdsConnection : DbConnection
private readonly IDictionary<string, DataSource> _dataSources;
private readonly ChangeDatabaseOptionsWrapper _options;
private readonly Dictionary<string, DataTypeReference> _globalVariableTypes;
private readonly Dictionary<string, object> _globalVariableValues;
private readonly Dictionary<string, INullable> _globalVariableValues;
private readonly TelemetryClient _ai;

/// <summary>
Expand Down Expand Up @@ -74,7 +74,7 @@ public Sql4CdsConnection(IDictionary<string, DataSource> dataSources)
["@@VERSION"] = DataTypeHelpers.NVarChar(Int32.MaxValue, _dataSources[_options.PrimaryDataSource].DefaultCollation, CollationLabel.CoercibleDefault),
["@@ERROR"] = DataTypeHelpers.Int,
};
_globalVariableValues = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
_globalVariableValues = new Dictionary<string, INullable>(StringComparer.OrdinalIgnoreCase)
{
["@@IDENTITY"] = SqlEntityReference.Null,
["@@ROWCOUNT"] = (SqlInt32)0,
Expand Down Expand Up @@ -291,7 +291,7 @@ public ColumnOrdering ColumnOrdering

internal Dictionary<string, DataTypeReference> GlobalVariableTypes => _globalVariableTypes;

internal Dictionary<string, object> GlobalVariableValues => _globalVariableValues;
internal Dictionary<string, INullable> GlobalVariableValues => _globalVariableValues;

internal TelemetryClient TelemetryClient => _ai;

Expand Down
8 changes: 4 additions & 4 deletions MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Sql4CdsDataReader : DbDataReader
private readonly IQueryExecutionOptions _options;
private readonly CommandBehavior _behavior;
private readonly Dictionary<string, DataTypeReference> _parameterTypes;
private readonly Dictionary<string, object> _parameterValues;
private readonly Dictionary<string, INullable> _parameterValues;
private Dictionary<string, int> _labelIndexes;
private int _recordsAffected;
private int _instructionPointer;
Expand Down Expand Up @@ -57,7 +57,7 @@ public Sql4CdsDataReader(Sql4CdsCommand command, IQueryExecutionOptions options,
Close();
}

internal Dictionary<string, object> ParameterValues => _parameterValues;
internal Dictionary<string, INullable> ParameterValues => _parameterValues;

private Dictionary<string, int> LabelIndexes
{
Expand All @@ -75,7 +75,7 @@ private Dictionary<string, int> LabelIndexes
}
}

private bool ExecuteWithExceptionHandling(Dictionary<string, DataTypeReference> parameterTypes, Dictionary<string, object> parameterValues)
private bool ExecuteWithExceptionHandling(Dictionary<string, DataTypeReference> parameterTypes, Dictionary<string, INullable> parameterValues)
{
while (true)
{
Expand Down Expand Up @@ -109,7 +109,7 @@ private bool ExecuteWithExceptionHandling(Dictionary<string, DataTypeReference>
}
}

private bool Execute(Dictionary<string, DataTypeReference> parameterTypes, Dictionary<string, object> parameterValues)
private bool Execute(Dictionary<string, DataTypeReference> parameterTypes, Dictionary<string, INullable> parameterValues)
{
IRootExecutionPlanNode logNode = null;
var context = new NodeExecutionContext(_connection.DataSources, _options, parameterTypes, parameterValues, msg => _connection.OnInfoMessage(logNode, msg));
Expand Down
5 changes: 3 additions & 2 deletions MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsParameterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.TransactSql.ScriptDom;
Expand Down Expand Up @@ -50,11 +51,11 @@ internal Dictionary<string, DataTypeReference> GetParameterTypes()
.ToDictionary(param => param.FullParameterName, param => param.GetDataType(), StringComparer.OrdinalIgnoreCase);
}

internal Dictionary<string, object> GetParameterValues()
internal Dictionary<string, INullable> GetParameterValues()
{
return _parameters
.Cast<Sql4CdsParameter>()
.ToDictionary(param => param.FullParameterName, param => (object) param.GetValue(), StringComparer.OrdinalIgnoreCase);
.ToDictionary(param => param.FullParameterName, param => param.GetValue(), StringComparer.OrdinalIgnoreCase);
}

public override bool Contains(string value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override void Execute(NodeExecutionContext context, out int recordsAffect
foreach (var entity in entities)
{
foreach (var variable in Variables)
context.ParameterValues[variable.VariableName] = valueAccessors[variable.VariableName](entity);
context.ParameterValues[variable.VariableName] = (INullable)valueAccessors[variable.VariableName](entity);

count++;
}
Expand Down
3 changes: 2 additions & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlan/InsertNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Linq;
using System.ServiceModel;
using System.Threading;
Expand Down Expand Up @@ -292,7 +293,7 @@ protected override bool FilterErrors(NodeExecutionContext context, OrganizationR
return true;
}

private void SetIdentity(OrganizationResponse response, IDictionary<string, object> parameterValues)
private void SetIdentity(OrganizationResponse response, IDictionary<string, INullable> parameterValues)
{
if (response is CreateResponse create)
parameterValues["@@IDENTITY"] = new SqlEntityReference(DataSource, LogicalName, create.id);
Expand Down
7 changes: 4 additions & 3 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/NestedLoopNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -71,14 +72,14 @@ protected override IEnumerable<Entity> ExecuteInternal(NodeExecutionContext cont
if (OuterReferences != null)
{
if (innerParameters == null)
innerParameters = new Dictionary<string, object>();
innerParameters = new Dictionary<string, INullable>();
else
innerParameters = new Dictionary<string, object>(innerParameters);
innerParameters = new Dictionary<string, INullable>(innerParameters);

foreach (var kvp in OuterReferences)
{
left.Attributes.TryGetValue(kvp.Key, out var outerValue);
innerParameters[kvp.Value] = outerValue;
innerParameters[kvp.Value] = (INullable)outerValue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected override IEnumerable<Entity> ExecuteInternal(NodeExecutionContext cont
Parent = this
};

var partitionParameterValues = new Dictionary<string, object>
var partitionParameterValues = new Dictionary<string, INullable>
{
["@PartitionStart"] = minKey,
["@PartitionEnd"] = maxKey
Expand Down
4 changes: 2 additions & 2 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/SelectDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class SelectDataReader : DbDataReader
private readonly IDisposable _timer;
private readonly INodeSchema _schema;
private readonly IEnumerator<Entity> _source;
private readonly IDictionary<string, object> _parameterValues;
private readonly IDictionary<string, INullable> _parameterValues;
private Entity _row;
private bool _closed;
private int _rowCount;

public SelectDataReader(List<SelectColumn> columnSet, IDisposable timer, INodeSchema schema, IEnumerable<Entity> source, IDictionary<string, object> parameterValues)
public SelectDataReader(List<SelectColumn> columnSet, IDisposable timer, INodeSchema schema, IEnumerable<Entity> source, IDictionary<string, INullable> parameterValues)
{
_columnSet = columnSet;
_timer = timer;
Expand Down
7 changes: 4 additions & 3 deletions MarkMpn.Sql4Cds.Engine/NodeContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Text;
using MarkMpn.Sql4Cds.Engine.ExecutionPlan;
using Microsoft.SqlServer.TransactSql.ScriptDom;
Expand Down Expand Up @@ -105,7 +106,7 @@ public NodeExecutionContext(
IDictionary<string, DataSource> dataSources,
IQueryExecutionOptions options,
IDictionary<string, DataTypeReference> parameterTypes,
IDictionary<string, object> parameterValues,
IDictionary<string, INullable> parameterValues,
Action<Sql4CdsError> log)
: base(dataSources, options, parameterTypes, log)
{
Expand All @@ -115,7 +116,7 @@ public NodeExecutionContext(
/// <summary>
/// Returns the current value of each parameter
/// </summary>
public IDictionary<string, object> ParameterValues { get; }
public IDictionary<string, INullable> ParameterValues { get; }

public Sql4CdsError Error { get; set; }
}
Expand Down Expand Up @@ -191,7 +192,7 @@ public ExpressionExecutionContext(
IDictionary<string, DataSource> dataSources,
IQueryExecutionOptions options,
IDictionary<string, DataTypeReference> parameterTypes,
IDictionary<string, object> parameterValues,
IDictionary<string, INullable> parameterValues,
Action<Sql4CdsError> log,
Entity entity)
: base(dataSources, options, parameterTypes, parameterValues, log)
Expand Down

0 comments on commit 5ec2bb2

Please sign in to comment.