github.com/jxskiss/gopkg@v0.17.3/sqlutil/types_bitmap.go (about)

     1  package sqlutil
     2  
     3  import (
     4  	"database/sql"
     5  	"database/sql/driver"
     6  )
     7  
     8  // Bitmap represents a bitmap value, it implements sql/driver.Valuer and sql.Scanner.
     9  // Bitmap provides Get, Set and Clear methods to manipulate the bitmap value.
    10  type Bitmap int
    11  
    12  // Value implements driver.Valuer interface.
    13  func (b Bitmap) Value() (driver.Value, error) {
    14  	return int64(b), nil
    15  }
    16  
    17  // Scan implements sql.Scanner interface.
    18  func (b *Bitmap) Scan(src interface{}) error {
    19  	var tmp sql.NullInt64
    20  	err := tmp.Scan(src)
    21  	if err == nil {
    22  		*b = Bitmap(tmp.Int64)
    23  	}
    24  	return err
    25  }
    26  
    27  func (b Bitmap) Get(mask int) bool {
    28  	return int(b)&mask != 0
    29  }
    30  
    31  func (b *Bitmap) Set(mask int) {
    32  	*b |= Bitmap(mask)
    33  }
    34  
    35  func (b *Bitmap) Clear(mask int) {
    36  	*b &= ^Bitmap(mask)
    37  }