Last active
September 22, 2015 12:19
-
-
Save jolle-c/bec94c372d8bd74c5477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**! | |
export_csv | |
Will export all rows from the given table as a csv file. First row will be the column names. | |
Depends on Ke Carltons DS found here | |
https://github.com/zeroloop/ds | |
Some advantages | |
It does not rely on creating a temporary file on the server. | |
Will send accumulated result to the browser ongoing while looping thru the resultset and thus minimize the memory need on the server. | |
There’s probably no limit on how many records it can export. | |
It is probably reasonably fast | |
EXAMPLE | |
export_csv( | |
ds(::mysql), | |
'help_keyword', | |
'demo.csv', | |
10 | |
) | |
*/ | |
define export_csv( | |
ds::ds, | |
table::string, | |
filename::string, | |
breakpoint::integer // this would be set to a really large number, like 20000 | |
) => { | |
local( | |
presentcount = 1, | |
_table = #table -> encodesql, | |
rowcount = #ds -> sql("SELECT COUNT(*) AS found FROM " + #_table, 1) -> firstrow -> find('found'), | |
exportrows = #ds -> sql("SELECT * FROM " + #_table + " LIMIT 0," + #breakpoint, #breakpoint) -> rows, | |
colnames = #exportrows -> first -> columns, | |
output = bytes | |
) | |
web_response -> setHeaders(array( | |
'MIME-Version' = '1.0', | |
'Content-Type' = 'application/text', | |
'Content-Disposition' = 'attachment; filename="' + #filename + '"' | |
)) | |
#output -> append('"' + #colnames -> join('","') + '"\n') | |
while(#presentcount <= #rowcount) => { | |
with row in #exportrows | |
let a_row = ('"' + | |
(with col in #colnames select( | |
#row -> find(#col) | |
)) -> join( '","') + '"\n') -> asbytes | |
do { | |
if(#output -> size + #a_row -> size > fcgi_bodyChunkSize) => { | |
web_response -> rawcontent = #output | |
web_response -> sendChunk | |
#output = bytes | |
} | |
#output -> append(#a_row) | |
} | |
#presentcount += #breakpoint | |
#exportrows = #ds -> sql("SELECT * FROM help_keyword LIMIT " + #presentcount + "," + #breakpoint + "", #breakpoint) -> rows | |
} | |
if(#output -> size) => { | |
web_response -> rawcontent = #output | |
web_response -> sendChunk | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment