/// <summary>
        /// Returns true if the base type of supplied property info is nullable
        /// </summary>
        internal static bool IsColumnNullable(Type type, PropertyInfo pi)
        {
            var att = DwarfPropertyAttribute.GetAttribute(pi);

            if (att == null)
            {
                return(true);
            }

            if (att.IsPrimaryKey)
            {
                return(false);
            }

            if (pi.Name.Equals("Id"))
            {
                return(false);
            }

            if (DwarfPropertyAttribute.IsFK(pi))
            {
                return(att.IsNullable);
            }

            if (pi.PropertyType == typeof(string))
            {
                return(true);
            }

            if (att.IsNullable)
            {
                return(true);
            }

            return(pi.PropertyType.IsGenericType);
        }
        /// <summary>
        /// Returns the base sql type for the supplied property
        /// </summary>
        internal static string TypeToColumnType(PropertyInfo pi)
        {
            var value = String.Empty;

            if (pi.Name.Equals("Id"))
            {
                value = "uniqueidentifier";
            }
            else if (DwarfPropertyAttribute.IsFK(pi))
            {
                value = "uniqueidentifier";
            }
            else if (pi.PropertyType.Implements <int?>())
            {
                value = "int";
            }
            else if (pi.PropertyType.Implements <decimal?>())
            {
                value = "decimal";
            }
            else if (pi.PropertyType.Implements <double?>())
            {
                value = "float";
            }
            else if (pi.PropertyType.Implements <DateTime?>())
            {
                value = "datetime";
            }
            else if (pi.PropertyType == typeof(string))
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.IsEnum())
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.Implements <bool?>())
            {
                value = "bit";
            }
            else if (pi.PropertyType.Implements <Guid?>())
            {
                value = "uniqueidentifier";
            }
            else if (pi.PropertyType.Implements <IGem>())
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.Implements <IGemList>())
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.Implements <Type>())
            {
                value = "nvarchar";
            }
            else if (pi.PropertyType.Implements <byte[]>())
            {
                value = "varbinary";
            }

            if (string.IsNullOrEmpty(value))
            {
                throw new InvalidOperationException(pi.Name + "'s type (" + pi.PropertyType + ") isn't supported.");
            }

            return(value);
        }
        /// <summary>
        /// Returns the sql needed to construct the supplied property as a column
        /// </summary>
        internal static string TypeToColumnConstruction(Type type, PropertyInfo pi, bool skipConstraint = false)
        {
            var value = TypeToColumnType(pi);

            var att = DwarfPropertyAttribute.GetAttribute(pi);

            if (pi.Name.Equals("Id"))
            {
                value += " NOT NULL";
            }
            else if (DwarfPropertyAttribute.IsFK(pi))
            {
                value += att.IsNullable ? string.Empty : " NOT NULL";
            }
            else if (pi.PropertyType == typeof(string))
            {
                value += att.UseMaxLength ? "(max)" : "(255)";
            }
            else if (pi.PropertyType.Implements <IGem>())
            {
                value += "(255)" + (att.IsNullable ? string.Empty : " NOT NULL");
            }
            else if (pi.PropertyType.Implements <IGemList>())
            {
                value += "(max)";
            }
            else if (pi.PropertyType.Implements <byte[]>())
            {
                value += "(max)";
            }
            else if (pi.PropertyType.Implements <Type>())
            {
                value += "(255)";
            }
            else if (pi.PropertyType.Implements <decimal?>())
            {
                value += "(28, 8)";
            }
            else if (pi.PropertyType.IsEnum())
            {
                value += "(255)" + (pi.PropertyType.IsGenericType ? string.Empty : " NOT NULL");
            }
            else if (!pi.PropertyType.IsGenericType && !att.IsNullable)
            {
                value += " NOT NULL";
            }

            if (att != null && att.IsUnique)
            {
                if (!skipConstraint)
                {
                    value += string.Format(" CONSTRAINT [UQ_{0}_{1}{2}] UNIQUE", type.Name, pi.Name, DwarfPropertyAttribute.RequiresAppendedId(pi) ? "Id" : string.Empty);
                }
                else
                {
                    value += "/* WARNING! You might manually have to drop and recreate any Unique Constraint*/";
                }
            }

            if (string.IsNullOrEmpty(value))
            {
                throw new InvalidOperationException(type.Name + "." + pi.Name + "'s type (" + pi.PropertyType + ") isn't supported.");
            }

            return(value + ", ");
        }
Example #4
0
        /// <summary>
        /// Converts an SqlDataReader row into an object of the type T
        /// </summary>
        protected T TupleToObject <T>(DbDataReader sdr) where T : Dwarf <T>, new()
        {
            var type = typeof(T);

            var obj = DwarfHelper.CreateInstance <T>();

            for (var i = 0; i < sdr.FieldCount; i++)
            {
                var propertyName  = sdr.GetName(i);
                var propertyValue = sdr.GetValue(i);

                var pi = PropertyHelper.GetProperty(type, propertyName) ?? PropertyHelper.GetProperty(type, propertyName.Replace("Id", string.Empty));

                if (pi == null)
                {
                    continue;
                }

                if (propertyValue is DBNull)
                {
                    propertyValue = pi.ContainedProperty.PropertyType == typeof(string) ? string.Empty : null;
                }
                else if (pi.PropertyType.Implements <IGem>())
                {
                    propertyValue = Cfg.LoadGem[pi.PropertyType](propertyValue.ToString());
                }
                else if (pi.PropertyType.IsEnum() && propertyValue != null)
                {
                    propertyValue = Enum.Parse(pi.PropertyType.GetTrueEnumType(), propertyValue.ToString());
                }
                else if (pi.PropertyType.Implements <Type>())
                {
                    propertyValue = typeof(T).Assembly.GetType(propertyValue.ToString());
                }
                else if (pi.PropertyType.Implements <IDwarf>())
                {
                    var att = DwarfPropertyAttribute.GetAttribute(pi.ContainedProperty);

                    if (att != null)
                    {
                        if (DwarfPropertyAttribute.IsFK(pi))
                        {
                            obj.SetOriginalValue(pi.Name, propertyValue);

                            if (att.EagerLoad)
                            {
                                var targetType = pi.PropertyType;

                                if (propertyValue != null)
                                {
                                    PropertyHelper.SetValue(obj, pi.Name, Cfg.LoadExpressions[targetType]((Guid)propertyValue));
                                }
                            }

                            continue;
                        }
                    }
                }

                if (pi.PropertyType == typeof(string) && propertyValue != null)
                {
                    propertyValue = propertyValue.ToString().Replace("\\r\\n", "\r\n");
                }

                PropertyHelper.SetValue(obj, pi.Name, propertyValue);
                obj.SetOriginalValue(pi.Name, propertyValue);
            }

            obj.IsSaved = true;

            return(obj);
        }