[plt-scheme] Resources in the transformer environment
Hi,
So, I've attempted to implement the macro suggested below:
On Fri, 27 Oct 2006 08:13:22 0800, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> Would it make sense to have a form like
>
> (with-database
> "host.domain.foo" 3306 "my_username" "my_password" "my_schema"
> <body>)
>
> that opened the database both a compile time and at run time? The macro
> might be implemented as mostly
>
> (dynamic-wind
> (lambda () <...open the database...>)
> (lambda () ... (local-expand <body> ...) ...)
> (lambda () <...close the database...>))
>
> but the expansion of <body> would also be wrapped with a similar
> `dynamic-wind' to perform the same DB open and close.
Here is the code:
(define-syntax (with-database stx)
(syntax-case stx ()
((_ (host port username password schema) body-expr ...)
(dynamic-wind (lambda ()
(display "opening transformer DB connection\n")
(current-connection (connect
(syntax-object->datum #'host)
(syntax-object->datum #'port)
(syntax-object->datum #'username)
(syntax-object->datum #'password)
(syntax-object->datum #'schema))))
(lambda ()
(local-expand #'(dynamic-wind (lambda ()
(display
"opening runtime DB connection\n")
(current-connection (connect host port username password schema)))
(lambda ()
body-expr ...)
(lambda ()
(display
"closing runtime DB connection\n")
(close
(current-connection))))
(syntax-local-context)
'()))
(lambda ()
(display "closing transformer DB connection\n")
(close (current-connection)))))))
When I attempt to use it, however, there is a problem:
(module foo mzscheme
(require (lib "dbo.ss" "dbo"))
(with-database (<host> <port> <username> <password> <schema>)
(define project%
(dbo-class* "project" object% ()
(super-new)))
(define o (make-object project%))
(define ps (send o find #:all))
(display ps))
)
...results in...
opening transformer DB connection
closing transformer DB connection
compile: identifier used out of context in: local-accessor
Perhaps I'm expanding the body in the wrong context?
-Jon