[Zope] Pluggable Brains?

Brian Lloyd Brian@digicool.com
Wed, 4 Aug 1999 17:51:21 -0400


> People,
> Can someone point me to some documentation, messages or 
> source code that will give me a handle on what 'pluggable 
> brains' are and what I can do with them?
> 
> Rob Page has just made a post saying that 
> Zope/ZODB3/pluggable brains can go a long way to integrating 
> with relational databases and I want to make sure I've got a 
> clue about all this! :)
> 
> thanks
> tone out,


I was going to refer you the ZSQL Methods User Guide, but I
just looked and it just barely mentions the concept of
pluggable brains at the moment :(

Simply put, pluggable brains means that you can associate
a class that you've written in Python with the result record
objects for a given SQL query. You can create a .py module in
your Extensions directory (brain.py, for example) with a class
definition such as:

class SimpleBrain:
  """A simple example brain."""

  def fullname(self):
    return self.first_name + self.last_name

Now you create an SQLMethod, which for the sake of our example
might look like:

  select first_name, last_name from employees

This query (like all queries) will return a sequence of "record"
objects, where each record object exposes its data as if the fields
were object attributes.

Now, if you go to the "advanced" tab for a query, you can enter the 
module and class name of a class that will be added as a "mixin" to
the normal record object behavior. Just like defining an external
method, you enter the module name (in Extensions) and the class name
to be used.

*Now* when you run the query, the record objects will also have your
custom class mixed in - now they can have not only their data exposed
as if they were Python attributes, but custom behavior too. In our 
example above, if you made brain.SimpleBrain the "pluggable brain" for
our SQL method, you can now do:

<!--#in myquery-->
  <!--#var fullname-->
<!--#/in-->

...in DTML, because fullname is provided by the class that is mixed
into the resulting record objects. The only real rule to keep 
in mind is that your "brain" must be a mixin class (e.g. you can't 
have an __init__ that expects any arguments).

Hope this helps!


Brian Lloyd        brian@digicool.com
Software Engineer  540.371.6909              
Digital Creations  http://www.digicool.com