directus

Форк
0
60 строк · 2.2 Кб
1
import type { Knex } from 'knex';
2
import type { FnHelperOptions } from '../types.js';
3
import { FnHelper } from '../types.js';
4

5
const parseLocaltime = (columnType?: string) => {
6
	if (columnType === 'timestamp') {
7
		return ` AT TIME ZONE 'UTC'`;
8
	}
9

10
	return '';
11
};
12

13
export class FnHelperMSSQL extends FnHelper {
14
	year(table: string, column: string, options: FnHelperOptions): Knex.Raw {
15
		return this.knex.raw(`DATEPART(year, ??.??${parseLocaltime(options?.type)})`, [table, column]);
16
	}
17

18
	month(table: string, column: string, options: FnHelperOptions): Knex.Raw {
19
		return this.knex.raw(`DATEPART(month, ??.??${parseLocaltime(options?.type)})`, [table, column]);
20
	}
21

22
	week(table: string, column: string, options: FnHelperOptions): Knex.Raw {
23
		return this.knex.raw(`DATEPART(week, ??.??${parseLocaltime(options?.type)})`, [table, column]);
24
	}
25

26
	day(table: string, column: string, options: FnHelperOptions): Knex.Raw {
27
		return this.knex.raw(`DATEPART(day, ??.??${parseLocaltime(options?.type)})`, [table, column]);
28
	}
29

30
	weekday(table: string, column: string, options: FnHelperOptions): Knex.Raw {
31
		return this.knex.raw(`DATEPART(weekday, ??.??${parseLocaltime(options?.type)})`, [table, column]);
32
	}
33

34
	hour(table: string, column: string, options: FnHelperOptions): Knex.Raw {
35
		return this.knex.raw(`DATEPART(hour, ??.??${parseLocaltime(options?.type)})`, [table, column]);
36
	}
37

38
	minute(table: string, column: string, options: FnHelperOptions): Knex.Raw {
39
		return this.knex.raw(`DATEPART(minute, ??.??${parseLocaltime(options?.type)})`, [table, column]);
40
	}
41

42
	second(table: string, column: string, options: FnHelperOptions): Knex.Raw {
43
		return this.knex.raw(`DATEPART(second, ??.??${parseLocaltime(options?.type)})`, [table, column]);
44
	}
45

46
	count(table: string, column: string, options?: FnHelperOptions): Knex.Raw<any> {
47
		const collectionName = options?.originalCollectionName || table;
48
		const type = this.schema.collections?.[collectionName]?.fields?.[column]?.type ?? 'unknown';
49

50
		if (type === 'json') {
51
			return this.knex.raw(`(SELECT COUNT(*) FROM OPENJSON(??.??, '$'))`, [table, column]);
52
		}
53

54
		if (type === 'alias') {
55
			return this._relationalCount(table, column, options);
56
		}
57

58
		throw new Error(`Couldn't extract type from ${table}.${column}`);
59
	}
60
}
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.