Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define actions interface #13

Open
ghost opened this issue Oct 5, 2019 · 0 comments
Open

Define actions interface #13

ghost opened this issue Oct 5, 2019 · 0 comments

Comments

@ghost
Copy link

ghost commented Oct 5, 2019

Hi,
for my *sql drivers I am using the following interface:

type SqlConnection interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	Exec(query string, args ...interface{}) (sql.Result, error)
}

this allows me to switch between the sql database and sql transaction object, since both support these methods. Then, in my (mysql/...)driver's methods I simply call driver.conn.Query() and so on, and not care about the underlying object.

The driver has the following methods:

BeginTransaction(ctx context.Context) (Driver, error)
CommitTransaction() error
RollbackTransaction() error
InTransaction() bool

with the following implementation:

func (r *repo) BeginTransaction(ctx context.Context) (foo.Repository, error) {
	r.mx.Lock()
	defer r.mx.Unlock()

	// if we're already in transaction, keep the same instance
	if _, ok := r.conn.(driver.Tx); ok {
		return r, nil
	}

	// if we're not in transaction, create new instance
	tx, err := r.opts.db.BeginTx(ctx, nil)
	if err != nil {
		return nil, ErrOut(err)
	}

	inTx := &repo{
		conn: tx,
		opts: r.opts,
	}

	return inTx, nil
}

func (r *repo) CommitTransaction() error {
	r.mx.Lock()
	defer r.mx.Unlock()

	if tx, ok := r.conn.(driver.Tx); ok {
		err := tx.Commit()
		r.conn = r.opts.db
		return ErrOut(err)
	}

	return ErrOut(ErrNotInTx)
}

func (r *repo) RollbackTransaction() error {
	r.mx.Lock()
	defer r.mx.Unlock()

	if tx, ok := r.conn.(driver.Tx); ok {
		err := tx.Rollback()
		r.conn = r.opts.db
		return ErrOut(err)
	}

	return ErrOut(ErrNotInTx)
}

I am now trying to use this library as replacement for mysql db because I think it will work better for me due to some graph edges logic I am needing that mysql is not suitable for.

And it would be nice to be able to follow my established logic with this library instead of having to check if I have open transaction or not and switch between the direct method or the transactioned one(ie. Delete() vs TxDelete()....). so I think having an established interface in bh would take care of this nicely and I tihnk many people would find it useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants