Skip to content

Commit ab98e71

Browse files
committed
增加sqlite测试用例,factory增加close方法
1 parent b159980 commit ab98e71

15 files changed

+636
-146
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313

1414
*.zip
1515
/.idea/
16+
*.db
1617

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ func InitDB() *gobatis.SessionManager {
4646
return gobatis.NewSessionManager(&fac)
4747
}
4848
```
49+
*注意:*
50+
51+
gobatis.NewFactory当连接数据库失败时会返回nil,如果需要知道具体的失败原因请使用:
52+
```cassandraql
53+
fac, err := gobatis.CreateFactory(
54+
gobatis.SetMaxConn(100),
55+
gobatis.SetMaxIdleConn(50),
56+
gobatis.SetDataSource(&datasource.MysqlDataSource{
57+
Host: "localhost",
58+
Port: 3306,
59+
DBName: "test",
60+
Username: "root",
61+
Password: "123",
62+
Charset: "utf8",
63+
}))
64+
if err != nil {
65+
t.Log(err)
66+
}
67+
```
4968

5069
### 2、定义Model
5170

datasource/common_datasource.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright (C) 2019, Xiongfa Li.
3+
* All right reserved.
4+
* @author xiongfa.li
5+
* @version V1.0
6+
* Description:
7+
*/
8+
9+
package datasource
10+
11+
type CommonDataSource struct {
12+
Name string
13+
Info string
14+
}
15+
16+
func (ds *CommonDataSource) DriverName() string {
17+
return ds.Name
18+
}
19+
20+
func (ds *CommonDataSource) DriverInfo() string {
21+
return ds.Info
22+
}

datasource/datasource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ package datasource
1010

1111
type DataSource interface {
1212
DriverName() string
13-
Info() string
13+
DriverInfo() string
1414
}

datasource/mysql_datasource.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package datasource
1010

1111
import "fmt"
1212

13+
//import _ "github.com/go-sql-driver/mysql"
14+
1315
type MysqlDataSource struct {
1416
Host string
1517
Port int
@@ -23,6 +25,6 @@ func (ds *MysqlDataSource) DriverName() string {
2325
return "mysql"
2426
}
2527

26-
func (ds *MysqlDataSource) Info() string {
28+
func (ds *MysqlDataSource) DriverInfo() string {
2729
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s", ds.Username, ds.Password, ds.Host, ds.Port, ds.DBName, ds.Charset)
2830
}

datasource/postgre_datasource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ func (ds *PostgreDataSource) DriverName() string {
2323
return "postgres"
2424
}
2525

26-
func (ds *PostgreDataSource) Info() string {
26+
func (ds *PostgreDataSource) DriverInfo() string {
2727
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)
2828
}

datasource/sqlite_datasource.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (C) 2019, Xiongfa Li.
2+
// All right reserved.
3+
// @author xiongfa.li
4+
// @version V1.0
5+
// Description:
6+
7+
package datasource
8+
9+
//import _ "github.com/mattn/go-sqlite3"
10+
11+
type SqliteDataSource struct {
12+
Path string
13+
}
14+
15+
func (ds *SqliteDataSource) DriverName() string {
16+
return "sqlite3"
17+
}
18+
19+
func (ds *SqliteDataSource) DriverInfo() string {
20+
return ds.Path
21+
}

faccreator.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import (
1616
type FacOpt func(f *factory.DefaultFactory)
1717

1818
func NewFactory(opts ...FacOpt) factory.Factory {
19+
f, _ := CreateFactory(opts...)
20+
return f
21+
}
22+
23+
func CreateFactory(opts ...FacOpt) (factory.Factory, error) {
1924
f := &factory.DefaultFactory{
2025
Log: logging.DefaultLogf,
2126
}
@@ -38,11 +43,12 @@ func NewFactory(opts ...FacOpt) factory.Factory {
3843
}
3944
}
4045

41-
if f.InitDB() != nil {
42-
return nil
46+
err := f.InitDB()
47+
if err != nil {
48+
return nil, err
4349
}
4450

45-
return f
51+
return f, nil
4652
}
4753

4854
func SetMaxConn(v int) FacOpt {

factory/default_factory.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (f *DefaultFactory) InitDB() error {
5353
return errors.FACTORY_INITED
5454
}
5555

56-
db, err := sql.Open(f.DataSource.DriverName(), f.DataSource.Info())
56+
db, err := sql.Open(f.DataSource.DriverName(), f.DataSource.DriverInfo())
5757
if err != nil {
5858
return err
5959
}
@@ -66,6 +66,13 @@ func (f *DefaultFactory) InitDB() error {
6666
return nil
6767
}
6868

69+
func (f *DefaultFactory) Close() error {
70+
if f.db != nil {
71+
return f.db.Close()
72+
}
73+
return nil
74+
}
75+
6976
func (f *DefaultFactory) CreateTransaction() transaction.Transaction {
7077
return transaction.NewDefaultTransaction(f.DataSource, f.db)
7178
}

factory/factory.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
package factory
1010

1111
import (
12-
"github.com/xfali/gobatis/executor"
13-
"github.com/xfali/gobatis/logging"
14-
"github.com/xfali/gobatis/session"
15-
"github.com/xfali/gobatis/transaction"
12+
"github.com/xfali/gobatis/executor"
13+
"github.com/xfali/gobatis/logging"
14+
"github.com/xfali/gobatis/session"
15+
"github.com/xfali/gobatis/transaction"
1616
)
1717

1818
type Factory interface {
19-
InitDB() error
20-
CreateTransaction() transaction.Transaction
21-
CreateExecutor(transaction.Transaction) executor.Executor
22-
CreateSession() session.SqlSession
23-
LogFunc() logging.LogFunc
19+
InitDB() error
20+
Close() error
21+
22+
CreateTransaction() transaction.Transaction
23+
CreateExecutor(transaction.Transaction) executor.Executor
24+
CreateSession() session.SqlSession
25+
LogFunc() logging.LogFunc
2426
}

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/xfali/gobatis
22

33
go 1.11
44

5-
require github.com/go-sql-driver/mysql v1.4.1
5+
require (
6+
github.com/go-sql-driver/mysql v1.4.1 // indirect
7+
github.com/mattn/go-sqlite3 v1.10.0 // indirect
8+
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
22
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
3+
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
4+
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=

0 commit comments

Comments
 (0)