(
query: string,
setQuery: (query: string) => void,
activeFile: any = null,
options: UseQueryExecutionOptions = {}
)
| 47 | * @returns State and functions for query execution and navigation |
| 48 | */ |
| 49 | export const useQueryExecution = ( |
| 50 | query: string, |
| 51 | setQuery: (query: string) => void, |
| 52 | activeFile: any = null, |
| 53 | options: UseQueryExecutionOptions = {} |
| 54 | ): QueryExecutionResult => { |
| 55 | const { |
| 56 | initialRowsPerPage = 100, |
| 57 | largeDatasetThreshold = 10000 |
| 58 | } = options; |
| 59 | |
| 60 | // External store access |
| 61 | const { |
| 62 | executePaginatedQuery, |
| 63 | isLoading: duckDBLoading, |
| 64 | motherDuckConnected, |
| 65 | isMotherDuckQuery |
| 66 | } = useDuckDBStore(); |
| 67 | |
| 68 | const { addRecentQuery } = useAppStore(); |
| 69 | |
| 70 | // Query result state |
| 71 | const [results, setResults] = useState<any[] | null>(null); |
| 72 | const [columns, setColumns] = useState<string[] | null>(null); |
| 73 | const [error, setError] = useState<string | null>(null); |
| 74 | const [executionTime, setExecutionTime] = useState<number | null>(null); |
| 75 | |
| 76 | // Pagination state |
| 77 | const [totalRows, setTotalRows] = useState<number>(0); |
| 78 | const [currentPage, setCurrentPage] = useState<number>(1); |
| 79 | const [totalPages, setTotalPages] = useState<number>(0); |
| 80 | const [rowsPerPage, setRowsPerPage] = useState<number>(initialRowsPerPage); |
| 81 | |
| 82 | // UI state |
| 83 | const [isChangingPage, setIsChangingPage] = useState<boolean>(false); |
| 84 | const [showLargeDataWarning, setShowLargeDataWarning] = useState<boolean>(false); |
| 85 | |
| 86 | /** |
| 87 | * Check if a query will potentially return a large dataset |
| 88 | */ |
| 89 | const isLargeDataQuery = useCallback((sql: string): boolean => { |
| 90 | const normalizedSql = sql.trim().toLowerCase(); |
| 91 | const hasNoLimit = !normalizedSql.includes('limit'); |
| 92 | const hasSelectAll = normalizedSql.includes('select *'); |
| 93 | |
| 94 | return hasSelectAll && hasNoLimit; |
| 95 | }, []); |
| 96 | |
| 97 | /** |
| 98 | * Add a LIMIT clause to a query if it doesn't already have one |
| 99 | */ |
| 100 | const addLimitToQuery = useCallback((sql: string, limit: number = 1000): string => { |
| 101 | const normalizedSql = sql.trim(); |
| 102 | if (!normalizedSql.toLowerCase().includes('limit')) { |
| 103 | return `${normalizedSql} LIMIT ${limit}`; |
| 104 | } |
| 105 | return sql; |
| 106 | }, []); |
no test coverage detected