github.com/team-ide/go-dialect@v1.9.20/vitess/sqlparser/impossible_query.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlparser
    18  
    19  // FormatImpossibleQuery creates an impossible query in a TrackedBuffer.
    20  // An impossible query is a modified version of a query where all selects have where clauses that are
    21  // impossible for mysql to resolve. This is used in the vtgate and vttablet:
    22  //
    23  // - In the vtgate it's used for joins: if the first query returns no result, then vtgate uses the impossible
    24  // query just to fetch field info from vttablet
    25  // - In the vttablet, it's just an optimization: the field info is fetched once form MySQL, cached and reused
    26  // for subsequent queries
    27  func FormatImpossibleQuery(buf *TrackedBuffer, node SQLNode) {
    28  	switch node := node.(type) {
    29  	case *Select:
    30  		buf.Myprintf("select %v from ", node.SelectExprs)
    31  		var prefix string
    32  		for _, n := range node.From {
    33  			buf.Myprintf("%s%v", prefix, n)
    34  			prefix = ", "
    35  		}
    36  		buf.Myprintf(" where 1 != 1")
    37  		if node.GroupBy != nil {
    38  			node.GroupBy.Format(buf)
    39  		}
    40  	case *Union:
    41  		if requiresParen(node.Left) {
    42  			buf.astPrintf(node, "(%v)", node.Left)
    43  		} else {
    44  			buf.astPrintf(node, "%v", node.Left)
    45  		}
    46  
    47  		buf.WriteString(" ")
    48  		if node.Distinct {
    49  			buf.WriteString(UnionStr)
    50  		} else {
    51  			buf.WriteString(UnionAllStr)
    52  		}
    53  		buf.WriteString(" ")
    54  
    55  		if requiresParen(node.Right) {
    56  			buf.astPrintf(node, "(%v)", node.Right)
    57  		} else {
    58  			buf.astPrintf(node, "%v", node.Right)
    59  		}
    60  	default:
    61  		node.Format(buf)
    62  	}
    63  }