Fix write query failing when a named parameter is called :sql (#2765)

Closes #2761
This commit is contained in:
Viraat Das 2026-06-10 20:15:03 -07:00 committed by GitHub
commit 3c1012dcc2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 79 additions and 20 deletions

View file

@ -27,16 +27,20 @@ window.datasetteSqlParameters = (() => {
manager.section
.querySelectorAll("[data-parameter-control]")
.forEach((control) => {
manager.parameterState.set(control.name, controlState(control));
manager.parameterState.set(
control.dataset.parameterName,
controlState(control)
);
});
}
function createControl(parameter, id, state) {
function createControl(parameter, id, state, namePrefix) {
const control = document.createElement(state.expanded ? "textarea" : "input");
control.id = id;
control.name = parameter;
control.name = `${namePrefix || ""}${parameter}`;
control.value = state.value;
control.setAttribute("data-parameter-control", "");
control.dataset.parameterName = parameter;
if (state.expanded) {
control.rows = 5;
} else {
@ -53,10 +57,16 @@ window.datasetteSqlParameters = (() => {
value,
selectionStart
) {
const replacement = createControl(control.name, control.id, {
value: value === undefined ? control.value : value,
expanded: expand,
});
const parameter = control.dataset.parameterName;
const replacement = createControl(
parameter,
control.id,
{
value: value === undefined ? control.value : value,
expanded: expand,
},
manager.namePrefix
);
button.textContent = expand ? "Collapse" : "Expand";
button.setAttribute("aria-expanded", expand ? "true" : "false");
control.replaceWith(replacement);
@ -64,7 +74,7 @@ window.datasetteSqlParameters = (() => {
if (selectionStart !== undefined && replacement.setSelectionRange) {
replacement.setSelectionRange(selectionStart, selectionStart);
}
manager.parameterState.set(replacement.name, controlState(replacement));
manager.parameterState.set(parameter, controlState(replacement));
}
function renderParameters(manager, parameters) {
@ -99,7 +109,7 @@ window.datasetteSqlParameters = (() => {
label.htmlFor = id;
label.textContent = parameter;
const control = createControl(parameter, id, state);
const control = createControl(parameter, id, state, manager.namePrefix);
row.append(label, control);
if (manager.allowExpand) {
@ -124,7 +134,10 @@ window.datasetteSqlParameters = (() => {
if (!control.matches || !control.matches("[data-parameter-control]")) {
return;
}
manager.parameterState.set(control.name, controlState(control));
manager.parameterState.set(
control.dataset.parameterName,
controlState(control)
);
});
if (!manager.allowExpand) {
@ -230,6 +243,7 @@ window.datasetteSqlParameters = (() => {
? section.dataset.allowExpand === "1"
: false
: options.allowExpand,
namePrefix: section ? section.dataset.parameterNamePrefix || "" : "",
parameterState: new Map(),
};
if (section) {