An experimental web framework where PostgreSQL does everything: routing, HTML, file storage, jobs and sessions.
helper_functions.sql · 8.3 KB · 322 lines
| 1 | -- Helper functions for web development (like early PHP) |
| 2 | -- These provide HTML encoding, URL encoding/decoding and other web utilities |
| 3 | |
| 4 | -- HTML encode function (like PHP's htmlspecialchars) |
| 5 | CREATE OR REPLACE FUNCTION html_encode(input text) |
| 6 | RETURNS text AS $$ |
| 7 | BEGIN |
| 8 | IF input IS NULL THEN |
| 9 | RETURN NULL; |
| 10 | END IF; |
| 11 | |
| 12 | -- Replace HTML special characters |
| 13 | RETURN replace( |
| 14 | replace( |
| 15 | replace( |
| 16 | replace( |
| 17 | replace(input, '&', '&'), |
| 18 | '<', '<'), |
| 19 | '>', '>'), |
| 20 | '"', '"'), |
| 21 | '''', '''); |
| 22 | END; |
| 23 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 24 | |
| 25 | -- HTML decode function |
| 26 | CREATE OR REPLACE FUNCTION html_decode(input text) |
| 27 | RETURNS text AS $$ |
| 28 | BEGIN |
| 29 | IF input IS NULL THEN |
| 30 | RETURN NULL; |
| 31 | END IF; |
| 32 | |
| 33 | -- Replace HTML entities back to characters |
| 34 | RETURN replace( |
| 35 | replace( |
| 36 | replace( |
| 37 | replace( |
| 38 | replace(input, '<', '<'), |
| 39 | '>', '>'), |
| 40 | '"', '"'), |
| 41 | ''', ''''), |
| 42 | '&', '&'); |
| 43 | END; |
| 44 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 45 | |
| 46 | -- URL encode function (like PHP's urlencode) |
| 47 | CREATE OR REPLACE FUNCTION url_encode(input text) |
| 48 | RETURNS text AS $$ |
| 49 | DECLARE |
| 50 | result text := ''; |
| 51 | char_code int; |
| 52 | i int; |
| 53 | BEGIN |
| 54 | IF input IS NULL THEN |
| 55 | RETURN NULL; |
| 56 | END IF; |
| 57 | |
| 58 | -- Encode each character |
| 59 | FOR i IN 1..length(input) LOOP |
| 60 | char_code := ascii(substring(input, i, 1)); |
| 61 | |
| 62 | -- Check if character needs encoding |
| 63 | IF (char_code >= 48 AND char_code <= 57) OR -- 0-9 |
| 64 | (char_code >= 65 AND char_code <= 90) OR -- A-Z |
| 65 | (char_code >= 97 AND char_code <= 122) OR -- a-z |
| 66 | char_code IN (45, 46, 95, 126) THEN -- - . _ ~ |
| 67 | -- Safe character, don't encode |
| 68 | result := result || substring(input, i, 1); |
| 69 | ELSIF char_code = 32 THEN |
| 70 | -- Space becomes + |
| 71 | result := result || '+'; |
| 72 | ELSE |
| 73 | -- Encode as %HH |
| 74 | result := result || '%' || upper(to_hex(char_code)); |
| 75 | END IF; |
| 76 | END LOOP; |
| 77 | |
| 78 | RETURN result; |
| 79 | END; |
| 80 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 81 | |
| 82 | -- URL decode function (like PHP's urldecode) |
| 83 | CREATE OR REPLACE FUNCTION url_decode(input text) |
| 84 | RETURNS text AS $$ |
| 85 | DECLARE |
| 86 | result text := ''; |
| 87 | i int := 1; |
| 88 | len int; |
| 89 | hex_str text; |
| 90 | BEGIN |
| 91 | IF input IS NULL THEN |
| 92 | RETURN NULL; |
| 93 | END IF; |
| 94 | |
| 95 | len := length(input); |
| 96 | |
| 97 | WHILE i <= len LOOP |
| 98 | IF substring(input, i, 1) = '%' AND i + 2 <= len THEN |
| 99 | -- Decode %HH |
| 100 | hex_str := substring(input, i + 1, 2); |
| 101 | IF hex_str ~ '^[0-9A-Fa-f]{2}$' THEN |
| 102 | result := result || chr(('x' || hex_str)::bit(8)::int); |
| 103 | i := i + 3; |
| 104 | ELSE |
| 105 | -- Invalid hex sequence, keep the % |
| 106 | result := result || '%'; |
| 107 | i := i + 1; |
| 108 | END IF; |
| 109 | ELSIF substring(input, i, 1) = '+' THEN |
| 110 | -- + becomes space |
| 111 | result := result || ' '; |
| 112 | i := i + 1; |
| 113 | ELSE |
| 114 | -- Regular character |
| 115 | result := result || substring(input, i, 1); |
| 116 | i := i + 1; |
| 117 | END IF; |
| 118 | END LOOP; |
| 119 | |
| 120 | RETURN result; |
| 121 | END; |
| 122 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 123 | |
| 124 | -- Base64 encode function |
| 125 | CREATE OR REPLACE FUNCTION base64_encode(input text) |
| 126 | RETURNS text AS $$ |
| 127 | BEGIN |
| 128 | RETURN encode(input::bytea, 'base64'); |
| 129 | END; |
| 130 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 131 | |
| 132 | -- Base64 decode function |
| 133 | CREATE OR REPLACE FUNCTION base64_decode(input text) |
| 134 | RETURNS text AS $$ |
| 135 | BEGIN |
| 136 | RETURN convert_from(decode(input, 'base64'), 'UTF8'); |
| 137 | END; |
| 138 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 139 | |
| 140 | -- JSON escape function (for embedding data in JSON strings) |
| 141 | CREATE OR REPLACE FUNCTION json_escape(input text) |
| 142 | RETURNS text AS $$ |
| 143 | BEGIN |
| 144 | IF input IS NULL THEN |
| 145 | RETURN NULL; |
| 146 | END IF; |
| 147 | |
| 148 | -- Use PostgreSQL's built-in JSON escaping |
| 149 | RETURN to_json(input)::text; |
| 150 | END; |
| 151 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 152 | |
| 153 | -- Query string parse function (like PHP's parse_str) |
| 154 | -- Returns a JSONB object from URL query string |
| 155 | CREATE OR REPLACE FUNCTION parse_query_string(query_string text) |
| 156 | RETURNS jsonb AS $$ |
| 157 | DECLARE |
| 158 | params jsonb := '{}'::jsonb; |
| 159 | pair text; |
| 160 | key text; |
| 161 | value text; |
| 162 | pos int; |
| 163 | BEGIN |
| 164 | IF query_string IS NULL OR query_string = '' THEN |
| 165 | RETURN params; |
| 166 | END IF; |
| 167 | |
| 168 | -- Remove leading ? if present |
| 169 | IF left(query_string, 1) = '?' THEN |
| 170 | query_string := substring(query_string, 2); |
| 171 | END IF; |
| 172 | |
| 173 | -- Split by & and process each pair |
| 174 | FOREACH pair IN ARRAY string_to_array(query_string, '&') LOOP |
| 175 | pos := position('=' IN pair); |
| 176 | IF pos > 0 THEN |
| 177 | key := url_decode(substring(pair, 1, pos - 1)); |
| 178 | value := url_decode(substring(pair, pos + 1)); |
| 179 | ELSE |
| 180 | key := url_decode(pair); |
| 181 | value := ''; |
| 182 | END IF; |
| 183 | |
| 184 | -- Add to params |
| 185 | params := params || jsonb_build_object(key, value); |
| 186 | END LOOP; |
| 187 | |
| 188 | RETURN params; |
| 189 | END; |
| 190 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 191 | |
| 192 | -- Build query string from JSONB (like PHP's http_build_query) |
| 193 | CREATE OR REPLACE FUNCTION build_query_string(params jsonb) |
| 194 | RETURNS text AS $$ |
| 195 | DECLARE |
| 196 | result text := ''; |
| 197 | key text; |
| 198 | value text; |
| 199 | first boolean := true; |
| 200 | BEGIN |
| 201 | IF params IS NULL THEN |
| 202 | RETURN ''; |
| 203 | END IF; |
| 204 | |
| 205 | FOR key, value IN SELECT * FROM jsonb_each_text(params) LOOP |
| 206 | IF NOT first THEN |
| 207 | result := result || '&'; |
| 208 | END IF; |
| 209 | result := result || url_encode(key) || '=' || url_encode(value); |
| 210 | first := false; |
| 211 | END LOOP; |
| 212 | |
| 213 | RETURN result; |
| 214 | END; |
| 215 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 216 | |
| 217 | -- Strip HTML tags (like PHP's strip_tags) |
| 218 | CREATE OR REPLACE FUNCTION strip_tags(input text) |
| 219 | RETURNS text AS $$ |
| 220 | BEGIN |
| 221 | IF input IS NULL THEN |
| 222 | RETURN NULL; |
| 223 | END IF; |
| 224 | |
| 225 | -- Remove all HTML tags |
| 226 | RETURN regexp_replace(input, '<[^>]+>', '', 'g'); |
| 227 | END; |
| 228 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 229 | |
| 230 | -- Truncate text with ellipsis |
| 231 | CREATE OR REPLACE FUNCTION truncate_text(input text, max_length int, ellipsis text DEFAULT '...') |
| 232 | RETURNS text AS $$ |
| 233 | BEGIN |
| 234 | IF input IS NULL OR length(input) <= max_length THEN |
| 235 | RETURN input; |
| 236 | END IF; |
| 237 | |
| 238 | RETURN left(input, max_length - length(ellipsis)) || ellipsis; |
| 239 | END; |
| 240 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 241 | |
| 242 | -- Generate random string (useful for tokens, session IDs, etc.) |
| 243 | CREATE OR REPLACE FUNCTION random_string(len int) |
| 244 | RETURNS text AS $$ |
| 245 | DECLARE |
| 246 | chars text := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 247 | result text := ''; |
| 248 | i int; |
| 249 | BEGIN |
| 250 | FOR i IN 1..len LOOP |
| 251 | result := result || substring(chars, floor(random() * length(chars) + 1)::int, 1); |
| 252 | END LOOP; |
| 253 | |
| 254 | RETURN result; |
| 255 | END; |
| 256 | $$ LANGUAGE plpgsql; |
| 257 | |
| 258 | -- Slugify function (convert text to URL-friendly slug) |
| 259 | CREATE OR REPLACE FUNCTION slugify(input text) |
| 260 | RETURNS text AS $$ |
| 261 | BEGIN |
| 262 | IF input IS NULL THEN |
| 263 | RETURN NULL; |
| 264 | END IF; |
| 265 | |
| 266 | RETURN lower( |
| 267 | regexp_replace( |
| 268 | regexp_replace( |
| 269 | regexp_replace( |
| 270 | input, |
| 271 | '[^a-zA-Z0-9\s-]', '', 'g' -- Remove special chars |
| 272 | ), |
| 273 | '\s+', '-', 'g' -- Replace spaces with hyphens |
| 274 | ), |
| 275 | '-+', '-', 'g' -- Replace multiple hyphens with single |
| 276 | ) |
| 277 | ); |
| 278 | END; |
| 279 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 280 | |
| 281 | -- Email validation function |
| 282 | CREATE OR REPLACE FUNCTION is_valid_email(email text) |
| 283 | RETURNS boolean AS $$ |
| 284 | BEGIN |
| 285 | RETURN email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'; |
| 286 | END; |
| 287 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 288 | |
| 289 | -- Get file extension from filename |
| 290 | CREATE OR REPLACE FUNCTION get_file_extension(filename text) |
| 291 | RETURNS text AS $$ |
| 292 | DECLARE |
| 293 | pos int; |
| 294 | BEGIN |
| 295 | pos := position('.' IN reverse(filename)); |
| 296 | IF pos > 0 THEN |
| 297 | RETURN lower(right(filename, pos - 1)); |
| 298 | END IF; |
| 299 | RETURN ''; |
| 300 | END; |
| 301 | $$ LANGUAGE plpgsql IMMUTABLE; |
| 302 | |
| 303 | -- Format bytes as human-readable size |
| 304 | CREATE OR REPLACE FUNCTION format_bytes(bytes bigint) |
| 305 | RETURNS text AS $$ |
| 306 | DECLARE |
| 307 | units text[] := ARRAY['B', 'KB', 'MB', 'GB', 'TB']; |
| 308 | unit_index int := 1; |
| 309 | size numeric := bytes; |
| 310 | BEGIN |
| 311 | WHILE size >= 1024 AND unit_index < array_length(units, 1) LOOP |
| 312 | size := size / 1024; |
| 313 | unit_index := unit_index + 1; |
| 314 | END LOOP; |
| 315 | |
| 316 | IF unit_index = 1 THEN |
| 317 | RETURN bytes || ' B'; |
| 318 | ELSE |
| 319 | RETURN round(size, 2) || ' ' || units[unit_index]; |
| 320 | END IF; |
| 321 | END; |
| 322 | $$ LANGUAGE plpgsql IMMUTABLE; |