Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/workerd/server/d1/d1.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,43 @@ function* dumpSql(db, { noSchema, noData, tables: only }) {
}
}

// shell.c.in's output_quoted_escaped_string.
// shell.c.in's output_quoted_escaped_string. A value that already contains
// the literal `\n` (or `\r`) must keep those occurrences: the replace()
// wrapper below rewrites every occurrence of its escape, so the escape is
// picked from `\n`, `\012`, `(\n0)`, `(\n1)`, ... (and `\r`, `\015`,
// `(\r0)`, ...) until one is found that the value does not contain.
// Ported from Miniflare's dumpSql.ts (workers-sdk#15334), which ports the
// same shell.c.in code.
function quote(text) {
let lf = false;
let cr = false;
let out = `'${text.replace(/'|(\n)|(\r)/g, (_, n, r) => {
if (n) return ((lf = true), '\\n');
if (r) return ((cr = true), '\\r');
return "''";
})}'`;
if (cr) out = `replace(${out},'\\r',char(13))`;
if (lf) out = `replace(${out},'\\n',char(10))`;
const hasLf = text.includes('\n');
const hasCr = text.includes('\r');
if (!hasLf && !hasCr) return `'${text.replaceAll("'", "''")}'`;
const lf = hasLf ? unusedEscape(text, '\\n', '\\012') : '';
const cr = hasCr ? unusedEscape(text, '\\r', '\\015') : '';
const escaped = text
.replaceAll("'", "''")
.replaceAll('\n', lf)
.replaceAll('\r', cr);
let out = `'${escaped}'`;
if (hasCr) out = `replace(${out},'${cr}',char(13))`;
if (hasLf) out = `replace(${out},'${lf}',char(10))`;
return out;
}

// The first of `primary`, `secondary`, `(${primary}0)`, `(${primary}1)`,
// ... that does not occur in `text`, so the replace() wrapper in quote()
// only rewrites the escapes it inserted.
function unusedEscape(text, primary, secondary) {
if (!text.includes(primary)) return primary;
if (!text.includes(secondary)) return secondary;
let suffix = 0;
let candidate;
do {
candidate = `(${primary}${suffix++})`;
} while (text.includes(candidate));
return candidate;
}

const escapeId = (id) => `"${id.replace(/"/g, '""')}"`;

// The end of the last statement in `text`. SQLite refuses trailing
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/server/tests/d1/d1-export-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const FIXTURE = [
"INSERT INTO plain VALUES (1, 'it''s', 1.5, X'00ff10', NULL)",
"INSERT INTO plain VALUES (2, 'line1\nline2\rend', 9e999, X'', 'x')",
"INSERT INTO plain VALUES (3, '', -9e999, NULL, 42)",
"INSERT INTO plain VALUES (4, 'a\nb\\nc\rd\\re', 0, NULL, NULL)",
'INSERT INTO "Quoted ""T""" ("select") VALUES (\'a\'), (\'b\')',
'INSERT INTO child VALUES (1, 1)',
'CREATE INDEX plain_t ON plain (t)',
Expand All @@ -29,6 +30,7 @@ const GOLDEN = {
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(1,\'it\'\'s\',1.5,X\'00ff10\',NULL);',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(2,replace(replace(\'line1\\nline2\\rend\',\'\\r\',char(13)),\'\\n\',char(10)),9e999,X\'\',\'x\');',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(3,\'\',-9e999,NULL,42);',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(4,replace(replace(\'a\\012b\\nc\\015d\\re\',\'\\015\',char(13)),\'\\012\',char(10)),0,NULL,NULL);',
'CREATE TABLE IF NOT EXISTS "Quoted ""T""" (id INTEGER PRIMARY KEY AUTOINCREMENT, "select" TEXT);',
'INSERT INTO "Quoted ""T""" ("id","select") VALUES(1,\'a\');',
'INSERT INTO "Quoted ""T""" ("id","select") VALUES(2,\'b\');',
Expand All @@ -45,6 +47,7 @@ const GOLDEN = {
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(1,\'it\'\'s\',1.5,X\'00ff10\',NULL);',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(2,replace(replace(\'line1\\nline2\\rend\',\'\\r\',char(13)),\'\\n\',char(10)),9e999,X\'\',\'x\');',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(3,\'\',-9e999,NULL,42);',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(4,replace(replace(\'a\\012b\\nc\\015d\\re\',\'\\015\',char(13)),\'\\012\',char(10)),0,NULL,NULL);',
'INSERT INTO "Quoted ""T""" ("id","select") VALUES(1,\'a\');',
'INSERT INTO "Quoted ""T""" ("id","select") VALUES(2,\'b\');',
'INSERT INTO "child" ("id","p") VALUES(1,1);',
Expand All @@ -66,6 +69,7 @@ const GOLDEN = {
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(1,\'it\'\'s\',1.5,X\'00ff10\',NULL);',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(2,replace(replace(\'line1\\nline2\\rend\',\'\\r\',char(13)),\'\\n\',char(10)),9e999,X\'\',\'x\');',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(3,\'\',-9e999,NULL,42);',
'INSERT INTO "plain" ("id","t","r","b","n") VALUES(4,replace(replace(\'a\\012b\\nc\\015d\\re\',\'\\015\',char(13)),\'\\012\',char(10)),0,NULL,NULL);',
],
};

Expand Down
Loading