22
33import org .apache .log4j .Logger ;
44import org .gusdb .fgputil .Timer ;
5- import org .gusdb .fgputil .db .platform .SupportedPlatform ;
65import org .gusdb .fgputil .validation .ValidObjectFactory .RunnableObj ;
76import org .gusdb .wdk .model .AttributeMetaQueryHandler ;
87import org .gusdb .wdk .model .WdkModel ;
@@ -318,16 +317,21 @@ public void resolveReferences(WdkModel wdkModel) throws WdkModelException {
318317 // IMPORTANT: Must populate macro BEFORE super.resolveReferences() which validates macros
319318 if (_attributeMetaQueryRef != null ) {
320319 Timer timer = new Timer ();
320+ List <String > metaColumnDefs = new ArrayList <>();
321321 for (Map <String ,Object > row : getDynamicallyDefinedAttributes (_attributeMetaQueryRef , wdkModel )) {
322322 Column column = new Column ();
323323 // Need to set this here since this column originates from the database
324324 column .setQuery (SqlQuery .this );
325325 AttributeMetaQueryHandler .populate (column , row );
326326 _columnMap .put (column .getName (), column );
327+ metaColumnDefs .add (column .getName () + " " + column .getType ().getPostgresType ());
327328 }
328-
329- populateCrosstabMacroWithDiscoveredColumns (wdkModel );
330-
329+ // Populate the crosstab macro with ordered column definitions from the meta query
330+ // If no columns returned, use a placeholder to prevent SQL parse errors
331+ String macroValue = metaColumnDefs .isEmpty () ?
332+ "_no_columns_defined text" :
333+ String .join (", " , metaColumnDefs );
334+ addSqlParamValue (META_ATTRIBUTE_COLUMNS_FOR_CROSSTAB , macroValue );
331335 LOG .debug ("Took " + timer .getElapsedString () + " to resolve AttributeMetaQuery: " + _attributeMetaQueryRef );
332336 }
333337
@@ -342,28 +346,6 @@ public void resolveReferences(WdkModel wdkModel) throws WdkModelException {
342346 }
343347 }
344348
345- private void populateCrosstabMacroWithDiscoveredColumns (WdkModel wdkModel ) throws WdkModelException {
346- // check if this query's SQL contains a crosstab macro that needs to be filled in with columns discovered by the meta query
347- if (_sql .contains (META_ATTRIBUTE_COLUMNS_FOR_CROSSTAB )) {
348-
349- // This macro is only supported on Postgres
350- if (wdkModel .getModelConfig ().getAppDB ().getPlatformEnum () != SupportedPlatform .POSTGRESQL ) {
351- throw new WdkModelException ("SQL Query " + getName () + ": Crosstab macro is only supported on Postgres App DBs." );
352- }
353-
354- // Populate the crosstab macro with ordered column definitions from the meta query
355- // If no columns returned, use a placeholder to prevent SQL parse errors
356- List <String > metaColumnDefs = new ArrayList <>();
357- for (Column column : _columnMap .values ()) {
358- metaColumnDefs .add (column .getName () + " " + column .getType ().getPostgresType ());
359- }
360- String macroValue = metaColumnDefs .isEmpty () ?
361- "_no_columns_defined text" :
362- String .join (", " , metaColumnDefs );
363- addSqlParamValue (META_ATTRIBUTE_COLUMNS_FOR_CROSSTAB , macroValue );
364- }
365- }
366-
367349 @ Override
368350 public Map <String , AttributeFieldDataType > resolveColumnTypes () throws WdkModelException {
369351 var types = new LinkedHashMap <String , AttributeFieldDataType >();
0 commit comments