github.com/dolthub/go-mysql-server@v0.18.0/sql/types/ok_result.go (about)

     1  // Copyright 2022 Dolthub, 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  // 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 types
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  )
    22  
    23  // OkResult is a representation of the OK packet MySQL sends for non-select queries such as UPDATE, INSERT, etc. It
    24  // can be returned as the only element in the row for a Node that doesn't select anything.
    25  // See https://dev.mysql.com/doc/internals/en/packet-OK_Packet.html
    26  type OkResult struct {
    27  	RowsAffected uint64       // Number of rows affected by this operation
    28  	InsertID     uint64       // Inserted ID, if any, or -1 if not
    29  	Info         fmt.Stringer // Human-readable status string for extra status info, echoed verbatim to clients.
    30  }
    31  
    32  // OkResultColumnName should be used as the schema column name for Nodes that return an OkResult
    33  const OkResultColumnName = "__ok_result__"
    34  
    35  // OkResultColumnType should be used as the schema column type for Nodes that return an OkResult
    36  var OkResultColumnType = Int64
    37  
    38  // OkResultSchema should be used as the schema of Nodes that return an OkResult
    39  var OkResultSchema = sql.Schema{
    40  	{
    41  		Name: OkResultColumnName,
    42  		Type: OkResultColumnType,
    43  	},
    44  }
    45  
    46  func IsOkResultSchema(schema sql.Schema) bool {
    47  	return len(schema) == 1 && schema[0] == OkResultSchema[0]
    48  }
    49  
    50  // NewOkResult returns a new OkResult with the given number of rows affected.
    51  func NewOkResult(rowsAffected int) OkResult {
    52  	return OkResult{RowsAffected: uint64(rowsAffected)}
    53  }
    54  
    55  // IsOkResult returns whether the given row represents an OkResult.
    56  func IsOkResult(row sql.Row) bool {
    57  	if len(row) == 1 {
    58  		if _, ok := row[0].(OkResult); ok {
    59  			return true
    60  		}
    61  	}
    62  	return false
    63  }
    64  
    65  // GetOkResult extracts the OkResult from the row given
    66  func GetOkResult(row sql.Row) OkResult {
    67  	return row[0].(OkResult)
    68  }