[banshee] Allow force insert in SqliteModelProvider.Save ().



commit b6a4a7542a7df8b5c9c8d853e74b1ff3ff8c8b68
Author: Ruben Vermeersch <ruben savanne be>
Date:   Fri Mar 26 01:38:27 2010 +0100

    Allow force insert in SqliteModelProvider.Save ().
    
    Adds a Save () overload to SqliteModelProvider which takes a second boolean
    parameter to cause a forced insert.
    
    This is needed for cases where you want to preinitialize the primary key to a
    certain value. Simply calling Save () would cause the model provider to think
    that this is an existing object, causing an update to happen. Result: object
    not saved.
    
    Example use case:
    
        private class CacheParameters
        {
            [DatabaseColumn(Constraints = DatabaseColumnConstraints.PrimaryKey)]
            public int CacheId { get; set; }
    
            [DatabaseColumn]
            public string Parameters { get; set; }
        }
    
    In this class, the CacheId parameter is set to match the id of objects in
    another table. Without the overload, there is no way to save it.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=613981

 .../Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
---
diff --git a/src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs b/src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs
index affa723..048ab8e 100644
--- a/src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs
+++ b/src/Libraries/Hyena/Hyena.Data.Sqlite/SqliteModelProvider.cs
@@ -288,10 +288,10 @@ namespace Hyena.Data.Sqlite
             ));
         }
 
-        public virtual void Save (T target)
+        public virtual void Save (T target, bool force_insert)
         {
             try {
-                if (Convert.ToInt32 (key.GetRawValue (target)) > 0) {
+                if (Convert.ToInt32 (key.GetRawValue (target)) > 0 && !force_insert) {
                     Update (target);
                 } else {
                     key.SetValue (target, Insert (target));
@@ -301,6 +301,12 @@ namespace Hyena.Data.Sqlite
                 Hyena.Log.DebugFormat ("type of key value: {0}", key.GetRawValue (target).GetType ());
                 throw;
             }
+
+        }
+
+        public virtual void Save (T target)
+        {
+            Save (target, false);
         }
 
         protected virtual object [] GetInsertParams (T target)



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