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

Add SetConditions and GetConditions methods to DeleteBuilder, SelectB… #145

Conversation

LeDuyViet
Copy link

Hi, i am building a package on top of this one
This is based multi-layer cache library that uses redis and some other in-memory library.The problem occurs when I need to clear the cache first to ensure related keys are also deleted at the same time. So I need to transfer the conditional statement of the delete builder to select builder to be able to select the records that need to be deleted from the cache

func (r *BaseDAO[T]) DeleteBy(query func(query *sqlbuilder.DeleteBuilder)) error {
	if len(r.DaoLayers) > 1 {
		datas := r.DaoLayers[len(r.DaoLayers)-1].GetAll(func(q *sqlbuilder.SelectBuilder) {
			deleteQuery := flavor.NewDeleteBuilder()
			query(deleteQuery)
			q.SetConditions(deleteQuery.GetConditions())
		})
		for i := 0; i < len(datas); i++ {
			for i := len(r.DaoLayers) - 2; i >= 0; i-- {
				err := r.DaoLayers[i].deleteByID(r.GetID(datas[i]), datas[i])
				if err != nil {
					v_log.V(1).Errorf("BaseDAO::UpdateMultiple - Error deleting cache in layer: %s - %v", r.DaoLayers[i].LayerName(), err)
				}
			}
			go r.PublishDeletedEvent(datas[i])
		}
	}

	if err := r.DaoLayers[len(r.DaoLayers)-1].DeleteBy(query); err != nil {
		v_log.V(1).Errorf("BaseDAO::DeleteBy - Error deleting data in db: %v", err)
		return err
	}

	return nil
}

@huandu
Copy link
Owner

huandu commented Apr 11, 2024

Thanks for your contribution!

I'm considering what's the best way to transfer or copy WHERE clause from one builder to another. Your PR is a good start. I think there can be a better way to address this issue by creating a new interface named WhereClause and letting Cond to implement this new interface.

I don't have a complete design right now. I'll create a new feature branch with my code changes and discuss with you later, maybe tomorrow.

@huandu
Copy link
Owner

huandu commented Apr 13, 2024

I submit #147 to implement the WhereClause struct I mentioned in previous comment. @LeDuyViet can you please take a look at this PR and give me some feedback on the API design? The whereclause_test.go can be a good start for you to see samples using new APIs. I hope the WhereClause can work for all needs that you have now. Thanks.

@huandu
Copy link
Owner

huandu commented Apr 15, 2024

Close this PR as #147 is merged.

@huandu huandu closed this Apr 15, 2024
@LeDuyViet
Copy link
Author

LeDuyViet commented Apr 15, 2024

@huandu I read about your PR, it's great and exactly what I needed. I'm building a package on this one, I hope you can review this once I finish it, thank you so much 😍

@huandu
Copy link
Owner

huandu commented Apr 15, 2024

No problem.

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

Successfully merging this pull request may close these issues.

None yet

3 participants