1
1
import { ascending , descending , reverse } from "./array.mjs" ;
2
+ import { FileAttachment } from "./fileAttachment.mjs" ;
2
3
3
4
const nChecks = 20 ; // number of values to check in each array
4
5
@@ -141,7 +142,7 @@ function isTypedArray(value) {
141
142
// __query is used by table cells; __query.sql is used by SQL cells.
142
143
export const __query = Object . assign (
143
144
async ( source , operations , invalidation ) => {
144
- source = await source ;
145
+ source = await loadDataSource ( await source , "table" ) ;
145
146
if ( isDatabaseClient ( source ) ) return evaluateQuery ( source , makeQueryTemplate ( operations , source ) , invalidation ) ;
146
147
if ( isDataArray ( source ) ) return __table ( source , operations ) ;
147
148
if ( ! source ) throw new Error ( "missing data source" ) ;
@@ -150,12 +151,31 @@ export const __query = Object.assign(
150
151
{
151
152
sql ( source , invalidation ) {
152
153
return async function ( ) {
153
- return evaluateQuery ( source , arguments , invalidation ) ;
154
+ return evaluateQuery ( await loadDataSource ( await source , "sql" ) , arguments , invalidation ) ;
154
155
} ;
155
156
}
156
157
}
157
158
) ;
158
159
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
+
159
179
async function evaluateQuery ( source , args , invalidation ) {
160
180
if ( ! source ) throw new Error ( "missing data source" ) ;
161
181
0 commit comments