Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #287 from erizocosmico/feature/engine-constructor
Browse files Browse the repository at this point in the history
sqle: change Engine constructor to accept a Config struct
  • Loading branch information
ajnavarro authored Jul 16, 2018
2 parents 0614764 + 2abbea9 commit a2b3539
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
18 changes: 15 additions & 3 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ import (
"gopkg.in/src-d/go-mysql-server.v0/sql/parse"
)

// Config for the Engine.
type Config struct {
// VersionPostfix to display with the `VERSION()` UDF.
VersionPostfix string
}

// Engine is a SQL engine.
type Engine struct {
Catalog *sql.Catalog
Analyzer *analyzer.Analyzer
}

// New creates a new Engine
func New(c *sql.Catalog, a *analyzer.Analyzer, versionPostfix string) *Engine {
// New creates a new Engine with custom configuration. To create an Engine with
// the default settings use `NewDefault`.
func New(c *sql.Catalog, a *analyzer.Analyzer, cfg *Config) *Engine {
var versionPostfix string
if cfg != nil {
versionPostfix = cfg.VersionPostfix
}

c.RegisterFunctions(function.Defaults)
c.RegisterFunction("version", sql.FunctionN(function.NewVersion(versionPostfix)))

Expand All @@ -28,7 +40,7 @@ func NewDefault() *Engine {
c := sql.NewCatalog()
a := analyzer.NewDefault(c)

return New(c, a, "go-mysql-server")
return New(c, a, nil)
}

// Query executes a query without attaching to any context.
Expand Down
4 changes: 2 additions & 2 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ var queries = []struct {
{
"SELECT version()",
[]sql.Row{
{string("8.0.11-go-mysql-server")},
{string("8.0.11")},
},
},
{
Expand Down Expand Up @@ -936,7 +936,7 @@ func TestReadOnly(t *testing.T) {

a := analyzer.NewBuilder(catalog).ReadOnly().Build()
a.CurrentDatabase = "mydb"
e := sqle.New(catalog, a, "")
e := sqle.New(catalog, a, nil)

_, _, err := e.Query(sql.NewEmptyContext(), `SELECT i FROM mytable`)
require.NoError(err)
Expand Down
28 changes: 16 additions & 12 deletions sql/expression/function/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package function

import (
"fmt"

"gopkg.in/src-d/go-mysql-server.v0/sql"
)

Expand All @@ -12,38 +14,40 @@ type Version string
// NewVersion creates a new Version UDF.
func NewVersion(versionPostfix string) func(...sql.Expression) (sql.Expression, error) {
return func(...sql.Expression) (sql.Expression, error) {
// need to return pointer because sql.Expression methods expect pointer receivers
e := Version(versionPostfix)
return &e, nil
return Version(versionPostfix), nil
}
}

// Type implements the Expression interface.
func (f *Version) Type() sql.Type { return sql.Text }
func (f Version) Type() sql.Type { return sql.Text }

// IsNullable implements the Expression interface.
func (f *Version) IsNullable() bool {
func (f Version) IsNullable() bool {
return false
}

func (f *Version) String() string {
return "version"
func (f Version) String() string {
return "VERSION()"
}

// TransformUp implements the Expression interface.
func (f *Version) TransformUp(fn sql.TransformExprFunc) (sql.Expression, error) {
func (f Version) TransformUp(fn sql.TransformExprFunc) (sql.Expression, error) {
return fn(f)
}

// Resolved implements the Expression interface.
func (f *Version) Resolved() bool {
func (f Version) Resolved() bool {
return true
}

// Children implements the Expression interface.
func (f *Version) Children() []sql.Expression { return nil }
func (f Version) Children() []sql.Expression { return nil }

// Eval implements the Expression interface.
func (f *Version) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return mysqlVersion + "-" + string(*f), nil
func (f Version) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if f == "" {
return mysqlVersion, nil
}

return fmt.Sprintf("%s-%s", mysqlVersion, string(f)), nil
}
8 changes: 7 additions & 1 deletion sql/expression/function/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ func TestNewVersion(t *testing.T) {
f, _ := NewVersion(versionPostfix)()
ctx := sql.NewEmptyContext()

val, err := f.Eval(ctx, sql.NewRow(nil))
val, err := f.Eval(ctx, nil)
require.NoError(err)
require.Equal("8.0.11-"+versionPostfix, val)

f, _ = NewVersion("")()

val, err = f.Eval(ctx, nil)
require.NoError(err)
require.Equal("8.0.11", val)
}

0 comments on commit a2b3539

Please sign in to comment.