public string GetUniqueForeignKeyName(bool isParent, string tableNameHumanCase, ForeignKey foreignKey, bool checkForFkNameClashes, bool makeSingular,
                                              Relationship relationship)
        {
            var userSpecifiedName = CheckForUserSpecifiedName(isParent, foreignKey);

            if (!string.IsNullOrEmpty(userSpecifiedName))
            {
                return(userSpecifiedName);
            }

            var addReverseNavigationUniquePropName = checkForFkNameClashes &&
                                                     (_table.DbName == foreignKey.FkTableName ||
                                                      (_table.DbName == foreignKey.PkTableName && foreignKey.IncludeReverseNavigation));

            // Attempt 1
            var fkName = (Settings.UsePascalCase ? Inflector.ToTitleCase(foreignKey.FkColumn) : foreignKey.FkColumn).Replace(" ", string.Empty).Replace("$", string.Empty);
            var name   = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 1);

            if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
            {
                foreignKey.UniqueName = name;
            }

            return(name);

            // todo
            return("LatestForeignKeyNamingStrategy_todo");
        }
        public void Test(string test, string expected, bool useCamelCase)
        {
            // Act
            var clean         = DatabaseReader.CleanUp(test);
            var singular      = clean;
            var nameHumanCase = (useCamelCase ? Inflector.ToTitleCase(singular) : singular).Replace(" ", "").Replace("$", "").Replace(".", "");

            // Assert
            Assert.AreEqual(expected, nameHumanCase);
        }
Ejemplo n.º 3
0
    public string PkTableHumanCase(string suffix)
    {
        var singular         = Inflector.MakeSingular(PkTableNameFiltered);
        var pkTableHumanCase = (Settings.UsePascalCase ? Inflector.ToTitleCase(singular) : singular).Replace(" ", "").Replace("$", "");

        if (string.Compare(PkSchema, "dbo", StringComparison.OrdinalIgnoreCase) != 0 && Settings.PrependSchemaName)
        {
            pkTableHumanCase = PkSchema + "_" + pkTableHumanCase;
        }
        pkTableHumanCase += suffix;
        return(pkTableHumanCase);
    }
        public string GetUniqueForeignKeyName(bool isParent, string tableNameHumanCase, ForeignKey foreignKey, bool checkForFkNameClashes, bool makeSingular,
                                              Relationship relationship)
        {
            var userSpecifiedName = CheckForUserSpecifiedName(isParent, foreignKey);

            if (!string.IsNullOrEmpty(userSpecifiedName))
            {
                return(userSpecifiedName);
            }

            var addReverseNavigationUniquePropName = checkForFkNameClashes &&
                                                     (_table.DbName == foreignKey.FkTableName ||
                                                      (_table.DbName == foreignKey.PkTableName && foreignKey.IncludeReverseNavigation));

            // Generate name
            if (ReverseNavigationUniquePropName.Count == 0)
            {
                // Reserve table name and all column names
                ReverseNavigationUniquePropName.Add(_table.NameHumanCase);
                ReverseNavigationUniquePropName.AddRange(_table.Columns.Select(c => c.NameHumanCase));
            }

            if (!makeSingular)
            {
                tableNameHumanCase = Inflector.MakePlural(tableNameHumanCase);
            }

            if (checkForFkNameClashes &&
                ReverseNavigationUniquePropName.Contains(tableNameHumanCase) &&
                !ReverseNavigationUniquePropNameClashes.Contains(tableNameHumanCase))
            {
                ReverseNavigationUniquePropNameClashes.Add(tableNameHumanCase); // Name clash
            }

            // Attempt 1
            var    fkName = (Settings.UsePascalCase ? Inflector.ToTitleCase(foreignKey.FkColumn) : foreignKey.FkColumn).Replace(" ", string.Empty).Replace("$", string.Empty);
            var    name   = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 1);
            string col;

            if (!ReverseNavigationUniquePropName.Contains(name) &&
                !ReverseNavigationUniquePropNameClashes.Contains(name))
            {
                if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                {
                    ReverseNavigationUniquePropName.Add(name);
                    foreignKey.UniqueName = name;
                }

                return(name);
            }

            if (_table.DbName == foreignKey.FkTableName)
            {
                // Attempt 2
                if (fkName.Length > 2 && fkName.ToLowerInvariant().EndsWith("id"))
                {
                    col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 2);

                    if (checkForFkNameClashes &&
                        ReverseNavigationUniquePropName.Contains(col) &&
                        !ReverseNavigationUniquePropNameClashes.Contains(col))
                    {
                        ReverseNavigationUniquePropNameClashes.Add(col); // Name clash
                    }

                    if (!ReverseNavigationUniquePropName.Contains(col) &&
                        !ReverseNavigationUniquePropNameClashes.Contains(col))
                    {
                        if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                        {
                            ReverseNavigationUniquePropName.Add(col);
                        }

                        return(col);
                    }
                }

                // Attempt 3
                col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 3);
                if (checkForFkNameClashes &&
                    ReverseNavigationUniquePropName.Contains(col) &&
                    !ReverseNavigationUniquePropNameClashes.Contains(col))
                {
                    ReverseNavigationUniquePropNameClashes.Add(col); // Name clash
                }

                if (!ReverseNavigationUniquePropName.Contains(col) &&
                    !ReverseNavigationUniquePropNameClashes.Contains(col))
                {
                    if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                    {
                        ReverseNavigationUniquePropName.Add(col);
                    }

                    return(col);
                }
            }

            // Attempt 4
            col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 4);
            if (checkForFkNameClashes &&
                ReverseNavigationUniquePropName.Contains(col) &&
                !ReverseNavigationUniquePropNameClashes.Contains(col))
            {
                ReverseNavigationUniquePropNameClashes.Add(col); // Name clash
            }

            if (!ReverseNavigationUniquePropName.Contains(col) &&
                !ReverseNavigationUniquePropNameClashes.Contains(col))
            {
                if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                {
                    ReverseNavigationUniquePropName.Add(col);
                }

                return(col);
            }

            // Attempt 5
            for (var n = 1; n < 99; ++n)
            {
                col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 5) + n;

                if (ReverseNavigationUniquePropName.Contains(col))
                {
                    continue;
                }

                if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                {
                    ReverseNavigationUniquePropName.Add(col);
                }

                return(col);
            }

            // Give up
            return(Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 6));
        }
