Exemplo n.º 1
0
            private static ICollection <AttrMetaData> Required(TableProperty prop, ICollection <TableProperty> properties)
            {
                if (prop.IsNullable || !Shared.NullableMap.Contains(prop.DataType))
                {
                    return(null);
                }

                return(new[] { new AttrMetaData {
                                   Name = "Required"
                               } });
            }
Exemplo n.º 2
0
            private static ICollection <AttrMetaData> Unicode(TableProperty prop, ICollection <TableProperty> properties)
            {
                if (!UnicodeTypes.Contains(prop.DataType))
                {
                    return(null);
                }

                return(new[] { new AttrMetaData {
                                   Name = "IsUnicode"
                               } });
            }
Exemplo n.º 3
0
            private static ICollection <AttrMetaData> Identity(TableProperty prop, ICollection <TableProperty> properties)
            {
                if (!prop.IsIdentity || prop.IsKey)
                {
                    return(null);
                }

                var attr = new AttrMetaData {
                    Name = "DatabaseGenerated"
                };

                attr.Params.Add(new KeyValuePair <string, string>("", "DatabaseGeneratedOption.Identity"));

                return(new[] { attr });;
            }
Exemplo n.º 4
0
            private static ICollection <AttrMetaData> Rename(TableProperty prop, ICollection <TableProperty> properties)
            {
                var sanitizedName = Shared.NameSanitize(prop.ColumnName);

                if (prop.ColumnName.Length == sanitizedName.Length)
                {
                    return(null);
                }

                var attr = new AttrMetaData {
                    Name = "Column"
                };

                attr.Params.Add(new KeyValuePair <string, string>("", $"\"{prop.ColumnName}\""));

                return(new[] { attr });
            }
Exemplo n.º 5
0
            private static ICollection <AttrMetaData> StringLength(TableProperty prop, ICollection <TableProperty> properties)
            {
                if (prop.Size == null || prop.Size == -1)
                {
                    return(null);
                }

                var attr = new AttrMetaData {
                    Name = "StringLength"
                };

                attr.Params.Add(new KeyValuePair <string, string>("", prop.Size.ToString()));

                if (MinLengthTypes.Contains(prop.DataType))
                {
                    attr.Params.Add(new KeyValuePair <string, string>("MinimumLength", prop.Size.ToString()));
                }

                return(new[] { attr });;
            }
Exemplo n.º 6
0
            public static ICollection <AttrMetaData> GetPropertyAttributes(TableProperty prop, ICollection <TableProperty> properties)
            {
                var attributes = new List <AttrMetaData>();

                void MaybeAddAttr(Func <TableProperty, ICollection <TableProperty>, ICollection <AttrMetaData> > func)
                {
                    var attrs = func(prop, properties);

                    if (attrs != null && attrs.Any())
                    {
                        foreach (var attr in attrs)
                        {
                            var existing = attributes.SingleOrDefault(x => x.Name == attr.Name);
                            if (existing != null)
                            {
                                existing.Params = existing.Params
                                                  .Concat(attr.Params)
                                                  .GroupBy(x => x.Key)
                                                  .Select(x => x.First())
                                                  .ToList();

                                continue;
                            }

                            attributes.Add(attr);
                        }
                    }
                }

                MaybeAddAttr(Unicode);
                MaybeAddAttr(Key);
                MaybeAddAttr(StringLength);
                MaybeAddAttr(Computed);
                MaybeAddAttr(Identity);
                MaybeAddAttr(DbNone);
                MaybeAddAttr(Required);
                MaybeAddAttr(Rename);
                MaybeAddAttr(DataType);

                return(attributes);
            }
Exemplo n.º 7
0
            private static ICollection <AttrMetaData> DbNone(TableProperty prop, ICollection <TableProperty> properties)
            {
                if (!prop.IsKey || prop.IsIdentity)
                {
                    return(null);
                }

                // composite PK not db generated is implied
                if (properties.Count(x => x.IsKey) > 1)
                {
                    return(null);
                }

                var attr = new AttrMetaData {
                    Name = "DatabaseGenerated"
                };

                attr.Params.Add(new KeyValuePair <string, string>("", "DatabaseGeneratedOption.None"));

                return(new[] { attr });;
            }
Exemplo n.º 8
0
        private static void WriteProperty(StringBuilder sb, TableProperty prop, ICollection <TableProperty> properties, bool firstProp)
        {
            var attributes = GetPropertyAttributes(prop, properties);

            if (attributes.Any() && !firstProp)
            {
                sb.Append("\r\n");
            }

            sb.Append("\t\t");
            foreach (var attr in attributes)
            {
                sb.Append($"[{attr.Name}");

                var defaultParam = attr.Params.SingleOrDefault(x => string.IsNullOrWhiteSpace(x.Key));
                var attrParams   = attr.Params
                                   .Where(x => !string.IsNullOrWhiteSpace(x.Key))
                                   .Aggregate(defaultParam.Value ?? string.Empty, (acc, cur) => $"{acc}, {cur.Key} = {cur.Value}")
                                   .Trim(new[] { ',', ' ' });

                if (attrParams.Length > 0)
                {
                    sb.Append($"({attrParams})");
                }

                sb.Append("]\r\n\t\t");
            }

            sb.Append($"public {Shared.DataMap[prop.DataType]}");

            if (prop.IsNullable && Shared.NonNullableMap.Contains(prop.DataType))
            {
                sb.Append("?");
            }

            sb.Append($" {Shared.NameSanitize(prop.ColumnName)} {{ get; set; }}");
            sb.Append("\r\n");
        }
