Changes between Version 8 and Version 9 of Stored Functions and Procedures


Ignore:
Timestamp:
05/18/26 23:38:58 (8 days ago)
Author:
193284
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Stored Functions and Procedures

    v8 v9  
    33== Description ==
    44
    5 This section contains reusable SQL functions and procedures used for calculations, validation, and reporting.
     5This section documents advanced PostgreSQL PL/pgSQL functions and procedures implemented for analytical processing, booking validation, overlap prevention, and RSVP reporting.
     6
     7The implementation demonstrates reusable database-side logic for operational validation and analytical reporting.
    68
    79== 1. calculate_wedding_total_cost() ==
     
    911=== Description ===
    1012
    11 Calculates the total confirmed cost of a wedding using venue, photographer, and band bookings.
    12 
    13 The function calculates venue costs directly from booking prices and additionally computes photographer and band costs based on booked duration and hourly pricing.
     13Calculates the total wedding cost by aggregating:
     14* venue booking expenses
     15* photographer booking expenses
     16* band booking expenses
    1417
    1518=== SQL Code ===
     
    1922CREATE OR REPLACE FUNCTION calculate_wedding_total_cost(p_wedding_id INTEGER)
    2023RETURNS NUMERIC AS $$
     24
    2125DECLARE
     26
    2227    venue_cost NUMERIC := 0;
    2328    photographer_cost NUMERIC := 0;
    2429    band_cost NUMERIC := 0;
    25 BEGIN
     30
     31BEGIN
     32
    2633    SELECT COALESCE(SUM(price), 0)
    2734    INTO venue_cost
     
    3441        * p.price_per_hour
    3542    ), 0)
     43
    3644    INTO photographer_cost
     45
    3746    FROM photographer_booking pb
    38     JOIN photographer p ON pb.photographer_id = p.photographer_id
     47
     48    JOIN photographer p
     49        ON pb.photographer_id = p.photographer_id
     50
    3951    WHERE pb.wedding_id = p_wedding_id
    4052      AND pb.status = 'confirmed';
     
    4456        * b.price_per_hour
    4557    ), 0)
     58
    4659    INTO band_cost
     60
    4761    FROM band_booking bb
    48     JOIN band b ON bb.band_id = b.band_id
     62
     63    JOIN band b
     64        ON bb.band_id = b.band_id
     65
    4966    WHERE bb.wedding_id = p_wedding_id
    5067      AND bb.status = 'confirmed';
    5168
    5269    RETURN venue_cost + photographer_cost + band_cost;
     70
    5371END;
    5472$$ LANGUAGE plpgsql;
     
    5977=== Description ===
    6078
    61 Checks whether a venue is available for a selected date and time interval.
    62 
    63 This implementation uses complete timestamp interval comparison instead of simple date equality checks in order to correctly detect all possible overlap scenarios.
    64 
    65 This function compares complete timestamp intervals by combining the booking date with start and end time. It does not only compare equal dates.
    66 
    67 The overlap rule is:
    68 
    69 {{{
    70 #!sql
    71 (new_start < existing_end) AND (new_end > existing_start)
    72 }}}
    73 
    74 This condition detects all possible overlap cases:
    75 * the new interval starts inside an existing interval
    76 * the new interval ends inside an existing interval
    77 * the new interval completely contains an existing interval
    78 * the existing interval completely contains the new interval
    79 * both intervals are exactly the same
    80 * intervals that only touch at the boundary are not treated as overlapping
     79Checks whether a venue is available for a requested time interval.
     80
     81The implementation explicitly validates multiple overlap scenarios using separate interval comparison conditions.
     82
     83The validation detects:
     84* intervals starting inside existing intervals
     85* intervals ending inside existing intervals
     86* intervals completely containing existing intervals
     87* intervals fully contained inside existing intervals
     88* identical intervals
     89* partial overlaps
     90
     91Boundary-touching intervals are treated as valid non-overlapping intervals.
    8192
    8293=== SQL Code ===
     
    8596#!sql
    8697CREATE OR REPLACE FUNCTION is_venue_available(
     98
    8799    p_venue_id INTEGER,
    88100    p_date DATE,
    89101    p_start TIME,
    90102    p_end TIME
     103
    91104)
     105
    92106RETURNS BOOLEAN AS $$
    93 BEGIN
     107
     108BEGIN
     109
    94110    IF p_end <= p_start THEN
    95         RAISE EXCEPTION 'Invalid interval: end time must be after start time.';
     111        RAISE EXCEPTION
     112        'Invalid interval: end time must be after start time.';
    96113    END IF;
    97114
    98115    RETURN NOT EXISTS (
     116
    99117        SELECT 1
     118
    100119        FROM venue_booking vb
     120
    101121        WHERE vb.venue_id = p_venue_id
    102122          AND vb.status <> 'cancelled'
     123
    103124          AND (
    104125
     
    122143          )
    123144    );
     145
    124146END;
    125147$$ LANGUAGE plpgsql;
     148}}}
    126149
    127150== 3. is_photographer_available() ==
     
    129152=== Description ===
    130153
    131 Checks whether a photographer is available for a selected date and time interval.
    132 
    133 This implementation uses complete timestamp interval comparison instead of simple date equality checks in order to correctly detect all possible overlap scenarios.
    134 
    135 The function combines booking dates and times into complete timestamp intervals in order to correctly validate overlapping reservations.
     154Checks whether a photographer is available for a requested booking interval.
     155
     156The function validates all important overlap scenarios and prevents conflicting reservations.
    136157
    137158=== SQL Code ===
     
    140161#!sql
    141162CREATE OR REPLACE FUNCTION is_photographer_available(
     163
    142164    p_photographer_id INTEGER,
    143165    p_date DATE,
    144166    p_start TIME,
    145167    p_end TIME
     168
    146169)
     170
    147171RETURNS BOOLEAN AS $$
    148 BEGIN
     172
     173BEGIN
     174
    149175    IF p_end <= p_start THEN
    150         RAISE EXCEPTION 'Invalid interval: end time must be after start time.';
     176        RAISE EXCEPTION
     177        'Invalid interval: end time must be after start time.';
    151178    END IF;
    152179
    153180    RETURN NOT EXISTS (
     181
    154182        SELECT 1
     183
    155184        FROM photographer_booking pb
     185
    156186        WHERE pb.photographer_id = p_photographer_id
    157187          AND pb.status <> 'cancelled'
     188
    158189          AND (
    159190
     
    177208          )
    178209    );
     210
    179211END;
    180212$$ LANGUAGE plpgsql;
     213}}}
    181214
    182215== 4. is_band_available() ==
     
    184217=== Description ===
    185218
    186 Checks whether a band is available for a selected date and time interval.
    187 
    188 This implementation uses complete timestamp interval comparison instead of simple date equality checks in order to correctly detect all possible overlap scenarios.
    189 
    190 The function validates overlapping booking intervals using complete timestamp comparison between requested and existing reservations.
     219Checks whether a music band is available for a requested booking interval.
     220
     221The implementation prevents all major overlap conflict scenarios.
    191222
    192223=== SQL Code ===
     
    195226#!sql
    196227CREATE OR REPLACE FUNCTION is_band_available(
     228
    197229    p_band_id INTEGER,
    198230    p_date DATE,
    199231    p_start TIME,
    200232    p_end TIME
     233
    201234)
     235
    202236RETURNS BOOLEAN AS $$
    203 BEGIN
     237
     238BEGIN
     239
    204240    IF p_end <= p_start THEN
    205         RAISE EXCEPTION 'Invalid interval: end time must be after start time.';
     241        RAISE EXCEPTION
     242        'Invalid interval: end time must be after start time.';
    206243    END IF;
    207244
    208245    RETURN NOT EXISTS (
     246
    209247        SELECT 1
     248
    210249        FROM band_booking bb
     250
    211251        WHERE bb.band_id = p_band_id
    212252          AND bb.status <> 'cancelled'
     253
    213254          AND (
    214255
     
    232273          )
    233274    );
     275
    234276END;
    235277$$ LANGUAGE plpgsql;
     278}}}
    236279
    237280== 5. generate_rsvp_summary() ==
     
    239282=== Description ===
    240283
    241 Returns RSVP statistics for a selected event.
    242 
    243 The procedure generates summarized RSVP information including accepted, declined, and pending responses for analytical and reporting purposes.
    244 
    245 === SQL Code ===
    246 
    247 {{{
    248 #!sql
    249 CREATE OR REPLACE PROCEDURE generate_rsvp_summary(p_event_id INTEGER)
     284Generates RSVP statistics for a selected event.
     285
     286The procedure reports:
     287* accepted RSVPs
     288* declined RSVPs
     289* pending RSVPs
     290
     291=== SQL Code ===
     292
     293{{{
     294#!sql
     295CREATE OR REPLACE PROCEDURE generate_rsvp_summary(
     296    p_event_id INTEGER
     297)
     298
    250299LANGUAGE plpgsql
     300
    251301AS $$
    252 BEGIN
     302
     303BEGIN
     304
    253305    RAISE NOTICE 'Accepted: %',
     306
    254307    (
    255308        SELECT COUNT(*)
     309
    256310        FROM event_rsvp
     311
    257312        WHERE event_id = p_event_id
    258313          AND status = 'accepted'
     
    260315
    261316    RAISE NOTICE 'Declined: %',
     317
    262318    (
    263319        SELECT COUNT(*)
     320
    264321        FROM event_rsvp
     322
    265323        WHERE event_id = p_event_id
    266324          AND status = 'declined'
     
    268326
    269327    RAISE NOTICE 'Pending: %',
     328
    270329    (
    271330        SELECT COUNT(*)
     331
    272332        FROM event_rsvp
     333
    273334        WHERE event_id = p_event_id
    274335          AND status = 'pending'
    275336    );
     337
    276338END;
    277339$$;
    278340}}}
     341
     342== Summary ==
     343
     344The implemented functions and procedures provide:
     345* reusable analytical logic
     346* overlap conflict prevention
     347* interval validation
     348* vendor availability checking
     349* RSVP reporting
     350* financial calculation functionality
     351
     352The implementation demonstrates advanced procedural programming capabilities inside PostgreSQL using PL/pgSQL.