private SelectStatement ResolveStatement(ReadOnlySpan <TSQLToken> tokens, ref int fileIndex, CompilerContext context) { statement = new SelectStatement(); SkipSelectPrequelStatements(tokens, ref fileIndex, context); statement.Expression = DetermineSourceColumns(tokens, ref fileIndex, context); ResolveIntoClause(tokens, ref fileIndex, context); var objects = StatementResolveHelper.ResolveFromStatement(tokens, ref fileIndex, context); AddObjectsToContext(objects, context); Beautifier.BeautifyColumns(statement.Expression, context); AddSynonymousObjects(objects); StatementResolveHelper.ResolveWhereStatement(tokens, ref fileIndex, context); ResolveGroupByClause(tokens, ref fileIndex, context); ResolveOrderByClause(tokens, ref fileIndex, context); //Resolve FOR-CLAUSE ResolveUnionclause(tokens, ref fileIndex, context); PopObjectsFromContextStack(context); statement.Expression.Name = "SELECT"; return(statement); }
public DataManipulation Resolve(ReadOnlySpan <TSQLToken> tokens, ref int fileIndex, CompilerContext context) { manipulation = new DataManipulation(); fileIndex++; //skip "merge" //skip top expression SkipTopExpression(tokens, ref fileIndex, context); if (tokens[fileIndex].Text.ToLower().Equals("into")) { fileIndex++; } targetObject = StatementResolveHelper.ResolveDatabaseObject(tokens, ref fileIndex, context); context.AddDatabaseObjectToCurrentContext(targetObject); if (!tokens[fileIndex].Text.ToLower().Equals("using")) { throw new InvalidSqlException("Trying to resolve a merge-statement without using keyword"); } var source = ResolveUsingStatement(tokens, ref fileIndex, context); context.AddDatabaseObjectToCurrentContext(source); if (!tokens[fileIndex].Text.Equals("on", StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidSqlException("Expected 'ON' keyword when resolving a 'MERGE'-statement"); } fileIndex++; //skip 'on' SearchConditionResolver.Resolve(tokens, ref fileIndex, context); while (tokens[fileIndex].Text.Equals("when", StringComparison.InvariantCultureIgnoreCase)) { ResolveWhenExpression(tokens, ref fileIndex, context); } var beautified = new List <Expression>(); foreach (var exp in manipulation.Expressions) { beautified.Add(Beautifier.BeautifyColumns(exp, context)); } manipulation.Expressions = beautified; while (!tokens[fileIndex].Text.ToLower().Equals(";")) { fileIndex++; if (fileIndex == tokens.Length) { throw new InvalidSqlException("Trying to resolve a merge-statement without proper ';' determination"); } } fileIndex++; //skip ';' context.CurrentDatabaseObjectContext.Pop(); return(manipulation); }
public DataManipulation Resolve(ReadOnlySpan <TSQLToken> tokens, ref int fileIndex, CompilerContext context) { manipulation = new DataManipulation(); fileIndex++; //skip 'update' var targetObject = StatementResolveHelper.ResolveDatabaseObject(tokens, ref fileIndex, context, true); fileIndex++; //skip 'set' do { //Resolves the target object. Note that this target can be an alias that is resolved later in the FROM statement var target = StatementResolveHelper.ResolveExpression(tokens, ref fileIndex, context); AddTargetObject(target, targetObject); fileIndex++; //skip '=' var source = StatementResolveHelper.ResolveExpression(tokens, ref fileIndex, context); //resolve source column target.ChildExpressions.Add(source); manipulation.Expressions.Add(target); if (fileIndex < tokens.Length && tokens[fileIndex].Text.Equals(",")) { fileIndex++; //skip ',' continue; } else { break; } } while (true); var objects = StatementResolveHelper.ResolveFromStatement(tokens, ref fileIndex, context); AddObjectsToContext(objects, context); if (objects.Count > 1) { targetObject = AssignRealTarget(objects, targetObject); var targetSynonymous = new Expression(ExpressionType.COLUMN) { Name = Beautifier.EnhanceNotation(targetObject, InternalConstants.WHOLE_OBJECT_SYNONYMOUS), WholeObjectSynonymous = true }; foreach (var dbo in objects) { if (!dbo.Type.Equals(DatabaseObjectType.REAL) || dbo.Equals(targetObject)) { continue; } var sourceSynonymous = new Expression(ExpressionType.COLUMN) { Name = Beautifier.EnhanceNotation(dbo, InternalConstants.WHOLE_OBJECT_SYNONYMOUS), WholeObjectSynonymous = true }; targetSynonymous.ChildExpressions.Add(sourceSynonymous); manipulation.Expressions.Add(targetSynonymous); } } var beautified = new List <Expression>(); foreach (var expr in manipulation.Expressions) { beautified.Add(Beautifier.BeautifyColumns(expr, context)); } manipulation.Expressions = beautified; StatementResolveHelper.ResolveWhereStatement(tokens, ref fileIndex, context); PopObjectsFromContextStack(context); return(manipulation); }
public DataManipulation Resolve(ReadOnlySpan <TSQLToken> tokens, ref int fileIndex, CompilerContext context) { manipulation = new DataManipulation(); fileIndex++; //skip 'insert' //TODO Resolve TOP Expression if (tokens[fileIndex].Text.ToLower().Equals("into")) { fileIndex++; //skip 'into' } targetObject = StatementResolveHelper.ResolveDatabaseObject(tokens, ref fileIndex, context, true); DetermineTargetColumns(tokens, ref fileIndex, context); //TODO Resolve Table Hint if (tokens[fileIndex].Text.Equals("values", StringComparison.InvariantCultureIgnoreCase)) { fileIndex += 2; //skip 'values (' DetermineSourceObjects(tokens, ref fileIndex, context); } else if (tokens[fileIndex].Text.Equals("select", StringComparison.InvariantCultureIgnoreCase)) { var selectStatement = new SelectStatementResolver().Resolve(tokens, ref fileIndex, context); sources = selectStatement.ChildExpressions; } if (targets.Count == 0) { for (int index = 0; index < sources.Count; index++) { Expression resolvedSource; if (sources[index].Type.Equals(ExpressionType.COMPLEX) || sources[index].Type.Equals(ExpressionType.CONSTANT)) { continue; } else if (sources[index].Type.Equals(ExpressionType.ALIAS)) { resolvedSource = sources[index].ChildExpressions[0]; } else { resolvedSource = sources[index]; } var singleManipulation = new Expression(ExpressionType.COLUMN) { Name = Beautifier.EnhanceNotation(targetObject, InternalConstants.UNRELATED_COLUMN_NAME) }; singleManipulation.ChildExpressions.Add(resolvedSource); manipulation.Expressions.Add(singleManipulation); } } else if (StatementResolveHelper.HaveEqualAmountOfRealExpression(sources, targets)) { for (int index = 0; index < targets.Count; index++) { if (!sources[index].Type.Equals(ExpressionType.COLUMN) && !sources[index].Type.Equals(ExpressionType.SCALAR_FUNCTION)) { continue; } var singleManipulation = new Expression(ExpressionType.COLUMN) { Name = targets[index].Name }; singleManipulation.ChildExpressions.Add(sources[index]); manipulation.Expressions.Add(singleManipulation); } } else { throw new InvalidSqlException("Amount of targets does not match the number of sources"); } if (fileIndex < tokens.Length && tokens[fileIndex].Text.Equals(";")) { fileIndex++; } var beautified = new List <Expression>(); foreach (var exp in manipulation.Expressions) { beautified.Add(Beautifier.BeautifyColumns(exp, context)); } manipulation.Expressions = beautified; return(manipulation); }