func (d *database) open() error { // Binding with sqladapter's logic. d.BaseDatabase = sqladapter.NewBaseDatabase(d) // Binding with sqlbuilder. b, err := sqlbuilder.WithSession(d.BaseDatabase, template) if err != nil { return err } d.Builder = b connFn := func() error { sess, err := sql.Open("mysql", d.ConnectionURL().String()) if err == nil { sess.SetConnMaxLifetime(connMaxLifetime) sess.SetMaxIdleConns(maxIdleConns) sess.SetMaxOpenConns(maxOpenConns) return d.BaseDatabase.BindSession(sess) } return err } if err := d.BaseDatabase.WaitForConnection(connFn); err != nil { return err } return nil }
func (d *database) open() error { // Binding with sqladapter's logic. d.BaseDatabase = sqladapter.NewBaseDatabase(d) // Binding with sqlbuilder. b, err := sqlbuilder.WithSession(d.BaseDatabase, template) if err != nil { return err } d.Builder = b openFn := func() error { openFiles := atomic.LoadInt32(&fileOpenCount) if openFiles < maxOpenFiles { sess, err := sql.Open("sqlite3", d.ConnectionURL().String()) if err == nil { if err := d.BaseDatabase.BindSession(sess); err != nil { return err } atomic.AddInt32(&fileOpenCount, 1) return nil } return err } return errTooManyOpenFiles } if err := d.BaseDatabase.WaitForConnection(openFn); err != nil { return err } return nil }
// New wraps the given *sql.DB session and creates a new db session. func New(sess *sql.DB) (sqlbuilder.Database, error) { d, err := newDatabase(nil) if err != nil { return nil, err } // Binding with sqladapter's logic. d.BaseDatabase = sqladapter.NewBaseDatabase(d) // Binding with sqlbuilder. b, err := sqlbuilder.WithSession(d.BaseDatabase, template) if err != nil { return nil, err } d.Builder = b if err := d.BaseDatabase.BindSession(sess); err != nil { return nil, err } return d, nil }
// NewTx returns a transaction session. func NewTx(sqlTx *sql.Tx) (sqlbuilder.Tx, error) { d, err := newDatabase(nil) if err != nil { return nil, err } // Binding with sqladapter's logic. d.BaseDatabase = sqladapter.NewBaseDatabase(d) // Binding with sqlbuilder. b, err := sqlbuilder.WithSession(d.BaseDatabase, template) if err != nil { return nil, err } d.Builder = b if err := d.BaseDatabase.BindTx(sqlTx); err != nil { return nil, err } newTx := sqladapter.NewTx(d) return &tx{DatabaseTx: newTx}, nil }