Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 15x 15x 8x 6x 2x 3x 3x 15x 2x 2x 1x 1x 1x 2x 2x 15x 2x 2x 1x 1x 1x 15x | import { stringify } from '@metamask/kernel-utils';
import { hasProperty } from '@metamask/utils';
import { useCallback } from 'react';
import { usePanelContext } from '../context/PanelContext.tsx';
/**
* Hook for database actions.
*
* @returns Database methods.
*/
export function useDatabase(): {
fetchTables: () => Promise<string[]>;
fetchTableData: (tableName: string) => Promise<Record<string, string>[]>;
executeQuery: (sql: string) => Promise<Record<string, string>[]>;
} {
const { callKernelMethod, logMessage } = usePanelContext();
// Execute a query and set the result as table data
const executeQuery = useCallback(
async (sql: string): Promise<Record<string, string>[]> => {
const result = await callKernelMethod({
method: 'executeDBQuery',
params: { sql },
});
if (hasProperty(result, 'error')) {
throw new Error(stringify(result.error, 0));
}
logMessage(stringify(result, 0), 'received');
return result;
},
[logMessage, callKernelMethod],
);
// Fetch available tables
const fetchTables = useCallback(async (): Promise<string[]> => {
const result = await callKernelMethod({
method: 'executeDBQuery',
params: { sql: "SELECT name FROM sqlite_master WHERE type='table'" },
});
if (hasProperty(result, 'error')) {
throw new Error(stringify(result.error, 0));
}
logMessage(stringify(result, 0), 'received');
return result
.map((row: Record<string, string>) => row.name)
.filter((name): name is string => name !== undefined);
}, [logMessage, callKernelMethod]);
// Fetch data for selected table
const fetchTableData = useCallback(
async (tableName: string): Promise<Record<string, string>[]> => {
const result = await callKernelMethod({
method: 'executeDBQuery',
params: { sql: `SELECT * FROM ${tableName}` },
});
if (hasProperty(result, 'error')) {
throw new Error(stringify(result.error, 0));
}
logMessage(stringify(result, 0), 'received');
return result;
},
[logMessage, callKernelMethod],
);
return {
fetchTables,
fetchTableData,
executeQuery,
};
}
|