[plt-scheme] Resources in the transformer environment
At Thu, 26 Oct 2006 18:41:16 -0400, "Jon Zeppieri" wrote:
> You're right; I could easily do this
> with a separate program. I'd prefer not to, since I'd rather not have
> to run this program after every schema change,
I agree. That makes sense when you consider the database to be part of
the code. It's unusual that the code is only available via an SQL
connection, but it's a consistent view. (Just as compiling normally
needs the code for libraries, your program needs the SQL database to be
available at compile time.)
> (require-for-syntax (lib "mysql.ss" "mysql"))
> (begin-for-syntax (current-connection (connect "host.domain.foo" 3306
> "my_username" "my_password" "my_schema")))
>
> (require (lib "mysql.ss" "mysql"))
> (require (lib "dbo.ss" "dbo"))
>
> (dynamic-wind
> (lambda ()
> (current-connection ()connect "host.domain.foo" 3306 "my_username"
> "my_password" "my_schema"))
> (lambda ()
> (define project%
> (dbo-class* "project" object% ()
> (super-new)))
>
> (define o (make-object project%))
> (define ps (send o find #:all))
> (display ps))
> (lambda ()
> (close (current-connection))))
>
> (begin-for-syntax (close (current-connection)))
>
> ===========================
>
> The above code works fine, but I'd like to use the dbo classes in web
> servlets, where I would be a lot more concerned about the lifetime of
> database connections. That final (begin-for-syntax) in the above
> example doesn't seem to belong anywhere in a servlet module.
FWIW, that wouldn't work in a module. The expansion of the arguments to
`dynamic-wind' would be delayed until all the top-level forms have been
processed, which means that the connection would be closed by the time
you try to expand `dbo-class*'.
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.
Matthew