|
| 1 | +# |
| 2 | +# This file is part of Astarte. |
| 3 | +# |
| 4 | +# Copyright 2025 SECO Mind Srl |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# SPDX-License-Identifier: Apache-2.0 |
| 19 | +# |
| 20 | + |
| 21 | +# TODO: Copied from astarte_data_access PR #71, see: https://github.com/astarte-platform/astarte_data_access/pull/71 |
| 22 | +# use `astarte_data_access` when it will be merged |
| 23 | +defmodule Astarte.AppEngine.API.Repo do |
| 24 | + @moduledoc false |
| 25 | + use Ecto.Repo, otp_app: :astarte_appengine_api, adapter: Exandra |
| 26 | + alias Astarte.DataAccess.Config |
| 27 | + require Ecto.Query |
| 28 | + |
| 29 | + @impl Ecto.Repo |
| 30 | + def init(_context, config) do |
| 31 | + config = |
| 32 | + Config.xandra_options!() |
| 33 | + |> Keyword.merge(config) |
| 34 | + |
| 35 | + {:ok, config} |
| 36 | + end |
| 37 | + |
| 38 | + def fetch(queryable, id, opts \\ []) do |
| 39 | + queryable |
| 40 | + |> query_for_get(id) |
| 41 | + |> fetch_one(opts) |
| 42 | + end |
| 43 | + |
| 44 | + def fetch_by(queryable, clauses, opts \\ []) do |
| 45 | + queryable |
| 46 | + |> Ecto.Query.where([], ^Enum.to_list(clauses)) |
| 47 | + |> fetch_one(opts) |
| 48 | + end |
| 49 | + |
| 50 | + def fetch_one(queryable, opts \\ []) do |
| 51 | + {error, opts} = Keyword.pop_first(opts, :error, :not_found) |
| 52 | + |
| 53 | + case all(queryable, opts) do |
| 54 | + [item] -> {:ok, item} |
| 55 | + [] -> {:error, error} |
| 56 | + other -> raise Ecto.MultipleResultsError, queryable: queryable, count: length(other) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + # Copyright: elixir-ecto/ecto |
| 61 | + defp query_for_get(_queryable, nil) do |
| 62 | + raise ArgumentError, "cannot perform Ecto.Repo.get/2 because the given value is nil" |
| 63 | + end |
| 64 | + |
| 65 | + # Copyright: elixir-ecto/ecto |
| 66 | + defp query_for_get(queryable, id) do |
| 67 | + query = Ecto.Queryable.to_query(queryable) |
| 68 | + schema = assert_schema!(query) |
| 69 | + |
| 70 | + case schema.__schema__(:primary_key) do |
| 71 | + [pk] -> |
| 72 | + Ecto.Query.from(x in query, where: field(x, ^pk) == ^id) |
| 73 | + |
| 74 | + pks -> |
| 75 | + raise ArgumentError, |
| 76 | + "Ecto.Repo.get/2 requires the schema #{inspect(schema)} " <> |
| 77 | + "to have exactly one primary key, got: #{inspect(pks)}" |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + # Copyright: elixir-ecto/ecto |
| 82 | + defp assert_schema!(%{from: %{source: {_source, schema}}}) when schema != nil, do: schema |
| 83 | + |
| 84 | + # Copyright: elixir-ecto/ecto |
| 85 | + defp assert_schema!(query) do |
| 86 | + raise Ecto.QueryError, |
| 87 | + query: query, |
| 88 | + message: "expected a from expression with a schema" |
| 89 | + end |
| 90 | +end |
0 commit comments