Example #1
0
        internal BlobStream(sqlite3_blob blob, SQLiteDatabaseConnection db, bool canWrite, int length)
        {
            this.blob     = blob;
            this.db       = db;
            this.canWrite = canWrite;
            this.length   = length;

            this.dbDisposing   = (o, e) => this.Dispose(true);
            this.db.Disposing += dbDisposing;
        }
/*
** Open a blob handle.
*/
int sqlite3_blob_open(
  sqlite3* db,            /* The database connection */
  string zDb,        /* The attached database containing the blob */
  string zTable,     /* The table containing the blob */
  string zColumn,    /* The column containing the blob */
  sqlite_int64 iRow,      /* The row containing the glob */
  int flags,              /* True -> read/write access, false -> read-only */
  sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
){
  int nAttempt = 0;
  int iCol;               /* Index of zColumn in row-record */
Example #3
0
        public void test_blob_write_with_byte_array_offset()
        {
            using (sqlite3 db = ugly.open(":memory:"))
            {
                const int len = 100;

                db.exec("CREATE TABLE foo (b blob);");
                db.exec("INSERT INTO foo (b) VALUES (zeroblob(?))", len);
                long rowid = db.last_insert_rowid();

                byte[] blob = db.query_scalar <byte[]>("SELECT b FROM foo;");
                Assert.AreEqual(blob.Length, len);

                for (int i = 0; i < 100; i++)
                {
                    Assert.AreEqual(blob[i], 0);
                }

                using (sqlite3_blob bh = db.blob_open("main", "foo", "b", rowid, 1))
                {
                    int len2 = bh.bytes();
                    Assert.AreEqual(len, len2);

                    byte[] blob2 = new byte[len];
                    for (int i = 0; i < 100; i++)
                    {
                        blob2[i] = 73;
                    }

                    bh.write(blob2, 40, 20, 50);
                }

                byte[] blob3 = db.query_scalar <byte[]>("SELECT b FROM foo;");
                Assert.AreEqual(blob3.Length, len);

                for (int i = 0; i < 50; i++)
                {
                    Assert.AreEqual(blob3[i], 0);
                }

                for (int i = 50; i < 70; i++)
                {
                    Assert.AreEqual(blob3[i], 73);
                }

                for (int i = 70; i < 100; i++)
                {
                    Assert.AreEqual(blob3[i], 0);
                }
            }
        }
Example #4
0
        private Stream ReadBlobIntoPooledStream(sqlite3_blob blob, int length)
        {
            var bytes = SQLitePersistentStorage.GetPooledBytes();

            try
            {
                ThrowIfNotOk(raw.sqlite3_blob_read(blob, bytes, length, offset: 0));

                // Copy those bytes into a pooled stream
                return(SerializableBytes.CreateReadableStream(bytes, length));
            }
            finally
            {
                // Return our small array back to the pool.
                SQLitePersistentStorage.ReturnPooledBytes(bytes);
            }
        }
Example #5
0
        private Stream ReadBlob(sqlite3_blob blob)
        {
            var length = raw.sqlite3_blob_bytes(blob);

            // If it's a small blob, just read it into one of our pooled arrays, and then
            // create a PooledStream over it.
            if (length <= SQLitePersistentStorage.MaxPooledByteArrayLength)
            {
                return(ReadBlobIntoPooledStream(blob, length));
            }
            else
            {
                // Otherwise, it's a large stream.  Just take the hit of allocating.
                var bytes = new byte[length];
                ThrowIfNotOk(raw.sqlite3_blob_read(blob, bytes, length, offset: 0));
                return(new MemoryStream(bytes));
            }
        }
Example #6
0
        public void test_blob_write()
        {
            using (sqlite3 db = ugly.open(":memory:"))
            {
                const int len = 100;

                db.exec("CREATE TABLE foo (b blob);");
                using (sqlite3_stmt stmt = db.prepare("INSERT INTO foo (b) VALUES (?)"))
                {
                    stmt.bind_zeroblob(1, len);
                    stmt.step();
                }

                long rowid = db.last_insert_rowid();

                using (sqlite3_blob bh = db.blob_open("main", "foo", "b", rowid, 1))
                {
                    int len2 = bh.bytes();
                    Assert.AreEqual(len, len2);

                    int passes = 10;

                    Assert.AreEqual(len % passes, 0);

                    int    sublen = len / passes;
                    byte[] buf    = new byte[sublen];
                    for (int i = 0; i < sublen; i++)
                    {
                        buf[i] = (byte)(i % 256);
                    }

                    for (int q = 0; q < passes; q++)
                    {
                        bh.write(buf, q * sublen);
                    }
                }
            }
        }
Example #7
0
        public void test_blob_read()
        {
            using (sqlite3 db = ugly.open(":memory:"))
            {
                const int len = 100;

                db.exec("CREATE TABLE foo (b blob);");
                db.exec("INSERT INTO foo (b) VALUES (randomblob(?))", len);
                long rowid = db.last_insert_rowid();

                byte[] blob = db.query_scalar <byte[]>("SELECT b FROM foo;");
                Assert.AreEqual(blob.Length, len);

                using (sqlite3_blob bh = db.blob_open("main", "foo", "b", rowid, 0))
                {
                    int len2 = bh.bytes();
                    Assert.AreEqual(len, len2);

                    int passes = 10;

                    Assert.AreEqual(len % passes, 0);

                    int    sublen = len / passes;
                    byte[] buf    = new byte[sublen];

                    for (int q = 0; q < passes; q++)
                    {
                        bh.read(buf, q * sublen);

                        for (int i = 0; i < sublen; i++)
                        {
                            Assert.AreEqual(blob[q * sublen + i], buf[i]);
                        }
                    }
                }
            }
        }
Example #8
0
        public static void close(this sqlite3_blob blob)
        {
            int rc = raw.sqlite3_blob_close(blob);

            check_ok(rc);
        }
Example #9
0
 public static int bytes(this sqlite3_blob blob)
 {
     return(raw.sqlite3_blob_bytes(blob));
 }
Example #10
0
        public static void read(this sqlite3_blob blob, byte[] b, int bOffset, int len, int offset)
        {
            int rc = raw.sqlite3_blob_read(blob, b, bOffset, len, offset);

            check_ok(rc);
        }
Example #11
0
        public static void read(this sqlite3_blob blob, byte[] b, int offset)
        {
            int rc = raw.sqlite3_blob_read(blob, b, b.Length, offset);

            check_ok(rc);
        }
Example #12
0
        public static void reopen(this sqlite3_blob blob, long rowid)
        {
            int rc = raw.sqlite3_blob_reopen(blob, rowid);

            check_ok(rc);
        }
Example #13
0
        public static void read(this sqlite3_blob blob, Span <byte> b, int offset)
        {
            int rc = raw.sqlite3_blob_read(blob, b, offset);

            check_ok(rc);
        }
Example #14
0
        public static void write(this sqlite3_blob blob, ReadOnlySpan <byte> b, int offset)
        {
            int rc = raw.sqlite3_blob_write(blob, b, offset);

            check_ok(rc);
        }
Example #15
0
 public SQLiteBlobReadStream(sqlite3 db, sqlite3_blob blob)
 {
     _db    = db;
     Length = raw.sqlite3_blob_bytes(blob);
     _blob  = blob;
 }