Kamirus / purescript-selda

A type-safe, high-level SQL library for PureScript
MIT License
90 stars 3 forks source link

Allow querying from arbitrary sources not only tables #34

Closed Kamirus closed 4 years ago

Kamirus commented 4 years ago

Currently

Right now we allow querying from Table definitions like

fooTable :: Table (c1 :: Int)
fooTable = Table { name: "foo_table" }

Which when used to generate the SQL is used like

SELECT foo_table_0.c1
FROM foo_table foo_table_0

The name of the table foo_table is aliased using this name and a unique number

New feature

We would like to write a query using PG function generate_series

SELECT tablealias.columnalias
FROM generate_series(1,11) tablealias (columnalias);

Proposal

Since datatype Table is used as a black box - meaning we only:

Then we could provide a Table-like datatype with more general alias-generation

generateSeries :: Source ( col :: Int )
generateSeries = Source
  { source: \aliasSufix -> "generate_series(1,11) " <> "gs" <> aliasSufix <> " (col)"
  , alias: \aliasSufix -> "gs" <> aliasSufix
  }

source function would be used in FROM/JOIN clause alias function would be used in other places to refer to this source These depend on the aliasSufix supplied during SQL generation to ensure that aliases are unique