Skip to content

Commit

Permalink
Attempting to add batch parsing support - jehugaleahsa#14
Browse files Browse the repository at this point in the history
1. Created a unit test 'TestBatch_MultipleInserts' that tests whether we can parse sql text that has 2 seperate insert statements in, using semi colon as the line termnator.
2. Extended the SqlTokenRegistry class with support for LineTerminator.
3. Extended the SqlGrammar class, the defineStart() method was changed so that it expects an optional LineTerminator after every statement.

I'm now stuck.. I think I want the Parser Class to return a MatchResult that includes all the Statements, however at the moment it just returns the individual InsertStatement - although it does correctly detect the LineTerminator token..
  • Loading branch information
dazinator committed Jul 6, 2014
1 parent 6ae486d commit 9a5c654
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
23 changes: 19 additions & 4 deletions SQLGeneration.Tests/CommandBuilderTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ public void TestSelect_FunctionWithBetweenFraming()
/// </summary>
[TestMethod]
public void TestSelect_FunctionWithStartFraming()
{
{
string commandText = "SELECT sale.prod_id, sale.month_num, sale.sales, SUM(sale.sales) OVER (PARTITION BY sale.prod_id ORDER BY sale.month_num ROWS 12 PRECEDING) FROM sale";
assertCanReproduce(commandText);
}
Expand Down Expand Up @@ -1025,6 +1025,21 @@ public void TestDelete_WhereClause()

#endregion

#region Batch

/// <summary>
/// This sees whether we can reproduce multiple insert statements in a batch.
/// </summary>
[TestMethod]
public void TestBatch_MultipleInserts()
{
string commandText = @"INSERT INTO Table VALUES();
INSERT INTO Table VALUES()";
assertCanReproduce(commandText);
}

#endregion

private void assertCanReproduce(string commandText, CommandBuilderOptions options = null)
{
CommandBuilder builder = new CommandBuilder();
Expand All @@ -1049,7 +1064,7 @@ public void TestSelect_AddFilter()
customerId.Qualify = false;
Placeholder parameter = new Placeholder("@customerId");
select.AddWhere(new EqualToFilter(customerId, parameter));

Formatter formatter = new Formatter();
string actual = formatter.GetCommandText(select);
string expected = "SELECT * FROM Customer WHERE CustomerId = @customerId";
Expand Down Expand Up @@ -1078,7 +1093,7 @@ public void TestSelect_ExtraWhitespace()
[TestMethod]
public void TestSelect_Newlines()
{
string commandText =
string commandText =
@"SELECT
*
FROM Table1
Expand Down Expand Up @@ -1213,7 +1228,7 @@ public void TestFilterGroup_Optimize_SimplifiesConditions()
new EqualToFilter(new Column("FirstName"), new StringLiteral("Max")),
new FilterGroup(Conjunction.And,
new EqualToFilter(new Column("LastName"), new StringLiteral("Planck")))));

wrapInParentheses(topFilter, true);

SelectBuilder selectBuilder = new SelectBuilder();
Expand Down
32 changes: 20 additions & 12 deletions SQLGeneration/Parsing/SqlGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public SqlGrammar(SqlTokenRegistry registry = null)
/// <summary>
/// Gets the default instance of the SqlGrammar.
/// </summary>
public static SqlGrammar Default
{
get { return instance; }
public static SqlGrammar Default
{
get { return instance; }
}

#region Start
Expand All @@ -89,8 +89,8 @@ public static class Start
public const string Name = "Start";

/// <summary>
/// Gets the name for the SELECT statement option.
/// </summary>
/// Gets the name for the SELECT statement option.
/// </summary>
public const string SelectStatement = "select_statement";

/// <summary>
Expand All @@ -107,6 +107,13 @@ public static class Start
/// Gets the name for the DELETE statement option.
/// </summary>
public const string DeleteStatement = "delete_statement";

/// <summary>
/// Gets the name for the Terminator of a statement.
/// </summary>
public const string Terminator = "terminator";


}

private void defineStart()
Expand All @@ -116,7 +123,8 @@ private void defineStart()
.Add(Start.SelectStatement, Expression(SelectStatement.Name))
.Add(Start.InsertStatement, Expression(InsertStatement.Name))
.Add(Start.UpdateStatement, Expression(UpdateStatement.Name))
.Add(Start.DeleteStatement, Expression(DeleteStatement.Name)));
.Add(Start.DeleteStatement, Expression(DeleteStatement.Name)))
.Add(Start.Terminator, false, Token(SqlTokenRegistry.LineTerminator));
}

#endregion
Expand Down Expand Up @@ -184,8 +192,8 @@ public static class SelectExpression
public const string Name = "SelectExpression";

/// <summary>
/// Describes the structure of the leading SELECT expression when it is surrounded by parenthesis.
/// </summary>
/// Describes the structure of the leading SELECT expression when it is surrounded by parenthesis.
/// </summary>
public static class Wrapped
{
/// <summary>
Expand Down Expand Up @@ -884,7 +892,7 @@ public static class Multiple
/// Gets the identifier for the comma separator.
/// </summary>
public const string Comma = "comma";

/// <summary>
/// Gets the identifier for the rest of the projection list.
/// </summary>
Expand Down Expand Up @@ -1899,7 +1907,7 @@ public static class Like
/// Gets the identifier indicating whether the filter is doing a string comparison.
/// </summary>
public const string Name = "Like";

/// <summary>
/// Gets the identifier for the expression being compared.
/// </summary>
Expand Down Expand Up @@ -2665,7 +2673,7 @@ public static class Wrapped
/// </summary>
public const string RightParenthesis = "right_parenthesis";
}

/// <summary>
/// Gets the identifier for an unwrapped item.
/// </summary>
Expand Down Expand Up @@ -3686,7 +3694,7 @@ public static class Multiple
/// Gets the identifier for the dot separator.
/// </summary>
public const string Dot = "dot";

/// <summary>
/// Gets the identifier for the rest of the identifiers.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions SQLGeneration/Parsing/SqlTokenRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ public class SqlTokenRegistry : TokenRegistry
/// </summary>
public const string End = "End";

/// <summary>
/// Gets the identifier for the Line Terminator which can be used to seperate SQL statements in a batch.
/// </summary>
public const string LineTerminator = "LineTerminator";

/// <summary>
/// Initializes a new instance of a SqlTokenizer.
/// </summary>
Expand Down Expand Up @@ -458,6 +463,7 @@ public SqlTokenRegistry()
Define(Number, @"[-+]?\d*\.?\d+([eE][-+]?\d+)?");
Define(RightParenthesis, @"\)");
Define(String, @"'([^']|'')*'");
Define(LineTerminator, @"('(?:\\.|''|[^'])*(')?)|(;)");
}
}
}

0 comments on commit 9a5c654

Please sign in to comment.