#dbcook.sf.net

# get a builder
import dbcook.usage.plainwrap as o2r
import sqlalchemy
# define some types
class Text( o2r.Type): pass
class Int(  o2r.Type): pass
class Bool( o2r.Type): #pass
    column_def = sqlalchemy.Boolean(30)

class Base( o2r.Base):
    DBCOOK_inheritance = 'joined'
    DBCOOK_no_mapping = True

class ContentType( Base):
    pass
class Content( Base):
    type = o2r.Reference( ContentType)
class Folder( Content):
    items = o2r.Collection( 'FolderContent')
class FolderContent( Base):
    content = o2r.Collection( Content)
    folder  = o2r.Reference( Folder)
class Country( Base):
    pass
class Event( Content):
    country = o2r.Reference( Country)


#eo model

import sqlalchemy, sys
import sqlalchemy.orm
meta = sqlalchemy.MetaData( sqlalchemy.create_engine('sqlite:///', echo= 'echo' in sys.argv ))

# map attr-types to sa-column-types
fieldtypemap = {
    Text: dict( type= sqlalchemy.String(100), ),
    Int : sqlalchemy.Integer,
    Bool: None, #i.e. use Bool.column_def
}

# build the mapping
mybuild = o2r.Builder( meta,
        locals(),       #just scan anything here that looks like subclass of Base
        fieldtypemap,
        generator =True     #lets see how this would look in plain sqlalchemy
    )

#### that was it. lets use it... ####

#see how this should look in plain SA calls
if mybuild.generator:
    print '========= generated SA set-up'
    print mybuild.generator.out
    print '========= eo generated SA set-up'
