Skip to content

Enums and Constants

Constants

Define reusable compiler-validated values and reference them safely from SQL.

Constants give a name to a value used in SQL. SQLBuild validates the value during compilation and asks the active adapter to render it safely for its SQL dialect. Constant values are data, never raw SQL snippets.

Put project-wide constants under the top-level constants/ directory. Files are discovered recursively, and one file may contain more than one declaration.

my_project/
├── constants/
│ ├── commerce/
│ │ └── thresholds.sql
│ └── reporting_day.sql
├── models/
└── sqlbuild_project.toml
-- constants/commerce/thresholds.sql
CONSTANT (name min_items, value 7);
CONSTANT (name fallback_source, value "web");
CONSTANT (name enabled, value true);
CONSTANT (name ratio, value 0.75);
CONSTANT (name missing_value, value null);

Folders below constants/ are organizational. They do not change where project-wide constants are available.

Reference a constant with @const("name"):

SELECT *
FROM order_batches
WHERE item_count >= @const("min_items")
AND source = @const("fallback_source")

References work in model queries, SQL hooks, SQL functions, audits, unit tests, scenarios, and inline source expressions. An unknown constant fails compilation.

Constants support strings, signed integers, booleans, finite floating-point numbers, exact decimals, and null. Integers use a portable signed 64-bit range. NaN and positive or negative infinity are rejected.

Use type decimal with a quoted value when decimal precision must be exact:

CONSTANT (
name usd_rate,
type decimal,
value "2.4700",
);

SQLBuild parses the quoted value directly as a decimal rather than first converting it to a binary float. An incompatible type and value fails compilation.

Public constant names must be unique among public constants and cannot begin with _. Enum, constant, and macro names use separate namespaces, so an enum and a constant may share a name.

To limit a constant to one folder, or to that folder and its child folders, see Declarations and Scopes.