🗄️ SQL PACT DIFFICULTY: Adept Creational

The Factory Method of Procedural Genesis

Stored procedures that abstract the creation pacts of underlying data structures.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Factory Method
ARCANUM SCHOOL: Conjuration // Procedural Forging
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SQL PATTERN //
CREATE TYPE construct_class AS ENUM ('GOLEM', 'WISP', 'PHANTOM');

CREATE TABLE ethereal_constructs (
    construct_id SERIAL PRIMARY KEY,
    class construct_class,
    mana_cost INT
);

-- The Factory Method
CREATE OR REPLACE FUNCTION forge_construct(c_class construct_class) 
RETURNS INT AS $$
DECLARE
    c_id INT;
    base_mana INT;
BEGIN
    IF c_class = 'GOLEM' THEN
        base_mana := 500;
    ELSIF c_class = 'WISP' THEN
        base_mana := 50;
    ELSIF c_class = 'PHANTOM' THEN
        base_mana := 250;
    END IF;

    INSERT INTO ethereal_constructs (class, mana_cost)
    VALUES (c_class, base_mana)
    RETURNING construct_id INTO c_id;

    RETURN c_id;
END;
$$ LANGUAGE plpgsql;

The Factory Method of Procedural Genesis

The direct INSERT command is a dangerous incantation for apprentices, leaving raw tabular structures exposed to the chaotic aether. The Factory Method obscures these creation pacts within a stored procedure.

Through conditional routing and internal parameter binding, the forge_construct function safely encapsulates the complexities of manifesting varied ethereal forms, returning only the binding identifier (the primary key) to the invoker.

Runic Tags: #procedures #creational #functions