goqu icon indicating copy to clipboard operation
goqu copied to clipboard

Add Function to Ensure Dialect is Imported

Open ktogo opened this issue 4 months ago • 1 comments

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 avatar Feb 22 '24 12:02 ktogo

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("*") //...

ktogo avatar Feb 25 '24 04:02 ktogo