github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/executor/adapter.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package executor
    15  
    16  import (
    17  	"github.com/insionng/yougam/libraries/juju/errors"
    18  	"github.com/insionng/yougam/libraries/pingcap/tidb/ast"
    19  	"github.com/insionng/yougam/libraries/pingcap/tidb/context"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/infoschema"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/optimizer/plan"
    22  )
    23  
    24  // recordSet wraps an executor, implements ast.RecordSet interface
    25  type recordSet struct {
    26  	fields   []*ast.ResultField
    27  	executor Executor
    28  }
    29  
    30  func (a *recordSet) Fields() ([]*ast.ResultField, error) {
    31  	return a.fields, nil
    32  }
    33  
    34  func (a *recordSet) Next() (*ast.Row, error) {
    35  	row, err := a.executor.Next()
    36  	if err != nil || row == nil {
    37  		return nil, errors.Trace(err)
    38  	}
    39  	return &ast.Row{Data: row.Data}, nil
    40  }
    41  
    42  func (a *recordSet) Close() error {
    43  	return a.executor.Close()
    44  }
    45  
    46  type statement struct {
    47  	is   infoschema.InfoSchema
    48  	plan plan.Plan
    49  }
    50  
    51  func (a *statement) OriginText() string {
    52  	return ""
    53  }
    54  
    55  func (a *statement) SetText(text string) {
    56  	return
    57  }
    58  
    59  func (a *statement) IsDDL() bool {
    60  	return false
    61  }
    62  
    63  func (a *statement) Exec(ctx context.Context) (ast.RecordSet, error) {
    64  	b := newExecutorBuilder(ctx, a.is)
    65  	e := b.build(a.plan)
    66  	if b.err != nil {
    67  		return nil, errors.Trace(b.err)
    68  	}
    69  
    70  	if executorExec, ok := e.(*ExecuteExec); ok {
    71  		err := executorExec.Build()
    72  		if err != nil {
    73  			return nil, errors.Trace(err)
    74  		}
    75  		e = executorExec.StmtExec
    76  	}
    77  
    78  	if len(e.Fields()) == 0 {
    79  		// No result fields means no Recordset.
    80  		defer e.Close()
    81  		for {
    82  			row, err := e.Next()
    83  			if err != nil {
    84  				return nil, errors.Trace(err)
    85  			}
    86  			if row == nil {
    87  				return nil, nil
    88  			}
    89  		}
    90  	}
    91  
    92  	fs := e.Fields()
    93  	for _, f := range fs {
    94  		if len(f.ColumnAsName.O) == 0 {
    95  			f.ColumnAsName = f.Column.Name
    96  		}
    97  	}
    98  	return &recordSet{
    99  		executor: e,
   100  		fields:   fs,
   101  	}, nil
   102  }