Browse Source

refactor: use getWeekdayByText instead

pull/2706/head
Wing-Kam Wong 2 years ago
parent
commit
3893b9d3f6
  1. 16
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mssql.ts
  2. 15
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mysql.ts
  3. 17
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/pg.ts
  4. 16
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/sqlite.ts

16
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mssql.ts

@ -1,5 +1,6 @@
import { MapFnArgs } from '../mapFunctionName'; import { MapFnArgs } from '../mapFunctionName';
import commonFns from './commonFns'; import commonFns from './commonFns';
import { getWeekdayByText } from '../helpers/formulaFnHelper';
const mssql = { const mssql = {
...commonFns, ...commonFns,
@ -111,19 +112,10 @@ const mssql = {
WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// DATEPART(WEEKDAY, DATE): sunday = 1, monday = 2, ..., saturday = 7 // DATEPART(WEEKDAY, DATE): sunday = 1, monday = 2, ..., saturday = 7
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
const m = {
monday: 0,
tuesday: 1,
wednesday: 2,
thursday: 3,
friday: 4,
saturday: 5,
sunday: 6,
};
return knex.raw( return knex.raw(
`(DATEPART(WEEKDAY, ${fn(pt.arguments[0])}) - 2 - ${ `(DATEPART(WEEKDAY, ${fn(pt.arguments[0])}) - 2 - ${getWeekdayByText(
m[pt?.arguments[1]?.value.toLowerCase()] || 0 pt?.arguments[1]?.value
} % 7 + 7) % 7 ${colAlias}` )} % 7 + 7) % 7 ${colAlias}`
); );
}, },
}; };

15
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mysql.ts

@ -1,5 +1,6 @@
import { MapFnArgs } from '../mapFunctionName'; import { MapFnArgs } from '../mapFunctionName';
import commonFns from './commonFns'; import commonFns from './commonFns';
import { getWeekdayByText } from '../helpers/formulaFnHelper';
const mysql2 = { const mysql2 = {
...commonFns, ...commonFns,
@ -57,18 +58,10 @@ const mysql2 = {
}, },
WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
const m = {
monday: 0,
tuesday: 1,
wednesday: 2,
thursday: 3,
friday: 4,
saturday: 5,
sunday: 6,
};
const offset = m[pt?.arguments[1]?.value.toLowerCase()] || 0;
return knex.raw( return knex.raw(
`(WEEKDAY(${fn(pt.arguments[0])}) - ${offset} % 7 + 7) % 7 ${colAlias}` `(WEEKDAY(${fn(pt.arguments[0])}) - ${getWeekdayByText(
pt?.arguments[1]?.value
)} % 7 + 7) % 7 ${colAlias}`
); );
}, },
}; };

17
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/pg.ts

@ -1,5 +1,6 @@
import { MapFnArgs } from '../mapFunctionName'; import { MapFnArgs } from '../mapFunctionName';
import commonFns from './commonFns'; import commonFns from './commonFns';
import { getWeekdayByText } from '../helpers/formulaFnHelper';
const pg = { const pg = {
...commonFns, ...commonFns,
@ -45,20 +46,10 @@ const pg = {
WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// isodow: the day of the week as Monday (1) to Sunday (7) // isodow: the day of the week as Monday (1) to Sunday (7)
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
const m = {
monday: 0,
tuesday: 1,
wednesday: 2,
thursday: 3,
friday: 4,
saturday: 5,
sunday: 6,
};
const offset = m[pt?.arguments[1]?.value.toLowerCase()] || 0;
return knex.raw( return knex.raw(
`(EXTRACT(ISODOW FROM ${fn( `(EXTRACT(ISODOW FROM ${fn(pt.arguments[0])}) - 1 - ${getWeekdayByText(
pt.arguments[0] pt?.arguments[1]?.value
)}) - 1 - ${offset} % 7 + 7) % 7 ${colAlias}` )} % 7 + 7) % 7 ${colAlias}`
); );
}, },
}; };

16
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/sqlite.ts

@ -1,5 +1,6 @@
import { MapFnArgs } from '../mapFunctionName'; import { MapFnArgs } from '../mapFunctionName';
import commonFns from './commonFns'; import commonFns from './commonFns';
import { getWeekdayByText } from '../helpers/formulaFnHelper';
const sqlite3 = { const sqlite3 = {
...commonFns, ...commonFns,
@ -79,19 +80,10 @@ const sqlite3 = {
WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// strftime('%w', date) - day of week 0 - 6 with Sunday == 0 // strftime('%w', date) - day of week 0 - 6 with Sunday == 0
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
const m = {
monday: 0,
tuesday: 1,
wednesday: 2,
thursday: 3,
friday: 4,
saturday: 5,
sunday: 6,
};
return knex.raw( return knex.raw(
`strftime('%w', ${fn(pt.arguments[0])}, 'weekday ${ `strftime('%w', ${fn(pt.arguments[0])}, 'weekday ${getWeekdayByText(
m[pt?.arguments[1]?.value.toLowerCase()] || 0 pt?.arguments[1]?.value
}')${colAlias}` )}')${colAlias}`
); );
}, },
}; };

Loading…
Cancel
Save