github.com/dolthub/go-mysql-server@v0.18.0/driver/result.go (about)

     1  // Copyright 2020-2021 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 driver
    16  
    17  import (
    18  	"errors"
    19  	"io"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/dolthub/go-mysql-server/sql/types"
    23  )
    24  
    25  func getOKResult(ctx *sql.Context, rows sql.RowIter) (types.OkResult, bool, error) {
    26  	var okr types.OkResult
    27  	var found bool
    28  	for !found {
    29  		row, err := rows.Next(ctx)
    30  		if errors.Is(err, io.EOF) {
    31  			break
    32  		} else if err != nil {
    33  			return okr, found, err
    34  		}
    35  
    36  		if len(row) != 1 {
    37  			continue
    38  		}
    39  
    40  		okr, found = row[0].(types.OkResult)
    41  	}
    42  
    43  	err := rows.Close(ctx)
    44  	return okr, found, err
    45  }
    46  
    47  // Result is the result of a query execution.
    48  type Result struct {
    49  	result types.OkResult
    50  }
    51  
    52  // LastInsertId returns the row auto-generated ID.
    53  //
    54  // For example: after an INSERT into a table with primary key.
    55  func (r *Result) LastInsertId() (int64, error) {
    56  	return int64(r.result.InsertID), nil
    57  }
    58  
    59  // RowsAffected returns the number of rows affected by the query.
    60  func (r *Result) RowsAffected() (int64, error) {
    61  	return int64(r.result.RowsAffected), nil
    62  }
    63  
    64  // ResultNotFound is returned when a row iterator does not return a result.
    65  type ResultNotFound struct{}
    66  
    67  // LastInsertId returns an error
    68  func (r *ResultNotFound) LastInsertId() (int64, error) {
    69  	return 0, errors.New("no result")
    70  }
    71  
    72  // RowsAffected returns an error
    73  func (r *ResultNotFound) RowsAffected() (int64, error) {
    74  	return 0, errors.New("no result")
    75  }