modelRT/sql/data_object_parameter.go

51 lines
1.8 KiB
Go

// Package sql defines reusable database SQL statements.
package sql
const (
// ParameterComponentQueryBase selects the component owning a parameter and
// joins its complete hierarchy so seven-part tokens can be validated as one path.
ParameterComponentQueryBase = `SELECT c.*
FROM component AS c
INNER JOIN station AS s ON s.id = c.station_id
INNER JOIN zone AS z ON z.id = s.zone_id
INNER JOIN grid AS g ON g.id = z.grid_id`
// ParameterSevenPartTokenWhere matches the complete parameter token prefix
// token1.token2.token3.token4.token5.
ParameterSevenPartTokenWhere = `WHERE g.tagname = ?
AND z.tagname = ?
AND s.tagname = ?
AND c.nspath = ?
AND c.tag = ?`
// ParameterFourPartTokenWhere matches token4.token5 and restricts the short
// token form to components belonging to a local station.
ParameterFourPartTokenWhere = `WHERE c.nspath = ?
AND c.tag = ?
AND s.is_local = TRUE`
// ParameterLimitTwo allows callers to distinguish a unique component from
// an ambiguous token without loading every matching row.
ParameterLimitTwo = `LIMIT 2`
// ParameterAttributeColumnType checks that token7 is an actual column of
// the dynamic parameter table and returns its PostgreSQL display type.
ParameterAttributeColumnType = `SELECT pg_catalog.format_type(a.atttypid, a.atttypmod)
FROM pg_catalog.pg_attribute AS a
INNER JOIN pg_catalog.pg_class AS c ON c.oid = a.attrelid
INNER JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
AND c.relname = ?
AND a.attname = ?
AND a.attnum > 0
AND NOT a.attisdropped
LIMIT 1`
// ParameterAttributeDescription returns the display name of token7 from the
// basic attribute metadata table. Two rows are enough to detect ambiguity.
ParameterAttributeDescription = `SELECT attribute_name
FROM basic.attribute
WHERE attribute = ?
LIMIT 2`
)