From 3ed422145d536bf6687549032039a836b64ff30d Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Thu, 22 Feb 2024 12:18:51 +0000 Subject: [PATCH] Add Dictionary import and fix Statement.toColumnType method --- .../Utils/AnalyticsDatabase/Statement.ts | 27 ++++++++++++------- .../AnalyticsDatabase/StatementGenerator.ts | 6 +++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CommonServer/Utils/AnalyticsDatabase/Statement.ts b/CommonServer/Utils/AnalyticsDatabase/Statement.ts index fa358cbefc..a7fae00db0 100644 --- a/CommonServer/Utils/AnalyticsDatabase/Statement.ts +++ b/CommonServer/Utils/AnalyticsDatabase/Statement.ts @@ -9,6 +9,7 @@ import LessThan from 'Common/Types/BaseDatabase/LessThan'; import LessThanOrEqual from 'Common/Types/BaseDatabase/LessThanOrEqual'; import Search from 'Common/Types/BaseDatabase/Search'; import OneUptimeDate from 'Common/Types/Date'; +import Dictionary from 'Common/Types/Dictionary'; import ObjectID from 'Common/Types/ObjectID'; import { inspect } from 'util'; @@ -31,11 +32,13 @@ export class Statement implements BaseQueryParams { let query: string = this.strings.reduce( (prev: string, curr: string, i: integer) => { const param: StatementParameter | string = this.values[i - 1]!; + const dataType: string = typeof param === 'string' ? 'Identifier' - : Statement.toColumnType(param.type); - return prev + `{p${i - 1}:${dataType}}` + curr; + : Statement.toColumnType(param); + + return prev + `{p${i - 1}:${dataType}}` + curr; } ); @@ -102,10 +105,10 @@ export class Statement implements BaseQueryParams { ) { finalValue = v.value.value; } else if (v.value instanceof Includes) { - if (v.type === TableColumnType.Text) { + if (v.type === TableColumnType.Text || v.type === TableColumnType.ObjectID) { finalValue = v.value.values.map((val: string | ObjectID) => { - return `'${val.toString()}'`; - }); + return `${val.toString()}`; + }) } else { finalValue = v.value.values; } @@ -122,8 +125,6 @@ export class Statement implements BaseQueryParams { finalValue = OneUptimeDate.toDatabaseDate(finalValue); } - debugger; - return finalValue; } @@ -150,10 +151,10 @@ export class Statement implements BaseQueryParams { })}`; } - private static toColumnType(type: TableColumnType): string { + private static toColumnType(statementParam: StatementParameter | string): string { // ensure we have a mapping for all types (a missing mapping will // be a compile error) - return { + const columnTypes: Dictionary = { [TableColumnType.Text]: 'String', [TableColumnType.ObjectID]: 'String', [TableColumnType.Boolean]: 'Bool', @@ -165,7 +166,13 @@ export class Statement implements BaseQueryParams { [TableColumnType.ArrayNumber]: 'Array(Int32)', [TableColumnType.ArrayText]: 'Array(String)', [TableColumnType.LongNumber]: 'Int128', - }[type]; + }; + + if((statementParam as StatementParameter).value instanceof Includes){ + return 'Array(String)'; + } + + return columnTypes[(statementParam as StatementParameter).type] || 'String'; } } diff --git a/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts b/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts index 50052b60b8..2d0951214d 100644 --- a/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts +++ b/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts @@ -391,10 +391,10 @@ export default class StatementGenerator { ); } else if (value instanceof Includes) { whereStatement.append( - SQL`AND ${key} IN (${{ + SQL`AND ${key} IN ${{ value: value, type: tableColumn.type, - }})` + }}` ); } else if (value instanceof IsNull) { whereStatement.append(SQL`AND ${key} IS NULL`); @@ -405,6 +405,8 @@ export default class StatementGenerator { } } + debugger; + return whereStatement; }