Ejemplo n.º 5
0
        public Column CreateColumn(RawTable rt, Table table, IDbContextFilter filter)
        {
            var col = new Column
            {
                Scale               = rt.Scale,
                PropertyType        = GetPropertyType(rt.TypeName),
                SqlPropertyType     = rt.TypeName,
                IsNullable          = rt.IsNullable,
                MaxLength           = rt.MaxLength,
                DateTimePrecision   = rt.DateTimePrecision,
                Precision           = rt.Precision,
                IsIdentity          = rt.IsIdentity,
                IsComputed          = rt.IsComputed,
                IsRowGuid           = rt.IsRowGuid,
                GeneratedAlwaysType = (ColumnGeneratedAlwaysType)rt.GeneratedAlwaysType,
                IsStoreGenerated    = rt.IsStoreGenerated,
                PrimaryKeyOrdinal   = rt.PrimaryKeyOrdinal,
                IsPrimaryKey        = rt.PrimaryKey,
                IsForeignKey        = rt.IsForeignKey,
                IsSpatial           = rt.TypeName == "geography" || rt.TypeName == "geometry",
                Ordinal             = rt.Ordinal,
                DbName              = rt.ColumnName,
                Default             = rt.Default,
                ParentTable         = table,
                ExistsInBaseClass   = false
            };

            if (col.MaxLength == -1 && (col.SqlPropertyType.EndsWith("varchar", StringComparison.InvariantCultureIgnoreCase) ||
                                        col.SqlPropertyType.EndsWith("varbinary", StringComparison.InvariantCultureIgnoreCase)))
            {
                col.SqlPropertyType += "(max)";
            }

            if (col.IsPrimaryKey && !col.IsIdentity && col.IsStoreGenerated && rt.TypeName == "uniqueidentifier")
            {
                col.IsStoreGenerated = false;
                col.IsIdentity       = true;
            }

            if (!col.IsPrimaryKey && filter.IsExcluded(col))
            {
                col.Hidden = true;
            }

            col.IsFixedLength = (rt.TypeName == "char" || rt.TypeName == "nchar");
            col.IsUnicode     = !(rt.TypeName == "char" || rt.TypeName == "varchar" || rt.TypeName == "text");
            col.IsMaxLength   = (rt.TypeName == "ntext");

            col.IsRowVersion = col.IsStoreGenerated && !col.IsNullable && rt.TypeName == "timestamp";
            if (col.IsRowVersion)
            {
                col.MaxLength = 8;
            }

            if (rt.TypeName == "hierarchyid")
            {
                col.MaxLength = 0;
            }

            col.CleanUpDefault();
            col.NameHumanCase = CleanUp(col.DbName);
            if (string.IsNullOrWhiteSpace(col.NameHumanCase))
            {
                col.NameHumanCase = "Unknown";
                col.Hidden        = true;
            }
            col.NameHumanCase = ReservedColumnNames.Replace(col.NameHumanCase, "_$1");

            if (ReservedKeywords.Contains(col.NameHumanCase))
            {
                col.NameHumanCase = "@" + col.NameHumanCase;
            }

            col.DisplayName = Column.ToDisplayName(col.DbName);

            var titleCase = (Settings.UsePascalCase ? Inflector.ToTitleCase(col.NameHumanCase) : col.NameHumanCase).Replace(" ", string.Empty);

            if (titleCase != string.Empty)
            {
                col.NameHumanCase = titleCase;
            }

            // Make sure property name doesn't clash with class name
            if (col.NameHumanCase == table.NameHumanCase)
            {
                col.NameHumanCase += "_";
            }

            if (char.IsDigit(col.NameHumanCase[0]))
            {
                col.NameHumanCase = "_" + col.NameHumanCase;
            }

            table.HasNullableColumns = col.IsColumnNullable();

            // If PropertyType is empty, return null. Most likely ignoring a column due to legacy (such as OData not supporting spatial types)
            if (string.IsNullOrEmpty(col.PropertyType))
            {
                return(null);
            }

            return(col);
        }
