github.com/team-ide/go-dialect@v1.9.20/vitess/sqlparser/truncate_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  var (
    20  	// TruncateUILen truncate queries in debug UIs to the given length. 0 means unlimited.
    21  	TruncateUILen = 512
    22  
    23  	// TruncateErrLen truncate queries in error logs to the given length. 0 means unlimited.
    24  	TruncateErrLen = 0
    25  )
    26  
    27  func init() {
    28  	//flag.IntVar(&TruncateUILen, "sql-max-length-ui", 512, "truncate queries in debug UIs to the given length (default 512)")
    29  	//flag.IntVar(&TruncateErrLen, "sql-max-length-errors", 0, "truncate queries in error logs to the given length (default unlimited)")
    30  }
    31  
    32  func truncateQuery(query string, max int) string {
    33  	sql, comments := SplitMarginComments(query)
    34  
    35  	if max == 0 || len(sql) <= max {
    36  		return comments.Leading + sql + comments.Trailing
    37  	}
    38  
    39  	return comments.Leading + sql[:max-12] + " [TRUNCATED]" + comments.Trailing
    40  }
    41  
    42  // TruncateForUI is used when displaying queries on various Vitess status pages
    43  // to keep the pages small enough to load and render properly
    44  func TruncateForUI(query string) string {
    45  	return truncateQuery(query, TruncateUILen)
    46  }
    47  
    48  // TruncateForLog is used when displaying queries as part of error logs
    49  // to avoid overwhelming logging systems with potentially long queries and
    50  // bind value data.
    51  func TruncateForLog(query string) string {
    52  	return truncateQuery(query, TruncateErrLen)
    53  }