ENUM
An enum, short for enumeration, is a data type in Accio that represents a set of predefined named values. It is used to define a collection of distinct values that an variable of that enum type can take. Enumerations provide a way to create more expressive and self-documenting code by giving meaningful names to the allowed values.
ENUM can be treated as a data type in column. In the following example, enum OrderStatus defined three kinds of value, FULFILLED
, PROCESSING
and OPEN
. And Orders.orderstatus type is OrderStatus, that means the values within the column are limited to only those defined by the enum OrderStatus.
enum OrderStatus {
FULFILLED: 'F',
PROCESSING: 'P',
OPEN: 'O',
}
Model Orders @sql(select * from tpch_tiny.orders) {
orderkey: INTEGER! @primaryKey
custkey: INTEGER!
orderstatus: OrderStatus
totalprice: REAL
}
When a column is declared as an enumeration, it’s a VARCHAR
value but the value is in the value list of the enumerate. We won’t do any check for the value now. We will offer some validations rule to check the value in the future.
Besides, we can use enumeration in SQL.
SELECT * FROM Orders o WHERE o.orderstatus = OrderStatus.FULFILLED
-- sql as above works the same as sql as follows but more semantically
SELECT * FROM Orders o WHERE o.orderstatus = 'F'
-- this will print 'F'
SELECT OrderStatus.FULFILLED