Skip to content

Commit

Permalink
Merge pull request #100 from bruce-dunwiddie/develop
Browse files Browse the repository at this point in the history
Fixed handling of asterisks within SELECT
  • Loading branch information
bruce-dunwiddie authored Jul 1, 2022
2 parents 724c330 + 430e4cb commit 5a20e60
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public TSQLExpression Parse(ITSQLTokenizer tokenizer)

if (
tokenizer.Current != null &&
tokenizer.Current.Text != "*" &&
tokenizer.Current.Type.In(
TSQLTokenType.Operator))
{
Expand Down Expand Up @@ -111,6 +110,7 @@ public TSQLExpression ParseNext(
group.InnerExpression =
new TSQLValueExpressionParser().Parse(
tokenizer);

group.Tokens.AddRange(group.InnerExpression.Tokens);

if (tokenizer.Current.IsCharacter(
Expand Down Expand Up @@ -293,7 +293,8 @@ public TSQLExpression ParseNext(

#endregion
}
else if (tokenizer.Current.Text == "*")
else if (tokenizer.Current.Text == "*" &&
tokens.Last().IsCharacter(TSQLCharacters.Period))
{
#region parse multi column reference

Expand All @@ -309,18 +310,15 @@ public TSQLExpression ParseNext(
.Where(t => !t.IsComment() && !t.IsWhitespace())
.ToList();

if (columnReference.Count > 0)
{
// p.* will have the single token p in the final list
// p.* will have the single token p in the final list

// AdventureWorks..ErrorLog.* will have 4 tokens in the final list
// e.g. {AdventureWorks, ., ., ErrorLog}
// AdventureWorks..ErrorLog.* will have 4 tokens in the final list
// e.g. {AdventureWorks, ., ., ErrorLog}

multi.TableReference = columnReference
.GetRange(0, columnReference
.FindLastIndex(t => t.IsCharacter(TSQLCharacters.Period)))
.ToList();
}
multi.TableReference = columnReference
.GetRange(0, columnReference
.FindLastIndex(t => t.IsCharacter(TSQLCharacters.Period)))
.ToList();

TSQLTokenParserHelper.ReadThroughAnyCommentsOrWhitespace(
tokenizer,
Expand Down
4 changes: 2 additions & 2 deletions TSQL_Parser/TSQL_Parser/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.2.2.0")]
[assembly: AssemblyFileVersion("2.2.2.0")]
[assembly: AssemblyVersion("2.3.0.0")]
[assembly: AssemblyFileVersion("2.3.0.0")]

[assembly: InternalsVisibleTo("Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100793625650b945744f8a2c57bc75da89cd4d2c551636aa180c3020b7a15b815c10e983e83c312eb02f131c6fcf18aaffd6c8d9af6c4353c91ca0e9206b0fb8fb7805fc07b510a47ff40705ae56977ae8893e2d247d166aa400926582840e8a5602df055762bc3479dd14c9621a77946b6e6b0a00a77204c78fb52c65121bd75ba")]
4 changes: 2 additions & 2 deletions TSQL_Parser/TSQL_Parser/Push.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
nuget SetApiKey %NUGET_KEY%
nuget push TSQL.Parser.2.2.2.snupkg -Source https://api.nuget.org/v3/index.json
nuget push TSQL.Parser.2.2.2.nupkg -Source https://api.nuget.org/v3/index.json
nuget push TSQL.Parser.2.3.0.snupkg -Source https://api.nuget.org/v3/index.json
nuget push TSQL.Parser.2.3.0.nupkg -Source https://api.nuget.org/v3/index.json
pause
4 changes: 2 additions & 2 deletions TSQL_Parser/TSQL_Parser/TSQL_Parser.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<package>
<metadata>
<id>TSQL.Parser</id>
<version>2.2.2</version>
<version>2.3.0</version>
<title>TSQL.Parser</title>
<authors>Bruce Dunwiddie</authors>
<owners>shriop</owners>
<license type="expression">Apache-2.0</license>
<projectUrl>https://github.com/bruce-dunwiddie/tsql-parser</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Library for Parsing SQL Server T-SQL Scripts</description>
<releaseNotes>Fixed parsing of CAST function arguments when data type specification includes parentheses.</releaseNotes>
<releaseNotes>Fixed handling of asterisks within SELECT, as both multiplication and multicolumn expression.</releaseNotes>
<copyright>Copyright © 2022</copyright>
<tags>sql parser sql-server tsql</tags>
</metadata>
Expand Down
6 changes: 3 additions & 3 deletions TSQL_Parser/TSQL_Parser/TSQL_Parser_NetStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>TSQL_Parser</AssemblyName>
<RootNamespace>TSQL_Parser</RootNamespace>
<Version>2.2.2.0</Version>
<AssemblyVersion>2.2.2.0</AssemblyVersion>
<FileVersion>2.2.2.0</FileVersion>
<Version>2.3.0.0</Version>
<AssemblyVersion>2.3.0.0</AssemblyVersion>
<FileVersion>2.3.0.0</FileVersion>
<Description>Library for Parsing SQL Server TSQL Scripts</Description>
<Copyright>Copyright © 2022</Copyright>
<Company />
Expand Down
21 changes: 21 additions & 0 deletions TSQL_Parser/Tests/Statements/SelectStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,5 +823,26 @@ public void SelectStatement_CAST_argument_parsing_with_whitespace()
Assert.AreEqual(456.321, argument.Expression.AsConstant.Literal.AsNumericLiteral.Value);
Assert.AreEqual("VARCHAR(10)", argument.DataType);
}

[Test]
public void SelectStatement_ColumnAliasAsEquals()
{
// example from https://docs.microsoft.com/en-us/sql/t-sql/queries/select-examples-transact-sql?view=sql-server-ver16
List<TSQLStatement> statements = TSQLStatementReader.ParseStatements(
@"SELECT p.Name AS ProductName,
NonDiscountSales = (OrderQty * UnitPrice),
Discounts = ((OrderQty * UnitPrice) * UnitPriceDiscount)
FROM Production.Product AS p
INNER JOIN Sales.SalesOrderDetail AS sod
ON p.ProductID = sod.ProductID
ORDER BY ProductName DESC;",
includeWhitespace: false);

Assert.AreEqual(1, statements.Count);
TSQLSelectStatement select = statements.Single().AsSelect;
Assert.AreEqual(3, select.Select.Columns.Count);
Assert.AreEqual("NonDiscountSales", select.Select.Columns[1].ColumnAlias.Name);
Assert.AreEqual("Discounts", select.Select.Columns[2].ColumnAlias.Name);
}
}
}

0 comments on commit 5a20e60

Please sign in to comment.