/// <summary> /// Visits a MatchCase builder. /// </summary> /// <param name="item">The item to visit.</param> protected internal virtual void VisitMatchCase(MatchCase item) { }
/// <summary> /// Generates the text for a MatchCase builder. /// </summary> /// <param name="item">The MatchCase builder to generate the text for.</param> protected internal override void VisitMatchCase(MatchCase item) { if (!item.Branches.Any()) { throw new SQLGenerationException(Resources.EmptyCaseExpression); } writer.Write("CASE "); item.Item.Accept(forSubCommand()); writer.Write(" "); foreach (MatchCaseBranch branch in item.Branches) { writer.Write("WHEN "); branch.Option.Accept(forSubCommand()); writer.Write(" THEN "); branch.Value.Accept(forSubCommand()); writer.Write(" "); } if (item.Default != null) { writer.Write("ELSE "); item.Default.Accept(forSubCommand()); writer.Write(" "); } writer.Write("END"); }
private void buildMatchList(MatchResult result, MatchCase options) { MatchResult match = result.Matches[SqlGrammar.MatchList.Match]; buildMatch(match, options); MatchResult matchListPrime = result.Matches[SqlGrammar.MatchList.MatchListPrime]; buildMatchListPrime(matchListPrime, options); }
private void buildMatchListPrime(MatchResult result, MatchCase options) { MatchResult matchResult = result.Matches[SqlGrammar.MatchListPrime.Match.Name]; if (matchResult.IsMatch) { MatchResult firstResult = matchResult.Matches[SqlGrammar.MatchListPrime.Match.First]; buildMatch(firstResult, options); MatchResult remainingResult = matchResult.Matches[SqlGrammar.MatchListPrime.Match.Remaining]; buildMatchListPrime(remainingResult, options); return; } MatchResult elseResult = result.Matches[SqlGrammar.MatchListPrime.Else.Name]; if (elseResult.IsMatch) { MatchResult valueResult = elseResult.Matches[SqlGrammar.MatchListPrime.Else.Value]; options.Default = (IProjectionItem)buildArithmeticItem(valueResult); return; } MatchResult emptyResult = result.Matches[SqlGrammar.MatchListPrime.Empty]; if (emptyResult.IsMatch) { return; } throw new InvalidOperationException(); }
private MatchCase buildMatchCase(MatchResult result) { MatchResult expressionResult = result.Matches[SqlGrammar.MatchCase.Expression]; IProjectionItem expression = (IProjectionItem)buildArithmeticItem(expressionResult); MatchCase options = new MatchCase(expression); MatchResult matchListResult = result.Matches[SqlGrammar.MatchCase.MatchList]; buildMatchList(matchListResult, options); return options; }
private void buildMatch(MatchResult result, MatchCase options) { MatchResult expressionResult = result.Matches[SqlGrammar.Match.Expression]; IProjectionItem expression = (IProjectionItem)buildArithmeticItem(expressionResult); MatchResult valueResult = result.Matches[SqlGrammar.Match.Value]; IProjectionItem value = (IProjectionItem)buildArithmeticItem(valueResult); options.AddBranch(expression, value); }
public void TestSelect_MatchCase_MultipleCases() { SelectBuilder builder = new SelectBuilder(); AliasedSource source = builder.AddTable(new Table("Table")); Column column = source.Column("Column"); MatchCase options = new MatchCase(column); options.AddBranch(new NumericLiteral(0), new StringLiteral("Sunday")); options.AddBranch(new NumericLiteral(1), new StringLiteral("Monday")); options.AddBranch(new NumericLiteral(2), new StringLiteral("Tuesday")); options.AddBranch(new NumericLiteral(3), new StringLiteral("Wednesday")); options.AddBranch(new NumericLiteral(4), new StringLiteral("Thursday")); options.AddBranch(new NumericLiteral(5), new StringLiteral("Friday")); options.AddBranch(new NumericLiteral(6), new StringLiteral("Saturday")); builder.AddProjection(options); Formatter formatter = new Formatter(); string actual = formatter.GetCommandText(builder); string expected = "SELECT CASE Table.Column WHEN 0 THEN 'Sunday' WHEN 1 THEN 'Monday' WHEN 2 THEN 'Tuesday' WHEN 3 THEN 'Wednesday' WHEN 4 THEN 'Thursday' WHEN 5 THEN 'Friday' WHEN 6 THEN 'Saturday' END FROM Table"; Assert.AreEqual(expected, actual, "The wrong SQL was generated."); }
public void TestSelect_MatchCase_Else() { SelectBuilder builder = new SelectBuilder(); AliasedSource source = builder.AddTable(new Table("Table")); Column column = source.Column("Column"); MatchCase options = new MatchCase(column); options.AddBranch(new StringLiteral("Admin"), new StringLiteral("Administrator")); options.Default = new StringLiteral("User"); builder.AddProjection(options); Formatter formatter = new Formatter(); string actual = formatter.GetCommandText(builder); string expected = "SELECT CASE Table.Column WHEN 'Admin' THEN 'Administrator' ELSE 'User' END FROM Table"; Assert.AreEqual(expected, actual, "The wrong SQL was generated."); }