Skip to content

Commit

Permalink
增加sqlite测试用例,factory增加close方法
Browse files Browse the repository at this point in the history
  • Loading branch information
xfali committed Jan 16, 2020
1 parent b159980 commit ab98e71
Show file tree
Hide file tree
Showing 15 changed files with 636 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

*.zip
/.idea/
*.db

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ func InitDB() *gobatis.SessionManager {
return gobatis.NewSessionManager(&fac)
}
```
*注意:*

gobatis.NewFactory当连接数据库失败时会返回nil,如果需要知道具体的失败原因请使用:
```cassandraql
fac, err := gobatis.CreateFactory(
gobatis.SetMaxConn(100),
gobatis.SetMaxIdleConn(50),
gobatis.SetDataSource(&datasource.MysqlDataSource{
Host: "localhost",
Port: 3306,
DBName: "test",
Username: "root",
Password: "123",
Charset: "utf8",
}))
if err != nil {
t.Log(err)
}
```

### 2、定义Model

Expand Down
22 changes: 22 additions & 0 deletions datasource/common_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (C) 2019, Xiongfa Li.
* All right reserved.
* @author xiongfa.li
* @version V1.0
* Description:
*/

package datasource

type CommonDataSource struct {
Name string
Info string
}

func (ds *CommonDataSource) DriverName() string {
return ds.Name
}

func (ds *CommonDataSource) DriverInfo() string {
return ds.Info
}
2 changes: 1 addition & 1 deletion datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ package datasource

type DataSource interface {
DriverName() string
Info() string
DriverInfo() string
}
4 changes: 3 additions & 1 deletion datasource/mysql_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package datasource

import "fmt"

//import _ "github.com/go-sql-driver/mysql"

type MysqlDataSource struct {
Host string
Port int
Expand All @@ -23,6 +25,6 @@ func (ds *MysqlDataSource) DriverName() string {
return "mysql"
}

func (ds *MysqlDataSource) Info() string {
func (ds *MysqlDataSource) DriverInfo() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s", ds.Username, ds.Password, ds.Host, ds.Port, ds.DBName, ds.Charset)
}
2 changes: 1 addition & 1 deletion datasource/postgre_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func (ds *PostgreDataSource) DriverName() string {
return "postgres"
}

func (ds *PostgreDataSource) Info() string {
func (ds *PostgreDataSource) DriverInfo() string {
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", ds.Host, ds.Port, ds.Username, ds.Password, ds.DBName, ds.SslMode)
}
21 changes: 21 additions & 0 deletions datasource/sqlite_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2019, Xiongfa Li.
// All right reserved.
// @author xiongfa.li
// @version V1.0
// Description:

package datasource

//import _ "github.com/mattn/go-sqlite3"

type SqliteDataSource struct {
Path string
}

func (ds *SqliteDataSource) DriverName() string {
return "sqlite3"
}

func (ds *SqliteDataSource) DriverInfo() string {
return ds.Path
}
12 changes: 9 additions & 3 deletions faccreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
type FacOpt func(f *factory.DefaultFactory)

func NewFactory(opts ...FacOpt) factory.Factory {
f, _ := CreateFactory(opts...)
return f
}

func CreateFactory(opts ...FacOpt) (factory.Factory, error) {
f := &factory.DefaultFactory{
Log: logging.DefaultLogf,
}
Expand All @@ -38,11 +43,12 @@ func NewFactory(opts ...FacOpt) factory.Factory {
}
}

if f.InitDB() != nil {
return nil
err := f.InitDB()
if err != nil {
return nil, err
}

return f
return f, nil
}

func SetMaxConn(v int) FacOpt {
Expand Down
9 changes: 8 additions & 1 deletion factory/default_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (f *DefaultFactory) InitDB() error {
return errors.FACTORY_INITED
}

db, err := sql.Open(f.DataSource.DriverName(), f.DataSource.Info())
db, err := sql.Open(f.DataSource.DriverName(), f.DataSource.DriverInfo())
if err != nil {
return err
}
Expand All @@ -66,6 +66,13 @@ func (f *DefaultFactory) InitDB() error {
return nil
}

func (f *DefaultFactory) Close() error {
if f.db != nil {
return f.db.Close()
}
return nil
}

func (f *DefaultFactory) CreateTransaction() transaction.Transaction {
return transaction.NewDefaultTransaction(f.DataSource, f.db)
}
Expand Down
20 changes: 11 additions & 9 deletions factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
package factory

import (
"github.com/xfali/gobatis/executor"
"github.com/xfali/gobatis/logging"
"github.com/xfali/gobatis/session"
"github.com/xfali/gobatis/transaction"
"github.com/xfali/gobatis/executor"
"github.com/xfali/gobatis/logging"
"github.com/xfali/gobatis/session"
"github.com/xfali/gobatis/transaction"
)

type Factory interface {
InitDB() error
CreateTransaction() transaction.Transaction
CreateExecutor(transaction.Transaction) executor.Executor
CreateSession() session.SqlSession
LogFunc() logging.LogFunc
InitDB() error
Close() error

CreateTransaction() transaction.Transaction
CreateExecutor(transaction.Transaction) executor.Executor
CreateSession() session.SqlSession
LogFunc() logging.LogFunc
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/xfali/gobatis

go 1.11

require github.com/go-sql-driver/mysql v1.4.1
require (
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/mattn/go-sqlite3 v1.10.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
Loading

0 comments on commit ab98e71

Please sign in to comment.