Exemplo n.º 9
0
            private static ICollection <AttrMetaData> DataType(TableProperty prop, ICollection <TableProperty> properties)
            {
                if (StringTypes.Contains(prop.DataType))
                {
                    var attr = new AttrMetaData {
                        Name = "Column"
                    };
                    attr.Params.Add(new KeyValuePair <string, string>("TypeName", $"\"{prop.DataType}({(prop.Size > -1 ? prop.Size.ToString() : "MAX")})\""));
                    return(new[] { attr });
                }


                if (NumericTypes.Contains(prop.DataType))
                {
                    var attr = new AttrMetaData {
                        Name = "Column"
                    };
                    attr.Params.Add(new KeyValuePair <string, string>("TypeName", $"\"{prop.DataType}({prop.Precision}, {prop.Scale})\""));
                    return(new[] { attr });
                }

                return(null);
            }
Exemplo n.º 10
0
            private static ICollection <AttrMetaData> Key(TableProperty prop, ICollection <TableProperty> properties)
            {
                if (!prop.IsKey)
                {
                    return(null);
                }

                var attrs = new List <AttrMetaData> {
                    new AttrMetaData {
                        Name = "Key"
                    }
                };

                if (properties.Count(x => x.IsKey) > 1)
                {
                    var order = -1;
                    for (int i = 0; i < properties.Count; i++)
                    {
                        var tableProperty = properties.ElementAt(i);
                        if (tableProperty.IsKey)
                        {
                            order++;

                            if (tableProperty.ColumnName == prop.ColumnName)
                            {
                                var columnAttr = new AttrMetaData {
                                    Name = "Column"
                                };
                                columnAttr.Params.Add(new KeyValuePair <string, string>("Order", order.ToString()));
                                attrs.Add(columnAttr);
                            }
                        }
                    }
                }

                return(attrs);
            }
Exemplo n.º 11
0
 private static ICollection <AttrMetaData> GetPropertyAttributes(TableProperty prop, ICollection <TableProperty> properties)
 {
     return(AttrMetaDataBuilder.GetPropertyAttributes(prop, properties));
 }
Exemplo n.º 12
0
        public List <TableProperty> GetProperties(string table, string schema)
        {
            const string sql = @" -- get columns query
				SELECT
					c.COLUMN_NAME AS ColumnName
					, c.ORDINAL_POSITION AS Position
					, c.IS_NULLABLE AS IsNullable
					, c.DATA_TYPE AS DataType
					, c.CHARACTER_MAXIMUM_LENGTH AS Size
					, c.NUMERIC_PRECISION AS Precision
					, c.NUMERIC_SCALE AS Scale
				FROM    INFORMATION_SCHEMA.COLUMNS c
				WHERE   c.TABLE_NAME = @TableName and ISNULL(@TableSchema, c.TABLE_SCHEMA) = c.TABLE_SCHEMA  
				ORDER BY c.ORDINAL_POSITION"                ;

            var pks             = GetPk(table, schema);
            var computeds       = GetComputed(table, schema);
            var identities      = GetIdentity(table, schema);
            var fks             = GetFks(table, schema);
            var dependentTables = GetDependentTables(table, schema);

            var properties = new List <TableProperty>();

            using (var cmd = new SqlCommand(sql, (SqlConnection)_connection))
            {
                cmd.Parameters.Add(new SqlParameter("@TableName", table));
                cmd.Parameters.Add(new SqlParameter("@TableSchema", schema));

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var size = default(int?);
                        if (int.TryParse(reader["Size"].ToString(), out var tmpSize))
                        {
                            size = tmpSize;
                        }

                        var precision = default(int?);
                        if (int.TryParse(reader["Precision"].ToString(), out var tmpPrecision))
                        {
                            precision = tmpPrecision;
                        }

                        var scale = default(int?);
                        if (int.TryParse(reader["Scale"].ToString(), out var tmpScale))
                        {
                            scale = tmpScale;
                        }

                        var colName = reader["ColumnName"].ToString();

                        var prop = new TableProperty
                        {
                            ColumnName   = colName,
                            Position     = int.Parse(reader["Position"].ToString()),
                            DataType     = reader["DataType"].ToString(),
                            PrimaryTable = fks
                                           .Where(x => x.column == colName)
                                           .Select(x => x.table)
                                           .FirstOrDefault(),
                            DependentTables = dependentTables
                                              .Where(x => x.refColumn == colName)
                                              .Select(x => x.dependentTable)
                                              .ToList(),
                            IsNullable = reader["IsNullable"].ToString() == "YES",
                            Size       = size,
                            Precision  = precision,
                            Scale      = scale,
                            IsComputed = computeds.Contains(colName),
                            IsIdentity = identities.Contains(colName),
                            IsKey      = pks.Contains(colName),
                        };

                        properties.Add(prop);
                    }
                }
            }

            return(properties);
        }