github.com/moznion/go-optional@v0.11.1-0.20240312043125-6881072e44c1/sql_driver.go (about)

     1  package optional
     2  
     3  import (
     4  	"database/sql/driver"
     5  )
     6  
     7  // Scan assigns a value from a database driver.
     8  // This method is required from database/sql.Scanner interface.
     9  func (o *Option[T]) Scan(src any) error {
    10  	if src == nil {
    11  		*o = None[T]()
    12  		return nil
    13  	}
    14  
    15  	var v T
    16  	err := sqlConvertAssign(&v, src)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	*o = Some[T](v)
    22  	return nil
    23  }
    24  
    25  // Value returns a driver Value.
    26  // This method is required from database/sql/driver.Valuer interface.
    27  func (o Option[T]) Value() (driver.Value, error) {
    28  	if o.IsNone() {
    29  		return nil, nil
    30  	}
    31  	return driver.DefaultParameterConverter.ConvertValue(o.Unwrap())
    32  }