hey all -

I attended to the issues of "select_by" and "select_by_col_name" tonight, as more people made the same comment that it was needed. As it turns out, I implemented the method with an extra twist which makes it better than anticipated.

Heres a basic select_by example. Lets do the usual "Users" and "Addresses" example:

        users = Table('users', engine,
                Column('user_id', Integer, primary_key=True),
                Column('username', String(20))
        )
        addresses = Table('addresses', engine,
                Column('address_id', Integer, primary_key=True),
                Column('user_id', Integer, ForeignKey('users.user_id')),
                Column('email', String(60))
        )

        class User(object):
                pass
        class Address(object):
                pass

User.mapper = mapper(User, users, properties = {'addresses', relation (Address, addresses)})

Select By means you can just specify column names or property names as keyword parameters:

        l = User.mapper.select_by(username='fred')

Which is equivalent to "l = User.mapper.select (User.c.username=='fred')".

There is also the scalar-producing "get_by",which just returns the first element in the list or None if the list is empty:

        u = User.mapper.get_by(username='fred')

In the style of Rails ActiveRecord, there is a dynamic method-name version of both, which just takes the remainder of the method name as the parameter name and the function argument as its value:

        l = User.mapper.select_by_username('fred')
        u = User.mapper.get_by_username('fred')

Now heres the twist. The key names you send to select_by and get_by are searched for in the property names assigned to the mapper, as well as the column names within the underlying Table object or objects. But if the key is not found in either, its going to search in the properties and columns of all the immediate relationships of the mapper, and if found generate the appropriate joins:

        l = User.mapper.select_by(email='[EMAIL PROTECTED]')

Which will produce the SQL:

        SELECT users.user_id AS users_user_id, users.username AS users_username
        FROM users, addresses
        WHERE addresses.email = :addresses_email
        AND users.user_id = addresses.user_id ORDER BY users.oid
        {'addresses_email': '[EMAIL PROTECTED]'}

As was already the case, eager loaders are no problem when you select on a join, as aliases are generated for any WHERE criterion that overlaps the eager criterion:

l = User.mapper.options(eagerload('addresses')).select_by (email='[EMAIL PROTECTED]')

SELECT users.user_id AS users_user_id, users.username AS users_username,
        addresses.address_id AS addresses_address_id,
addresses.user_id AS addresses_user_id, addresses.email AS addresses_email
        FROM addresses AS addresses_c9ff,
        users LEFT OUTER JOIN addresses ON users.user_id = addresses.user_id    
        WHERE addresses_c9ff.email = :addresses_email
AND users.user_id = addresses_c9ff.user_id ORDER BY users.oid, addresses.oid
        {'addresses_email': '[EMAIL PROTECTED]'}

The ZBlog demo, which some had complained was still too verbose, has been scaled down to use these new methods, and the mapper module has all of the 'find' methods removed, so its pretty much just the table and mapper definitions.

- mike




-------------------------------------------------------
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_id=7637&alloc_id=16865&op=click
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to