vitess.io/vitess@v0.16.2/go/vt/binlog/binlogplayer/fake_dbclient.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 binlogplayer
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"vitess.io/vitess/go/sqltypes"
    24  )
    25  
    26  type fakeDBClient struct {
    27  }
    28  
    29  // NewFakeDBClient returns a fake DBClient. Its functions return
    30  // preset responses to requests.
    31  func NewFakeDBClient() DBClient {
    32  	return &fakeDBClient{}
    33  }
    34  
    35  func (dc *fakeDBClient) DBName() string {
    36  	return "db"
    37  }
    38  
    39  func (dc *fakeDBClient) Connect() error {
    40  	return nil
    41  }
    42  
    43  func (dc *fakeDBClient) Begin() error {
    44  	return nil
    45  }
    46  
    47  func (dc *fakeDBClient) Commit() error {
    48  	return nil
    49  }
    50  
    51  func (dc *fakeDBClient) Rollback() error {
    52  	return nil
    53  }
    54  
    55  func (dc *fakeDBClient) Close() {
    56  }
    57  
    58  func (dc *fakeDBClient) ExecuteFetch(query string, maxrows int) (qr *sqltypes.Result, err error) {
    59  	query = strings.ToLower(query)
    60  	switch {
    61  	case strings.HasPrefix(query, "insert"):
    62  		return &sqltypes.Result{InsertID: 1}, nil
    63  	case strings.HasPrefix(query, "update"):
    64  		return &sqltypes.Result{RowsAffected: 1}, nil
    65  	case strings.HasPrefix(query, "delete"):
    66  		return &sqltypes.Result{RowsAffected: 1}, nil
    67  	case strings.HasPrefix(query, "select"):
    68  		if strings.Contains(query, "where") {
    69  			return sqltypes.MakeTestResult(
    70  				sqltypes.MakeTestFields(
    71  					"id|state|source|message",
    72  					"int64|varchar|varchar|varchar",
    73  				),
    74  				`1|Running|keyspace:"ks" shard:"0" key_range:<end:"\200" > |`,
    75  			), nil
    76  		}
    77  		return &sqltypes.Result{}, nil
    78  	case strings.HasPrefix(query, "use"):
    79  		return &sqltypes.Result{}, nil
    80  	}
    81  	return nil, fmt.Errorf("unexpected: %v", query)
    82  }