select into
select into
newBeer
❖ Week 04 Thursday |
select
into
if not found then
raise notice
execute(
)
❖ 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
integer
real
text
char(8)
create table
create view
create type
❖ Functions vs Views |
Functions returning setof
Views are used like tables.
What's the difference?
❖ Exercise: Function vs View |
Consider a view that gives Drinkers, but without their phone:
create or replace view d1(drinker,surburb) as ...
Then implement the same thing as a function
Then add parameter to restrict to names matching pattern
❖ 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
seq(n)
seq(n,m)
seq(n,m,incr)
seq()
❖ select into |
Can capture individual attributes in a select statement
select att1, att2, ... attn into var1, var2, ... varn from ...
Notes:
atti
atti
vari
create table R (...); declare tup R; select * into tup from R where Condition;
❖ 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
❖ 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 ...
❖ 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)
❖ Exercise: Missing Data |
Implement a PLpgSQL function
create or replace function tfnOf(name text) returns text as $$ ...
That takes a name as argument ...
❖ 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
❖ 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)
❖ Exercise: A newBeer |
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
Beers
Brewed_by
Notice
id
Beers.id
serial
❖ 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
❖ Dynamic Queries |
Queries can be "built" as strings of SQL code within functions
And then evaluated using execute
Including constant and identifiers in queries needs
quote_literal
quote_ident
❖ Exercise: Dynamic Queries |
Write a PLpgSQL function
create or replce function nTuples(tableName text) return integer as $$ ...
The function
dbPop()
Produced: 9 Mar 2023