Make WordPress Core

Ticket #15652: system.sortable.sql

File system.sortable.sql, 2.6 KB (added by Denis-de-Bernardy, 13 years ago)
Line 
1/**
2 * @param varchar _table The table's name.
3 * @param varchar _field Optional. The table field to use. Defaults to name.
4 * @return varchar The table's name.
5 */
6CREATE OR REPLACE FUNCTION sortable(varchar, varchar = 'name')
7        RETURNS varchar
8AS $$
9DECLARE
10        _table          alias for $1;
11        _field          alias for $2;
12BEGIN
13        IF      NOT system.column_exists('sort', _table)
14        THEN
15                EXECUTE $X$
16                ALTER TABLE $X$ || quote_ident(_table) || $X$
17                ADD COLUMN sort varchar NOT NULL DEFAULT '';
18                $X$;
19        END IF;
20       
21        IF      NOT system.constraint_exists(_table || '_sort_check', _table)
22        THEN
23                EXECUTE $X$
24                ALTER TABLE $X$ || quote_ident(_table) || $X$
25                ADD CONSTRAINT $X$ || quote_ident(_table || '_sort_check') || $X$
26                        CHECK (sort = public.natsort($X$ || quote_ident(_field) || $X$));
27                $X$;
28        END IF;
29       
30        IF      NOT system.function_exists(_table || '_sort')
31        THEN
32                EXECUTE $X$
33                CREATE OR REPLACE FUNCTION $X$ || quote_ident(_table || '_sort') || $X$()
34                        RETURNS trigger
35                AS $DEF$
36                BEGIN
37                        NEW.sort := public.natsort(NEW.$X$ || quote_ident(_field) || $X$);
38                        RETURN NEW;
39                END;
40                $DEF$ LANGUAGE plpgsql;
41                $X$;
42        END IF;
43       
44        IF      NOT system.trigger_exists(_table || '_90_sort_ins', _table)
45        THEN
46                EXECUTE $X$
47                CREATE TRIGGER $X$ || quote_ident(_table || '_90_sort_ins') || $X$
48                        BEFORE INSERT ON $X$ || quote_ident(_table) || $X$
49                FOR EACH ROW
50                WHEN (NEW.$X$ || quote_ident(_field) || $X$ <> '' AND
51                        NEW.sort <> public.natsort(NEW.$X$ || quote_ident(_field) || $X$))
52                EXECUTE PROCEDURE $X$ || quote_ident(_table || '_sort') || $X$();
53                $X$;
54        END IF;
55       
56        IF      NOT system.trigger_exists(_table || '_90_sort_upd', _table)
57        THEN
58                EXECUTE $X$
59                CREATE TRIGGER $X$ || quote_ident(_table || '_90_sort_upd') || $X$
60                        BEFORE UPDATE ON $X$ || quote_ident(_table) || $X$
61                FOR EACH ROW
62                WHEN (OLD.$X$ || quote_ident(_field) || $X$ <> NEW.$X$ || quote_ident(_field) || $X$ AND
63                        NEW.sort <> public.natsort(NEW.$X$ || quote_ident(_field) || $X$))
64                EXECUTE PROCEDURE $X$ || quote_ident(_table || '_sort') || $X$();
65                $X$;
66        END IF;
67       
68        IF      NOT system.index_exists(_table || '_sort_idx', _table)
69        THEN
70                IF      system.column_exists('status', _table)
71                THEN
72                        EXECUTE $X$
73                        CREATE INDEX $X$ || quote_ident(_table || '_sort_idx') || $X$
74                                ON $X$ || quote_ident(_table) || $X$ (status, sort);
75                        $X$;
76                ELSE
77                        EXECUTE $X$
78                        CREATE INDEX $X$ || quote_ident(_table || '_sort_idx') || $X$
79                                ON $X$ || quote_ident(_table) || $X$ (sort);
80                        $X$;
81                END IF;
82        END IF;
83       
84        RETURN _table;
85END;
86$$ LANGUAGE plpgsql;
87
88COMMENT ON FUNCTION system.sortable(varchar, varchar) IS
89'Sortable behavior. Automatically manages a table''s sort field based on another field.';