Esempio n. 1
0
        public string ExtractIntoFunction()
        {
            var browser = new SolutionBrowserForm("");

            browser.ShowDialog();

            var destination = browser.DestinationItem;

            if (destination == null)
            {
                return(null);
            }

            var name = browser.GetObjectName();

            var function     = new CreateFunctionStatement();
            var returnSelect = (function.ReturnType = new SelectFunctionReturnType()) as SelectFunctionReturnType;

            returnSelect.SelectStatement = GetSelectStatementForQuery();
            function.Name = name.ToSchemaObjectName();

            var classFolder = destination.ProjectItems.AddFromTemplate("Procedure", name.UnQuote() + ".sql");
            var filePath    = classFolder.GetStringProperty("FullPath");

            File.WriteAllText(filePath, ScriptDom.GenerateTSql(function));

            classFolder.Open().Visible = true;

            return(GetCallingCode(function));
        }
 public override void ExplicitVisit(CreateFunctionStatement func)
 {
     if (IsSupportedForCurrentType(func.GetType()))
     {
         Name = func.Name;
     }
 }
Esempio n. 3
0
        public string ExtractIntoFunction()
        {
            var browser = new SolutionBrowserForm("");
            browser.ShowDialog();

            var destination = browser.DestinationItem;
            if (destination == null)
                return null;
            
            var name = browser.GetObjectName();           
            
            var function = new CreateFunctionStatement();
            var returnSelect = (function.ReturnType = new SelectFunctionReturnType()) as SelectFunctionReturnType;
            returnSelect.SelectStatement = GetSelectStatementForQuery();
            function.Name = name.ToSchemaObjectName();
            
            var classFolder = destination.ProjectItems.AddFromTemplate("Procedure", name.UnQuote() + ".sql");
            var filePath = classFolder.GetStringProperty("FullPath");
            
            File.WriteAllText(filePath, ScriptDom.GenerateTSql(function));

            classFolder.Open().Visible = true;
            
            return GetCallingCode(function);

       }
Esempio n. 4
0
        public override void Visit(CreateFunctionStatement node)
        {
            base.Visit(node);

            CheckFirstStatementInBatch(node, "CREATE FUNCTION");
            CheckLastStatementInBatch(node, "CREATE FUNCTION");
        }
Esempio n. 5
0
        internal static TestFunction LoadFunctionMetatadaFromDDL(TestSchema schema, string ddl)
        {
            TSqlFragment            sqlF = ScriptDomFacade.Parse(ddl);
            CreateFunctionStatement stmt_CreateFunction = (CreateFunctionStatement)((TSqlScript)sqlF).Batches[0].Statements[0];

            string schemaName   = stmt_CreateFunction.Name.SchemaIdentifier.Dequote();
            string functionName = stmt_CreateFunction.Name.BaseIdentifier.Dequote();

            if (stmt_CreateFunction.StatementList != null)
            {
                // 'hlsyssec_query_agentsystemacl'
                return(LoadMultiStatementTableValuedFunction(schemaName, functionName, stmt_CreateFunction));
            }

            // inline 'hlsur_query_surveyresults'
            string functionBody = GetFragmentStreamAsText(((SelectFunctionReturnType)stmt_CreateFunction.ReturnType).SelectStatement);

            var function = new TestFunction(schemaName, functionName, functionBody, f => LoadFunctionOutputColumns(schema, f, stmt_CreateFunction));

            foreach (ProcedureParameter prm in stmt_CreateFunction.Parameters)
            {
                string parameterName   = prm.VariableName.Dequote();
                var    parameterDbType = ProcedureGenerator.ResolveToDbDataType(prm.DataType);

                function.AddParameter(parameterName, parameterDbType, true);
            }

            return(function);
        }
Esempio n. 6
0
        private static string ChangeCreateToAlter(CreateFunctionStatement function, string wholeScript)
        {
            //get part of script we are interested in...
            var subScript = wholeScript.Substring(function.StartOffset, function.FragmentLength);

            IList <ParseError> errors;
            var fragment = new TSql130Parser(false).Parse(new StringReader(subScript), out errors);

            bool haveCreate = false;
            var  output     = new StringBuilder();

            foreach (var token in fragment.ScriptTokenStream)
            {
                if (!haveCreate && token.TokenType == TSqlTokenType.Create)
                {
                    var alterToken = new TSqlParserToken(TSqlTokenType.Alter, "alter");
                    output.Append(alterToken.Text);
                    haveCreate = true;
                    continue;
                }

                output.Append(token.Text);
            }

            return(output.ToString());
        }
