Please note: this project is inactive since early 2006



Home
Features

Quick example

Project's status
Contributors

Installation

Documentation
    User's Guide (pdf)

Framework APIs
    Modeling
    Notification

Downloads

SF Project Page
     Mailing list
     News
     Bugs
     Web SVN

Licence

A very quick overview

Design your model
Write sample_pymodel.py:
#! /usr/bin/env python
from Modeling.PyModel import *

# Defaults
AString.defaults['width'] = 40

Entity.defaults['properties'] = [
  APrimaryKey('id', isClassProperty=0, isRequired=1, doc='PK')
]
##
_connDict = {'database': 'SampleDB', 'host': 'localhost',
             'user': 'postgres', 'password': ''}
model = Model('Sample',adaptorName='Postgresql', connDict=_connDict)
model.version='0.1'
model.entities = [
  Entity('Person',
         properties=[ AString('firstName'),
                      AString('lastName',isRequired=1)]  ),
  Entity('Address',
         properties=[  AString('street'),
                       AString('zipCode', width=10),
                       AString('town', isRequired=1) ],  ),
  ]
#---
model.associations=[
  Association('Address','Person',
              relations=['person','addresses'],
              delete=['nullify','deny'] ),
  ]

Verify the correctness of your model
shell> mdl_validate_model.py sample_pymodel.py

(note: use option -h on python scripts for details)

Generate the python code
shell> mdl_generate_python_code.py sample_pymodel.py

Note: python modules and classes can also be generated dynamically on the fly.
If you're interested in such a feature, you want to have a look at this patch for details.

Generate the corresponding database schema
Either get the coresponding sql statements:
shell>  mdl_generate_DB_schema.py -c -C sample_pymodel.py
CREATE TABLE PERSON (
  FIRST_NAME VARCHAR(40) , 
  LAST_NAME VARCHAR(40) NOT NULL, 
  ID INTEGER NOT NULL);
CREATE TABLE ADDRESS (
  TOWN VARCHAR(40) NOT NULL, 
  FK_PERSON INTEGER , 
  STREET VARCHAR(40) , 
  ID INTEGER NOT NULL, 
  ZIP_CODE VARCHAR(10) );
ALTER TABLE PERSON ADD PRIMARY KEY (ID);
ALTER TABLE ADDRESS ADD PRIMARY KEY (ID);
ALTER TABLE ADDRESS ADD CONSTRAINT person FOREIGN KEY (FK_PERSON) REFERENCES PERSON(ID) INITIALLY DEFERRED;
CREATE SEQUENCE PK_SEQ_PERSON START 1;
CREATE SEQUENCE PK_SEQ_ADDRESS START 1;

Or create the database directly:

shell> mdl_generate_DB_schema.py -C --admin-dsn="localhost:template1:postgres:" sample_pymodel.py

Use it!
>>> from Sample.Person import Person
>>> from Sample.Address import Address
>>> from Modeling.EditingContext import EditingContext
>>> ec=EditingContext()

>>> # create objects
... john=Person(firstName='John', lastName='Cleese')
>>> ec.insert(john)
>>> jeanne=Person(firstName='Jeanne', lastName='Cleese')
>>> ec.insert(jeanne)

>>> # create relationships
... a_john=Address(town='London')
>>> ec.insert(a_john)
>>> john.addToAddresses(a_john); a_john.setPerson(john)
>>>
>>> a_jeanne=Address(town='Paris')
>>> ec.insert(a_jeanne)
>>> jeanne.addToAddresses(a_jeanne); a_jeanne.setPerson(jeanne)

>>> # save changes
>>> ec.saveChanges()

>>> # fetch objects
... in_london=ec.fetch('Person', 'addresses.town ilike "*london*"')
>>> [(p.getLastName(), p.getFirstName()) for p in in_london]

Hosted by: SourceForge.net Logo

Sébastien Bigaret 2006-03-04