コード例 #1
0
 /// <summary>
 /// Extracts the column name from the supplied property
 /// </summary>
 internal static object TypeToColumnName(PropertyInfo pi)
 {
     return(DwarfPropertyAttribute.RequiresAppendedId(pi) ? "[" + pi.Name + "Id]" : "[" + pi.Name + "]");
 }
コード例 #2
0
        /// <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 + ", ");
        }