Adding handling for columns with dots in them.
This commit is contained in:
parent
abf68fdd36
commit
35c1f5bbce
|
@ -2,6 +2,10 @@ SELECT 'CREATE DATABASE main'
|
||||||
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec
|
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec
|
||||||
CREATE SCHEMA "test-1";
|
CREATE SCHEMA "test-1";
|
||||||
CREATE TYPE person_job AS ENUM ('qa', 'programmer', 'designer', 'support');
|
CREATE TYPE person_job AS ENUM ('qa', 'programmer', 'designer', 'support');
|
||||||
|
CREATE TABLE "Bad.Table" (
|
||||||
|
BadID SERIAL PRIMARY KEY,
|
||||||
|
"Bad.Column" text
|
||||||
|
);
|
||||||
CREATE TABLE Persons (
|
CREATE TABLE Persons (
|
||||||
PersonID SERIAL PRIMARY KEY,
|
PersonID SERIAL PRIMARY KEY,
|
||||||
LastName varchar(255),
|
LastName varchar(255),
|
||||||
|
|
|
@ -55,11 +55,10 @@ export default class AliasTables {
|
||||||
|
|
||||||
aliasField(field: string) {
|
aliasField(field: string) {
|
||||||
const tableNames = this.tableNames
|
const tableNames = this.tableNames
|
||||||
if (field.includes(".")) {
|
const foundTable = tableNames.find(name => field.includes(name))
|
||||||
const [tableName, column] = field.split(".")
|
if (foundTable) {
|
||||||
if (tableNames.includes(tableName)) {
|
const aliasedTable = this.getAlias(foundTable)
|
||||||
return `${this.getAlias(tableName)}.${column}`
|
return field.replace(foundTable, aliasedTable)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return field
|
return field
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,4 +174,13 @@ describe("Captures of real examples", () => {
|
||||||
expect(alias).toEqual("cv")
|
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")
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue