Browse Source

feat: `CreateTime` or `LastModifiedTime` - select query - singleQuery

pull/7304/head
Pranav C 12 months ago
parent
commit
d000252ac1
  1. 22
      packages/nocodb/src/db/BaseModelSqlv2.ts

22
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -2126,8 +2126,9 @@ class BaseModelSqlv2 {
switch (column.uidt) { switch (column.uidt) {
case UITypes.CreateTime: case UITypes.CreateTime:
case UITypes.LastModifiedTime: case UITypes.LastModifiedTime:
case UITypes.DateTime: { case UITypes.DateTime:
const columnName = getColumnName(column) ; {
const columnName = getColumnName(column);
if (this.isMySQL) { if (this.isMySQL) {
// MySQL stores timestamp in UTC but display in timezone // MySQL stores timestamp in UTC but display in timezone
// To verify the timezone, run `SELECT @@global.time_zone, @@session.time_zone;` // To verify the timezone, run `SELECT @@global.time_zone, @@session.time_zone;`
@ -2137,9 +2138,9 @@ class BaseModelSqlv2 {
// the value 2023-01-01 10:00:00 (UTC) would display as 2023-01-01 18:00:00 (UTC+8) // the value 2023-01-01 10:00:00 (UTC) would display as 2023-01-01 18:00:00 (UTC+8)
// our existing logic is based on UTC, during the query, we need to take the UTC value // our existing logic is based on UTC, during the query, we need to take the UTC value
// hence, we use CONVERT_TZ to convert back to UTC value // hence, we use CONVERT_TZ to convert back to UTC value
res[sanitize(column.id || column.column_name)] = this.dbDriver.raw( res[sanitize(column.id || columnName)] = this.dbDriver.raw(
`CONVERT_TZ(??, @@GLOBAL.time_zone, '+00:00')`, `CONVERT_TZ(??, @@GLOBAL.time_zone, '+00:00')`,
[`${sanitize(alias || this.tnPath)}.${column.column_name}`], [`${sanitize(alias || this.tnPath)}.${columnName}`],
); );
break; break;
} else if (this.isPg) { } else if (this.isPg) {
@ -2150,10 +2151,10 @@ class BaseModelSqlv2 {
column.dt !== 'timestamp with time zone' && column.dt !== 'timestamp with time zone' &&
column.dt !== 'timestamptz' column.dt !== 'timestamptz'
) { ) {
res[sanitize(column.id || column.column_name)] = this.dbDriver res[sanitize(column.id || columnName)] = this.dbDriver
.raw( .raw(
`?? AT TIME ZONE CURRENT_SETTING('timezone') AT TIME ZONE 'UTC'`, `?? AT TIME ZONE CURRENT_SETTING('timezone') AT TIME ZONE 'UTC'`,
[`${sanitize(alias || this.tnPath)}.${column.column_name}`], [`${sanitize(alias || this.tnPath)}.${columnName}`],
) )
.wrap('(', ')'); .wrap('(', ')');
break; break;
@ -2163,16 +2164,15 @@ class BaseModelSqlv2 {
// convert to database timezone, // convert to database timezone,
// then convert to UTC // then convert to UTC
if (column.dt !== 'datetimeoffset') { if (column.dt !== 'datetimeoffset') {
res[sanitize(column.id || column.column_name)] = res[sanitize(column.id || columnName)] = this.dbDriver.raw(
this.dbDriver.raw(
`CONVERT(DATETIMEOFFSET, ?? AT TIME ZONE 'UTC')`, `CONVERT(DATETIMEOFFSET, ?? AT TIME ZONE 'UTC')`,
[`${sanitize(alias || this.tnPath)}.${column.column_name}`], [`${sanitize(alias || this.tnPath)}.${columnName}`],
); );
break; break;
} }
} }
res[sanitize(column.id || column.column_name)] = sanitize( res[sanitize(column.id || columnName)] = sanitize(
`${alias || this.tnPath}.${column.column_name}`, `${alias || this.tnPath}.${columnName}`,
); );
} }
break; break;

Loading…
Cancel
Save