Skip to content

Instantly share code, notes, and snippets.

View Samuyi's full-sized avatar
🎯
Focusing

Samuyi Obasuyi Samuyi

🎯
Focusing
  • Far Away
View GitHub Profile
@Samuyi
Samuyi / flattener.js
Last active December 6, 2017 00:04
Flatten arrays with javascript
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
@Samuyi
Samuyi / sales_func.sql
Last active January 7, 2019 12:01
Potgresql stored function example
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'
@Samuyi
Samuyi / tables.sql
Last active January 7, 2019 12:20
tables for a tutorial
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,
@Samuyi
Samuyi / quarterly_summary_func.sql
Last active January 7, 2019 12:14
a postgreql example function for summarizing data
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
@Samuyi
Samuyi / car_bonus.sql
Created January 7, 2019 12:16
a sample postgresql function
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;
@Samuyi
Samuyi / passwd_table.sql
Last active January 9, 2019 05:14
passwd sample table for my article
CREATE TABLE passwd (
id serial primary key,
name text,
password text,
position text
);
@Samuyi
Samuyi / passwd_func.sql
Created January 8, 2019 16:22
passwd function for my article
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;
@Samuyi
Samuyi / passwd_trigger.sql
Created January 8, 2019 16:35
postgresql trigger
CREATE TRIGGER passwd_trigger BEFORE INSERT OR UPDATE
ON passwd
FOR EACH ROW EXECUTE PROCEDURE passwd_func();
@Samuyi
Samuyi / account_table.sql
Created January 8, 2019 16:39
more tables
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,
@Samuyi
Samuyi / audit_trigger.sql
Created January 8, 2019 16:43
a stored function for a trigger
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