|
| 1 | +/** |
| 2 | + * Output formatters for psql command |
| 3 | + */ |
| 4 | + |
| 5 | +import type { PsqlOptions } from "./parser.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Format query results based on output options |
| 9 | + */ |
| 10 | +export function formatResults( |
| 11 | + columns: string[], |
| 12 | + rows: unknown[][], |
| 13 | + options: PsqlOptions, |
| 14 | +): string { |
| 15 | + if (rows.length === 0 && options.tuplesOnly) { |
| 16 | + return ""; |
| 17 | + } |
| 18 | + |
| 19 | + switch (options.outputFormat) { |
| 20 | + case "aligned": |
| 21 | + return formatAligned(columns, rows, options); |
| 22 | + case "unaligned": |
| 23 | + return formatUnaligned(columns, rows, options); |
| 24 | + case "csv": |
| 25 | + return formatCsv(columns, rows, options); |
| 26 | + case "json": |
| 27 | + return formatJson(columns, rows); |
| 28 | + case "html": |
| 29 | + return formatHtml(columns, rows, options); |
| 30 | + default: |
| 31 | + return formatAligned(columns, rows, options); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Format as aligned table (default psql output) |
| 37 | + */ |
| 38 | +function formatAligned( |
| 39 | + columns: string[], |
| 40 | + rows: unknown[][], |
| 41 | + options: PsqlOptions, |
| 42 | +): string { |
| 43 | + if (columns.length === 0) return ""; |
| 44 | + |
| 45 | + const widths = columns.map((col, i) => { |
| 46 | + const maxDataWidth = Math.max( |
| 47 | + ...rows.map((row) => String(row[i] ?? "").length), |
| 48 | + ); |
| 49 | + return Math.max(col.length, maxDataWidth); |
| 50 | + }); |
| 51 | + |
| 52 | + let output = ""; |
| 53 | + |
| 54 | + // Header |
| 55 | + if (!options.tuplesOnly) { |
| 56 | + output += |
| 57 | + columns.map((col, i) => col.padEnd(widths[i])).join(" | ") + |
| 58 | + options.recordSeparator; |
| 59 | + |
| 60 | + // Separator line |
| 61 | + output += |
| 62 | + widths.map((w) => "-".repeat(w)).join("-+-") + options.recordSeparator; |
| 63 | + } |
| 64 | + |
| 65 | + // Rows |
| 66 | + for (const row of rows) { |
| 67 | + output += |
| 68 | + row.map((val, i) => String(val ?? "").padEnd(widths[i])).join(" | ") + |
| 69 | + options.recordSeparator; |
| 70 | + } |
| 71 | + |
| 72 | + // Footer with row count |
| 73 | + if (!options.tuplesOnly && !options.quiet) { |
| 74 | + const rowText = rows.length === 1 ? "row" : "rows"; |
| 75 | + output += `(${rows.length} ${rowText})${options.recordSeparator}`; |
| 76 | + } |
| 77 | + |
| 78 | + return output; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Format as unaligned output (field separator delimited) |
| 83 | + */ |
| 84 | +function formatUnaligned( |
| 85 | + columns: string[], |
| 86 | + rows: unknown[][], |
| 87 | + options: PsqlOptions, |
| 88 | +): string { |
| 89 | + let output = ""; |
| 90 | + |
| 91 | + // Header |
| 92 | + if (!options.tuplesOnly) { |
| 93 | + output += columns.join(options.fieldSeparator) + options.recordSeparator; |
| 94 | + } |
| 95 | + |
| 96 | + // Rows |
| 97 | + for (const row of rows) { |
| 98 | + output += |
| 99 | + row.map((val) => String(val ?? "")).join(options.fieldSeparator) + |
| 100 | + options.recordSeparator; |
| 101 | + } |
| 102 | + |
| 103 | + return output; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Format as CSV |
| 108 | + */ |
| 109 | +function formatCsv( |
| 110 | + columns: string[], |
| 111 | + rows: unknown[][], |
| 112 | + options: PsqlOptions, |
| 113 | +): string { |
| 114 | + let output = ""; |
| 115 | + |
| 116 | + // Header |
| 117 | + if (!options.tuplesOnly) { |
| 118 | + output += columns.map(escapeCsv).join(",") + options.recordSeparator; |
| 119 | + } |
| 120 | + |
| 121 | + // Rows |
| 122 | + for (const row of rows) { |
| 123 | + output += |
| 124 | + row.map((val) => escapeCsv(String(val ?? ""))).join(",") + |
| 125 | + options.recordSeparator; |
| 126 | + } |
| 127 | + |
| 128 | + return output; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Escape CSV field (add quotes if needed) |
| 133 | + */ |
| 134 | +function escapeCsv(field: string): string { |
| 135 | + if ( |
| 136 | + field.includes(",") || |
| 137 | + field.includes('"') || |
| 138 | + field.includes("\n") || |
| 139 | + field.includes("\r") |
| 140 | + ) { |
| 141 | + return `"${field.replace(/"/g, '""')}"`; |
| 142 | + } |
| 143 | + return field; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Format as JSON array of objects |
| 148 | + */ |
| 149 | +function formatJson(columns: string[], rows: unknown[][]): string { |
| 150 | + const objects = rows.map((row) => { |
| 151 | + const obj: Record<string, unknown> = {}; |
| 152 | + for (let i = 0; i < columns.length; i++) { |
| 153 | + obj[columns[i]] = row[i]; |
| 154 | + } |
| 155 | + return obj; |
| 156 | + }); |
| 157 | + |
| 158 | + return `${JSON.stringify(objects, null, 2)}\n`; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Format as HTML table |
| 163 | + */ |
| 164 | +function formatHtml( |
| 165 | + columns: string[], |
| 166 | + rows: unknown[][], |
| 167 | + options: PsqlOptions, |
| 168 | +): string { |
| 169 | + let output = "<table>\n"; |
| 170 | + |
| 171 | + // Header |
| 172 | + if (!options.tuplesOnly) { |
| 173 | + output += " <thead>\n <tr>\n"; |
| 174 | + for (const col of columns) { |
| 175 | + output += ` <th>${escapeHtml(col)}</th>\n`; |
| 176 | + } |
| 177 | + output += " </tr>\n </thead>\n"; |
| 178 | + } |
| 179 | + |
| 180 | + // Body |
| 181 | + output += " <tbody>\n"; |
| 182 | + for (const row of rows) { |
| 183 | + output += " <tr>\n"; |
| 184 | + for (const val of row) { |
| 185 | + output += ` <td>${escapeHtml(String(val ?? ""))}</td>\n`; |
| 186 | + } |
| 187 | + output += " </tr>\n"; |
| 188 | + } |
| 189 | + output += " </tbody>\n"; |
| 190 | + |
| 191 | + output += "</table>\n"; |
| 192 | + return output; |
| 193 | +} |
| 194 | + |
| 195 | +/** |
| 196 | + * Escape HTML entities |
| 197 | + */ |
| 198 | +function escapeHtml(text: string): string { |
| 199 | + return text |
| 200 | + .replace(/&/g, "&") |
| 201 | + .replace(/</g, "<") |
| 202 | + .replace(/>/g, ">") |
| 203 | + .replace(/"/g, """) |
| 204 | + .replace(/'/g, "'"); |
| 205 | +} |
0 commit comments