Пример #1
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);
        }
Пример #2
0
        public List <RawTable> ReadTables(bool includeSynonyms)
        {
            if (DatabaseReaderPlugin != null)
            {
                return(DatabaseReaderPlugin.ReadTables());
            }

            var result = new List <RawTable>();

            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);
                }

                string sql;
                if (includeSynonyms && Settings.DatabaseType != DatabaseType.SqlCe)
                {
                    sql = SynonymTableSQLSetup() + TableSQL() + SynonymTableSQL() + SpecialQueryFlags();
                }
                else
                {
                    sql = TableSQL() + SpecialQueryFlags();
                }

                if (!HasTemporalTableSupport())
                {
                    // Replace the column names (only present in SQL Server 2016 or later) with literal constants so the query works with older versions of SQL Server.
                    sql = sql
                          .Replace("[sc].[generated_always_type]", "0")
                          .Replace("[c].[generated_always_type]", "0")
                          .Replace("[st].[temporal_type]", "0");
                }

                cmd.CommandText = sql;

                using (var rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        var table = new RawTable(
                            rdr["SchemaName"].ToString().Trim(),
                            rdr["TableName"].ToString().Trim(),
                            string.Compare(rdr["TableType"].ToString().Trim(), "View", StringComparison.OrdinalIgnoreCase) == 0,
                            ChangeType <int>(rdr["Scale"]),
                            rdr["TypeName"].ToString().Trim().ToLower(),
                            ChangeType <bool>(rdr["IsNullable"]),
                            ChangeType <int>(rdr["MaxLength"]),
                            ChangeType <int>(rdr["DateTimePrecision"]),
                            ChangeType <int>(rdr["Precision"]),
                            ChangeType <bool>(rdr["IsIdentity"]),
                            ChangeType <bool>(rdr["IsComputed"]),
                            ChangeType <bool>(rdr["IsRowGuid"]),
                            ChangeType <byte>(rdr["GeneratedAlwaysType"]),
                            ChangeType <bool>(rdr["IsStoreGenerated"]),
                            ChangeType <int>(rdr["PrimaryKeyOrdinal"]),
                            ChangeType <bool>(rdr["PrimaryKey"]),
                            ChangeType <bool>(rdr["IsForeignKey"]),
                            ChangeType <int>(rdr["Ordinal"]),
                            rdr["ColumnName"].ToString().Trim(),
                            rdr["Default"].ToString().Trim()
                            );

                        result.Add(table);
                    }
                }
            }

            return(result);
        }