growing_buffer* group_buf = buffer_init(128);
growing_buffer* having_buf = buffer_init(128);
+ int aggregate_found = 0; // boolean
+
// Build a select list
if(from_function) // From a function we select everything
OSRF_BUFFER_ADD_CHAR( select_buf, '*' );
// Now build the actual select list
int sel_pos = 1;
- jsonObject* is_agg = jsonObjectFindPath(selhash, "//aggregate");
+ //jsonObject* is_agg = jsonObjectFindPath(selhash, "//aggregate");
first = 1;
gfirst = 1;
jsonIterator* selclass_itr = jsonNewIterator( selhash );
"Selected class is not defined"
);
jsonIteratorFree( selclass_itr );
- jsonObjectFree( is_agg );
+ //jsonObjectFree( is_agg );
buffer_free( sql_buf );
buffer_free( select_buf );
buffer_free( order_buf );
buffer_free( group_buf );
buffer_free( having_buf );
+ if( defaultselhash ) jsonObjectFree( defaultselhash );
free( core_class );
return NULL;
}
"Selected class not in FROM clause in JSON query"
);
jsonIteratorFree( selclass_itr );
- jsonObjectFree( is_agg );
+ //jsonObjectFree( is_agg );
buffer_free( sql_buf );
buffer_free( select_buf );
buffer_free( order_buf );
buffer_free( group_buf );
buffer_free( having_buf );
+ if( defaultselhash ) jsonObjectFree( defaultselhash );
free( core_class );
return NULL;
}
);
jsonIteratorFree( select_itr );
jsonIteratorFree( selclass_itr );
- jsonObjectFree( is_agg );
+ //jsonObjectFree( is_agg );
buffer_free( sql_buf );
buffer_free( select_buf );
buffer_free( order_buf );
buffer_free( group_buf );
buffer_free( having_buf );
+ if( defaultselhash ) jsonObjectFree( defaultselhash );
free( core_class );
return NULL;
}
);
jsonIteratorFree( select_itr );
jsonIteratorFree( selclass_itr );
- jsonObjectFree( is_agg );
+ //jsonObjectFree( is_agg );
buffer_free( sql_buf );
buffer_free( select_buf );
buffer_free( order_buf );
buffer_free( group_buf );
buffer_free( having_buf );
+ if( defaultselhash ) jsonObjectFree( defaultselhash );
free( core_class );
return NULL;
}
);
jsonIteratorFree( select_itr );
jsonIteratorFree( selclass_itr );
- jsonObjectFree( is_agg );
+ //jsonObjectFree( is_agg );
buffer_free( sql_buf );
buffer_free( select_buf );
buffer_free( order_buf );
buffer_free( group_buf );
buffer_free( having_buf );
+ if( defaultselhash ) jsonObjectFree( defaultselhash );
free( core_class );
return NULL;
}
+ const jsonObject* agg_obj = jsonObjectGetKey( selfield, "aggregate" );
+ if( agg_obj )
+ aggregate_found = 1;
+
+ if( ( ! agg_obj ) || ( ! obj_is_true( agg_obj ) ) ) {
+ // Append a comma (except for the first one)
+ // and add the column to a GROUP BY clause
+ if (gfirst)
+ gfirst = 0;
+ else
+ OSRF_BUFFER_ADD_CHAR( group_buf, ',' );
+
+ buffer_fadd(group_buf, " %d", sel_pos);
+ }
+
+#if 0
if (is_agg->size || (flags & SELECT_DISTINCT)) {
const jsonObject* aggregate_obj = jsonObjectGetKey( selfield, "aggregate" );
}
buffer_fadd(group_buf, " %d", sel_pos);
+
/*
} else if (is_agg = jsonObjectGetKey( selfield, "having" )) {
if (gfirst) {
*/
}
}
+#endif
sel_pos++;
} // end while -- iterating across SELECT columns
jsonIteratorFree(selclass_itr);
- if (is_agg) jsonObjectFree(is_agg);
+ //if (is_agg) jsonObjectFree(is_agg);
}
string = buffer_release(group_buf);
- if ( *string ) {
+ if ( *string && ( aggregate_found || (flags & SELECT_DISTINCT) ) ) {
OSRF_BUFFER_ADD( sql_buf, " GROUP BY " );
OSRF_BUFFER_ADD( sql_buf, string );
}