plpython 이 쓴 저장 프로 세 스 함수
                                            
 4152 단어  Python
                    
-- 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;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Python의 None과 NULL의 차이점 상세 정보그래서 대상 = 속성 + 방법 (사실 방법도 하나의 속성, 데이터 속성과 구별되는 호출 가능한 속성 같은 속성과 방법을 가진 대상을 클래스, 즉 Classl로 분류할 수 있다.클래스는 하나의 청사진과 같아서 하나의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.