The get_worksheet_data function is a PostgreSQL PL/pgSQL function that dynamically generates SQL queries to extract worksheet data for reporting purposes. It handles multiple worksheet types, field types, and data structures including both root-level fields and DataGrid components.
CREATE OR REPLACE FUNCTION "Reporting".get_worksheet_data(
correlation_id uuid,
report_map_id uuid
)
RETURNS text
LANGUAGE plpgsql- Generates dynamic SQL queries based on report column mappings
- Extracts worksheet data from JSONB structures
- Handles complex field types (DataGrids, Checkbox Groups, Radio Groups)
- Supports multiple worksheets and field configurations in a single query
- Returns a formatted SQL query string that can be executed to retrieve reporting data
- correlation_id: UUID identifying the worksheet instance correlation
- report_map_id: UUID identifying the report column mapping configuration
Returns a TEXT string containing a complete SQL query that can be executed to retrieve worksheet data formatted for reporting.
The function queries the following tables:
"Reporting"."ReportColumnsMaps": Contains column mapping configurations"Flex"."WorksheetInstances": Contains worksheet instance data"Flex"."Worksheets": Contains worksheet metadata
Extracts column mapping information from the ReportColumnsMaps table:
- ColumnName: Report column identifier
- Type: Field type (checkbox, radio, text, etc.)
- DataPath: Path to data in JSON structure
- TypePath: Hierarchical type information
- Path: Field path within worksheet structure
- Uses
DISTINCT ON (column_name)to ensure each column gets only one mapping - Extracts worksheet name and data grid information
- Splits DataPath to identify DataGrid and field names
DataGrid Fields (unique_worksheet_datagrids CTE):
- Identifies fields within DataGrid components
- Groups by worksheet and DataGrid name
Root Level Fields (unique_worksheets_with_root CTE):
- Identifies fields at the worksheet root level (non-DataGrid)
Generates queries for DataGrid field extraction:
- Iterates through DataGrid rows using
jsonb_array_elements - Creates row identifiers:
{datagrid_name}_r{row_number} - Handles all column types with appropriate DataGrid cell extraction
Generates queries for root-level field extraction:
- Single row per worksheet with identifier:
'root' - Extracts values directly from worksheet CurrentValue structure
- Text: Direct string extraction
- Currency: Validates numeric format, converts to DECIMAL(10,2)
- Number: Validates numeric format, converts to NUMERIC
- Date: Validates and converts to TIMESTAMP
Data Structure: {"key": "Field9", "value": "true"}
Processing: Converts text values to boolean using predefined value lists
- True values: 'true', 't', '1', 'yes', 'on'
- False values: 'false', 'f', '0', 'no', 'off', ''
Data Structure: {"key": "Field10", "value": "[{\"key\":\"check1\",\"value\":false},{\"key\":\"check2\",\"value\":true}]"}
Processing:
- Parses JSON array from stored string
- Extracts individual checkbox values by key
- Returns boolean values for each checkbox option
Data Structure: {"key": "Field12", "value": "Radio1"}
Processing: Returns the selected option value as text (not boolean)
The final output is a UNION query combining:
- All DataGrid queries (one per worksheet-DataGrid combination)
- All root field queries (one per worksheet with root fields)
- worksheet_instance_id: Worksheet instance identifier
- application_id: Correlation ID
- worksheet_name: Name of the worksheet
- row_identifier: Row identifier ('root' for root fields, '{datagrid}_r{n}' for DataGrid rows)
- Dynamic columns: All mapped report columns with appropriate data types
- NULL handling for missing or invalid data
- Type conversion with fallback to NULL for invalid formats
- JSON parsing errors gracefully handled
- Missing worksheet or field data returns NULL values
- Standard:
"(WorksheetName)FieldName" - DataGrid:
"(WorksheetName)DataGridName->FieldName" - Checkbox Group:
"(WorksheetName)FieldName->OptionName"
- Root Field:
"worksheet->section->fieldtype" - DataGrid Field:
"worksheet->section->datagrid->fieldtype" - Checkbox Group:
"worksheet->section->checkboxgroup->Checkbox"
| Field Type | SQL Type | NULL Type |
|---|---|---|
| Currency | DECIMAL(10,2) | NULL::DECIMAL(10,2) |
| Number | NUMERIC | NULL::NUMERIC |
| Date | TIMESTAMP | NULL::TIMESTAMP |
| Checkbox | BOOLEAN | NULL::BOOLEAN |
| Radio | TEXT | NULL::TEXT |
| Default | TEXT | NULL::TEXT |
- Uses DISTINCT ON for deduplication
- Leverages JSONB operators for efficient JSON parsing
- Generates optimized column lists with appropriate NULL handling
- Orders results by worksheet_name and row_identifier
- Generates one query with 'root' row identifier
- All fields extracted from CurrentValue->values array
- Generates one query per DataGrid
- Multiple rows per DataGrid based on data
- Row identifiers: DataGridName_r1, DataGridName_r2, etc.
- Generates separate queries for each worksheet-DataGrid combination
- Generates separate queries for worksheets with root fields
- All combined with UNION ALL
- Checkbox groups parsed from JSON arrays
- Radio fields return actual selected values
- Regular checkboxes converted to boolean
- Missing Data: Check if TypePath correctly identifies field location
- Wrong Data Type: Verify column_type matching in mapping
- JSON Parse Errors: Check DataPath format and clean_data_path extraction
- Duplicate Columns: Ensure DISTINCT ON is working with proper ordering
-- Check mapping data
SELECT * FROM "Reporting"."ReportColumnsMaps" WHERE "Id" = 'your-report-map-id';
-- Check worksheet instances
SELECT * FROM "Flex"."WorksheetInstances"
WHERE "WorksheetCorrelationId" = 'your-correlation-id';
-- Test function output
SELECT "Reporting".get_worksheet_data('correlation-id', 'report-map-id');- v1.0: Initial implementation with basic field types
- v1.1: Added DataGrid support
- v1.2: Added checkbox group support
- v1.3: Fixed radio field handling to return text values
- v1.4: Enhanced error handling and NULL type consistency