diff --git a/src/SQLite.jl b/src/SQLite.jl index 6525c84..37e1520 100644 --- a/src/SQLite.jl +++ b/src/SQLite.jl @@ -20,9 +20,11 @@ const StmtWrapper = Ref{StmtHandle} # Normal constructor from filename function sqliteexception(handle::DBHandle) + isopen(handle) || throw(SQLiteException("DB is closed")) SQLiteException(unsafe_string(C.sqlite3_errmsg(handle))) end function sqliteexception(handle::DBHandle, stmt::StmtHandle) + isopen(handle) || throw(SQLiteException("DB is closed")) errstr = unsafe_string(C.sqlite3_errmsg(handle)) stmt_text_handle = C.sqlite3_expanded_sql(stmt) stmt_text = unsafe_string(stmt_text_handle) @@ -77,7 +79,8 @@ DBInterface.connect(::Type{DB}) = DB() DBInterface.connect(::Type{DB}, f::AbstractString) = DB(f) DBInterface.close!(db::DB) = _close_db!(db) Base.close(db::DB) = _close_db!(db) -Base.isopen(db::DB) = db.handle != C_NULL +Base.isopen(db::DB) = isopen(db.handle) +Base.isopen(handle::DBHandle) = handle != C_NULL function finalize_statements!(db::DB) # close stmts diff --git a/test/runtests.jl b/test/runtests.jl index 9c78383..2db87b9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1019,4 +1019,11 @@ end tbl_b = DBInterface.execute(db_b, "select myfunc(1) as x") |> columntable @test tbl_a.x == [2] @test tbl_b.x == [10] + + # Throw an error when interfacing with a closed database + close(db_b) + @test_throws SQLiteException("DB is closed") DBInterface.execute( + db_b, + "select myfunc(1) as x", + ) end