database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 2988 게시물 읽기
No. 2988
Tree-structure functions (1)
작성자
정재익
작성일
2001-06-21 23:25
조회수
2,080

원본출처 : http://www.brasileiro.net/postgres/cookbook/view-one-recipe.adp?recipe_id=15

 

 

Submitted on: 03-16-2001

Description:

This is a solution I created when I needed a hierarchicalcategory tree with ability to easily get the number of items in each category, get the full name from the tree root etc.

Code:

 

The pa_categories table is as follows:

Create table pa_categories (

category_id serial NOT NULL,

name varchar(100) NOT NULL,

code varchar(50) NOT NULL,

parent_id Integer NOT NULL,

Primary Key (category_id)

);

 

The functions return

- full category name (with its parents), that is also hyperlinked,

- a trigger to calculate the code - it's similar to the 160 encoded tree structure, that was mentioned at the OpenACS bboards, but much more primitive - i do not expect to have this many rows with categories. The code consists of category_ids from the root (separated by #, though there might be a better sign).

The tree leaves of a certain root may be obtained by a string comparison (ie. for code '0#1#124#80' SQL would be

WHERE code LIKE '0#1#124#80').

This code is not considered finished, and I would appreciate all comments.

 

Known problems: when using different locale than the default 'C' with PG, optimalization of LIKE and REGEXP searches is turned off, forcing a Seq scan)

 

 

CREATE FUNCTION full_category_name(INT) RETURNS VARCHAR AS'

DECLARE v_category_id ALIAS FOR $1;

DECLARE tmp_record RECORD;

DECLARE tmp_id VARCHAR;

DECLARE tmp_code VARCHAR;

BEGIN

tmp_code:='''';

SELECT INTO tmp_record * FROM pa_categories WHERE category_id=v_category_id;

IF NOT FOUND THEN

RETURN ''''::varchar;

END IF;

IF tmp_record.category_id=0 THEN

RETURN tmp_record.name;

END IF;

tmp_id:=full_category_name(tmp_record.parent_id);

IF tmp_record.category_id<>0 THEN

tmp_code:=tmp_id::varchar || '' / '' || tmp_record.name::varchar || '''';

END IF;

RETURN tmp_code;

END;

' LANGUAGE 'plpgsql';

 

 

CREATE FUNCTION category_code(INT) RETURNS VARCHAR AS '

DECLARE v_category_id ALIAS FOR $1;

DECLARE tmp_record RECORD;

DECLARE tmp_id VARCHAR;

DECLARE tmp_code VARCHAR;

BEGIN

tmp_code:='''';

SELECT INTO tmp_record * FROM pa_categories WHERE category_id=v_category_id;

IF NOT FOUND THEN

RETURN ''0''::varchar;

END IF;

IF tmp_record.category_id=0 THEN

RETURN tmp_record.category_id;

END IF;

tmp_id:=category_code(tmp_record.parent_id);

IF tmp_record.category_id<>0 THEN

tmp_code:=tmp_id::varchar || ''#'' || tmp_record.category_id::varchar;

END IF;

RETURN tmp_code;

END;

' LANGUAGE 'plpgsql';

 

CREATE FUNCTION t_code_kategorii() RETURNS OPAQUE AS '

BEGIN

NEW.code:=category_code(NEW.parent_id)||''#''|| NEW.category_id;

RETURN NEW;

END;

' LANGUAGE 'plpgsql';

 

CREATE TRIGGER code_kategorii_tr

BEFORE INSERT OR UPDATE ON pa_kategorie

FOR EACH ROW EXECUTE PROCEDURE t_code_kategorii();

 

Code by Grzegorz Mucha - feel free to use it!

[Top]
No.
제목
작성자
작성일
조회
2991날짜 계산함수 -- DATE_ADD(), DATE_SUB()
정재익
2001-06-21
2480
2990Tree-structure functions (3)
정재익
2001-06-21
1680
2989Tree-structure functions (2)
정재익
2001-06-21
1967
2988Tree-structure functions (1)
정재익
2001-06-21
2080
2987Writing Large Objects to Postgresql via ODBC using VB
정재익
2001-06-21
1847
2986Porting Access97 data to PostgreSQL
정재익
2001-06-21
2204
2984대.소문자 구분에 관하여...
초보자
2001-06-21
1815
2985┕>Re: 대.소문자 구분에 관하여...
정재익
2001-06-21 13:39:03
1882
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.019초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다