schema.ex (3091B)
1 defmodule Ecto.Adapter.Schema do 2 @moduledoc """ 3 Specifies the schema API required from adapters. 4 """ 5 6 @typedoc "Proxy type to the adapter meta" 7 @type adapter_meta :: Ecto.Adapter.adapter_meta() 8 9 @typedoc "Ecto.Schema metadata fields" 10 @type schema_meta :: %{ 11 autogenerate_id: {schema_field :: atom, source_field :: atom, Ecto.Type.t()}, 12 context: term, 13 prefix: binary | nil, 14 schema: atom, 15 source: binary 16 } 17 18 @type fields :: Keyword.t() 19 @type filters :: Keyword.t() 20 @type constraints :: Keyword.t() 21 @type returning :: [atom] 22 @type placeholders :: [term] 23 @type options :: Keyword.t() 24 25 @type on_conflict :: 26 {:raise, list(), []} 27 | {:nothing, list(), [atom]} 28 | {[atom], list(), [atom]} 29 | {Ecto.Query.t(), list(), [atom]} 30 31 @doc """ 32 Called to autogenerate a value for id/embed_id/binary_id. 33 34 Returns the autogenerated value, or nil if it must be 35 autogenerated inside the storage or raise if not supported. 36 """ 37 @callback autogenerate(field_type :: :id | :binary_id | :embed_id) :: term | nil 38 39 @doc """ 40 Inserts multiple entries into the data store. 41 42 In case an `Ecto.Query` given as any of the field values by the user, 43 it will be sent to the adapter as a tuple with in the shape of 44 `{query, params}`. 45 """ 46 @callback insert_all( 47 adapter_meta, 48 schema_meta, 49 header :: [atom], 50 [[{atom, term | {Ecto.Query.t(), list()}}]], 51 on_conflict, 52 returning, 53 placeholders, 54 options 55 ) :: {non_neg_integer, [[term]] | nil} 56 57 @doc """ 58 Inserts a single new struct in the data store. 59 60 ## Autogenerate 61 62 The primary key will be automatically included in `returning` if the 63 field has type `:id` or `:binary_id` and no value was set by the 64 developer or none was autogenerated by the adapter. 65 """ 66 @callback insert(adapter_meta, schema_meta, fields, on_conflict, returning, options) :: 67 {:ok, fields} | {:invalid, constraints} 68 69 @doc """ 70 Updates a single struct with the given filters. 71 72 While `filters` can be any record column, it is expected that 73 at least the primary key (or any other key that uniquely 74 identifies an existing record) be given as a filter. Therefore, 75 in case there is no record matching the given filters, 76 `{:error, :stale}` is returned. 77 """ 78 @callback update(adapter_meta, schema_meta, fields, filters, returning, options) :: 79 {:ok, fields} | {:invalid, constraints} | {:error, :stale} 80 81 @doc """ 82 Deletes a single struct with the given filters. 83 84 While `filters` can be any record column, it is expected that 85 at least the primary key (or any other key that uniquely 86 identifies an existing record) be given as a filter. Therefore, 87 in case there is no record matching the given filters, 88 `{:error, :stale}` is returned. 89 """ 90 @callback delete(adapter_meta, schema_meta, filters, options) :: 91 {:ok, fields} | {:invalid, constraints} | {:error, :stale} 92 end