public void SelectClause_VariableAssignment() { using (StringReader reader = new StringReader( @"SELECT @id = p.ProductID FROM Production.Product p WHERE p.[Name] = 'Blade';" )) using (ITSQLTokenizer tokenizer = new TSQLTokenizer(reader)) { Assert.IsTrue(tokenizer.MoveNext()); TSQLSelectClause select = new TSQLSelectClauseParser().Parse(tokenizer); Assert.AreEqual(6, select.Tokens.Count); Assert.AreEqual(TSQLKeywords.FROM, tokenizer.Current.AsKeyword.Keyword); Assert.AreEqual(1, select.Columns.Count); TSQLSelectColumn column = select.Columns[0]; Assert.IsNull(column.ColumnAlias); Assert.AreEqual(TSQLExpressionType.VariableAssignment, column.Expression.Type); TSQLVariableAssignmentExpression assignmentExpression = column.Expression.AsVariableAssignment; Assert.AreEqual("=", assignmentExpression.Operator.Text); Assert.AreEqual("@id", assignmentExpression.Variable.Text); TSQLColumnExpression columnExpression = assignmentExpression.ValueExpression.AsColumn; Assert.AreEqual("p", columnExpression.TableReference.Single().AsIdentifier.Name); Assert.AreEqual("ProductID", columnExpression.Column.Name); } }
public TSQLVariableAssignmentExpression Parse( ITSQLTokenizer tokenizer, TSQLVariableExpression variable) { TSQLVariableAssignmentExpression opExpression = new TSQLVariableAssignmentExpression(); opExpression.Variable = variable.Variable; opExpression.Operator = tokenizer.Current.AsOperator; opExpression.Tokens.AddRange(variable.Tokens); opExpression.Tokens.Add(tokenizer.Current); while ( tokenizer.MoveNext() && ( tokenizer.Current.IsWhitespace() || tokenizer.Current.IsComment() )) { opExpression.Tokens.Add(tokenizer.Current); } TSQLExpression rightSide = new TSQLValueExpressionParser().Parse( tokenizer); // TODO: add test for nested operators like below // a + (b + (c + d)) if ( tokenizer.Current != null && tokenizer.Current.Type.In( TSQLTokenType.Operator)) { rightSide = new TSQLOperatorExpressionParser().Parse( tokenizer, rightSide); } opExpression.ValueExpression = rightSide; opExpression.Tokens.AddRange(rightSide.Tokens); return(opExpression); }