Esempio n. 7
0
        private static string BuildIfNotExistsStatementsInlineFunction(CreateFunctionStatement function)
        {
            var generateIfExists =
                string.Format("if object_id('{0}') is null\r\nbegin\r\n execute sp_executeSql N' create function {0}() \r\n RETURNS TABLE as RETURN SELECT 1 as a' \r\nEND;",
                              function.Name.ToQuotedString());

            return(generateIfExists);
        }
Esempio n. 8
0
        private static string BuildIfNotExistsStatements(CreateFunctionStatement function)
        {
            var generateIfExists =
                string.Format("if object_id('{0}') is null\r\nbegin\r\n execute sp_executeSql N' create function {0}() \r\n RETURNS @t TABLE (stringValue VARCHAR(128))\r\n as \r\nbegin\r\ninsert into @t values(''ee'')\r\n return\r\nend';\r\nend",
                              function.Name.ToQuotedString());

            return(generateIfExists);
        }
Esempio n. 9
0
        public override void Visit(CreateFunctionStatement node)
        {
            base.Visit(node);

            var name = node.Name.SchemaIdentifier.Value + '.' + node.Name.BaseIdentifier.Value;

            functions.Add(name, node);
        }
Esempio n. 10
0
        private static string BuildIfNotExistsStatements(CreateFunctionStatement function)
        {
            var generateIfExists =
                string.Format("if object_id('{0}') is null\r\nbegin\r\n execute sp_executeSql N' create function {0}() \r\n RETURNS @t TABLE (stringValue VARCHAR(128))\r\n as \r\nbegin\r\ninsert into @t values(''ee'')\r\n return\r\nend';\r\nend",
                    function.Name.ToQuotedString());

            return generateIfExists;
        }
Esempio n. 11
0
        private List <Statement> BuildStatements(string filePath)
        {
            var statements = _planParser.GetStatements(File.ReadAllText(filePath));

            var f = new CreateFunctionStatement();

            return(statements);
        }
Esempio n. 12
0
        private static string BuildIfNotExistsStatementsInlineFunction(CreateFunctionStatement function)
        {

            var generateIfExists =
                string.Format("if object_id('{0}') is null\r\nbegin\r\n execute sp_executeSql N' create function {0}() \r\n RETURNS TABLE as RETURN SELECT 1 as a' \r\nEND;",
                    function.Name.ToQuotedString());

            return generateIfExists;
        }
Esempio n. 13
0
        private string GetCallingCode(CreateFunctionStatement function)
        {
            var callingSelect = new SelectStatement();
            var spec = (callingSelect.QueryExpression = new QuerySpecification()) as QuerySpecification;
            spec.FromClause = new FromClause();
            spec.FromClause.TableReferences.Add(new SchemaObjectFunctionTableReference()
            {
                SchemaObject = function.Name
            });

            spec.SelectElements.Add(new SelectStarExpression());

            return ScriptDom.GenerateTSql(spec);
        }
Esempio n. 14
0
        private string GetCallingCode(CreateFunctionStatement function)
        {
            var callingSelect = new SelectStatement();
            var spec          = (callingSelect.QueryExpression = new QuerySpecification()) as QuerySpecification;

            spec.FromClause = new FromClause();
            spec.FromClause.TableReferences.Add(new SchemaObjectFunctionTableReference()
            {
                SchemaObject = function.Name
            });

            spec.SelectElements.Add(new SelectStarExpression());

            return(ScriptDom.GenerateTSql(spec));
        }
Esempio n. 15
0
        public override void ExplicitVisit(CreateFunctionStatement node)
        {
            var target = new AlterFunctionStatement();

            target.Name = node.Name;
            target.Parameters.AddRange(node.Parameters);
            target.ReturnType = node.ReturnType;
            target.Options.AddRange(node.Options);
            target.StatementList   = node.StatementList;
            target.MethodSpecifier = node.MethodSpecifier;
            target.OrderHint       = node.OrderHint;
            //base.ExplicitVisit(node);
            this._ReplaceSource = node;
            this._ReplaceTarget = target;
        }
 public override void Visit(CreateFunctionStatement node)
 {
     base.Visit(node);
     _statements.Add(node);
 }
 public override void ExplicitVisit(CreateFunctionStatement node)
 {
     IsFunction = true;
 }
