Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 24, 2014 23:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/9751463 to your computer and use it in GitHub Desktop.
Save bennadel/9751463 to your computer and use it in GitHub Desktop.
Ask Ben: Converting A Query To A Struct
<cfset strCellValue = qData[ COLUMN_NAME ][ ROW_INDEX ] />
<cffunction name="QueryToStruct" access="public" returntype="any" output="false"
hint="Converts an entire query or the given record to a struct. This might return a structure (single record) or an array of structures.">
<!--- Define arguments. --->
<cfargument name="Query" type="query" required="true" />
<cfargument name="Row" type="numeric" required="false" default="0" />
<cfscript>
// Define the local scope.
var LOCAL = StructNew();
// Determine the indexes that we will need to loop over.
// To do so, check to see if we are working with a given row,
// or the whole record set.
if (ARGUMENTS.Row){
// We are only looping over one row.
LOCAL.FromIndex = ARGUMENTS.Row;
LOCAL.ToIndex = ARGUMENTS.Row;
} else {
// We are looping over the entire query.
LOCAL.FromIndex = 1;
LOCAL.ToIndex = ARGUMENTS.Query.RecordCount;
}
// Get the list of columns as an array and the column count.
LOCAL.Columns = ListToArray( ARGUMENTS.Query.ColumnList );
LOCAL.ColumnCount = ArrayLen( LOCAL.Columns );
// Create an array to keep all the objects.
LOCAL.DataArray = ArrayNew( 1 );
// Loop over the rows to create a structure for each row.
for (LOCAL.RowIndex = LOCAL.FromIndex ; LOCAL.RowIndex LTE LOCAL.ToIndex ; LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
// Create a new structure for this row.
ArrayAppend( LOCAL.DataArray, StructNew() );
// Get the index of the current data array object.
LOCAL.DataArrayIndex = ArrayLen( LOCAL.DataArray );
// Loop over the columns to set the structure values.
for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE LOCAL.ColumnCount ; LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
// Get the column value.
LOCAL.ColumnName = LOCAL.Columns[ LOCAL.ColumnIndex ];
// Set column value into the structure.
LOCAL.DataArray[ LOCAL.DataArrayIndex ][ LOCAL.ColumnName ] = ARGUMENTS.Query[ LOCAL.ColumnName ][ LOCAL.RowIndex ];
}
}
// At this point, we have an array of structure objects that
// represent the rows in the query over the indexes that we
// wanted to convert. If we did not want to convert a specific
// record, return the array. If we wanted to convert a single
// row, then return the just that STRUCTURE, not the array.
if (ARGUMENTS.Row){
// Return the first array item.
return( LOCAL.DataArray[ 1 ] );
} else {
// Return the entire array.
return( LOCAL.DataArray );
}
</cfscript>
</cffunction>
<!--- Convert the entire query to an array of structures. --->
<cfset arrGirls = QueryToStruct( qGirls ) />
<!--- Convert the second record to a structure. --->
<cfset objGirl = QueryToStruct( qGirls, 2 ) />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment