( data: SQLExportData, format: SQLExportFormat )
| 36 | * 导出 SQL 结果到文件 |
| 37 | */ |
| 38 | export async function exportSQLResult( |
| 39 | data: SQLExportData, |
| 40 | format: SQLExportFormat |
| 41 | ): Promise<{ success: boolean; filePath?: string; error?: string }> { |
| 42 | if (data.rows.length === 0) { |
| 43 | return { success: false, error: 'No data to export' } |
| 44 | } |
| 45 | |
| 46 | const timestamp = dayjs().format('YYYYMMDD_HHmmss') |
| 47 | const filename = `sql_result_${timestamp}.${format}` |
| 48 | |
| 49 | let content: string |
| 50 | let mimeType: string |
| 51 | |
| 52 | if (format === 'json') { |
| 53 | content = formatAsJSON(data) |
| 54 | mimeType = 'application/json' |
| 55 | } else { |
| 56 | content = formatAsCSV(data) |
| 57 | mimeType = 'text/csv' |
| 58 | } |
| 59 | |
| 60 | // 转换为 data URL 并保存 |
| 61 | const dataUrl = `data:${mimeType};charset=utf-8,${encodeURIComponent(content)}` |
| 62 | const { useCacheService } = await import('@/services/cache/service') |
| 63 | const result = await useCacheService().saveToDownloads(filename, dataUrl) |
| 64 | |
| 65 | return result |
| 66 | } |
nothing calls this directly
no test coverage detected