-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(functions): reorganize operations functions (#1074)
- Loading branch information
1 parent
464513a
commit c58dcdb
Showing
17 changed files
with
180 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { MigrationOptions } from '../../types'; | ||
import { escapeValue, formatParams } from '../../utils'; | ||
import type { DropOptions, Name, Value } from '../generalTypes'; | ||
import { dropFunction } from './dropFunction'; | ||
import type { FunctionOptions, FunctionParam } from './shared'; | ||
|
||
export type CreateFunctionFn = ( | ||
functionName: Name, | ||
functionParams: FunctionParam[], | ||
functionOptions: FunctionOptions & DropOptions, | ||
definition: Value | ||
) => string | string[]; | ||
|
||
export type CreateFunction = CreateFunctionFn & { reverse: CreateFunctionFn }; | ||
|
||
export function createFunction(mOptions: MigrationOptions): CreateFunction { | ||
const _create: CreateFunction = ( | ||
functionName, | ||
functionParams = [], | ||
functionOptions, | ||
definition | ||
) => { | ||
const { | ||
replace, | ||
returns = 'void', | ||
language, | ||
window, | ||
behavior = 'VOLATILE', | ||
onNull, | ||
parallel, | ||
} = functionOptions; | ||
|
||
const options: string[] = []; | ||
|
||
if (behavior) { | ||
options.push(behavior); | ||
} | ||
|
||
if (language) { | ||
options.push(`LANGUAGE ${language}`); | ||
} else { | ||
throw new Error( | ||
`Language for function ${functionName} have to be specified` | ||
); | ||
} | ||
|
||
if (window) { | ||
options.push('WINDOW'); | ||
} | ||
|
||
if (onNull) { | ||
options.push('RETURNS NULL ON NULL INPUT'); | ||
} | ||
|
||
if (parallel) { | ||
options.push(`PARALLEL ${parallel}`); | ||
} | ||
|
||
const replaceStr = replace ? ' OR REPLACE' : ''; | ||
const paramsStr = formatParams(functionParams, mOptions); | ||
const functionNameStr = mOptions.literal(functionName); | ||
|
||
return `CREATE${replaceStr} FUNCTION ${functionNameStr}${paramsStr} | ||
RETURNS ${returns} | ||
AS ${escapeValue(definition)} | ||
${options.join('\n ')};`; | ||
}; | ||
|
||
_create.reverse = dropFunction(mOptions); | ||
|
||
return _create; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { MigrationOptions } from '../../types'; | ||
import { formatParams } from '../../utils'; | ||
import type { DropOptions, Name } from '../generalTypes'; | ||
import type { FunctionParam } from './shared'; | ||
|
||
export type DropFunction = ( | ||
functionName: Name, | ||
functionParams: FunctionParam[], | ||
dropOptions?: DropOptions | ||
) => string | string[]; | ||
|
||
export function dropFunction(mOptions: MigrationOptions): DropFunction { | ||
const _drop: DropFunction = ( | ||
functionName, | ||
functionParams = [], | ||
options = {} | ||
) => { | ||
const { ifExists, cascade } = options; | ||
|
||
const ifExistsStr = ifExists ? ' IF EXISTS' : ''; | ||
const cascadeStr = cascade ? ' CASCADE' : ''; | ||
const paramsStr = formatParams(functionParams, mOptions); | ||
const functionNameStr = mOptions.literal(functionName); | ||
|
||
return `DROP FUNCTION${ifExistsStr} ${functionNameStr}${paramsStr}${cascadeStr};`; | ||
}; | ||
|
||
return _drop; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './createFunction'; | ||
export * from './dropFunction'; | ||
export * from './renameFunction'; | ||
export * from './shared'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { MigrationOptions } from '../../types'; | ||
import { formatParams } from '../../utils'; | ||
import type { Name } from '../generalTypes'; | ||
import type { FunctionParam } from './shared'; | ||
|
||
export type RenameFunctionFn = ( | ||
oldFunctionName: Name, | ||
functionParams: FunctionParam[], | ||
newFunctionName: Name | ||
) => string | string[]; | ||
|
||
export type RenameFunction = RenameFunctionFn & { reverse: RenameFunctionFn }; | ||
|
||
export function renameFunction(mOptions: MigrationOptions): RenameFunction { | ||
const _rename: RenameFunction = ( | ||
oldFunctionName, | ||
functionParams = [], | ||
newFunctionName | ||
) => { | ||
const paramsStr = formatParams(functionParams, mOptions); | ||
const oldFunctionNameStr = mOptions.literal(oldFunctionName); | ||
const newFunctionNameStr = mOptions.literal(newFunctionName); | ||
|
||
return `ALTER FUNCTION ${oldFunctionNameStr}${paramsStr} RENAME TO ${newFunctionNameStr};`; | ||
}; | ||
|
||
_rename.reverse = (oldFunctionName, functionParams, newFunctionName) => | ||
_rename(newFunctionName, functionParams, oldFunctionName); | ||
|
||
return _rename; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { Value } from '../generalTypes'; | ||
|
||
export interface FunctionParamType { | ||
mode?: 'IN' | 'OUT' | 'INOUT' | 'VARIADIC'; | ||
|
||
name?: string; | ||
|
||
type: string; | ||
|
||
default?: Value; | ||
} | ||
|
||
export type FunctionParam = string | FunctionParamType; | ||
|
||
export interface FunctionOptions { | ||
returns?: string; | ||
|
||
language: string; | ||
|
||
replace?: boolean; | ||
|
||
window?: boolean; | ||
|
||
behavior?: 'IMMUTABLE' | 'STABLE' | 'VOLATILE'; | ||
|
||
onNull?: boolean; | ||
|
||
parallel?: 'UNSAFE' | 'RESTRICTED' | 'SAFE'; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.