plpython 이 쓴 저장 프로 세 스 함수

4152 단어 Python
오늘 은 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;


좋은 웹페이지 즐겨찾기