@alexsun-top/opencode-chat-room 0.8.4 → 0.8.5
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.
- package/CHANGELOG.md +5 -1
- package/README.md +4 -4
- package/README.zh-CN.md +4 -4
- package/cli.ts +1 -1
- package/dist/cli.js +35 -16
- package/package.json +14 -11
- package/packages/chat-room-channeld/README.md +3 -2
- package/packages/chat-room-channeld/src/inbound.ts +3 -6
- package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
- package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
- package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
- package/plugin.ts +1 -1
- package/server/assets/chat/admin.js +4 -1
- package/server/assets/chat/announcements.js +3 -0
- package/server/assets/chat/attachments.js +3 -3
- package/server/assets/chat/clipboard.js +1 -1
- package/server/assets/chat/copy-action.js +1 -1
- package/server/assets/chat/doc-preview.js +1 -1
- package/server/assets/chat/editor.js +11 -7
- package/server/assets/chat/events.js +7 -5
- package/server/assets/chat/files.js +5 -1
- package/server/assets/chat/integration-editor.js +2 -0
- package/server/assets/chat/integrations.js +1 -1
- package/server/assets/chat/join-requests.js +1 -1
- package/server/assets/chat/markdown-footnotes.js +8 -7
- package/server/assets/chat/markdown.js +6 -5
- package/server/assets/chat/members.js +3 -3
- package/server/assets/chat/mentions.js +8 -5
- package/server/assets/chat/message-cache.js +1 -1
- package/server/assets/chat/message-polling.js +2 -2
- package/server/assets/chat/message-window.js +1 -1
- package/server/assets/chat/messages.js +17 -3
- package/server/assets/chat/office-preview.js +3 -3
- package/server/assets/chat/palette.js +5 -3
- package/server/assets/chat/panels.js +19 -0
- package/server/assets/chat/questions.js +3 -2
- package/server/assets/chat/reminders.js +3 -0
- package/server/assets/chat/replies.js +6 -3
- package/server/assets/chat/resource-format.js +2 -2
- package/server/assets/chat/rooms.js +7 -4
- package/server/assets/chat/settings.js +4 -3
- package/server/assets/chat/shortcuts.js +3 -1
- package/server/assets/chat/side-pane.js +1 -1
- package/server/assets/chat/state.js +30 -11
- package/server/assets/chat/system-events.js +3 -2
- package/server/assets/chat/system-messages.js +15 -15
- package/server/assets/chat/tasks.js +4 -3
- package/server/assets/chat/window-controls.js +3 -2
- package/server/assets/conversation.css +14 -2
- package/server/assets/doc-preview.css +6 -0
- package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
- package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
- package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
- package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
- package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
- package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
- package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
- package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
- package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
- package/server/sw.js +3 -1
- package/skills/chatroom/SKILL.md +9 -0
- package/src/README.md +1 -1
- package/src/agent/agent-receipts.ts +12 -4
- package/src/center/center-client.ts +1 -1
- package/src/state/pg-sql.mjs +157 -142
- package/src/state/pg-sql.mts +175 -0
- package/src/state/pg-worker.mjs +107 -99
- package/src/state/pg-worker.mts +166 -0
- package/src/state/store.ts +21 -7
- package/src/tui/app-types.mjs +1 -0
- package/src/tui/app-types.mts +175 -0
- package/src/tui/app.mjs +426 -423
- package/src/tui/app.mts +500 -0
- package/src/tui/config.mjs +166 -158
- package/src/tui/config.mts +247 -0
- package/src/tui/http.mjs +239 -206
- package/src/tui/http.mts +412 -0
- package/src/tui/i18n.mjs +17 -20
- package/src/tui/i18n.mts +42 -0
- package/src/tui/info-app.mjs +431 -427
- package/src/tui/info-app.mts +500 -0
- package/src/tui/info-content.mjs +205 -199
- package/src/tui/info-content.mts +341 -0
- package/src/tui/join-app.mjs +75 -64
- package/src/tui/join-app.mts +99 -0
- package/src/tui/locales/en.mjs +302 -320
- package/src/tui/locales/en.mts +349 -0
- package/src/tui/locales/zh-CN.mjs +302 -320
- package/src/tui/locales/zh-CN.mts +349 -0
- package/src/tui/main.mjs +219 -213
- package/src/tui/main.mts +255 -0
- package/src/tui/markdown.mjs +128 -272
- package/src/tui/markdown.mts +152 -0
- package/src/tui/mentions.mjs +181 -184
- package/src/tui/mentions.mts +251 -0
- package/src/tui/message-app.mjs +181 -173
- package/src/tui/message-app.mts +213 -0
- package/src/tui/message-view.mjs +90 -111
- package/src/tui/message-view.mts +151 -0
- package/src/tui/messages.mjs +55 -49
- package/src/tui/messages.mts +106 -0
- package/src/tui/palette-app.mjs +364 -344
- package/src/tui/palette-app.mts +388 -0
- package/src/tui/palette.mjs +100 -98
- package/src/tui/palette.mts +176 -0
- package/src/tui/prefs.mjs +56 -63
- package/src/tui/prefs.mts +126 -0
- package/src/tui/reminder-app.mjs +191 -181
- package/src/tui/reminder-app.mts +228 -0
- package/src/tui/room-settings-app.mjs +257 -242
- package/src/tui/room-settings-app.mts +301 -0
- package/src/tui/session.mjs +30 -29
- package/src/tui/session.mts +51 -0
- package/src/tui/settings-app.mjs +151 -138
- package/src/tui/settings-app.mts +179 -0
- package/src/tui/ui-content.mjs +426 -421
- package/src/tui/ui-content.mts +499 -0
- package/src/tui/ui-help.mjs +58 -67
- package/src/tui/ui-help.mts +87 -0
- package/src/tui/ui-mentions.mjs +34 -36
- package/src/tui/ui-mentions.mts +60 -0
- package/src/tui/ui-overlay-view.mjs +259 -267
- package/src/tui/ui-overlay-view.mts +322 -0
- package/src/tui/ui-suggest-view.mjs +54 -57
- package/src/tui/ui-suggest-view.mts +78 -0
- package/src/tui/ui-suggest.mjs +164 -167
- package/src/tui/ui-suggest.mts +248 -0
- package/src/tui/ui-theme.mjs +52 -57
- package/src/tui/ui-theme.mts +98 -0
- package/src/tui/ui-view.mjs +267 -275
- package/src/tui/ui-view.mts +308 -0
- package/src/tui/ui.mjs +372 -359
- package/src/tui/ui.mts +440 -0
- package/src/tui/wire.mjs +215 -0
- package/src/tui/wire.mts +383 -0
package/src/state/pg-sql.mjs
CHANGED
|
@@ -4,172 +4,187 @@
|
|
|
4
4
|
//
|
|
5
5
|
// This is intentionally not a general SQLite translator. Keep the dialect
|
|
6
6
|
// narrow and add a test when the state layer starts using a new construct.
|
|
7
|
-
|
|
8
7
|
const PRAGMA_TABLE_INFO = /PRAGMA\s+table_info\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)/gi;
|
|
9
|
-
|
|
10
8
|
function pragmaTableInfo(table) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
const tableName = table.replace(/'/g, "''");
|
|
10
|
+
return `SELECT c.column_name AS name,c.data_type AS type,` +
|
|
11
|
+
`CASE WHEN c.is_nullable='NO' THEN 1 ELSE 0 END AS "notnull",` +
|
|
12
|
+
`c.column_default AS dflt_value,` +
|
|
13
|
+
`CASE WHEN EXISTS (SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=ANY(i.indkey) ` +
|
|
14
|
+
`WHERE i.indrelid=(quote_ident(c.table_schema)||'.'||quote_ident(c.table_name))::regclass AND i.indisprimary AND a.attname=c.column_name) ` +
|
|
15
|
+
`THEN 1 ELSE 0 END AS pk ` +
|
|
16
|
+
`FROM information_schema.columns c WHERE c.table_schema=current_schema() AND c.table_name='${tableName}' ORDER BY c.ordinal_position`;
|
|
19
17
|
}
|
|
20
|
-
|
|
21
18
|
const isIdentStart = (ch) => !!ch && /[A-Za-z_]/.test(ch);
|
|
22
19
|
const isIdentChar = (ch) => !!ch && /[A-Za-z0-9_$]/.test(ch);
|
|
23
|
-
|
|
24
20
|
function skipQuoted(sql, index) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
const quote = sql[index];
|
|
22
|
+
let i = index + 1;
|
|
23
|
+
while (i < sql.length) {
|
|
24
|
+
if (sql[i] === quote) {
|
|
25
|
+
if (sql[i + 1] === quote) {
|
|
26
|
+
i += 2;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
return i + 1;
|
|
30
|
+
}
|
|
31
|
+
i++;
|
|
31
32
|
}
|
|
32
|
-
i
|
|
33
|
-
}
|
|
34
|
-
return i;
|
|
33
|
+
return i;
|
|
35
34
|
}
|
|
36
|
-
|
|
37
35
|
function skipComment(sql, index) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
if (sql[index + 1] === "-") {
|
|
37
|
+
const end = sql.indexOf("\n", index + 2);
|
|
38
|
+
return end < 0 ? sql.length : end;
|
|
39
|
+
}
|
|
40
|
+
const end = sql.indexOf("*/", index + 2);
|
|
41
|
+
return end < 0 ? sql.length : end + 2;
|
|
44
42
|
}
|
|
45
|
-
|
|
46
43
|
function hasTopLevelComma(sql, openIndex) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
44
|
+
let depth = 0;
|
|
45
|
+
for (let i = openIndex; i < sql.length; i++) {
|
|
46
|
+
const ch = sql[i];
|
|
47
|
+
if (ch === "'" || ch === '"') {
|
|
48
|
+
i = skipQuoted(sql, i) - 1;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (ch === "-" && sql[i + 1] === "-") {
|
|
52
|
+
i = skipComment(sql, i) - 1;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (ch === "/" && sql[i + 1] === "*") {
|
|
56
|
+
i = skipComment(sql, i) - 1;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (ch === "(")
|
|
60
|
+
depth++;
|
|
61
|
+
else if (ch === ")") {
|
|
62
|
+
depth--;
|
|
63
|
+
if (depth === 0)
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
else if (ch === "," && depth === 1)
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
58
70
|
}
|
|
59
|
-
|
|
60
71
|
function wordBefore(sql, index) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
let i = index - 1;
|
|
73
|
+
while (i >= 0 && /\s/.test(sql.charAt(i)))
|
|
74
|
+
i--;
|
|
75
|
+
const end = i + 1;
|
|
76
|
+
while (i >= 0 && isIdentChar(sql[i]))
|
|
77
|
+
i--;
|
|
78
|
+
return sql.slice(i + 1, end).toLowerCase();
|
|
66
79
|
}
|
|
67
|
-
|
|
68
80
|
function wordAfter(sql, index) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
81
|
+
let i = index + 1;
|
|
82
|
+
while (i < sql.length && /\s/.test(sql.charAt(i)))
|
|
83
|
+
i++;
|
|
84
|
+
const start = i;
|
|
85
|
+
while (i < sql.length && isIdentChar(sql[i]))
|
|
86
|
+
i++;
|
|
87
|
+
return sql.slice(start, i).toLowerCase();
|
|
74
88
|
}
|
|
75
|
-
|
|
76
89
|
function scan(sql) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
if (ch === "-" && sql[index + 1] === "-") {
|
|
89
|
-
const end = skipComment(sql, index);
|
|
90
|
-
out += sql.slice(index, end);
|
|
91
|
-
index = end;
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (ch === "/" && sql[index + 1] === "*") {
|
|
95
|
-
const end = skipComment(sql, index);
|
|
96
|
-
out += sql.slice(index, end);
|
|
97
|
-
index = end;
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
if (ch === "?") {
|
|
101
|
-
// CASE WHEN ? needs a boolean; a bare `? IS NULL` has no type to infer.
|
|
102
|
-
const nextWord = wordAfter(sql, index);
|
|
103
|
-
const cast = (wordBefore(sql, index) === "when" && nextWord === "then") || nextWord === "is";
|
|
104
|
-
out += `$${++parameter}${cast ? (nextWord === "is" ? "::text" : "::boolean") : ""}`;
|
|
105
|
-
index++;
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
if (isIdentStart(ch)) {
|
|
109
|
-
let end = index + 1;
|
|
110
|
-
while (isIdentChar(sql[end])) end++;
|
|
111
|
-
const word = sql.slice(index, end);
|
|
112
|
-
const lower = word.toLowerCase();
|
|
113
|
-
// SQLite's scalar max(a,b)/min(a,b); the one-argument aggregate forms stay.
|
|
114
|
-
if (lower === "max" || lower === "min") {
|
|
115
|
-
let next = end;
|
|
116
|
-
while (next < sql.length && /\s/.test(sql[next])) next++;
|
|
117
|
-
if (sql[next] === "(" && hasTopLevelComma(sql, next)) {
|
|
118
|
-
out += lower === "max" ? "GREATEST" : "LEAST";
|
|
119
|
-
index = end;
|
|
120
|
-
continue;
|
|
90
|
+
let out = "";
|
|
91
|
+
let index = 0;
|
|
92
|
+
let parameter = 0;
|
|
93
|
+
while (index < sql.length) {
|
|
94
|
+
const ch = sql.charAt(index);
|
|
95
|
+
if (ch === "'" || ch === '"') {
|
|
96
|
+
const end = skipQuoted(sql, index);
|
|
97
|
+
out += sql.slice(index, end);
|
|
98
|
+
index = end;
|
|
99
|
+
continue;
|
|
121
100
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
let next = end;
|
|
127
|
-
while (next < sql.length && /\s/.test(sql[next])) next++;
|
|
128
|
-
if (isIdentStart(sql[next])) {
|
|
129
|
-
let aliasEnd = next + 1;
|
|
130
|
-
while (isIdentChar(sql[aliasEnd])) aliasEnd++;
|
|
131
|
-
const alias = sql.slice(next, aliasEnd);
|
|
132
|
-
if (/[A-Z]/.test(alias) && previous !== "default" && alias.toLowerCase() !== "identity") {
|
|
133
|
-
out += `AS "${alias}"`;
|
|
134
|
-
index = aliasEnd;
|
|
101
|
+
if (ch === "-" && sql[index + 1] === "-") {
|
|
102
|
+
const end = skipComment(sql, index);
|
|
103
|
+
out += sql.slice(index, end);
|
|
104
|
+
index = end;
|
|
135
105
|
continue;
|
|
136
|
-
}
|
|
137
106
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
107
|
+
if (ch === "/" && sql[index + 1] === "*") {
|
|
108
|
+
const end = skipComment(sql, index);
|
|
109
|
+
out += sql.slice(index, end);
|
|
110
|
+
index = end;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (ch === "?") {
|
|
114
|
+
// CASE WHEN ? needs a boolean; a bare `? IS NULL` has no type to infer.
|
|
115
|
+
const nextWord = wordAfter(sql, index);
|
|
116
|
+
const cast = (wordBefore(sql, index) === "when" && nextWord === "then") || nextWord === "is";
|
|
117
|
+
out += `$${++parameter}${cast ? (nextWord === "is" ? "::text" : "::boolean") : ""}`;
|
|
118
|
+
index++;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (isIdentStart(ch)) {
|
|
122
|
+
let end = index + 1;
|
|
123
|
+
while (isIdentChar(sql[end]))
|
|
124
|
+
end++;
|
|
125
|
+
const word = sql.slice(index, end);
|
|
126
|
+
const lower = word.toLowerCase();
|
|
127
|
+
// SQLite's scalar max(a,b)/min(a,b); the one-argument aggregate forms stay.
|
|
128
|
+
if (lower === "max" || lower === "min") {
|
|
129
|
+
let next = end;
|
|
130
|
+
while (next < sql.length && /\s/.test(sql.charAt(next)))
|
|
131
|
+
next++;
|
|
132
|
+
if (sql[next] === "(" && hasTopLevelComma(sql, next)) {
|
|
133
|
+
out += lower === "max" ? "GREATEST" : "LEAST";
|
|
134
|
+
index = end;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Preserve camelCase result aliases: PostgreSQL folds unquoted identifiers.
|
|
139
|
+
if (lower === "as") {
|
|
140
|
+
const previous = wordBefore(sql, index);
|
|
141
|
+
let next = end;
|
|
142
|
+
while (next < sql.length && /\s/.test(sql.charAt(next)))
|
|
143
|
+
next++;
|
|
144
|
+
if (isIdentStart(sql[next])) {
|
|
145
|
+
let aliasEnd = next + 1;
|
|
146
|
+
while (isIdentChar(sql[aliasEnd]))
|
|
147
|
+
aliasEnd++;
|
|
148
|
+
const alias = sql.slice(next, aliasEnd);
|
|
149
|
+
if (/[A-Z]/.test(alias) && previous !== "default" && alias.toLowerCase() !== "identity") {
|
|
150
|
+
out += `AS "${alias}"`;
|
|
151
|
+
index = aliasEnd;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
out += word;
|
|
157
|
+
index = end;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
out += ch;
|
|
161
|
+
index++;
|
|
142
162
|
}
|
|
143
|
-
out
|
|
144
|
-
index++;
|
|
145
|
-
}
|
|
146
|
-
return out;
|
|
163
|
+
return out;
|
|
147
164
|
}
|
|
148
|
-
|
|
149
165
|
function appendOnConflictDoNothing(sql) {
|
|
150
|
-
|
|
166
|
+
return `${sql.replace(/[\s;]+$/, "")} ON CONFLICT DO NOTHING`;
|
|
151
167
|
}
|
|
152
|
-
|
|
153
168
|
export function translate(sql) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
169
|
+
let text = String(sql);
|
|
170
|
+
// Connection tuning pragmas have no PostgreSQL equivalent; foreign keys are
|
|
171
|
+
// always enforced and concurrency is handled by advisory locks.
|
|
172
|
+
text = text.replace(/PRAGMA\s+(?:busy_timeout|journal_mode|foreign_keys)\s*(?:=\s*[^;]+)?\s*;?/gi, "");
|
|
173
|
+
text = text.replace(PRAGMA_TABLE_INFO, (_, table) => pragmaTableInfo(table));
|
|
174
|
+
// SQLite INTEGER is 64-bit; PostgreSQL INTEGER is 32-bit and epoch
|
|
175
|
+
// milliseconds overflow it.
|
|
176
|
+
text = text.replace(/\bINTEGER\b/gi, "BIGINT");
|
|
177
|
+
text = text.replace(/\bBIGINT\s+PRIMARY\s+KEY\s+AUTOINCREMENT\b/gi, "BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY");
|
|
178
|
+
// Manual `BEGIN IMMEDIATE` appears inside receiver-state's schema script.
|
|
179
|
+
text = text.replace(/\bBEGIN\s+(?:DEFERRED|IMMEDIATE|EXCLUSIVE)\b/gi, "BEGIN");
|
|
180
|
+
if (/\bINSERT\s+OR\s+IGNORE\b/i.test(text)) {
|
|
181
|
+
text = text.replace(/\bINSERT\s+OR\s+IGNORE\b/gi, "INSERT");
|
|
182
|
+
if (!/\bON\s+CONFLICT\b/i.test(text))
|
|
183
|
+
text = appendOnConflictDoNothing(text);
|
|
184
|
+
}
|
|
185
|
+
// SQLite treats 1/0 as booleans; PostgreSQL needs an explicit cast.
|
|
186
|
+
return scan(text).trim();
|
|
171
187
|
}
|
|
172
|
-
|
|
173
188
|
export function hasSql(text) {
|
|
174
|
-
|
|
175
|
-
}
|
|
189
|
+
return text.replace(/[;\s]/g, "").length > 0;
|
|
190
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// SQLite -> PostgreSQL translation for the chat-room state SQL. The app's SQL
|
|
2
|
+
// uses ? placeholders, SQLite DDL, a few pragmas and a handful of scalar
|
|
3
|
+
// functions; everything else is deliberately passed through unchanged.
|
|
4
|
+
//
|
|
5
|
+
// This is intentionally not a general SQLite translator. Keep the dialect
|
|
6
|
+
// narrow and add a test when the state layer starts using a new construct.
|
|
7
|
+
|
|
8
|
+
const PRAGMA_TABLE_INFO = /PRAGMA\s+table_info\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)/gi;
|
|
9
|
+
|
|
10
|
+
function pragmaTableInfo(table: string): string {
|
|
11
|
+
const tableName = table.replace(/'/g, "''");
|
|
12
|
+
return `SELECT c.column_name AS name,c.data_type AS type,` +
|
|
13
|
+
`CASE WHEN c.is_nullable='NO' THEN 1 ELSE 0 END AS "notnull",` +
|
|
14
|
+
`c.column_default AS dflt_value,` +
|
|
15
|
+
`CASE WHEN EXISTS (SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=ANY(i.indkey) ` +
|
|
16
|
+
`WHERE i.indrelid=(quote_ident(c.table_schema)||'.'||quote_ident(c.table_name))::regclass AND i.indisprimary AND a.attname=c.column_name) ` +
|
|
17
|
+
`THEN 1 ELSE 0 END AS pk ` +
|
|
18
|
+
`FROM information_schema.columns c WHERE c.table_schema=current_schema() AND c.table_name='${tableName}' ORDER BY c.ordinal_position`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const isIdentStart = (ch: string | undefined): boolean => !!ch && /[A-Za-z_]/.test(ch);
|
|
22
|
+
const isIdentChar = (ch: string | undefined): boolean => !!ch && /[A-Za-z0-9_$]/.test(ch);
|
|
23
|
+
|
|
24
|
+
function skipQuoted(sql: string, index: number): number {
|
|
25
|
+
const quote = sql[index];
|
|
26
|
+
let i = index + 1;
|
|
27
|
+
while (i < sql.length) {
|
|
28
|
+
if (sql[i] === quote) {
|
|
29
|
+
if (sql[i + 1] === quote) { i += 2; continue; }
|
|
30
|
+
return i + 1;
|
|
31
|
+
}
|
|
32
|
+
i++;
|
|
33
|
+
}
|
|
34
|
+
return i;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function skipComment(sql: string, index: number): number {
|
|
38
|
+
if (sql[index + 1] === "-") {
|
|
39
|
+
const end = sql.indexOf("\n", index + 2);
|
|
40
|
+
return end < 0 ? sql.length : end;
|
|
41
|
+
}
|
|
42
|
+
const end = sql.indexOf("*/", index + 2);
|
|
43
|
+
return end < 0 ? sql.length : end + 2;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function hasTopLevelComma(sql: string, openIndex: number): boolean {
|
|
47
|
+
let depth = 0;
|
|
48
|
+
for (let i = openIndex; i < sql.length; i++) {
|
|
49
|
+
const ch = sql[i];
|
|
50
|
+
if (ch === "'" || ch === '"') { i = skipQuoted(sql, i) - 1; continue; }
|
|
51
|
+
if (ch === "-" && sql[i + 1] === "-") { i = skipComment(sql, i) - 1; continue; }
|
|
52
|
+
if (ch === "/" && sql[i + 1] === "*") { i = skipComment(sql, i) - 1; continue; }
|
|
53
|
+
if (ch === "(") depth++;
|
|
54
|
+
else if (ch === ")") { depth--; if (depth === 0) return false; }
|
|
55
|
+
else if (ch === "," && depth === 1) return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function wordBefore(sql: string, index: number): string {
|
|
61
|
+
let i = index - 1;
|
|
62
|
+
while (i >= 0 && /\s/.test(sql.charAt(i))) i--;
|
|
63
|
+
const end = i + 1;
|
|
64
|
+
while (i >= 0 && isIdentChar(sql[i])) i--;
|
|
65
|
+
return sql.slice(i + 1, end).toLowerCase();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function wordAfter(sql: string, index: number): string {
|
|
69
|
+
let i = index + 1;
|
|
70
|
+
while (i < sql.length && /\s/.test(sql.charAt(i))) i++;
|
|
71
|
+
const start = i;
|
|
72
|
+
while (i < sql.length && isIdentChar(sql[i])) i++;
|
|
73
|
+
return sql.slice(start, i).toLowerCase();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function scan(sql: string): string {
|
|
77
|
+
let out = "";
|
|
78
|
+
let index = 0;
|
|
79
|
+
let parameter = 0;
|
|
80
|
+
while (index < sql.length) {
|
|
81
|
+
const ch = sql.charAt(index);
|
|
82
|
+
if (ch === "'" || ch === '"') {
|
|
83
|
+
const end = skipQuoted(sql, index);
|
|
84
|
+
out += sql.slice(index, end);
|
|
85
|
+
index = end;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (ch === "-" && sql[index + 1] === "-") {
|
|
89
|
+
const end = skipComment(sql, index);
|
|
90
|
+
out += sql.slice(index, end);
|
|
91
|
+
index = end;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (ch === "/" && sql[index + 1] === "*") {
|
|
95
|
+
const end = skipComment(sql, index);
|
|
96
|
+
out += sql.slice(index, end);
|
|
97
|
+
index = end;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (ch === "?") {
|
|
101
|
+
// CASE WHEN ? needs a boolean; a bare `? IS NULL` has no type to infer.
|
|
102
|
+
const nextWord = wordAfter(sql, index);
|
|
103
|
+
const cast = (wordBefore(sql, index) === "when" && nextWord === "then") || nextWord === "is";
|
|
104
|
+
out += `$${++parameter}${cast ? (nextWord === "is" ? "::text" : "::boolean") : ""}`;
|
|
105
|
+
index++;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (isIdentStart(ch)) {
|
|
109
|
+
let end = index + 1;
|
|
110
|
+
while (isIdentChar(sql[end])) end++;
|
|
111
|
+
const word = sql.slice(index, end);
|
|
112
|
+
const lower = word.toLowerCase();
|
|
113
|
+
// SQLite's scalar max(a,b)/min(a,b); the one-argument aggregate forms stay.
|
|
114
|
+
if (lower === "max" || lower === "min") {
|
|
115
|
+
let next = end;
|
|
116
|
+
while (next < sql.length && /\s/.test(sql.charAt(next))) next++;
|
|
117
|
+
if (sql[next] === "(" && hasTopLevelComma(sql, next)) {
|
|
118
|
+
out += lower === "max" ? "GREATEST" : "LEAST";
|
|
119
|
+
index = end;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Preserve camelCase result aliases: PostgreSQL folds unquoted identifiers.
|
|
124
|
+
if (lower === "as") {
|
|
125
|
+
const previous = wordBefore(sql, index);
|
|
126
|
+
let next = end;
|
|
127
|
+
while (next < sql.length && /\s/.test(sql.charAt(next))) next++;
|
|
128
|
+
if (isIdentStart(sql[next])) {
|
|
129
|
+
let aliasEnd = next + 1;
|
|
130
|
+
while (isIdentChar(sql[aliasEnd])) aliasEnd++;
|
|
131
|
+
const alias = sql.slice(next, aliasEnd);
|
|
132
|
+
if (/[A-Z]/.test(alias) && previous !== "default" && alias.toLowerCase() !== "identity") {
|
|
133
|
+
out += `AS "${alias}"`;
|
|
134
|
+
index = aliasEnd;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
out += word;
|
|
140
|
+
index = end;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
out += ch;
|
|
144
|
+
index++;
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function appendOnConflictDoNothing(sql: string): string {
|
|
150
|
+
return `${sql.replace(/[\s;]+$/, "")} ON CONFLICT DO NOTHING`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function translate(sql: string): string {
|
|
154
|
+
let text = String(sql);
|
|
155
|
+
// Connection tuning pragmas have no PostgreSQL equivalent; foreign keys are
|
|
156
|
+
// always enforced and concurrency is handled by advisory locks.
|
|
157
|
+
text = text.replace(/PRAGMA\s+(?:busy_timeout|journal_mode|foreign_keys)\s*(?:=\s*[^;]+)?\s*;?/gi, "");
|
|
158
|
+
text = text.replace(PRAGMA_TABLE_INFO, (_, table) => pragmaTableInfo(table));
|
|
159
|
+
// SQLite INTEGER is 64-bit; PostgreSQL INTEGER is 32-bit and epoch
|
|
160
|
+
// milliseconds overflow it.
|
|
161
|
+
text = text.replace(/\bINTEGER\b/gi, "BIGINT");
|
|
162
|
+
text = text.replace(/\bBIGINT\s+PRIMARY\s+KEY\s+AUTOINCREMENT\b/gi, "BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY");
|
|
163
|
+
// Manual `BEGIN IMMEDIATE` appears inside receiver-state's schema script.
|
|
164
|
+
text = text.replace(/\bBEGIN\s+(?:DEFERRED|IMMEDIATE|EXCLUSIVE)\b/gi, "BEGIN");
|
|
165
|
+
if (/\bINSERT\s+OR\s+IGNORE\b/i.test(text)) {
|
|
166
|
+
text = text.replace(/\bINSERT\s+OR\s+IGNORE\b/gi, "INSERT");
|
|
167
|
+
if (!/\bON\s+CONFLICT\b/i.test(text)) text = appendOnConflictDoNothing(text);
|
|
168
|
+
}
|
|
169
|
+
// SQLite treats 1/0 as booleans; PostgreSQL needs an explicit cast.
|
|
170
|
+
return scan(text).trim();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function hasSql(text: string): boolean {
|
|
174
|
+
return text.replace(/[;\s]/g, "").length > 0;
|
|
175
|
+
}
|