This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flattener(array, newArray=[]) { | |
// test if parameters supplied is an array | |
if (!Array.isArray(array) && !Array.isArray(newArray)) { | |
throw new Error('parameters must be arrays!'); | |
} | |
// loop through the array | |
for (let i = 0; i < array.length; ++i) { | |
// check if element at index i is an array | |
if (Array.isArray(array[i])) { | |
// if element at index i is an array recursively call flattener function if true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION sales_func(employee_id int, vehicle_id int) | |
RETURNS SETOF sales | |
AS $$ | |
DECLARE | |
car_model text; | |
car_price int; | |
sales_bonus int; | |
bonus int; | |
BEGIN | |
EXECUTE 'SELECT model, sales_bonus, price FROM cars WHERE car_id = $1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE staff ( | |
staff_id serial primary key, | |
name text, | |
salary int, | |
created_at timestamp with time zone default now() | |
); | |
CREATE TABLE cars( | |
car_id serial primary key, | |
brand text, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION quarterly_summary_func(start_date date DEFAULT CURRENT_TIMESTAMP) | |
RETURNS TABLE (staff_name text, staff_bonus int, quarter tsrange) | |
As $$ | |
DECLARE | |
employee RECORD; | |
total_bonus int; | |
sales_total int; | |
end_date date := start_date + interval '3 months'; | |
BEGIN | |
FOR employee IN SELECT staff_id FROM staff LOOP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION car_bonus_func(vehicle_id int, bonus int) | |
RETURNS cars | |
AS $$ | |
DECLARE | |
car cars; | |
BEGIN | |
PERFORM model FROM cars WHERE car_id = vehicle_id; | |
IF NOT FOUND THEN | |
RAISE EXCEPTION 'car with id of % not found', vehicle_id; | |
END IF; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE passwd ( | |
id serial primary key, | |
name text, | |
password text, | |
position text | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION passwd_func() | |
RETURNS TRIGGER | |
AS $$ | |
BEGIN | |
IF length(NEW.password) < 10 OR NEW.password IS NULL THEN | |
RAISE EXCEPTION 'password cannot be less than 10 characters'; | |
END IF; | |
IF NEW.NAME IS NULL THEN | |
RAISE EXCEPTION 'Name cannot be NULL'; | |
END IF; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TRIGGER passwd_trigger BEFORE INSERT OR UPDATE | |
ON passwd | |
FOR EACH ROW EXECUTE PROCEDURE passwd_func(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE account ( | |
id serial primary key, | |
name text, | |
debt int, | |
balance int | |
); | |
CREATE TABLE account_audit( | |
id serial primary key, | |
db_user text NOT NULL default session_user, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION account_audit_func() | |
RETURNS TRIGGER | |
AS $$ | |
BEGIN | |
IF TG_OP = 'INSERT' THEN | |
INSERT INTO account_audit (operation, account_id, account_name, debt, balance) VALUES | |
(TG_OP, NEW.*); | |
RETURN NEW; | |
ELSIF TG_OP = 'UPDATE' THEN | |
INSERT INTO account_audit (operation, account_id, account_name, debt, balance) VALUES |
OlderNewer