AdvancedQuery: discovering a new wrinkle
AdvancedQuery by Dieter Mauer is one of my favorite zope extensions. It gives the ZCatalog, zope's indexing system for the zodb the ability to do SQL-like and even more advanced queries against a set of indexes.
While doing some work on wicked, I discovered another fantastic piece of syntactic sugar. The query objects that you use to build the queries support inplace operations. This allows for the elegant creation of aggregate queries. consider this contrived example for a keyword index::
Representing queries as objects is a bit more cumbersome for getting started than existing query languages, but being able to programatically build queries with the overhead of awkward string parsing is a big win. The inplace "or" is icing on the cake.
I could see AdvancedQuery forming the basis for a sqlish query language for the catalog and ZODB, something many rdb lovers complain is missing from the world of zope. Since all statements would be backed by objects, programmatically creating queries would be as easy as just demonstrated.
note: |= and &= are also available for sets in python
While doing some work on wicked, I discovered another fantastic piece of syntactic sugar. The query objects that you use to build the queries support inplace operations. This allows for the elegant creation of aggregate queries. consider this contrived example for a keyword index::
>>> from Products.AdvancedQuery import Eq
>>> tags = ['dogs', 'love', 'trucks']
>>> def findADate(gender, interests, iam_easy=True):
... q = lambda tag: Eq('keywords', tag) & Eq('gender', 'gender')
...
... # gotta start with something
... query=q(interest[0])
...
... for interest in interest[1:]:
... if iam_easy: query |= q(interest)
... else: query &= q(interest)
...
... return query
once we've built a query, we can use it
with the special method AdvQ adds to all catalogs
>>> from Products.CMFCore.utils import getToolByName
>>> catalog = getToolByName(app.portal, 'portal_catalog')
>>> q = findADate('female', tags)
>>> listofdates = catalog.evalAdvancedQuery(q)
Representing queries as objects is a bit more cumbersome for getting started than existing query languages, but being able to programatically build queries with the overhead of awkward string parsing is a big win. The inplace "or" is icing on the cake.
I could see AdvancedQuery forming the basis for a sqlish query language for the catalog and ZODB, something many rdb lovers complain is missing from the world of zope. Since all statements would be backed by objects, programmatically creating queries would be as easy as just demonstrated.
note: |= and &= are also available for sets in python
2 Comments:
hey whit!
a small not on your nice partner-finding example:
if iam_easy is set to True the gender parameter should be ignored ;-)
加气混凝土设备
加气混凝土设备
加气混凝土设备
疤痕
糖尿病
搜索引擎优化
游戏币
买金币
魔兽金币
魔兽金币
游戏币
买金币
食道癌
食管癌
食道癌的治疗
食管癌的治疗
贲门癌
贲门癌的治疗
食道癌的治疗
癌症
肿瘤
食道癌
肝癌
胃癌
肺癌
脑瘤
乳腺癌
直肠癌
淋巴癌
鼻咽癌
乳腺癌
胃癌
肝癌
土工格栅
碳纤维
输送设备
输送设备
非淋菌性尿道炎
疱疹
鄂式破碎机
免烧砖机
烘干机
碎石机
球磨机
耐火材料
煤气发生炉
雷蒙磨
雷蒙磨
球磨机
选矿设备
牛皮癣
银屑病
牛皮癣
银屑病
球磨机
碎石机
免烧砖机
食道癌
膀胱癌
肺癌
桥式起重机
桥式起重机
起重机
起重机
天车
电动葫芦
室内设计
癫痫
鲜花
旅游策划
旅游规划
旅游规划
旅游开发
旅游规划
旅游规划
造粒
反应釜
反应釜
胶带机
涂布机
胶带机
造粒机
肝硬化腹水
肝腹水
肝硬化
瘦脸
抽脂
除皱
变性
丰胸
整形美容
整形美容
网络电话
液晶电视
等离子电视
食道癌
食道癌
食管癌
贲门癌
糖尿病
癫痫病
癫痫
糖尿病
Post a Comment
<< Home