2005/12/18, Michael Bayer <[EMAIL PROTECTED]>:
>
> On Dec 17, 2005, at 12:05 AM, limodou wrote:
> >
> > If there is a count() method, just like:
> >
> >   number = count([user]).execute()
> >
> > or number = select([user]).execute().count()
> >
> > or somethins else
> >
> > May be more helpful. So that you don't need to invoke fetchone()
> > again.
> >
>
> I would vote for a "count" function on the base class for Selectable,
> which is any table, join, alias, select statement, union, etc.
>
> so when you do:
>
>         x = mytable.count()
>
> it compiles to:
>
>         x = select([count(1)], from_obj=[mytable]).scalar()
>
> when you do:
>
>         x = mytable.count(mytable.c.col1==7)
>
> it compiles to:
>
>         x = select([count(1)], mytable.c.col1==7).scalar()
>
> the question is, do we want to put these functions at the ORM level
> as well:
>
>         x = User.mapper.count(User.c.user_name=='fred')
>
>         x = User.mapper.count_by(user_name='fred')
>
>
>
At this point I'm puzzled with  which one should I persist in: Table
metadata , user-defined class or mapper. Some functions can be found
in either Table metadata or mapper. But user-defined class seems only
be used to make ORM. But in other Python ORM modules, class may be the
most important things. Of cause a mapper can be saved to  a
user-defined class, but you should use:

myclass.mapper.XXX

Why not just use myclass.XXX and let the mapper is under the class, so:

mapper(User, user) will do :

User.mapper = mapper(User, user) automaticly, and all mapper's
function can be used as:

User.select(), User.count(),

etc. And I thought the class may be like a mixturer of the
user-defined class and mapper. But what I thought may limit the
mapper's effort. So if there could be a easy way to combine the
user-defined class and mapper for some simple circumstance. I'm sorry
for my English. Can you understand me?

And I think I can do more in the user-defined class:

class BaseTable(object):
    def get(cls, id):
        r = cls.mapper.get(id)
        return r
    get = classmethod(get)

    def save(self):
        objectstore.commit(self)

class Category(BaseTable):
    def __init__(self, title=None):
        self.title = title

    def __repr__(self):
        return "title=%s" % self.title

class Feed(BaseTable):
    def __init__(self, category_id=None, title='', link='', description=''):
        self.category_id = category_id
        self.title = title
        self.link = link
        self.description = description

    def __repr__(self):
        return "title=%s, link=%s, description=%s" % (self.title,
self.link, self.description)

    Feed.mapper = mapper(Feed, RssFeed)
    Category.mapper = mapper(Category, RssCategory, properties = {
                    'feeds' : relation(Feed.mapper, private=True)
                }
            )

I'm not very familiar with metaclass, so I made a base class
BaseTable, it can do some magic things. So my class can do somethings
which the mapper should do. And I can also user obj.save() to replace
in objectstore.commit(obj). And what I want may be just short typing.
Just some thought, you can think about :)

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to