-
Notifications
You must be signed in to change notification settings - Fork 180
/
createOperatorClass.ts
59 lines (50 loc) · 1.76 KB
/
createOperatorClass.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { MigrationOptions } from '../../types';
import { applyType } from '../../utils';
import type { Name, Reversible, Type } from '../generalTypes';
import type { DropOperatorClassOptions } from './dropOperatorClass';
import { dropOperatorClass } from './dropOperatorClass';
import type { OperatorListDefinition } from './shared';
import { operatorMap } from './shared';
export interface CreateOperatorClassOptions {
default?: boolean;
family?: string;
}
export type CreateOperatorClassFn = (
operatorClassName: Name,
type: Type,
indexMethod: Name,
operatorList: OperatorListDefinition[],
operatorClassOptions: CreateOperatorClassOptions & DropOperatorClassOptions
) => string;
export type CreateOperatorClass = Reversible<CreateOperatorClassFn>;
export function createOperatorClass(
mOptions: MigrationOptions
): CreateOperatorClass {
const _create: CreateOperatorClass = (
operatorClassName,
type,
indexMethod,
operatorList,
options
) => {
const { default: isDefault, family } = options;
const operatorClassNameStr = mOptions.literal(operatorClassName);
const defaultStr = isDefault ? ' DEFAULT' : '';
const typeStr = mOptions.literal(applyType(type).type);
const indexMethodStr = mOptions.literal(indexMethod);
const familyStr = family ? ` FAMILY ${family}` : '';
const operatorListStr = operatorList
.map(operatorMap(mOptions))
.join(',\n ');
return `CREATE OPERATOR CLASS ${operatorClassNameStr}${defaultStr} FOR TYPE ${typeStr} USING ${indexMethodStr}${familyStr} AS
${operatorListStr};`;
};
_create.reverse = (
operatorClassName,
type,
indexMethod,
operatorList,
options
) => dropOperatorClass(mOptions)(operatorClassName, indexMethod, options);
return _create;
}