Generate enum types for sqlbuilder.
This commit is contained in:
parent
7c98fb508c
commit
ca5a30983e
6 changed files with 64 additions and 17 deletions
|
|
@ -70,6 +70,12 @@ func Generate(destDir string, genData GeneratorData) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = generate(schemaInfo, destDir, "enum", enumTypeTemplate, schemaInfo.EnumInfos)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -124,3 +124,17 @@ func (e {{camelize $.Name}}) String() string {
|
|||
}
|
||||
|
||||
`
|
||||
var enumTypeTemplate = `package enum
|
||||
|
||||
import "github.com/sub0zero/go-sqlbuilder/sqlbuilder"
|
||||
|
||||
var {{camelize $.Name}} = &struct {
|
||||
{{- range $index, $element := .Values}}
|
||||
{{camelize $element}} sqlbuilder.StringExpression
|
||||
{{- end}}
|
||||
} {
|
||||
{{- range $index, $element := .Values}}
|
||||
{{camelize $element}}: sqlbuilder.NewEnumValue("{{$element}}"),
|
||||
{{- end}}
|
||||
}
|
||||
`
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
package sqlbuilder
|
||||
|
||||
type cast struct {
|
||||
expression expression
|
||||
expression
|
||||
castType string
|
||||
}
|
||||
|
||||
func newCast(expression expression, castType string) cast {
|
||||
return cast{
|
||||
func newCast(expression expression, castType string) *cast {
|
||||
return &cast{
|
||||
expression: expression,
|
||||
castType: castType,
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@ type boolCast struct {
|
|||
}
|
||||
|
||||
func newBoolCast(expression expression) BoolExpression {
|
||||
boolCast := &boolCast{cast: newCast(expression, "boolean")}
|
||||
boolCast := &boolCast{cast: *newCast(expression, "boolean")}
|
||||
|
||||
boolCast.boolInterfaceImpl.parent = boolCast
|
||||
boolCast.expressionInterfaceImpl.parent = boolCast
|
||||
|
|
@ -40,7 +40,7 @@ type integerCast struct {
|
|||
}
|
||||
|
||||
func newIntegerCast(expression expression) IntegerExpression {
|
||||
integerCast := &integerCast{cast: newCast(expression, "integer")}
|
||||
integerCast := &integerCast{cast: *newCast(expression, "integer")}
|
||||
|
||||
integerCast.integerInterfaceImpl.parent = integerCast
|
||||
integerCast.expressionInterfaceImpl.parent = integerCast
|
||||
|
|
@ -55,7 +55,7 @@ type floatCast struct {
|
|||
}
|
||||
|
||||
func newDoubleCast(expression expression) FloatExpression {
|
||||
floatCast := &floatCast{cast: newCast(expression, "double precision")}
|
||||
floatCast := &floatCast{cast: *newCast(expression, "double precision")}
|
||||
|
||||
floatCast.floatInterfaceImpl.parent = floatCast
|
||||
floatCast.expressionInterfaceImpl.parent = floatCast
|
||||
|
|
@ -70,7 +70,7 @@ type textCast struct {
|
|||
}
|
||||
|
||||
func newTextCast(expression expression) StringExpression {
|
||||
textCast := &textCast{cast: newCast(expression, "text")}
|
||||
textCast := &textCast{cast: *newCast(expression, "text")}
|
||||
|
||||
textCast.stringInterfaceImpl.parent = textCast
|
||||
textCast.expressionInterfaceImpl.parent = textCast
|
||||
|
|
@ -85,7 +85,7 @@ type dateCast struct {
|
|||
}
|
||||
|
||||
func newDateCast(expression expression) DateExpression {
|
||||
dateCast := &dateCast{cast: newCast(expression, "date")}
|
||||
dateCast := &dateCast{cast: *newCast(expression, "date")}
|
||||
|
||||
dateCast.dateInterfaceImpl.parent = dateCast
|
||||
dateCast.expressionInterfaceImpl.parent = dateCast
|
||||
|
|
@ -100,7 +100,7 @@ type timeCast struct {
|
|||
}
|
||||
|
||||
func newTimeCast(expression expression) TimeExpression {
|
||||
timeCast := &timeCast{cast: newCast(expression, "time without time zone")}
|
||||
timeCast := &timeCast{cast: *newCast(expression, "time without time zone")}
|
||||
|
||||
timeCast.timeInterfaceImpl.parent = timeCast
|
||||
timeCast.expressionInterfaceImpl.parent = timeCast
|
||||
|
|
@ -115,7 +115,7 @@ type timezCast struct {
|
|||
}
|
||||
|
||||
func newTimezCast(expression expression) TimezExpression {
|
||||
timezCast := &timezCast{cast: newCast(expression, "time with time zone")}
|
||||
timezCast := &timezCast{cast: *newCast(expression, "time with time zone")}
|
||||
|
||||
timezCast.timezInterfaceImpl.parent = timezCast
|
||||
timezCast.expressionInterfaceImpl.parent = timezCast
|
||||
|
|
@ -130,7 +130,7 @@ type timestampCast struct {
|
|||
}
|
||||
|
||||
func newTimestampCast(expression expression) TimestampExpression {
|
||||
timestampCast := ×tampCast{cast: newCast(expression, "timestamp without time zone")}
|
||||
timestampCast := ×tampCast{cast: *newCast(expression, "timestamp without time zone")}
|
||||
|
||||
timestampCast.timestampInterfaceImpl.parent = timestampCast
|
||||
timestampCast.expressionInterfaceImpl.parent = timestampCast
|
||||
|
|
@ -145,7 +145,7 @@ type timestampzCast struct {
|
|||
}
|
||||
|
||||
func newTimestampzCast(expression expression) TimestampzExpression {
|
||||
timestampzCast := ×tampzCast{cast: newCast(expression, "timestamp with time zone")}
|
||||
timestampzCast := ×tampzCast{cast: *newCast(expression, "timestamp with time zone")}
|
||||
|
||||
timestampzCast.timestampzInterfaceImpl.parent = timestampzCast
|
||||
timestampzCast.expressionInterfaceImpl.parent = timestampzCast
|
||||
|
|
|
|||
21
sqlbuilder/enum_value.go
Normal file
21
sqlbuilder/enum_value.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package sqlbuilder
|
||||
|
||||
type enumValue struct {
|
||||
expressionInterfaceImpl
|
||||
stringInterfaceImpl
|
||||
name string
|
||||
}
|
||||
|
||||
func NewEnumValue(name string) StringExpression {
|
||||
enumValue := &enumValue{name: name}
|
||||
|
||||
enumValue.expressionInterfaceImpl.parent = enumValue
|
||||
enumValue.stringInterfaceImpl.parent = enumValue
|
||||
|
||||
return enumValue
|
||||
}
|
||||
|
||||
func (e enumValue) serialize(statement statementType, out *queryData, options ...serializeOption) error {
|
||||
out.insertConstantArgument(e.name)
|
||||
return nil
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ type expression interface {
|
|||
ASC() orderByClause
|
||||
DESC() orderByClause
|
||||
|
||||
CAST_TO(dbType string) expression
|
||||
CAST_TO_BOOL() BoolExpression
|
||||
CAST_TO_INTEGER() IntegerExpression
|
||||
CAST_TO_DOUBLE() FloatExpression
|
||||
|
|
@ -65,6 +66,10 @@ func (e *expressionInterfaceImpl) DESC() orderByClause {
|
|||
return &orderByClauseImpl{expression: e.parent, ascent: false}
|
||||
}
|
||||
|
||||
func (e *expressionInterfaceImpl) CAST_TO(dbType string) expression {
|
||||
return newCast(e.parent, dbType)
|
||||
}
|
||||
|
||||
func (e *expressionInterfaceImpl) CAST_TO_BOOL() BoolExpression {
|
||||
return newBoolCast(e.parent)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
. "github.com/sub0zero/go-sqlbuilder/sqlbuilder"
|
||||
"github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/dvds/enum"
|
||||
"github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/dvds/model"
|
||||
. "github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/dvds/table"
|
||||
model2 "github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/test_sample/model"
|
||||
|
|
@ -287,10 +288,10 @@ LIMIT 15;
|
|||
query := Film.
|
||||
INNER_JOIN(Language, Film.LanguageID.EQ(Language.LanguageID)).
|
||||
SELECT(Language.AllColumns, Film.AllColumns).
|
||||
WHERE(Film.Rating.EQ(String(model.MpaaRating_NC17.String()))).
|
||||
WHERE(Film.Rating.EQ(enum.MpaaRating.NC17)).
|
||||
LIMIT(15)
|
||||
|
||||
assertQuery(t, query, expectedSql, model.MpaaRating_NC17.String(), int64(15))
|
||||
assertQuery(t, query, expectedSql, int64(15))
|
||||
|
||||
err := query.Query(db, &filmsPerLanguage)
|
||||
|
||||
|
|
@ -742,7 +743,7 @@ FROM dvds.actor
|
|||
`
|
||||
|
||||
rFilmsOnly := Film.SELECT(Film.FilmID, Film.Title, Film.Rating).
|
||||
WHERE(Film.Rating.EQ(String("R"))).
|
||||
WHERE(Film.Rating.EQ(enum.MpaaRating.R)).
|
||||
AsTable("films")
|
||||
|
||||
query := Actor.INNER_JOIN(FilmActor, Actor.ActorID.EQ(FilmActor.FilmID)).
|
||||
|
|
@ -756,7 +757,7 @@ FROM dvds.actor
|
|||
|
||||
fmt.Println(query.Sql())
|
||||
|
||||
assertQuery(t, query, expectedQuery, "R")
|
||||
assertQuery(t, query, expectedQuery)
|
||||
|
||||
dest := []model.Actor{}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue