Hi all,

I mentioned in my first post to the list that it could be interesting
(for people like me at least) to write the code of Chapter 5 of
CherryPy Essentials using Storm. Why? Because it is a well written
simple comparison between 3 ORMs SQLObject, SQLAlchemy and DejaVu.
Plus, it is available for free (it is the sample chapter
<http://www.packtpub.com/files/1848_CherryPy_SampleChapter.pdf>).

I have attached a file named cherry-storm.py which is my take on the subject.

Please bear in mind that it is the first time that I use a ORM and
that the only resource available at this stage for Storm in term of
documentation is the Tutorial. I would appreciate comments

I hope I managed to replicate the spirit of Sylvain's code (I cc: him
to get his feedback if he feels like it). I know there is one thing I
didn't do : cascading.

Does Storm support cascading operations through a cascade=True
parameter of the Reference or through a special method à la DejaVu
on_forget or ...?

Thanks for your time and assistance,

EuGeNe -- http://www.3kwa.com

PS: as far as manipulating data is concerned ... I think Storm's syntax rocks!!!

PPS: is there a Python ORM Shootout (like the Web Framework Shoutout
:P)? I can't find on. If not, do you think it could be interested to
put something like that together?
# Chapter 5 of CheeryPy Essentials, a Storm perspective

# 1 - Mapping the entities

from storm.locals import *

class Song(Storm):
    __storm_table__ = "song"
    id = Int(primary=True)
    title = Chars()
    position = Int()
    album_id = Int()
    album = Reference(album_id, "Album.id")
    # must use stringified version henceforth inherit from Storm

class Album(Storm):
    __storm_table__ = "album"
    id = Int(primary=True)
    title = Chars()
    release_year = Int()
    artist_id = Int()
    artist = Reference(artist_id, "Artist.id")
    songs = ReferenceSet("Album.id", Song.album_id)
    # Album not aware of itself
    
class Artist(Storm):
    __storm_table__ = "artist"
    id = Int(primary=True)
    name = Chars()
    albums = ReferenceSet("Artist.id",Album.artist_id)
    # Artist not aware of itself

# 2 - Setting up the access to the database

database = create_database("sqlite:")
store = Store(database)

# 3 - Manipulating tables

def create_tables():
    store.execute("""CREATE TABLE song
            (id INTEGER PRIMARY KEY,
            title VARCHAR,
            position INTEGER,
            album_id INTEGER)""",
            noresult=True)
    store.execute("""CREATE TABLE album
            (id INTEGER PRIMARY KEY,
            title VARCHAR,
            release_year INTEGER,
            artist_id INTEGER)""",
            noresult=True)
    store.execute("""CREATE TABLE artist
            (id INTEGER PRIMARY KEY,
            name VARCHARS)""",
            noresult=True)

def drop_tables():
    # can only execure one statement at a time
    store.execute("DROP TABLE song")  
    store.execute("DROP TABLE album")
    store.execute("DROP TABLE artist")
    pass

# 4 -  Loading data

create_tables()

jeff_buckley = Artist()
jeff_buckley.name = "Jeff Buckley"

grace = Album()
grace.title = "Grace"
grace.release_year = 1994
grace.artist = jeff_buckley

dream_brother=Song()
dream_brother.title = "Dream Brother"
dream_brother.position = 10
dream_brother.album = grace

mojo_pin = Song()
mojo_pin.title = "Mojo Pin"
mojo_pin.position = 1
mojo_pin.album = grace

lilac_wine = Song()
lilac_wine.title = "Lilac Wine"
lilac_wine.position = 4

# must be added to the store before being added to a ReferenceSet field
store.add(lilac_wine)
grace.songs.add(lilac_wine)

store.flush()

# 5 - Manipulating data 

def display_info(artist):
    for album in artist.albums:
        message = """%s released %s in %d
It contains the following songs:\n""" % (artist.name, 
                                         album.title,
                                         album.release_year)
        for song in album.songs:
            message = message + "    %s\n" % (song.title, )
        print message

buckley = store.find(Artist,name="Jeff Buckley").one()
display_info(buckley)

songs = store.find(Song,
                   Artist.name == "Jeff Buckley",
                   Song.title.like("%la%"))
for song in songs:
    print "%s" % (song.title, )

songs = store.find(Song)
print "found %d songs, let's show only a few of them:" % (songs.count(), )
for song in songs[1:-1]:
    print "    %s" % (song.title, )

album = store.find(Album, id=1).one()
print album.title

# no idea if cascading is possible using Storm this only removes the album
store.remove(album)
store.flush()
-- 
storm mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/storm

Reply via email to