Common table expression
with "active" as (...) select ...
import expr { Expr } "./expr.lt"
import sql_core { Sql } "./sql_core.lt"
import schema { Table } "./schema.lt"
import dialect { Dialect } "./dialect.lt"
import support "./support.lt"
import pg_core "./pg_core.lt"
import select { Select } "./select.lt"
package case_cte {
fn t() -> Table {
schema.plain("users", [pg_core.serial("id"), pg_core.text("name"), pg_core.integer("age")])
}
export fn q() -> Select {
let tb = t()
let inner = select.where(select.fields(select.from(select.create(), tb), [schema.field(tb, "id")]), expr.eq(schema.col(tb, "name"), expr.str("a")))
let named = select.with_cte(select.create(), "active", inner)
select.fields(select.from(named, schema.plain("active", [pg_core.integer("id")])), [expr.raw_sql(sql_core.identifier("id"))])
}
test "postgres" {
expect.not_equal(support.render(dialect.postgres(), select.to_sql(q())), "")
}
test "mysql" {
expect.not_equal(support.render(dialect.mysql(), select.to_sql(q())), "")
}
test "sqlite" {
expect.not_equal(support.render(dialect.sqlite(), select.to_sql(q())), "")
}
}
// Generated by Lingua Tungga. Do not edit by hand.
import * as lt from "../_lingua_rt";
import * as m0 from "./case_cte";
import * as m4 from "./dialect";
import * as m6 from "./support";
import * as m8 from "./select";
function main(): void {
{
lt.print(m6.render(m4.postgres(), m8.toSql(m0.q())));
}
{
lt.print(m6.render(m4.mysql(), m8.toSql(m0.q())));
}
{
lt.print(m6.render(m4.sqlite(), m8.toSql(m0.q())));
}
}
main();
with "active" as (select "id" from "users" where "users"."name" = $1) select "id" from "active" text:a with `active` as (select `id` from `users` where `users`.`name` = ?) select `id` from `active` text:a with "active" as (select "id" from "users" where "users"."name" = ?) select "id" from "active" text:a
# Generated by Lingua Tungga. Do not edit by hand.
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import lt_runtime as _lt
from typing import Any, Callable
import gen.case_cte as m0
import gen.dialect as m4
import gen.support as m6
import gen.select as m8
def _usage0() -> None:
_lt.print_value(m6.render(m4.postgres(), m8.to_sql(m0.q())))
def _usage1() -> None:
_lt.print_value(m6.render(m4.mysql(), m8.to_sql(m0.q())))
def _usage2() -> None:
_lt.print_value(m6.render(m4.sqlite(), m8.to_sql(m0.q())))
def main() -> None:
_usage0()
_usage1()
_usage2()
if __name__ == "__main__":
main()
with "active" as (select "id" from "users" where "users"."name" = $1) select "id" from "active" text:a with `active` as (select `id` from `users` where `users`.`name` = ?) select `id` from `active` text:a with "active" as (select "id" from "users" where "users"."name" = ?) select "id" from "active" text:a
// Generated by Lingua Tungga. Do not edit by hand.
package main
import (
. "linguatungga/gen/case_cte"
m4 "linguatungga/gen/dialect"
m8 "linguatungga/gen/select"
m6 "linguatungga/gen/support"
lt "linguatungga/lt"
)
func main() {
func() {
lt.Print(m6.Render(m4.Postgres(), m8.ToSql(Q())))
}()
func() {
lt.Print(m6.Render(m4.Mysql(), m8.ToSql(Q())))
}()
func() {
lt.Print(m6.Render(m4.Sqlite(), m8.ToSql(Q())))
}()
}
with "active" as (select "id" from "users" where "users"."name" = $1) select "id" from "active" text:a with `active` as (select `id` from `users` where `users`.`name` = ?) select `id` from `active` text:a with "active" as (select "id" from "users" where "users"."name" = ?) select "id" from "active" text:a
| dialect | port | drizzle | |
|---|---|---|---|
| postgres | with "active" as (select "id" from "users" where "users"."name" = $1) select "id" from "active" text:a |
with "active" as (select "id" from "users" where "users"."name" = $1) select "id" from "active" text:a |
match |
| mysql | with `active` as (select `id` from `users` where `users`.`name` = ?) select `id` from `active` text:a |
with `active` as (select `id` from `users` where `users`.`name` = ?) select `id` from `active` text:a |
match |
| sqlite | with "active" as (select "id" from "users" where "users"."name" = ?) select "id" from "active" text:a |
with "active" as (select "id" from "users" where "users"."name" = ?) select "id" from "active" text:a |
match |