Mike Barkmin

Zufälliger Benutzername als Postgres-Funktion

Ich habe versucht eindeutige zufällige Benutzernamen direkt in einer Postgres-Funktion generieren zulassen. Das ist das Ergebnis:

CREATE OR REPLACE FUNCTION generate_username() 
RETURNS text AS $$
DECLARE
  animals text[] := ARRAY[
  'Aardvark',
  'Albatross',
  'Alligator',
  'Alpaca',
  'Ant',
  'Anteater',
  'Antelope',
  'Ape',
  'Armadillo',
  'Donkey',
  'Baboon',
  'Badger',
  'Barracuda',
  'Bat',
  'Bear',
  'Beaver',
  'Bee',
  'Bison',
  'Boar',
  'Buffalo',
  'Butterfly',
  'Camel',
  'Capybara',
  'Caribou',
  'Cassowary',
  'Cat',
  'Caterpillar',
  'Cattle',
  'Chamois',
  'Cheetah',
  'Chicken',
  'Chimpanzee',
  'Chinchilla',
  'Chough',
  'Clam',
  'Cobra',
  'Cockroach',
  'Cod',
  'Cormorant',
  'Coyote',
  'Crab',
  'Crane',
  'Crocodile',
  'Crow',
  'Curlew',
  'Deer',
  'Dinosaur',
  'Dog',
  'Dogfish',
  'Dolphin',
  'Dotterel',
  'Dove',
  'Dragonfly',
  'Duck',
  'Dugong',
  'Dunlin',
  'Eagle',
  'Echidna',
  'Eel',
  'Eland',
  'Elephant',
  'Elk',
  'Emu',
  'Falcon',
  'Ferret',
  'Finch',
  'Fish',
  'Flamingo',
  'Fly',
  'Fox',
  'Frog',
  'Gaur',
  'Gazelle',
  'Gerbil',
  'Giraffe',
  'Gnat',
  'Gnu',
  'Goat',
  'Goldfinch',
  'Goldfish',
  'Goose',
  'Gorilla',
  'Goshawk',
  'Grasshopper',
  'Grouse',
  'Guanaco',
  'Gull',
  'Hamster',
  'Hare',
  'Hawk',
  'Hedgehog',
  'Heron',
  'Herring',
  'Hippopotamus',
  'Hornet',
  'Horse',
  'Human',
  'Hummingbird',
  'Hyena',
  'Ibex',
  'Ibis',
  'Jackal',
  'Jaguar',
  'Jay',
  'Jellyfish',
  'Kangaroo',
  'Kingfisher',
  'Koala',
  'Kookabura',
  'Kouprey',
  'Kudu',
  'Lapwing',
  'Lark',
  'Lemur',
  'Leopard',
  'Lion',
  'Llama',
  'Lobster',
  'Locust',
  'Loris',
  'Louse',
  'Lyrebird',
  'Magpie',
  'Mallard',
  'Manatee',
  'Mandrill',
  'Mantis',
  'Marten',
  'Meerkat',
  'Mink',
  'Mole',
  'Mongoose',
  'Monkey',
  'Moose',
  'Mosquito',
  'Mouse',
  'Mule',
  'Narwhal',
  'Newt',
  'Nightingale',
  'Octopus',
  'Okapi',
  'Opossum',
  'Oryx',
  'Ostrich',
  'Otter',
  'Owl',
  'Oyster',
  'Panther',
  'Parrot',
  'Partridge',
  'Peafowl',
  'Pelican',
  'Penguin',
  'Pheasant',
  'Pig',
  'Pigeon',
  'Pony',
  'Porcupine',
  'Porpoise',
  'Quail',
  'Quelea',
  'Quetzal',
  'Rabbit',
  'Raccoon',
  'Rail',
  'Ram',
  'Rat',
  'Raven',
  'Red deer',
  'Red panda',
  'Reindeer',
  'Rhinoceros',
  'Rook',
  'Salamander',
  'Salmon',
  'Sand Dollar',
  'Sandpiper',
  'Sardine',
  'Scorpion',
  'Seahorse',
  'Seal',
  'Shark',
  'Sheep',
  'Shrew',
  'Skunk',
  'Snail',
  'Snake',
  'Sparrow',
  'Spider',
  'Spoonbill',
  'Squid',
  'Squirrel',
  'Starling',
  'Stingray',
  'Stinkbug',
  'Stork',
  'Swallow',
  'Swan',
  'Tapir',
  'Tarsier',
  'Termite',
  'Tiger',
  'Toad',
  'Trout',
  'Turkey',
  'Turtle',
  'Viper',
  'Vulture',
  'Wallaby',
  'Walrus',
  'Wasp',
  'Weasel',
  'Whale',
  'Wildcat',
  'Wolf',
  'Wolverine',
  'Wombat',
  'Woodcock',
  'Woodpecker',
  'Worm',
  'Wren',
  'Yak',
  'Zebra'
  ];
  adjectives text[] := ARRAY[
      'Admirable',
  'Adroit',
  'Affectionate',
  'Agreeable',
  'Alluring',
  'Altruistic',
  'Amazing',
  'Ambitious',
  'Amiable',
  'Amicable',
  'Amused',
  'Angelic',
  'Approachable',
  'Articulate',
  'Artistic',
  'Assiduous',
  'Astounding',
  'Attentive',
  'Authentic',
  'Aware',
  'Awesome',
  'Balanced',
  'Beautiful',
  'Benevolent',
  'Blissful',
  'Bold',
  'Breathtaking',
  'Bright',
  'Brilliant',
  'Bubbly',
  'Buoyant',
  'Calm',
  'Capable',
  'Captivating',
  'Carefree',
  'Careful',
  'Caring',
  'Centered',
  'Charismatic',
  'Charming',
  'Cheerful',
  'Chivalrous',
  'Civil',
  'Classic',
  'Clever',
  'Colorful',
  'Companionable',
  'Compassionate',
  'Compelling',
  'Composed',
  'Conciliatory',
  'Confident',
  'Conscientious',
  'Considerate',
  'Consistent',
  'Constructive',
  'Content',
  'Convivial',
  'Cool',
  'Cooperative',
  'Cordial',
  'Courageous',
  'Courteous',
  'Creative',
  'Credible',
  'Cultured',
  'Curious',
  'Dapper',
  'Daring',
  'Dashing',
  'Debonair',
  'Decisive',
  'Dedicated',
  'Deep',
  'Definite',
  'Delicate',
  'Delightful',
  'Dependable',
  'Deserving',
  'Determined',
  'Devoted',
  'Dignified',
  'Diligent',
  'Diplomatic',
  'Discerning',
  'Discreet',
  'Distinguished',
  'Divine',
  'Dynamic',
  'Eager',
  'Earnest',
  'Easygoing',
  'Ebullient',
  'Eclectic',
  'Ecstatic',
  'Educated',
  'Effervescent',
  'Efficient',
  'Effortless',
  'Elated',
  'Elegant',
  'Eloquent',
  'Empathetic',
  'Empathic',
  'Enchanting',
  'Endearing',
  'Enduring',
  'Energetic',
  'Engaging',
  'Enhanced',
  'Enlightened',
  'Enterprising',
  'Entertaining',
  'Enthusiastic',
  'Ethical',
  'Euphoric',
  'Excellent',
  'Exceptional',
  'Exemplary',
  'Exhilarating',
  'Expansive',
  'Experienced',
  'Extraordinary',
  'Exuberant',
  'Fabulous',
  'Fair',
  'Faithful',
  'Famous',
  'Fantastic',
  'Fascinating',
  'Fearless',
  'Fertile',
  'Fervent',
  'Fierce',
  'Fit',
  'Flamboyant',
  'Flawless',
  'Flourishing',
  'Focused',
  'Fortunate',
  'Friendly',
  'Fulfilled',
  'Fun',
  'Funny',
  'Futuristic',
  'Gallant',
  'Generous',
  'Genial',
  'Gentle',
  'Genuine',
  'Giddy',
  'Giving',
  'Glamorous',
  'Gleeful',
  'Glorious',
  'Good',
  'Gorgeous',
  'Graceful',
  'Gracious',
  'Grateful',
  'Great',
  'Gregarious',
  'Groundbreaking',
  'Grounded',
  'Gutsy',
  'Happy',
  'Hardworking',
  'Harmonious',
  'Hearty',
  'Helpful',
  'Heroic',
  'Hilarious',
  'Holy',
  'Honest',
  'Honorable',
  'Hopeful',
  'Hospitable',
  'Humble',
  'Humorous',
  'Idealistic',
  'Illuminated',
  'Illustrious',
  'Imaginative',
  'Immaculate',
  'Impartial',
  'Impeccable',
  'Impressive',
  'Incomparable',
  'Incredible',
  'Independent',
  'Industrious',
  'Ineffable',
  'Innovative',
  'Insightful',
  'Inspirational',
  'Inspired',
  'Intelligent',
  'Intuitive',
  'Inventive',
  'Invincible',
  'Inviting',
  'Jolly',
  'Jovial',
  'Joyful',
  'Joyous',
  'Jubilant',
  'Judicious',
  'Keen',
  'Kind',
  'Kindhearted',
  'Knowledgeable',
  'Laudable',
  'Legendary',
  'Lighthearted',
  'Likable',
  'Lively',
  'Lovable',
  'Lovely',
  'Loving',
  'Loyal',
  'Luminous',
  'Magnanimous',
  'Magnificent',
  'Majestic',
  'Marvelous',
  'Memorable',
  'Meticulous',
  'Mindful',
  'Miraculous',
  'Motivated',
  'Motivating',
  'Natural',
  'Noble',
  'Nurturing',
  'Observant',
  'Optimistic',
  'Orderly',
  'Organized',
  'Original',
  'Outgoing',
  'Outstanding',
  'Passionate',
  'Patient',
  'Peaceful',
  'Perceptive',
  'Perky',
  'Persevering',
  'Persistent',
  'Phenomenal',
  'Philanthropic',
  'Philosophical',
  'Playful',
  'Pleasant',
  'Plucky',
  'Polished',
  'Polite',
  'Popular',
  'Positive',
  'Powerful',
  'Practical',
  'Praiseworthy',
  'Precious',
  'Proactive',
  'Profound',
  'Progressive',
  'Prosperous',
  'Protective',
  'Prudent',
  'Punctual',
  'Radiant',
  'Rational',
  'Reassuring',
  'Refined',
  'Refreshing',
  'Reliable',
  'Remarkable',
  'Resilient',
  'Resourceful',
  'Respectful',
  'Responsible',
  'Revered',
  'Rewarding',
  'Righteous',
  'Romantic',
  'Sagacious',
  'Sage',
  'Saintly',
  'Satisfied',
  'Savvy',
  'Sincere',
  'Skillful',
  'Smart',
  'Sociable',
  'Sophisticated',
  'Soulful',
  'Spectacular',
  'Spirited',
  'Spiritual',
  'Spontaneous',
  'Stellar',
  'Stoic',
  'Strong',
  'Stunning',
  'Successful',
  'Supportive',
  'Surprising',
  'Sweet',
  'Sympathetic',
  'Talented',
  'Tender',
  'Terrific',
  'Thankful',
  'Thorough',
  'Thoughtful',
  'Thrilling',
  'Tidy',
  'Tolerant',
  'Tranquil',
  'Trustworthy',
  'Truthful',
  'Unique',
  'Upbeat',
  'Uplifting',
  'Valiant',
  'Versatile',
  'Vibrant',
  'Victorious',
  'Vigorous',
  'Virtuous',
  'Vivacious',
  'Warm',
  'Warmhearted',
  'Welcoming',
  'Whimsical',
  'Wise',
  'Witty',
  'Wonderful',
  'Worthy',
  'Youthful',
  'Zany',
  'Zealous',
  'Zestful',
  'Zesty'
  ];
  username_new text;
  username_exists boolean := true;
BEGIN
  WHILE username_exists LOOP
    username_new := concat(
      adjectives[ceil((random() * array_length(adjectives, 1)))::int],
      animals[ceil((random() * array_length(animals, 1)))::int],  
      floor(random() * 9)::int, 
      floor(random() * 9)::int
    );
    
    SELECT EXISTS(SELECT 1 FROM public.profiles WHERE username = username_new) INTO username_exists;
  END LOOP;

  RETURN username_new;
END;
$$ LANGUAGE plpgsql;

Jetzt kann man zum Beispiel den Standardwert für Benutzernamen auf generate_username() setzen.

Ausprobieren kann man die Funktion mit diesem Ausdruck:

SELECT generate_username();