Beispiel #1
0
        public int Delete(String table, String whereClause, params String[] whereArgs)
        {
            if (_readOnly)
            {
                throw new CouchbaseLiteException("Attempting to write to a readonly database", StatusCode.Forbidden);
            }

            Debug.Assert(!StringEx.IsNullOrWhiteSpace(table));

            var t = Factory.StartNew(() =>
            {
                var resultCount = -1;
                var command     = GetDeleteCommand(table, whereClause, whereArgs);
                try
                {
                    var result = command.step();
                    if (result == SQLiteResult.ERROR)
                    {
                        throw new CouchbaseLiteException("Error deleting from table " + table, StatusCode.DbError);
                    }

                    resultCount = _writeConnection.changes();
                    if (resultCount < 0)
                    {
                        throw new CouchbaseLiteException("Failed to delete the records.", StatusCode.DbError);
                    }
                }
                catch (Exception ex)
                {
                    LastErrorCode = raw.sqlite3_errcode(_writeConnection);
                    Log.E(TAG, "Error {0} when deleting from table {1}".Fmt(_writeConnection.extended_errcode(), table), ex);
                    throw;
                }
                finally
                {
                    command.Dispose();
                }
                return(resultCount);
            });

            // NOTE.ZJG: Just a sketch here. Needs better error handling, etc.
            var r = t.GetAwaiter().GetResult();

            if (t.Exception != null)
            {
                //this is bad: should not arbitrarily crash the app
                throw t.Exception;
            }

            return(r);
        }