[hyena] Sync Hyena.Data.Sqlite with Banshee



commit cb131ddfbe7c49d859485dc1716156ed7245c2e0
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Tue Jan 12 12:08:17 2010 -0800

    Sync Hyena.Data.Sqlite with Banshee

 .../HyenaSqliteArrayDataReader.cs                  |   27 +++++++------
 .../Hyena.Data.Sqlite/HyenaSqliteCommand.cs        |   14 +++----
 .../Hyena.Data.Sqlite/HyenaSqliteConnection.cs     |   16 +++-----
 .../Hyena.Data.Sqlite/SqliteModelCache.cs          |   41 +++++++++++++++++++-
 .../Hyena.Data.Sqlite/SqliteModelProvider.cs       |    3 +
 5 files changed, 68 insertions(+), 33 deletions(-)
---
diff --git a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteArrayDataReader.cs b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteArrayDataReader.cs
index 1f89a1f..7653995 100644
--- a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteArrayDataReader.cs
+++ b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteArrayDataReader.cs
@@ -34,6 +34,7 @@ using System.Data;
 using System.Data.Common;
 using System.Text;
 using System.Collections;
+using System.Collections.Generic;
 using Mono.Data.Sqlite;
 
 namespace Hyena.Data.Sqlite
@@ -52,9 +53,9 @@ namespace Hyena.Data.Sqlite
     {
         #region Fields
 
-        private System.Collections.Generic.List<object[]> rows;
+        private List<object[]> rows;
         private string[] columns;
-        private Hashtable column_names_sens, column_names_insens;
+        private Dictionary<string, int> column_names;
         private int current_row;
         private bool closed;
         private int records_affected;
@@ -66,9 +67,8 @@ namespace Hyena.Data.Sqlite
 
         internal HyenaSqliteArrayDataReader (SqliteDataReader reader)
         {
-            rows = new System.Collections.Generic.List<object[]> ();
-            column_names_sens = new Hashtable ();
-            column_names_insens = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
+            rows = new List<object[]> ();
+            column_names = new Dictionary<string, int> ();
             closed = false;
             current_row = -1;
             ReadAllRows (reader);
@@ -123,8 +123,8 @@ namespace Hyena.Data.Sqlite
             for (ii = 0; ii < field_count; ii++) {
                     string column_name = reader.GetName (ii);
                     columns[ii] = column_name;
-                    column_names_sens[column_name] = ii;
-                    column_names_insens[column_name] = ii;
+                    column_names[column_name] = ii;
+                    column_names[column_name.ToUpper ()] = ii;
             }
 
             /* Read all rows, store in this->rows */
@@ -349,12 +349,13 @@ namespace Hyena.Data.Sqlite
 
         public int GetOrdinal (string name)
         {
-            object v = column_names_sens[name];
-            if (v == null)
-                v = column_names_insens[name];
-            if (v == null)
-                throw new ArgumentException("Column does not exist.");
-            return (int) v;
+            int v = -1;
+            if (!column_names.TryGetValue (name, out v)) {
+                if (!column_names.TryGetValue (name.ToUpper (), out v)) {
+                    throw new ArgumentException("Column does not exist.");
+                }
+            }
+            return v;
         }
 
         public string GetString (int i)
diff --git a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteCommand.cs b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
index be45a9e..b0970d3 100644
--- a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
+++ b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
@@ -81,11 +81,7 @@ namespace Hyena.Data.Sqlite
             get { return command; }
         }
 
-        private HyenaCommandType command_type;
-        internal HyenaCommandType CommandType {
-            get { return command_type; }
-            set { command_type = value; }
-        }
+        internal HyenaCommandType CommandType;
 
 #endregion
 
@@ -118,7 +114,7 @@ namespace Hyena.Data.Sqlite
                     if (log_all)
                         ticks = System.Environment.TickCount;
 
-                    switch (command_type) {
+                    switch (CommandType) {
                         case HyenaCommandType.Reader:
                             using (SqliteDataReader reader = sql_command.ExecuteReader ()) {
                                 result = new HyenaSqliteArrayDataReader (reader);
@@ -159,7 +155,9 @@ namespace Hyena.Data.Sqlite
                 conn.ResultReadySignal.WaitOne ();
             }
 
+            // Reference the results since they could be overwritten
             object ret = result;
+            var exception = execution_exception;
 
             // Reset to false in case run again
             finished = false;
@@ -167,8 +165,8 @@ namespace Hyena.Data.Sqlite
             conn.ClaimResult ();
             finished_event.Set ();
 
-            if (execution_exception != null) {
-                throw execution_exception;
+            if (exception != null) {
+                throw exception;
             }
 
             return ret;
diff --git a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteConnection.cs b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
index cad33e5..037c192 100644
--- a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
+++ b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteConnection.cs
@@ -100,7 +100,7 @@ namespace Hyena.Data.Sqlite
         private volatile bool dispose_requested = false;
         private volatile int results_ready = 0;
         private AutoResetEvent queue_signal = new AutoResetEvent (false);
-        private ManualResetEvent result_ready_signal = new ManualResetEvent (false);
+        internal ManualResetEvent ResultReadySignal = new ManualResetEvent (false);
 
         private volatile Thread transaction_thread = null;
         private ManualResetEvent transaction_signal = new ManualResetEvent (true);
@@ -111,10 +111,6 @@ namespace Hyena.Data.Sqlite
             set { warn_if_called_from_thread = value; }
         }
 
-        internal ManualResetEvent ResultReadySignal {
-            get { return result_ready_signal; }
-        }
-
         public event EventHandler<ExecutingEventArgs> Executing;
 
         public HyenaSqliteConnection(string dbpath)
@@ -218,14 +214,14 @@ namespace Hyena.Data.Sqlite
         {
             command.CommandType = HyenaCommandType.Execute;;
             QueueCommand(command);
-            return (int) command.WaitForResult (this);
+            return Convert.ToInt32 (command.WaitForResult (this));
         }
 
         public int Execute (HyenaSqliteCommand command, params object [] param_values)
         {
             command.CommandType = HyenaCommandType.Execute;;
             QueueCommand(command, param_values);
-            return (int) command.WaitForResult (this);
+            return Convert.ToInt32 (command.WaitForResult (this));
         }
 
         public int Execute (string command_str, params object [] param_values)
@@ -402,9 +398,9 @@ namespace Hyena.Data.Sqlite
         internal void ClaimResult ()
         {
             lock (command_queue) {
-                results_ready++;
+                results_ready--;
                 if (results_ready == 0) {
-                    result_ready_signal.Reset ();
+                    ResultReadySignal.Reset ();
                 }
             }
         }
@@ -443,7 +439,7 @@ namespace Hyena.Data.Sqlite
 
                     lock (command_queue) {
                         results_ready++;
-                        result_ready_signal.Set ();
+                        ResultReadySignal.Set ();
                     }
                 }
 
diff --git a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelCache.cs b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelCache.cs
index c4a8344..269291a 100644
--- a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelCache.cs
+++ b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelCache.cs
@@ -30,6 +30,9 @@
 using System;
 using System.Collections.Generic;
 using System.Data;
+using System.Text;
+
+using Hyena.Query;
 
 namespace Hyena.Data.Sqlite
 {
@@ -45,9 +48,11 @@ namespace Hyena.Data.Sqlite
         private HyenaSqliteCommand delete_selection_command;
         private HyenaSqliteCommand save_selection_command;
         private HyenaSqliteCommand get_selection_command;
+        private HyenaSqliteCommand indexof_command;
 
         private string select_str;
         private string reload_sql;
+        private string last_indexof_where_fragment;
         private long uid;
         private long selection_uid;
         private long rows;
@@ -102,7 +107,7 @@ namespace Hyena.Data.Sqlite
 
             if (model.CachesJoinTableEntries) {
                 select_str = String.Format (
-                    @"SELECT {0}, {5}.ItemID  FROM {1}
+                    @"SELECT {0}, OrderID, {5}.ItemID  FROM {1}
                         INNER JOIN {2}
                             ON {3} = {2}.{4}
                         INNER JOIN {5}
@@ -134,7 +139,7 @@ namespace Hyena.Data.Sqlite
                 );
             } else {
                 select_str = String.Format (
-                    @"SELECT {0}, {2}.ItemID FROM {1}
+                    @"SELECT {0}, OrderID, {2}.ItemID FROM {1}
                         INNER JOIN {2}
                             ON {3} = {2}.ItemID
                         WHERE
@@ -228,6 +233,38 @@ namespace Hyena.Data.Sqlite
              }
         }
 
+        public long IndexOf (string where_fragment, long offset)
+        {
+            if (String.IsNullOrEmpty (where_fragment)) {
+                return -1;
+            }
+
+            if (!where_fragment.Equals (last_indexof_where_fragment)) {
+                last_indexof_where_fragment = where_fragment;
+
+                if (!where_fragment.Trim ().ToLower ().StartsWith ("and ")) {
+                    where_fragment = " AND " + where_fragment;
+                }
+
+                string sql = String.Format ("{0} {1} LIMIT ?, 1", select_str, where_fragment);
+                indexof_command = new HyenaSqliteCommand (sql);
+            }
+
+            lock (this) {
+                using (IDataReader reader = connection.Query (indexof_command, offset)) {
+                    if (reader.Read ()) {
+                        long target_id = (long) reader[reader.FieldCount - 2];
+                        if (target_id == 0) {
+                            return -1;
+                        }
+                        return target_id - FirstOrderId;
+                    }
+                }
+
+                return -1;
+            }
+        }
+
         public long IndexOf (ICacheableItem item)
         {
             if (item == null || item.CacheModelId != CacheId) {
diff --git a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelProvider.cs b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelProvider.cs
index 15beb74..2ddd0cd 100644
--- a/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelProvider.cs
+++ b/src/Hyena.Data.Sqlite/Hyena.Data.Sqlite/SqliteModelProvider.cs
@@ -488,6 +488,9 @@ namespace Hyena.Data.Sqlite
                     column.SetValue (copy, column.GetRawValue (original));
                 }
             }
+            foreach (var column in virtual_columns) {
+                column.SetValue (copy, column.GetRawValue (original));
+            }
         }
 
         protected virtual HyenaSqliteCommand CreateCommand {



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