[hyena/sqlite] [Hyena.Data.Sqlite] Ensure readers disposed, add funcs



commit 49b897f695b8e71abb5ce4a8a3e045d4d16fe3bc
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Fri Nov 12 14:14:17 2010 -0600

    [Hyena.Data.Sqlite] Ensure readers disposed, add funcs
    
    Any Query, Query<T>, or Execute command should result in the Statement's
    QueryReader being disposed before any further binding or querying.

 Hyena.Data.Sqlite/Hyena.Data.Sqlite/Sqlite.cs      |   19 ++++-
 .../Hyena.Data.Sqlite/Tests/SqliteTests.cs         |   81 +++++++++++++-------
 2 files changed, 68 insertions(+), 32 deletions(-)
---
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Sqlite.cs b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Sqlite.cs
index 1b285ac..f818161 100644
--- a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Sqlite.cs
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Sqlite.cs
@@ -27,7 +27,13 @@ namespace Hyena.Data.Sqlite
             if (ptr == IntPtr.Zero)
                 throw new Exception ("Unable to open connection");
 
+            Console.WriteLine ("Opened connection to {0}", dbPath);
             Native.sqlite3_extended_result_codes (ptr, 1);
+
+            AddFunction<BinaryFunction> ();
+            AddFunction<CollationKeyFunction> ();
+            AddFunction<SearchKeyFunction> ();
+            AddFunction<Md5Function> ();
         }
 
         public void Dispose ()
@@ -298,8 +304,10 @@ namespace Hyena.Data.Sqlite
         private void Reset ()
         {
             CheckDisposed ();
+            if (Reading)
+                throw new InvalidOperationException ("Can't reset statement while it's being read; make sure to Dispose any IDataReaders");
+
             CheckError (Native.sqlite3_reset (ptr));
-            Reading = false;
         }
 
         public IEnumerator<IDataReader> GetEnumerator ()
@@ -318,14 +326,18 @@ namespace Hyena.Data.Sqlite
         public Statement Execute ()
         {
             Reset ();
-            reader.Read ();
+            using (reader) {
+                reader.Read ();
+            }
             return this;
         }
 
         public T Query<T> ()
         {
             Reset ();
-            return reader.Read () ? reader.Get<T> (0) : (T) SqliteUtils.FromDbFormat <T> (null);
+            using (reader) {
+                return reader.Read () ? reader.Get<T> (0) : (T) SqliteUtils.FromDbFormat <T> (null);
+            }
         }
 
         public QueryReader Query ()
@@ -345,6 +357,7 @@ namespace Hyena.Data.Sqlite
 
         public void Dispose ()
         {
+            Statement.Reading = false;
             if (Statement.ReaderDisposes) {
                 Statement.Dispose ();
             }
diff --git a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Tests/SqliteTests.cs b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Tests/SqliteTests.cs
index 8246344..6eb1be7 100644
--- a/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Tests/SqliteTests.cs
+++ b/Hyena.Data.Sqlite/Hyena.Data.Sqlite/Tests/SqliteTests.cs
@@ -60,24 +60,38 @@ namespace Hyena.Data.Sqlite
         }
 
         [Test]
+        public void TestBindWhileReading ()
+        {
+            Assert.Fail ();
+        }
+
+        [Test]
+        public void TestQueryWhileReading ()
+        {
+            // TODO
+            Assert.Fail ();
+        }
+
+        [Test]
         public void Test ()
         {
             using (var stmt = con.CreateStatement ("SELECT 'foobar' as version")) {
                 Assert.AreEqual ("foobar", stmt.Query<string> ());
-                Assert.AreEqual ("foobar", stmt.First ()[0]);
-                Assert.AreEqual ("foobar", stmt.First ()["version"]);
-            }
-
-            using (var stmt = con.CreateStatement ("SELECT 2 + 5 as res")) {
-                Assert.AreEqual (7, stmt.Query<int> ());
-                Assert.AreEqual (7, stmt.First ()[0]);
-                Assert.AreEqual (7, stmt.First ()["res"]);
 
                 try {
                     stmt.Bind ();
                     Assert.Fail ("should not be able to bind parameterless statement");
                 } catch {}
             }
+
+            using (var stmt = con.CreateStatement ("SELECT 2 + 5 as res")) {
+                using (var reader = stmt.Query ()) {
+                    Assert.IsTrue (reader.Read ());
+                    Assert.AreEqual (7, reader.Get<int> (0));
+                    Assert.AreEqual (7, reader[0]);
+                    Assert.AreEqual (7, reader["res"]);
+                }
+            }
         }
 
         [Test]
