public void DataSource_returns_connection_string_filename_when_closed()
 {
     using (var connection = new SQLiteConnection("Filename=test.db"))
     {
         Assert.Equal("test.db", connection.DataSource);
     }
 }
 public void ConnectionString_gets_and_sets_value()
 {
     using (var connection = new SQLiteConnection { ConnectionString = "Filename=test.db" })
     {
         Assert.Equal("Filename=test.db", connection.ConnectionString);
     }
 }
 public void Ctor_sets_connection_string()
 {
     using (var connection = new SQLiteConnection("Filename=test.db"))
     {
         Assert.Equal("Filename=test.db", connection.ConnectionString);
     }
 }
 public void Database_returns_value()
 {
     using (var connection = new SQLiteConnection())
     {
         Assert.Equal("main", connection.Database);
     }
 }
        public override void Commit()
        {
            CheckCompleted();

            _connection.ExecuteNonQuery("COMMIT");
            _connection.Transaction = null;
            _connection = null;
        }
        public SQLiteCommand([NotNull] string commandText, [NotNull] SQLiteConnection connection)
            : this(commandText)
        {
            Check.NotEmpty(commandText, "commandText");
            Check.NotNull(connection, "connection");

            _connection = connection;
        }
        public void ConnectionString_setter_validates_argument()
        {
            using (var connection = new SQLiteConnection())
            {
                var ex = Assert.Throws<ArgumentException>(() => connection.ConnectionString = null);

                Assert.Equal(Strings.FormatArgumentIsNullOrWhitespace("value"), ex.Message);
            }
        }
        public void Ctor_throws_when_invalid_isolation_level()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                connection.Open();

                var ex = Assert.Throws<ArgumentException>(() => connection.BeginTransaction(0));

                Assert.Equal(Strings.FormatInvalidIsolationLevel(0), ex.Message);
            }
        }
        public void DataSource_returns_actual_filename_when_open()
        {
            using (var connection = new SQLiteConnection("Filename=test.db"))
            {
                connection.Open();

                var result = connection.DataSource;

                Assert.True(Path.IsPathRooted(result));
                Assert.Equal("test.db", Path.GetFileName(result));
            }
        }
        public void Ctor_unsets_read_uncommitted_when_serializable()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                connection.Open();

                using (connection.BeginTransaction(IsolationLevel.Serializable))
                {
                    Assert.Equal(0L, connection.ExecuteScalar<long>("PRAGMA read_uncommitted"));
                }
            }
        }
        public void ConnectionString_setter_throws_when_open()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                connection.Open();

                var ex = Assert.Throws<InvalidOperationException>(
                    () => connection.ConnectionString = "Filename=test.db");

                Assert.Equal(Strings.ConnectionStringRequiresClosedConnection, ex.Message);
            }
        }
        public void IsolationLevel_is_infered_when_unspecified()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                connection.Open();
                connection.ExecuteNonQuery("PRAGMA read_uncommitted = 1");

                using (var transaction = connection.BeginTransaction())
                {
                    Assert.Equal(IsolationLevel.ReadUncommitted, transaction.IsolationLevel);
                }
            }
        }
        public void IsolationLevel_throws_when_completed()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                connection.Open();

                var transaction = connection.BeginTransaction();
                transaction.Dispose();

                var ex = Assert.Throws<InvalidOperationException>(() => transaction.IsolationLevel);

                Assert.Equal(Strings.TransactionCompleted, ex.Message);
            }
        }
        public void Ctor_validates_arguments()
        {
            var ex = Assert.Throws<ArgumentException>(() => new SQLiteCommand(null));
            Assert.Equal(Strings.FormatArgumentIsNullOrWhitespace("commandText"), ex.Message);

            Assert.Throws<ArgumentNullException>("connection", () => new SQLiteCommand("SELECT 1", null));

            using (var connection = new SQLiteConnection())
            {
                Assert.Throws<ArgumentNullException>(
                    "transaction",
                    () => new SQLiteCommand("SELECT 1", connection, null));
            }
        }
        public void FieldCount_works()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT 1";
                    connection.Open();

                    using (var reader = command.ExecuteReader())
                    {
                        Assert.Equal(1, reader.FieldCount);
                    }
                }
            }
        }
        public void HasRows_works()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT 1";
                    connection.Open();

                    using (var reader = command.ExecuteReader())
                    {
                        Assert.True(reader.HasRows);
                    }
                }
            }
        }
        public void ParameterName_doesnt_unset_bound_when_unchanged()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT @Parameter";
                    var parameter = command.Parameters.AddWithValue("@Parameter", 1);
                    connection.Open();
                    command.ExecuteNonQuery();

                    parameter.ParameterName = "@Parameter";

                    Assert.True(parameter.Bound);
                }
            }
        }
        public void RecordsAffected_works()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                using (var command = connection.CreateCommand())
                {
                    connection.Open();
                    CreateTestTable(connection);

                    command.CommandText = "INSERT INTO TestTable (Int32Column) VALUES (2)";

                    using (var reader = command.ExecuteReader())
                    {
                        Assert.Equal(1, reader.RecordsAffected);
                    }
                }
            }
        }
