Skip to content

Commit

Permalink
Add support for reading CSV files (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeflateAwning authored Feb 14, 2025
1 parent 95eae4c commit 921940d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
},
{
"filenamePattern": "*.pq"
},
{
"filenamePattern": "*.csv"
}
],
"priority": "default"
Expand Down
29 changes: 21 additions & 8 deletions src/parquetDocument.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as vscode from 'vscode';
import { Disposable } from './dispose';
import { getNonce } from './util';
import { parse } from "path"

import { parse, extname } from "path"
import * as duckdb from 'duckdb';

const CSV_EXTENSIONS = [".csv"];
const PARQUET_EXTENSIONS = [".pq", ".parq", ".parquet"];

type IMessage = {
type: 'query' | 'more';
success: boolean;
Expand All @@ -14,7 +16,7 @@ type IMessage = {
} | { type: 'config', autoQuery: boolean };

/**
* Define the document (the data model) used for paw draw files.
* Define the document (the data model) used for table files.
*/
class ParquetDocument extends Disposable implements vscode.CustomDocument {

Expand All @@ -35,14 +37,25 @@ class ParquetDocument extends Disposable implements vscode.CustomDocument {
this._uri = uri;
this._db = new duckdb.Database(':memory:');

const config = vscode.workspace.getConfiguration('flat-file-explorer')
const config = vscode.workspace.getConfiguration('flat-file-explorer');
let tableName: string = config.get("tableName")!;
if (config.get("useFileNameAsTableName"))
tableName = parse(uri.fsPath).name
tableName = parse(uri.fsPath).name;

const fileExtension = extname(uri.fsPath).toLowerCase();
let query = "";

if (CSV_EXTENSIONS.includes(fileExtension)) {
query = `CREATE VIEW ${tableName} AS SELECT * FROM read_csv('${uri.fsPath}');`;
} else if (PARQUET_EXTENSIONS.includes(fileExtension)) {
query = `CREATE VIEW ${tableName} AS SELECT * FROM read_parquet('${uri.fsPath}');`;
} else {
// If this error occurs, check that the trigger types in `package.json` are in sync
// with the extension definition arrays at the top of this file.
throw new Error("Unsupported file type. Should not have opened with Flat File Explorer.");
}

this.db.exec(
`CREATE VIEW ${tableName} AS SELECT * FROM read_parquet('${uri.fsPath}');`
);
this.db.exec(query);
}

public get uri() { return this._uri; }
Expand Down

0 comments on commit 921940d

Please sign in to comment.