sqlite

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

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

Go to latest
Published: Oct 12, 2022 License: MIT Imports: 13 Imported by: 0

README

GORM Sqlite Mock Driver

DEMO

https://github.com/Lichmaker/gorm-sqlite-sqlmock-demo

WHY

自从升级到 gorm v2 之后, sqlite 使用 gorm 时如下:


    // ......

    import (
        "gorm.io/driver/sqlite"
        "gorm.io/gorm"
    )

    //......

    // 初始化数据库链接
	dbSqlite, err := gorm.Open(sqlite.Open("/tmp/mydbfile.db"), &gorm.Config{
		SkipDefaultTransaction: true,
	})
	if err != nil {
		panic("failed to connect database")
	}

当使用 sqlmock 时,需要把mock出来的 *sql.DB 注入到 gorm 中。但即使使用 gorm.Config 中的 ConnPool 也是无法起效的。 一个失败的例子:

    // A wrong example

	db, mock, err = sqlmock.New()
	if err != nil {
		panic(err)
	}

	dbSqlite, err := gorm.Open(sqlite.Open("/tmp/mydbfile.db"), &gorm.Config{
        ConnPool: db,
		SkipDefaultTransaction: true,
	})
	if err != nil {
		panic("failed to connect database")
	}

因此,通过改写 gorm-sqlite-driver ,强行把 *sql.DB 注入进去。

USE


    // ......
    
    import (
        "github.com/DATA-DOG/go-sqlmock"
        sqlitemock "github.com/lichmaker/sqlite-mock"
        "gorm.io/gorm"
    )

    // ......

	db, mock, err = sqlmock.New()
	if err != nil {
		panic(err)
	}

	myMockDriver := sqlitemock.Open(db)
	dbSqlite, err := gorm.Open(myMockDriver, &gorm.Config{
		SkipDefaultTransaction: true,
	})
	if err != nil {
		panic("failed to connect database")
	}

Documentation

Index

Constants

View Source
const DriverName = "sqlite3"

DriverName is the default driver name for SQLite.

Variables

View Source
var (
	ErrConstraintsNotImplemented = errors.New("constraints not implemented on sqlite, consider using DisableForeignKeyConstraintWhenMigrating, more details https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft#all-new-migrator")
)

Functions

func Open

func Open(mockDb *sql.DB) gorm.Dialector

Types

type Dialector

type Dialector struct {
	DriverName string
	DSN        string
	Conn       gorm.ConnPool
	MockDb     *sql.DB
}

func (Dialector) BindVarTo

func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})

func (Dialector) ClauseBuilders

func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder

func (Dialector) DataTypeOf

func (dialector Dialector) DataTypeOf(field *schema.Field) string

func (Dialector) DefaultValueOf

func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression

func (Dialector) Explain

func (dialector Dialector) Explain(sql string, vars ...interface{}) string

func (Dialector) Initialize

func (dialector Dialector) Initialize(db *gorm.DB) (err error)

func (Dialector) Migrator

func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator

func (Dialector) Name

func (dialector Dialector) Name() string

func (Dialector) QuoteTo

func (dialector Dialector) QuoteTo(writer clause.Writer, str string)

func (Dialector) RollbackTo

func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error

func (Dialector) SavePoint

func (dialectopr Dialector) SavePoint(tx *gorm.DB, name string) error

type Migrator

type Migrator struct {
	migrator.Migrator
}

func (Migrator) AlterColumn

func (m Migrator) AlterColumn(value interface{}, name string) error

func (Migrator) BuildIndexOptions

func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{})

func (Migrator) ColumnTypes

func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error)

ColumnTypes return columnTypes []gorm.ColumnType and execErr error

func (Migrator) CreateConstraint

func (m Migrator) CreateConstraint(value interface{}, name string) error

func (Migrator) CreateIndex

func (m Migrator) CreateIndex(value interface{}, name string) error

func (Migrator) CurrentDatabase

func (m Migrator) CurrentDatabase() (name string)

func (Migrator) DropColumn

func (m Migrator) DropColumn(value interface{}, name string) error

func (Migrator) DropConstraint

func (m Migrator) DropConstraint(value interface{}, name string) error

func (Migrator) DropIndex

func (m Migrator) DropIndex(value interface{}, name string) error

func (Migrator) DropTable

func (m Migrator) DropTable(values ...interface{}) error

func (Migrator) GetTables

func (m Migrator) GetTables() (tableList []string, err error)

func (Migrator) HasColumn

func (m Migrator) HasColumn(value interface{}, name string) bool

func (Migrator) HasConstraint

func (m Migrator) HasConstraint(value interface{}, name string) bool

func (Migrator) HasIndex

func (m Migrator) HasIndex(value interface{}, name string) bool

func (Migrator) HasTable

func (m Migrator) HasTable(value interface{}) bool

func (Migrator) RenameIndex

func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error

func (*Migrator) RunWithoutForeignKey

func (m *Migrator) RunWithoutForeignKey(fc func() error) error

Jump to

Keyboard shortcuts

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