Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliases for tables. #12

Open
NightBlues opened this issue Mar 18, 2019 · 0 comments
Open

Aliases for tables. #12

NightBlues opened this issue Mar 18, 2019 · 0 comments

Comments

@NightBlues
Copy link
Contributor

There are alias and --> funcitons in Sequoia.Select, but they operate over Expr.t while tables are not expressions.
Example:

module Weapon = struct
  include (val Postgresql.table "weapon")
  let id = Field.int "id"
  let name = Field.string "name"
  let damage = Field.int "damage"
end

module Unit = struct
  include (val Postgresql.table "unit")
  let id = Field.int "id"
  let name = Field.string "name"
  let main_hand = Field.foreign_key "main_hand" ~references:Weapon.id
  let off_hand = Field.foreign_key "off_hand" ~references:Weapon.id
end    
      
let list_query =
  Postgresql.Select.(Expr.(
      from Unit.table
      |> left_join (that Unit.main_hand There)
      |> left_join (that Unit.off_hand (Skip There))
      |> select [
        field Unit.id (Skip (Skip (There)));
        field Unit.name (Skip (Skip (There)));
        field Weapon.name There;
        field Weapon.damage There;
        field Weapon.name (Skip There);
        field Weapon.damage (Skip There);
      ]
      |> seal
    ))

generates following sql:

SELECT unit.id, unit.name, weapon.name, weapon.damage, weapon.name, weapon.damage
FROM unit
LEFT JOIN weapon ON weapon.id = unit.main_hand
LEFT JOIN weapon ON weapon.id = unit.off_hand

which seems to be not valid.

Whould be cool to have ability to do something like:

let list_query =
  Postgresql.Select.(Expr.(
      from Unit.table
      |> (left_join (that Unit.main_hand There) --> "mainhand")
      |> (left_join (that Unit.off_hand (Skip There)) --> "offhand")
      |> select [
        field Unit.id (Skip (Skip (There)));
        field Unit.name (Skip (Skip (There)));
        field Weapon.name There;
        field Weapon.damage There;
        field Weapon.name (Skip There);
        field Weapon.damage (Skip There);
      ]
      |> seal
    ))

and get sql:

SELECT unit.id, unit.name, mainhand.name, mainhand.damage,
offhand.name, offhand.damage
FROM unit
LEFT JOIN weapon AS mainhand ON weapon.id = unit.main_hand
LEFT JOIN weapon AS offhand ON weapon.id = unit.off_hand

Excuse me, if I'm missing something - types are hard enough to understand:) May be a workaround can exist?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant