[plt-scheme] Resources in the transformer environment

From: Keith Frost (keith.l.frost at gmail.com)
Date: Thu Oct 26 18:32:55 EDT 2006

I'm not at all sure that macro-expansion time is the best time to be
making a connection to a database.  It might be better -- and trivial
in scheme -- to implement a separate program which walks the code,
makes any database connections and sets up any necessary schema, and
then leaves some record of its having done so, to avoid unnecessary
repetition.  When the component is then loaded into the web-server
process, it could check to see if things were setup properly, and do
something appropriate if not.

-- Keith F.

On 10/26/06, Jon Zeppieri <zeppieri at gmail.com> wrote:
> I've been playing around with class.ss, attempting to implement
> something similar to Ruby's ActiveRecord class, which provides an
> automatic mapping between a class and a relational database table.
>
> I have a rudimentary form of this working, where each column from the
> DB table is represented by a field in the class.  This is accomplished
> by a macro, dbo-class*, which expands to a class* form.  Since the DB
> columns have to be known at expansion time (and since a goal of this
> project is to create the class automatically from the DB schema), I
> need to connect to the database during macro expansion.  However,
> using instances of the created classes obviously requires a connection
> to the database during normal runtime.
>
> As far as I can tell, there is no way to do this without creating two
> different connections, since the transformer and runtime environments
> are kept completely separate.  Now, opening two database connections
> isn't such a problem, but I am bothered by the fact that while I can
> sensibly manage the runtime connection (e.g., I can use dynamic-wind
> to create, use, and close the connection reliably), I'm not sure how
> to manage the connection in the transformer environment.
>
> Below is an example of the class's use.  Some information that might
> be useful in making sense of it:
>   - current-connection is a parameter provided my mysql.ss
>   - dbo.ss provides the dbo-class* macro
>   - the first parameter to dbo-class* is the name of the database
> table to be represented by the class
>  - the find method returns instances of the class, each of which
> represents a row of the DB table
>
> ==============================
> (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.
>
> Any ideas?  If it would be at all useful to see the dbo.ss code (or
> anything else), I can send it along.
>
> -Jon
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.