Global Variables?

Поиск
Список
Период
Сортировка
От Eric Radman
Тема Global Variables?
Дата
Msg-id 20111011140650.GA22580@SDF.ORG
обсуждение исходный текст
Ответы Re: Global Variables?  (Alban Hertroys <haramrae@gmail.com>)
Re: Global Variables?  (Achilleas Mantzios <achill@matrix.gatewaynet.com>)
Список pgsql-general
When writing unit tests it's sometimes useful to stub functions such as
the current date and time

-- define mock functions
CREATE OR REPLACE FUNCTION _now() RETURNS timestamp with time zone AS $$
  BEGIN RETURN '2011-10-10 10:00'; END;
$$ LANGUAGE plpgsql;

-- define tables "accounts"
CREATE TABLE accounts (username varchar, expiration timestamp);

-- populate with sample data
COPY accounts FROM '/home/eradman/sample_accounts.txt';

-- define view "expired_accounts"
CREATE OR REPLACE VIEW expired_accounts AS SELECT * FROM accounts WHERE expiration < _now();

-- test views
SELECT assert(0, (SELECT count(*) FROM expired_accounts)::integer);

Is it possible to declare a global variable that can be referenced from
the user-defined function _now()? I'm looking for a means of abstraction
that allows me to avoid issuing CREATE OR REPLACE FUNCTION ... before
each assert()

current_time := '2012-01-01'::timestamp
SELECT assert(5, (SELECT count(*) FROM expired_accounts)::integer);

--
Eric Radman  |  http://eradman.com

В списке pgsql-general по дате отправления:

Предыдущее
От: "Krishnanand Gopinathan Sathikumari"
Дата:
Сообщение: Question on GiST re-index
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Should casting to integer produce same result as trunc()