github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/stmtctx/warnings.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 stmtctx
    16  
    17  import (
    18  	"math"
    19  )
    20  
    21  const (
    22  	// WarnLevelError represents level "Error" for 'SHOW WARNINGS' syntax.
    23  	WarnLevelError = "Error"
    24  	// WarnLevelWarning represents level "Warning" for 'SHOW WARNINGS' syntax.
    25  	WarnLevelWarning = "Warning"
    26  	// WarnLevelNote represents level "Note" for 'SHOW WARNINGS' syntax.
    27  	WarnLevelNote = "Note"
    28  )
    29  
    30  // SQLWarn relates a sql warning and it's level.
    31  type SQLWarn struct {
    32  	Level string
    33  	Err   error
    34  }
    35  
    36  // AppendError appends a warning with level 'Error'.
    37  func (sc *Context) AppendError(warn error) {
    38  	sc.mu.Lock()
    39  	defer sc.mu.Unlock()
    40  	if len(sc.mu.warnings) < math.MaxUint16 {
    41  		sc.mu.warnings = append(sc.mu.warnings, SQLWarn{WarnLevelError, warn})
    42  		sc.mu.errorCount++
    43  	}
    44  }
    45  
    46  // AppendWarning appends a warning with level 'Warning'.
    47  func (sc *Context) AppendWarning(warn error) {
    48  	sc.mu.Lock()
    49  	defer sc.mu.Unlock()
    50  	if len(sc.mu.warnings) < math.MaxUint16 {
    51  		sc.mu.warnings = append(sc.mu.warnings, SQLWarn{WarnLevelWarning, warn})
    52  	}
    53  }
    54  
    55  // AppendWarnings appends some warnings.
    56  func (sc *Context) AppendWarnings(warns []SQLWarn) {
    57  	sc.mu.Lock()
    58  	defer sc.mu.Unlock()
    59  	if len(sc.mu.warnings) < math.MaxUint16 {
    60  		sc.mu.warnings = append(sc.mu.warnings, warns...)
    61  	}
    62  }
    63  
    64  // AppendNote appends a warning with level 'Note'.
    65  func (sc *Context) AppendNote(warn error) {
    66  	sc.mu.Lock()
    67  	defer sc.mu.Unlock()
    68  	if len(sc.mu.warnings) < math.MaxUint16 {
    69  		sc.mu.warnings = append(sc.mu.warnings, SQLWarn{WarnLevelNote, warn})
    70  	}
    71  }