Skip to content

Commit

Permalink
Remove main_alias (#31)
Browse files Browse the repository at this point in the history
* Remove main_alias

* fix docs
  • Loading branch information
lucasavila00 authored Jul 1, 2022
1 parent f52c160 commit d242c2a
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 154 deletions.
12 changes: 0 additions & 12 deletions docs-eval/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,6 @@ users
.select((f) => f);
```

# Alias of sub-selection

Sub selections that are not in a join context can be refered to by using `main_alias`.

```ts eval --yield=sql
yield users
.selectStar()
.where((f) => sql`${f.id} = 5`)
.select((f) => ({ a: f["main_alias.id"] }))
.stringify();
```

# Control order of selection

Although it works on most cases, order of selection is not guaranteed.
Expand Down
5 changes: 2 additions & 3 deletions docs/api/classes/compound.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ Added in v0.0.0
```ts
select: <NewSelection extends string>(
f: (
fields: Record<Selection | `main_alias.${Selection}`, SafeString> &
NoSelectFieldsCompileError
fields: Record<Selection, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
) => SelectStatement<Selection | `main_alias.${Selection}`, NewSelection>;
) => SelectStatement<Selection, NewSelection>;
```
Added in v0.0.0
Expand Down
8 changes: 3 additions & 5 deletions docs/api/classes/select-statement.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ Added in v0.0.0
```ts
select: <NewSelection extends string>(
f: (
f: Record<Selection | `main_alias.${Selection}`, SafeString> &
NoSelectFieldsCompileError
f: Record<Selection, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
) => SelectStatement<Selection | `main_alias.${Selection}`, NewSelection>;
) => SelectStatement<Selection, NewSelection>;
```
Added in v0.0.0
Expand Down Expand Up @@ -117,8 +116,7 @@ Added in v0.0.0
```ts
appendSelect: <NewSelection extends string>(
f: (
f: Record<Selection | Scope | `main_alias.${Selection}`, SafeString> &
NoSelectFieldsCompileError
f: Record<Selection | Scope, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
) => SelectStatement<Scope, Selection | NewSelection>;
```
Expand Down
26 changes: 0 additions & 26 deletions docs/examples/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,32 +303,6 @@ users
.select((f) => f);
```

# Alias of sub-selection

Sub selections that are not in a join context can be refered to by using `main_alias`.

```ts
users
.selectStar()
.where((f) => sql`${f.id} = 5`)
.select((f) => ({ a: f["main_alias.id"] }))
.stringify();
```

```sql
SELECT
`main_alias`.`id` AS `a`
FROM
(
SELECT
*
FROM
`users`
WHERE
`id` = 5
) AS `main_alias`
```

# Control order of selection

Although it works on most cases, order of selection is not guaranteed.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sql-select-ts",
"version": "0.0.5",
"version": "0.0.6",
"description": "A modern, database-agnostic, composable SELECT query builder with great typescript support.",
"main": "lib/index.js",
"module": "es6/index.js",
Expand Down
5 changes: 2 additions & 3 deletions src/classes/compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ export class Compound<Scope extends string, Selection extends string> {
*/
public select = <NewSelection extends string>(
f: (
fields: Record<Selection | `main_alias.${Selection}`, SafeString> &
NoSelectFieldsCompileError
fields: Record<Selection, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
): SelectStatement<Selection | `main_alias.${Selection}`, NewSelection> =>
): SelectStatement<Selection, NewSelection> =>
SelectStatement.__fromTableOrSubquery(this, [AliasedRows(f(proxy))]);

/**
Expand Down
10 changes: 3 additions & 7 deletions src/classes/select-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ export class SelectStatement<Scope extends string, Selection extends string> {
*/
public select = <NewSelection extends string>(
f: (
f: Record<Selection | `main_alias.${Selection}`, SafeString> &
NoSelectFieldsCompileError
f: Record<Selection, SafeString> & NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
): SelectStatement<Selection | `main_alias.${Selection}`, NewSelection> =>
): SelectStatement<Selection, NewSelection> =>
SelectStatement.__fromTableOrSubquery(this, [AliasedRows(f(proxy))]);

/**
Expand All @@ -256,10 +255,7 @@ export class SelectStatement<Scope extends string, Selection extends string> {
*/
public appendSelect = <NewSelection extends string>(
f: (
f: Record<
Selection | Scope | `main_alias.${Selection}`,
SafeString
> &
f: Record<Selection | Scope, SafeString> &
NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>
): SelectStatement<Scope, Selection | NewSelection> =>
Expand Down
5 changes: 0 additions & 5 deletions src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,6 @@ export const printSelectStatementInternal = <
? printInternal(selectStatement.__props.from, true).with_ ?? ""
: "";

const doesSelectMainAlias = selection.includes("main_alias");

const main_alias = doesSelectMainAlias ? "AS `main_alias`" : "";

const distinct = selectStatement.__props.distinct ? "DISTINCT" : "";

const clickhouseWith =
Expand All @@ -260,7 +256,6 @@ export const printSelectStatementInternal = <
selection,
replace,
from,
main_alias,
prewhere,
where,
printGroupBy(selectStatement.__props.groupBy),
Expand Down
76 changes: 0 additions & 76 deletions tests/main_alias.test.ts

This file was deleted.

32 changes: 16 additions & 16 deletions tests/sqlite-suite/select1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1706,11 +1706,11 @@ describe("sqlite select1", () => {
.orderBy((f) => f.f1)
.orderBy((f) => f.f2)
.selectStar()
.select((f) => ({ f1: f["main_alias.f1"], f2: f.f2 }))
.select((f) => ({ f1: f["f1"], f2: f.f2 }))
.stringify();

expect(q).toMatchInlineSnapshot(
`SELECT \`main_alias\`.\`f1\` AS \`f1\`, \`f2\` AS \`f2\` FROM (SELECT * FROM (SELECT * FROM \`test1\` WHERE \`f1\` < \`f2\` UNION ALL SELECT * FROM \`test1\` WHERE \`f1\` > \`f2\` ORDER BY \`f1\`, \`f2\`)) AS \`main_alias\``
`SELECT \`f1\` AS \`f1\`, \`f2\` AS \`f2\` FROM (SELECT * FROM (SELECT * FROM \`test1\` WHERE \`f1\` < \`f2\` UNION ALL SELECT * FROM \`test1\` WHERE \`f1\` > \`f2\` ORDER BY \`f1\`, \`f2\`))`
);
expect(await run(q)).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -1783,12 +1783,12 @@ describe("sqlite select1", () => {
.limit(1);

const q = u
.select((f) => ({ it: f["main_alias.f1"] }))
.select((f) => ({ it: f["f1"] }))
.orderBy((f) => f.f2)
.stringify();

expect(q).toMatchInlineSnapshot(
`SELECT \`main_alias\`.\`f1\` AS \`it\` FROM (SELECT * FROM \`test1\` WHERE \`f1\` < \`f2\` UNION ALL SELECT * FROM \`test1\` WHERE \`f1\` > \`f2\` ORDER BY \`f1\`, \`f2\` LIMIT 1) AS \`main_alias\` ORDER BY \`f2\``
`SELECT \`f1\` AS \`it\` FROM (SELECT * FROM \`test1\` WHERE \`f1\` < \`f2\` UNION ALL SELECT * FROM \`test1\` WHERE \`f1\` > \`f2\` ORDER BY \`f1\`, \`f2\` LIMIT 1) ORDER BY \`f2\``
);
expect(await run(q)).toMatchInlineSnapshot(`
Array [
Expand All @@ -1810,13 +1810,13 @@ describe("sqlite select1", () => {
.limit(1);

const q = u
.select((f) => ({ f1: f["main_alias.f1"] }))
.appendSelect((f) => ({ f2: f["main_alias.f2"] }))
.select((f) => ({ f1: f["f1"] }))
.appendSelect((f) => ({ f2: f["f2"] }))
.orderBy((f) => f.f2)
.stringify();

expect(q).toMatchInlineSnapshot(
`SELECT \`main_alias\`.\`f1\` AS \`f1\`, \`main_alias\`.\`f2\` AS \`f2\` FROM (SELECT * FROM \`test1\` WHERE \`f1\` < \`f2\` UNION ALL SELECT * FROM \`test1\` WHERE \`f1\` > \`f2\` ORDER BY \`f1\`, \`f2\` LIMIT 1) AS \`main_alias\` ORDER BY \`f2\``
`SELECT \`f1\` AS \`f1\`, \`f2\` AS \`f2\` FROM (SELECT * FROM \`test1\` WHERE \`f1\` < \`f2\` UNION ALL SELECT * FROM \`test1\` WHERE \`f1\` > \`f2\` ORDER BY \`f1\`, \`f2\` LIMIT 1) ORDER BY \`f2\``
);
expect(await run(q)).toMatchInlineSnapshot(`
Array [
Expand All @@ -1832,11 +1832,11 @@ describe("sqlite select1", () => {
const q = test1
.selectStar()
.where((f) => sql`${f.f1} < ${f.f2}`)
.select((f) => ({ f1: f["main_alias.f1"] }))
.select((f) => ({ f1: f["f1"] }))
.stringify();

expect(q).toMatchInlineSnapshot(
`SELECT \`main_alias\`.\`f1\` AS \`f1\` FROM (SELECT * FROM \`test1\` WHERE \`f1\` < \`f2\`) AS \`main_alias\``
`SELECT \`f1\` AS \`f1\` FROM (SELECT * FROM \`test1\` WHERE \`f1\` < \`f2\`)`
);
expect(await run(q)).toMatchInlineSnapshot(`
Array [
Expand All @@ -1850,11 +1850,11 @@ describe("sqlite select1", () => {
const q = test1
.select((f) => ({ f1: f["test1.f1"], f2: f["test1.f2"] }))
.where((f) => sql`${f.f1} < ${f.f2}`)
.select((f) => ({ f1: f["main_alias.f1"] }))
.select((f) => ({ f1: f["f1"] }))
.stringify();

expect(q).toMatchInlineSnapshot(
`SELECT \`main_alias\`.\`f1\` AS \`f1\` FROM (SELECT \`test1\`.\`f1\` AS \`f1\`, \`test1\`.\`f2\` AS \`f2\` FROM \`test1\` WHERE \`f1\` < \`f2\`) AS \`main_alias\``
`SELECT \`f1\` AS \`f1\` FROM (SELECT \`test1\`.\`f1\` AS \`f1\`, \`test1\`.\`f2\` AS \`f2\` FROM \`test1\` WHERE \`f1\` < \`f2\`)`
);
expect(await run(q)).toMatchInlineSnapshot(`
Array [
Expand All @@ -1869,11 +1869,11 @@ describe("sqlite select1", () => {
.selectStar()
.appendSelect((f) => ({ f1: f["test1.f1"] }))
.where((f) => sql`${f.f1} < ${f.f2}`)
.select((f) => ({ f1: f["main_alias.f1"] }))
.select((f) => ({ f1: f["f1"] }))
.stringify();

expect(q).toMatchInlineSnapshot(
`SELECT \`main_alias\`.\`f1\` AS \`f1\` FROM (SELECT *, \`test1\`.\`f1\` AS \`f1\` FROM \`test1\` WHERE \`f1\` < \`f2\`) AS \`main_alias\``
`SELECT \`f1\` AS \`f1\` FROM (SELECT *, \`test1\`.\`f1\` AS \`f1\` FROM \`test1\` WHERE \`f1\` < \`f2\`)`
);
expect(await run(q)).toMatchInlineSnapshot(`
Array [
Expand All @@ -1889,12 +1889,12 @@ describe("sqlite select1", () => {
.selectStar()
.appendSelect((f) => ({ f1: f["test1.f1"] }))
.where((f) => sql`${f.f1} < ${f.f2}`)
.select((f) => ({ f1: f["main_alias.f1"] }))
.appendSelect((f) => ({ f2: f["main_alias.f2"] }))
.select((f) => ({ f1: f["f1"] }))
.appendSelect((f) => ({ f2: f["f2"] }))
.stringify();

expect(q).toMatchInlineSnapshot(
`SELECT \`main_alias\`.\`f1\` AS \`f1\`, \`main_alias\`.\`f2\` AS \`f2\` FROM (SELECT *, \`test1\`.\`f1\` AS \`f1\` FROM \`test1\` WHERE \`f1\` < \`f2\`) AS \`main_alias\``
`SELECT \`f1\` AS \`f1\`, \`f2\` AS \`f2\` FROM (SELECT *, \`test1\`.\`f1\` AS \`f1\` FROM \`test1\` WHERE \`f1\` < \`f2\`)`
);
expect(await run(q)).toMatchInlineSnapshot(`
Array [
Expand Down

0 comments on commit d242c2a

Please sign in to comment.