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 Function to Ensure Dialect is Imported #409

Open
3 tasks
ktogo opened this issue Feb 22, 2024 · 1 comment
Open
3 tasks

Add Function to Ensure Dialect is Imported #409

ktogo opened this issue Feb 22, 2024 · 1 comment
Assignees

Comments

@ktogo
Copy link

ktogo commented Feb 22, 2024

Is your feature request related to a problem? Please describe.

Goqu generates queries with unintended default dialect when forget to import the dialect in every files.

This situation especially occurs when running single unit test.


Describe the solution you'd like

Provide following functions which requires specific dialect to be imported.

package goqu

// RequireDialect returns dialect if available, otherwise returns an error.
func RequireDialect(dialect string) (DialectWrapper, error)

// MustDialect returns dialect if available, otherwise panics.
func MustDialect(dialect string) DialectWrapper

Describe alternatives you've considered

The following code achieves it (by non-straightforward way though), but I would like to suggest this to be a part of goqu official functions.

func RequireDialect(dialect string) (goqu.DialectWrapper, error) {
	d := goqu.Dialect(dialect)
	if goqu.GetDialect(dialect).Dialect() == dialect {
		return d, nil
	}
	return d, errors.New("goqu dialect unavailable: " + dialect)
}

func MustDialect(dialect string) goqu.DialectWrapper {
	d, err := RequireDialect(dialect)
	if err != nil {
		panic(err)
	}
	return d
}

Dialect

  • postgres
  • mysql
  • sqlite3

Additional context
Add any other context or screenshots about the feature request here.

@ktogo
Copy link
Author

ktogo commented Feb 25, 2024

Or another approach:

package goqu

// RequireDialect returns an error if specified dialect is unavailable.
func RequireDialect(dialect string) (error)

Usage:

if err := goqu.RequireDialect("postgres"); err != nil {
	panic(err)
}

goqu.Dialect("postgres").Select("*") //...

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