Skip to content

Commit

Permalink
Prepare & Close added
Browse files Browse the repository at this point in the history
  • Loading branch information
je4 committed Nov 10, 2021
1 parent 56c5501 commit 45507b1
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/MySQLReprepareStmt/MySQLReprepareStmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,45 @@ func IsRePrepareError(err error) bool {
return false
}

func Prepare(db *sql.DB, query string) (*Stmt, error) {
s := &Stmt{
query: query,
db: db,
}
return s, s.Prepare()
}

func PrepareContext(ctx context.Context, db *sql.DB, query string) (*Stmt, error) {
s := &Stmt{
query: query,
db: db,
}
return s, s.PrepareContext(ctx)
}

func (s *Stmt) Prepare() error {
var err error
if s.stmt, err = s.db.Prepare(s.query); err != nil {
return errors.Wrapf(err, "cannot close statement - %s", s.query)
}
return nil
}

func (s *Stmt) PrepareContext(ctx context.Context) error {
var err error
if s.stmt, err = s.db.PrepareContext(ctx, s.query); err != nil {
return errors.Wrapf(err, "cannot close statement - %s", s.query)
}
return nil
}

func (s *Stmt) Close() error {
if err := s.stmt.Close(); err != nil {
return errors.Wrapf(err, "cannot close statement - %s", s.query)
}
return nil
}

func (s *Stmt) RePrepare() error {
var err error
if err = s.stmt.Close(); err != nil {
Expand Down

0 comments on commit 45507b1

Please sign in to comment.