[hyena/sqlite] [Hyena.Data.Sqlite] Load result data into array



commit f5006cd7fd86f738146040501c0bea5948bec073
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Sat Nov 13 19:06:16 2010 -0600

    [Hyena.Data.Sqlite] Load result data into array
    
    For the same reason we did before with HyenaSqliteArrayDataReader -- we
    put all the result rows' data into managed memory so the sqlite
    statement can be finalized.  Ideally we wouldn't have to do this, but
    for now it's the path of least resistance.  This is only done for
    HyenaSqliteCommand queries, not for queries done directly with the new
    sqlite binding.

 .../Hyena.Data.Sqlite/ArrayDataReader.cs           |  107 ++++++++++++++++++++
 .../Hyena.Data.Sqlite/HyenaSqliteCommand.cs        |   15 +--
 Hyena.Data.Sqlite/Makefile.am                      |    1 +
 3 files changed, 112 insertions(+), 11 deletions(-)
---
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/ArrayDataReader.cs b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/ArrayDataReader.cs
new file mode 100644
index 0000000..16dbd2b
--- /dev/null
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/ArrayDataReader.cs
@@ -0,0 +1,107 @@
+//
+// ArrayDataReader.cs
+//
+// Authors:
+//   Gabriel Burt <gburt novell com>
+//
+// Copyright (C) 2010 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Linq;
+using System.Text;
+using System.Collections;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+
+using Hyena;
+
+namespace Hyena.Data.Sqlite
+{
+    internal class ArrayDataReader : IDataReader
+    {
+        int rows;
+        int row = -1;
+        List<object[]> data = new List<object[]> ();
+
+        internal ArrayDataReader (IDataReader reader)
+        {
+            if (!reader.Read ())
+                return;
+
+            FieldCount = reader.FieldCount;
+            FieldNames = reader.FieldNames;
+
+            do {
+                var vals = new object[FieldCount];
+                for (int i = 0; i < FieldCount; i++) {
+                    vals[i] = reader[i];
+                }
+
+                data.Add (vals);
+                rows++;
+            } while (reader.Read ());
+        }
+
+        public void Dispose ()
+        {
+            row = -1;
+        }
+
+        public int FieldCount { get; private set; }
+        public string [] FieldNames { get; private set; }
+
+        public bool Read ()
+        {
+            row++;
+            return row < rows;
+        }
+
+        public object this[int i] {
+            get { return data[row][i]; }
+        }
+
+        public object this[string columnName] {
+            get { return this[GetColumnIndex (columnName)]; }
+        }
+
+        public T Get<T> (int i)
+        {
+            return (T) Get (i, typeof(T));
+        }
+
+        public object Get (int i, Type asType)
+        {
+            return QueryReader.GetAs (this[i], asType);
+        }
+
+        public T Get<T> (string columnName)
+        {
+            return Get<T> (GetColumnIndex (columnName));
+        }
+
+        private int GetColumnIndex (string columnName)
+        {
+            return Array.IndexOf (FieldNames, columnName);
+        }
+    }
+}
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteCommand.cs b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
index 1e746a3..712baff 100644
--- a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
@@ -60,8 +60,6 @@ namespace Hyena.Data.Sqlite
         private object [] current_values;
         private int ticks;
 
-        private Thread last_thread;
-
         public string Text {
             get { return command; }
         }
@@ -97,10 +95,9 @@ namespace Hyena.Data.Sqlite
 
                 switch (CommandType) {
                     case HyenaCommandType.Reader:
-                        result = connection.Query (command_text);
-                        /*using (SqliteDataReader reader = sql_command.ExecuteReader ()) {
-                            result = new HyenaSqliteArrayDataReader (reader);
-                        }*/
+                        using (var reader = connection.Query (command_text)) {
+                            result = new ArrayDataReader (reader);
+                        }
                         break;
 
                     case HyenaCommandType.Scalar:
@@ -120,6 +117,7 @@ namespace Hyena.Data.Sqlite
                 }
             } catch (Exception e) {
                 Log.DebugFormat ("Exception executing command: {0}", command_text);
+                Log.Exception (e);
                 execution_exception = e;
             }
 
@@ -146,11 +144,6 @@ namespace Hyena.Data.Sqlite
 
         internal object WaitForResult (HyenaSqliteConnection conn)
         {
-            if (last_thread != null && last_thread != Thread.CurrentThread) {
-                Log.WarningFormat ("Calling HyenaSqliteCommand from different thread ({0}) than last time it was ran ({1})\n  sql: {2}", Thread.CurrentThread.Name, last_thread.Name, Text);
-            }
-            last_thread = Thread.CurrentThread;
-
             while (!finished) {
                 conn.ResultReadySignal.WaitOne ();
             }
diff --git a/Hyena.Data.Sqlite/Makefile.am b/Hyena.Data.Sqlite/Makefile.am
index 6f2d697..1495de3 100644
--- a/Hyena.Data.Sqlite/Makefile.am
+++ b/Hyena.Data.Sqlite/Makefile.am
@@ -3,6 +3,7 @@ TARGET = library
 LINK = -r:Mono.Posix -r:System -r:System.Core -r:$(DIR_BIN)/Hyena.dll
 
 SOURCES =  \
+	Hyena.Data.Sqlite/ArrayDataReader.cs \
 	Hyena.Data.Sqlite/Sqlite.cs \
 	Hyena.Data.Sqlite/DatabaseColumn.cs \
 	Hyena.Data.Sqlite/DatabaseColumnAttribute.cs \



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