Path: blob/main/vendor/github.com/google/uuid/sql.go
2875 views
// Copyright 2016 Google Inc. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package uuid56import (7"database/sql/driver"8"fmt"9)1011// Scan implements sql.Scanner so UUIDs can be read from databases transparently.12// Currently, database types that map to string and []byte are supported. Please13// consult database-specific driver documentation for matching types.14func (uuid *UUID) Scan(src interface{}) error {15switch src := src.(type) {16case nil:17return nil1819case string:20// if an empty UUID comes from a table, we return a null UUID21if src == "" {22return nil23}2425// see Parse for required string format26u, err := Parse(src)27if err != nil {28return fmt.Errorf("Scan: %v", err)29}3031*uuid = u3233case []byte:34// if an empty UUID comes from a table, we return a null UUID35if len(src) == 0 {36return nil37}3839// assumes a simple slice of bytes if 16 bytes40// otherwise attempts to parse41if len(src) != 16 {42return uuid.Scan(string(src))43}44copy((*uuid)[:], src)4546default:47return fmt.Errorf("Scan: unable to scan type %T into UUID", src)48}4950return nil51}5253// Value implements sql.Valuer so that UUIDs can be written to databases54// transparently. Currently, UUIDs map to strings. Please consult55// database-specific driver documentation for matching types.56func (uuid UUID) Value() (driver.Value, error) {57return uuid.String(), nil58}596061