Skip to content

Commit

Permalink
added left join excluding rows check
Browse files Browse the repository at this point in the history
  • Loading branch information
alistair-singh committed Mar 30, 2015
1 parent c8f65dd commit 1ddd597
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
87 changes: 79 additions & 8 deletions samples/Sample.CodeAnalysis/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,87 @@ private static void Analyze(IEnumerable<IStatement> ast)
if (statement is SelectStatement)
{
var select = statement as SelectStatement;
if (select.FromList != null && select.FromList.Any())
{
var leftJoinAlias = select.FromList
.OfType<ReferenceFrom>()
.Where(x => x.Join == JoinType.LEFT)
.Select(x => x.Alias ?? x.Name.Identifier)
.ToList();
}
Test(select.FromList.OfType<ReferenceFrom>(), select.WhereClause);
}
if (statement is SelectInsertStatement)
{
var select = (statement as SelectInsertStatement).SelectStatement;
Test(select.FromList.OfType<ReferenceFrom>(), select.WhereClause);
}
if (statement is DeleteStatement)
{
var select = statement as DeleteStatement;
Test(select.FromList.OfType<ReferenceFrom>(), select.WhereClause);
}
}
}

private static void Test(IEnumerable<ReferenceFrom> from, IBooleanExpression whereClause)
{
if (from == null || !from.Any())
return;

var leftJoinAlias = from
.OfType<ReferenceFrom>()
.Where(x => x.Join == JoinType.LEFT)
.Select(x => x.Alias ?? x.Name.Identifier)
.ToList();

TreeVisitor visitor = new TreeVisitor(new AnalyzeVisitor(x =>
{
if (leftJoinAlias.Any(y => x.Identifier.StartsWith(y)))
{
Console.WriteLine("Where clause excluding left join rows '{0}', line {1} col {2}",
x.Identifier, x.Token.Line, x.Token.Column);
}
}));
if (whereClause != null)
whereClause.Accept(visitor);

}

class AnalyzeVisitor : ITreeVisitor
{
private Action<ReferenceExpression> _action;
public AnalyzeVisitor(Action<ReferenceExpression> action)
{
_action = action;
}

public void Visit(UnaryExpression unaryExpression) { }
public void Visit(GroupedExpression groupedExpression) { }
public void Visit(NullExpression nullExpression) { }
public void Visit(ConstantExpression constantExpression) { }
public void Visit(SelectStatementExpression selectStatementExpression) { }
public void Visit(ReferenceExpression referenceExpression) { _action(referenceExpression); }
public void Visit(FunctionCallExpression functionCallExpression) { }
public void Visit(BinaryOperationExpression binaryOperationExpression) { }
public void Visit(BooleanInSubqueryExpression booleanInSubqueryExpression) { }
public void Visit(BooleanNotExpresison booleanNotExpresison) { }
public void Visit(BooleanInListExpression booleanInListExpression) { }
public void Visit(BooleanBetweenExpression booleanBetweenExpression) { }
public void Visit(GroupedBooleanExpression groupedBooleanExpression) { }
public void Visit(BooleanRangeExpression booleanRangeExpression) { }
public void Visit(BooleanExistsExpression booleanExistsExpression) { }
public void Visit(BooleanNullCheckExpression booleanNullCheckExpression) { }
public void Visit(BooleanComparisonExpression booleanComparisonExpression) { }
public void Visit(BooleanBinaryExpression booleanBinaryExpression) { }
public void Visit(SetExpressionColumn setExpressionColumn) { }
public void Visit(StarColumn starColumn) { }
public void Visit(ExpressionColumn expressionColumn) { }
public void Visit(ReferenceFrom referenceFrom) { }
public void Visit(SubqueryFrom subqueryFrom) { }
public void Visit(WhileStatement whileStatement) { }
public void Visit(IfStatement ifStatement) { }
public void Visit(BlockStatement blockStatement) { }
public void Visit(UpdateStatement updateStatement) { }
public void Visit(SelectStatement selectStatement) { }
public void Visit(SelectInsertStatement selectInsertStatement) { }
public void Visit(ValuesInsertStatement valuesInsertStatement) { }
public void Visit(EmptyStatement emptyStatement) { }
public void Visit(DeleteStatement deleteStatement) { }
public void Visit(ValuesRow valuesRow) { }
public void Visit(Values values) { }
}
}
}
7 changes: 7 additions & 0 deletions samples/Sample.CodeAnalysis/Test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ WHERE t.Name = 'Person with a Name'
SELECT Name
,Age
FROM tb_Account

DELETE t
FROM tb_test1 t
left join tb_test2 b
ON t.AccountID = b.AccountID
WHERE t.col1 = 'some string'
AND b.col2 > 0
2 changes: 1 addition & 1 deletion src/tsqlc/Util/SqlWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

namespace tsqlc.Util
{
//TODO: Implement with visitor pattern
//TODO: Implement using TreeVisitor to walk tree
public class SqlWriter : ITreeVisitor
{
private TextWriter _writer;
Expand Down

0 comments on commit 1ddd597

Please sign in to comment.