|
| 1 | +// Copyright 2019 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 ast |
| 15 | + |
| 16 | +import ( |
| 17 | + . "github.com/pingcap/parser/format" |
| 18 | +) |
| 19 | + |
| 20 | +var _ StmtNode = &IndexAdviseStmt{} |
| 21 | + |
| 22 | +// IndexAdviseStmt is used to advise indexes |
| 23 | +type IndexAdviseStmt struct { |
| 24 | + stmtNode |
| 25 | + |
| 26 | + IsLocal bool |
| 27 | + Path string |
| 28 | + MaxMinutes uint64 |
| 29 | + MaxIndexNum *MaxIndexNumClause |
| 30 | + LinesInfo *LinesClause |
| 31 | +} |
| 32 | + |
| 33 | +// Restore implements Node Accept interface. |
| 34 | +func (n *IndexAdviseStmt) Restore(ctx *RestoreCtx) error { |
| 35 | + ctx.WriteKeyWord("INDEX ADVISE ") |
| 36 | + if n.IsLocal { |
| 37 | + ctx.WriteKeyWord("LOCAL ") |
| 38 | + } |
| 39 | + ctx.WriteKeyWord("INFILE ") |
| 40 | + ctx.WriteString(n.Path) |
| 41 | + if n.MaxMinutes != UnspecifiedSize { |
| 42 | + ctx.WriteKeyWord(" MAX_MINUTES ") |
| 43 | + ctx.WritePlainf("%d", n.MaxMinutes) |
| 44 | + } |
| 45 | + if n.MaxIndexNum != nil { |
| 46 | + n.MaxIndexNum.Restore(ctx) |
| 47 | + } |
| 48 | + n.LinesInfo.Restore(ctx) |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +// Accept implements Node Accept interface. |
| 53 | +func (n *IndexAdviseStmt) Accept(v Visitor) (Node, bool) { |
| 54 | + newNode, skipChildren := v.Enter(n) |
| 55 | + if skipChildren { |
| 56 | + return v.Leave(newNode) |
| 57 | + } |
| 58 | + n = newNode.(*IndexAdviseStmt) |
| 59 | + return v.Leave(n) |
| 60 | +} |
| 61 | + |
| 62 | +// MaxIndexNumClause represents 'maximum number of indexes' clause in index advise statement. |
| 63 | +type MaxIndexNumClause struct { |
| 64 | + PerTable uint64 |
| 65 | + PerDB uint64 |
| 66 | +} |
| 67 | + |
| 68 | +// Restore for max index num clause |
| 69 | +func (n *MaxIndexNumClause) Restore(ctx *RestoreCtx) error { |
| 70 | + ctx.WriteKeyWord(" MAX_IDXNUM") |
| 71 | + if n.PerTable != UnspecifiedSize { |
| 72 | + ctx.WriteKeyWord(" PER_TABLE ") |
| 73 | + ctx.WritePlainf("%d", n.PerTable) |
| 74 | + } |
| 75 | + if n.PerDB != UnspecifiedSize { |
| 76 | + ctx.WriteKeyWord(" PER_DB ") |
| 77 | + ctx.WritePlainf("%d", n.PerDB) |
| 78 | + } |
| 79 | + return nil |
| 80 | +} |
0 commit comments