xll
xll

Reputation: 3109

nHibernate with SQLite format date

I am using nHibernate + FluentNHibernate with SQLite database. All dates are stored as text in format 'YYYY-MM-DD HH:MM:SS'

How can I instruct nHibernate (or sqlite driver???) to truncate time part and store date in 'YYYY-MM-DD' format for certain fields (and storing the full date for other fields) ?

Or how can I instruct to store all dates as integers ?

I am concerned on space usage, because dates takes a lot of space in the database and there are indexes on dates too, so I need to make them taking as less space as possible.

Upvotes: 1

Views: 1108

Answers (1)

SHSE
SHSE

Reputation: 2433

If you convert DateTime to String manually, I can't see any problem to replace string format by YYYY-MM-DD.

If you let NHibernate to perform this conversion, then you could create custom NHibernate UserType like so:

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);
    }
}

And map your DateTime property using either TruncatedDateTimeAsStringType or FullDateTimeAsStringType:

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

Upvotes: 2

Related Questions