Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to function templates #467

Merged

Conversation

samir-puranik
Copy link
Collaborator

@samir-puranik samir-puranik commented Aug 23, 2023

  • Updated template files ensuring
    • add PgAdmin License header
    • change data.func_without_args to data.func_args
    • macros are being pathed to properly
  • Using .server instead of ._server outside of NodeObject class
  • Added conn to FunctionBase query_data methods

@samir-puranik samir-puranik requested a review from a team as a code owner August 23, 2023 17:47
@DaeunYim
Copy link
Contributor

functions as well as Indexes and constraints all look good. Really nicely done!

Teseted on pg 11-15


CREATE TABLE students (
    student_id SERIAL PRIMARY KEY,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    age INT CHECK (age >= 0 AND age <= 120),
    enrollment_date DATE DEFAULT CURRENT_DATE,
    graduation_date DATE CHECK (graduation_date >= enrollment_date OR graduation_date IS NULL)
);

CREATE RULE prevent_student_delete AS
ON DELETE TO students
DO INSTEAD NOTHING;

CREATE TABLE student_log (
    log_id SERIAL PRIMARY KEY,
    student_id INT,
    action VARCHAR(100),
    log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE OR REPLACE FUNCTION log_student_insert()
RETURNS TRIGGER AS $$
BEGIN
    INSERT INTO student_log (student_id, action) 
    VALUES (NEW.student_id, 'INSERTED');
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_log_student_insert
AFTER INSERT ON students
FOR EACH ROW
EXECUTE FUNCTION log_student_insert();


CREATE INDEX idx_students_last_name ON students (last_name);
CREATE INDEX idx_students_first_last_name ON students (first_name, last_name);
CREATE INDEX idx_students_adults ON students (student_id) WHERE age >= 18;
ALTER TABLE students ADD COLUMN courses INT[];
CREATE INDEX idx_students_courses ON students USING gin(courses);
-- Assuming you have a table with geometric points representing locations
CREATE INDEX idx_students_email_hash ON students USING hash(email);
CREATE INDEX idx_students_enrollment_date_brin ON students USING brin(enrollment_date);



CREATE OR REPLACE FUNCTION avg_student_age() 
RETURNS FLOAT AS $$
DECLARE
    average_age FLOAT;
BEGIN
    SELECT AVG(age) INTO average_age FROM students;
    RETURN average_age;
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION duration_enroll_to_grad(student_id INT) 
RETURNS INT AS $$
DECLARE
    days_duration INT;
BEGIN
    SELECT EXTRACT(DAY FROM (graduation_date - enrollment_date)) 
    INTO days_duration 
    FROM students 
    WHERE student_id = $1;
    
    RETURN days_duration;
END;
$$ LANGUAGE plpgsql;


CREATE OR REPLACE PROCEDURE enroll_multiple_students(students_data JSONB[])
LANGUAGE plpgsql AS $$
DECLARE
    student_record JSONB;
BEGIN
    FOR i IN 1..array_length(students_data, 1) LOOP
        student_record := students_data[i];
        INSERT INTO students (first_name, last_name, email, age) 
        VALUES (student_record ->> 'first_name', student_record ->> 'last_name', student_record ->> 'email', (student_record ->> 'age')::INT);
    END LOOP;
    COMMIT;
END;
$$;

@DaeunYim
Copy link
Contributor

One minor issue for the last PR added.
Index script as create misses 'IF NOT EXISTS ' which I don't understand why since your have added create_index sql file that includes that line.

I think, going forward, we can add trigger scripts (currently show error for when clicking script as create) and other scripts if possible.

@samir-puranik
Copy link
Collaborator Author

One minor issue for the last PR added. Index script as create misses 'IF NOT EXISTS ' which I don't understand why since your have added create_index sql file that includes that line.

I think, going forward, we can add trigger scripts (currently show error for when clicking script as create) and other scripts if possible.

Hi Daeun, ah yes I see that for index scripts. can add that in the next PR

@samir-puranik samir-puranik merged commit a7f10d8 into master Aug 24, 2023
@nasc17 nasc17 added the Release-v0.6 Bug fix to be included in Release-v0.6 of PgSql extension label Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release-v0.6 Bug fix to be included in Release-v0.6 of PgSql extension
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants