Skip to content

Commit

Permalink
add parallelization to queryToCSV
Browse files Browse the repository at this point in the history
  • Loading branch information
cfsimplicity committed Oct 31, 2023
2 parents f6128fb + 6093c1f commit f98e62a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Spreadsheet.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1092,8 +1092,9 @@ component accessors="true"{
return new( sheetName=arguments.sheetName, xmlFormat=true );
}

public string function queryToCsv( required query query, boolean includeHeaderRow=false, string delimiter="," ){
var data = getCsvHelper().queryToArrayForCsv( arguments.query, arguments.includeHeaderRow );
/* row order is not guaranteed if using more than one thread */
public string function queryToCsv( required query query, boolean includeHeaderRow=false, string delimiter=",", numeric threads=1 ){
var data = getCsvHelper().queryToArrayForCsv( arguments.query, arguments.includeHeaderRow, arguments.threads );
var builder = getStringHelper().newJavaStringBuilder();
var csvFormat = getCsvHelper().delimiterIsTab( arguments.delimiter )?
createJavaObject( "org.apache.commons.csv.CSVFormat" )[ JavaCast( "string", "TDF" ) ]
Expand Down
11 changes: 6 additions & 5 deletions helpers/csv.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ component extends="base"{
return result;
}

array function queryToArrayForCsv( required query query, required boolean includeHeaderRow ){
/* row order is not guaranteed if using more than one thread */
array function queryToArrayForCsv( required query query, required boolean includeHeaderRow, numeric threads=1){
var result = [];
var columns = getQueryHelper()._QueryColumnArray( arguments.query );
if( arguments.includeHeaderRow )
result.Append( columns );
for( var row IN arguments.query ){
queryEach(arguments.query, function(row){
var rowValues = [];
for( var column IN columns ){
arrayEach(columns, function(column){
var cellValue = row[ column ];
if( getDateHelper().isDateObject( cellValue ) )
cellValue = DateTimeFormat( cellValue, library().getDateFormats().DATETIME );
if( IsValid( "integer", cellValue ) )
cellValue = JavaCast( "string", cellValue );// prevent CSV writer converting 1 to 1.0
rowValues.Append( cellValue );
}
});
result.Append( rowValues );
}
}, arguments.threads gt 1, arguments.threads)
return result;
}

Expand Down

0 comments on commit f98e62a

Please sign in to comment.