final class CanonicalZoneSql (View source)

Resolves which zones row a Poweradmin zone ID refers to.

A zone ID reaching the infrastructure layer is zones.domain_id on installs migrated from SQL mode and zones.id on zones this application created, so both have to be matched. The two id spaces overlap, so a single ID can match one row by id and a different row by domain_id - picking the wrong one updates the wrong zone. Every resolver must therefore agree on the same preference, which is why this lives in one place.

Known limit: an extra-ownership row (NULL zone_name) is keyed only by canonical id, so under that collision no query can tell which of the two zones it belongs to. The ownership checks therefore ignore such rows for a shared id (SharedZoneIds).

Methods

static string
canonicalIdColumn(string $alias, bool $rowIdFallback)

SQL expression for a zones row's canonical id, the value API mode hands to callers.

static array
zoneNameJoin(bool $zonesTableIsCanonical, string $zoneIdColumn, string $domainsTable)

JOIN onto the table that names a zone, for a query keyed on $zoneIdColumn.

static string
selectByZoneId(string $columns)

SELECT that resolves a zone ID to exactly one zones row.

static void
bindZoneId(PDOStatement $stmt, int $zoneId)

Bind every placeholder selectByZoneId() declares. PDO will not reuse one named placeholder across positions, hence the repetition.

Details

static string canonicalIdColumn(string $alias, bool $rowIdFallback)

SQL expression for a zones row's canonical id, the value API mode hands to callers.

The PHP form of this rule is domain_id ?: id, which treats 0 as absent. A bare COALESCE does not: it skips NULL only, so a row stranded at domain_id = 0 resolves to 0 rather than to its own id. NULLIF folds that 0 into NULL first, which is what keeps this expression in step with the PHP rule.

The fallback to id belongs to the API backend, where zones is the source of truth (BackendCapabilitiesInterface::allocatesZoneIdsLocally()). In SQL mode domain_id is a foreign key into domains and always populated, and zones.id is an unrelated id space, so callers there pass false and get the bare column; the fallback would point at another zone.

Bind ids compared against this with PDO::PARAM_INT. An expression carries none of the column's type affinity, so SQLite compares a string-bound id as text and matches nothing, where the bare column would have coerced it.

Parameters

string $alias

Table alias or name without the dot, e.g. 'z' or 'zones'; '' for none

bool $rowIdFallback

Whether zones.id may stand in for a missing domain_id (API backend)

Return Value

string

static array zoneNameJoin(bool $zonesTableIsCanonical, string $zoneIdColumn, string $domainsTable)

JOIN onto the table that names a zone, for a query keyed on $zoneIdColumn.

When the zones table is the source of truth (BackendCapabilitiesInterface::allocatesZoneIdsLocally()) it carries zone_name; otherwise the name lives in the PowerDNS domains table, which the caller resolves through TableNameService.

Parameters

bool $zonesTableIsCanonical

Whether zone ids are allocated from the zones table

string $zoneIdColumn

Qualified zone id column of the driving table, e.g. 'log_zones.zone_id'

string $domainsTable

Resolved PowerDNS domains table name

Return Value

array

The JOIN clause and the qualified name column

static string selectByZoneId(string $columns)

SELECT that resolves a zone ID to exactly one zones row.

Placeholder ownership rows (zone_name IS NULL) never win. Among real rows the order is: a row whose id and domain_id both match, then a domain_id match, then an id match. domain_id outranks id because that is the identifier API mode hands to callers - see ApiDnsBackendProvider::getZones(), which emits domain_id ?: id.

Bind with bindZoneId(). The caller supplies the column list it needs.

Parameters

string $columns

SELECT-list columns, e.g. "id, zone_name, zone_type"

Return Value

string

static void bindZoneId(PDOStatement $stmt, int $zoneId)

Bind every placeholder selectByZoneId() declares. PDO will not reuse one named placeholder across positions, hence the repetition.

Parameters

PDOStatement $stmt
int $zoneId

Return Value

void