[hyena] Sqlite: Return rowid values as long instead of int (bgo#696520)



commit 11687d9788fe585f4b3ae743d3e1b6bd86729f75
Author: Bertrand Lorentz <bertrand lorentz gmail com>
Date:   Sun May 19 17:35:32 2013 +0200

    Sqlite: Return rowid values as long instead of int (bgo#696520)
    
    When the command type is "execute", the result is the rowid of the last
    inserted row (connection.LastInsertRowId), which is a long.
    
    We now return the value as a long, instead of converting it to an int
    ourselves, and throwing an exception when the value is above
    Int32.MaxValue.
    
    In SQLite, a column defined as INTEGER PRIMARY KEY is used as the rowid,
    so it should now be handled as a long.
    
    As a consequence, for a class using SqliteModelProvider, the member
    marked as the primary key must be a long. Previously, it was assumed to
    be assignable from an int, so we add a specific runtime check for that.

 .../Hyena.Data.Sqlite/HyenaSqliteConnection.cs     |   12 ++++++------
 .../Hyena.Data.Sqlite/SqliteModelProvider.cs       |   11 ++++++++---
 .../Hyena.Data.Sqlite/Tests/DbBoundType.cs         |    2 +-
 3 files changed, 15 insertions(+), 10 deletions(-)
---
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteConnection.cs 
b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
index a87e513..9394754 100644
--- a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
@@ -215,26 +215,26 @@ namespace Hyena.Data.Sqlite
         }
 
         // INSERT, UPDATE, DELETE queries
-        public int Execute (HyenaSqliteCommand command)
+        public long Execute (HyenaSqliteCommand command)
         {
             command.CommandType = HyenaCommandType.Execute;;
             QueueCommand(command);
-            return Convert.ToInt32 (command.WaitForResult (this));
+            return (long)command.WaitForResult (this);
         }
 
-        public int Execute (HyenaSqliteCommand command, params object [] param_values)
+        public long Execute (HyenaSqliteCommand command, params object [] param_values)
         {
             command.CommandType = HyenaCommandType.Execute;;
             QueueCommand(command, param_values);
-            return Convert.ToInt32 (command.WaitForResult (this));
+            return (long)command.WaitForResult (this);
         }
 
-        public int Execute (string command_str, params object [] param_values)
+        public long Execute (string command_str, params object [] param_values)
         {
             return Execute (new HyenaSqliteCommand (command_str, param_values) { ReaderDisposes = true });
         }
 
-        public int Execute (object command)
+        public long Execute (object command)
         {
             return Execute (new HyenaSqliteCommand (command.ToString ()) { ReaderDisposes = true });
         }
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelProvider.cs 
b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelProvider.cs
index 28f1a5b..e10ded5 100644
--- a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelProvider.cs
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelProvider.cs
@@ -262,6 +262,11 @@ namespace Hyena.Data.Sqlite
                         "Multiple primary keys in the {0} table", TableName)
                     );
                 }
+                if (!c.ValueType.IsAssignableFrom (typeof (long))) {
+                    throw new Exception (String.Format (
+                        "Primary key {0} in the {1} class must be of type 'long'", c.Name, typeof(T))
+                    );
+                }
                 key = c;
             }
         }
@@ -290,7 +295,7 @@ namespace Hyena.Data.Sqlite
         public virtual void Save (T target, bool force_insert)
         {
             try {
-                if (Convert.ToInt32 (key.GetRawValue (target)) > 0 && !force_insert) {
+                if (Convert.ToInt64 (key.GetRawValue (target)) > 0 && !force_insert) {
                     Update (target);
                 } else {
                     key.SetValue (target, Insert (target));
@@ -322,7 +327,7 @@ namespace Hyena.Data.Sqlite
             return values;
         }
 
-        protected int Insert (T target)
+        protected long Insert (T target)
         {
             return connection.Execute (InsertCommand, GetInsertParams (target));
         }
@@ -478,7 +483,7 @@ namespace Hyena.Data.Sqlite
             if (key == null || item == null)
                 return false;
 
-            int id = (int) key.GetValue (item);
+            long id = (long) key.GetValue (item);
             if (id < 1)
                 return false;
 
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Tests/DbBoundType.cs 
b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Tests/DbBoundType.cs
index 58b7dc6..b11591b 100644
--- a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Tests/DbBoundType.cs
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Tests/DbBoundType.cs
@@ -81,7 +81,7 @@ namespace Hyena.Data.Sqlite.Tests
     internal class DbBoundType
     {
         [DatabaseColumn ("PrimaryKey", Constraints = DatabaseColumnConstraints.PrimaryKey)]
-        public int PrimaryKey;
+        public long PrimaryKey;
 
         [DatabaseColumn ("PublicIntField")]
         public int PublicIntField;


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