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

     1  package sqlike
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"database/sql/driver"
     7  	"errors"
     8  	"log"
     9  
    10  	"github.com/RevenueMonster/sqlike/sql/dialect"
    11  	sqldialect "github.com/RevenueMonster/sqlike/sql/dialect"
    12  	"github.com/RevenueMonster/sqlike/sql/dialect/mysql"
    13  	"github.com/RevenueMonster/sqlike/sqlike/options"
    14  )
    15  
    16  func init() {
    17  	dialect.RegisterDialect("mysql", mysql.New())
    18  }
    19  
    20  // Open : open connection to sql server with connection string
    21  func Open(ctx context.Context, driver string, opt *options.ConnectOptions) (client *Client, err error) {
    22  	if opt == nil {
    23  		return nil, errors.New("sqlike: invalid connection options <nil>")
    24  	}
    25  	var db *sql.DB
    26  	dialect := sqldialect.GetDialectByDriver(driver)
    27  	connStr := dialect.Connect(opt)
    28  	log.Println("Connecting to :", connStr)
    29  	db, err = sql.Open(driver, connStr)
    30  	if err != nil {
    31  		return
    32  	}
    33  	client, err = newClient(ctx, driver, db, dialect, opt.Charset, opt.Collate)
    34  	return
    35  }
    36  
    37  // MustOpen : must open will panic if it cannot establish a connection to sql server
    38  func MustOpen(ctx context.Context, driver string, opt *options.ConnectOptions) *Client {
    39  	client, err := Open(ctx, driver, opt)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	return client
    44  }
    45  
    46  // Connect : connect and ping the sql server, throw error when unable to ping
    47  func Connect(ctx context.Context, driver string, opt *options.ConnectOptions) (client *Client, err error) {
    48  	client, err = Open(ctx, driver, opt)
    49  	if err != nil {
    50  		return
    51  	}
    52  	err = client.PingContext(ctx)
    53  	if err != nil {
    54  		client.Close()
    55  		return
    56  	}
    57  	return
    58  }
    59  
    60  // MustConnect will panic if cannot connect to sql server
    61  func MustConnect(ctx context.Context, driver string, opt *options.ConnectOptions) *Client {
    62  	conn, err := Connect(ctx, driver, opt)
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	return conn
    67  }
    68  
    69  // ConnectDB :
    70  func ConnectDB(ctx context.Context, driver string, conn driver.Connector) (*Client, error) {
    71  	db := sql.OpenDB(conn)
    72  	dialect := sqldialect.GetDialectByDriver(driver)
    73  	client, err := newClient(ctx, driver, db, dialect, "", "")
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	if err := client.PingContext(ctx); err != nil {
    78  		return nil, err
    79  	}
    80  	return client, nil
    81  }
    82  
    83  // MustConnectDB :
    84  func MustConnectDB(ctx context.Context, driver string, conn driver.Connector) *Client {
    85  	client, err := ConnectDB(ctx, driver, conn)
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  	return client
    90  }