Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Ceen.Database.ColumnMapping"/> class.
 /// </summary>
 /// <param name="dialect">The database dialect.</param>
 /// <param name="property">The property to map.</param>
 public ColumnMapping(IDatabaseDialect dialect, PropertyInfo property)
 {
     Name         = dialect.GetName(property);
     IsPrimaryKey = property.GetCustomAttributes <PrimaryKeyAttribute>(true).Any();
     SqlType      = dialect.GetSqlColumnType(property);
     Property     = property;
 }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Ceen.Database.ColumnMapping"/> class.
        /// </summary>
        /// <param name="dialect">The database dialect.</param>
        /// <param name="member">The member to map.</param>
        /// <param name="memberType">The member type</param>
        private ColumnMapping(IDatabaseDialect dialect, MemberInfo member, Type memberType)
        {
            Member           = member ?? throw new ArgumentNullException(nameof(member));
            MemberType       = memberType;
            ColumnName       = dialect.GetName(member);
            QuotedColumnName = dialect.QuoteName(ColumnName);
            IsPrimaryKey     = member.GetCustomAttributes <PrimaryKeyAttribute>(true).Any();
            var sqlType = dialect.GetSqlColumnType(member);

            SqlType            = sqlType.Item1;
            AutoGenerateAction = sqlType.Item2;
            ValidationRules    = member.GetCustomAttributes <ValidationBaseAttribute>(true).ToArray();
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Ceen.Database.TableMapping"/> class.
        /// </summary>
        /// <param name="dialect">The database dialect.</param>
        /// <param name="type">The type to mape.</param>
        /// <param name="nameoverride">An optional name override for the table name</param>
        public TableMapping(IDatabaseDialect dialect, Type type, string nameoverride = null)
        {
            Dialect    = dialect ?? throw new ArgumentNullException(nameof(dialect));
            Type       = type;
            Name       = nameoverride ?? dialect.GetName(type);
            AllColumns = type
                         .GetProperties()
                         .Where(x => !x.GetCustomAttributes <IgnoreAttribute>(true).Any())
                         .Select(x => new ColumnMapping(dialect, x))
                         .ToArray();

            ColumnsWithoutPrimaryKey = AllColumns.Where(x => !x.IsPrimaryKey).ToArray();
            PrimaryKeys              = AllColumns.Where(x => x.IsPrimaryKey).ToArray();
            AllColumnsBySqlName      = AllColumns.ToDictionary(x => x.Name, x => x);
            AllColumnsByPropertyName = AllColumns.ToDictionary(x => x.Property.Name, x => x);

            // Build the unique maps
            var uniques = AllColumns
                          .Select(x => new Tuple <ColumnMapping, string[]>(x, x.Property.GetCustomAttributes <UniqueAttribute>().Select(y => y.Group).ToArray()))
                          .Where(x => x.Item2 != null && x.Item2.Length > 0);

            var solos  = uniques.Where(x => x.Item2.Contains(null));
            var groups = uniques
                         .SelectMany(x =>
                                     x.Item2
                                     .Where(y => y != null)
                                     .Select(y => new KeyValuePair <string, ColumnMapping>(y, x.Item1))
                                     )
                         .GroupBy(x => x.Key);

            Uniques =
                solos
                .Select(x => new UniqueMapping(null, new ColumnMapping[] { x.Item1 }))
                .Concat(
                    groups.Select(x => new UniqueMapping(x.Key, x.Select(y => y.Value).ToArray()))
                    ).ToArray();
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Ceen.Database.TableMapping"/> class.
        /// </summary>
        /// <param name="dialect">The database dialect.</param>
        /// <param name="type">The type to mape.</param>
        /// <param name="nameoverride">An optional name override for the table name</param>
        /// <param name="ignorefields">An optional flag that removes all fields from the map</param>
        public TableMapping(IDatabaseDialect dialect, Type type, string nameoverride = null, bool ignoreFields = false)
        {
            Dialect         = dialect ?? throw new ArgumentNullException(nameof(dialect));
            Type            = type;
            Name            = nameoverride ?? dialect.GetName(type);
            QuotedTableName = dialect.QuoteName(Name);
            AllColumns      = type
                              .GetProperties()
                              .Where(x => !x.GetCustomAttributes <IgnoreAttribute>(true).Any())
                              .Select(x => new ColumnMapping(dialect, x))
                              .Concat(
                type
                .GetFields()
                .Where(x => !x.GetCustomAttributes <IgnoreAttribute>(true).Any() && !ignoreFields)
                .Select(x => new ColumnMapping(dialect, x))
                )
                              .ToArray();

            var dbcreated = new[] { AutoGenerateAction.DatabaseAutoID, AutoGenerateAction.DatabaseChangeTimestamp, AutoGenerateAction.DatabaseCreateTimestamp };

            InsertColumns            = AllColumns.Where(x => !dbcreated.Contains(x.AutoGenerateAction)).ToArray();
            ColumnsWithoutPrimaryKey = AllColumns.Where(x => !x.IsPrimaryKey).ToArray();
            UpdateColumns            = ColumnsWithoutPrimaryKey.Where(x => !dbcreated.Contains(x.AutoGenerateAction)).ToArray();

            PrimaryKeys            = AllColumns.Where(x => x.IsPrimaryKey).ToArray();
            AllColumnsBySqlName    = AllColumns.ToDictionary(x => x.ColumnName, x => x);
            AllColumnsByMemberName = AllColumns.ToDictionary(x => x.Member.Name, x => x);

            // Build the unique maps
            var uniques = AllColumns
                          .Select(x => new Tuple <ColumnMapping, string[]>(x, x.Member.GetCustomAttributes <UniqueAttribute>().Select(y => y.Group).ToArray()))
                          .Where(x => x.Item2 != null && x.Item2.Length > 0);

            var solos  = uniques.Where(x => x.Item2.Contains(null));
            var groups = uniques
                         .SelectMany(x =>
                                     x.Item2
                                     .Where(y => y != null)
                                     .Select(y => new KeyValuePair <string, ColumnMapping>(y, x.Item1))
                                     )
                         .GroupBy(x => x.Key);

            Uniques =
                solos
                .Select(x => new UniqueMapping(null, new ColumnMapping[] { x.Item1 }))
                .Concat(
                    groups.Select(x => new UniqueMapping(x.Key, x.Select(y => y.Value).ToArray()))
                    ).ToArray();

            ClientGenerated = new HashSet <ColumnMapping>(AllColumns
                                                          .Where(x =>
                                                                 x.AutoGenerateAction == AutoGenerateAction.ClientGenerateGuid ||
                                                                 x.AutoGenerateAction == AutoGenerateAction.ClientChangeTimestamp ||
                                                                 x.AutoGenerateAction == AutoGenerateAction.ClientCreateTimestamp
                                                                 )
                                                          );

            DatabaseGenerated = new HashSet <ColumnMapping>(AllColumns
                                                            .Where(x =>
                                                                   x.AutoGenerateAction == AutoGenerateAction.DatabaseAutoID ||
                                                                   x.AutoGenerateAction == AutoGenerateAction.DatabaseChangeTimestamp ||
                                                                   x.AutoGenerateAction == AutoGenerateAction.DatabaseCreateTimestamp
                                                                   )
                                                            );

            IsPrimaryKeyAutogenerated           = PrimaryKeys.Any(x => x.AutoGenerateAction == AutoGenerateAction.DatabaseAutoID);
            HasDatabaseGeneratedCreateTimestamp = ColumnsWithoutPrimaryKey.Any(x => x.AutoGenerateAction == AutoGenerateAction.DatabaseCreateTimestamp);
            HasDatabaseGeneratedChangeTimestamp = ColumnsWithoutPrimaryKey.Any(x => x.AutoGenerateAction == AutoGenerateAction.DatabaseChangeTimestamp);
            ValidationRules = type.GetCustomAttributes <ValidationBaseAttribute>(true).ToArray();
            AllColumnsWithValidationRules = AllColumns.Where(x => x.ValidationRules.Length > 0).ToArray();
            HasValidationRules            = ValidationRules.Length + AllColumnsWithValidationRules.Length > 0;
        }
Esempio n. 5
0
 /// <summary>
 /// Gets the name for a class
 /// </summary>
 /// <param name="self">The dialect instance to use</param>
 /// <returns>The name.</returns>
 /// <typeparam name="T">The type to get the name from</typeparam>
 public static string GetName <T>(this IDatabaseDialect self) => self.GetName(typeof(T));