Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Add helpful functions for interacting with db manually #862

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions migrations/20240115052708_base62-helper-functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
CREATE OR REPLACE FUNCTION base62to10(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 base10to62(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;
Loading