今天花了好久使用plpython写了一个存储过程函数,终于解决了问题,庆祝一下,哈哈。下面把代码贴上来,可供参考:
-- Function: bazaar_denim_nav(text)
DROP FUNCTION bazaar_denim_nav(text);
CREATE OR REPLACE FUNCTION bazaar_denim_nav(text)
RETURNS text AS
$BODY$
import cPickle as pickle
# Unpack args
filters = [f.strip() for f in args[0].split(',')]
filters = [f for f in filters if f]
filters.append('designerdenimnav')
# Work out some SQL to extract all products that match a set of filters.
if filters:
# Find complete list of categories we should be using.
inClause = ','.join(['$'+str(i+1) for i in range(len(filters))])
qNodes = plpy.prepare(
"""select lft_node, rgt_node from hierarchy where object in
(select id from category where alphanum in (%s))""" % inClause,
['text']*len(filters)
)
nodes = list(plpy.execute(qNodes, filters))
# Build query
parts = []
for node in nodes:
parts.append(
"""select distinct i.id from item i
join item_category ic on ic.item_id=i.id
join category c on c.id=ic.category_id
where ic.category_id in (select object from hierarchy
where lft_node >= %s and rgt_node <= %s)""" % (node['lft_node'], node['rgt_node'])
)
filterSql = ' intersect '.join(parts)
filterSql = ' '.join(filterSql.split())
# Prepare most of the plans
if filters:
if len(filterSql) > 0:
qItems = plpy.prepare("select i.id from item i inner join stock_level sl on i.id=sl.item_id where sl.in_stock>0 and i.id in (%s) order by i.id"%filterSql)
else:
# Nothing matches the criteria so return now
return pickle.dumps([])
else:
qItems = plpy.prepare("select i.id from item i inner join stock_level sl on i.id=sl.item_id where sl.in_stock>0 order by i.id")
# get item lists
items = list(plpy.execute(qItems))
itemids = [item['id'] for item in items]
strItemIds = ', '.join([str(id) for id in itemids])
# define get sub categories function which filter by the in_stock
def getChildren(alphanum, strItemIds):
if strItemIds:
qChildren = plpy.prepare("""select distinct c.name, c.alphanum from category c inner join item_category ic on c.id=ic.category_id
where c.id in (select object from hierarchy where
parent=(select h.id from hierarchy h inner join category ca on ca.id=h.object where ca.alphanum='%s'))
and ic.item_id in (%s)"""%(alphanum, strItemIds))
children = list(plpy.execute(qChildren))
else:
children = []
return children
# Classification list
classification = {}
classification['brand'] = []
classification['type'] = []
classification['brand'] = getChildren('brand', strItemIds)
top_types = getChildren('designerdenimnav', strItemIds)
for temp in top_types:
t_alphanum = temp['alphanum'][1:]
t_name = temp['name'][1:]
t_children = getChildren(t_alphanum, strItemIds)
classification['type'].append({'name':t_name, 'alphanum':t_alphanum, 'children': t_children})
return pickle.dumps(classification)
$BODY$
LANGUAGE 'plpythonu' VOLATILE
COST 100;
ALTER FUNCTION bazaar_denim_nav(text) OWNER TO postgres;
分享到:
相关推荐
在PostgreSQL数据库系统中,PL/Python是一种非常有用的自定义函数语言,它允许用户使用Python编程语言编写存储过程。在Postgres中安装PL/Python时,可能会遇到需要`plpython2.so`文件的情况。这个动态链接库是PL/...
例如,可以创建一个销售数据表,然后使用PLPython定义一个函数来返回查询结果中的特定数据。这样的操作减少了数据处理的复杂度,并且允许数据科学家在数据库内直接使用Python强大的数据处理能力。 Greenplum机器...
在PostgreSQL中,PL/Python(或称为plpythonu for untrusted,plpython3u for Python 3)是用于执行Python脚本的存储过程语言。这意味着你可以利用Python的强大功能来处理数据库中的数据,比如进行数据清洗、分析或...
PostgreSQL严格遵循ACID原则,支持外键、连接、视图、触发器和存储过程(支持多种编程语言,如C/C++、Java、.Net、Perl、Python、Ruby、Tcl和ODBC)。 在数据类型方面,PostgreSQL提供了丰富的支持,包括INTEGER、...
**PostGIS** 是一个用于 PostgreSQL 的空间数据库扩展,它可以存储、管理和分析地理空间数据。PostGIS 支持多种地理数据类型,包括点、线、多边形等,并提供丰富的空间操作函数。 - **简介** - **项目指导委员会**...
CS 645 最终项目的存储库 Imagenet 图像应添加到“imagenet”目录下的图像文件夹中。 这将被 gitignore 文件自动从 git 中排除。 您需要为 alexnet 和 caffe 参考网络生成 caffemodel。 您可以在 caffe 安装目录中...