9.10. Функции для перечислений

Для типов перечислений (описанных в Разделе 8.7) предусмотрено несколько функций, которые позволяют сделать код чище, не «зашивая» в нём конкретные значения перечисления. Эти функции перечислены в Таблице 9.34. В этих примерах подразумевается, что перечисление создано так:

CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');

Таблица 9.34. Функции для перечислений

Функция

Описание

Пример(ы)

enum_first ( anyenum ) → anyenum

Возвращает первое значение заданного перечисления.

enum_first(null::rainbow)red

enum_last ( anyenum ) → anyenum

Возвращает последнее значение заданного перечисления.

enum_last(null::rainbow)purple

enum_range ( anyenum ) → anyarray

Возвращает все значения заданного перечисления в упорядоченном массиве.

enum_range(null::rainbow){red,orange,yellow,​green,blue,purple}

enum_range ( anyenum, anyenum ) → anyarray

Возвращает набор значений перечисления, лежащих между двумя заданными, в виде упорядоченного массива. Значения в параметрах должны принадлежать одному перечислению. Если в первом параметре передаётся NULL, результат функции начинается с первого значения перечисления, а если во втором — заканчивается последним.

enum_range('orange'::rainbow, 'green'::rainbow){orange,yellow,green}

enum_range(NULL, 'green'::rainbow){red,orange,​yellow,green}

enum_range('orange'::rainbow, NULL){orange,yellow,green,​blue,purple}


Заметьте, что за исключением варианта enum_range с двумя аргументами, эти функции не обращают внимание на конкретное переданное им значение; их интересует только объявленный тип. Они возвращают один и тот же результат, когда им передаётся NULL или любое другое значение типа. Обычно эти функции применяются к столбцам таблицы или аргументам внешних функций, а не к предопределённым типам, как в этих примерах.

9.10. Enum Support Functions

For enum types (described in Section 8.7), there are several functions that allow cleaner programming without hard-coding particular values of an enum type. These are listed in Table 9.34. The examples assume an enum type created as:

CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');

Table 9.34. Enum Support Functions

Function

Description

Example(s)

enum_first ( anyenum ) → anyenum

Returns the first value of the input enum type.

enum_first(null::rainbow)red

enum_last ( anyenum ) → anyenum

Returns the last value of the input enum type.

enum_last(null::rainbow)purple

enum_range ( anyenum ) → anyarray

Returns all values of the input enum type in an ordered array.

enum_range(null::rainbow){red,orange,yellow,​green,blue,purple}

enum_range ( anyenum, anyenum ) → anyarray

Returns the range between the two given enum values, as an ordered array. The values must be from the same enum type. If the first parameter is null, the result will start with the first value of the enum type. If the second parameter is null, the result will end with the last value of the enum type.

enum_range('orange'::rainbow, 'green'::rainbow){orange,yellow,green}

enum_range(NULL, 'green'::rainbow){red,orange,​yellow,green}

enum_range('orange'::rainbow, NULL){orange,yellow,green,​blue,purple}


Notice that except for the two-argument form of enum_range, these functions disregard the specific value passed to them; they care only about its declared data type. Either null or a specific value of the type can be passed, with the same result. It is more common to apply these functions to a table column or function argument than to a hardwired type name as used in the examples.

FAQ