2012-10-20 145 views
1

我使用nHibernate + FluentNHibernate與SQLite數據庫。 所有日期都以文本格式'YYYY-MM-DD HH:MM:SS'存儲。nHibernate與SQLite格式日期

如何指示nHibernate(或sqlite驅動程序???)將時間部分和存儲日期截斷爲'YYYY-MM --DD'格式的某些字段(並存儲其他字段的完整日期)?

或者我該如何指導將所有日期存儲爲整數?

我很關心空間使用情況,因爲日期需要數據庫中的大量空間,並且日期也有索引,所以我需要儘可能減少空間。

回答

2

如果您手工將DateTime轉換爲String,我看不到任何問題以將字符串格式替換爲YYYY-MM-DD

如果讓NHibernate的執行此轉換,那麼你可以創建自定義的NHibernate UserType像這樣:

public abstract class DateTimeAsStringType : IUserType 
{ 
    public object Assemble(object cached, object owner) 
    { 
     return cached; 
    } 

    public object DeepCopy(object value) 
    { 
     return value; 
    } 

    public object Disassemble(object value) 
    { 
     return value; 
    } 

    public bool Equals(object x, object y) 
    { 
     if (ReferenceEquals(x, y)) 
      return true; 

     if (x == null && y == null) 
      return false; 

     return x.Equals(y); 
    } 

    public int GetHashCode(object x) 
    { 
     return x.GetHashCode(); 
    } 

    public bool IsMutable 
    { 
     get { return false; } 
    } 

    public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner) 
    { 
     var serialized = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string; 

     if (string.IsNullOrEmpty(serialized)) 
      return null; 

     return Deserialize(serialized); 
    } 

    protected abstract DateTime Deserialize(string value); 
    protected abstract string Serialize(DateTime value); 

    public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index) 
    { 
     if (value == null) 
      NHibernateUtil.String.NullSafeSet(cmd, DBNull.Value, index); 
     else 
      NHibernateUtil.String.NullSafeSet(cmd, Serialize((DateTime)value), index); 
    } 

    public object Replace(object original, object target, object owner) 
    { 
     return original; 
    } 

    public Type ReturnedType 
    { 
     get { return typeof(DateTime); } 
    } 

    public NHibernate.SqlTypes.SqlType[] SqlTypes 
    { 
     get { return new[] { NHibernateUtil.String.SqlType }; } 
    } 
} 

public class TruncatedDateTimeAsStringType : DateTimeAsStringType 
{ 
    private const string Format = "yyyy-MM-dd"; 

    protected override string Serialize(DateTime value) 
    { 
     return value.ToString(Format, CultureInfo.InvariantCulture); 
    } 

    protected override DateTime Deserialize(string value) 
    { 
     return DateTime.ParseExact(value, Format, CultureInfo.InvariantCulture, DateTimeStyles.None); 
    } 
} 

public class FullDateTimeAsStringType : DateTimeAsStringType 
{ 
    private const string Format = "yyyy-MM-dd hh:mm:ss"; 

    protected override string Serialize(DateTime value) 
    { 
     return value.ToString(Format, CultureInfo.InvariantCulture); 
    } 

    protected override DateTime Deserialize(string value) 
    { 
     return DateTime.ParseExact(value, Format, CultureInfo.InvariantCulture, DateTimeStyles.None); 
    } 
} 

而且使用無論是TruncatedDateTimeAsStringTypeFullDateTimeAsStringType映射您DateTime屬性:

<property name="TruncatedDateTime" type="Your.Namespance.TruncatedDateTimeAsStringType, Your.Assembly" /> 
<property name="NotTruncatedDateTime" type="Your.Namespance.FullDateTimeAsStringType, Your.Assembly" /> 
+0

這只是工作。謝謝。 – xll