dsh-data-window 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/AGENTS.md +111 -0
  2. package/CHANGELOG.md +239 -0
  3. package/LICENSE +21 -0
  4. package/README.md +146 -2
  5. package/client.js +7779 -0
  6. package/cordis.patch.yml +4 -0
  7. package/docs/BRIDGE.md +100 -0
  8. package/docs/DESIGN.md +417 -0
  9. package/docs/DETAIL.md +220 -0
  10. package/docs/screenshots/ddl.png +0 -0
  11. package/docs/screenshots/workbench.png +0 -0
  12. package/icon.svg +30 -0
  13. package/index.js +180 -0
  14. package/locale/en.json +286 -0
  15. package/locale/zh.json +286 -0
  16. package/package.json +68 -4
  17. package/src/client/00-css.js +993 -0
  18. package/src/client/05-icons.js +137 -0
  19. package/src/client/05-theme.js +107 -0
  20. package/src/client/06-shortcuts.js +89 -0
  21. package/src/client/07-bridge.js +193 -0
  22. package/src/client/10-core.js +1352 -0
  23. package/src/client/20-tree.js +281 -0
  24. package/src/client/25-sql.js +631 -0
  25. package/src/client/30-editor.js +537 -0
  26. package/src/client/40-results.js +556 -0
  27. package/src/client/45-export.js +219 -0
  28. package/src/client/50-panel.js +761 -0
  29. package/src/client/60-settings.js +180 -0
  30. package/src/client/63-schema.js +186 -0
  31. package/src/client/64-manual.js +350 -0
  32. package/src/client/65-ai.js +320 -0
  33. package/src/client/66-optimize.js +98 -0
  34. package/src/client/90-apply.js +183 -0
  35. package/src/host/ai.js +721 -0
  36. package/src/host/cache.js +157 -0
  37. package/src/host/catalog.js +161 -0
  38. package/src/host/consoles.js +116 -0
  39. package/src/host/discovery.js +603 -0
  40. package/src/host/manual.js +276 -0
  41. package/src/host/mysql.js +650 -0
  42. package/src/host/remembered.js +225 -0
  43. package/src/host/rpc.js +487 -0
  44. package/src/host/shared.js +172 -0
  45. package/src/host/yaml.js +335 -0
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Result export: CSV, a minimal XLSX writer and an INSERT script.
3
+ *
4
+ * Browsers cannot write to an arbitrary path on their own, so `dwSaveFile` uses the
5
+ * File System Access API (`showSaveFilePicker`) when the host provides it — that is
6
+ * what lets the user pick the directory and file name. Where it is unavailable the
7
+ * file is downloaded instead, and the caller reports that in the run log.
8
+ *
9
+ * Everything here is pure except the save step: nothing is sent to the database,
10
+ * and the XLSX is assembled by hand (a stored-entry ZIP) so the bundle stays free of
11
+ * third-party archives.
12
+ */
13
+
14
+ const DW_CRC_TABLE = (() => {
15
+ const table = new Uint32Array(256)
16
+ for (let index = 0; index < 256; index += 1) {
17
+ let value = index
18
+ for (let bit = 0; bit < 8; bit += 1) value = (value & 1) === 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1
19
+ table[index] = value >>> 0
20
+ }
21
+ return table
22
+ })()
23
+
24
+ function dwCrc32(bytes) {
25
+ let crc = 0xffffffff
26
+ for (let index = 0; index < bytes.length; index += 1) crc = DW_CRC_TABLE[(crc ^ bytes[index]) & 0xff] ^ (crc >>> 8)
27
+ return (crc ^ 0xffffffff) >>> 0
28
+ }
29
+
30
+ function dwConcatBytes(parts) {
31
+ const total = parts.reduce((sum, part) => sum + part.length, 0)
32
+ const out = new Uint8Array(total)
33
+ let offset = 0
34
+ for (const part of parts) {
35
+ out.set(part, offset)
36
+ offset += part.length
37
+ }
38
+ return out
39
+ }
40
+
41
+ /** A ZIP with stored (uncompressed) entries, which is all an XLSX needs. */
42
+ function dwZipStore(files) {
43
+ const encoder = new TextEncoder()
44
+ const entries = files.map((file) => ({
45
+ name: encoder.encode(file.name),
46
+ data: typeof file.data === 'string' ? encoder.encode(file.data) : file.data,
47
+ }))
48
+ const localParts = []
49
+ const centralParts = []
50
+ let offset = 0
51
+ for (const entry of entries) {
52
+ const crc = dwCrc32(entry.data)
53
+ const local = new Uint8Array(30 + entry.name.length)
54
+ const localView = new DataView(local.buffer)
55
+ localView.setUint32(0, 0x04034b50, true)
56
+ localView.setUint16(4, 20, true)
57
+ localView.setUint16(6, 0x0800, true)
58
+ localView.setUint16(8, 0, true)
59
+ localView.setUint32(14, crc, true)
60
+ localView.setUint32(18, entry.data.length, true)
61
+ localView.setUint32(22, entry.data.length, true)
62
+ localView.setUint16(26, entry.name.length, true)
63
+ local.set(entry.name, 30)
64
+ localParts.push(local, entry.data)
65
+
66
+ const central = new Uint8Array(46 + entry.name.length)
67
+ const centralView = new DataView(central.buffer)
68
+ centralView.setUint32(0, 0x02014b50, true)
69
+ centralView.setUint16(4, 20, true)
70
+ centralView.setUint16(6, 20, true)
71
+ centralView.setUint16(8, 0x0800, true)
72
+ centralView.setUint32(16, crc, true)
73
+ centralView.setUint32(20, entry.data.length, true)
74
+ centralView.setUint32(24, entry.data.length, true)
75
+ centralView.setUint16(28, entry.name.length, true)
76
+ centralView.setUint32(42, offset, true)
77
+ central.set(entry.name, 46)
78
+ centralParts.push(central)
79
+ offset += local.length + entry.data.length
80
+ }
81
+ const centralSize = centralParts.reduce((sum, part) => sum + part.length, 0)
82
+ const end = new Uint8Array(22)
83
+ const endView = new DataView(end.buffer)
84
+ endView.setUint32(0, 0x06054b50, true)
85
+ endView.setUint16(8, entries.length, true)
86
+ endView.setUint16(10, entries.length, true)
87
+ endView.setUint32(12, centralSize, true)
88
+ endView.setUint32(16, offset, true)
89
+ return dwConcatBytes([...localParts, ...centralParts, end])
90
+ }
91
+
92
+ /** One value as text for CSV: NULL becomes an empty field, quotes doubled. */
93
+ function dwCsvField(value) {
94
+ if (value === null || value === undefined) return ''
95
+ const text = String(value)
96
+ return /[",\r\n]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text
97
+ }
98
+
99
+ /** CSV text with a BOM, so Excel opens UTF-8 Chinese columns correctly. */
100
+ export function dwCsvText(columns, rows) {
101
+ const header = columns.map((column) => dwCsvField(column.name)).join(',')
102
+ const body = rows.map((row) => columns.map((column, index) => dwCsvField(row[index])).join(','))
103
+ return `\ufeff${[header, ...body].join('\r\n')}\r\n`
104
+ }
105
+
106
+ /** One SQL literal for the INSERT script (same rules as the UPDATE generator). */
107
+ function dwExportLiteral(value, numeric) {
108
+ if (value === null || value === undefined) return 'NULL'
109
+ const text = String(value)
110
+ if (numeric === true && /^-?\d+(\.\d+)?$/.test(text.trim())) return text.trim()
111
+ return `'${text.replace(/'/g, "''")}'`
112
+ }
113
+
114
+ /** INSERT statements for the displayed rows; the caller must know the table. */
115
+ export function dwInsertText(target, columns, rows, numeric) {
116
+ const quote = (name) => `\`${String(name).replace(/`/g, '``')}\``
117
+ const from = target.database === '' ? quote(target.table) : `${quote(target.database)}.${quote(target.table)}`
118
+ const names = columns.map((column) => quote(column.name)).join(', ')
119
+ return `${rows.map((row) => `INSERT INTO ${from} (${names}) VALUES (${columns.map((column, index) => dwExportLiteral(row[index], numeric[index])).join(', ')});`).join('\n')}\n`
120
+ }
121
+
122
+ function dwXmlEscape(text) {
123
+ return String(text)
124
+ .replace(/[<>&"']/g, (character) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;', "'": '&apos;' }[character]))
125
+ .replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, '')
126
+ }
127
+
128
+ /** A1, B1 … AA1: spreadsheet column names for a cell reference. */
129
+ function dwColumnName(index) {
130
+ let name = ''
131
+ let rest = index
132
+ while (rest >= 0) {
133
+ name = String.fromCharCode(65 + (rest % 26)) + name
134
+ rest = Math.floor(rest / 26) - 1
135
+ }
136
+ return name
137
+ }
138
+
139
+ function dwXlsxCell(reference, value, numeric) {
140
+ if (value === null || value === undefined || value === '') return `<c r="${reference}"/>`
141
+ const text = String(value)
142
+ // Excel keeps only 15 significant digits; longer integers stay text so a BIGINT
143
+ // id is not silently rounded.
144
+ if (numeric === true && /^-?\d+(\.\d+)?$/.test(text) && text.replace(/[-.]/g, '').length <= 15) {
145
+ return `<c r="${reference}"><v>${text}</v></c>`
146
+ }
147
+ return `<c r="${reference}" t="inlineStr"><is><t xml:space="preserve">${dwXmlEscape(text)}</t></is></c>`
148
+ }
149
+
150
+ /** An XLSX workbook with one worksheet, as bytes. */
151
+ export function dwXlsxBytes(columns, rows, numeric) {
152
+ const sheetRows = [
153
+ `<row r="1">${columns.map((column, index) => dwXlsxCell(`${dwColumnName(index)}1`, column.name)).join('')}</row>`,
154
+ ...rows.map((row, rowIndex) => {
155
+ const line = rowIndex + 2
156
+ return `<row r="${line}">${columns.map((column, index) => dwXlsxCell(`${dwColumnName(index)}${line}`, row[index], numeric[index])).join('')}</row>`
157
+ }),
158
+ ]
159
+ const sheet = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
160
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>${sheetRows.join('')}</sheetData></worksheet>`
161
+ return dwZipStore([
162
+ {
163
+ name: '[Content_Types].xml',
164
+ data: `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
165
+ <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/></Types>`,
166
+ },
167
+ {
168
+ name: '_rels/.rels',
169
+ data: `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
170
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>`,
171
+ },
172
+ {
173
+ name: 'xl/workbook.xml',
174
+ data: `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
175
+ <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Result" sheetId="1" r:id="rId1"/></sheets></workbook>`,
176
+ },
177
+ {
178
+ name: 'xl/_rels/workbook.xml.rels',
179
+ data: `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
180
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/></Relationships>`,
181
+ },
182
+ { name: 'xl/worksheets/sheet1.xml', data: sheet },
183
+ ])
184
+ }
185
+
186
+ /**
187
+ * Ask the user where to save, then write there.
188
+ *
189
+ * @returns `{ via: 'picker' | 'download', name }` or `{ cancelled: true }`
190
+ */
191
+ export async function dwSaveFile(options) {
192
+ const picker = globalThis.showSaveFilePicker
193
+ if (typeof picker === 'function') {
194
+ try {
195
+ const handle = await picker({
196
+ suggestedName: options.name,
197
+ types: [{ description: options.description, accept: { [options.mime]: [options.extension] } }],
198
+ })
199
+ const writable = await handle.createWritable()
200
+ await writable.write(options.data)
201
+ await writable.close()
202
+ return { via: 'picker', name: handle.name ?? options.name }
203
+ } catch (error) {
204
+ if (error?.name === 'AbortError') return { cancelled: true }
205
+ // A host that exposes the picker but refuses it (permissions, Electron build)
206
+ // still gets the file through the download path below.
207
+ }
208
+ }
209
+ const blob = new Blob([options.data], { type: options.mime })
210
+ const url = URL.createObjectURL(blob)
211
+ const anchor = document.createElement('a')
212
+ anchor.href = url
213
+ anchor.download = options.name
214
+ document.body.appendChild(anchor)
215
+ anchor.click()
216
+ anchor.remove()
217
+ setTimeout(() => URL.revokeObjectURL(url), 5000)
218
+ return { via: 'download', name: options.name }
219
+ }