Skip to content

Commit

Permalink
Split dialect interface (#426)
Browse files Browse the repository at this point in the history
This will ease testing and help to break dependencies.
  • Loading branch information
stanislas-m committed Aug 25, 2019
1 parent 219ea04 commit c73d3a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
26 changes: 19 additions & 7 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,37 @@ import (
"github.com/gobuffalo/pop/columns"
)

type crudable interface {
SelectOne(store, *Model, Query) error
SelectMany(store, *Model, Query) error
Create(store, *Model, columns.Columns) error
Update(store, *Model, columns.Columns) error
Destroy(store, *Model) error
}

type fizzable interface {
FizzTranslator() fizz.Translator
}

type quotable interface {
Quote(key string) string
}

type dialect interface {
crudable
fizzable
quotable
Name() string
URL() string
MigrationURL() string
Details() *ConnectionDetails
TranslateSQL(string) string
Create(store, *Model, columns.Columns) error
Update(store, *Model, columns.Columns) error
Destroy(store, *Model) error
SelectOne(store, *Model, Query) error
SelectMany(store, *Model, Query) error
CreateDB() error
DropDB() error
DumpSchema(io.Writer) error
LoadSchema(io.Reader) error
FizzTranslator() fizz.Translator
Lock(func() error) error
TruncateAll(*Connection) error
Quote(key string) string
}

type afterOpenable interface {
Expand Down
10 changes: 3 additions & 7 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ func (commonDialect) Quote(key string) string {
return fmt.Sprintf(`"%s"`, key)
}

type quoter interface {
Quote(key string) string
}

func genericCreate(s store, model *Model, cols columns.Columns, quoter quoter) error {
func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable) error {
keyType := model.PrimaryKeyType()
switch keyType {
case "int", "int64":
Expand Down Expand Up @@ -90,7 +86,7 @@ func genericCreate(s store, model *Model, cols columns.Columns, quoter quoter) e
return errors.Errorf("can not use %s as a primary key type!", keyType)
}

func genericUpdate(s store, model *Model, cols columns.Columns, quoter quoter) error {
func genericUpdate(s store, model *Model, cols columns.Columns, quoter quotable) error {
stmt := fmt.Sprintf("UPDATE %s SET %s WHERE %s", quoter.Quote(model.TableName()), cols.Writeable().QuotedUpdateString(quoter), model.whereNamedID())
log(logging.SQL, stmt, model.ID())
_, err := s.NamedExec(stmt, model.Value)
Expand All @@ -100,7 +96,7 @@ func genericUpdate(s store, model *Model, cols columns.Columns, quoter quoter) e
return nil
}

func genericDestroy(s store, model *Model, quoter quoter) error {
func genericDestroy(s store, model *Model, quoter quotable) error {
stmt := fmt.Sprintf("DELETE FROM %s WHERE %s", quoter.Quote(model.TableName()), model.whereID())
_, err := genericExec(s, stmt, model.ID())
if err != nil {
Expand Down

0 comments on commit c73d3a9

Please sign in to comment.