forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
134 lines (118 loc) · 3.01 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 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 executor
import (
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/sessionctx/variable"
)
// recordSet wraps an executor, implements ast.RecordSet interface
type recordSet struct {
fields []*ast.ResultField
executor Executor
schema expression.Schema
}
func (a *recordSet) Fields() ([]*ast.ResultField, error) {
if len(a.fields) == 0 {
for _, col := range a.schema {
rf := &ast.ResultField{
ColumnAsName: col.ColName,
TableAsName: col.TblName,
DBName: col.DBName,
Column: &model.ColumnInfo{
FieldType: *col.RetType,
Name: col.ColName,
},
}
a.fields = append(a.fields, rf)
}
}
return a.fields, nil
}
func (a *recordSet) Next() (*ast.Row, error) {
row, err := a.executor.Next()
if err != nil || row == nil {
return nil, errors.Trace(err)
}
return &ast.Row{Data: row.Data}, nil
}
func (a *recordSet) Close() error {
return a.executor.Close()
}
type statement struct {
is infoschema.InfoSchema
plan plan.Plan
text string
isDDL bool
}
func (a *statement) OriginText() string {
return a.text
}
func (a *statement) SetText(text string) {
a.text = text
return
}
func (a *statement) IsDDL() bool {
return a.isDDL
}
func (a *statement) Exec(ctx context.Context) (ast.RecordSet, error) {
b := newExecutorBuilder(ctx, a.is)
e := b.build(a.plan)
if b.err != nil {
return nil, errors.Trace(b.err)
}
if executorExec, ok := e.(*ExecuteExec); ok {
err := executorExec.Build()
if err != nil {
return nil, errors.Trace(err)
}
e = executorExec.StmtExec
}
if len(e.Fields()) == 0 && len(e.Schema()) == 0 {
// Write statements do not have record set, check if snapshot ts is set.
switch e.(type) {
case *DeleteExec, *InsertExec, *UpdateExec, *ReplaceExec, *LoadData, *DDLExec:
snapshotTS := variable.GetSnapshotTS(ctx)
if snapshotTS != 0 {
return nil, errors.New("Can not execute write statement when 'tidb_snapshot' is set.")
}
}
// No result fields means no Recordset.
defer e.Close()
for {
row, err := e.Next()
if err != nil {
return nil, errors.Trace(err)
}
if row == nil {
return nil, nil
}
}
}
fs := e.Fields()
for _, f := range fs {
if len(f.ColumnAsName.O) == 0 {
f.ColumnAsName = f.Column.Name
}
}
return &recordSet{
executor: e,
fields: fs,
schema: e.Schema(),
}, nil
}