// Package sql defines reusable database SQL statements package sql const ( // ParameterInitializationRoutes returns the dynamic-table mappings used by // supported parameter attribute groups. ParameterInitializationRoutes = `SELECT name, tag, group_name FROM project_manager WHERE group_name IN ?` // DynamicParameterInitializationRows joins a dynamic parameter table to its // component hierarchy and project_manager route. The table identifier is // inserted only after application-level identifier and allowlist checks. DynamicParameterInitializationRows = `WITH dynamic_rows AS ( SELECT dynamic_record.*, COUNT(*) OVER ( PARTITION BY dynamic_record.global_uuid, dynamic_record.attribute_group ) AS initialization_record_count FROM public.%[1]s AS dynamic_record ) SELECT grid.tagname AS grid_tag, zone.tagname AS zone_tag, station.tagname AS station_tag, station.is_local AS station_is_local, component.global_uuid::text AS component_uuid, component.nspath AS component_nspath, component.tag AS component_tag, project.group_name AS attribute_group, attribute.key AS attribute_name, attribute.value::text AS attribute_value, UPPER(pg_catalog.format_type(column_attribute.atttypid, column_attribute.atttypmod)) AS attribute_type, attribute_description.description, attribute_description.description_count, dynamic_row.initialization_record_count AS dynamic_record_count FROM public.grid AS grid INNER JOIN public.zone AS zone ON zone.grid_id = grid.id INNER JOIN public.station AS station ON station.zone_id = zone.id INNER JOIN public.component AS component ON component.station_id = station.id INNER JOIN public.project_manager AS project ON project.tag = component.model_name INNER JOIN dynamic_rows AS dynamic_row ON dynamic_row.global_uuid = component.global_uuid AND dynamic_row.attribute_group = project.group_name CROSS JOIN LATERAL jsonb_each( to_jsonb(dynamic_row) - 'id' - 'global_uuid' - 'attribute_group' - 'initialization_record_count' ) AS attribute INNER JOIN pg_catalog.pg_namespace AS table_namespace ON table_namespace.nspname = 'public' INNER JOIN pg_catalog.pg_class AS parameter_table ON parameter_table.relnamespace = table_namespace.oid AND parameter_table.relname = project.name INNER JOIN pg_catalog.pg_attribute AS column_attribute ON column_attribute.attrelid = parameter_table.oid AND column_attribute.attname = attribute.key AND column_attribute.attnum > 0 AND NOT column_attribute.attisdropped LEFT JOIN LATERAL ( SELECT MIN(basic_attribute.attribute_name) AS description, COUNT(*) AS description_count FROM basic.attribute AS basic_attribute WHERE basic_attribute.attribute = attribute.key ) AS attribute_description ON TRUE WHERE project.name = ? AND project.tag = ? AND project.group_name = ? AND grid.tagname <> '' AND zone.tagname <> '' AND station.tagname <> '' AND component.nspath <> '' AND component.tag <> ''` // ComponentParameterInitializationRows expands the component table into one // row per queryable component attribute while retaining the full hierarchy. ComponentParameterInitializationRows = `SELECT grid.tagname AS grid_tag, zone.tagname AS zone_tag, station.tagname AS station_tag, station.is_local AS station_is_local, component.global_uuid::text AS component_uuid, component.nspath AS component_nspath, component.tag AS component_tag, 'component' AS attribute_group, attribute.key AS attribute_name, attribute.value::text AS attribute_value, UPPER(pg_catalog.format_type(column_attribute.atttypid, column_attribute.atttypmod)) AS attribute_type, attribute_description.description, attribute_description.description_count, 1::bigint AS dynamic_record_count FROM public.grid AS grid INNER JOIN public.zone AS zone ON zone.grid_id = grid.id INNER JOIN public.station AS station ON station.zone_id = zone.id INNER JOIN public.component AS component ON component.station_id = station.id CROSS JOIN LATERAL jsonb_each( to_jsonb(component) - 'station_id' ) AS attribute INNER JOIN pg_catalog.pg_namespace AS table_namespace ON table_namespace.nspname = 'public' INNER JOIN pg_catalog.pg_class AS component_table ON component_table.relnamespace = table_namespace.oid AND component_table.relname = 'component' INNER JOIN pg_catalog.pg_attribute AS column_attribute ON column_attribute.attrelid = component_table.oid AND column_attribute.attname = attribute.key AND column_attribute.attnum > 0 AND NOT column_attribute.attisdropped LEFT JOIN LATERAL ( SELECT MIN(basic_attribute.attribute_name) AS description, COUNT(*) AS description_count FROM basic.attribute AS basic_attribute WHERE basic_attribute.attribute = attribute.key ) AS attribute_description ON TRUE WHERE grid.tagname <> '' AND zone.tagname <> '' AND station.tagname <> '' AND component.nspath <> '' AND component.tag <> ''` )