Ejemplo n.º 1
0
        public static string GetMonthlyCreateTableSQL <T>(DateTime time) where T : IMonthly
        {
            StringBuilder fileds    = new StringBuilder();
            bool          isPrimary = false;

            Type type = typeof(T);

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                Navigation navigationType = (Navigation)Attribute.GetCustomAttribute(property, typeof(Navigation));
                if (navigationType != null)
                {
                    continue;
                }

                fileds.AppendFormat(" [{0}]", property.Name);
                fileds.AppendFormat(" [{0}]", TypeConverter.GetSQLType(property.PropertyType));

                PropertyType propertyType = (PropertyType)Attribute.GetCustomAttribute(property, typeof(PropertyType));
                if (propertyType != null && propertyType.IsPrimaryKey)
                {
                    isPrimary = true;
                    fileds.AppendFormat(" {0}", PrimaryConverter.GetDefaultPrimaryFiledString());
                }

                if (TypeConverter.IsNullableType(property.PropertyType))
                {
                    fileds.AppendFormat(" {0},", DefaultValueConverter.GetDefaultNullValue());
                }
                else
                {
                    fileds.AppendFormat(" {0},", DefaultValueConverter.GetDefaultNOTNullValue());
                }
            }

            string monthString = Convert.ToString(time.Year).PadLeft(4, '0');

            monthString += Convert.ToString(time.Month).PadLeft(2, '0');

            StringBuilder commandBuilder = new StringBuilder();

            commandBuilder.AppendFormat("CREATE TABLE {0} ({1}) ", type.Name + monthString, fileds.ToString() + "");
            if (isPrimary)
            {
                commandBuilder.Append(PrimaryConverter.GetDefaultPrimaryTableEndString());
            }

            commandBuilder.Append(";");
            return(commandBuilder.ToString());
        }
Ejemplo n.º 2
0
        public static string GetCreateTableSQL <T>(T t) where T : IEntity
        {
            StringBuilder fileds    = new StringBuilder();
            bool          isPrimary = false;

            Type type = typeof(T);

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                Navigation navigationType = (Navigation)Attribute.GetCustomAttribute(property, typeof(Navigation));
                if (navigationType != null)
                {
                    continue;
                }

                fileds.AppendFormat("[{0}]", property.Name);
                fileds.AppendFormat(" [{0}]", TypeConverter.GetSQLType(property.PropertyType));

                PropertyType propertyType = (PropertyType)Attribute.GetCustomAttribute(property, typeof(PropertyType));
                if (propertyType != null && propertyType.IsPrimaryKey)
                {
                    isPrimary = true;
                    fileds.AppendFormat(" {0}", PrimaryConverter.GetDefaultPrimaryFiledString());
                }

                if (TypeConverter.IsNullableType(property.PropertyType))
                {
                    fileds.AppendFormat(" {0}", DefaultValueConverter.GetDefaultNullValue());
                }
                else
                {
                    fileds.AppendFormat(" {0}", DefaultValueConverter.GetDefaultNOTNullValue());
                }
            }

            StringBuilder commandBuilder = new StringBuilder();

            commandBuilder.AppendFormat("CREATE TABLE {0} ({1}) ", type.Name, fileds.ToString());
            if (isPrimary)
            {
                commandBuilder.Append(PrimaryConverter.GetDefaultPrimaryTableEndString());
            }

            return(commandBuilder.ToString());
        }