github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/parser/ast/ddl.go (about) 1 // Copyright 2022 zGraph Authors. All rights reserved. 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ast 16 17 import ( 18 "github.com/vescale/zgraph/parser/format" 19 "github.com/vescale/zgraph/parser/model" 20 ) 21 22 var ( 23 _ DDLNode = &CreateGraphStmt{} 24 _ DDLNode = &DropGraphStmt{} 25 _ DDLNode = &CreateLabelStmt{} 26 _ DDLNode = &DropLabelStmt{} 27 _ DDLNode = &CreateIndexStmt{} 28 _ DDLNode = &DropIndexStmt{} 29 ) 30 31 type CreateGraphStmt struct { 32 ddlNode 33 34 IfNotExists bool 35 Graph model.CIStr 36 } 37 38 func (n *CreateGraphStmt) Restore(ctx *format.RestoreCtx) error { 39 ctx.WriteKeyWord("CREATE GRAPH ") 40 ctx.WriteName(n.Graph.String()) 41 return nil 42 } 43 44 func (n *CreateGraphStmt) Accept(v Visitor) (Node, bool) { 45 newNode, skipChildren := v.Enter(n) 46 if skipChildren { 47 return v.Leave(newNode) 48 } 49 return v.Leave(newNode) 50 } 51 52 type DropGraphStmt struct { 53 ddlNode 54 55 IfExists bool 56 Graph model.CIStr 57 } 58 59 func (n *DropGraphStmt) Restore(ctx *format.RestoreCtx) error { 60 ctx.WriteKeyWord("DROP GRAPH ") 61 ctx.WriteName(n.Graph.String()) 62 return nil 63 } 64 65 func (n *DropGraphStmt) Accept(v Visitor) (Node, bool) { 66 newNode, skipChildren := v.Enter(n) 67 if skipChildren { 68 return v.Leave(newNode) 69 } 70 return v.Leave(newNode) 71 } 72 73 type CreateLabelStmt struct { 74 ddlNode 75 76 IfNotExists bool 77 Label model.CIStr 78 } 79 80 // Restore implements Node interface. 81 func (n *CreateLabelStmt) Restore(ctx *format.RestoreCtx) error { 82 ctx.WriteKeyWord("CREATE ") 83 ctx.WriteKeyWord("LABEL ") 84 if n.IfNotExists { 85 ctx.WriteKeyWord("IF NOT EXISTS ") 86 } 87 ctx.WriteName(n.Label.String()) 88 return nil 89 } 90 91 // Accept implements Node Accept interface. 92 func (n *CreateLabelStmt) Accept(v Visitor) (Node, bool) { 93 newNode, skipChildren := v.Enter(n) 94 if skipChildren { 95 return v.Leave(newNode) 96 } 97 98 return v.Leave(newNode) 99 } 100 101 type DropLabelStmt struct { 102 ddlNode 103 104 IfExists bool 105 Label model.CIStr 106 } 107 108 func (n *DropLabelStmt) Restore(ctx *format.RestoreCtx) error { 109 ctx.WriteKeyWord("DROP LABEL ") 110 ctx.WriteName(n.Label.String()) 111 return nil 112 } 113 114 func (n *DropLabelStmt) Accept(v Visitor) (Node, bool) { 115 newNode, skipChildren := v.Enter(n) 116 if skipChildren { 117 return v.Leave(newNode) 118 } 119 return v.Leave(newNode) 120 } 121 122 // IndexKeyType is the type for index key. 123 type IndexKeyType int 124 125 // Index key types. 126 const ( 127 IndexKeyTypeNone IndexKeyType = iota 128 IndexKeyTypeUnique 129 ) 130 131 // CreateIndexStmt is a statement to create an index. 132 // See https://dev.mysql.com/doc/refman/5.7/en/create-index.html 133 type CreateIndexStmt struct { 134 ddlNode 135 136 KeyType IndexKeyType 137 IfNotExists bool 138 139 IndexName model.CIStr 140 Properties []model.CIStr 141 } 142 143 // Restore implements Node interface. 144 func (n *CreateIndexStmt) Restore(ctx *format.RestoreCtx) error { 145 ctx.WriteKeyWord("CREATE ") 146 switch n.KeyType { 147 case IndexKeyTypeUnique: 148 ctx.WriteKeyWord("UNIQUE ") 149 } 150 ctx.WriteKeyWord("INDEX ") 151 if n.IfNotExists { 152 ctx.WriteKeyWord("IF NOT EXISTS ") 153 } 154 ctx.WriteName(n.IndexName.String()) 155 156 ctx.WritePlain(" (") 157 for i, propName := range n.Properties { 158 if i != 0 { 159 ctx.WritePlain(", ") 160 } 161 ctx.WriteName(propName.String()) 162 } 163 ctx.WritePlain(")") 164 165 return nil 166 } 167 168 // Accept implements Node Accept interface. 169 func (n *CreateIndexStmt) Accept(v Visitor) (Node, bool) { 170 newNode, skipChildren := v.Enter(n) 171 if skipChildren { 172 return v.Leave(newNode) 173 } 174 return v.Leave(newNode) 175 } 176 177 // DropIndexStmt is a statement to drop the index. 178 // See https://dev.mysql.com/doc/refman/5.7/en/drop-index.html 179 type DropIndexStmt struct { 180 ddlNode 181 182 IfExists bool 183 IndexName model.CIStr 184 } 185 186 // Restore implements Node interface. 187 func (n *DropIndexStmt) Restore(ctx *format.RestoreCtx) error { 188 ctx.WriteKeyWord("DROP INDEX ") 189 if n.IfExists { 190 ctx.WriteWithSpecialComments("", func() { 191 ctx.WriteKeyWord("IF EXISTS ") 192 }) 193 } 194 ctx.WriteName(n.IndexName.String()) 195 196 return nil 197 } 198 199 // Accept implements Node Accept interface. 200 func (n *DropIndexStmt) Accept(v Visitor) (Node, bool) { 201 newNode, skipChildren := v.Enter(n) 202 if skipChildren { 203 return v.Leave(newNode) 204 } 205 return v.Leave(newNode) 206 }