github.com/RevenueMonster/sqlike@v1.0.6/sqlike/options/connect.go (about)

     1  package options
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  
     7  	"github.com/RevenueMonster/sqlike/sql/charset"
     8  	"github.com/RevenueMonster/sqlike/sqlike/logs"
     9  )
    10  
    11  // ConnectOptions :
    12  type ConnectOptions struct {
    13  	raw      string
    14  	Username string
    15  	Password string
    16  	Protocol string
    17  	Host     string
    18  	Port     string
    19  	Socket   string
    20  	Charset  charset.Code
    21  	Collate  string
    22  	Logger   logs.Logger
    23  }
    24  
    25  // Connect :
    26  func Connect() *ConnectOptions {
    27  	return &ConnectOptions{}
    28  }
    29  
    30  // RawConnStr :
    31  func (opt *ConnectOptions) RawConnStr() string {
    32  	return opt.raw
    33  }
    34  
    35  // ApplyURI :
    36  func (opt *ConnectOptions) ApplyURI(uri string) *ConnectOptions {
    37  	opt.raw = uri
    38  	return opt
    39  }
    40  
    41  // SetUsername :
    42  func (opt *ConnectOptions) SetUsername(username string) *ConnectOptions {
    43  	opt.Username = strings.TrimSpace(username)
    44  	return opt
    45  }
    46  
    47  // SetPassword :
    48  func (opt *ConnectOptions) SetPassword(password string) *ConnectOptions {
    49  	opt.Password = password
    50  	return opt
    51  }
    52  
    53  // SetProtocol :
    54  func (opt *ConnectOptions) SetProtocol(network string) *ConnectOptions {
    55  	opt.Protocol = strings.TrimSpace(network)
    56  	return opt
    57  }
    58  
    59  // SetHost :
    60  func (opt *ConnectOptions) SetHost(host string) *ConnectOptions {
    61  	// if ip := net.ParseIP(opt.Host); ip != nil {
    62  	// 	panic("invalid ip address")
    63  	// }
    64  	opt.Host = strings.TrimSpace(host)
    65  	return opt
    66  }
    67  
    68  // SetPort :
    69  func (opt *ConnectOptions) SetPort(port string) *ConnectOptions {
    70  	if len(regexp.MustCompile("[0-9]+").FindAllString(port, -1)) != 1 {
    71  		panic("invalid port format")
    72  	}
    73  	opt.Port = strings.TrimSpace(port)
    74  	return opt
    75  }
    76  
    77  // SetSocket :
    78  func (opt *ConnectOptions) SetSocket(sckt string) *ConnectOptions {
    79  	opt.Socket = strings.TrimSpace(sckt)
    80  	return opt
    81  }
    82  
    83  // SetCharset :
    84  func (opt *ConnectOptions) SetCharset(code charset.Code) *ConnectOptions {
    85  	opt.Charset = code
    86  	return opt
    87  }
    88  
    89  // SetCollate :
    90  func (opt *ConnectOptions) SetCollate(collate string) *ConnectOptions {
    91  	opt.Collate = collate
    92  	return opt
    93  }