1
0
Fork 0
mirror of synced 2024-07-01 20:41:03 +12:00

Attempting to fix issue with table name needing to be aliased.

This commit is contained in:
mike12345567 2024-03-04 16:55:10 +00:00
parent 02e3b36cd8
commit a7fcd7cca1
13 changed files with 35 additions and 55 deletions

View file

@ -10,6 +10,11 @@ CREATE TABLE Persons (
City varchar(255),
PRIMARY KEY (PersonID)
);
CREATE TABLE Person (
PersonID int NOT NULL AUTO_INCREMENT,
Name varchar(255),
PRIMARY KEY (PersonID)
);
CREATE TABLE Tasks (
TaskID int NOT NULL AUTO_INCREMENT,
PersonID INT,
@ -27,6 +32,7 @@ CREATE TABLE Products (
);
INSERT INTO Persons (FirstName, LastName, Age, Address, City, CreatedAt) VALUES ('Mike', 'Hughes', 28.2, '123 Fake Street', 'Belfast', '2021-01-19 03:14:07');
INSERT INTO Persons (FirstName, LastName, Age, Address, City, CreatedAt) VALUES ('Dave', 'Johnson', 29, '124 Fake Street', 'Belfast', '2022-04-01 00:11:11');
INSERT INTO Person (Name) VALUES ('Elf');
INSERT INTO Tasks (PersonID, TaskName, CreatedAt) VALUES (1, 'assembling', '2020-01-01');
INSERT INTO Tasks (PersonID, TaskName, CreatedAt) VALUES (2, 'processing', '2019-12-31');
INSERT INTO Products (name, updated) VALUES ('Meat', '11:00:22'), ('Fruit', '10:00:00');

View file

@ -62,7 +62,11 @@ export default class AliasTables {
if (idx === -1 || idx > 1) {
return
}
return Math.abs(tableName.length - name.length) <= 2
// this might look a bit mad, but the idea is if the field is wrapped, say in "", `` or []
// then the idx of the table name will be 1, and we should allow for it ending in a closing
// character - otherwise it should be the full length if the index is zero
const allowedCharacterDiff = idx * 2
return Math.abs(tableName.length - name.length) <= allowedCharacterDiff
})
if (foundTableName) {
const aliasedTableName = tableName.replace(
@ -111,11 +115,6 @@ export default class AliasTables {
const aliasingEnabled = fieldLength && fieldLength > 0
if (aliasingEnabled) {
json = cloneDeep(json)
const aliasTable = (table: Table) => ({
...table,
name: this.getAlias(table.name),
originalName: table.name,
})
// run through the query json to update anywhere a table may be used
if (json.resource?.fields) {
json.resource.fields = json.resource.fields.map(field =>
@ -134,6 +133,14 @@ export default class AliasTables {
json.filters[filterKey as keyof SearchFilters] = aliasedFilters
}
}
if (json.meta?.table) {
this.getAlias(json.meta.table.name)
}
if (json.meta?.tables) {
Object.keys(json.meta.tables).forEach(tableName =>
this.getAlias(tableName)
)
}
if (json.relationships) {
json.relationships = json.relationships.map(relationship => ({
...relationship,
@ -144,16 +151,6 @@ export default class AliasTables {
]),
}))
}
if (json.meta?.table) {
json.meta.table = aliasTable(json.meta.table)
}
if (json.meta?.tables) {
const aliasedTables: Record<string, Table> = {}
for (let [tableName, table] of Object.entries(json.meta.tables)) {
aliasedTables[this.getAlias(tableName)] = aliasTable(table)
}
json.meta.tables = aliasedTables
}
// invert and return
const invertedTableAliases: Record<string, string> = {}
for (let [key, value] of Object.entries(this.tableAliases)) {

View file

@ -120,29 +120,6 @@ function generateSelectStatement(
})
}
function disableAliasing(json: QueryJson) {
if (json.tableAliases) {
json.tableAliases = undefined
}
const removeTableAlias = (table: Table) => {
if (table.originalName) {
table.name = table.originalName
}
return table
}
if (json.meta?.table) {
json.meta.table = removeTableAlias(json.meta.table)
}
if (json.meta?.tables) {
for (let tableName of Object.keys(json.meta.tables)) {
json.meta.tables[tableName] = removeTableAlias(
json.meta.tables[tableName]
)
}
}
return json
}
class InternalBuilder {
private readonly client: string
@ -348,15 +325,18 @@ class InternalBuilder {
addSorting(query: Knex.QueryBuilder, json: QueryJson): Knex.QueryBuilder {
let { sort, paginate } = json
const table = json.meta?.table
const aliases = json.tableAliases
const aliased =
table?.name && aliases?.[table.name] ? aliases[table.name] : table?.name
if (sort && Object.keys(sort || {}).length > 0) {
for (let [key, value] of Object.entries(sort)) {
const direction =
value.direction === SortDirection.ASCENDING ? "asc" : "desc"
query = query.orderBy(`${table?.name}.${key}`, direction)
query = query.orderBy(`${aliased}.${key}`, direction)
}
} else if (this.client === SqlClient.MS_SQL && paginate?.limit) {
// @ts-ignore
query = query.orderBy(`${table?.name}.${table?.primary[0]}`)
query = query.orderBy(`${aliased}.${table?.primary[0]}`)
}
return query
}
@ -635,8 +615,6 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
if (!json.extra || !json.extra.idFilter) {
return {}
}
// disable aliasing if it is enabled
json = disableAliasing(json)
const input = this._query({
endpoint: {
...json.endpoint,

View file

@ -68,7 +68,7 @@
"primary": [
"personid"
],
"name": "a",
"name": "persons",
"schema": {
"year": {
"type": "number",

View file

@ -34,7 +34,7 @@
"name",
"age"
],
"name": "a",
"name": "people",
"schema": {
"name": {
"type": "string",
@ -55,8 +55,7 @@
}
}
},
"primaryDisplay": "name",
"originalName": "people"
"primaryDisplay": "name"
}
},
"tableAliases": {

View file

@ -58,7 +58,7 @@
"primary": [
"personid"
],
"name": "a",
"name": "persons",
"schema": {
"year": {
"type": "number",

View file

@ -34,7 +34,7 @@
"keypartone",
"keyparttwo"
],
"name": "a",
"name": "compositetable",
"schema": {
"keyparttwo": {
"type": "string",

View file

@ -49,7 +49,7 @@
"primary": [
"taskid"
],
"name": "a",
"name": "tasks",
"schema": {
"executorid": {
"type": "number",

View file

@ -63,7 +63,7 @@
"primary": [
"productid"
],
"name": "a",
"name": "products",
"schema": {
"productname": {
"type": "string",

View file

@ -53,7 +53,7 @@
"primary": [
"productid"
],
"name": "a",
"name": "products",
"schema": {
"productname": {
"type": "string",

View file

@ -109,7 +109,7 @@
"primary": [
"taskid"
],
"name": "a",
"name": "tasks",
"schema": {
"executorid": {
"type": "number",

View file

@ -66,7 +66,7 @@
"primary": [
"personid"
],
"name": "a",
"name": "persons",
"schema": {
"year": {
"type": "number",

View file

@ -66,7 +66,7 @@
"primary": [
"personid"
],
"name": "a",
"name": "persons",
"schema": {
"year": {
"type": "number",