Skip to content

Commit 5a20e60

Browse files
Merge pull request #100 from bruce-dunwiddie/develop
Fixed handling of asterisks within SELECT
2 parents 724c330 + 430e4cb commit 5a20e60

6 files changed

Lines changed: 40 additions & 21 deletions

File tree

TSQL_Parser/TSQL_Parser/Expressions/Parsers/TSQLValueExpressionParser.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public TSQLExpression Parse(ITSQLTokenizer tokenizer)
1919

2020
if (
2121
tokenizer.Current != null &&
22-
tokenizer.Current.Text != "*" &&
2322
tokenizer.Current.Type.In(
2423
TSQLTokenType.Operator))
2524
{
@@ -111,6 +110,7 @@ public TSQLExpression ParseNext(
111110
group.InnerExpression =
112111
new TSQLValueExpressionParser().Parse(
113112
tokenizer);
113+
114114
group.Tokens.AddRange(group.InnerExpression.Tokens);
115115

116116
if (tokenizer.Current.IsCharacter(
@@ -293,7 +293,8 @@ public TSQLExpression ParseNext(
293293

294294
#endregion
295295
}
296-
else if (tokenizer.Current.Text == "*")
296+
else if (tokenizer.Current.Text == "*" &&
297+
tokens.Last().IsCharacter(TSQLCharacters.Period))
297298
{
298299
#region parse multi column reference
299300

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

312-
if (columnReference.Count > 0)
313-
{
314-
// p.* will have the single token p in the final list
313+
// p.* will have the single token p in the final list
315314

316-
// AdventureWorks..ErrorLog.* will have 4 tokens in the final list
317-
// e.g. {AdventureWorks, ., ., ErrorLog}
315+
// AdventureWorks..ErrorLog.* will have 4 tokens in the final list
316+
// e.g. {AdventureWorks, ., ., ErrorLog}
318317

319-
multi.TableReference = columnReference
320-
.GetRange(0, columnReference
321-
.FindLastIndex(t => t.IsCharacter(TSQLCharacters.Period)))
322-
.ToList();
323-
}
318+
multi.TableReference = columnReference
319+
.GetRange(0, columnReference
320+
.FindLastIndex(t => t.IsCharacter(TSQLCharacters.Period)))
321+
.ToList();
324322

325323
TSQLTokenParserHelper.ReadThroughAnyCommentsOrWhitespace(
326324
tokenizer,

TSQL_Parser/TSQL_Parser/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("2.2.2.0")]
36-
[assembly: AssemblyFileVersion("2.2.2.0")]
35+
[assembly: AssemblyVersion("2.3.0.0")]
36+
[assembly: AssemblyFileVersion("2.3.0.0")]
3737

3838
[assembly: InternalsVisibleTo("Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100793625650b945744f8a2c57bc75da89cd4d2c551636aa180c3020b7a15b815c10e983e83c312eb02f131c6fcf18aaffd6c8d9af6c4353c91ca0e9206b0fb8fb7805fc07b510a47ff40705ae56977ae8893e2d247d166aa400926582840e8a5602df055762bc3479dd14c9621a77946b6e6b0a00a77204c78fb52c65121bd75ba")]

TSQL_Parser/TSQL_Parser/Push.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
nuget SetApiKey %NUGET_KEY%
2-
nuget push TSQL.Parser.2.2.2.snupkg -Source https://api.nuget.org/v3/index.json
3-
nuget push TSQL.Parser.2.2.2.nupkg -Source https://api.nuget.org/v3/index.json
2+
nuget push TSQL.Parser.2.3.0.snupkg -Source https://api.nuget.org/v3/index.json
3+
nuget push TSQL.Parser.2.3.0.nupkg -Source https://api.nuget.org/v3/index.json
44
pause

TSQL_Parser/TSQL_Parser/TSQL_Parser.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<package>
33
<metadata>
44
<id>TSQL.Parser</id>
5-
<version>2.2.2</version>
5+
<version>2.3.0</version>
66
<title>TSQL.Parser</title>
77
<authors>Bruce Dunwiddie</authors>
88
<owners>shriop</owners>
99
<license type="expression">Apache-2.0</license>
1010
<projectUrl>https://github.com/bruce-dunwiddie/tsql-parser</projectUrl>
1111
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1212
<description>Library for Parsing SQL Server T-SQL Scripts</description>
13-
<releaseNotes>Fixed parsing of CAST function arguments when data type specification includes parentheses.</releaseNotes>
13+
<releaseNotes>Fixed handling of asterisks within SELECT, as both multiplication and multicolumn expression.</releaseNotes>
1414
<copyright>Copyright © 2022</copyright>
1515
<tags>sql parser sql-server tsql</tags>
1616
</metadata>

TSQL_Parser/TSQL_Parser/TSQL_Parser_NetStandard.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<AssemblyName>TSQL_Parser</AssemblyName>
66
<RootNamespace>TSQL_Parser</RootNamespace>
7-
<Version>2.2.2.0</Version>
8-
<AssemblyVersion>2.2.2.0</AssemblyVersion>
9-
<FileVersion>2.2.2.0</FileVersion>
7+
<Version>2.3.0.0</Version>
8+
<AssemblyVersion>2.3.0.0</AssemblyVersion>
9+
<FileVersion>2.3.0.0</FileVersion>
1010
<Description>Library for Parsing SQL Server TSQL Scripts</Description>
1111
<Copyright>Copyright © 2022</Copyright>
1212
<Company />

TSQL_Parser/Tests/Statements/SelectStatementTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,5 +823,26 @@ public void SelectStatement_CAST_argument_parsing_with_whitespace()
823823
Assert.AreEqual(456.321, argument.Expression.AsConstant.Literal.AsNumericLiteral.Value);
824824
Assert.AreEqual("VARCHAR(10)", argument.DataType);
825825
}
826+
827+
[Test]
828+
public void SelectStatement_ColumnAliasAsEquals()
829+
{
830+
// example from https://docs.microsoft.com/en-us/sql/t-sql/queries/select-examples-transact-sql?view=sql-server-ver16
831+
List<TSQLStatement> statements = TSQLStatementReader.ParseStatements(
832+
@"SELECT p.Name AS ProductName,
833+
NonDiscountSales = (OrderQty * UnitPrice),
834+
Discounts = ((OrderQty * UnitPrice) * UnitPriceDiscount)
835+
FROM Production.Product AS p
836+
INNER JOIN Sales.SalesOrderDetail AS sod
837+
ON p.ProductID = sod.ProductID
838+
ORDER BY ProductName DESC;",
839+
includeWhitespace: false);
840+
841+
Assert.AreEqual(1, statements.Count);
842+
TSQLSelectStatement select = statements.Single().AsSelect;
843+
Assert.AreEqual(3, select.Select.Columns.Count);
844+
Assert.AreEqual("NonDiscountSales", select.Select.Columns[1].ColumnAlias.Name);
845+
Assert.AreEqual("Discounts", select.Select.Columns[2].ColumnAlias.Name);
846+
}
826847
}
827848
}

0 commit comments

Comments
 (0)