tidblite

package module
v0.0.0-...-f1bd5eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2021 License: Apache-2.0 Imports: 35 Imported by: 0 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tidb-lite test

English README

tidb-lite 是一个库,可以方便地使用该库在 golang 应用或者单元测试中创建 mocktikv 模式的 TiDB。

提供的接口

  • func NewTiDBServer(options *Options) (*TiDBServer, error)

    创建一个 TiDB Server,使用 options 来设置 TiDB 存储数据的路径和服务的端口号。

  • func GetTiDBServer() (*TiDBServer, error)

    获取已经创建的 TiDB Server。

  • func (t *TiDBServer) CreateConn() (*sql.DB, error)

    获取一个 TiDB 的链接。

  • func (t *TiDBServer) Close()

    关闭 TiDB 服务。

  • func (t *TiDBServer) CloseGracefully()

    优雅地关闭 TiDB 服务。

使用示例

可以通过 example 路径下的示例代码了解 tidb-lite 的使用方法。

在 example.go 中定义了一个函数 GetRowCount 计算指定表中符合条件的数据的行数。 在 example_test.go 中使用以下代码创建 TiDB Server 并获取数据库链接:

tidbServer, err := tidblite.NewTiDBServer(tidblite.NewOptions(c.MkDir()))
c.Assert(err, IsNil)
defer tidbServer.Close()

dbConn, err := tidbServer.CreateConn()
c.Assert(err, IsNil)

然后就可以使用链接 dbConn 生成测试数据,对函数 GetRowCount 进行测试。

注意

tidb-lite 只允许同一时刻运行一个 TiDB 实例,需要保证已经创建的 TiDB 实例 close 后再创建新的实例。

Documentation

Index

Constants

View Source
const (
	// DefaultSocket is the default socket to use
	DefaultSocket   = "/tmp/tidb-lite-socket"
	DefaultConnOpts = "charset=utf8mb4"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// DataDir is the directory to save db data
	DataDir string

	Socket string

	Port int

	//Connection options pass to the database, separated with & like charset=utf8mb4&parseTime=True
	ConnOpts string
}

func NewOptions

func NewOptions(dataDir string) *Options

NewOptions creates a new Options with default port

func (*Options) WithConnOpts

func (o *Options) WithConnOpts(connOpts string) *Options

WithConnOpts overrides default connection options

func (*Options) WithPort

func (o *Options) WithPort(port int) *Options

WithPort set the port

func (*Options) WithSocket

func (o *Options) WithSocket(socket string) *Options

WithSocket set the socket

type TiDBServer

type TiDBServer struct {
	// contains filtered or unexported fields
}

TiDBServer ...

func GetTiDBServer

func GetTiDBServer() (*TiDBServer, error)

GetTiDBServer returns the tidb server if it is not nil

func NewTiDBServer

func NewTiDBServer(options *Options) (*TiDBServer, error)

NewTiDBServer returns a new TiDBServer

func (*TiDBServer) Close

func (t *TiDBServer) Close()

Close closes TiDB Server.

func (*TiDBServer) CloseGracefully

func (t *TiDBServer) CloseGracefully()

CloseGracefully closes TiDB server gracefully.

func (*TiDBServer) CreateConn

func (t *TiDBServer) CreateConn() (*sql.DB, error)

CreateConn creates a database connection.

func (*TiDBServer) GetStorage

func (t *TiDBServer) GetStorage() kv.Storage

func (*TiDBServer) SetDBInfoMetaAndReload

func (t *TiDBServer) SetDBInfoMetaAndReload(newDBs []*model.DBInfo) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址