Output := output || substr(shuffle_alphabet, (id % char_length) + 1, 1) Ĭurse := consistent_shuffle(curse, output || salt) Shuffle_alphabet TEXT := consistent_shuffle(alphabet, salt) The main function: CREATE OR REPLACE FUNCTION generate_uid(id INT, min_length INT, salt TEXT) RETURNS TEXT AS $$Ĭlean_alphabet TEXT := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' Ĭurse TEXT := curse_chars || UPPER(curse_chars) Īlphabet TEXT := regexp_replace(clean_alphabet, '', '', 'gi') Output := substr(output, 1, i) || temp || substr(output, i + 2) Output := substr(output, 1, j) || substr(output, i + 1, 1) || substr(output, j + 2) Integer := ASCII(substr(salt, v + 1, 1)) IF salt IS NULL OR length(LTRIM(RTRIM(salt))) = 0 THEN Shuffle alphabet: CREATE OR REPLACE FUNCTION consistent_shuffle(alphabet TEXT, salt TEXT) RETURNS TEXT AS $$ Thanks to Evan Carroll answer, I took a look on .įor Postgres you have to compile the extension or run some TSQL functions.īut for my needs, I created something simpler based on hashids ideas (short, unguessable, unique, custom alphabet, avoid curse words). CREATE FUNCTION random_string(randomLength int)įROM generate_series(1,randomLength) AS gs(x)Īnd then SELECT * FROM random_string(10) SELECT array_to_string(įrom here we can even turn it into a function. Then we wrap it in an ARRAY constructor (because this is fast) SELECT ARRAY(Īnd, we call array_to_string() to get a text. Now we need a range between 1 and 63 SELECT trunc(random()*62+1)::int+1 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', First we demonstrate that we can take the random-range and pull from it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |