From 087a37bbbcdafd245fbef3fc5726e3f83068d184 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Wed, 19 Apr 2023 13:40:06 +0800 Subject: [PATCH] refacotr(nocodb): move the logic out as convertDateFormatByType --- .../lib/sql/helpers/formulaFnHelper.ts | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/helpers/formulaFnHelper.ts b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/helpers/formulaFnHelper.ts index 5a21ba3261..6e71ba9df1 100644 --- a/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/helpers/formulaFnHelper.ts +++ b/packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/helpers/formulaFnHelper.ts @@ -54,6 +54,25 @@ export function validateDateWithUnknownFormat(v: string) { return false; } +export function convertDateFormatByType(query, clientType, dateFormat) { + if (clientType === 'mysql2') { + query = `DATE_FORMAT(${query}, '${convertDateFormat( + dateFormat, + clientType + )}')`; + } else if (clientType === 'pg') { + query = `TO_CHAR(${query}, '${convertDateFormat(dateFormat, clientType)}')`; + } else if (clientType === 'sqlite3') { + query = `strftime('${convertDateFormat( + dateFormat, + clientType + )}', ${query})`; + } else if (clientType === 'mssql') { + query = `FORMAT(${query}, '${convertDateFormat(dateFormat, clientType)}')`; + } + return query; +} + export async function convertDateFormatForConcat( o, columnIdToUidt, @@ -71,27 +90,7 @@ export async function convertDateFormatForConcat( }) ).meta; - if (clientType === 'mysql2') { - query = `DATE_FORMAT(${query}, '${convertDateFormat( - meta.date_format, - clientType - )}')`; - } else if (clientType === 'pg') { - query = `TO_CHAR(${query}, '${convertDateFormat( - meta.date_format, - clientType - )}')`; - } else if (clientType === 'sqlite3') { - query = `strftime('${convertDateFormat( - meta.date_format, - clientType - )}', ${query})`; - } else if (clientType === 'mssql') { - query = `FORMAT(${query}, '${convertDateFormat( - meta.date_format, - clientType - )}')`; - } + return convertDateFormatByType(query, clientType, meta.date_format); } return query; }