11import { ascending , descending , reverse } from "./array.mjs" ;
2+ import { FileAttachment } from "./fileAttachment.mjs" ;
23
34const nChecks = 20 ; // number of values to check in each array
45
@@ -141,7 +142,7 @@ function isTypedArray(value) {
141142// __query is used by table cells; __query.sql is used by SQL cells.
142143export const __query = Object . assign (
143144 async ( source , operations , invalidation ) => {
144- source = await source ;
145+ source = await loadDataSource ( await source , "table" ) ;
145146 if ( isDatabaseClient ( source ) ) return evaluateQuery ( source , makeQueryTemplate ( operations , source ) , invalidation ) ;
146147 if ( isDataArray ( source ) ) return __table ( source , operations ) ;
147148 if ( ! source ) throw new Error ( "missing data source" ) ;
@@ -150,12 +151,31 @@ export const __query = Object.assign(
150151 {
151152 sql ( source , invalidation ) {
152153 return async function ( ) {
153- return evaluateQuery ( source , arguments , invalidation ) ;
154+ return evaluateQuery ( await loadDataSource ( await source , "sql" ) , arguments , invalidation ) ;
154155 } ;
155156 }
156157 }
157158) ;
158159
160+ export async function loadDataSource ( source , mode ) {
161+ if ( source instanceof FileAttachment ) {
162+ if ( mode === "table" ) {
163+ switch ( source . mimeType ) {
164+ case "text/csv" : return source . csv ( { typed : true } ) ;
165+ case "text/tab-separated-values" : return source . tsv ( { typed : true } ) ;
166+ case "application/json" : return source . json ( ) ;
167+ }
168+ }
169+ if ( mode === "table" || mode === "sql" ) {
170+ switch ( source . mimeType ) {
171+ case "application/x-sqlite3" : return source . sqlite ( ) ;
172+ }
173+ }
174+ throw new Error ( `unsupported file type: ${ source . mimeType } ` ) ;
175+ }
176+ return source ;
177+ }
178+
159179async function evaluateQuery ( source , args , invalidation ) {
160180 if ( ! source ) throw new Error ( "missing data source" ) ;
161181
0 commit comments