473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Custom Functions

I'm trying to write two custom recursive functions: one that selects,
and the other that selects/updates.

I've been looking around the internet for examples for they're so
limiting and completely off track from what I need to do. The best
examples I can find is in the source code for Postgres, but that's so
complicated I barely have an idea whats going on with its limited
comments.

So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?

Thanks in advance,

~ Matthew
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #1
5 1318
Matthew Metnetsky wrote:
I'm trying to write two custom recursive functions: one that selects,
and the other that selects/updates.

I've been looking around the internet for examples for they're so
limiting and completely off track from what I need to do. The best
examples I can find is in the source code for Postgres, but that's so
complicated I barely have an idea whats going on with its limited
comments.

So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?

Thanks in advance,

~ Matthew
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)


I've just been doing the same sort of thing using plpgsql. Here's one of
the ones I did this morning (sorry, no DDL, but it gives you an idea of
some syntax).

john

PS. I've been using this for less than a week so hold back on the abuse
please.

CREATE OR REPLACE FUNCTION get_basic_products_for_order(integer) RETURNS
setof RECORD AS
'
DECLARE
reg RECORD;
input_order_id ALIAS FOR $1;
before int;
after int;
BEGIN
/*
* Prepare tables for use.
*/
DELETE FROM GBPFO_products_list;
DELETE FROM GBPFO_to_be_added;
/*
* Record all basic products in the given order number
*/
INSERT INTO GBPFO_products_list
SELECT basic_product_id,
false,
true,
true
FROM
orderline_leaf_items
WHERE
order_id = input_order_id;
/*
* Iterate through the list, adding general products that are
children of existing
* products.
*/
LOOP
SELECT INTO before COUNT(*) FROM GBPFO_products_list;
INSERT INTO GBPFO_to_be_added
SELECT
general_products.id,
false,
false,
true
FROM
general_products,
GBPFO_products_list
WHERE
GBPFO_products_list.basic_product_id = general_products.parent_id
AND
GBPFO_products_list.active = true;
UPDATE GBPFO_products_list
SET active = false;
INSERT INTO GBPFO_products_list
SELECT * FROM GBPFO_to_be_added;
DELETE FROM GBPFO_to_be_added;
SELECT INTO after COUNT(*) FROM GBPFO_products_list;
EXIT WHEN before = after;
END LOOP;
/*
* Iterate through the list, adding specific products that are
children of existing
* products.
*/
UPDATE GBPFO_products_list SET active = true;
LOOP
SELECT INTO before COUNT(*) FROM GBPFO_products_list;
INSERT INTO GBPFO_to_be_added
SELECT
specific_products.id,
false,
false,
true
FROM
specific_products,
GBPFO_products_list
WHERE
GBPFO_products_list.basic_product_id = specific_products.parent_id
AND
GBPFO_products_list.active = true;
UPDATE GBPFO_products_list
SET active = false;
INSERT INTO GBPFO_products_list
SELECT * FROM GBPFO_to_be_added;
DELETE FROM GBPFO_to_be_added;
SELECT INTO after COUNT(*) FROM GBPFO_products_list;
EXIT WHEN before = after;
END LOOP;
/*
* Output the required result.
*/
FOR
reg
IN
SELECT general_products.id,
general_products.parent_id,
general_products.description,
0.00
FROM GBPFO_products_list,
general_products
WHERE GBPFO_products_list.is_specific = false
AND GBPFO_products_list.in_order = false
AND GBPFO_products_list.basic_product_id = general_products.id
UNION
SELECT specific_products.id,
specific_products.parent_id,
specific_products.description,
specific_products.unit_price
FROM GBPFO_products_list,
specific_products
WHERE GBPFO_products_list.is_specific = true
AND GBPFO_products_list.in_order = false
AND GBPFO_products_list.basic_product_id = specific_products.id
LOOP
RETURN NEXT reg;
END LOOP;
RETURN;
END;
'
LANGUAGE plpgsql;
Nov 23 '05 #2
> So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?


I'm not very experienced in PostgreSQL but maybe SPI functions is what you
need.

Regards,
Ntinos Katsaros
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #3
On Tue, 2004-09-28 at 10:55, Katsaros Kwn/nos wrote:
So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?


I'm not very experienced in PostgreSQL but maybe SPI functions is what you
need.


Yeah I took a look at them, but I'm not sold because when I look at
Postgres' build in functions I never see it. Its as if SPI_* is slower
or something.

Thanks though,

~ Matthew
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4
Matthew Metnetsky <me*@uberstats.com> writes:
On Tue, 2004-09-28 at 10:55, Katsaros Kwn/nos wrote:
> So, does anyone know of (or have) good examples of queries within
> functions (and returning sets of data)?


I'm not very experienced in PostgreSQL but maybe SPI functions is what you
need.


Yeah I took a look at them, but I'm not sold because when I look at
Postgres' build in functions I never see it. Its as if SPI_* is slower
or something.


SPI is an internal API for server-side functions to use. Client apps
will never use it.

I think Katsaros might have meant "set-returning functions" (SRF)
which are documented in the manual and available to client apps.

-Doug
--
Let us cross over the river, and rest under the shade of the trees.
--T. J. Jackson, 1863

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #5
Hello,

On Tue, 2004-09-28 at 13:57, Tim Penhey wrote:
Matthew Metnetsky wrote:
So, does anyone know of (or have) good examples of queries within
functions (and returning sets of data)?

Thanks in advance,

~ Matthew

Hi Matthew,

I wrote up an example which can be found at
http://www.scorefirst.com/postgresql.html, not sure if it is what you
are after or not.

Tim


That was the exact foundation I was looking for. I'm not sure why I
over looked plpgsql but I did, and its much easier than C custom
functions which are hideous and completely far from portable.

Thanks,

~ Matthew
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stanley | last post by:
Hi, I'm using FFTW library to do my FFT calculation. My sample size is so large to get the resolution I want ( >10^7 samples). It's time and memory consuming. Since I'm just interested in the...
9
by: Marcin Kalicinski | last post by:
Hi, I have a set of C-like functions for file access (like fopen, fwrite, fread, fseek etc.). But I want to access the files using C++ stream, not these functions. What I probably need to do is...
2
by: Sean Mullaly | last post by:
I have a custom Access menu with sub-menus and sub-sub-menus. (220 items). Right now I have created 220 Functions and attached each individual Function to the specific OnAction property. The...
22
by: TC | last post by:
I have an Access database application with a lot of custom row functions written in VBA. In other words, a lot of queries contain calculated fields which use functions defined in the modules. I...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
3
by: useenmelately | last post by:
Hello, I am working with asp.net 2.0 and have run into a problem with the custom validators. The client validation functions were originally written in vbscript, but as this is not compatible...
1
by: ThunderMusic | last post by:
Hi, I'm making a Custom Web Control and I'm wondering how I can insert Javascript code into the head part of the document from within my web control... I lloked at the methods of the...
27
by: Wayne | last post by:
I've been clicking around Access 2007 Beta 2 and can't see the custom menu bar designer. Is it in the beta? Maybe I'm blind. The question that comes to mind is: Will custom menu bars be the same...
27
by: Keith Wilby | last post by:
I've been trying to get my head around custom classes by following the example in the Visual Basic Language Developer's Handbook by Sybex. I think I have a handle on what they're about, albeit a...
1
by: ncsthbell | last post by:
I have a dilemia, I have an application that I am having to make some changes to in order for it to run sucessfully under Runtime 2007. We are currently working on upgrading from 2000 to 2007. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.