Hallo,
Aaron Held hat gesagt: // Aaron Held wrote:

> Will you be releasing the newer version of the store?

I'm not sure yet, as it is in parts quite specific. 

> I'd like to see how you did the querypath translation.

I will however release things like my Cart.py, that are of a more
general use, to the ww-sandbox as GPL. 

Interestingly the QueryBuilder class I used already was released as a
post to the sqlobject list. It's a bit evolved since then, but the
basic functionality is complete in this old mail by me:

--- old mail: 

So, and to give something back, here's my QueryBuilder class that's
intended to be used with Webware's extraPathInfo and automates
searching via the extra-URL:

### snip ###

from SQLObject import *

class QueryBuilder(object):

        def __init__(self, table, url=""):
                self.__url = url
                self.__sql = None
                self.__conds = {}
                self._table = table
                self._cols = self._table._SO_columnDict.keys()
                id url:
                        self.parse()

        def set_url(self, url):
                self.__url = url
                self.parse()
        
        def get_sql(self):
                query = None
                for colName, ops in self.__conds.items():
                        for op in ops:                     
                                subquery =  LIKE(getattr(self._table.q, colName), 
"%%%s%%" % op)
                                if colName == "id":
                                        subquery = self._table.q.id == op
                                if query:
                                        query = AND(query, subquery)
                                else:
                                        query = subquery
                return query

        def parse(self):
                cols = self._cols
                cols.append("id")
                u = self.__url.split("/")
                l = len(u)
                i = 0
                conds = {}
                last = None
                while i < l:
                        if last and u[i]:
                                conds[last] = conds.get(last, [])
                                conds[last].append(u[i])
                                last = None
                        elif u[i] in cols:
                                last = u[i] 
                        i += 1  
                self.__conds = conds

        url =  property(None, set_url, None, "URL property, write only")
        sql =  property(get_sql, None, None, "SQL property, read only")

### snip ###

Not that elegant, but working fine for "flat" databases.  It's used
like this: 

# Product is an SQLObject with attrs: name, title, description,...
q = QueryBuilder(Product)
q.url = "name/potter/name/harry/description/book/foo/bar/"
print q.sql
print "Number of results: %d " % len (table.select(q.sql))
for n in list(table.select(q.sql)[0:10]):
        print "%s: %s" % (n.name, n.title)


Have fun,
-- 
 Frank Barknecht                               _ ______footils.org__


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to