Skip to content

Commit

Permalink
Investigate selection shortcut, where alias is not allowed (#57)
Browse files Browse the repository at this point in the history
* select shortcut

* add more shortcuts

* add shortcuts
  • Loading branch information
lucasavila00 authored Jul 5, 2022
1 parent 3b4d8b3 commit 8a9d6bd
Show file tree
Hide file tree
Showing 29 changed files with 637 additions and 241 deletions.
16 changes: 4 additions & 12 deletions docs-eval/common-table-expression.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ yield with_(
select(
(f) => ({ region: f.region, total_sales: SUM(f.amount) }),
orders
).groupBy((f) => f.region)
).groupBy(["region"])
)
.with_("top_regions", (acc) =>
select(
(f) => ({
region: f.region,
}),
acc.regional_sales
).where(
select(["region"], acc.regional_sales).where(
(f) =>
sql`${f.total_sales} > ${select(
(f) => ({ it: sql`SUM(${f.total_sales})/10` }),
Expand All @@ -60,12 +55,9 @@ yield with_(
)
.where(
(f) =>
sql`${f.region} IN ${select(
(f) => ({ region: f.region }),
acc.top_regions
)}`
sql`${f.region} IN ${select(["region"], acc.top_regions)}`
)
.groupBy((f) => [f.region, f.product])
.groupBy(["region", "product"])
)
.stringify();
```
4 changes: 1 addition & 3 deletions docs-eval/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ yield str;
## String interpolation

```ts eval
const q2 = users
.select((fields) => ({ age: fields.age }))
.where((fields) => sql`${fields.id}=1`);
const q2 = users.select(["age"]).where((fields) => sql`${fields.id}=1`);
```

```ts eval --yield=sql
Expand Down
22 changes: 18 additions & 4 deletions docs-eval/group-by.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ layout: default
</details>

```ts eval --replacePrintedInput=../src,sql-select-ts
import { table } from "../src";
import { table, dsql as sql, SafeString } from "../src";
```

We will use this table
Expand All @@ -31,34 +31,48 @@ const users = table(
/* columns: */ ["id", "age", "name"],
/* db-name & alias: */ "users"
);

const lowercase = (it: SafeString): SafeString => sql`lowerCase(${it})`;
```

# One Clause

```ts eval --yield=sql
yield users
.selectStar()
.groupBy((f) => f.age)
.groupBy((f) => lowercase(f.name))
.stringify();
```

```ts eval --yield=sql
yield users.selectStar().groupBy(["name"]).stringify();
```

# Two Clauses

## One call

```ts eval --yield=sql
yield users
.selectStar()
.groupBy((f) => [f.age, f.id])
.groupBy((f) => [f.name, f.id])
.stringify();
```

```ts eval --yield=sql
yield users.selectStar().groupBy(["name", "id"]).stringify();
```

## Two calls

```ts eval --yield=sql
yield users
.selectStar()
.groupBy((f) => f.age)
.groupBy((f) => f.name)
.groupBy((f) => f.id)
.stringify();
```

```ts eval --yield=sql
yield users.selectStar().groupBy(["name"]).groupBy(["id"]).stringify();
```
14 changes: 14 additions & 0 deletions docs-eval/having.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ yield t0
.stringify();
```

```ts eval --yield=sql
yield t0
.selectStar()
.groupBy(["x"])
.having(["x"])
.groupBy(["y"])
.having(["y"])
.stringify();
```

## In one call

```ts eval --yield=sql
Expand All @@ -73,3 +83,7 @@ yield t0
.having((f) => [f.x, f.y])
.stringify();
```

```ts eval --yield=sql
yield t0.selectStar().groupBy(["x", "y"]).having(["x", "y"]).stringify();
```
4 changes: 4 additions & 0 deletions docs-eval/order-by.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ yield users
.orderBy((f) => f.id)
.stringify();
```

```ts eval --yield=sql
yield users.selectStar().orderBy(["age", "id"]).stringify();
```
30 changes: 9 additions & 21 deletions docs-eval/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ Starting at query top
```ts eval --yield=sql
yield selectStar(
select(
(f) => ({ it2: f.it }),
["it"],
//
initialData
).where((f) => sql`${f.it2} = 1`)
).where((f) => sql`${f.it} = 1`)
).stringify();
```

Starting at query root

```ts eval --yield=sql
yield initialData
.select((f) => ({ it2: f.it }))
.where((f) => sql`${f.it2} = 1`)
.select(["it"])
.where((f) => sql`${f.it} = 1`)
.selectStar()
.stringify();
```
Expand Down Expand Up @@ -147,10 +147,7 @@ yield users.select((f) => ({ maxAge: MAX(f.age) })).stringify();
## Select distinct

```ts eval --yield=sql
yield admins
.select((f) => ({ name: f.name }))
.distinct()
.stringify();
yield admins.select(["name"]).distinct().stringify();
```

## Select star and a field
Expand Down Expand Up @@ -178,18 +175,14 @@ yield admins
## Select from sub-select

```ts eval --yield=sql
yield users
.selectStar()
.select((f) => ({ age: f.age }))
.selectStar()
.stringify();
yield users.selectStar().select(["age"]).selectStar().stringify();
```

## Select from union

```ts eval --yield=sql
yield unionAll([users.selectStar(), admins.selectStar()])
.select((f) => ({ age: f.age }))
.select(["age"])
.stringify();
```

Expand All @@ -199,10 +192,7 @@ yield unionAll([users.selectStar(), admins.selectStar()])
yield users
.joinTable("LEFT", admins)
.using(["id"])
.select((f) => ({
userName: f["users.name"],
admName: f["adm.name"],
}))
.select(["users.name", "adm.name"])
.stringify();
```

Expand Down Expand Up @@ -246,9 +236,7 @@ yield users
.select((f) => ({
["123"]: f.age,
}))
.appendSelect((f) => ({
name: f.name,
}))
.appendSelect(["name"])
.appendSelect((f) => ({
["456"]: f.id,
}))
Expand Down
3 changes: 2 additions & 1 deletion docs-ts.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"src/proxy.ts",
"src/types.ts",
"src/data-wrappers.ts",
"src/wrap-alias.ts"
"src/wrap-alias.ts",
"src/consume-fields.ts"
],
"outDir": "docs-ts-out"
}
23 changes: 15 additions & 8 deletions docs/api/classes/compound.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ Added in v0.0.0

```ts
orderBy: (
f: (
fields: Record<Scope | Selection, SafeString>
) => SafeString[] | SafeString
f:
| readonly (Scope | Selection)[]
| ((
fields: Record<Scope | Selection, SafeString>
) => SafeString[] | SafeString)
) => Compound<Scope, Selection>;
```

Expand All @@ -76,11 +78,16 @@ Added in v0.0.0
**Signature**

```ts
select: <NewSelection extends string>(
f: (
fields: Record<Selection, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
) => SelectStatement<Selection, NewSelection>;
select: <
NewSelection extends string = never,
SubSelection extends Selection = never
>(
f:
| readonly SubSelection[]
| ((
fields: Record<Selection, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>)
) => SelectStatement<Selection, NewSelection | SubSelection>;
```

Added in v0.0.0
Expand Down
15 changes: 10 additions & 5 deletions docs/api/classes/joined.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ Added in v0.0.0
**Signature**

```ts
select: <NewSelection extends string>(
f: (
f: Record<Selection | Scope, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
) => SelectStatement<Selection | Scope, NewSelection>;
select: <
NewSelection extends string = never,
SubSelection extends Selection | Scope = never
>(
f:
| readonly SubSelection[]
| ((
f: Record<Selection | Scope, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>)
) => SelectStatement<Selection | Scope, NewSelection | SubSelection>;
```

Added in v0.0.0
Expand Down
62 changes: 40 additions & 22 deletions docs/api/classes/select-statement.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ clickhouse: {
with_: <NewSelection extends string>(
it: Record<NewSelection, SelectStatement<any, any> | StringifiedSelectStatement<any>>
) => SelectStatement<Scope | NewSelection, Selection>
prewhere: (f: (fields: Record<Scope | Selection, SafeString>) => ReadonlyArray<SafeString> | SafeString) =>
SelectStatement<Scope, Selection>
prewhere: (
f:
| readonly (Scope | Selection)[]
| ((fields: Record<Scope | Selection, SafeString>) => ReadonlyArray<SafeString> | SafeString)
) => SelectStatement<Scope, Selection>
replace: <NewSelection extends string>(
f: (f: Record<Selection | Scope, SafeString> & NoSelectFieldsCompileError) => ReplaceT<Selection>
) => SelectStatement<Scope, Selection | NewSelection>
Expand All @@ -81,11 +84,16 @@ Added in v0.0.0
**Signature**
```ts
select: <NewSelection extends string>(
f: (
f: Record<Selection | Scope, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
) => SelectStatement<Selection, NewSelection>;
select: <
NewSelection extends string = never,
SubSelection extends Scope | Selection = never
>(
f:
| readonly SubSelection[]
| ((
f: Record<Selection | Scope, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>)
) => SelectStatement<Selection, NewSelection | SubSelection>;
```
Added in v0.0.0
Expand Down Expand Up @@ -116,9 +124,11 @@ Added in v0.0.0
```ts
appendSelect: <NewSelection extends string>(
f: (
f: Record<Selection | Scope, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
f:
| readonly (Scope | Selection)[]
| ((
f: Record<Selection | Scope, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>)
) => SelectStatement<Scope, Selection | NewSelection>;
```
Expand All @@ -130,9 +140,11 @@ Added in v0.0.0
```ts
where: (
f: (
fields: Record<Scope | Selection, SafeString>
) => ReadonlyArray<SafeString> | SafeString
f:
| readonly (Scope | Selection)[]
| ((
fields: Record<Scope | Selection, SafeString>
) => ReadonlyArray<SafeString> | SafeString)
) => SelectStatement<Scope, Selection>;
```
Expand All @@ -144,9 +156,11 @@ Added in v0.0.0
```ts
having: (
f: (
fields: Record<Scope | Selection, SafeString>
) => ReadonlyArray<SafeString> | SafeString
f:
| readonly (Scope | Selection)[]
| ((
fields: Record<Scope | Selection, SafeString>
) => ReadonlyArray<SafeString> | SafeString)
) => SelectStatement<Scope, Selection>;
```
Expand All @@ -168,9 +182,11 @@ Added in v0.0.0
```ts
orderBy: (
f: (
fields: Record<Scope | Selection, SafeString>
) => ReadonlyArray<SafeString> | SafeString
f:
| readonly (Scope | Selection)[]
| ((
fields: Record<Scope | Selection, SafeString>
) => ReadonlyArray<SafeString> | SafeString)
) => SelectStatement<Scope, Selection>;
```
Expand All @@ -182,9 +198,11 @@ Added in v0.0.0
```ts
groupBy: (
f: (
fields: Record<Scope | Selection, SafeString>
) => ReadonlyArray<SafeString> | SafeString
f:
| readonly (Scope | Selection)[]
| ((
fields: Record<Scope | Selection, SafeString>
) => ReadonlyArray<SafeString> | SafeString)
) => SelectStatement<Scope, Selection>;
```
Expand Down
Loading

0 comments on commit 8a9d6bd

Please sign in to comment.