Skip to content

Commit

Permalink
Added support to NEWID() expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
glecchi committed Nov 21, 2024
1 parent 813f7ad commit 5bf97f0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/CSQLQueryExpress/CSQLQueryExpress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Title>CSQLQueryExpress</Title>
<Version>1.4.3</Version>
<Version>1.4.4</Version>
<Description>A simple c# library to compile TSQL queries</Description>
<PackageProjectUrl></PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
6 changes: 6 additions & 0 deletions Lib/CSQLQueryExpress/Compiler/SQLQueryTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ private Expression VisitSysMethodCall(MethodCallExpression node)

return node;
}
else if (node.Method.Name == nameof(Sys.NewID))
{
_queryBuilder.Append($"NEWID()");

return node;
}

throw new NotSupportedException(string.Format("The method '{0}' is not supported.", node.Method.Name));
}
Expand Down
5 changes: 5 additions & 0 deletions Lib/CSQLQueryExpress/Statements/Sys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@ public static DateTime UtcDateTime()
{
return default;
}

public static Guid NewID()
{
return default;
}
}
}
19 changes: 19 additions & 0 deletions Tests/CSQLQueryExpress.Tests/UnitTests/QuerySelectUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,25 @@ public void TestStatement11()
Assert.DoesNotThrow(() => queryCommand.ToList());
}

[Test]
public void TestStatement12()
{
var query = new SQLQuery()
.From<dbo.Products>()
.Select(p => Sys.NewID(), p => p.ProductID, p => p.ProductName);

var compiledQuery = query.Compile();

Assert.That(compiledQuery.Parameters.Count, Is.EqualTo(0));

Assert.That(compiledQuery.Statement.Replace(Environment.NewLine, string.Empty),
Is.EqualTo(@"SELECT NEWID(), _t0.[ProductID], _t0.[ProductName] FROM [dbo].[Products] AS _t0"));

var queryCommand = new SQLQueryCommandReader(ConnectionString, compiledQuery);

Assert.DoesNotThrow(() => queryCommand.ToList());
}

private string GetSQLStatement([CallerMemberName] string memeberName = null)
{
var statement = File.ReadAllText($@"UnitTests\SelectStatements\{memeberName}.txt");
Expand Down

0 comments on commit 5bf97f0

Please sign in to comment.