diff -r 000000000000 -r 616aeab8d3c0 sql/schéma.sql --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sql/schéma.sql Tue Apr 28 22:26:46 2009 +0200 @@ -0,0 +1,562 @@ +-- +-- PostgreSQL database dump +-- + +-- Started on 2009-04-28 21:06:29 CEST + +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = off; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET escape_string_warning = off; + +-- +-- TOC entry 6 (class 2615 OID 16386) +-- Name: miniobchod; Type: SCHEMA; Schema: -; Owner: miniobchod +-- + +CREATE SCHEMA miniobchod; + + +ALTER SCHEMA miniobchod OWNER TO miniobchod; + +SET search_path = miniobchod, pg_catalog; + +-- +-- TOC entry 332 (class 1247 OID 16398) +-- Dependencies: 333 6 +-- Name: email; Type: DOMAIN; Schema: miniobchod; Owner: miniobchod +-- + +CREATE DOMAIN email AS character varying(255) NOT NULL + CONSTRAINT email_regexp CHECK (((VALUE)::text ~ '^[_a-zA-Z0-9\\.\\-]+@[_a-zA-Z0-9\\.\\-]+\\.[a-zA-Z]{2,4}$'::text)); + + +ALTER DOMAIN miniobchod.email OWNER TO miniobchod; + +-- +-- TOC entry 1821 (class 0 OID 0) +-- Dependencies: 332 +-- Name: DOMAIN email; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON DOMAIN email IS 'e-mailová adresa'; + + +-- +-- TOC entry 344 (class 1247 OID 16566) +-- Dependencies: 345 6 +-- Name: psc; Type: DOMAIN; Schema: miniobchod; Owner: miniobchod +-- + +CREATE DOMAIN psc AS character varying(255) NOT NULL + CONSTRAINT psc_regexp CHECK (((VALUE)::text ~ '^\\d{3} ?\\d{2}$'::text)); + + +ALTER DOMAIN miniobchod.psc OWNER TO miniobchod; + +-- +-- TOC entry 1822 (class 0 OID 0) +-- Dependencies: 344 +-- Name: DOMAIN psc; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON DOMAIN psc IS 'Poštovní směrovací číslo'; + + +-- +-- TOC entry 55 (class 1255 OID 16459) +-- Dependencies: 6 +-- Name: nahodne_heslo(); Type: FUNCTION; Schema: miniobchod; Owner: miniobchod +-- + +CREATE FUNCTION nahodne_heslo() RETURNS character + AS $$select sha512(random()::text || random()::text || random()::text);$$ + LANGUAGE sql; + + +ALTER FUNCTION miniobchod.nahodne_heslo() OWNER TO miniobchod; + +-- +-- TOC entry 56 (class 1255 OID 16460) +-- Dependencies: 6 +-- Name: sha1(text); Type: FUNCTION; Schema: miniobchod; Owner: miniobchod +-- + +CREATE FUNCTION sha1(text) RETURNS text + AS $_$ +SELECT encode(digest(decode($1,'escape'::text),'sha1'::text),'hex'); +$_$ + LANGUAGE sql IMMUTABLE STRICT; + + +ALTER FUNCTION miniobchod.sha1(text) OWNER TO miniobchod; + +-- +-- TOC entry 57 (class 1255 OID 16461) +-- Dependencies: 6 +-- Name: sha256(text); Type: FUNCTION; Schema: miniobchod; Owner: miniobchod +-- + +CREATE FUNCTION sha256(text) RETURNS text + AS $_$ +SELECT encode(digest(decode($1,'escape'::text),'sha256'::text),'hex'); +$_$ + LANGUAGE sql IMMUTABLE STRICT; + + +ALTER FUNCTION miniobchod.sha256(text) OWNER TO miniobchod; + +-- +-- TOC entry 54 (class 1255 OID 16458) +-- Dependencies: 6 +-- Name: sha512(text); Type: FUNCTION; Schema: miniobchod; Owner: miniobchod +-- + +CREATE FUNCTION sha512(text) RETURNS text + AS $_$ +SELECT encode(digest(decode($1,'escape'::text),'sha512'::text),'hex'); +$_$ + LANGUAGE sql IMMUTABLE STRICT; + + +ALTER FUNCTION miniobchod.sha512(text) OWNER TO miniobchod; + +-- +-- TOC entry 58 (class 1255 OID 16608) +-- Dependencies: 350 6 +-- Name: vyjimka(character varying); Type: FUNCTION; Schema: miniobchod; Owner: miniobchod +-- + +CREATE FUNCTION vyjimka(chyba character varying) RETURNS void + AS $$BEGIN +RAISE EXCEPTION '%', chyba; +END;$$ + LANGUAGE plpgsql; + + +ALTER FUNCTION miniobchod.vyjimka(chyba character varying) OWNER TO miniobchod; + +-- +-- TOC entry 1823 (class 0 OID 0) +-- Dependencies: 58 +-- Name: FUNCTION vyjimka(chyba character varying); Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON FUNCTION vyjimka(chyba character varying) IS 'Vyhodí výjimku'; + + +-- +-- TOC entry 1528 (class 1259 OID 16575) +-- Dependencies: 6 +-- Name: adresa_seq; Type: SEQUENCE; Schema: miniobchod; Owner: miniobchod +-- + +CREATE SEQUENCE adresa_seq + INCREMENT BY 1 + NO MAXVALUE + NO MINVALUE + CACHE 1; + + +ALTER TABLE miniobchod.adresa_seq OWNER TO miniobchod; + +SET default_tablespace = ''; + +SET default_with_oids = false; + +-- +-- TOC entry 1529 (class 1259 OID 16578) +-- Dependencies: 1804 332 344 6 +-- Name: adresa; Type: TABLE; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +CREATE TABLE adresa ( + id bigint DEFAULT nextval('adresa_seq'::regclass) NOT NULL, + uzivatel email, + jmeno character varying(255), + ulice character varying(255), + cp smallint, + mesto character varying(255), + psc psc +); + + +ALTER TABLE miniobchod.adresa OWNER TO miniobchod; + +-- +-- TOC entry 1824 (class 0 OID 0) +-- Dependencies: 1529 +-- Name: TABLE adresa; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON TABLE adresa IS 'Poštovní adresa'; + + +-- +-- TOC entry 1825 (class 0 OID 0) +-- Dependencies: 1529 +-- Name: COLUMN adresa.jmeno; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN adresa.jmeno IS 'Jméno příjemce'; + + +-- +-- TOC entry 1826 (class 0 OID 0) +-- Dependencies: 1529 +-- Name: COLUMN adresa.cp; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN adresa.cp IS 'Číslo popisné'; + + +-- +-- TOC entry 1827 (class 0 OID 0) +-- Dependencies: 1529 +-- Name: COLUMN adresa.psc; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN adresa.psc IS 'Poštovní směrovací číslo'; + + +-- +-- TOC entry 1527 (class 1259 OID 16540) +-- Dependencies: 1803 6 +-- Name: objednane_zbozi; Type: TABLE; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +CREATE TABLE objednane_zbozi ( + objednavka bigint NOT NULL, + zbozi bit varying NOT NULL, + mnozstvi smallint DEFAULT 1 NOT NULL +); + + +ALTER TABLE miniobchod.objednane_zbozi OWNER TO miniobchod; + +-- +-- TOC entry 1828 (class 0 OID 0) +-- Dependencies: 1527 +-- Name: TABLE objednane_zbozi; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON TABLE objednane_zbozi IS 'Vazební tabulka, která definuje obsah objednávky – objednané zboží'; + + +-- +-- TOC entry 1525 (class 1259 OID 16408) +-- Dependencies: 6 +-- Name: objednavka_seq; Type: SEQUENCE; Schema: miniobchod; Owner: miniobchod +-- + +CREATE SEQUENCE objednavka_seq + INCREMENT BY 1 + NO MAXVALUE + NO MINVALUE + CACHE 1; + + +ALTER TABLE miniobchod.objednavka_seq OWNER TO miniobchod; + +-- +-- TOC entry 1526 (class 1259 OID 16410) +-- Dependencies: 1800 1801 1802 6 332 +-- Name: objednavka; Type: TABLE; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +CREATE TABLE objednavka ( + id bigint DEFAULT nextval('objednavka_seq'::regclass) NOT NULL, + kod character varying DEFAULT nahodne_heslo() NOT NULL, + uzivatel email, + podano timestamp with time zone DEFAULT now() NOT NULL, + adresa bigint +); + + +ALTER TABLE miniobchod.objednavka OWNER TO miniobchod; + +-- +-- TOC entry 1829 (class 0 OID 0) +-- Dependencies: 1526 +-- Name: TABLE objednavka; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON TABLE objednavka IS 'Objednávka zboží'; + + +-- +-- TOC entry 1830 (class 0 OID 0) +-- Dependencies: 1526 +-- Name: COLUMN objednavka.id; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN objednavka.id IS 'Číslo objednávky je zároveň variabilním symbolem'; + + +-- +-- TOC entry 1831 (class 0 OID 0) +-- Dependencies: 1526 +-- Name: COLUMN objednavka.kod; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN objednavka.kod IS 'Potvrzovací kód – pošle se zákazníkovi e-mailem a ten pomocí něj objednávku potvrdí.'; + + +-- +-- TOC entry 1832 (class 0 OID 0) +-- Dependencies: 1526 +-- Name: COLUMN objednavka.uzivatel; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN objednavka.uzivatel IS 'Kdo si objednal'; + + +-- +-- TOC entry 1833 (class 0 OID 0) +-- Dependencies: 1526 +-- Name: COLUMN objednavka.podano; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN objednavka.podano IS 'Datum a čas podání objednávky'; + + +-- +-- TOC entry 1834 (class 0 OID 0) +-- Dependencies: 1526 +-- Name: COLUMN objednavka.adresa; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN objednavka.adresa IS 'Kam se má objednávka doručit'; + + +-- +-- TOC entry 1524 (class 1259 OID 16400) +-- Dependencies: 1798 1799 6 332 +-- Name: uzivatel; Type: TABLE; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +CREATE TABLE uzivatel ( + email email NOT NULL, + heslo character varying DEFAULT nahodne_heslo() NOT NULL, + zalozen timestamp with time zone DEFAULT now() +); + + +ALTER TABLE miniobchod.uzivatel OWNER TO miniobchod; + +-- +-- TOC entry 1835 (class 0 OID 0) +-- Dependencies: 1524 +-- Name: TABLE uzivatel; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON TABLE uzivatel IS 'Zákazník'; + + +-- +-- TOC entry 1836 (class 0 OID 0) +-- Dependencies: 1524 +-- Name: COLUMN uzivatel.email; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN uzivatel.email IS 'Uživatel je identifikovaný pomocí své e-mailové adresy'; + + +-- +-- TOC entry 1837 (class 0 OID 0) +-- Dependencies: 1524 +-- Name: COLUMN uzivatel.heslo; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN uzivatel.heslo IS 'Vygenerované heslo, kterým se uživatel prokazuje (pošle se mu na e-mail)'; + + +-- +-- TOC entry 1838 (class 0 OID 0) +-- Dependencies: 1524 +-- Name: COLUMN uzivatel.zalozen; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN uzivatel.zalozen IS 'Datum a čas registrace zákazníka'; + + +-- +-- TOC entry 1522 (class 1259 OID 16387) +-- Dependencies: 6 +-- Name: zbozi_seq; Type: SEQUENCE; Schema: miniobchod; Owner: miniobchod +-- + +CREATE SEQUENCE zbozi_seq + INCREMENT BY 1 + NO MAXVALUE + NO MINVALUE + CACHE 1; + + +ALTER TABLE miniobchod.zbozi_seq OWNER TO miniobchod; + +-- +-- TOC entry 1523 (class 1259 OID 16389) +-- Dependencies: 1797 6 +-- Name: zbozi; Type: TABLE; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +CREATE TABLE zbozi ( + id bigint DEFAULT nextval('zbozi_seq'::regclass) NOT NULL, + nazev character varying(255), + popis text +); + + +ALTER TABLE miniobchod.zbozi OWNER TO miniobchod; + +-- +-- TOC entry 1839 (class 0 OID 0) +-- Dependencies: 1523 +-- Name: TABLE zbozi; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON TABLE zbozi IS 'Položka katalogu'; + + +-- +-- TOC entry 1840 (class 0 OID 0) +-- Dependencies: 1523 +-- Name: COLUMN zbozi.id; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN zbozi.id IS 'Číslo zboží'; + + +-- +-- TOC entry 1841 (class 0 OID 0) +-- Dependencies: 1523 +-- Name: COLUMN zbozi.nazev; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN zbozi.nazev IS 'Název zboží'; + + +-- +-- TOC entry 1842 (class 0 OID 0) +-- Dependencies: 1523 +-- Name: COLUMN zbozi.popis; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON COLUMN zbozi.popis IS 'Popis zboží'; + + +-- +-- TOC entry 1815 (class 2606 OID 16586) +-- Dependencies: 1529 1529 +-- Name: adresa_pk; Type: CONSTRAINT; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +ALTER TABLE ONLY adresa + ADD CONSTRAINT adresa_pk PRIMARY KEY (id); + + +-- +-- TOC entry 1813 (class 2606 OID 16548) +-- Dependencies: 1527 1527 1527 +-- Name: objednane_zbozi_pk; Type: CONSTRAINT; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +ALTER TABLE ONLY objednane_zbozi + ADD CONSTRAINT objednane_zbozi_pk PRIMARY KEY (objednavka, zbozi); + + +-- +-- TOC entry 1811 (class 2606 OID 16415) +-- Dependencies: 1526 1526 +-- Name: objednavka_pk; Type: CONSTRAINT; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +ALTER TABLE ONLY objednavka + ADD CONSTRAINT objednavka_pk PRIMARY KEY (id); + + +-- +-- TOC entry 1808 (class 2606 OID 16407) +-- Dependencies: 1524 1524 +-- Name: uzivatel_pk; Type: CONSTRAINT; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +ALTER TABLE ONLY uzivatel + ADD CONSTRAINT uzivatel_pk PRIMARY KEY (email); + + +-- +-- TOC entry 1806 (class 2606 OID 16397) +-- Dependencies: 1523 1523 +-- Name: zbozi_pk; Type: CONSTRAINT; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +ALTER TABLE ONLY zbozi + ADD CONSTRAINT zbozi_pk PRIMARY KEY (id); + + +-- +-- TOC entry 1809 (class 1259 OID 16597) +-- Dependencies: 1526 +-- Name: fki_objednavka_adresa_fk; Type: INDEX; Schema: miniobchod; Owner: miniobchod; Tablespace: +-- + +CREATE INDEX fki_objednavka_adresa_fk ON objednavka USING btree (adresa); + + +-- +-- TOC entry 1605 (class 2618 OID 16598) +-- Dependencies: 1526 1529 1526 58 1526 1526 1526 1529 +-- Name: objednavka_adresa_uzivatel; Type: RULE; Schema: miniobchod; Owner: miniobchod +-- + +CREATE RULE objednavka_adresa_uzivatel AS ON INSERT TO objednavka WHERE ((new.uzivatel)::text <> ((SELECT adresa.uzivatel FROM adresa WHERE (adresa.id = new.adresa)))::text) DO SELECT vyjimka('Uživatel může objednat zboží jen na svoji adresu'::character varying) AS vyjimka; + + +-- +-- TOC entry 1843 (class 0 OID 0) +-- Dependencies: 1605 +-- Name: RULE objednavka_adresa_uzivatel ON objednavka; Type: COMMENT; Schema: miniobchod; Owner: miniobchod +-- + +COMMENT ON RULE objednavka_adresa_uzivatel ON objednavka IS 'Uživatel může objednat zboží jen na svoji adresu'; + + +-- +-- TOC entry 1818 (class 2606 OID 16587) +-- Dependencies: 1807 1529 1524 +-- Name: adresa_uzivatel_fk; Type: FK CONSTRAINT; Schema: miniobchod; Owner: miniobchod +-- + +ALTER TABLE ONLY adresa + ADD CONSTRAINT adresa_uzivatel_fk FOREIGN KEY (uzivatel) REFERENCES uzivatel(email); + + +-- +-- TOC entry 1817 (class 2606 OID 16592) +-- Dependencies: 1814 1529 1526 +-- Name: objednavka_adresa_fk; Type: FK CONSTRAINT; Schema: miniobchod; Owner: miniobchod +-- + +ALTER TABLE ONLY objednavka + ADD CONSTRAINT objednavka_adresa_fk FOREIGN KEY (adresa) REFERENCES adresa(id); + + +-- +-- TOC entry 1816 (class 2606 OID 16493) +-- Dependencies: 1807 1524 1526 +-- Name: objednavka_uzivatel_fk; Type: FK CONSTRAINT; Schema: miniobchod; Owner: miniobchod +-- + +ALTER TABLE ONLY objednavka + ADD CONSTRAINT objednavka_uzivatel_fk FOREIGN KEY (uzivatel) REFERENCES uzivatel(email); + + +-- Completed on 2009-04-28 21:06:38 CEST + +-- +-- PostgreSQL database dump complete +-- +