Esempio n. 18
0
 public override void Visit(CreateFunctionStatement node) { this.action(node); }
Esempio n. 19
0
 protected override void VisitCreateFunctionStatement(CreateFunctionStatement statement)
 {
     VisitProcedureAndBodyParameters(statement, false, statement.CheckIfNotExists);
 }
 public override void ExplicitVisit(CreateFunctionStatement node)
 {
     FunctionName = node.Name.BaseIdentifier.Value;
     ParseParameters(node.Parameters);
     base.ExplicitVisit(node);
 }
Esempio n. 21
0
 protected override void VisitCreateFunctionStatement(CreateFunctionStatement statement)
 {
     this.CreateOrReplaceFunction(statement);
 }
 public override void ExplicitVisit(CreateFunctionStatement fragment)
 {
     _fragments.Add(fragment);
 }
Esempio n. 23
0
 public override void ExplicitVisit(CreateFunctionStatement func)
 {
     if (IsSupportedForCurrentType(func.GetType()))
     {
         Name = func.Name;
     }
 }
Esempio n. 24
0
        private static TestFunction LoadMultiStatementTableValuedFunction(string schemaName, string functionName, CreateFunctionStatement stmt_CreateFunction)
        {
            if (stmt_CreateFunction.ReturnType is TableValuedFunctionReturnType tvfReturnType)
            {
                TestFunction function = new TestFunction(schemaName, functionName, "", (x) =>
                {
                });

                //tvfReturnType.DeclareTableVariableBody.VariableName
                var tableBody = tvfReturnType.DeclareTableVariableBody;

                foreach (ColumnDefinition col in tableBody.Definition.ColumnDefinitions)
                {
                    string columnName   = col.ColumnIdentifier.Dequote();
                    var    columnDbType = ProcedureGenerator.ResolveToDbDataType(col.DataType);

                    function.AddFunctionColumn(columnName, columnDbType, true);
                }

                return(function);
            }
            throw new NotImplementedException(stmt_CreateFunction.ReturnType.WhatIsThis());
        }
Esempio n. 25
0
 public override void ExplicitVisit(CreateFunctionStatement node)
 {
     this.HandleFunction(node);
 }
Esempio n. 26
0
 protected abstract void VisitCreateFunctionStatement(CreateFunctionStatement statement);
Esempio n. 27
0
        private List<Statement> BuildStatements(string filePath)
        {
            var statements = _planParser.GetStatements(File.ReadAllText(filePath));

            var f = new CreateFunctionStatement();

            return statements;
        }
Esempio n. 28
0
        private static string ChangeCreateToAlter(CreateFunctionStatement function, string wholeScript)
        {
            //get part of script we are interested in...
            var subScript = wholeScript.Substring(function.StartOffset, function.FragmentLength);

            IList<ParseError> errors;
            var fragment = new TSql130Parser(false).Parse(new StringReader(subScript), out errors);

            bool haveCreate = false;
            var output = new StringBuilder();

            foreach (var token in fragment.ScriptTokenStream)
            {
                if (!haveCreate && token.TokenType == TSqlTokenType.Create)
                {
                    var alterToken = new TSqlParserToken(TSqlTokenType.Alter, "alter");
                    output.Append(alterToken.Text);
                    haveCreate = true;
                    continue;
                }

                output.Append(token.Text);
            }

            return output.ToString();
        }
Esempio n. 29
0
 private static void LoadFunctionOutputColumns(TestSchema schema, TestFunction function_source, CreateFunctionStatement stmt_CreateFunction)
 {
     ProcedureGenerator.LoadFunctionOutputColumns(schema, function_source, function_source.FunctionBodyScript, (col) =>
     {
         function_source.AddFunctionColumn(col.OutputColumnName, col.ColumnType.ColumnDbType, col.ColumnType.AllowNull);
     });
 }
Esempio n. 30
0
 public override void Visit(CreateFunctionStatement node)
 {
     Functions.Add(node);
 }
Esempio n. 31
0
 public override void Visit(CreateFunctionStatement node)
 {
     Statements.Add(node);
 }
Esempio n. 32
0
 protected override void VisitCreateFunctionStatement(CreateFunctionStatement statement)
 {
     throw new NotImplementedException();
 }