示例#1
0
        public void test_row()
        {
            using (sqlite3 db = ugly.open(":memory:"))
            {
                db.exec("CREATE TABLE foo (x int, v int, t text, d real, b blob, q blob);");
                byte[] blob = db.query_scalar <byte[]>("SELECT randomblob(5);");
                db.exec("INSERT INTO foo (x,v,t,d,b,q) VALUES (?,?,?,?,?,?)", 32, 44, "hello", 3.14, blob, null);
                foreach (row r in db.query <row>("SELECT x,v,t,d,b,q FROM foo;"))
                {
                    Assert.AreEqual(r.x, 32);
                    Assert.AreEqual(r.v, 44);
                    Assert.AreEqual(r.t, "hello");
                    Assert.AreEqual(r.d, 3.14);
                    Assert.AreEqual(r.b.Length, blob.Length);
                    for (int i = 0; i < blob.Length; i++)
                    {
                        Assert.AreEqual(r.b[i], blob[i]);
                    }
                    Assert.AreEqual(r.q, null);
                }
                using (sqlite3_stmt stmt = db.prepare("SELECT x,v,t,d,b,q FROM foo;"))
                {
                    stmt.step();

                    Assert.AreEqual(stmt.db_handle(), db);

                    Assert.AreEqual(stmt.column_int(0), 32);
                    Assert.AreEqual(stmt.column_int64(1), 44);
                    Assert.AreEqual(stmt.column_text(2), "hello");
                    Assert.AreEqual(stmt.column_double(3), 3.14);
                    Assert.AreEqual(stmt.column_bytes(4), blob.Length);
                    byte[] b2 = stmt.column_blob(4);
                    Assert.AreEqual(b2.Length, blob.Length);
                    for (int i = 0; i < blob.Length; i++)
                    {
                        Assert.AreEqual(b2[i], blob[i]);
                    }

                    Assert.AreEqual(stmt.column_type(5), raw.SQLITE_NULL);

                    Assert.AreEqual(stmt.column_name(0), "x");
                    Assert.AreEqual(stmt.column_name(1), "v");
                    Assert.AreEqual(stmt.column_name(2), "t");
                    Assert.AreEqual(stmt.column_name(3), "d");
                    Assert.AreEqual(stmt.column_name(4), "b");
                    Assert.AreEqual(stmt.column_name(5), "q");
                }
            }
        }
示例#2
0
 public static object column(this sqlite3_stmt stmt, int index, Type t)
 {
     if (typeof(String) == t)
     {
         return(stmt.column_text(index));
     }
     else if (
         (typeof(Int32) == t) ||
         (typeof(Boolean) == t) ||
         (typeof(Byte) == t) ||
         (typeof(UInt16) == t) ||
         (typeof(Int16) == t) ||
         (typeof(sbyte) == t)
         )
     {
         return(Convert.ChangeType(stmt.column_int(index), t, null));
     }
     else if (
         (typeof(double) == t) ||
         (typeof(float) == t)
         )
     {
         return(Convert.ChangeType(stmt.column_double(index), t, null));
     }
     else if (typeof(DateTime) == t)
     {
         DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
         return(origin.AddSeconds(stmt.column_int64(index)));
     }
     else if (
         (typeof(Int64) == t) ||
         (typeof(UInt32) == t)
         )
     {
         return(Convert.ChangeType(stmt.column_int64(index), t, null));
     }
     else if (typeof(System.Nullable <long>) == t)
     {
         if (stmt.column_type(index) == raw.SQLITE_NULL)
         {
             return(null);
         }
         else
         {
             long?x = stmt.column_int64(index);
             return(x);
         }
     }
     else if (typeof(System.Nullable <double>) == t)
     {
         if (stmt.column_type(index) == raw.SQLITE_NULL)
         {
             return(null);
         }
         else
         {
             double?x = stmt.column_double(index);
             return(x);
         }
     }
     else if (typeof(System.Nullable <int>) == t)
     {
         if (stmt.column_type(index) == raw.SQLITE_NULL)
         {
             return(null);
         }
         else
         {
             int?x = stmt.column_int(index);
             return(x);
         }
     }
     else if (typeof(decimal) == t)
     {
         return((decimal)Convert.ChangeType(stmt.column_double(index), t, null));
     }
     else if (typeof(byte[]) == t)
     {
         return(stmt.column_blob(index));
     }
     else
     {
         throw new NotSupportedException("Invalid type conversion" + t);
     }
 }
示例#3
0
 public static object column(this sqlite3_stmt stmt, int index, Type t)
 {
     if (typeof(String) == t)
     {
         return(stmt.column_text(index));
     }
     else if (
         (typeof(Int32) == t) ||
         (typeof(Boolean) == t) ||
         (typeof(Byte) == t) ||
         (typeof(UInt16) == t) ||
         (typeof(Int16) == t) ||
         (typeof(sbyte) == t)
         )
     {
         return(Convert.ChangeType(stmt.column_int(index), t, null));
     }
     else if (
         (typeof(double) == t) ||
         (typeof(float) == t)
         )
     {
         return(Convert.ChangeType(stmt.column_double(index), t, null));
     }
     else if (typeof(DateTime) == t)
     {
         DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
         return(origin.AddSeconds(stmt.column_int64(index)));
     }
     else if (
         (typeof(Int64) == t) ||
         (typeof(UInt32) == t)
         )
     {
         return(Convert.ChangeType(stmt.column_int64(index), t, null));
     }
     else if (typeof(System.Nullable <long>) == t)
     {
         if (stmt.column_type(index) == raw.SQLITE_NULL)
         {
             return(null);
         }
         else
         {
             long?x = stmt.column_int64(index);
             return(x);
         }
     }
     else if (typeof(System.Nullable <double>) == t)
     {
         if (stmt.column_type(index) == raw.SQLITE_NULL)
         {
             return(null);
         }
         else
         {
             double?x = stmt.column_double(index);
             return(x);
         }
     }
     else if (typeof(System.Nullable <int>) == t)
     {
         if (stmt.column_type(index) == raw.SQLITE_NULL)
         {
             return(null);
         }
         else
         {
             int?x = stmt.column_int(index);
             return(x);
         }
     }
     else if (typeof(byte[]) == t)
     {
         // TODO hmmm.  how should this function adapt to Span/Memory ?
         // need a way to ask for ReadOnlySpan<byte> ?
         if (stmt.column_type(index) == raw.SQLITE_NULL)
         {
             return(null);
         }
         else
         {
             return(stmt.column_blob(index).ToArray());
         }
     }
     else
     {
         throw new NotSupportedException("Invalid type conversion" + t);
     }
 }