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

A table wrapper analogous to the dialect wrapper #414

Open
3 tasks done
IanBurgan opened this issue Mar 26, 2024 · 0 comments
Open
3 tasks done

A table wrapper analogous to the dialect wrapper #414

IanBurgan opened this issue Mar 26, 2024 · 0 comments
Assignees

Comments

@IanBurgan
Copy link

Is your feature request related to a problem? Please describe.
Currently if the end user has a service that pertains to only one table, they have to specify the table every time that they call From, Insert, etc...

Describe the solution you'd like
The DialectWrapper enables setting the dialect once with Dialect(dialect string) and then calling From, Insert, Update, etc. without needing to specify WithDialect(dialect string). It would be nice to have a similar feature for setting the table once without needing to pass in the table to all of the above listed methods.

One possible solution would be to have a TableWrapper on top of the dialect wrapper. This would require specifying a dialect when you want to specify a table, but I don't think that is an issue. Here is what using it would look like:

table := Dialect("postgres").Table("users")

query := table.Select(cols...)

query := table.Insert()

Here is what the implementation might look like:

type TableWrapper struct {
	table string
	DialectWrapper
}

func (dw DialectWrapper) Table(table string) TableWrapper {
	return TableWrapper{
		DialectWrapper: dw,
		table: table,
	}
}

// Create a new dataset for creating SELECT sql statements
func (tw TableWrapper) Select(cols ...interface{}) *SelectDataset {
	return tw.DialectWrapper.From(tw.table).Select(cols...)
}

Describe alternatives you've considered
It would also be possible to have a table wrapper that doesn't directly rely on the dialect wrapper, but it would make it harder to use both at the same time.

Dialect

  • postgres
  • mysql
  • sqlite3

Additional Context
I can create the PR for this with tests, docs, and examples if it's something that seems like it's worth adding

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

2 participants