uuid.ex (748B)
1 defmodule Postgrex.Extensions.UUID do 2 @moduledoc false 3 import Postgrex.BinaryUtils, warn: false 4 use Postgrex.BinaryExtension, send: "uuid_send" 5 6 def init(opts), do: Keyword.fetch!(opts, :decode_binary) 7 8 def encode(_) do 9 quote location: :keep, generated: true do 10 uuid when is_binary(uuid) and byte_size(uuid) == 16 -> 11 [<<16::int32()>> | uuid] 12 13 other -> 14 raise DBConnection.EncodeError, Postgrex.Utils.encode_msg(other, "a binary of 16 bytes") 15 end 16 end 17 18 def decode(:copy) do 19 quote location: :keep do 20 <<16::int32(), uuid::binary-16>> -> :binary.copy(uuid) 21 end 22 end 23 24 def decode(:reference) do 25 quote location: :keep do 26 <<16::int32(), uuid::binary-16>> -> uuid 27 end 28 end 29 end