-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c14aa36
commit b688fa1
Showing
5 changed files
with
65 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { OptionalTypeString, TypeString } from "./IStrongPG"; | ||
import Schema, { DatabaseSchema, FunctionParameters, FunctionSchema, TableSchema } from "./Schema"; | ||
import { SelectFromVirtualTable } from "./statements/Select"; | ||
|
||
export type FunctionOutput<SCHEMA extends DatabaseSchema, FUNCTION extends FunctionSchema, FUNCTION_NAME extends DatabaseSchema.FunctionName<SCHEMA>> = | ||
FUNCTION extends FunctionSchema<(TypeString | OptionalTypeString)[], infer OUT, infer RETURN> ? | ||
{ [I in keyof OUT as OUT[I] extends [TypeString, infer NAME extends PropertyKey] ? NAME : never]: OUT[I] extends [infer TYPE extends TypeString, string] ? TYPE : never } extends infer OUT_COLUMNS ? | ||
RETURN extends `SETOF ${infer TABLE_NAME extends DatabaseSchema.TableName<SCHEMA>}` ? | ||
DatabaseSchema.Table<SCHEMA, TABLE_NAME> extends infer TABLE ? | ||
|
||
OUT["length"] extends 0 ? TABLE | ||
: { [KEY in keyof TABLE | keyof OUT_COLUMNS]: KEY extends keyof OUT_COLUMNS ? OUT_COLUMNS[KEY] : KEY extends keyof TABLE ? Extract<TABLE[KEY], TypeString> : never } | ||
|
||
: never | ||
: (OUT["length"] extends 0 ? { [KEY in FUNCTION_NAME]: RETURN } : "Error! Function return type must be a table if there are OUT parameters") | ||
: never | ||
: never | ||
|
||
interface FunctionCall<FUNCTION extends FunctionSchema, SCHEMA extends DatabaseSchema, FUNCTION_NAME extends DatabaseSchema.FunctionName<SCHEMA>, OUTPUT = FunctionOutput<SCHEMA, FUNCTION, FUNCTION_NAME>> { | ||
select<COLUMNS extends Schema.Column<OUTPUT>[]> (...columns: COLUMNS): SelectFromVirtualTable<Extract<OUTPUT, TableSchema>, never, COLUMNS> | ||
} | ||
|
||
function FunctionCall<FUNCTION extends FunctionSchema, SCHEMA extends DatabaseSchema, FUNCTION_NAME extends DatabaseSchema.FunctionName<SCHEMA>> (name: FUNCTION_NAME, params: FunctionParameters<DatabaseSchema.Function<SCHEMA, FUNCTION_NAME>>): FunctionCall<FUNCTION, SCHEMA, FUNCTION_NAME> { | ||
return { | ||
select: (...columns) => new SelectFromVirtualTable<never, never, never>(name, columns as never) as never, | ||
} | ||
} | ||
|
||
export default FunctionCall | ||
|
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