cfsimplicity / spreadsheet-cfml

Standalone library for working with spreadsheets and CSV in CFML
MIT License
127 stars 36 forks source link

queryToCsv with empty query returns incorrectly delimited header row #292

Closed JamoCA closed 2 years ago

JamoCA commented 2 years ago

Using v3.4.4, I used queryToCsv to backup sets of data, but in some cases "no results" are returned.

When exporting using the following settings (using tab as a delimiter):

spreadsheet.queryToCsv(query=myQuery, includeHeaderRow=true, delimiter=chr(9));

The header incorrectly used a period as a delimiter:

COL1.COL2.COL3

... instead of tabs:

COL1    COL2    COL3
cfsimplicity commented 2 years ago

Seems to be working as expected with a basic query with no records:

myQuery = QueryNew( "column1,column2", "VarChar,VarChar", [] );
result = spreadsheet.queryToCsv( query=myQuery, includeHeaderRow=true, delimiter=chr(9) );
writeDump( result );

Outputs

column1 column2

Can you provide any more detail about the query that's causing this issue for you?

JamoCA commented 2 years ago

Hmm... I see that.

Perhaps it's an issue with FileWrite. Try this with the tab-delimited CSV results. (NOTE: I'm using ACF2016.)

filewrite("c:\emptyquery.csv", result, "utf-8");

This converts the tabs to periods. I don't think I've ever seen this before. (I'll test later in ACF2021 and latest Lucee.)

cfsimplicity commented 2 years ago

Getting the same (expected) result in the file using Lucee.

cfsimplicity commented 2 years ago

Getting the same result in the file with each of ACF2016, 2018 and 2021:

myQuery = QueryNew( "column1,column2", "VarChar,VarChar", [] );
result = spreadsheet.queryToCsv( query=myQuery, includeHeaderRow=true, delimiter=chr(9) );
WriteDump( result );
Filewrite( ExpandPath( "emptyquery.csv" ), result, "utf-8");
JamoCA commented 2 years ago

Wait a minute... I opened this file in VSCode (rather than previewing) and see that it is indeed correct.

I use Total Commander (TC) and it has a "lister" app that is pretty good at previewing file contents. When previewed, it rendered the file's contents as "Binary (fixed line length)" rather than "utf-8". (ie, it didn't correctly auto-detect.) After toggling the view mode to "UTF-8", "text only", "HTML text (strip tags)" or "Explorer Preview", the preview looks correct (using tabs instead of periods).

Sorry for not testing this further. I don't usually write text files as UTF-8, but this is a multi-language database table. It appears that "Lister" isn't capable of correctly detecting a single line text file and renders non-printable characters as periods. (I've used TC for ~20 years now and hadn't ever encountered this before today.) Thanks!