modelRT/sql/data_object_measurement.go

83 lines
3.6 KiB
Go

// Package sql defines reusable database SQL statements.
package sql
const (
// MeasurementCountSelect selects the number of measurements matching a
// token hierarchy and is used during token existence validation.
MeasurementCountSelect = "SELECT COUNT(*)"
// MeasurementRowsSelect selects complete measurement rows after a token has
// been parsed into its hierarchy conditions.
MeasurementRowsSelect = "SELECT m.*"
// MeasurementLimitTwo limits token resolution to two rows so callers can
// distinguish a unique match from an ambiguous token without loading all matches.
MeasurementLimitTwo = "LIMIT 2"
// MeasurementIDWhere is the GORM condition used to query a measurement by
// its database primary key.
MeasurementIDWhere = "id = ?"
// MeasurementTokenValidationQueryBase contains the common hierarchy joins
// used by all supported measurement token formats.
MeasurementTokenValidationQueryBase = MeasurementCountSelect + `
FROM measurement AS m
INNER JOIN component AS c ON c.global_uuid = m.component_uuid
INNER JOIN bay AS b ON b.bay_uuid = m.bay_uuid
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`
// MeasurementSevenPartTokenWhere matches a complete token in the form
// token1.token2.token3.token4.token5.token6.token7. Token6 is validated as
// "bay" before this condition is used.
MeasurementSevenPartTokenWhere = `WHERE g.tagname = ?
AND z.tagname = ? AND s.tagname = ?
AND c.nspath = ? AND c.tag = ?
AND m.tag = ?`
// MeasurementFourPartTokenWhere matches a local measurement token in the
// form token4.token5.token6.token7. Token6 is validated as "bay" before use.
MeasurementFourPartTokenWhere = `WHERE c.nspath = ? AND c.tag = ?
AND m.tag = ?`
// MeasurementTwoPartTokenWhere matches the short measurement token format
// token4.token7 using component namespace path and measurement tag.
MeasurementTwoPartTokenWhere = `WHERE c.nspath = ? AND m.tag = ?`
// MeasurementComponentByUUID returns the component hierarchy fields needed
// to construct a measurement's canonical name and seven-part ID.
MeasurementComponentByUUID = `SELECT global_uuid, nspath,
tag, grid, zone, station FROM component
WHERE global_uuid = ? LIMIT 1`
// MeasurementGridTags returns every grid tag used to construct the first
// level of the measurement recommendation hierarchy.
MeasurementGridTags = `SELECT tagname FROM grid`
// MeasurementZoneHierarchy returns zones together with their parent grid
// tags for building the grid-to-zone recommendation mapping.
MeasurementZoneHierarchy = `SELECT zone.*,
grid.tagname AS grid_tag FROM zone
LEFT JOIN grid ON zone.grid_id = grid.id`
// MeasurementStationHierarchy returns stations together with their parent
// zone tags for building the zone-to-station recommendation mapping.
MeasurementStationHierarchy = `SELECT station.*, zone.tagname AS zone_tag
FROM station
LEFT JOIN zone ON station.zone_id = zone.id`
// MeasurementComponentHierarchy returns components together with their
// parent station tags for building station, namespace, and component mappings.
MeasurementComponentHierarchy = `SELECT component.*,
station.tagname AS station_tag
FROM component LEFT JOIN station
ON component.station_id = station.id`
// MeasurementTagHierarchy returns measurements together with their owning
// component tags for building the component-to-measurement mapping.
MeasurementTagHierarchy = `SELECT measurement.*, component.tag AS comp_tag
FROM measurement LEFT JOIN component
ON measurement.component_uuid = component.global_uuid`
)