forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddl_procedure.go
166 lines (148 loc) · 4.32 KB
/
ddl_procedure.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2021 Dolthub, 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"fmt"
"time"
"github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/go-mysql-server/sql"
)
type CreateProcedure struct {
*Procedure
ddlNode
BodyString string
}
var _ sql.Node = (*CreateProcedure)(nil)
var _ sql.Databaser = (*CreateProcedure)(nil)
var _ sql.DebugStringer = (*CreateProcedure)(nil)
var _ sql.CollationCoercible = (*CreateProcedure)(nil)
// NewCreateProcedure returns a *CreateProcedure node.
func NewCreateProcedure(
db sql.Database,
name,
definer string,
params []ProcedureParam,
createdAt, modifiedAt time.Time,
securityContext ProcedureSecurityContext,
characteristics []Characteristic,
body sql.Node,
comment, createString, bodyString string,
) *CreateProcedure {
procedure := NewProcedure(
name,
definer,
params,
securityContext,
comment,
characteristics,
createString,
body,
createdAt,
modifiedAt)
return &CreateProcedure{
Procedure: procedure,
BodyString: bodyString,
ddlNode: ddlNode{db},
}
}
// Database implements the sql.Databaser interface.
func (c *CreateProcedure) Database() sql.Database {
return c.Db
}
// WithDatabase implements the sql.Databaser interface.
func (c *CreateProcedure) WithDatabase(database sql.Database) (sql.Node, error) {
cp := *c
cp.Db = database
return &cp, nil
}
// Resolved implements the sql.Node interface.
func (c *CreateProcedure) Resolved() bool {
return c.ddlNode.Resolved() && c.Procedure.Resolved()
}
func (c *CreateProcedure) IsReadOnly() bool {
return false
}
// Schema implements the sql.Node interface.
func (c *CreateProcedure) Schema() sql.Schema {
return types.OkResultSchema
}
// Children implements the sql.Node interface.
func (c *CreateProcedure) Children() []sql.Node {
return []sql.Node{c.Procedure}
}
// WithChildren implements the sql.Node interface.
func (c *CreateProcedure) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(c, len(children), 1)
}
procedure, ok := children[0].(*Procedure)
if !ok {
return nil, fmt.Errorf("expected `*Procedure` but got `%T`", children[0])
}
nc := *c
nc.Procedure = procedure
return &nc, nil
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*CreateProcedure) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 7
}
// String implements the sql.Node interface.
func (c *CreateProcedure) String() string {
definer := ""
if c.Definer != "" {
definer = fmt.Sprintf(" DEFINER = %s", c.Definer)
}
params := ""
for i, param := range c.Params {
if i > 0 {
params += ", "
}
params += param.String()
}
comment := ""
if c.Comment != "" {
comment = fmt.Sprintf(" COMMENT '%s'", c.Comment)
}
characteristics := ""
for _, characteristic := range c.Characteristics {
characteristics += fmt.Sprintf(" %s", characteristic.String())
}
return fmt.Sprintf("CREATE%s PROCEDURE %s (%s) %s%s%s %s",
definer, c.Name, params, c.SecurityContext.String(), comment, characteristics, c.Procedure.String())
}
// DebugString implements the sql.DebugStringer interface.
func (c *CreateProcedure) DebugString() string {
definer := ""
if c.Definer != "" {
definer = fmt.Sprintf(" DEFINER = %s", c.Definer)
}
params := ""
for i, param := range c.Params {
if i > 0 {
params += ", "
}
params += param.String()
}
comment := ""
if c.Comment != "" {
comment = fmt.Sprintf(" COMMENT '%s'", c.Comment)
}
characteristics := ""
for _, characteristic := range c.Characteristics {
characteristics += fmt.Sprintf(" %s", characteristic.String())
}
return fmt.Sprintf("CREATE%s PROCEDURE %s (%s) %s%s%s %s",
definer, c.Name, params, c.SecurityContext.String(), comment, characteristics, sql.DebugString(c.Procedure))
}