G'day Ana, if you are writing a proper app you would most likely
create your tables in SQL and import them into postgres/mysql etc.

If you are using sqlite then I've got a little Table() class I've
written to bootstrap some databases/tables for our test-suite, feel
free to use it in your application. Simply change the Table()
instances at the bottom of the file, then import the module and call
setup(), it will do the rest (see attached file) I've left in some of
the test tables as an example.

usage:

Table('databasename','tablename',*fields)

Hope this helps

-tjs

On Dec 4, 2007 6:58 AM, Ana Júlia <[EMAIL PROTECTED]> wrote:
> i learned in the tutorial that i have to create a table at the end of the
> class. How i should  to do to create all tables in the end of the program?
>
> --
> storm mailing list
> [email protected]
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/storm
>
>



-- 
Timothy J Stebbing
# This module sets up an SQLITE database for content and principals 
# databases and registers them with IZStorm, just import and call
# setup() which will return a dict of created stores keyed by name.

# Add your test tables at the end of the file
from zope.app import zapi
from storm.zope.interfaces import IZStorm


class Table(object):

    schemas = {}

    def __init__(self, _db, _name, **kwargs):
        self.db = _db
        self.name = _name
        self.kwargs = kwargs
        if _db not in self.schemas:
            self.schemas[_db] = []
        self.schemas[_db].append(self)

    def dump(self):
        s = "CREATE TABLE %s (" % (self.name,)
        s += ", ".join([("%s %s" % attr) for attr in self.kwargs.items()])
        return s + ")"

    def create(self):
        zstorm = zapi.getUtility(IZStorm)
        store = zstorm.get(self.db, default_uri="sqlite:")
        store.execute(self.dump())

    @classmethod
    def showAll(cls):
        for db, tables in cls.schemas.items():
            print "DB: %s\n" % (db,)
            print 70*"="
            for t in tables:
                print t.dump()
                print "\n"

def setup():
    storm = zapi.getUtility(IZStorm)
    stores = {}
    for db, tables in Table.schemas.items():
        store = storm.create(db, "sqlite:")
        stores[db] = store
        for t in tables:
            store.execute(t.dump())
    return stores

class Attr(object):
    primary = False
    def __init__(self, name): self.name = name

    def __repr__(self): 
        s = self.name
        if self.primary:
            s += " PRIMARY KEY"
        return s

    def __call__(self, primary=False): 
        a = Attr(self.name)
        a.primary = primary
        return a

integer = Attr('INTEGER')
text = Attr('TEXT')
blob = Attr('BLOB')
varchar = Attr('VARCHAR')
boolean = Attr('BOOLEAN')
datetime = Attr('VARCHAR')


def also(*dicts):
    d = {}
    map(d.update, dicts)
    return d


#some common groups of attributes 
contained = {'zope_parent' : blob, 'zope_name' : varchar}
container = dict(xrefKeys=blob, xrefChildren=blob)
dublinCore = {'dublin_core_id' : integer}
locale = {'locale' : varchar}
subset = {'typeLabel' : varchar}
exerciseVocabAttrs = {'object_id' : integer, 'vocab_id' : integer}

# add table create statements below here
# ======================================

Table('content', 'folders', id=integer(primary=True), xrefKeys=blob, 
      xrefChildren=blob, **also(contained))

Table('content', 'vocabs', id=integer(primary=True), vocab=varchar, token=text)

Table('content', 'dublin_core', id=integer(primary=True), title=text, 
      description=text, created=datetime, modified=datetime, effective=datetime,
      expires=datetime, creators=blob, subjects=blob, publisher=text, 
      contributors=blob)

Table('content', 'images', id=integer(primary=True), data=blob, size=integer, 
      width=integer, height=integer, content_type=varchar, 
      **also(contained, dublinCore))

Table('content', 'tags', id=integer(primary=True), tag=varchar)

Table('content', 'exercises', id=integer(primary=True), title=text, 
      description=text, body=text, method=text, tip=text, partner=boolean,
      thumbnail_id=integer, image_id=integer, subtopics=blob, topic=text,
      region_search=blob, region_planner=blob, 
      **also(contained, dublinCore, locale, subset))

Table('content', 'exercise_accessories_vocabs', **also(exerciseVocabAttrs))
Table('content', 'exercise_expertise_vocabs', **also(exerciseVocabAttrs))
Table('content', 'exercise_plan_type_vocabs', **also(exerciseVocabAttrs))
Table('content', 'exercise_mode_vocabs', **also(exerciseVocabAttrs))
Table('content', 'exercise_primary_region_vocabs', **also(exerciseVocabAttrs))
Table('content', 'exercise_secondary_region_vocabs', **also(exerciseVocabAttrs))

Table('content', 'articles', id=integer(primary=True), title=text, 
      description=text, body=text, thumbnail_id=integer, image_id=integer, 
      **also(contained, dublinCore, locale))

Table('content', 'i18nexercise', id=integer(primary=True),
    **also(contained, container, dublinCore))
-- 
storm mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/storm

Reply via email to