COMP3311 Week 4 Thursday Lecture

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [0/16]
❖ Week 04 Thursday

In today's lecture ...

Things to do ...

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [1/16]
❖ More on PLpgSQL Functions

Function definition ...

create or replace function
   FunctionName ( Parameters ) returns [setof] Type
as $$
declare
   VariableDeclarations
begin
   FunctionBody
end;
$$ language ProgLanguage

Type  can be

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [2/16]
❖ Functions vs Views


Functions returning setof are used like tables.

Views are used like tables.

What's the difference?

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [3/16]
❖ Exercise: Function vs View


Consider a view that gives Drinkers, but without their phone:

create or replace view d1(drinker,surburb)
as
...

Implement this view

Then implement the same thing as a function

Then add parameter to restrict to names matching pattern

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [4/16]
❖ Exercise: Function Overloading

A function is udentified by its name and parameter types.

Can have several functions with same name, but different parameters.

Consider a function seq to return sequences of integer values

Implement this as three different PLpgSQL functions, all called  seq()
COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [5/16]
select into

Can capture individual attributes in a select statement

select att1, att2, ... attn
  into var1, var2, ... varn
from   ...

Notes:

Can also assign whole tuples, e.g.

create table R (...);

declare tup R;

select * into tup from R where Condition;

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [6/16]
❖ Exercise: select into


Consider a table  R(a integer, b text, c float)

Implement different ways to collect individual attributes into variables

Variables are  x integer, y text, z float

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [7/16]
❖ Missing Data

With functions, often use parameter values to find tuples

What to do if value leads to no match ...

select * into tup from R where name ~ Value
if (not found) then
   handle case for no matching tuple
else
   handle case for matching tuple
end if

Alternative method (but requires two selects if there is a match)

select count(*) into nmatches from R where name ~ Value
if (nmatches = 0) then ...
COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [8/16]
❖ A new database


Examples based on Department/Employee database:

Departments(id, name, manager))
Employees(tfn,givenname,familyname,hourspweek,salary)
WorksFor(employee, department, percentage)
DeptMission(department,keyword)

Exercise: implement views and functions for

EmpDept(employee, department, fraction)

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [9/16]
❖ Exercise: Missing Data


Implement a PLpgSQL function

create or replace function
	tfnOf(name text) returns text
as $$ ...

That takes a name as argument ...

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [10/16]
❖ Exercise: Generating a Table


Consider that we want a mechanism to give tuples

create type EmpDept as
(employee text, department text, fraction float)

using the data in the employees/departments database

Implement this as a view and as a function

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [11/16]
❖ An "old" database

Recall the database for Assignment 1

Countries(id, code, name)

Locations(id, within, region, metro, town)

Styles(id, name, min_abv, max_abv)

Ingredients(id, itype, name, origin)

Beers(id, name, vintage, style, abv, ibu, sold_in, ...)

Contains(beer, ingredient)

Brewed_by(beer, brewery)

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [12/16]
❖ Exercise: A newBeer Function

Write a PLpgSQL function on the Ass1 database

create or replace function
   addBeer(_name text, _brewery text, _style text, _abv float)
   returns integer
as $$ ...

that takes the name, brewery, style and abv

What if the  Beers.id  field is defined as  serial ?
COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [13/16]
❖ Debugging PLpgSQL

PLpgSQL does not have I/O functions

How to do debugging (see intermediate states)?

Use PostgreSQL logging statement

raise notice 'Message' [ , Expr1 , Expr2 , ... ]

Examples:

declare x integer; y float;
...
raise notice 'x = %', x
raise notice 'x = %, y = %', x, y
raise notice 'x^2 = %', x*x

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [14/16]
❖ Dynamic Queries

Queries can be "built" as strings of SQL code within functions

And then evaluated using  execute  Query

Including constant and identifiers in queries needs

COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [15/16]
❖ Exercise: Dynamic Queries

Write a PLpgSQL function

create or replce function
   nTuples(tableName text) return integer
as $$ ...

The function


See the  dbPop()  function for an extension of this idea.
COMP3311 22T3 ♢ Week 4 Thursday Lecture ♢ [16/16]


Produced: 9 Mar 2023