dbcook 0.1

svilen_dobrev at users.sourceforge.net svilen_dobrev at users.sourceforge.net
Thu Jul 19 11:12:59 CEST 2007


This is dbcook, a framework to cook databases from recipes, written 
as python declarations. Eventually the result may be edible (;-)

If u're interested, have a look, dbcook/usage/example1.py might be a 
start. The directory-structure is still in flux - u need to get the 
internal dbcook/ accessible somehow (PYTHONPATH or else). Licensed as 

svn co https://dbcook.svn.sf.net/svnroot/dbcook/trunk


Here a short description what it probably is, and can do:

A framework for declarative mapping of object hierarchies/relations 
into a (relational or not) database.

The "language" itself is made with independence in mind, although 
currently all available builder stuff is over SQLAlchemy as backend 
(so it looks like wrapping declarative translator).

It completely hides/automates the table/key/column/mapper/whatever 
creation. The user can only control certain characteristics of the 
way the mapping happens, mostly related to hierarchy and relations 
between objects (subclasses, instances, leafs, uniqueness etc).

it's about 10 months old now. It can handle: 
 - data column types
 - reference columns -> foreign keys
 - class inheritance, and class inclusion (inheritance without 
    database mapping of the base), and virtual classes (those never
    have instances). More in mapcontext._Base
 - polymorphism, giving 3 kinds of queries for each mapped class: ALL, 
    base-only, subclasses only
 - any combination of table-inheritance-types within the tree 
    (concrete/joined/no-single-yet; beware that sql-polymorphism only 
    works with joined-table for now)
 - automatic solving of cyclical references/dependencies (and putting 
    proper alter_table / post_update)
 - associations (many2many) - implicit and explicit
 - more? maybe

Example declarations:

import dbcook.usage.plainwrap as o2r
class Text( o2r.Type): pass

class Address( Base):
	place = Text()

class Person( o2r.Base):
	name = Text()
	address = o2r.Type4SubStruct( Address)
	friend  = o2r.Type4SubStruct( 'Person')

class Employee( Person):
	job = Text()

..... and no mentioning of tables, columns etc whatsoever .....

Once the mapping is built, any of these can be done:
 - use it as plain SA - session.query( class).. and similar
 - query-clause expressions can be automatically created from plain 
    python functions over objects' attributes, e.g.: 
 lambda self: (self.friend.manager.age < 40) & self.name.endswith('a')
 - generate a source of the equivalent plain SA-calls to build it

So far it is just a library. There is no single way to use it. The 
directory usage/ has 2 possible reflectors (things that walk the 
declared classes and gather info from them), one is just plain python 
(plainwrap.py), another is for my static_type/ framework. You can 
make your own for your own taste.

The usage/samanager thing tries to (correctly - context like) keep all 
related things in one place - mappings, dbs, engines, sessions, etc. 
If destroy() is requested, it'll try hard to clear all side-effects 
of its existence.

Certain pieces are (almost) independent and are usable as is without 
all the rest: expression.py, usage/hack*py, sa_generator.py - all 
SQLAlchemy related.

The tests/ directory contains 2 types of tests - sa/ which proves that 
the way SA is used in dbcook is correct/working, and mapper/other 
which check whether the cooker does right thing as result. It still 
uses makefile to run all stuff.

external dependencies: 
 - needs kjbuckets (from gadfly) for graph-arithmetics
 - needs sqlalchemy, 0.3.6+?
 - optional static_type/ framework - makes staticly-declared structs 
    (a-la java/C semantics) in python - do mail if interested.

Some todo's, long- or short- term:
 - nicer/some way of declaring collections 
     (SA: relation(.. use_list=True))
 - some documentation, translate all in both languages.
 - tests for all the stuff, in most of combinations, 
    instead of just trying this/that
 - generate sql-server-side functions, triggers etc
 - autoload / reverse-engineering
 - single-table inheritance
 - other reflectors/"syntaxes" - e.g. elixir-like 
 - concrete-polymorphism: polymunion of (type,id) key

have fun

svilen_dobrev at users point sourceforge point net
Might be easier to reach me at sqlalchemy's newsgroup.


<P><A HREF="http://dbcook.sf.net">dbcook 0.1</A> - A framework for 
declarative mapping of object hierarchies/relations 
into a (relational or not) database. (19-jul-2007)

More information about the Python-announce-list mailing list