PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 12th, 2003, 11:25 PM
cliffyman cliffyman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Washington DC, USA
Posts: 156 cliffyman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 7 sec
Reputation Power: 13
Send a message via AIM to cliffyman
first go at plpgsql - comment?

I'm just getting my feet with with plpgsql and was wondering if someone could take a look at my code. I've been reading 3 different books at O'Reilly Safari on PostgreSQL but they presented several ways to do things, and didn't really give a recommedation as which route to go. Also, I just have a few general questions:

Do functions always have to return a value?
Is it possible to invoke functions without using select?
Are there any performance concerns with calling multiple functions from within other functions, as I've done in add_bookmark()?

Thanks! Any comments welcome


Code:
-- create table to hold bookmarks
CREATE TABLE bookmark
(
  id serial NOT NULL
, parent_id int4 NOT NULL
, description varchar(64) NOT NULL
, url varchar(255)
, rank int4 NOT NULL DEFAULT 1
, date_created timestamp(0) with time zone DEFAULT CURRENT_TIMESTAMP
, date_updated timestamp(0) with time zone DEFAULT CURRENT_TIMESTAMP
, PRIMARY KEY (id)
);

INSERT INTO bookmark (id, parent_id, description, rank) VALUES (0,0,'Bookmarks',0);

-- implement parent/child integrity
ALTER TABLE bookmark
ADD FOREIGN KEY (parent_id) 
REFERENCES bookmark (id)
ON UPDATE CASCADE
ON DELETE CASCADE;

-- implement rank unique index
CREATE UNIQUE INDEX bookmark_u1
ON bookmark (parent_id, rank);

INSERT INTO bookmark (parent_id, description, rank) VALUES (0,'News',1);
INSERT INTO bookmark (parent_id, description, rank) VALUES (0,'Programming',2);
INSERT INTO bookmark (parent_id, description, rank) VALUES (0,'Entertainment',3);
INSERT INTO bookmark (parent_id, description, rank) VALUES (0,'Financial',4);

-- create function to get max rank of all children from input parent_id
CREATE OR REPLACE FUNCTION get_child_max_rank(bookmark.parent_id%TYPE) RETURNS INTEGER AS '

DECLARE
  
  max_rank bookmark.rank%TYPE;
   
BEGIN
  
  SELECT INTO max_rank
    MAX(rank) FROM bookmark
    WHERE parent_id = $1;

  IF max_rank IS NULL THEN
    max_rank = 0;
  END IF;  

  return max_rank;

END;

' LANGUAGE 'plpgsql';



-- create function to add bookmark
CREATE OR REPLACE FUNCTION add_bookmark(bookmark.parent_id%TYPE, bookmark.description%TYPE, bookmark.url%TYPE) RETURNS BOOLEAN AS '

DECLARE
  
  max_rank bookmark.rank%TYPE;
     
BEGIN
  
  max_rank := get_child_max_rank($1);

  INSERT INTO bookmark
  (parent_id, description, url, rank)
  VALUES ($1, $2, $3, max_rank + 1);

  RETURN true;

END;

' LANGUAGE 'plpgsql';

Last edited by cliffyman : July 12th, 2003 at 11:30 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > first go at plpgsql - comment?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap