Skip to content

Commit

Permalink
Merge pull request #62 from AnyhowStep/tpt-insert-and-fetch
Browse files Browse the repository at this point in the history
Tpt insert and fetch
  • Loading branch information
AnyhowStep authored Dec 13, 2019
2 parents 225fdfb + a2fd75b commit 34264df
Show file tree
Hide file tree
Showing 79 changed files with 3,703 additions and 250 deletions.
24 changes: 24 additions & 0 deletions src/built-in-expr/built-in-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ export type BuiltInExpr_NonCorrelated<TypeT> =
| NonValueExpr_NonCorrelated<TypeT>
;

/**
* Workaround,
* https://github.com/microsoft/TypeScript/issues/35616#issuecomment-564894944
*/
export type BuiltInExpr_NonCorrelatedOrUndefined<TypeT> =
| BuiltInExpr_NonCorrelated<TypeT>
| undefined
;

/**
* We don't support subqueries because it's too complicated
* to check their `IUsedRef`
Expand All @@ -181,3 +190,18 @@ export type BuiltInExpr_MapCorrelated<
alias : string,
}>
;

/**
* Workaround,
* https://github.com/microsoft/TypeScript/issues/35616#issuecomment-564894944
*
* We don't support subqueries because it's too complicated
* to check their `IUsedRef`
*/
export type BuiltInExpr_MapCorrelatedOrUndefined<
ColumnMapT extends ColumnMap,
TypeT
> =
| BuiltInExpr_MapCorrelated<ColumnMapT, TypeT>
| undefined
;
24 changes: 24 additions & 0 deletions src/custom-expr/custom-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export type CustomExpr_NonCorrelated<TypeT> =
| NonValueExpr_NonCorrelated<TypeT>
;

/**
* Workaround,
* https://github.com/microsoft/TypeScript/issues/35616#issuecomment-564894944
*/
export type CustomExpr_NonCorrelatedOrUndefined<TypeT> =
| CustomExpr_NonCorrelated<TypeT>
| undefined
;

/**
* We don't support subqueries because it's too complicated
* to check their `IUsedRef`... For now.
Expand All @@ -33,3 +42,18 @@ export type CustomExpr_MapCorrelated<
alias : string,
}>
;

/**
* Workaround,
* https://github.com/microsoft/TypeScript/issues/35616#issuecomment-564894944
*
* We don't support subqueries because it's too complicated
* to check their `IUsedRef`... For now.
*/
export type CustomExpr_MapCorrelatedOrUndefined<
ColumnMapT extends ColumnMap,
TypeT
> =
| CustomExpr_MapCorrelated<ColumnMapT, TypeT>
| undefined
;
10 changes: 7 additions & 3 deletions src/design-pattern-log/util/execution/track-or-insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ILog} from "../../log";
import {IsolableInsertOneConnection} from "../../../execution";
import {PrimaryKey_Input} from "../../../primary-key";
import {TableUtil} from "../../../table";
import {CustomExpr_NonCorrelated} from "../../../custom-expr";
import {CustomExpr_NonCorrelated, CustomExpr_NonCorrelatedOrUndefined} from "../../../custom-expr";
import {Track, unsafeTrack} from "./unsafe-track";

export type TrackOrInsertRow<LogT extends ILog> =
Expand All @@ -15,7 +15,9 @@ export type TrackOrInsertRow<LogT extends ILog> =
*/
& {
readonly [columnAlias in LogT["trackedWithDefaultValue"][number]]? : (
CustomExpr_NonCorrelated<TableUtil.ColumnType<LogT["logTable"], columnAlias>>
CustomExpr_NonCorrelatedOrUndefined<
TableUtil.ColumnType<LogT["logTable"], columnAlias>
>
)
}
/**
Expand Down Expand Up @@ -50,7 +52,9 @@ export type TrackOrInsertRow<LogT extends ILog> =
LogT["doNotCopy"][number],
TableUtil.OptionalColumnAlias<LogT["logTable"]>
>]? : (
CustomExpr_NonCorrelated<TableUtil.ColumnType<LogT["logTable"], columnAlias>>
CustomExpr_NonCorrelatedOrUndefined<
TableUtil.ColumnType<LogT["logTable"], columnAlias>
>
)
}
;
Expand Down
10 changes: 7 additions & 3 deletions src/design-pattern-log/util/execution/unsafe-track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ILog} from "../../log";
import {IsolableInsertOneConnection, ExecutionUtil, SelectConnection} from "../../../execution";
import {PrimaryKey_Input} from "../../../primary-key";
import {TableUtil} from "../../../table";
import {CustomExpr_NonCorrelated} from "../../../custom-expr";
import {CustomExpr_NonCorrelated, CustomExpr_NonCorrelatedOrUndefined} from "../../../custom-expr";
import {fetchLatestOrDefault} from "./fetch-latest-or-default";
import {DefaultRow} from "./fetch-default";
import {escapeIdentifierWithDoubleQuotes} from "../../../sqlstring";
Expand All @@ -21,7 +21,9 @@ export type TrackRow<LogT extends ILog> =
*/
& {
readonly [columnAlias in LogT["trackedWithDefaultValue"][number]]? : (
CustomExpr_NonCorrelated<TableUtil.ColumnType<LogT["logTable"], columnAlias>>
CustomExpr_NonCorrelatedOrUndefined<
TableUtil.ColumnType<LogT["logTable"], columnAlias>
>
)
}
/**
Expand Down Expand Up @@ -67,7 +69,9 @@ export type TrackRow<LogT extends ILog> =
LogT["doNotCopy"][number],
TableUtil.OptionalColumnAlias<LogT["logTable"]>
>]? : (
CustomExpr_NonCorrelated<TableUtil.ColumnType<LogT["logTable"], columnAlias>>
CustomExpr_NonCorrelatedOrUndefined<
TableUtil.ColumnType<LogT["logTable"], columnAlias>
>
)
}
;
Expand Down
9 changes: 9 additions & 0 deletions src/design-pattern-table-per-type/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ Adds table-per-type inheritance.
+ Abstract vs Concrete
+ Incomplete vs Complete

-----

### Unsoundness

+ This utility is unsound!
+ It is possible for `insertAndFetch()` to fail during run-time,
if your inheritance hierarchy does not make sense to the library.


-----

### Notes
Expand Down
40 changes: 36 additions & 4 deletions src/design-pattern-table-per-type/table-per-type-impl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {ITablePerType, TablePerTypeData} from "./table-per-type";
import {ITablePerType, TablePerTypeData, InsertableTablePerType} from "./table-per-type";
import * as TablePerTypeUtil from "./util";
import {ITable} from "../table";
import {SelectConnection, ExecutionUtil} from "../execution";
import {TableWithPrimaryKey} from "../table";
import {SelectConnection, ExecutionUtil, IsolableInsertOneConnection} from "../execution";
import {WhereDelegate} from "../where-clause";
import {OnlyKnownProperties} from "../type-util";

export class TablePerType<DataT extends TablePerTypeData> implements ITablePerType<DataT> {
readonly childTable : DataT["childTable"];
Expand All @@ -13,6 +14,8 @@ export class TablePerType<DataT extends TablePerTypeData> implements ITablePerTy

readonly explicitAutoIncrementValueEnabled : DataT["explicitAutoIncrementValueEnabled"];

readonly insertAndFetchPrimaryKey : DataT["insertAndFetchPrimaryKey"];

readonly joins : ITablePerType["joins"];

constructor (
Expand All @@ -23,12 +26,13 @@ export class TablePerType<DataT extends TablePerTypeData> implements ITablePerTy
this.parentTables = data.parentTables;
this.autoIncrement = data.autoIncrement;
this.explicitAutoIncrementValueEnabled = data.explicitAutoIncrementValueEnabled;
this.insertAndFetchPrimaryKey = data.insertAndFetchPrimaryKey;

this.joins = joins;
}

addParent<
ParentTableT extends ITable|ITablePerType
ParentTableT extends TableWithPrimaryKey|ITablePerType
> (
parentTable : ParentTableT
) : (
Expand All @@ -47,4 +51,32 @@ export class TablePerType<DataT extends TablePerTypeData> implements ITablePerTy
whereDelegate
);
}

insertAndFetch<
RowT extends TablePerTypeUtil.InsertAndFetchRow<
Extract<this, InsertableTablePerType>
>
> (
this : Extract<this, InsertableTablePerType>,
connection : IsolableInsertOneConnection,
row : OnlyKnownProperties<
RowT,
TablePerTypeUtil.InsertAndFetchRow<
Extract<this, InsertableTablePerType>
>
>
) : (
Promise<
TablePerTypeUtil.InsertedAndFetchedRow<
Extract<this, InsertableTablePerType>,
RowT
>
>
) {
return TablePerTypeUtil.insertAndFetch(
this,
connection,
row
);
}
}
33 changes: 30 additions & 3 deletions src/design-pattern-table-per-type/table-per-type.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import {ITable} from "../table";
import {TableWithPrimaryKey} from "../table";
import {Key} from "../key";

export interface TablePerTypeData {
readonly childTable : ITable,
readonly childTable : TableWithPrimaryKey,

readonly parentTables : readonly ITable[],
readonly parentTables : readonly TableWithPrimaryKey[],

readonly autoIncrement : readonly string[],

readonly explicitAutoIncrementValueEnabled : readonly string[],

/**
* Some `parentTables` may not have an `autoIncrement` column.
*
* When such a `parentTable` is encountered and
* we call `TablePerTypeUtil.insertAndFetch()`,
* we need to provide explicit values for the primary key
* of the `parentTable`.
*
* A value of `readonly never[]` indicates
* we do not need to specify any primary key.
*/
readonly insertAndFetchPrimaryKey : Key,
}

export interface ITablePerType<DataT extends TablePerTypeData=TablePerTypeData> {
Expand All @@ -30,6 +44,8 @@ export interface ITablePerType<DataT extends TablePerTypeData=TablePerTypeData>

readonly explicitAutoIncrementValueEnabled : DataT["explicitAutoIncrementValueEnabled"];

readonly insertAndFetchPrimaryKey : DataT["insertAndFetchPrimaryKey"];

/**
* An array of 2-tuples containing table aliases.
*
Expand Down Expand Up @@ -59,3 +75,14 @@ export interface ITablePerType<DataT extends TablePerTypeData=TablePerTypeData>
]
)[];
}

type InsertableTablePerTypeImpl =
& Omit<ITablePerType, "parentTables">
& {
childTable : { insertEnabled : true },
parentTables : readonly (TableWithPrimaryKey & { insertEnabled : true })[],
}
;
export interface InsertableTablePerType extends InsertableTablePerTypeImpl {

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import {ITable, TableUtil} from "../../../table";
import {TableWithPrimaryKey} from "../../../table";
import {TablePerType} from "../../table-per-type-impl";
import {
ExtractAutoIncrement,
ExtractExplicitAutoIncrementValueEnabled,
ExtractInsertAndFetchPrimaryKey,
extractAutoIncrement,
extractExplicitAutoIncrementValueEnabled,
extractInsertAndFetchPrimaryKey
} from "../query";

export function tablePerType<ChildTableT extends ITable> (
export function tablePerType<ChildTableT extends TableWithPrimaryKey> (
childTable : ChildTableT
) : TablePerType<{
childTable : ChildTableT,
parentTables : readonly [],
autoIncrement : readonly Extract<ChildTableT["autoIncrement"], string>[],
explicitAutoIncrementValueEnabled : readonly TableUtil.ExplicitAutoIncrement<ChildTableT>[],
autoIncrement : readonly ExtractAutoIncrement<ChildTableT>[],
explicitAutoIncrementValueEnabled : readonly ExtractExplicitAutoIncrementValueEnabled<ChildTableT>[],
insertAndFetchPrimaryKey : readonly ExtractInsertAndFetchPrimaryKey<ChildTableT>[],
}> {
return new TablePerType<{
childTable : ChildTableT,
parentTables : readonly [],
autoIncrement : readonly Extract<ChildTableT["autoIncrement"], string>[],
explicitAutoIncrementValueEnabled : readonly TableUtil.ExplicitAutoIncrement<ChildTableT>[],
autoIncrement : readonly ExtractAutoIncrement<ChildTableT>[],
explicitAutoIncrementValueEnabled : readonly ExtractExplicitAutoIncrementValueEnabled<ChildTableT>[],
insertAndFetchPrimaryKey : readonly ExtractInsertAndFetchPrimaryKey<ChildTableT>[],
}>(
{
childTable,
parentTables : [],
autoIncrement : (
childTable.autoIncrement == undefined ?
[] :
[childTable.autoIncrement as Extract<ChildTableT["autoIncrement"], string>]
),
explicitAutoIncrementValueEnabled : (
childTable.autoIncrement == undefined ?
[] :
TableUtil.isExplicitAutoIncrement(childTable, childTable.autoIncrement) ?
[childTable.autoIncrement] :
[]
),
autoIncrement : extractAutoIncrement(childTable),
explicitAutoIncrementValueEnabled : extractExplicitAutoIncrementValueEnabled(childTable),
insertAndFetchPrimaryKey : extractInsertAndFetchPrimaryKey(childTable),
},
[],
);
Expand Down
2 changes: 2 additions & 0 deletions src/design-pattern-table-per-type/util/execution/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./fetch-one";
export * from "./insert-and-fetch";
export * from "./insert-row";
Loading

0 comments on commit 34264df

Please sign in to comment.