Skip to content

Commit

Permalink
Merge pull request pingcap#192 from pingcap/shenli/builtin-info
Browse files Browse the repository at this point in the history
expressions: Extract builtin information functions to builtin_info.go
  • Loading branch information
qiuyesuifeng committed Sep 18, 2015
2 parents 63f147d + 146f183 commit 922ffc9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 59 deletions.
26 changes: 0 additions & 26 deletions expression/expressions/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
"strings"

"github.com/juju/errors"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/sessionctx/db"
"github.com/pingcap/tidb/sessionctx/variable"
)

var builtin = map[string]struct {
Expand Down Expand Up @@ -109,26 +106,3 @@ func builtinCoalesce(args []interface{}, ctx map[interface{}]interface{}) (v int
}
return nil, nil
}

func builtinDatabase(args []interface{}, data map[interface{}]interface{}) (v interface{}, err error) {
c, ok := data[ExprEvalArgCtx]
if !ok {
return nil, errors.Errorf("Missing ExprEvalArgCtx when evalue builtin")
}
ctx := c.(context.Context)
d := db.GetCurrentSchema(ctx)
if d == "" {
return nil, nil
}
return d, nil
}

// See: https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_found-rows
func builtinFoundRows(arg []interface{}, data map[interface{}]interface{}) (interface{}, error) {
c, ok := data[ExprEvalArgCtx]
if !ok {
return nil, errors.Errorf("Missing ExprEvalArgCtx when evalue builtin")
}
ctx := c.(context.Context)
return variable.GetSessionVars(ctx).FoundRows, nil
}
49 changes: 49 additions & 0 deletions expression/expressions/builtin_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2013 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSES/QL-LICENSE file.

// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package expressions

import (
"github.com/juju/errors"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/sessionctx/db"
"github.com/pingcap/tidb/sessionctx/variable"
)

// See: https://dev.mysql.com/doc/refman/5.7/en/information-functions.html

func builtinDatabase(args []interface{}, data map[interface{}]interface{}) (v interface{}, err error) {
c, ok := data[ExprEvalArgCtx]
if !ok {
return nil, errors.Errorf("Missing ExprEvalArgCtx when evalue builtin")
}
ctx := c.(context.Context)
d := db.GetCurrentSchema(ctx)
if d == "" {
return nil, nil
}
return d, nil
}

func builtinFoundRows(arg []interface{}, data map[interface{}]interface{}) (interface{}, error) {
c, ok := data[ExprEvalArgCtx]
if !ok {
return nil, errors.Errorf("Missing ExprEvalArgCtx when evalue builtin")
}
ctx := c.(context.Context)
return variable.GetSessionVars(ctx).FoundRows, nil
}
56 changes: 56 additions & 0 deletions expression/expressions/builtin_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package expressions

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/sessionctx/db"
"github.com/pingcap/tidb/sessionctx/variable"
)

var _ = Suite(&testBuiltinInfoSuite{})

type testBuiltinInfoSuite struct {
}

func (s *testBuiltinSuite) TestDatabase(c *C) {
ctx := newMockCtx()
m := map[interface{}]interface{}{}
v, err := builtinDatabase(nil, m)
c.Assert(err, NotNil)

m[ExprEvalArgCtx] = ctx
v, err = builtinDatabase(nil, m)
c.Assert(err, IsNil)
c.Assert(v, IsNil)

db.BindCurrentSchema(ctx, "test")
v, err = builtinDatabase(nil, m)
c.Assert(err, IsNil)
c.Assert(v, Equals, "test")
}

func (s *testBuiltinSuite) TestFoundRows(c *C) {
ctx := newMockCtx()
m := map[interface{}]interface{}{}
v, err := builtinFoundRows(nil, m)
c.Assert(err, NotNil)

variable.BindSessionVars(ctx)

m[ExprEvalArgCtx] = ctx
v, err = builtinFoundRows(nil, m)
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(0))
}
33 changes: 0 additions & 33 deletions expression/expressions/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ package expressions

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/sessionctx/db"
"github.com/pingcap/tidb/sessionctx/variable"
)

var _ = Suite(&testBuiltinSuite{})
Expand All @@ -43,34 +41,3 @@ func (s *testBuiltinSuite) TestCoalesce(c *C) {
c.Assert(err, IsNil)
c.Assert(v, IsNil)
}

func (s *testBuiltinSuite) TestDatabase(c *C) {
ctx := newMockCtx()
m := map[interface{}]interface{}{}
v, err := builtinDatabase(nil, m)
c.Assert(err, NotNil)

m[ExprEvalArgCtx] = ctx
v, err = builtinDatabase(nil, m)
c.Assert(err, IsNil)
c.Assert(v, IsNil)

db.BindCurrentSchema(ctx, "test")
v, err = builtinDatabase(nil, m)
c.Assert(err, IsNil)
c.Assert(v, Equals, "test")
}

func (s *testBuiltinSuite) TestFoundRows(c *C) {
ctx := newMockCtx()
m := map[interface{}]interface{}{}
v, err := builtinFoundRows(nil, m)
c.Assert(err, NotNil)

variable.BindSessionVars(ctx)

m[ExprEvalArgCtx] = ctx
v, err = builtinFoundRows(nil, m)
c.Assert(err, IsNil)
c.Assert(v, Equals, uint64(0))
}

0 comments on commit 922ffc9

Please sign in to comment.