Skip to content

Commit

Permalink
refactor: small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vwh committed Nov 8, 2024
1 parent bd56573 commit c3c708d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/components/landing/url-fetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function UrlFetch() {

setDatabaseData({ name: file.name, size: file.size });
await loadDatabaseBytes(bytes);

setFetchError(null);
} catch (error) {
if (!useProxy) {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useQueryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function useQueryData(
}`;
const countResult: QueryExecResult[] = query(countQueryString);
const newTotalRows = countResult[0].values[0][0] as number;

setTotalRows(newTotalRows);

// Main query
Expand Down Expand Up @@ -126,6 +127,7 @@ export function useQueryData(
);
const { data: newData, columns: newColumns } =
mapQueryResults(customResult);

setColumns(newColumns);
setData(newData);
setIsCustomQuery(true);
Expand Down
24 changes: 19 additions & 5 deletions src/lib/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ const SQL_WASM_PATH = "/sql.wasm";
// Initialize SQL.js once
let SQL: Awaited<ReturnType<typeof initSqlJs>>;
const initSQL = async () => {
if (!SQL) {
SQL = await initSqlJs({ locateFile: () => SQL_WASM_PATH });
}
if (!SQL) SQL = await initSqlJs({ locateFile: () => SQL_WASM_PATH });

return SQL;
};

Expand All @@ -18,6 +17,7 @@ export const loadDatabaseBytes = async (
): Promise<Database> => {
try {
const SQL = await initSQL();

return new SQL.Database(bytes);
} catch (error) {
console.error("Failed to load database:", error);
Expand All @@ -31,10 +31,13 @@ export const getTableNames = (database: Database): string[] => {
"SELECT name FROM sqlite_master WHERE type='table';"
);
const names: string[] = [];

while (stmt.step()) {
names.push(stmt.get()[0] as string);
}

stmt.free();

return names;
} catch {
throw new Error("Invalid database file");
Expand All @@ -56,6 +59,7 @@ export const getTableSchema = (database: Database, tableName: string) => {
const tableInfoStmt = database.prepare(
`PRAGMA table_info("${tableName}");`
);

while (tableInfoStmt.step()) {
const row = tableInfoStmt.getAsObject();
tableSchema[row.name as string] = {
Expand All @@ -67,17 +71,19 @@ export const getTableSchema = (database: Database, tableName: string) => {
nullable: row.notnull === 0
};
}

tableInfoStmt.free();

const foreignKeyStmt = database.prepare(
`PRAGMA foreign_key_list("${tableName}");`
);

while (foreignKeyStmt.step()) {
const row = foreignKeyStmt.getAsObject();
if (tableSchema[row.from as string]) {
if (tableSchema[row.from as string])
tableSchema[row.from as string].isForeignKey = true;
}
}

foreignKeyStmt.free();

return tableSchema;
Expand All @@ -99,13 +105,15 @@ export const mapQueryResults = (
const data = values.map((row) =>
Object.fromEntries(columns.map((col, i) => [col, row[i]]))
);

return { data, columns };
};

export const downloadDatabase = (database: Database): void => {
try {
const binaryArray = database.export();
const blob = new Blob([binaryArray], { type: "application/x-sqlite3" });

saveAs(blob, "database.sqlite");
} catch (error) {
console.error("Failed to export database:", error);
Expand All @@ -118,6 +126,7 @@ const arrayToCSV = (columns: string[], rows: any[]): string => {
const csvRows = rows.map((row) =>
columns.map((col) => `"${row[col] ?? ""}"`).join(",")
);

return [header, ...csvRows].join("\n");
};

Expand All @@ -135,6 +144,7 @@ const exportFromQuery = (
const row = stmt.getAsObject();
data.push(row as TableRow);
}

stmt.free();

if (data.length === 0) {
Expand All @@ -143,6 +153,7 @@ const exportFromQuery = (

const csvContent = arrayToCSV(columns, data);
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });

saveAs(blob, `${tableName}.csv`);
} catch (error) {
console.error(`Failed to get CSV for query "${query}":`, error);
Expand All @@ -157,14 +168,17 @@ export const exportTableAsCSV = (
const tableNames = getTableNames(database);
const tableName = tableNames[tableIndex];
const query = `SELECT * FROM "${tableName}"`;

exportFromQuery(query, database, tableName);
};

export const exportAllTablesAsCSV = (database: Database): void => {
const tableNames = getTableNames(database);

for (const tableName of tableNames) {
try {
const query = `SELECT * FROM "${tableName}"`;

exportFromQuery(query, database, tableName);
} catch (error) {
console.error(`Failed to get CSV for table "${tableName}":`, error);
Expand Down

0 comments on commit c3c708d

Please sign in to comment.