vitess.io/vitess@v0.16.2/go/vt/vtorc/test/db.go (about)

     1  /*
     2  Copyright 2021 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 test
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"vitess.io/vitess/go/vt/external/golib/sqlutils"
    23  	"vitess.io/vitess/go/vt/log"
    24  	"vitess.io/vitess/go/vt/vtorc/db"
    25  )
    26  
    27  var _ db.DB = (*DB)(nil)
    28  
    29  type DB struct {
    30  	rowMaps [][]sqlutils.RowMap
    31  }
    32  
    33  func NewTestDB(rowMaps [][]sqlutils.RowMap) *DB {
    34  	return &DB{
    35  		rowMaps: rowMaps,
    36  	}
    37  }
    38  
    39  func (t *DB) QueryVTOrc(query string, argsArray []any, onRow func(sqlutils.RowMap) error) error {
    40  	log.Info("test")
    41  	rowMaps, err := t.getRowMapsForQuery()
    42  	if err != nil {
    43  		return err
    44  	}
    45  	for _, rowMap := range rowMaps {
    46  		err = onRow(rowMap)
    47  		if err != nil {
    48  			return err
    49  		}
    50  	}
    51  	return nil
    52  }
    53  
    54  func (t *DB) getRowMapsForQuery() ([]sqlutils.RowMap, error) {
    55  	if len(t.rowMaps) == 0 {
    56  		return nil, fmt.Errorf("no rows left to return. We received more queries than expected")
    57  	}
    58  	result := t.rowMaps[0]
    59  	t.rowMaps = t.rowMaps[1:]
    60  	return result, nil
    61  }