vitess.io/vitess@v0.16.2/go/vt/vtadmin/vtsql/fakevtsql/driver.go (about)

     1  /*
     2  Copyright 2020 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 fakevtsql
    18  
    19  import (
    20  	"context"
    21  	"database/sql/driver"
    22  
    23  	vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
    24  )
    25  
    26  type fakedriver struct {
    27  	tablets   []*vtadminpb.Tablet
    28  	shouldErr bool
    29  }
    30  
    31  var _ driver.Driver = (*fakedriver)(nil)
    32  
    33  func (d *fakedriver) Open(name string) (driver.Conn, error) {
    34  	return &conn{tablets: d.tablets, shouldErr: d.shouldErr}, nil
    35  }
    36  
    37  // Connector implements the driver.Connector interface, providing a sql-like
    38  // thing that can respond to vtadmin vtsql queries with mocked data.
    39  type Connector struct {
    40  	Tablets []*vtadminpb.Tablet
    41  	// (TODO:@amason) - allow distinction between Query errors and errors on
    42  	// Rows operations (e.g. Next, Err, Scan).
    43  	ShouldErr bool
    44  }
    45  
    46  var _ driver.Connector = (*Connector)(nil)
    47  
    48  // Connect is part of the driver.Connector interface.
    49  func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
    50  	return &conn{tablets: c.Tablets, shouldErr: c.ShouldErr}, nil
    51  }
    52  
    53  // Driver is part of the driver.Connector interface.
    54  func (c *Connector) Driver() driver.Driver {
    55  	return &fakedriver{tablets: c.Tablets, shouldErr: c.ShouldErr}
    56  }