Beispiel #1
0
 public abstract string CreateColumnQuery(PropertyInfo property, ColumnAttribute column);
Beispiel #2
0
        public override string CreateColumnQuery(PropertyInfo property, ColumnAttribute column)
        {
            if (property == null) throw new ArgumentNullException("property");
            if (column == null) throw new ArgumentNullException("column");

            var type = column.Type ?? property.PropertyType;
            var name = property.Name;
            var result = new StringBuilder(string.Format(" {0} ", name));

            // Тип данных

            if (type == typeof(DateTime)) result.Append("TEXT");
            else if (type == typeof(DateTime?)) result.Append("TEXT");
            else if (type == typeof(int)) result.Append("INTEGER");
            else if (type == typeof(int?)) result.Append("INTEGER");
            else if (type == typeof(long)) result.Append("INTEGER");
            else if (type == typeof(long?)) result.Append("INTEGER");
            else if (type == typeof(float)) result.Append("REAL");
            else if (type == typeof(float?)) result.Append("REAL");
            else if (type == typeof(decimal)) result.Append("REAL");
            else if (type == typeof(decimal?)) result.Append("REAL");
            else if (type == typeof(string)) result.Append("TEXT");
            else if (type == typeof(bool)) result.Append("INTEGER");
            else if (type == typeof(bool?)) result.Append("INTEGER");
            else if (type.IsEnum) result.Append("INTEGER");
            else throw new Exception("Неизвестный тип данных");

            // Ограничения

            if (column.Primary) result.Append(" PRIMARY KEY");
            if (column.AutoIncrement) result.Append(" AUTOINCREMENT");
            if (column.Unique) result.Append(" UNIQUE");
            if (column.NotNull) result.Append(" NOT NULL");
            if (!string.IsNullOrEmpty(column.Default))
                result.AppendFormat(" DEFAULT {0}", column.Default);

            // Результат

            return result.ToString();
        }
Beispiel #3
0
        public override string CreateColumnQuery(PropertyInfo property, ColumnAttribute column)
        {
            var type = column.Type ?? property.PropertyType;
            var name = property.Name;
            var result = new StringBuilder(string.Format(" {0} ", name));

            // Тип данных

            if (type == typeof(XmlDocument)) result.Append("xml");
            else if (type == typeof(object)) result.Append("sql_variant");
            else if (type == typeof(Guid)) result.Append("uniqueidentifier");
            else if (type == typeof(DateTime)) result.Append("datetime2");
            else if (type == typeof(DateTime?)) result.Append("datetime2");
            else if (type == typeof(int)) result.Append("int");
            else if (type == typeof(int?)) result.Append("int");
            else if (type == typeof(long)) result.Append("bigint");
            else if (type == typeof(long?)) result.Append("bigint");
            else if (type == typeof(float)) result.Append("float");
            else if (type == typeof(float?)) result.Append("float");
            else if (type == typeof(decimal)) result.Append("real");
            else if (type == typeof(decimal?)) result.Append("real");
            else if (type == typeof(string)) result.AppendFormat("nvarchar({0})", column.Size);
            else if (type == typeof(bool)) result.Append("bit");
            else if (type == typeof(bool?)) result.Append("bit");
            else if (type.IsEnum) result.Append("int");
            else throw new Exception("Неизвестный тип данных");

            // Ограничения

            if (column.Primary) result.Append(" PRIMARY KEY");
            if (column.AutoIncrement) result.Append(" IDENTITY");
            if (column.Unique) result.Append(" UNIQUE");
            if (column.NotNull) result.Append(" NOT NULL");
            if (!string.IsNullOrEmpty(column.Default))
                result.AppendFormat(" DEFAULT {0}", column.Default);

            // Результат

            return result.ToString();
        }