Skip to content

Commit

Permalink
Refactored session variables
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Oct 7, 2024
1 parent 7c6a314 commit 82b6178
Show file tree
Hide file tree
Showing 51 changed files with 1,315 additions and 862 deletions.
59 changes: 59 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/AdoProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2796,5 +2796,64 @@ public void DateTimeOffsetToString(string type, string suffix)
Assert.AreEqual($"2024-10-04 12:01:02{suffix} +05:10", actual);
}
}

[DataTestMethod]
[DataRow("mdy", "2003-01-02")]
[DataRow("dmy", "2003-02-01")]
[DataRow("ymd", "2001-02-03")]
[DataRow("ydm", "2001-03-02")]
[DataRow("myd", "2002-01-03")]
[DataRow("dym", "2002-03-01")]
public void SetDataFormat(string format, string expected)
{
using (var con = new Sql4CdsConnection(_localDataSources))
using (var cmd = con.CreateCommand())
{
cmd.CommandText = $@"
SET DATEFORMAT {format};
SELECT CAST('01/02/03' AS DATETIME)";

var actual = (DateTime)cmd.ExecuteScalar();
Assert.AreEqual(DateTime.ParseExact(expected, "yyyy-MM-dd", CultureInfo.InvariantCulture), actual);
}
}

[TestMethod]
public void ErrorNumberPersistedBetweenExecutions()
{
using (var con = new Sql4CdsConnection(_localDataSources))
{
using (var cmd = con.CreateCommand())
{
try
{
cmd.CommandText = "SELECT 1/0";
cmd.ExecuteScalar();
Assert.Fail();
}
catch (Sql4CdsException ex)
{
if (ex.Number != 8134)
Assert.Fail();
}
}

using (var cmd = con.CreateCommand())
{
// Error should be persisted in the connection session from the previous command
cmd.CommandText = "SELECT @@ERROR";
var error = (int)cmd.ExecuteScalar();
Assert.AreEqual(8134, error);
}

using (var cmd = con.CreateCommand())
{
// Error should be reset by the previous execution
cmd.CommandText = "SELECT @@ERROR";
var error = (int)cmd.ExecuteScalar();
Assert.AreEqual(0, error);
}
}
}
}
}
52 changes: 27 additions & 25 deletions MarkMpn.Sql4Cds.Engine.Tests/CteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class CteTests : FakeXrmEasyTestsBase, IQueryExecutionOptions

bool IQueryExecutionOptions.BypassCustomPlugins => false;

public event EventHandler PrimaryDataSourceChanged;

void IQueryExecutionOptions.ConfirmInsert(ConfirmDmlStatementEventArgs e)
{
}
Expand Down Expand Up @@ -78,7 +80,7 @@ void IQueryExecutionOptions.Progress(double? progress, string message)
[TestMethod]
public void SimpleSelect()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (SELECT accountid, name FROM account)
Expand All @@ -102,7 +104,7 @@ WITH cte AS (SELECT accountid, name FROM account)
[TestMethod]
public void ColumnAliases()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte (id, n) AS (SELECT accountid, name FROM account)
Expand All @@ -126,7 +128,7 @@ WITH cte (id, n) AS (SELECT accountid, name FROM account)
[TestMethod]
public void MultipleAnchorQueries()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte (id, n) AS (SELECT accountid, name FROM account UNION ALL select contactid, fullname FROM contact)
Expand Down Expand Up @@ -159,7 +161,7 @@ WITH cte (id, n) AS (SELECT accountid, name FROM account UNION ALL select contac
[TestMethod]
public void MergeFilters()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (SELECT contactid, firstname, lastname FROM contact WHERE firstname = 'Mark')
Expand Down Expand Up @@ -188,7 +190,7 @@ WITH cte AS (SELECT contactid, firstname, lastname FROM contact WHERE firstname
[TestMethod]
public void MultipleReferencesWithAliases()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (SELECT contactid, firstname, lastname FROM contact WHERE firstname = 'Mark')
Expand Down Expand Up @@ -226,7 +228,7 @@ WITH cte AS (SELECT contactid, firstname, lastname FROM contact WHERE firstname
[TestMethod]
public void MultipleReferencesInUnionAll()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (SELECT contactid, firstname, lastname FROM contact WHERE firstname = 'Mark')
Expand Down Expand Up @@ -262,7 +264,7 @@ WITH cte AS (SELECT contactid, firstname, lastname FROM contact WHERE firstname
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void MultipleRecursiveReferences()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -279,7 +281,7 @@ UNION ALL
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void HintsOnRecursiveReference()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -296,7 +298,7 @@ UNION ALL
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void RecursionWithoutUnionAll()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -313,7 +315,7 @@ SELECT cte.* FROM cte
[ExpectedException(typeof(QueryParseException))]
public void OrderByWithoutTop()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -331,7 +333,7 @@ ORDER BY firstname
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void GroupByOnRecursiveReference()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -348,7 +350,7 @@ UNION ALL
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void AggregateOnRecursiveReference()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -365,7 +367,7 @@ SELECT MIN(contactid), MIN(firstname), MIN(lastname) FROM cte
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void TopOnRecursiveReference()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -382,7 +384,7 @@ SELECT TOP 10 cte.* FROM cte
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void OuterJoinOnRecursiveReference()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -399,7 +401,7 @@ UNION ALL
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void SubqueryOnRecursiveReference()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -416,7 +418,7 @@ UNION ALL
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void IncorrectColumnCount()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte (id, fname) AS (
Expand All @@ -431,7 +433,7 @@ WITH cte (id, fname) AS (
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void AnonymousColumn()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand All @@ -446,7 +448,7 @@ WITH cte AS (
[ExpectedException(typeof(NotSupportedQueryFragmentException))]
public void MissingAnchorQuery()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte (x, y) AS (
Expand All @@ -460,7 +462,7 @@ WITH cte (x, y) AS (
[TestMethod]
public void AliasedAnonymousColumn()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte (id, fname, lname) AS (
Expand Down Expand Up @@ -498,7 +500,7 @@ WITH cte (id, fname, lname) AS (
[TestMethod]
public void SelectStarFromValues()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH source_data_cte AS (
Expand Down Expand Up @@ -535,7 +537,7 @@ WITH source_data_cte AS (
[TestMethod]
public void SimpleRecursion()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH cte AS (
Expand Down Expand Up @@ -675,7 +677,7 @@ union all
[TestMethod]
public void Under()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH account_hierarchical(accountid) AS (
Expand Down Expand Up @@ -706,7 +708,7 @@ UNION ALL
[TestMethod]
public void EqOrUnder()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH account_hierarchical(accountid) AS (
Expand Down Expand Up @@ -737,7 +739,7 @@ UNION ALL
[TestMethod]
public void Above()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH account_hierarchical(accountid, parentaccountid) AS (
Expand Down Expand Up @@ -768,7 +770,7 @@ UNION ALL
[TestMethod]
public void EqOrAbove()
{
var planBuilder = new ExecutionPlanBuilder(_localDataSources.Values, this);
var planBuilder = new ExecutionPlanBuilder(new SessionContext(_localDataSources, this), this);

var query = @"
WITH account_hierarchical(accountid, parentaccountid) AS (
Expand Down
Loading

0 comments on commit 82b6178

Please sign in to comment.