@@ -100,19 +114,26 @@ namespace Hyena.Data.Sqlite
                 } catch {}
 
                 stmt.Bind (21);
-                Assert.AreEqual (21, stmt.First ()[0]);
-                Assert.AreEqual (21, stmt.First ()["version"]);
+                Assert.AreEqual (21, stmt.Query<int> ());
 
                 stmt.Bind ("ffoooo");
-                Assert.AreEqual ("ffoooo", stmt.First ()[0]);
-                Assert.AreEqual ("ffoooo", stmt.First ()["version"]);
+                using (var reader = stmt.First ()) {
+                    Assert.AreEqual ("ffoooo", reader[0]);
+                    Assert.AreEqual ("ffoooo", reader["version"]);
+                }
             }
 
             using (var stmt = con.CreateStatement ("SELECT ? as a, ? as b, ?")) {
                 stmt.Bind (1, "two", 3.3);
-                Assert.AreEqual (1, stmt.Query<int> ());
-                Assert.AreEqual ("two", stmt.First ()["b"]);
-                Assert.AreEqual (3.3, stmt.First ()[2]);
+
+                using (var reader = stmt.Query ()) {
+                    Assert.IsTrue (reader.Read ());
+                    Assert.AreEqual (1, reader.Get<int> (0));
+                    Assert.AreEqual ("two", reader["b"]);
+                    Assert.AreEqual ("two", reader.Get<string> ("b"));
+                    Assert.AreEqual ("two", reader.Get<string> (1));
+                    Assert.AreEqual (3.3, reader[2]);
+                }
             }
         }
 
@@ -125,18 +146,20 @@ namespace Hyena.Data.Sqlite
                 Assert.AreEqual (2, stmt.Query<int> ());
             }
 
-            using (var stmt = con.CreateStatement ("SELECT ID, Name FROM Users ORDER BY NAME")) {
-                var row1 = stmt.First ();
-                Assert.AreEqual ("Aaron", row1["Name"]);
-                Assert.AreEqual ("Aaron", row1[1]);
-                Assert.AreEqual (2, row1["ID"]);
-                Assert.AreEqual (2, row1[0]);
-
-                var row2 = stmt.Skip (1).First ();
-                Assert.AreEqual ("Gabriel", row2["Name"]);
-                Assert.AreEqual ("Gabriel", row2[1]);
-                Assert.AreEqual (1, row2["ID"]);
-                Assert.AreEqual (1, row2[0]);
+            using (var reader = con.Query ("SELECT ID, Name FROM Users ORDER BY NAME")) {
+                Assert.IsTrue (reader.Read ());
+                Assert.AreEqual ("Aaron", reader["Name"]);
+                Assert.AreEqual ("Aaron", reader[1]);
+                Assert.AreEqual (2, reader["ID"]);
+                Assert.AreEqual (2, reader[0]);
+
+                Assert.IsTrue (reader.Read ());
+                Assert.AreEqual ("Gabriel", reader["Name"]);
+                Assert.AreEqual ("Gabriel", reader[1]);
+                Assert.AreEqual (1, reader["ID"]);
+                Assert.AreEqual (1, reader[0]);
+
+                Assert.IsFalse (reader.Read ());
             }
         }
 
@@ -377,7 +400,7 @@ namespace Hyena.Data.Sqlite
 
         private void AssertRoundTrip<T> (T val, Func<T, T, bool> func)
         {
-            var o = select_literal.Bind (val).First ().Get<T> (0);
+            var o = select_literal.Bind (val).Query<T> ();
             if (func == null) {
                 Assert.AreEqual (val, o);
             } else {
@@ -387,7 +410,7 @@ namespace Hyena.Data.Sqlite
 
         private void AssertGetNull<T> (T val)
         {
-            Assert.AreEqual (val, select_literal.Bind (null).First ().Get<T> (0));
+            Assert.AreEqual (val, select_literal.Bind (null).Query<T> ());
         }
     }
 }



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