public long InsertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, ConflictResolutionStrategy conflictResolutionStrategy)
        {
            if (!StringEx.IsNullOrWhiteSpace(nullColumnHack))
            {
                var e = new InvalidOperationException("{0} does not support the 'nullColumnHack'.".Fmt(TAG));
                Log.E(TAG, "Unsupported use of nullColumnHack", e);
                throw e;
            }

            var t = Factory.StartNew(() =>
            {
                var lastInsertedId = -1L;
                var command        = GetInsertCommand(table, initialValues, conflictResolutionStrategy);

                try
                {
                    LastErrorCode = command.step();
                    command.Dispose();
                    if (LastErrorCode == SQLiteResult.ERROR)
                    {
                        throw new CouchbaseLiteException(raw.sqlite3_errmsg(_writeConnection), StatusCode.DbError);
                    }

                    int changes = _writeConnection.changes();
                    if (changes > 0)
                    {
                        lastInsertedId = _writeConnection.last_insert_rowid();
                    }

                    if (lastInsertedId == -1L)
                    {
                        if (conflictResolutionStrategy != ConflictResolutionStrategy.Ignore)
                        {
                            Log.E(TAG, "Error inserting " + initialValues + " using " + command);
                            throw new CouchbaseLiteException("Error inserting " + initialValues + " using " + command, StatusCode.DbError);
                        }
                    }
                    else
                    {
                        Log.V(TAG, "Inserting row {0} into {1} with values {2}", lastInsertedId, table, initialValues);
                    }
                }
                catch (Exception ex)
                {
                    Log.E(TAG, "Error inserting into table " + table, ex);
                    LastErrorCode = raw.sqlite3_errcode(_writeConnection);
                    throw;
                }
                return(lastInsertedId);
            });

            var r = t.ConfigureAwait(false).GetAwaiter().GetResult();

            if (t.Exception != null)
            {
                throw t.Exception;
            }

            return(r);
        }
Beispiel #2
0
 public void test_last_insert_rowid()
 {
     using (sqlite3 db = ugly.open(":memory:"))
     {
         db.exec("CREATE TABLE foo (x text);");
         db.exec("INSERT INTO foo (x) VALUES ('b')");
         Assert.AreEqual(db.last_insert_rowid(), 1);
     }
 }
        public long InsertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, ConflictResolutionStrategy conflictResolutionStrategy)
        {
            if (!String.IsNullOrWhiteSpace(nullColumnHack))
            {
                var e = new InvalidOperationException("{0} does not support the 'nullColumnHack'.".Fmt(Tag));
                Log.E(Tag, "Unsupported use of nullColumnHack", e);
                throw e;
            }

            var lastInsertedId = -1L;
            var command        = GetInsertCommand(table, initialValues, conflictResolutionStrategy);

            try {
                int result;
                lock (dbLock) {
                    result = command.step();
                }
                if (result == SQLiteResult.ERROR)
                {
                    throw new CouchbaseLiteException(raw.sqlite3_errmsg(db), StatusCode.DbError);
                }

                int changes;
                lock (dbLock) {
                    changes = db.changes();
                }
                if (changes > 0)
                {
                    lock (dbLock) {
                        lastInsertedId = db.last_insert_rowid();
                    }
                }

                if (lastInsertedId == -1L)
                {
                    Log.E(Tag, "Error inserting " + initialValues + " using " + command);
                    throw new CouchbaseLiteException("Error inserting " + initialValues + " using " + command, StatusCode.DbError);
                }
                else
                {
                    Log.V(Tag, "Inserting row {0} into {1} with values {2}", lastInsertedId, table, initialValues);
                }
            } catch (Exception ex) {
                Log.E(Tag, "Error inserting into table " + table, ex);
                throw;
            } finally {
                lock (dbLock) {
                    command.Dispose();
                }
            }

            return(lastInsertedId);
        }
Beispiel #4
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);
                }
            }
        }
Beispiel #5
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);
                    }
                }
            }
        }
Beispiel #6
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]);
                        }
                    }
                }
            }
        }
        public void test_result_zeroblob()
        {
            delegate_function_scalar zeroblob_func =
                (ctx, user_data, args) =>
            {
                int size = raw.sqlite3_value_int(args[0]);
                raw.sqlite3_result_zeroblob(ctx, size);
            };

            using (sqlite3 db = ugly.open(":memory:"))
            {
                db.exec("CREATE TABLE foo (x blob);");
                db.create_function("createblob", 1, null, zeroblob_func);
                db.exec("INSERT INTO foo (x) VALUES(createblob(10));");

                var    rowid = db.last_insert_rowid();
                byte[] blob  = db.query_scalar <byte[]>("SELECT x FROM foo WHERE rowid=" + rowid);
                Assert.AreEqual(blob.Length, 10);
                foreach (var b in blob)
                {
                    Assert.AreEqual(b, 0);
                }
            }
        }
        public long InsertWithOnConflict(String table, String nullColumnHack, ContentValues initialValues, ConflictResolutionStrategy conflictResolutionStrategy)
        {
            if (_readOnly)
            {
                throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.Forbidden, TAG,
                                                 "Attempting to write to a readonly database");
            }

            if (!StringEx.IsNullOrWhiteSpace(nullColumnHack))
            {
                throw new InvalidOperationException("Don't use nullColumnHack");
            }

            Log.To.TaskScheduling.V(TAG, "Scheduling InsertWithOnConflict");
            var t = Factory.StartNew(() =>
            {
                Log.To.TaskScheduling.V(TAG, "Running InsertWithOnConflict");
                var lastInsertedId = -1L;
                var command        = GetInsertCommand(table, initialValues, conflictResolutionStrategy);

                try {
                    LastErrorCode = command.step();
                    command.Dispose();
                    if (LastErrorCode == raw.SQLITE_ERROR)
                    {
                        throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.DbError, TAG,
                                                         raw.sqlite3_errmsg(_writeConnection));
                    }

                    int changes = _writeConnection.changes();
                    if (changes > 0)
                    {
                        lastInsertedId = _writeConnection.last_insert_rowid();
                    }

                    if (lastInsertedId == -1L)
                    {
                        if (conflictResolutionStrategy != ConflictResolutionStrategy.Ignore)
                        {
                            throw Misc.CreateExceptionAndLog(Log.To.Database, StatusCode.DbError, TAG,
                                                             "Error inserting {0} using {1}", initialValues, command);
                        }
                    }
                    else
                    {
                        Log.To.Database.V(TAG, "Inserting row {0} into {1} with values {2}", lastInsertedId, table, initialValues);
                    }
                }
                catch (CouchbaseLiteException) {
                    LastErrorCode = raw.sqlite3_errcode(_writeConnection);
                    Log.To.Database.E(TAG, "Error inserting into table {0}, rethrowing...", table);
                    throw;
                } catch (Exception ex) {
                    LastErrorCode = raw.sqlite3_errcode(_writeConnection);
                    throw Misc.CreateExceptionAndLog(Log.To.Database, ex, StatusCode.DbError, TAG,
                                                     "Error inserting into table {0}", table);
                }

                return(lastInsertedId);
            });

            var r = t.ConfigureAwait(false).GetAwaiter().GetResult();

            if (t.Exception != null)
            {
                throw t.Exception;
            }

            return(r);
        }