public void GetBytes_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                connection.ExecuteNonQuery("CREATE TABLE Test(Value);");
                connection.ExecuteNonQuery("INSERT INTO Test VALUES(x'427E5743');");
                connection.ExecuteNonQuery("INSERT INTO Test VALUES(x'538F6854');");
                connection.ExecuteNonQuery("INSERT INTO Test VALUES(x'649A7965');");

                using (var reader = connection.ExecuteReader("SELECT Value FROM Test;"))
                {
                    var list = new List <byte[]>();
                    while (reader.Read())
                    {
                        var buffer    = new byte[6];
                        var bytesRead = reader.GetBytes(0, 0, buffer, 0, buffer.Length);
                        Assert.Equal(4, bytesRead);
                        list.Add(buffer);
                    }

                    Assert.Equal(3, list.Count);
                    Assert.Equal(new byte[6] {
                        0x42, 0x7E, 0x57, 0x43, 0, 0
                    }, list[0]);
                    Assert.Equal(new byte[6] {
                        0x53, 0x8F, 0x68, 0x54, 0, 0
                    }, list[1]);
                    Assert.Equal(new byte[6] {
                        0x64, 0x9A, 0x79, 0x65, 0, 0
                    }, list[2]);
                }
            }
        }
        public void GetChars_works_with_overflow()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 'test';"))
                {
                    var hasData = reader.Read();
                    Assert.True(hasData);

                    var  hugeBuffer = new char[1024];
                    long charsRead  = reader.GetChars(0, 1, hugeBuffer, 0, hugeBuffer.Length);
                    Assert.Equal(3, charsRead);

                    var correctBytes = new char[3] {
                        'e', 's', 't'
                    };
                    for (int i = 0; i < charsRead; i++)
                    {
                        Assert.Equal(correctBytes[i], hugeBuffer[i]);
                    }
                }
            }
        }
        public void GetBytes_works_with_overflow()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT x'427E5743';"))
                {
                    var hasData = reader.Read();
                    Assert.True(hasData);

                    var  hugeBuffer = new byte[1024];
                    long bytesRead  = reader.GetBytes(0, 1, hugeBuffer, 0, hugeBuffer.Length);
                    Assert.Equal(3, bytesRead);

                    var correctBytes = new byte[3] {
                        0x7E, 0x57, 0x43
                    };
                    for (int i = 0; i < bytesRead; i++)
                    {
                        Assert.Equal(correctBytes[i], hugeBuffer[i]);
                    }
                }
            }
        }
        public void GetOrdinal_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1 AS Id;"))
                {
                    Assert.Equal(0, reader.GetOrdinal("Id"));
                }
            }
        }
        public void FieldCount_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    Assert.Equal(1, reader.FieldCount);
                }
            }
        }
        public void HasRows_returns_false_when_no_rows()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1 WHERE 0 = 1;"))
                {
                    Assert.False(reader.HasRows);
                }
            }
        }
        public void Depth_returns_zero()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    Assert.Equal(0, reader.Depth);
                }
            }
        }
        public void IsClosed_returns_false_when_active()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    Assert.False(reader.IsClosed);
                }
            }
        }
        public void IsClosed_returns_true_when_closed()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                var reader = connection.ExecuteReader("SELECT 1;");
                reader.Close();

                Assert.True(reader.IsClosed);
            }
        }
        public void RecordsAffected_works_when_no_DDL()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                var reader = connection.ExecuteReader("SELECT 1;");
                ((IDisposable)reader).Dispose();

                Assert.Equal(-1, reader.RecordsAffected);
            }
        }
        public void GetFieldType_works(string sql, Type expected)
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader(sql))
                {
                    Assert.Equal(expected, reader.GetFieldType(0));
                }
            }
        }
        public void GetDataTypeName_works_when_column()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();
                connection.ExecuteNonQuery("CREATE TABLE Person ( Name nvarchar(4000) );");

                using (var reader = connection.ExecuteReader("SELECT Name FROM Person;"))
                {
                    Assert.Equal("nvarchar", reader.GetDataTypeName(0));
                }
            }
        }
        private static void X_throws_when_closed(Action <DbDataReader> action, string operation)
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                var reader = connection.ExecuteReader("SELECT 1;");
                ((IDisposable)reader).Dispose();

                var ex = Assert.Throws <InvalidOperationException>(() => action(reader));
                Assert.Equal(Resources.DataReaderClosed(operation), ex.Message);
            }
        }
        public void RecordsAffected_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();
                connection.ExecuteNonQuery("CREATE TABLE Test(Value);");

                var reader = connection.ExecuteReader("INSERT INTO Test VALUES(1);");
                ((IDisposable)reader).Dispose();

                Assert.Equal(1, reader.RecordsAffected);
            }
        }
        private static void X_throws_before_read(Action <DbDataReader> action)
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT NULL;"))
                {
                    var ex = Assert.Throws <InvalidOperationException>(() => action(reader));

                    Assert.Equal(Resources.NoData, ex.Message);
                }
            }
        }
