Re: [Banshee-List] Patch for the DateTime stuff



Works great, please commit.
Thanks!
--Aaron

>>> "Scott Peterson" <lunchtimemama gmail com> 03/13/08 3:17 AM >>>
Attached and inline

Index: src/Libraries/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs	(revision 3439)
+++ src/Libraries/Hyena/Hyena.Data.Sqlite/DatabaseColumn.cs	(working copy)
@@ -84,26 +84,8 @@
         public void SetValue (object target, IDataReader reader, int column)
         {
             // FIXME should we insist on nullable types?
-            object result;
-            if (type == typeof (string)) {
-                result = !reader.IsDBNull (column)
-                    ? String.Intern (reader.GetString (column))
-                    : null;
-            } else if (type == typeof (int)) {
-                result = !reader.IsDBNull (column)
-                    ? reader.GetInt32 (column)
-                    : 0;
-            } else if (type == typeof (int)) {
-                result = !reader.IsDBNull (column)
-                    ? Convert.ToInt64 (reader.GetValue (column))
-                    : Int64.MinValue;
-            } else {
-                result = !reader.IsDBNull (column)
-                    ? Convert.ToInt64 (reader.GetValue (column))
-                    : 0;
-            }
-            result = SqliteUtils.FromDbFormat (type, result);
-            SetValue (target, result);
+            object value = reader.IsDBNull (column) ? null :
reader.GetValue (column);
+            SetValue (target, SqliteUtils.FromDbFormat(type, value));
         }

         public void SetValue (object target, object value)
Index: src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteUtils.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteUtils.cs	(revision 3439)
+++ src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteUtils.cs	(working copy)
@@ -27,6 +27,7 @@
 //

 using System;
+using System.Reflection;
 using System.Text;

 namespace Hyena.Data.Sqlite
@@ -49,13 +50,13 @@
         public static object ToDbFormat (Type type, object value)
         {
             if (type == typeof (DateTime)) {
-                if (DateTime.MinValue.Equals (value)) {
-                    value = "NULL";
-                } else {
-                    value = DateTimeUtil.FromDateTime ((DateTime)value);
-                }
+                return DateTime.MinValue.Equals ((DateTime)value)
+                    ? (object)null
+                    : DateTimeUtil.FromDateTime ((DateTime)value);
             } else if (type == typeof (TimeSpan)) {
-                value = ((TimeSpan)value).TotalMilliseconds;
+                return TimeSpan.MinValue.Equals ((TimeSpan)value)
+                    ? (object)null
+                    : ((TimeSpan)value).TotalMilliseconds;
             }
             return value;
         }
@@ -63,11 +64,22 @@
         public static object FromDbFormat (Type type, object value)
         {
             if (type == typeof (DateTime)) {
-                value = Int64.MinValue.Equals (value) ?
DateTime.MinValue : DateTimeUtil.ToDateTime ((long) value);
+                return value == null
+                    ? DateTime.MinValue
+                    : DateTimeUtil.ToDateTime (Convert.ToInt64 (value));
             } else if (type == typeof (TimeSpan)) {
-                value = TimeSpan.FromMilliseconds ((long)value);
+                return value == null
+                    ? TimeSpan.MinValue
+                    : TimeSpan.FromMilliseconds (Convert.ToInt64 (value));
+            } else if (value == null) {
+                if (type.IsValueType) {
+                    return Activator.CreateInstance (type);
+                } else {
+                    return null;
+                }
+            } else {
+                return Convert.ChangeType (value, type);
             }
-            return value;
         }

         public static string BuildColumnSchema (string type,
Index: src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
===================================================================
--- src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteConnection.cs	(revision
3439)
+++ src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteConnection.cs	(working
copy)
@@ -109,9 +109,7 @@
                 result = command.WaitForResult (this);
             }

-            return result == null
-                ? default (T)
-                : (T) SqliteUtils.FromDbFormat (typeof (T),
Convert.ChangeType (result, typeof (T)));
+            return (T)SqliteUtils.FromDbFormat (typeof (T), result);
         }

         public T Query<T> (HyenaSqliteCommand command, params object
[] param_values)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]