sqlite 及test

This commit is contained in:
suguo.yao 2021-09-03 16:44:20 +08:00
parent b2090ff793
commit 40a95aafbc
7 changed files with 121 additions and 4 deletions

3
.gitignore vendored
View File

@ -15,3 +15,6 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
go.sum go.sum
tmp/
.vscode/
logs/

View File

@ -11,7 +11,6 @@
#### 组件实现 #### 组件实现
* MySQL * MySQL
> "gorm.io/driver/mysql" > 支持连接池dbresolver
> "gorm.io/gorm" * Sqlite
> "gorm.io/gorm/logger" > 支持多级目录创建
> "gorm.io/plugin/dbresolver"

2
go.mod
View File

@ -4,6 +4,7 @@ go 1.17
require ( require (
gorm.io/driver/mysql v1.1.2 gorm.io/driver/mysql v1.1.2
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.21.14 gorm.io/gorm v1.21.14
gorm.io/plugin/dbresolver v1.1.0 gorm.io/plugin/dbresolver v1.1.0
) )
@ -12,4 +13,5 @@ require (
github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect github.com/jinzhu/now v1.1.2 // indirect
github.com/mattn/go-sqlite3 v1.14.5 // indirect
) )

View File

@ -1,6 +1,7 @@
package mysql package mysql
import ( import (
"errors"
"time" "time"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
@ -34,6 +35,10 @@ func New() (*gorm.DB, error) {
return _db, nil return _db, nil
} }
if _conf == nil {
return nil, errors.New("组件未初始化请执行Init")
}
var err error var err error
_db, err = gorm.Open(mysql.Open(_conf.ConnString), &gorm.Config{ _db, err = gorm.Open(mysql.Open(_conf.ConnString), &gorm.Config{
SkipDefaultTransaction: true, SkipDefaultTransaction: true,

5
sqlite/config.go Normal file
View File

@ -0,0 +1,5 @@
package sqlite
type Config struct {
DBFile string //DB文件名
}

63
sqlite/sqlite.go Normal file
View File

@ -0,0 +1,63 @@
package sqlite
import (
"errors"
"os"
"path/filepath"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var (
_conf *Config
_db *gorm.DB
)
//Init mysql初始化
func Init(conf *Config) error {
if conf != nil {
_conf = conf
}
return nil
}
//New 创建实例
func New() (*gorm.DB, error) {
if _db != nil {
return _db, nil
}
if _conf == nil {
return nil, errors.New("组件未初始化请执行Init")
}
dir, _ := filepath.Split(_conf.DBFile)
if dir != "" {
_, err := os.Stat(dir)
if os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
}
}
var err error
_db, err = gorm.Open(sqlite.Open(_conf.DBFile), &gorm.Config{
SkipDefaultTransaction: true,
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return nil, err
}
return _db, nil
}
func Migrate(dest ...interface{}) error {
db, err := New()
if err != nil {
return err
}
return db.AutoMigrate(dest...)
}

40
test/sqlite_test.go Normal file
View File

@ -0,0 +1,40 @@
package test
import (
"testing"
"myschools.me/suguo/norm/sqlite"
)
type ABC struct {
A string
B string
C string
}
type CBA struct {
D string
E string
F string
}
func TestSqlite(t *testing.T) {
conf := &sqlite.Config{
DBFile: "tmp1/abc/aaaaa/abc.db",
}
if err := sqlite.Init(conf); err != nil {
t.Fatal(err)
}
db, err := sqlite.New()
if err != nil {
t.Fatal(err)
}
if db == nil {
t.Error("DB nil")
}
if err := sqlite.Migrate(&ABC{}, &CBA{}); err != nil {
t.Fatal(err)
}
}