Add helpful functions for interacting with db manually (#3602)

Brought over from modrinth/labrinth#862
This commit is contained in:
Emma Alexia 2025-05-03 08:15:15 -04:00 committed by GitHub
parent bd0d6a9ac0
commit f71830e0fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,40 @@
CREATE OR REPLACE FUNCTION from_base62(input VARCHAR)
RETURNS BIGINT AS $$
DECLARE
base INT := 62;
chars VARCHAR := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
result BIGINT := 0;
i INT;
char VARCHAR;
index INT;
BEGIN
FOR i IN 1..LENGTH(input) LOOP
char := SUBSTRING(input FROM i FOR 1);
index := POSITION(char IN chars) - 1;
IF index < 0 THEN
RAISE EXCEPTION 'Error: Invalid character in input string';
END IF;
result := result * base + index;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION to_base62(input BIGINT)
RETURNS VARCHAR AS $$
DECLARE
base INT := 62;
chars VARCHAR := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
result VARCHAR := '';
remainder INT;
BEGIN
WHILE input > 0 LOOP
remainder := input % base;
result := SUBSTRING(chars FROM remainder + 1 FOR 1) || result;
input := input / base;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;