Adding handling for columns with dots in them.

This commit is contained in:
mike12345567 2024-02-27 17:15:20 +00:00
parent abf68fdd36
commit 35c1f5bbce
3 changed files with 17 additions and 5 deletions

View File

@ -2,6 +2,10 @@ SELECT 'CREATE DATABASE main'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec
CREATE SCHEMA "test-1";
CREATE TYPE person_job AS ENUM ('qa', 'programmer', 'designer', 'support');
CREATE TABLE "Bad.Table" (
BadID SERIAL PRIMARY KEY,
"Bad.Column" text
);
CREATE TABLE Persons (
PersonID SERIAL PRIMARY KEY,
LastName varchar(255),

View File

@ -55,11 +55,10 @@ export default class AliasTables {
aliasField(field: string) {
const tableNames = this.tableNames
if (field.includes(".")) {
const [tableName, column] = field.split(".")
if (tableNames.includes(tableName)) {
return `${this.getAlias(tableName)}.${column}`
}
const foundTable = tableNames.find(name => field.includes(name))
if (foundTable) {
const aliasedTable = this.getAlias(foundTable)
return field.replace(foundTable, aliasedTable)
}
return field
}

View File

@ -174,4 +174,13 @@ describe("Captures of real examples", () => {
expect(alias).toEqual("cv")
})
})
describe("check some edge cases", () => {
it("should handle table names/columns with dots in them", () => {
const tableNames = ["hello.world", "foo.bar.baz"]
const aliasing = new AliasTables(tableNames)
const aliased = aliasing.aliasField("hello.world.field")
expect(aliased).toEqual("a.field")
})
})
})