«99 бутылок» и «Постгрес»
Недавно моему сайту исполнилось 25 лет и я вспоминал свою первую заметку, с которой всё началось. Она положила начало традиции, которая длится столько, сколько существует этот сайт — писать «песню о пиве», это такая американская считалочка, на различных языках программирования.
Когда я пересматривал ту заметку, обратил внимание насколько сложно написана была «песня о пиве» на языке СУБД «Постгрес». Это был
(
WITH phrase(ph) AS (
SELECT COALESCE(NULLIF(beer, 0)::text, 'no') || ' bottle' ||
CASE beer WHEN 1 THEN '' ELSE 's' END || ' of beer'
FROM generate_series(99, 0, -1) AS c(beer)
)
SELECT ph || ' on the wall, ' || ph || '.' || e'\n' ||
'Take one down and pass it around, ' ||
LEAD(ph) OVER() || e' on the wall.\n' AS " "
FROM phrase LIMIT 99
)
UNION ALL SELECT
'No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.';
Комментарии 7
select format(
e'%1$s bottle%3$s of beer on the wall, %1$s bottle%3$s of beer.\nTake one down and pass it around, %2$s bottles of beer on the wall.',
i, case (i — 1) when 0 then 'no' else (i — 1)::text end, (array ['s'])[(i — 1)::bool::int]
)
from generate_series(99, 1, -1) as bc(i)
union all
select 'No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.';