db: add `sqlite3-available?'

Loading `db/sqlite3' no longer raises an exception if
the SQLite library isn't found. Instead, `sqlite3-connect'
raises an exception, while `sqlite3-available?' reports
whether it will work.

The dynamic test allows the documentation-help system
to continue to work if SQLite3 is not available. Currently,
though, `raco setup' still insists on using SQLite3 to
build the database of documented tags.
This commit is contained in:
Matthew Flatt 2012-11-20 17:36:37 -07:00
parent 8c1b5db815
commit 41e9e3e5ff
6 changed files with 31 additions and 11 deletions

View File

@ -14,7 +14,8 @@
mysql-guess-socket-path mysql-guess-socket-path
mysql-password-hash)] mysql-password-hash)]
["private/sqlite3/main.rkt" ["private/sqlite3/main.rkt"
(sqlite3-connect)] (sqlite3-connect
sqlite3-available?)]
["private/odbc/main.rkt" ["private/odbc/main.rkt"
(odbc-connect (odbc-connect
odbc-driver-connect odbc-driver-connect
@ -71,6 +72,8 @@
#:use-place boolean? #:use-place boolean?
#:debug? any/c) #:debug? any/c)
connection?)] connection?)]
[sqlite3-available?
(-> boolean?)]
;; Duplicates contracts at odbc.rkt ;; Duplicates contracts at odbc.rkt
[odbc-connect [odbc-connect

View File

@ -5,10 +5,14 @@
(provide (all-from-out "ffi-constants.rkt") (provide (all-from-out "ffi-constants.rkt")
(protect-out (all-defined-out))) (protect-out (all-defined-out)))
(define-ffi-definer define-sqlite (define sqlite-lib
(case (system-type) (case (system-type)
((windows) (ffi-lib "sqlite3.dll")) [(windows) (ffi-lib "sqlite3.dll" #:fail (lambda () #f))]
(else (ffi-lib "libsqlite3" '("0" #f))))) [else (ffi-lib "libsqlite3" '("0" #f) #:fail (lambda () #f))]))
(define-ffi-definer define-sqlite
sqlite-lib
#:default-make-fail make-not-available)
; Types ; Types
(define-cpointer-type _sqlite3_database) (define-cpointer-type _sqlite3_database)

View File

@ -5,7 +5,8 @@
"connection.rkt" "connection.rkt"
"dbsystem.rkt" "dbsystem.rkt"
"ffi.rkt") "ffi.rkt")
(provide sqlite3-connect) (provide sqlite3-connect
sqlite3-available?)
(define (sqlite3-connect #:database path (define (sqlite3-connect #:database path
#:mode [mode 'read/write] #:mode [mode 'read/write]
@ -53,3 +54,6 @@
(class place-proxy-connection% (class place-proxy-connection%
(super-new) (super-new)
(define/override (get-dbsystem) dbsystem))) (define/override (get-dbsystem) dbsystem)))
(define (sqlite3-available?)
(and sqlite-lib #t))

View File

@ -247,6 +247,13 @@ Base connections are made using the following functions.
(new connection%)]] (new connection%)]]
} }
@defproc[(sqlite3-available?) boolean?]{
Reports whether the SQLite native library is found, in which case
@racket[sqlite3-connect] works, otherwise it raises an exception.}
@defproc[(odbc-connect [#:dsn dsn string?] @defproc[(odbc-connect [#:dsn dsn string?]
[#:user user (or/c string? #f) #f] [#:user user (or/c string? #f) #f]
[#:password password (or/c string? #f) #f] [#:password password (or/c string? #f) #f]
@ -742,10 +749,9 @@ Provides only @racket[mysql-connect] and
@defmodule*/no-declare[(db/sqlite3)] @defmodule*/no-declare[(db/sqlite3)]
Provides only @racket[sqlite3-connect]. In contrast to Provides @racket[sqlite3-connect] plus @racket[sqlite3-available?]. When
@racketmodname[db], this module immediately attempts to the SQLite native library cannot be found, @racket[sqlite3-connect]
load the SQLite native library when required, and it raises an raises an exception.
exception if it cannot be found.
@defmodule*/no-declare[(db/odbc)] @defmodule*/no-declare[(db/odbc)]

View File

@ -12,4 +12,6 @@
#:busy-retry-delay (and/c rational? (not/c negative?)) #:busy-retry-delay (and/c rational? (not/c negative?))
#:use-place any/c #:use-place any/c
#:debug? any/c) #:debug? any/c)
connection?)]) connection?)]
[sqlite3-available?
(-> boolean?)])

View File

@ -8,7 +8,8 @@
doc-db-key->path doc-db-key->path
doc-db-file->connection) doc-db-file->connection)
(define (doc-db-available?) #t) (define (doc-db-available?)
(sqlite3-available?))
(define (doc-db-file->connection db-file) (define (doc-db-file->connection db-file)
(sqlite3-connect #:database db-file)) (sqlite3-connect #:database db-file))