FatEcto v0.5.0 FatEcto.FatQuery.FatJoin View Source

Builds a join query with another table on the type of join passed in the params. It also supports additional join_on clauses.

$right_join

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Join query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"experience_years" => 2},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  },
...>  "$right_join" => %{
...>    "fat_rooms" => %{
...>      "$on_field" => "id",
...>      "$on_table_field" => "hospital_id",
...>      "$on_type" => "$in_x",
...>      "$select" => ["beds", "capacity", "level"],
...>      "$where" => %{"incharge" => "John"}
...>    }
...>  }
...> }
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in "fat_rooms", on: f0.id in f1.hospital_id, left_join: f2 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, where: f1.incharge == ^"John" and ^true, where: f2.experience_years == ^2 and ^true, order_by: [desc: f2.id], order_by: [desc: f0.id], limit: ^34, offset: ^0, select: merge(map(f0, [:name, :location, :rating, {:fat_rooms, [:beds, :capacity]}]), %{^"fat_rooms" => map(f1, [:beds, :capacity, :level])}), preload: [[fat_doctors: [:fat_patients]]]>

Options

  • $include - Include the assoication doctors.
  • $include: :fat_patients- Include the assoication patients. Which has association with doctors.
  • $select - Select the fields from hospital and rooms.
  • $where - Added the where attribute in the query.
  • $order - Sort the result based on the order attribute.
  • $right_join - Specify the type of join.
  • $on_type - Specify the type of condition on the join.
  • $on_field - Specify the field for join.
  • $on_table_field - Specify the field for join in the joining table.

$left_join

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Join query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"experience_years" => 2},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  },
...>  "$left_join" => %{
...>    "fat_rooms" => %{
...>      "$on_field" => "id",
...>      "$on_table_field" => "hospital_id",
...>      "$on_type" => "$not_eq",
...>      "$select" => ["beds", "capacity", "level"],
...>      "$where" => %{"incharge" => "John"}
...>    }
...>  }
...> }
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, left_join: f1 in "fat_rooms", on: f0.id != f1.hospital_id, left_join: f2 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, where: f1.incharge == ^"John" and ^true, where: f2.experience_years == ^2 and ^true, order_by: [desc: f2.id], order_by: [desc: f0.id], limit: ^34, offset: ^0, select: merge(map(f0, [:name, :location, :rating, {:fat_rooms, [:beds, :capacity]}]), %{^"fat_rooms" => map(f1, [:beds, :capacity, :level])}), preload: [[fat_doctors: [:fat_patients]]]>

Options

  • $include - Include the assoication doctors.
  • $include: :fat_patients- Include the assoication patients. Which has association with doctors.
  • $select - Select the fields from hospital and rooms.
  • $where - Added the where attribute in the query.
  • $order - Sort the result based on the order attribute.
  • $right_join - Specify the type of join.
  • $on_type - Specify the type of condition on the join.
  • $on_field - Specify the field for join.
  • $on_table_field - Specify the field for join in the joining table.

$inner_join

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Join query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"experience_years" => 2},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  },
...>  "$inner_join" => %{
...>    "fat_rooms" => %{
...>      "$on_field" => "id",
...>      "$on_table_field" => "hospital_id",
...>      "$on_type" => "$in",
...>      "$select" => ["beds", "capacity", "level"],
...>      "$where" => %{"incharge" => "John"}
...>    }
...>  }
...> }
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, join: f1 in "fat_rooms", on: f1.hospital_id in f0.id, left_join: f2 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, where: f1.incharge == ^"John" and ^true, where: f2.experience_years == ^2 and ^true, order_by: [desc: f2.id], order_by: [desc: f0.id], limit: ^34, offset: ^0, select: merge(map(f0, [:name, :location, :rating, {:fat_rooms, [:beds, :capacity]}]), %{^"fat_rooms" => map(f1, [:beds, :capacity, :level])}), preload: [[fat_doctors: [:fat_patients]]]>

Options

  • $include - Include the assoication doctors.
  • $include: :fat_patients- Include the assoication patients. Which has association with doctors.
  • $select - Select the fields from hospital and rooms.
  • $where - Added the where attribute in the query.
  • $order - Sort the result based on the order attribute.
  • $right_join - Specify the type of join.
  • $on_type - Specify the type of condition on the join.
  • $on_field - Specify the field for join.
  • $on_table_field - Specify the field for join in the joining table.

$full_join

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Join query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"experience_years" => 2},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  },
...>  "$full_join" => %{
...>    "fat_rooms" => %{
...>      "$on_field" => "id",
...>      "$on_table_field" => "hospital_id",
...>      "$select" => ["beds", "capacity", "level"],
...>      "$where" => %{"incharge" => "John"}
...>    }
...>  }
...> }
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, full_join: f1 in "fat_rooms", on: f0.id == f1.hospital_id, left_join: f2 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, where: f1.incharge == ^"John" and ^true, where: f2.experience_years == ^2 and ^true, order_by: [desc: f2.id], order_by: [desc: f0.id], limit: ^34, offset: ^0, select: merge(map(f0, [:name, :location, :rating, {:fat_rooms, [:beds, :capacity]}]), %{^"fat_rooms" => map(f1, [:beds, :capacity, :level])}), preload: [[fat_doctors: [:fat_patients]]]>

Options

  • $include - Include the assoication doctors.
  • $include: :fat_patients- Include the assoication patients. Which has association with doctors.
  • $select - Select the fields from hospital and rooms.
  • $where - Added the where attribute in the query.
  • $order - Sort the result based on the order attribute.
  • $right_join - Specify the type of join.
  • $on_field - Specify the field for join.
  • $on_table_field - Specify the field for join in the joining table.

Link to this section Summary

Functions

Builds a join query based on the join type passed in the params.

Link to this section Functions

Link to this function

build_join(queryable, join_params, join_type \\ "$join", options)

View Source

Builds a join query based on the join type passed in the params.

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • join_params - Join query options as a map.
  • join_type - Type of join.
  • options - Pass options related to otp_app.

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"experience_years" => 2},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  },
...>  "$right_join" => %{
...>    "fat_rooms" => %{
...>      "$on_field" => "id",
...>      "$on_table_field" => "hospital_id",
...>      "$select" => ["beds", "capacity", "level"],
...>      "$where" => %{"incharge" => "John"}
...>    }
...>  }
...> }
iex> Elixir.FatEcto.FatQuery.FatJoin.build_join(FatEcto.FatHospital, query_opts["$right_join"], "$right_join", [])
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in "fat_rooms", on: f0.id == f1.hospital_id, where: f1.incharge == ^"John" and ^true, select: %{^"fat_rooms" => map(f1, [:beds, :capacity, :level])}>