Skip to content

Handle column renaming in makeQueryTemplate and __table #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 1, 2023
Merged
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
27 changes: 26 additions & 1 deletion src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ export function makeQueryTemplate(operations, source) {
throw new Error("missing from table");
if (select.columns && select.columns.length === 0)
throw new Error("at least one column must be selected");
const columns = select.columns ? select.columns.map(escaper).join(", ") : "*";
const names = new Map(operations.names?.map(({column, name}) => [column, name]));
const columns = select.columns ? select.columns.map((column) => {
const override = names.get(column);
return override ? `${escaper(column)} AS ${escaper(override)}` : escaper(column);
}).join(", ") : "*";
const args = [
[`SELECT ${columns} FROM ${formatTable(from.table, escaper)}`]
];
Expand Down Expand Up @@ -659,6 +663,27 @@ export function __table(source, operations) {
Object.fromEntries(operations.select.columns.map((c) => [c, d[c]]))
);
}
if (!primitive && operations.names) {
Copy link
Contributor

@libbey-observable libbey-observable Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice, glad it was an easy fix!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the real fix for the original issue you found is actually in this change in Next! but while i was investigating i realized we should also add a check here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaah! thanks for explaining that.

const overridesByName = new Map(operations.names.map((n) => [n.column, n]));
if (schema) {
schema = schema.map((s) => {
const override = overridesByName.get(s.name);
return ({...s, ...(override ? {name: override.name} : null)});
});
}
if (columns) {
columns = columns.map((c) => {
const override = overridesByName.get(c);
return override?.name ?? c;
});
}
source = source.map((d) =>
Object.fromEntries(Object.keys(d).map((k) => {
const override = overridesByName.get(k);
return [override?.name ?? k, d[k]];
}))
);
}
if (primitive) source = source.map((d) => d.value);
if (source !== input) {
if (schema) source.schema = schema;
Expand Down
41 changes: 41 additions & 0 deletions test/table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const baseOperations = {
}
};

function escape(identifier) {
return `\`${identifier.replace(/`/g, "``")}\``;
}

describe("makeQueryTemplate", () => {
it("makeQueryTemplate null table", () => {
const source = {};
Expand Down Expand Up @@ -437,6 +441,25 @@ describe("makeQueryTemplate", () => {
);
assert.deepStrictEqual(params, ["val1", "val2"]);
});

it("makeQueryTemplate names", () => {
const source = {name: "db", dialect: "mysql", escape};
let operations = {
...baseOperations,
select: {
columns: ["col1", "col2", "col3"]
},
names: [
{column: "col1", name: "name1"},
{column: "col2", name: "name2"},
{column: "col3", name: "name3"}
]
};

const [parts, ...params] = makeQueryTemplate(operations, source);
assert.deepStrictEqual(parts.join("?"), "SELECT `col1` AS `name1`, `col2` AS `name2`, `col3` AS `name3` FROM `table1`");
assert.deepStrictEqual(params, []);
});
});

describe("__table", () => {
Expand Down Expand Up @@ -585,6 +608,24 @@ describe("__table", () => {
[{name: "a", type: "number"}, {name: "b", type: "number"}, {name: "c", type: "number"}]
);
});

it("__table names", () => {
const operations = {
...EMPTY_TABLE_DATA.operations,
names: [{column: "a", name: "nameA"}]
};
assert.deepStrictEqual(__table(source, operations), [{nameA: 1, b: 2, c: 3}, {nameA: 2, b: 4, c: 6}, {nameA: 3, b: 6, c: 9}]);
source.columns = ["a", "b", "c"];
assert.deepStrictEqual(
__table(source, operations).columns,
["nameA", "b", "c"]
);
source.schema = [{name: "a", type: "number"}, {name: "b", type: "number"}, {name: "c", type: "number"}];
assert.deepStrictEqual(
__table(source, operations).schema,
[{name: "nameA", type: "number"}, {name: "b", type: "number"}, {name: "c", type: "number"}]
);
});
});

describe("getTypeValidator filters accurately", () => {
Expand Down