Exemplo n.º 16
0
        public Task ExecuteReader_retries_when_locked(bool extendedErrorCode)
        {
            const string connectionString = "Data Source=locked;Mode=Memory;Cache=Shared";

            var selectedSignal = new AutoResetEvent(initialState: false);

            return(Task.WhenAll(
                       Task.Run(
                           async() =>
            {
                using (var connection = new SqliteConnection(connectionString))
                {
                    connection.Open();
                    if (extendedErrorCode)
                    {
                        raw.sqlite3_extended_result_codes(connection.Handle, 1);
                    }

                    connection.ExecuteNonQuery(
                        "CREATE TABLE Data (Value); INSERT INTO Data VALUES (0);");

                    using (connection.ExecuteReader("SELECT * FROM Data;"))
                    {
                        selectedSignal.Set();

                        await Task.Delay(1000);
                    }
                }
            }),
                       Task.Run(
                           () =>
            {
                using (var connection = new SqliteConnection(connectionString))
                {
                    connection.Open();
                    if (extendedErrorCode)
                    {
                        raw.sqlite3_extended_result_codes(connection.Handle, 1);
                    }

                    selectedSignal.WaitOne();

                    var command = connection.CreateCommand();
                    command.CommandText = "DROP TABLE Data;";

                    command.ExecuteNonQuery();
                }
            })));
        }
Exemplo n.º 17
0
        public async Task ExecuteReader_retries_when_busy()
        {
            const string connectionString = "Data Source=busy.db";

            var selectedSignal = new AutoResetEvent(initialState: false);

            try
            {
                await Task.WhenAll(
                    Task.Run(
                        async() =>
                {
                    using (var connection = new SqliteConnection(connectionString))
                    {
                        connection.Open();

                        connection.ExecuteNonQuery(
                            "CREATE TABLE Data (Value); INSERT INTO Data VALUES (0);");

                        using (connection.ExecuteReader("SELECT * FROM Data;"))
                        {
                            selectedSignal.Set();

                            await Task.Delay(1000);
                        }
                    }
                }),
                    Task.Run(
                        () =>
                {
                    using (var connection = new SqliteConnection(connectionString))
                    {
                        connection.Open();

                        selectedSignal.WaitOne();

                        var command         = connection.CreateCommand();
                        command.CommandText = "DROP TABLE Data;";

                        command.ExecuteNonQuery();
                    }
                }));
            }
            finally
            {
                SqliteConnection.ClearPool(new SqliteConnection(connectionString));
                File.Delete("busy.db");
            }
        }
Exemplo n.º 18
0
        public void GetFieldType_throws_when_ordinal_out_of_range()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    var ex = Assert.Throws <ArgumentOutOfRangeException>(() => reader.GetFieldType(1));

                    Assert.AreEqual("ordinal", ex.ParamName);
                    Assert.AreEqual(1, ex.ActualValue);
                }
            }
        }
        public void IsDBNull_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT NULL;"))
                {
                    var hasData = reader.Read();

                    Assert.True(hasData);
                    Assert.True(reader.IsDBNull(0));
                }
            }
        }
        public void GetEnumerator_not_implemented()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    var hasData = reader.Read();
                    Assert.True(hasData);

                    Assert.Throws <NotImplementedException>(() => reader.GetEnumerator());
                }
            }
        }
        public void Item_by_name_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1 AS Id;"))
                {
                    var hasData = reader.Read();
                    Assert.True(hasData);

                    Assert.Equal(1L, reader["Id"]);
                }
            }
        }
        private static void GetX_throws_when_null(Action <DbDataReader> action)
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT NULL;"))
                {
                    var hasData = reader.Read();

                    Assert.True(hasData);
                    Assert.Throws <InvalidCastException>(() => action(reader));
                }
            }
        }
        private static void GetX_works <T>(string sql, Func <DbDataReader, T> action, T expected)
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader(sql))
                {
                    var hasData = reader.Read();

                    Assert.True(hasData);
                    Assert.Equal(expected, action(reader));
                }
            }
        }
        public void GetEnumerator_works()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    var hasData = reader.Read();
                    Assert.True(hasData);

                    Assert.NotNull(reader.GetEnumerator());
                }
            }
        }
        public void GetValue_works(string sql, object expected)
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader(sql))
                {
                    var hasData = reader.Read();

                    Assert.True(hasData);
                    Assert.Equal(expected, reader.GetValue(0));
                }
            }
        }
        public void GetOrdinal_throws_when_out_of_range()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    var ex = Assert.Throws <ArgumentOutOfRangeException>(() => reader.GetOrdinal("Name"));
                    Assert.NotNull(ex.Message);
                    Assert.Equal("name", ex.ParamName);
                    Assert.Equal("Name", ex.ActualValue);
                }
            }
        }
        public void GetFieldValue_of_DBNull_throws_when_not_null()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    var hasData = reader.Read();

                    Assert.True(hasData);
                    Assert.Throws <InvalidCastException>(() => reader.GetFieldValue <DBNull>(0));
                }
            }
        }
        public void GetChars_not_supported()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 'test';"))
                {
                    var hasData = reader.Read();
                    Assert.True(hasData);

                    var buffer = new char[4];
                    Assert.Throws <NotSupportedException>(() => reader.GetChars(0, 0, buffer, 0, buffer.Length));
                }
            }
        }
        public void NextResult_can_be_called_more_than_once()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1;"))
                {
                    var hasResults = reader.NextResult();
                    Assert.False(hasResults);

                    hasResults = reader.NextResult();
                    Assert.False(hasResults);
                }
            }
        }
        public void HasRows_works_when_batching()
        {
            using (var connection = new SqliteConnection("Data Source=:memory:"))
            {
                connection.Open();

                using (var reader = connection.ExecuteReader("SELECT 1 WHERE 0 = 1; SELECT 1;"))
                {
                    Assert.False(reader.HasRows);

                    reader.NextResult();

                    Assert.True(reader.HasRows);
                }
            }
        }