vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/planbuilder/query_gen.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 planbuilder
    18  
    19  import (
    20  	"vitess.io/vitess/go/vt/sqlparser"
    21  )
    22  
    23  // GenerateFullQuery generates the full query from the ast.
    24  func GenerateFullQuery(statement sqlparser.Statement) *sqlparser.ParsedQuery {
    25  	buf := sqlparser.NewTrackedBuffer(nil)
    26  	statement.Format(buf)
    27  	return buf.ParsedQuery()
    28  }
    29  
    30  // GenerateFieldQuery generates a query to just fetch the field info
    31  // by adding impossible where clauses as needed.
    32  func GenerateFieldQuery(statement sqlparser.Statement) *sqlparser.ParsedQuery {
    33  	buf := sqlparser.NewTrackedBuffer(sqlparser.FormatImpossibleQuery).WriteNode(statement)
    34  
    35  	if buf.HasBindVars() {
    36  		return nil
    37  	}
    38  
    39  	return buf.ParsedQuery()
    40  }
    41  
    42  // GenerateLimitQuery generates a select query with a limit clause.
    43  func GenerateLimitQuery(selStmt sqlparser.SelectStatement) *sqlparser.ParsedQuery {
    44  	buf := sqlparser.NewTrackedBuffer(nil)
    45  	switch sel := selStmt.(type) {
    46  	case *sqlparser.Select:
    47  		limit := sel.Limit
    48  		if limit == nil {
    49  			sel.Limit = execLimit
    50  			defer func() {
    51  				sel.Limit = nil
    52  			}()
    53  		}
    54  	case *sqlparser.Union:
    55  		// Code is identical to *Select, but this one is a *Union.
    56  		limit := sel.Limit
    57  		if limit == nil {
    58  			sel.Limit = execLimit
    59  			defer func() {
    60  				sel.Limit = nil
    61  			}()
    62  		}
    63  	}
    64  	buf.Myprintf("%v", selStmt)
    65  	return buf.ParsedQuery()
    66  }