1 Star 0 Fork 0

wenj91/sqls

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
database_mock.go 11.00 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
package database
import (
"context"
"database/sql"
"gitee.com/wenj91/sqls/dialect"
)
type MockDBRepository struct {
MockDatabase func(context.Context) (string, error)
MockDatabases func(context.Context) ([]string, error)
MockDatabaseTables func(context.Context) (map[string][]string, error)
MockTables func(context.Context) ([]string, error)
MockDescribeTable func(context.Context, string) ([]*ColumnDesc, error)
MockDescribeDatabaseTable func(context.Context) ([]*ColumnDesc, error)
MockDescribeDatabaseTableBySchema func(context.Context, string) ([]*ColumnDesc, error)
MockExec func(context.Context, string) (sql.Result, error)
MockQuery func(context.Context, string) (*sql.Rows, error)
MockDescribeForeignKeysBySchema func(context.Context, string) ([]*ForeignKey, error)
}
func NewMockDBRepository(_ *sql.DB) DBRepository {
return &MockDBRepository{
MockDatabase: func(ctx context.Context) (string, error) { return "world", nil },
MockDatabases: func(ctx context.Context) ([]string, error) { return dummyDatabases, nil },
MockDatabaseTables: func(ctx context.Context) (map[string][]string, error) { return dummyDatabaseTables, nil },
MockTables: func(ctx context.Context) ([]string, error) { return dummyTables, nil },
MockDescribeTable: func(ctx context.Context, tableName string) ([]*ColumnDesc, error) {
switch tableName {
case "city":
return dummyCityColumns, nil
case "country":
return dummyCountryColumns, nil
case "countrylanguage":
return dummyCountryLanguageColumns, nil
}
return nil, nil
},
MockDescribeDatabaseTable: func(ctx context.Context) ([]*ColumnDesc, error) {
var res []*ColumnDesc
res = append(res, dummyCityColumns...)
res = append(res, dummyCountryColumns...)
res = append(res, dummyCountryLanguageColumns...)
return res, nil
},
MockDescribeDatabaseTableBySchema: func(ctx context.Context, schemaName string) ([]*ColumnDesc, error) {
var res []*ColumnDesc
res = append(res, dummyCityColumns...)
res = append(res, dummyCountryColumns...)
res = append(res, dummyCountryLanguageColumns...)
return res, nil
},
MockExec: func(ctx context.Context, query string) (sql.Result, error) {
return &MockResult{
MockLastInsertID: func() (int64, error) { return 11, nil },
MockRowsAffected: func() (int64, error) { return 22, nil },
}, nil
},
MockQuery: func(ctx context.Context, query string) (*sql.Rows, error) {
return &sql.Rows{}, nil
},
MockDescribeForeignKeysBySchema: func(ctx context.Context, schemaName string) ([]*ForeignKey, error) {
return foreignKeys, nil
},
}
}
func (m *MockDBRepository) Driver() dialect.DatabaseDriver {
return "mock"
}
func (m *MockDBRepository) CurrentDatabase(ctx context.Context) (string, error) {
return m.MockDatabase(ctx)
}
func (m *MockDBRepository) Databases(ctx context.Context) ([]string, error) {
return m.MockDatabases(ctx)
}
func (m *MockDBRepository) CurrentSchema(ctx context.Context) (string, error) {
return m.MockDatabase(ctx)
}
func (m *MockDBRepository) Schemas(ctx context.Context) ([]string, error) {
return m.MockDatabases(ctx)
}
func (m *MockDBRepository) SchemaTables(ctx context.Context) (map[string][]string, error) {
return m.MockDatabaseTables(ctx)
}
func (m *MockDBRepository) Tables(ctx context.Context) ([]string, error) {
return m.MockTables(ctx)
}
func (m *MockDBRepository) DescribeDatabaseTable(ctx context.Context) ([]*ColumnDesc, error) {
return m.MockDescribeDatabaseTable(ctx)
}
func (m *MockDBRepository) DescribeDatabaseTableBySchema(ctx context.Context, schemaName string) ([]*ColumnDesc, error) {
return m.MockDescribeDatabaseTableBySchema(ctx, schemaName)
}
func (m *MockDBRepository) Exec(ctx context.Context, query string) (sql.Result, error) {
return m.MockExec(ctx, query)
}
func (m *MockDBRepository) Query(ctx context.Context, query string) (*sql.Rows, error) {
return m.MockQuery(ctx, query)
}
func (m *MockDBRepository) DescribeForeignKeysBySchema(ctx context.Context, schemaName string) ([]*ForeignKey, error) {
return m.MockDescribeForeignKeysBySchema(ctx, schemaName)
}
var dummyDatabases = []string{
"information_schema",
"mysql",
"performance_schema",
"sys",
"world",
}
var dummyDatabaseTables = map[string][]string{
"world": {
"city",
"country",
"countrylanguage",
},
}
var dummyTables = []string{
"city",
"country",
"countrylanguage",
}
var dummyCityColumns = []*ColumnDesc{
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "city",
Name: "ID",
},
Type: "int(11)",
Null: "NO",
Key: "PRI",
Default: sql.NullString{
String: "<null>",
Valid: false,
},
Extra: "auto_increment",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "city",
Name: "Name",
},
Type: "char(35)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "city",
Name: "CountryCode",
},
Type: "char(3)",
Null: "NO",
Key: "MUL",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "city",
Name: "District",
},
Type: "char(20)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "city",
Name: "Population",
},
Type: "int(11)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
}
var dummyCountryColumns = []*ColumnDesc{
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "Code",
},
Type: "char(3)",
Null: "NO",
Key: "PRI",
Default: sql.NullString{
String: "<null>",
Valid: false,
},
Extra: "auto_increment",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "Name",
},
Type: "char(52)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "CountryCode",
},
Type: "char(3)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "Continent",
},
Type: "enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America')",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "Asia",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "Region",
},
Type: "char(26)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "SurfaceArea",
},
Type: "decimal(10,2)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "0.00",
Valid: false,
},
Extra: "auto_increment",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "IndepYear",
},
Type: "smallint(6)",
Null: "YES",
Key: "",
Default: sql.NullString{
String: "0",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "LifeExpectancy",
},
Type: "decimal(3,1)",
Null: "YES",
Key: "",
Default: sql.NullString{
String: "<null>",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "GNP",
},
Type: "decimal(10,2)",
Null: "YES",
Key: "",
Default: sql.NullString{
String: "<null>",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "GNPOld",
},
Type: "decimal(10,2)",
Null: "YES",
Key: "",
Default: sql.NullString{
String: "<null>",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "LocalName",
},
Type: "char(45)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "GovernmentForm",
},
Type: "char(45)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "HeadOfState",
},
Type: "char(60)",
Null: "YES",
Key: "",
Default: sql.NullString{
String: "<null>",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "Capital",
},
Type: "int(11)",
Null: "YES",
Key: "",
Default: sql.NullString{
String: "<null>",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "country",
Name: "Code2",
},
Type: "char(2)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
}
var dummyCountryLanguageColumns = []*ColumnDesc{
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "countrylanguage",
Name: "CountryCode",
},
Type: "char(3)",
Null: "NO",
Key: "PRI",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "countrylanguage",
Name: "Language",
},
Type: "char(30)",
Null: "NO",
Key: "PRI",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "countrylanguage",
Name: "IsOfficial",
},
Type: "enum('T','F')",
Null: "NO",
Key: "F",
Default: sql.NullString{
String: "",
Valid: false,
},
Extra: "",
},
{
ColumnBase: ColumnBase{
Schema: "world",
Table: "countrylanguage",
Name: "Percentage",
},
Type: "decimal(4,1)",
Null: "NO",
Key: "",
Default: sql.NullString{
String: "0.0",
Valid: false,
},
Extra: "",
},
}
var foreignKeys = []*ForeignKey{
{
[2]*ColumnBase{
{
Schema: "world",
Table: "city",
Name: "CountryCode",
},
{
Schema: "world",
Table: "country",
Name: "Code",
},
},
},
{
[2]*ColumnBase{
{
Schema: "world",
Table: "countrylanguage",
Name: "CountryCode",
},
{
Schema: "world",
Table: "country",
Name: "Code",
},
},
},
}
type MockResult struct {
MockLastInsertID func() (int64, error)
MockRowsAffected func() (int64, error)
}
func (m *MockResult) LastInsertId() (int64, error) {
return m.MockLastInsertID()
}
func (m *MockResult) RowsAffected() (int64, error) {
return m.MockRowsAffected()
}
func init() {
RegisterOpen("mock", func(connCfg *DBConfig) (*DBConnection, error) { return &DBConnection{}, nil })
RegisterFactory("mock", NewMockDBRepository)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wenj91/sqls.git
git@gitee.com:wenj91/sqls.git
wenj91
sqls
sqls
v1.0.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385