Ejemplo n.º 19
0
        public DbException GetConnectionError(string overrideConnectionInfo)
        {
            DbException exception = null;

            SQLiteConnection connection;

            if (
                (overrideConnectionInfo != null)
                && (overrideConnectionInfo.Length > 0)
              )
            {
                connection = new SQLiteConnection(overrideConnectionInfo);
            }
            else
            {
                connection = new SQLiteConnection(connectionString);
            }

            try
            {
                connection.Open();


            }
            catch (DbException ex)
            {
                exception = ex;
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }


            return exception;

        }
Ejemplo n.º 20
0
        private static void PrepareCommand(
            SQLiteCommand command,
            SQLiteConnection connection,
            SQLiteTransaction transaction,
            CommandType commandType,
            string commandText,
            DbParameter[] commandParameters)
        {
            if (command == null) throw new ArgumentNullException("command");
            if (string.IsNullOrEmpty(commandText)) throw new ArgumentNullException("commandText");

            command.CommandType = commandType;
            command.CommandText = commandText;
            command.Connection = connection;

            if (transaction != null)
            {
                if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
                command.Transaction = transaction;
            }

            if (commandParameters != null) { AttachParameters(command, commandParameters); }
        }
        internal SQLiteTransaction(SQLiteConnection connection, IsolationLevel isolationLevel)
        {
            Debug.Assert(connection != null, "connection is null.");
            Debug.Assert(connection.State == ConnectionState.Open, "connection.State is not Open.");

            _connection = connection;
            _isolationLevel = isolationLevel;

            if (_isolationLevel == IsolationLevel.ReadUncommitted)
            {
                _connection.ExecuteNonQuery("PRAGMA read_uncommitted = 1");
            }
            else if (_isolationLevel == IsolationLevel.Serializable)
            {
                _connection.ExecuteNonQuery("PRAGMA read_uncommitted = 0");
            }
            else if (_isolationLevel != IsolationLevel.Unspecified)
            {
                throw new ArgumentException(Strings.FormatInvalidIsolationLevel(isolationLevel));
            }

            _connection.ExecuteNonQuery("BEGIN");
        }
 private static void CreateTestTable(SQLiteConnection connection)
 {
     connection.Open();
     connection.ExecuteNonQuery(@"
         CREATE TABLE TestTable (
             TestColumn INTEGER
         )");
 }
        public void Dispose_can_be_called_more_than_once()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                CreateTestTable(connection);

                var transaction = connection.BeginTransaction();
                transaction.Dispose();
                transaction.Dispose();
            }
        }
        public void Dispose_works()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                CreateTestTable(connection);

                using (var transaction = connection.BeginTransaction())
                {
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "INSERT INTO TestTable VALUES (1)";
                        command.ExecuteNonQuery();

                        transaction.Dispose();

                        Assert.Null(connection.Transaction);
                        Assert.Null(transaction.Connection);
                    }
                }

                Assert.Equal(0L, connection.ExecuteScalar<long>("SELECT COUNT(*) FROM TestTable"));
            }
        }
        public void Commit_throws_when_connection_closed()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction())
                {
                    connection.Close();

                    var ex = Assert.Throws<InvalidOperationException>(() => transaction.Commit());

                    Assert.Equal(Strings.TransactionCompleted, ex.Message);
                }
            }
        }
        public void BeginTransaction_works()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction())
                {
                    Assert.NotNull(transaction);
                    Assert.Same(transaction, connection.Transaction);
                    Assert.Equal(connection, transaction.Connection);
                    Assert.Equal(IsolationLevel.Serializable, transaction.IsolationLevel);
                }
            }
        }
        public void ServerVersion_returns_value()
        {
            using (var connection = new SQLiteConnection())
            {
                var version = connection.ServerVersion;

                Assert.NotNull(version);
                Assert.True(version.StartsWith("3."));
            }
        }
        public void BeginTransaction_throws_when_parallel_transaction()
        {
            using (var connection = new SQLiteConnection("Filename=:memory:"))
            {
                connection.Open();

                using (connection.BeginTransaction())
                {
                    var ex = Assert.Throws<InvalidOperationException>(() => connection.BeginTransaction());

                    Assert.Equal(Strings.ParallelTransactionsNotSupported, ex.Message);
                }
            }
        }
        protected override void Dispose(bool disposing)
        {
            if (!disposing
                || _connection == null)
            {
                return;
            }

            if (_connection.State == ConnectionState.Open)
            {
                _connection.ExecuteNonQuery("ROLLBACK");
            }

            _connection.Transaction = null;
            _connection = null;
        }
 public void ChangeDatabase_not_supported()
 {
     using (var connection = new SQLiteConnection())
     {
         Assert.Throws<NotSupportedException>(() => connection.ChangeDatabase("new"));
     }
 }