ViewStatementBody

We found 10 examples in language CSharp for this search. You will see 28 fragments of code.

Other methods

////This file was generated 6/24/2017 12:42:27 AM
using System;
using System.Collections.Generic;

namespace SqlT.SqlTDom
{
    [Serializable()]
    public class AlterViewStatement : ViewStatementBody, ISqlTDomAlterStatement
    {
    }
}

////This file was generated 6/24/2017 12:42:27 AM
using System;
using System.Collections.Generic;

namespace SqlT.SqlTDom
{
    [Serializable()]
    public class CreateViewStatement : ViewStatementBody, ISqlTDomCreateStatement
    {
    }
}

////This file was generated 6/24/2017 12:42:27 AM
using System;
using System.Collections.Generic;

namespace SqlT.SqlTDom
{
    [Serializable()]
    public abstract class ViewStatementBody : TSqlStatement, ISqlTDomStatement
    {
        public SchemaObjectName SchemaObjectName
        {
            get;
            set;
        }

        public IList<Identifier> Columns
        {
            get;
            set;
        }

        public IList<ViewOption> ViewOptions
        {
            get;
            set;
        }

        public SelectStatement SelectStatement
        {
            get;
            set;
        }

        public bool WithCheckOption
        {
            get;
            set;
        }
    }
}


        public void ProcessViewStatementBody(ViewStatementBody StatementBody)
        {
            TestViewReference(StatementBody.SchemaObjectName);
            new SelectStatementProcessor(_smells).Process(StatementBody.SelectStatement, "VW", true);
        }

        public override void Accept(SqlFragmentVisitor visitor) => visitor?.ExplicitVisit(this);


    public void Parse(String SQL) {
      TSql120Parser SqlParser = new TSql120Parser(false);

      IList<ParseError> parseErrors;
      TSqlFragment result = SqlParser.Parse(new StringReader(SQL),
                                            out parseErrors);

      TSqlScript SqlScript = result as TSqlScript;

      foreach (TSqlBatch sqlBatch in SqlScript.Batches) {
        foreach (TSqlStatement sqlStatement in sqlBatch.Statements) {
          ProcessViewStatementBody(sqlStatement);
        }
      }
    }


    private void ProcessViewStatementBody(TSqlStatement sqlStatement) {

      ViewStatementBody aViewStatementBody = (ViewStatementBody)sqlStatement;

        AddLogText("Columns:");
        foreach (Identifier aColumnIdentifier in aViewStatementBody.Columns) {
          AddLogText(string.Format("Column:{0}", aColumnIdentifier.Value));
          aColumnInfoList.ColumnList.Add(new ColumnInfo { Alias = aColumnIdentifier.Value });
        }

        AddLogText("");
        AddLogText("QueryExpression SelectElements:");
        SelectStatement aSelectStatement = aViewStatementBody.SelectStatement;
        QueryExpression aQueryExpression = aSelectStatement.QueryExpression;
        if (aQueryExpression.GetType() == typeof(QuerySpecification)) {
          QuerySpecification aQuerySpecification = (QuerySpecification)aQueryExpression;
          int aSelectElementID = 0;
          foreach (SelectElement aSelectElement in aQuerySpecification.SelectElements) {
            if (aSelectElement.GetType() == typeof(SelectScalarExpression)) {
              SelectScalarExpression aSelectScalarExpression = (SelectScalarExpression)aSelectElement;

              string identStr = string.Empty;
              IdentifierOrValueExpression aIdentifierOrValueExpression =
                  aSelectScalarExpression.ColumnName;
              if (aIdentifierOrValueExpression != null) {
                if (aIdentifierOrValueExpression.ValueExpression == null) {
                  AddLogText(string.Format("Identifier={0}",
                      aIdentifierOrValueExpression.Identifier.Value));
                  identStr = aIdentifierOrValueExpression.Identifier.Value;
                } else {
                  AddLogText("Expression");
                }
              }
              aColumnInfoList.AddIfNeeded(aSelectElementID, identStr);

              ScalarExpression aScalarExpression = aSelectScalarExpression.Expression;
              PrintSelectScalarExperssionRecurse(aSelectElementID, aScalarExpression);
            } else {
              aColumnInfoList.AddIfNeeded(aSelectElementID,
                  "Error, something else than SelectScalarExpression found");
              AddLogText("We only support SelectScalarExpression.");
            }
            aSelectElementID = aSelectElementID + 1;
            AddLogText("");
          }
          AddLogText("");
          AddLogText("Table References:");
          FromClause aFromClause = aQuerySpecification.FromClause;
          foreach (TableReference aTableReference in aFromClause.TableReferences) {
            PrintTableReferenceRecurse(aTableReference);
          }
        }
        aColumnInfoList.FillEmptyAlias();

    }//function

    private string MultiPartIdentifierToString(int aSelectElementID, MultiPartIdentifier aMultiPartIdentifier) {
      String res = String.Empty;
      foreach (Identifier aIdentifier in aMultiPartIdentifier.Identifiers) {
        if (String.IsNullOrEmpty(res)) {
          res = aIdentifier.Value;
        } else {
          res = res + "." + aIdentifier.Value;
        }
      }
      aColumnInfoList.AddRefereceIdentifier(aSelectElementID, aMultiPartIdentifier);
      return res;
    }

    private void PrintSelectScalarExperssionRecurse(int aSelectElementID, ScalarExpression aScalarExpression) {
      aColumnInfoList.AddExpression(aSelectElementID, aScalarExpression);
      if (aScalarExpression.GetType() == typeof(ColumnReferenceExpression)) {
        ColumnReferenceExpression aColumnReferenceExpression = (ColumnReferenceExpression)aScalarExpression;
        AddLogText(string.Format("ColumnType={0}", aColumnReferenceExpression.ColumnType));
        MultiPartIdentifier aMultiPartIdentifier = aColumnReferenceExpression.MultiPartIdentifier;
        AddLogText(string.Format("Reference Identifier={0}",
            MultiPartIdentifierToString(aSelectElementID, aMultiPartIdentifier)));
      } else if (aScalarExpression.GetType() == typeof(ConvertCall)) {
        ConvertCall aConvertCall = (ConvertCall)aScalarExpression;
        ScalarExpression aScalarExpressionParameter = aConvertCall.Parameter;
        PrintSelectScalarExperssionRecurse(aSelectElementID, aScalarExpressionParameter);
      } else {
        AddLogText(String.Format("Not supported Expression:{0}", aScalarExpression.GetType()));
      }
    }

    private void PrintTableReferenceRecurse(TableReference aTableReference) {
      if (aTableReference.GetType() == typeof(NamedTableReference)) {
        NamedTableReference aNamedTableReference = (NamedTableReference)aTableReference;
        Identifier aAliasIdentifier = aNamedTableReference.Alias;
        SchemaObjectName aSchemaObjectName = aNamedTableReference.SchemaObject;
        AddLogText(string.Format("Table Reference Schema.Base={0}.{1}",
            (aSchemaObjectName.SchemaIdentifier != null) ? aSchemaObjectName.SchemaIdentifier.Value : "",
            (aSchemaObjectName.BaseIdentifier != null) ? aSchemaObjectName.BaseIdentifier.Value : "")
            );
        //foreach (Identifier aSchemaObjectNameIdentifier in aSchemaObjectName.Identifiers)
        //{
        //    AddText(string.Format("Table Reference Identifier={0}", aSchemaObjectNameIdentifier.Value));
        //}
        if (aAliasIdentifier != null) {
          AddLogText(string.Format("Table Reference Alias:{0}", aAliasIdentifier.Value));
        }
        aColumnInfoList.AddTableReference(aSchemaObjectName, aAliasIdentifier);
      }
      if (aTableReference.GetType() == typeof(QualifiedJoin)) {
        QualifiedJoin aQualifiedJoin = (QualifiedJoin)aTableReference;
        AddLogText(string.Format("Table Reference QualifiedJoinType ={0}", aQualifiedJoin.QualifiedJoinType));
        PrintTableReferenceRecurse(aQualifiedJoin.FirstTableReference);
        PrintTableReferenceRecurse(aQualifiedJoin.SecondTableReference);
      }
      if (aTableReference.GetType() == typeof(JoinTableReference)) {
        JoinTableReference aJoinTableReference = (JoinTableReference)aTableReference;
        PrintTableReferenceRecurse(aJoinTableReference.FirstTableReference);
        PrintTableReferenceRecurse(aJoinTableReference.SecondTableReference);
      }
    }

    private void AddLogText(string aLine) {
      TheData.AppendLine(aLine);
    }


        /// <inheritdoc />
        public override void ExplicitVisit(FunctionCall node)
        {
            base.ExplicitVisit(node);

            // NOTE: Remove known built-in function arguments, e.g. DATEADD(month, 1, t.Column) should only report t.Column
            if (node.FunctionName?.QuoteType == QuoteType.NotQuoted && ParametersToIgnore.HasIgnoredParameters(node.FunctionName.Value, out var indexes))
            {
                foreach (var idx in indexes!)
                {
                    if (node.Parameters[idx] is ColumnReferenceExpression columnReference)
                        ColumnReferences.Remove(columnReference);
                }
            }
        }

        /// <inheritdoc />
        public override void ExplicitVisit(ColumnReferenceExpression node)
        {
            if (node.MultiPartIdentifier != null)
                ColumnReferences.Add(node);

            base.ExplicitVisit(node);
        }

        /// <inheritdoc />
        public override void ExplicitVisit(CreateOrAlterViewStatement node)
        {
            ViewName = node.SchemaObjectName;
            Body = node;

            base.ExplicitVisit(node);
        }

        /// <inheritdoc />
        public override void ExplicitVisit(CreateViewStatement node)
        {
            ViewName = node.SchemaObjectName;
            Body = node;

            base.ExplicitVisit(node);
        }

        /// <inheritdoc />
        public override void ExplicitVisit(NamedTableReference node)
        {
            NamedTableReferences.Add(node);

            if (connection.IsView(node.SchemaObject))
                Views.Add(node);
            else
                Tables.Add(node);

            base.ExplicitVisit(node);
        }

        /// <inheritdoc />
        public override void ExplicitVisit(QuerySpecification node) // TODO: Handle UNION
        {
            Query ??= node;

            // TODO: Process nested queries?
            var previous = currentQuery;
            currentQuery = node;

            base.ExplicitVisit(node);

            currentQuery = previous;
        }


		// Token: 0x06002984 RID: 10628 RVA: 0x001C78AC File Offset: 0x001C5AAC
		public override void AcceptChildren(TSqlFragmentVisitor visitor)
		{
			if (this.SchemaObjectName != null)
			{
				this.SchemaObjectName.Accept(visitor);
			}
			int i = 0;
			int count = this.Columns.Count;
			while (i < count)
			{
				this.Columns[i].Accept(visitor);
				i++;
			}
			int j = 0;
			int count2 = this.ViewOptions.Count;
			while (j < count2)
			{
				this.ViewOptions[j].Accept(visitor);
				j++;
			}
			if (this.SelectStatement != null)
			{
				this.SelectStatement.Accept(visitor);
			}
			base.AcceptChildren(visitor);
		}


        public void FillEmptyAlias()
        {
            foreach (ColumnInfo aColumnInfo in ColumnList)
            {
                if (string.IsNullOrWhiteSpace(aColumnInfo.Alias))
                {
                    aColumnInfo.Alias = aColumnInfo.TableColumnName;
                }
            }
        }

        public void AddColumnIfNeeded(int aSelectElementID, string aIdentifier)
        {
            if (ColumnList.Count > aSelectElementID)
            {
                //Insert error
            }
            else
            {
                ColumnList.Add(new ColumnInfo { Alias = aIdentifier });
            }
        }

        public void AddRefereceIdentifier(int aSelectElementID, MultiPartIdentifier aMultiPartIdentifier)
        {
            if (ColumnList.Count > aSelectElementID)
            {
                ColumnInfo aColumnInfo = ColumnList[aSelectElementID];
                int aIdentIdx = 0;
                foreach (Identifier aIdentifier in aMultiPartIdentifier.Identifiers)
                {
                    if (aMultiPartIdentifier.Identifiers.Count == 2)
                    {
                        if (aIdentIdx == 0)
                            aColumnInfo.TableAlias = aIdentifier.Value;
                        if (aIdentIdx == 1)
                            aColumnInfo.TableColumnName = aIdentifier.Value;
                    }
                    if (aMultiPartIdentifier.Identifiers.Count == 3)
                    {
                        if (aIdentIdx == 0)
                            aColumnInfo.TableSchema = aIdentifier.Value;
                        if (aIdentIdx == 1)
                            aColumnInfo.TableName = aIdentifier.Value;
                        if (aIdentIdx == 2)
                            aColumnInfo.TableColumnName = aIdentifier.Value;
                    }
                    aIdentIdx = aIdentIdx + 1;
                }
            }
            else
            {
                AddLogText(string.Format("Error: column with idx:{0} not found.", aSelectElementID));
            }
        }

        public void AddTableReference(SchemaObjectName aSchemaObjectName, Identifier aAliasIdentifier)
        {
            if (ColumnList.Count > 0)
            {
                foreach (ColumnInfo aColumnInfo in ColumnList)
                {
                    if (aAliasIdentifier != null && aColumnInfo.TableAlias.ToLower() == aAliasIdentifier.Value.ToLower())
                    {
                        if (aSchemaObjectName.ServerIdentifier != null)
                            aColumnInfo.TableServer = aSchemaObjectName.ServerIdentifier.Value;
                        if (aSchemaObjectName.DatabaseIdentifier != null)
                            aColumnInfo.TableDatabase = aSchemaObjectName.DatabaseIdentifier.Value;
                        if (aSchemaObjectName.SchemaIdentifier != null)
                            aColumnInfo.TableSchema = aSchemaObjectName.SchemaIdentifier.Value;
                        if (aSchemaObjectName.BaseIdentifier != null)
                            aColumnInfo.TableName = aSchemaObjectName.BaseIdentifier.Value;
                    }
                    else if ((aAliasIdentifier == null) &&
                             (aSchemaObjectName.BaseIdentifier != null) &&
                             (aSchemaObjectName.BaseIdentifier.Value.ToLower() == aColumnInfo.TableAlias.ToLower()))
                    {
                        if (aSchemaObjectName.ServerIdentifier != null)
                            aColumnInfo.TableServer = aSchemaObjectName.ServerIdentifier.Value;
                        if (aSchemaObjectName.DatabaseIdentifier != null)
                            aColumnInfo.TableDatabase = aSchemaObjectName.DatabaseIdentifier.Value;
                        if (aSchemaObjectName.SchemaIdentifier != null)
                            aColumnInfo.TableSchema = aSchemaObjectName.SchemaIdentifier.Value;
                        if (aSchemaObjectName.BaseIdentifier != null)
                            aColumnInfo.TableName = aSchemaObjectName.BaseIdentifier.Value;
                    }
                }
            }
            else
            {
                AddLogText("Could not add table reference becouse there are no Columns!");
            }
        }

        private void ProcessViewStatementBody(TSqlStatement sqlStatement)
        {
            if ((sqlStatement.GetType() == typeof(CreateViewStatement)) ||
                (sqlStatement.GetType() == typeof(AlterViewStatement)))
            {
                ViewStatementBody aViewStatementBody = (ViewStatementBody) sqlStatement;
                AddLogText("Columns meta data:");
                foreach (Identifier aColumnIdentifier in aViewStatementBody.Columns)
                {
                    AddLogText(string.Format("Column:{0}", aColumnIdentifier.Value));
                    ColumnList.Add(new ColumnInfo { Alias = aColumnIdentifier.Value });
                }

                AddLogText("");
                AddLogText("QueryExpression SelectElements:");
                SelectStatement aSelectStatement = aViewStatementBody.SelectStatement;
                QueryExpression aQueryExpression = aSelectStatement.QueryExpression;
                if (aQueryExpression.GetType() == typeof(QuerySpecification))
                {
                    QuerySpecification aQuerySpecification = (QuerySpecification) aQueryExpression;
                    int aSelectElementID = 0;
                    foreach (SelectElement aSelectElement in aQuerySpecification.SelectElements)
                    {
                        if (aSelectElement.GetType() == typeof(SelectScalarExpression))
                        {
                            SelectScalarExpression aSelectScalarExpression = (SelectScalarExpression) aSelectElement;

                            string identStr = string.Empty;
                            IdentifierOrValueExpression aIdentifierOrValueExpression =
                                aSelectScalarExpression.ColumnName;
                            if (aIdentifierOrValueExpression != null)
                            {
                                if (aIdentifierOrValueExpression.ValueExpression == null)
                                {
                                    AddLogText(string.Format("Identifier={0}",
                                        aIdentifierOrValueExpression.Identifier.Value));
                                    identStr = aIdentifierOrValueExpression.Identifier.Value;
                                }
                                else
                                {
                                    AddLogText("Value Expression found!");
                                }
                            }
                            AddColumnIfNeeded(aSelectElementID, identStr);

                            ScalarExpression aScalarExpression = aSelectScalarExpression.Expression;
                            PrintSelectScalarExperssionRecurse(aSelectElementID, aScalarExpression);
                        }
                        else
                        {
                            AddColumnIfNeeded(aSelectElementID, "Error, something else than SelectScalarExpression found");
                            AddLogText("We only support SelectScalarExpression.");
                        }
                        aSelectElementID = aSelectElementID + 1;
                        AddLogText("");
                    }
                    AddLogText("");
                    AddLogText("Table References:");
                    FromClause aFromClause = aQuerySpecification.FromClause;
                    foreach (TableReference aTableReference in aFromClause.TableReferences)
                    {
                        PrintTableReferenceRecurse(aTableReference);
                    }
                }
                FillEmptyAlias();
            }
            else
            {
                AddLogText("This is not a view statement, but a:" + sqlStatement.ToString());
            }
        }

        private string MultiPartIdentifierToString(int aSelectElementID, MultiPartIdentifier aMultiPartIdentifier)
        {
            String res = String.Empty;
            foreach (Identifier aIdentifier in aMultiPartIdentifier.Identifiers)
            {
                if (String.IsNullOrEmpty(res))
                {
                    res = aIdentifier.Value;
                }
                else
                {
                    res = res + "." + aIdentifier.Value;
                }
            }
            AddRefereceIdentifier(aSelectElementID, aMultiPartIdentifier);
            return res;
        }

        

        [SqlFunction(
            FillRowMethodName = "FindColumns", 
            TableDefinition = "Alias nvarchar(500), TableColumnName nvarchar(500), TableAlias nvarchar(500), TableName nvarchar(500)")]
        public static IEnumerable ParseView(string tsql)
        {
            LinkedList<ViewColumn> viewColumns = new LinkedList<ViewColumn>();
            viewColumns.AddLast(new ViewColumn()
            {
                Alias = new SqlString("Alias"),
                TableName = new SqlString("TableName"),
                TableAlias = new SqlString("TableAlias"),
                TableColumnName = new SqlString("TableColumnName")
            });

            return viewColumns;
        }


        public static void FindColumns(object objColumns, out SqlString alias, out SqlString tableColumnName, out SqlString tableAlias, out SqlString tableName)
        {
            ViewColumn viewColumn = (ViewColumn)objColumns;
            alias = viewColumn.Alias;
            tableColumnName = viewColumn.TableColumnName;
            tableAlias = viewColumn.TableAlias;
            tableName = viewColumn.TableName;
        }


        private static IEnumerable<ViewColumn> ParseViewStatementBody(ViewStatementBody sqlStatement)
        {
            LinkedList<ViewColumn> result = new LinkedList<ViewColumn>();
            SelectStatement aSelectStatement = sqlStatement.SelectStatement;
            QueryExpression aQueryExpression = aSelectStatement.QueryExpression;

            if (aQueryExpression.GetType() != typeof(QuerySpecification))
            {
                return null;
            }

            QuerySpecification aQuerySpecification = (QuerySpecification)aQueryExpression;
            var selectElements = aQuerySpecification.SelectElements
                .OfType<SelectScalarExpression>()
                .ToList();

            foreach (SelectScalarExpression aSelectScalarExpression in selectElements)
            {
                string identStr = string.Empty;
                IdentifierOrValueExpression aIdentifierOrValueExpression =
                    aSelectScalarExpression.ColumnName;
                if (aIdentifierOrValueExpression != null)
                {
                    if (aIdentifierOrValueExpression.ValueExpression == null)
                    {
                        identStr = aIdentifierOrValueExpression.Identifier.Value;
                    }
                }

                result.AddLast(new ViewColumn());
                result.Last.Value.Alias = identStr;

                ScalarExpression aScalarExpression = aSelectScalarExpression.Expression;
                var aMultiPartIdentifier = FindSelectScalarExperssionRecurse(aScalarExpression);
                string aColumnInfoTableColumnName = "";
                string aColumnInfoTableAlias = "";
                string aColumnInfoReferencedTableName = "";

                if (aMultiPartIdentifier != null)
                {
                    int aIdentIdx = 0;
                    foreach (var aIdentifier in aMultiPartIdentifier.Identifiers)
                    {
                        if (aMultiPartIdentifier.Identifiers.Count == 2)
                        {
                            if (aIdentIdx == 0)
                            {
                                aColumnInfoTableAlias = aIdentifier.Value;
                            }
                            if (aIdentIdx == 1)
                                aColumnInfoTableColumnName = aIdentifier.Value;
                        }

                        if (aMultiPartIdentifier.Identifiers.Count == 3)
                        {
                            if (aIdentIdx == 1)
                            {
                                aColumnInfoReferencedTableName = aIdentifier.Value;
                            }
                            if (aIdentIdx == 2)
                                aColumnInfoTableColumnName = aIdentifier.Value;
                        }

                        result.Last.Value.TableAlias = aColumnInfoTableAlias;
                        result.Last.Value.TableColumnName = aColumnInfoTableColumnName;
                        result.Last.Value.TableName = aColumnInfoReferencedTableName;

                        aIdentIdx = aIdentIdx + 1;
                    }
                }
            }
            return result;
        }

        private static MultiPartIdentifier FindSelectScalarExperssionRecurse(ScalarExpression aScalarExpression)
        {
            if (aScalarExpression.GetType() == typeof(ColumnReferenceExpression))
            {
                ColumnReferenceExpression aColumnReferenceExpression = (ColumnReferenceExpression)aScalarExpression;
                MultiPartIdentifier aMultiPartIdentifier = aColumnReferenceExpression.MultiPartIdentifier;
                return aMultiPartIdentifier;
            }

            if (aScalarExpression.GetType() == typeof(ConvertCall))
            {
                ConvertCall aConvertCall = (ConvertCall)aScalarExpression;
                ScalarExpression aScalarExpressionParameter = aConvertCall.Parameter;
                FindSelectScalarExperssionRecurse(aScalarExpressionParameter);
            }

            return null;
        }

Microsoft.SqlServer.TransactSql.ScriptDom.ViewStatementBody : TSqlStatement

Methods :

public SchemaObjectName get_SchemaObjectName()
public Void set_SchemaObjectName(SchemaObjectName value = )
public IList<Identifier> get_Columns()
public IList<ViewOption> get_ViewOptions()
public SelectStatement get_SelectStatement()
public Void set_SelectStatement(SelectStatement value = )
public Boolean get_WithCheckOption()
public Void set_WithCheckOption(Boolean value = )
public Boolean get_IsMaterialized()
public Void set_IsMaterialized(Boolean value = )
public Void AcceptChildren(TSqlFragmentVisitor visitor = )
public Void Accept(TSqlFragmentVisitor visitor = )
public Int32 get_StartOffset()
public Int32 get_FragmentLength()
public Int32 get_StartLine()
public Int32 get_StartColumn()
public Int32 get_FirstTokenIndex()
public Void set_FirstTokenIndex(Int32 value = )
public Int32 get_LastTokenIndex()
public Void set_LastTokenIndex(Int32 value = )
public IList<TSqlParserToken> get_ScriptTokenStream()
public Void set_ScriptTokenStream(IList<TSqlParserToken> value = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()