Esempio n. 1
0
        protected string GetKey(SqlObjectType type, object subType = null)
        {
            var result = "";

            switch (type)
            {
            case SqlObjectType.Sproc:
                var sprocType = SqlGeneratorHelper.GetSubType <SqlSprocType>(subType);
                result = $"/Procedures/{sprocType}";
                break;

            case SqlObjectType.Udf:
                var udfType = SqlGeneratorHelper.GetSubType <SqlUdfType>(subType);
                result = $"/Functions/{udfType}";
                break;

            case SqlObjectType.Table:
                var tblType = SqlGeneratorHelper.GetSubType <SqlTableType>(subType);
                result = $"/Tables/{tblType}";
                break;

            case SqlObjectType.Type:
                var udtType = SqlGeneratorHelper.GetSubType <SqlUdtType>(subType);
                result = $"/Types/{udtType}";
                break;

            case SqlObjectType.View:
                var viewType = SqlGeneratorHelper.GetSubType <SqlViewType>(subType);
                result = $"/Views/{viewType}";
                break;

            case SqlObjectType.Constraint:
                var constraintType = SqlGeneratorHelper.GetSubType <SqlConstraintType>(subType);
                result = $"/Constraints/{constraintType}";
                break;

            case SqlObjectType.Key:
                var keyType = SqlGeneratorHelper.GetSubType <SqlKeyType>(subType);
                result = $"/Keys/{keyType}";
                break;

            case SqlObjectType.Index: result = "Indexes"; break;

            case SqlObjectType.Assembly: result = "/Assemblies"; break;

            case SqlObjectType.SecurityPolicy: result = "/SecurityPolicies"; break;

            default:
                result = $"/{type}s{(subType == null ? "": $"/{subType}")}";
                break;
            }
        public Dictionary <string, int> Count(SqlObjectType type, object subType = null, string keyword = "")
        {
            var result     = new Dictionary <string, int>();
            var strSubType = subType?.ToString();
            KeyValuePair <string, int> kv;

            switch (type)
            {
            case SqlObjectType.Sproc:
                if (string.IsNullOrEmpty(strSubType) || string.Compare(strSubType, "all", true) == 0)
                {
                    result = CountAll(type, typeof(SqlSprocType), keyword);
                }
                else
                {
                    var sprocType = SqlGeneratorHelper.GetSubType <SqlSprocType>(subType);

                    if (sprocType.HasValue)
                    {
                        kv = CountInternal(type, sprocType.Value, keyword);

                        result.Add(kv.Key, kv.Value);
                    }
                    else
                    {
                        Logger.Danger($"No subtype specified or subtype ('{subType}') is invalid.");
                    }
                }

                break;

            case SqlObjectType.Udf:
                if (string.IsNullOrEmpty(strSubType) || string.Compare(strSubType, "all", true) == 0)
                {
                    result = CountAll(type, typeof(SqlUdfType), keyword);
                }
                else
                {
                    var udfType = SqlGeneratorHelper.GetSubType <SqlUdfType>(subType);

                    if (udfType.HasValue)
                    {
                        kv = CountInternal(type, udfType.Value, keyword);

                        result.Add(kv.Key, kv.Value);
                    }
                    else
                    {
                        Logger.Danger($"No subtype specified or subtype ('{subType}') is invalid.");
                    }
                }

                break;

            case SqlObjectType.Table:
            {
                if (string.IsNullOrEmpty(strSubType) || string.Compare(strSubType, "all", true) == 0)
                {
                    result = CountAll(type, typeof(SqlTableType), keyword);
                }
                else
                {
                    var tableType = SqlGeneratorHelper.GetSubType <SqlTableType>(subType);

                    if (tableType.HasValue)
                    {
                        kv = CountInternal(type, tableType.Value, keyword);

                        result.Add(kv.Key, kv.Value);
                    }
                    else
                    {
                        Logger.Danger($"No subtype specified or subtype ('{subType}') is invalid.");
                    }
                }
            }

            break;

            case SqlObjectType.View:
                kv = CountInternal(type, SqlViewType.Sql, keyword);

                result.Add(kv.Key, kv.Value);

                break;

            case SqlObjectType.Trigger:
                var triggerType = SqlGeneratorHelper.GetSubType <SqlTriggerType>(subType);

                if (triggerType.HasValue)
                {
                    kv = CountInternal(type, triggerType.Value, keyword);

                    result.Add(kv.Key, kv.Value);
                }
                else
                {
                    Logger.Danger($"No subtype specified or subtype ('{subType}') is invalid.");
                }

                break;
            }

            return(result);
        }
        public virtual string GetPath(SqlObjectType type, object subType = null)
        {
            var result = "";

            switch (type)
            {
            case SqlObjectType.Sproc:
                var sprocType = SqlGeneratorHelper.GetSubType <SqlSprocType>(subType);
                result = $"/Procedures/{sprocType}";
                break;

            case SqlObjectType.Udf:
                var udfType = SqlGeneratorHelper.GetSubType <SqlUdfType>(subType);
                result = $"/Functions/{udfType}";
                break;

            case SqlObjectType.Table:
                var tblType = SqlGeneratorHelper.GetSubType <SqlTableType>(subType);
                result = $"/Tables/{tblType}";
                break;

            case SqlObjectType.Type:
                var udtType = SqlGeneratorHelper.GetSubType <SqlUdtType>(subType);
                result = $"/Types/{udtType}";
                break;

            case SqlObjectType.Trigger: result = $"/Triggers"; break;

            case SqlObjectType.Index: result = "Indexes"; break;

            case SqlObjectType.View:
                var viewType = SqlGeneratorHelper.GetSubType <SqlViewType>(subType);
                result = $"/Views/{viewType}";
                break;

            case SqlObjectType.Synonym: result = "/Synonyms"; break;

            case SqlObjectType.Constraint:
                var constraintType = SqlGeneratorHelper.GetSubType <SqlConstraintType>(subType);
                result = $"/Constraints/{constraintType}";
                break;

            case SqlObjectType.FileGroup: result = "/FileGroups"; break;

            case SqlObjectType.File: result = ""; break;

            case SqlObjectType.Partition: result = "/File"; break;

            case SqlObjectType.Assembly: result = "/Assemblies"; break;

            case SqlObjectType.Rule: result = "/Rules"; break;

            case SqlObjectType.Sequence: result = "/Sequences"; break;

            case SqlObjectType.Diagram: result = "/Diagrams"; break;

            case SqlObjectType.Schema: result = "/Schemas"; break;

            case SqlObjectType.User: result = "/Users"; break;

            case SqlObjectType.Role: result = "/Roles"; break;

            case SqlObjectType.Permission: result = "/Permissions"; break;

            case SqlObjectType.Certificate: result = "/Certificates"; break;

            case SqlObjectType.SecurityPolicy: result = "/SecurityPolicies"; break;

            case SqlObjectType.Key:
                var keyType = SqlGeneratorHelper.GetSubType <SqlKeyType>(subType);
                result = $"/Keys/{keyType}";
                break;

            default:
                break;
            }

            return(BasePath + result);
        }
        public void Generate(GenerationType generateType, SqlObjectType type, object subType = null, string keyword = "")
        {
            var strSubType = subType?.ToString();

            switch (type)
            {
            case SqlObjectType.Sproc:
                if (string.IsNullOrEmpty(strSubType) || string.Compare(strSubType, "all", true) == 0)
                {
                    GenerateAll(generateType, type, typeof(SqlSprocType), keyword);
                }
                else
                {
                    var sprocType = SqlGeneratorHelper.GetSubType <SqlSprocType>(subType);

                    if (sprocType.HasValue)
                    {
                        GenerateInternal(generateType, type, sprocType.Value, keyword);
                    }
                    else
                    {
                        Logger.Danger($"No subtype specified or subtype ('{subType}') is invalid.");
                    }
                }

                break;

            case SqlObjectType.Udf:
                if (string.IsNullOrEmpty(strSubType) || string.Compare(strSubType, "all", true) == 0)
                {
                    GenerateAll(generateType, type, typeof(SqlUdfType), keyword);
                }
                else
                {
                    var udfType = SqlGeneratorHelper.GetSubType <SqlUdfType>(subType);

                    if (udfType.HasValue)
                    {
                        GenerateInternal(generateType, type, udfType.Value, keyword);
                    }
                    else
                    {
                        Logger.Danger($"No subtype specified or subtype ('{subType}') is invalid.");
                    }
                }

                break;

            case SqlObjectType.Table:
                var sp_getDDL_exists = false;

                using (var con = new SqlConnection(Options.ConnectionString))
                {
                    using (var cmd = new SqlCommand($"use master;select case when exists(select 1 from sys.procedures where name = 'sp_GetDDL') then 1 else 0 end", con))
                    {
                        con.Open();
                        sp_getDDL_exists = ((int)cmd.ExecuteScalar()) == 1;
                    }
                }

                if (!sp_getDDL_exists)
                {
                    Logger.Warn($"sp_GetDDL was not found. It is required to generate scripts of Tables. You can download it from https://github.com/ironcodev/SqlGenerator");
                }
                else
                {
                    if (string.IsNullOrEmpty(strSubType) || string.Compare(strSubType, "all", true) == 0)
                    {
                        GenerateAll(generateType, type, typeof(SqlTableType), keyword);
                    }
                    else
                    {
                        var tableType = SqlGeneratorHelper.GetSubType <SqlTableType>(subType);

                        if (tableType.HasValue)
                        {
                            GenerateInternal(generateType, type, tableType.Value, keyword);
                        }
                        else
                        {
                            Logger.Danger($"No subtype specified or subtype ('{subType}') is invalid.");
                        }
                    }
                }

                break;

            case SqlObjectType.View:
                GenerateInternal(generateType, type, SqlViewType.Sql, keyword);

                break;

            case SqlObjectType.Trigger:
                var triggerType = SqlGeneratorHelper.GetSubType <SqlTriggerType>(subType);

                if (triggerType.HasValue)
                {
                    GenerateInternal(generateType, type, triggerType.Value, keyword);
                }
                else
                {
                    Logger.Danger($"No subtype specified or subtype ('{subType}') is invalid.");
                }

                break;
            }
        }