Ejemplo n.º 6
0
        public List <Enumeration> ReadEnums(List <EnumerationSettings> enums)
        {
            var result = new List <Enumeration>();

            using (var conn = _factory.CreateConnection())
            {
                if (conn == null)
                {
                    return(result);
                }

                conn.ConnectionString = Settings.ConnectionString;
                conn.Open();

                var cmd = GetCmd(conn);
                if (cmd == null)
                {
                    return(result);
                }

                foreach (var e in enums)
                {
                    var sql = EnumSQL(e.Table, e.NameField, e.ValueField);
                    if (string.IsNullOrEmpty(sql))
                    {
                        continue;
                    }

                    cmd.CommandText = sql;

                    try
                    {
                        using (var rdr = cmd.ExecuteReader())
                        {
                            var items = new List <KeyValuePair <string, string> >();
                            while (rdr.Read())
                            {
                                var name = rdr["NameField"].ToString().Trim();
                                if (string.IsNullOrEmpty(name))
                                {
                                    continue;
                                }

                                name = RemoveNonAlphanumerics.Replace(name, string.Empty);
                                name = Inflector.ToTitleCase(name).Replace(" ", "").Trim();
                                if (string.IsNullOrEmpty(name))
                                {
                                    continue;
                                }

                                var value = rdr["ValueField"].ToString().Trim();
                                if (string.IsNullOrEmpty(value))
                                {
                                    continue;
                                }

                                items.Add(new KeyValuePair <string, string>(name, value));
                            }

                            if (items.Any())
                            {
                                result.Add(new Enumeration(e.Name, items));
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // Enum table does not exist in database, skip
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 7
0
        public List <RawStoredProcedure> ReadStoredProcs(bool includeSynonyms)
        {
            if (DatabaseReaderPlugin != null)
            {
                return(DatabaseReaderPlugin.ReadStoredProcs());
            }

            var result = new List <RawStoredProcedure>();

            using (var conn = _factory.CreateConnection())
            {
                if (conn == null)
                {
                    return(result);
                }

                conn.ConnectionString = Settings.ConnectionString;
                conn.Open();

                var storedProcedureSQL = StoredProcedureSQL();
                if (string.IsNullOrEmpty(storedProcedureSQL))
                {
                    return(result);
                }

                var cmd = GetCmd(conn);
                if (cmd == null)
                {
                    return(result);
                }

                if (includeSynonyms)
                {
                    cmd.CommandText = SynonymStoredProcedureSQLSetup() + storedProcedureSQL + SynonymStoredProcedureSQL() + SpecialQueryFlags();
                }
                else
                {
                    cmd.CommandText = storedProcedureSQL + SpecialQueryFlags();
                }

                using (var rdr = cmd.ExecuteReader())
                {
                    var lastName         = string.Empty;
                    var emptyParamNumber = 1;
                    while (rdr.Read())
                    {
                        var rawDataType    = rdr["DATA_TYPE"];
                        var schema         = rdr["SPECIFIC_SCHEMA"].ToString().Trim();
                        var name           = rdr["SPECIFIC_NAME"].ToString().Trim();
                        var routineType    = rdr["ROUTINE_TYPE"].ToString().Trim().ToLower();
                        var returnDataType = rdr["RETURN_DATA_TYPE"].ToString().Trim().ToLower();
                        var dataType       = rawDataType.ToString().Trim().ToLower();
                        var parameterMode  = rdr["PARAMETER_MODE"].ToString().Trim().ToLower();

                        if (name != lastName)
                        {
                            lastName         = name;
                            emptyParamNumber = 1;
                        }

                        var isTableValuedFunction  = (routineType == "function" && returnDataType == "table");
                        var isScalarValuedFunction = (routineType == "function" && returnDataType != "table");
                        var isStoredProcedure      = (routineType == "procedure");

                        StoredProcedureParameter parameter = null;
                        if (rawDataType != DBNull.Value)
                        {
                            parameter = new StoredProcedureParameter
                            {
                                Ordinal             = ChangeType <int>(rdr["ORDINAL_POSITION"]),
                                Name                = rdr["PARAMETER_NAME"].ToString().Trim(),
                                SqlDbType           = GetStoredProcedureParameterDbType(dataType),
                                ReturnSqlDbType     = GetStoredProcedureParameterDbType(returnDataType),
                                PropertyType        = GetPropertyType(dataType),
                                ReturnPropertyType  = GetPropertyType(returnDataType),
                                DateTimePrecision   = ChangeType <short>(rdr["DATETIME_PRECISION"]),
                                MaxLength           = ChangeType <int>(rdr["CHARACTER_MAXIMUM_LENGTH"]),
                                Precision           = ChangeType <byte>(rdr["NUMERIC_PRECISION"]),
                                Scale               = ChangeType <int>(rdr["NUMERIC_SCALE"]),
                                UserDefinedTypeName = rdr["USER_DEFINED_TYPE"].ToString().Trim(),
                                IsSpatial           = SpatialTypes.Contains(dataType)
                            };

                            if (string.IsNullOrEmpty(parameter.Name))
                            {
                                parameter.Name = "p" + emptyParamNumber++;
                            }

                            switch (parameterMode)
                            {
                            case "in":
                                parameter.Mode = StoredProcedureParameterMode.In;
                                break;

                            case "out":
                                parameter.Mode = StoredProcedureParameterMode.Out;
                                break;

                            default:
                                parameter.Mode = StoredProcedureParameterMode.InOut;
                                break;
                            }

                            var clean = CleanUp(parameter.Name.Replace("@", string.Empty));
                            if (!string.IsNullOrEmpty(clean))
                            {
                                parameter.NameHumanCase = Inflector.MakeInitialLower((Settings.UsePascalCase ? Inflector.ToTitleCase(clean) : clean).Replace(" ", ""));

                                if (ReservedKeywords.Contains(parameter.NameHumanCase))
                                {
                                    parameter.NameHumanCase = "@" + parameter.NameHumanCase;
                                }
                            }
                        }

                        var rsp = new RawStoredProcedure(schema, name, isTableValuedFunction, isScalarValuedFunction, isStoredProcedure, parameter);
                        result.Add(rsp);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 8
0
        public string GetUniqueForeignKeyName(bool isParent, string tableNameHumanCase, ForeignKey foreignKey, bool checkForFkNameClashes, bool makeSingular,
                                              Relationship relationship)
        {
            // For unit testing
            //if (tableNameHumanCase.StartsWith("Burak") || tableNameHumanCase.StartsWith("Car") || tableNameHumanCase.StartsWith("User"))
            //{
            //    var s = $"[TestCase(\"00\", \"{foreignKey.FkTableName}\",  \"{NameHumanCase}\", \"{string.Join("|",Columns.Select(c => c.NameHumanCase))}\", {isParent}, \"{tableNameHumanCase}\", {checkForFkNameClashes}, {makeSingular}, Relationship.{relationship}, \"{foreignKey.FkTableName}\", \"{foreignKey.PkTableName}\", {foreignKey.IncludeReverseNavigation}, \"{foreignKey.FkColumn}\")]{Environment.NewLine}";
            //    System.IO.File.AppendAllText("c:/temp/unit.txt", s);
            //}

            // User specified name
            if (isParent && !string.IsNullOrEmpty(foreignKey.ParentName))
            {
                return(foreignKey.ParentName);
            }

            // User specified name
            if (!isParent && !string.IsNullOrEmpty(foreignKey.ChildName))
            {
                return(foreignKey.ChildName);
            }

            // Generate name
            var addReverseNavigationUniquePropName = checkForFkNameClashes &&
                                                     (_table.DbName == foreignKey.FkTableName ||
                                                      (_table.DbName == foreignKey.PkTableName && foreignKey.IncludeReverseNavigation));

            if (ReverseNavigationUniquePropName.Count == 0)
            {
                // Reserve table name and all column names
                ReverseNavigationUniquePropName.Add(_table.NameHumanCase);
                ReverseNavigationUniquePropName.AddRange(_table.Columns.Select(c => c.NameHumanCase));
            }

            if (!makeSingular)
            {
                tableNameHumanCase = Inflector.MakePlural(tableNameHumanCase);
            }

            if (checkForFkNameClashes &&
                ReverseNavigationUniquePropName.Contains(tableNameHumanCase) &&
                !ReverseNavigationUniquePropNameClashes.Contains(tableNameHumanCase))
            {
                ReverseNavigationUniquePropNameClashes.Add(tableNameHumanCase); // Name clash
            }

            // Attempt 1
            var    fkName = (Settings.UsePascalCase ? Inflector.ToTitleCase(foreignKey.FkColumn) : foreignKey.FkColumn).Replace(" ", string.Empty).Replace("$", string.Empty);
            var    name   = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 1);
            string col;

            if (!ReverseNavigationUniquePropName.Contains(name) &&
                !ReverseNavigationUniquePropNameClashes.Contains(name))
            {
                if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                {
                    ReverseNavigationUniquePropName.Add(name);
                    foreignKey.UniqueName = name;
                }

                return(name);
            }

            if (_table.DbName == foreignKey.FkTableName)
            {
                // Attempt 2
                if (fkName.ToLowerInvariant().EndsWith("id"))
                {
                    col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 2);

                    if (checkForFkNameClashes &&
                        ReverseNavigationUniquePropName.Contains(col) &&
                        !ReverseNavigationUniquePropNameClashes.Contains(col))
                    {
                        ReverseNavigationUniquePropNameClashes.Add(col); // Name clash
                    }

                    if (!ReverseNavigationUniquePropName.Contains(col) &&
                        !ReverseNavigationUniquePropNameClashes.Contains(col))
                    {
                        if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                        {
                            ReverseNavigationUniquePropName.Add(col);
                        }

                        return(col);
                    }
                }

                // Attempt 3
                col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 3);
                if (checkForFkNameClashes &&
                    ReverseNavigationUniquePropName.Contains(col) &&
                    !ReverseNavigationUniquePropNameClashes.Contains(col))
                {
                    ReverseNavigationUniquePropNameClashes.Add(col); // Name clash
                }

                if (!ReverseNavigationUniquePropName.Contains(col) &&
                    !ReverseNavigationUniquePropNameClashes.Contains(col))
                {
                    if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                    {
                        ReverseNavigationUniquePropName.Add(col);
                    }

                    return(col);
                }
            }

            // Attempt 4
            col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 4);
            if (checkForFkNameClashes &&
                ReverseNavigationUniquePropName.Contains(col) &&
                !ReverseNavigationUniquePropNameClashes.Contains(col))
            {
                ReverseNavigationUniquePropNameClashes.Add(col); // Name clash
            }

            if (!ReverseNavigationUniquePropName.Contains(col) &&
                !ReverseNavigationUniquePropNameClashes.Contains(col))
            {
                if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                {
                    ReverseNavigationUniquePropName.Add(col);
                }

                return(col);
            }

            // Attempt 5
            for (var n = 1; n < 99; ++n)
            {
                col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 5) + n;

                if (ReverseNavigationUniquePropName.Contains(col))
                {
                    continue;
                }

                if (addReverseNavigationUniquePropName || !checkForFkNameClashes)
                {
                    ReverseNavigationUniquePropName.Add(col);
                }

                return(col);
            }

            // Give up
            return(Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 6));
        }
Ejemplo n.º 9
0
    public string GetUniqueColumnName(string tableNameHumanCase, ForeignKey foreignKey, bool checkForFkNameClashes, bool makeSingular, Relationship relationship)
    {
        var addReverseNavigationUniquePropName = (checkForFkNameClashes || Name == foreignKey.FkTableName || (Name == foreignKey.PkTableName && foreignKey.IncludeReverseNavigation));

        if (ReverseNavigationUniquePropName.Count == 0)
        {
            ReverseNavigationUniquePropName.Add(NameHumanCase);
            ReverseNavigationUniquePropName.AddRange(Columns.Select(c => c.NameHumanCase));
        }

        if (!makeSingular)
        {
            tableNameHumanCase = Inflector.MakePlural(tableNameHumanCase);
        }

        if (checkForFkNameClashes && ReverseNavigationUniquePropName.Contains(tableNameHumanCase) && !ReverseNavigationUniquePropNameClashes.Contains(tableNameHumanCase))
        {
            ReverseNavigationUniquePropNameClashes.Add(tableNameHumanCase);     // Name clash
        }
        // Attempt 1
        string fkName = (Settings.UsePascalCase ? Inflector.ToTitleCase(foreignKey.FkColumn) : foreignKey.FkColumn).Replace(" ", "").Replace("$", "");
        string name   = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 1);
        string col;

        if (!ReverseNavigationUniquePropNameClashes.Contains(name) && !ReverseNavigationUniquePropName.Contains(name))
        {
            if (addReverseNavigationUniquePropName)
            {
                ReverseNavigationUniquePropName.Add(name);
            }

            return(name);
        }

        if (Name == foreignKey.FkTableName)
        {
            // Attempt 2
            if (fkName.ToLowerInvariant().EndsWith("id"))
            {
                col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 2);
                if (checkForFkNameClashes && ReverseNavigationUniquePropName.Contains(col) &&
                    !ReverseNavigationUniquePropNameClashes.Contains(col))
                {
                    ReverseNavigationUniquePropNameClashes.Add(col);     // Name clash
                }
                if (!ReverseNavigationUniquePropNameClashes.Contains(col) &&
                    !ReverseNavigationUniquePropName.Contains(col))
                {
                    if (addReverseNavigationUniquePropName)
                    {
                        ReverseNavigationUniquePropName.Add(col);
                    }

                    return(col);
                }
            }

            // Attempt 3
            col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 3);
            if (checkForFkNameClashes && ReverseNavigationUniquePropName.Contains(col) &&
                !ReverseNavigationUniquePropNameClashes.Contains(col))
            {
                ReverseNavigationUniquePropNameClashes.Add(col);     // Name clash
            }
            if (!ReverseNavigationUniquePropNameClashes.Contains(col) &&
                !ReverseNavigationUniquePropName.Contains(col))
            {
                if (addReverseNavigationUniquePropName)
                {
                    ReverseNavigationUniquePropName.Add(col);
                }

                return(col);
            }
        }

        // Attempt 4
        col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 4);
        if (checkForFkNameClashes && ReverseNavigationUniquePropName.Contains(col) && !ReverseNavigationUniquePropNameClashes.Contains(col))
        {
            ReverseNavigationUniquePropNameClashes.Add(col);     // Name clash
        }
        if (!ReverseNavigationUniquePropNameClashes.Contains(col) && !ReverseNavigationUniquePropName.Contains(col))
        {
            if (addReverseNavigationUniquePropName)
            {
                ReverseNavigationUniquePropName.Add(col);
            }

            return(col);
        }

        // Attempt 5
        for (int n = 1; n < 99; ++n)
        {
            col = Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 5) + n;

            if (ReverseNavigationUniquePropName.Contains(col))
            {
                continue;
            }

            if (addReverseNavigationUniquePropName)
            {
                ReverseNavigationUniquePropName.Add(col);
            }

            return(col);
        }

        // Give up
        return(Settings.ForeignKeyName(tableNameHumanCase, foreignKey, fkName, relationship, 6));
    }
        public List <Enumeration> ReadEnums(List <EnumerationSettings> enums)
        {
            var result = new List <Enumeration>();

            using (var conn = _factory.CreateConnection())
            {
                if (conn == null)
                {
                    return(result);
                }

                conn.ConnectionString = Settings.ConnectionString;
                conn.Open();

                var cmd = GetCmd(conn);
                if (cmd == null)
                {
                    return(result);
                }

                foreach (var e in enums)
                {
                    var sql = EnumSQL(e.Table, e.NameField, e.ValueField);
                    if (string.IsNullOrEmpty(sql))
                    {
                        continue;
                    }

                    cmd.CommandText = sql;

                    try
                    {
                        using (var rdr = cmd.ExecuteReader())
                        {
                            var items = new List <EnumerationMember>();

                            while (rdr.Read())
                            {
                                var name = rdr["NameField"].ToString().Trim();
                                if (string.IsNullOrEmpty(name))
                                {
                                    continue;
                                }

                                name = RemoveNonAlphanumerics.Replace(name, string.Empty);
                                name = (Settings.UsePascalCaseForEnumMembers ? Inflector.ToTitleCase(name) : name).Replace(" ", string.Empty).Trim();
                                if (string.IsNullOrEmpty(name))
                                {
                                    continue;
                                }

                                var value = rdr["ValueField"].ToString().Trim();
                                if (string.IsNullOrEmpty(value))
                                {
                                    continue;
                                }

                                var allValues = new Dictionary <string, object>();
                                for (var n = 2; n < rdr.FieldCount; ++n)
                                {
                                    var o = rdr.GetValue(n);
                                    allValues.Add(rdr.GetName(n), o != DBNull.Value ? o : null);
                                }

                                items.Add(new EnumerationMember(name, value, allValues));
                            }

                            if (items.Any())
                            {
                                result.Add(new Enumeration(e.Name, items));
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // Enum table does not exist in database, skip
                    }
                }
            }
            return(result);
        }