Skip to content

Commit b159980

Browse files
committed
修改内置interface的默认实现,修改DefaultFactory,使之能更好的集成多种数据源
1 parent 531e455 commit b159980

File tree

11 files changed

+256
-132
lines changed

11 files changed

+256
-132
lines changed

README.md

+11-14
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,17 @@ foreach | foreach 允许指定一个集合,声明可以在元素体内使用
3232

3333
```
3434
func InitDB() *gobatis.SessionManager {
35-
fac := factory.DefaultFactory{
36-
Host: "localhost",
37-
Port: 3306,
38-
DBName: "test",
39-
Username: "root",
40-
Password: "123",
41-
Charset: "utf8",
42-
43-
MaxConn: 1000,
44-
MaxIdleConn: 500,
45-
46-
Log: logging.DefaultLogf,
47-
}
48-
fac.Init()
35+
fac := gobatis.NewFactory(
36+
gobatis.SetMaxConn(100),
37+
gobatis.SetMaxIdleConn(50),
38+
gobatis.SetDataSource(&datasource.MysqlDataSource{
39+
Host: "localhost",
40+
Port: 3306,
41+
DBName: "test",
42+
Username: "root",
43+
Password: "123",
44+
Charset: "utf8",
45+
}))
4946
return gobatis.NewSessionManager(&fac)
5047
}
5148
```

connection/mysql_connection.go renamed to connection/default_connection.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ import (
1818
"github.com/xfali/gobatis/util"
1919
)
2020

21-
type MysqlConnection sql.DB
22-
type MysqlStatement sql.Stmt
21+
type DefaultConnection sql.DB
22+
type DefaultStatement sql.Stmt
2323

24-
func (c *MysqlConnection) Prepare(sqlStr string) (statement.Statement, error) {
24+
func (c *DefaultConnection) Prepare(sqlStr string) (statement.Statement, error) {
2525
db := (*sql.DB)(c)
2626
s, err := db.Prepare(sqlStr)
2727
if err != nil {
2828
return nil, errors.CONNECTION_PREPARE_ERROR
2929
}
30-
return (*MysqlStatement)(s), nil
30+
return (*DefaultStatement)(s), nil
3131
}
3232

33-
func (c *MysqlConnection) Query(ctx context.Context, result reflection.Object, sqlStr string, params ...interface{}) error {
33+
func (c *DefaultConnection) Query(ctx context.Context, result reflection.Object, sqlStr string, params ...interface{}) error {
3434
db := (*sql.DB)(c)
3535
rows, err := db.QueryContext(ctx, sqlStr, params...)
3636
if err != nil {
@@ -42,12 +42,12 @@ func (c *MysqlConnection) Query(ctx context.Context, result reflection.Object, s
4242
return nil
4343
}
4444

45-
func (c *MysqlConnection) Exec(ctx context.Context, sqlStr string, params ...interface{}) (common.Result, error) {
45+
func (c *DefaultConnection) Exec(ctx context.Context, sqlStr string, params ...interface{}) (common.Result, error) {
4646
db := (*sql.DB)(c)
4747
return db.ExecContext(ctx, sqlStr, params...)
4848
}
4949

50-
func (s *MysqlStatement) Query(ctx context.Context, result reflection.Object, params ...interface{}) error {
50+
func (s *DefaultStatement) Query(ctx context.Context, result reflection.Object, params ...interface{}) error {
5151
stmt := (*sql.Stmt)(s)
5252
rows, err := stmt.QueryContext(ctx, params...)
5353
if err != nil {
@@ -59,12 +59,12 @@ func (s *MysqlStatement) Query(ctx context.Context, result reflection.Object, pa
5959
return nil
6060
}
6161

62-
func (s *MysqlStatement) Exec(ctx context.Context, params ...interface{}) (common.Result, error) {
62+
func (s *DefaultStatement) Exec(ctx context.Context, params ...interface{}) (common.Result, error) {
6363
stmt := (*sql.Stmt)(s)
6464
return stmt.ExecContext(ctx, params...)
6565
}
6666

67-
func (s *MysqlStatement) Close() {
67+
func (s *DefaultStatement) Close() {
6868
stmt := (*sql.Stmt)(s)
6969
stmt.Close()
7070
}

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-
Url() string
13+
Info() string
1414
}

datasource/mysql_datasource.go

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

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

datasource/postgre_datasource.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 "fmt"
10+
11+
//import _ "github.com/lib/pq"
12+
13+
type PostgreDataSource struct {
14+
Host string
15+
Port int
16+
DBName string
17+
Username string
18+
Password string
19+
SslMode string
20+
}
21+
22+
func (ds *PostgreDataSource) DriverName() string {
23+
return "postgres"
24+
}
25+
26+
func (ds *PostgreDataSource) Info() string {
27+
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)
28+
}

errors/errcode.go

+36-33
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,42 @@ type ErrCode struct {
1616
fmtErr string `json:"-"`
1717
}
1818

19-
var PARSE_MODEL_TABLEINFO_FAILED *ErrCode = New("11001", "Parse Model's table info failed")
20-
var MODEL_NOT_REGISTER *ErrCode = New("11002", "Register model not found")
21-
var OBJECT_NOT_SUPPORT *ErrCode = New("11101", "Object not support")
22-
var PARSE_OBJECT_NOT_STRUCT *ErrCode = New("11102", "Parse interface's info but not a struct")
23-
var PARSE_OBJECT_NOT_SLICE *ErrCode = New("11103", "Parse interface's info but not a slice")
24-
var PARSE_OBJECT_NOT_MAP *ErrCode = New("11104", "Parse interface's info but not a map")
25-
var PARSE_OBJECT_NOT_SIMPLETYPE *ErrCode = New("11105", "Parse interface's info but not a simple type")
26-
var SLICE_SLICE_NOT_SUPPORT *ErrCode = New("11106", "Parse interface's info: [][]slice not support")
27-
var GET_OBJECTINFO_FAILED *ErrCode = New("11121", "Parse interface's info failed")
28-
var SQL_ID_DUPLICATES *ErrCode = New("11205", "Sql id is duplicates")
29-
var DESERIALIZE_FAILED *ErrCode = New("11206", "Deserialize value failed")
30-
var PARSE_SQL_VAR_ERROR *ErrCode = New("12001", "SQL PARSE ERROR")
31-
var PARSE_SQL_PARAM_ERROR *ErrCode = New("12002", "SQL PARSE parameter error")
32-
var PARSE_SQL_PARAM_VAR_NUMBER_ERROR *ErrCode = New("12003", "SQL PARSE parameter var number error")
33-
var PARSE_DYNAMIC_SQL_ERROR *ErrCode = New("12010", "Parse dynamic sql error")
34-
var EXECUTOR_COMMIT_ERROR *ErrCode = New("21001", "executor was closed when transaction commit")
35-
var EXECUTOR_BEGIN_ERROR *ErrCode = New("21002", "executor was closed when transaction begin")
36-
var EXECUTOR_QUERY_ERROR *ErrCode = New("21003", "executor was closed when exec sql")
37-
var EXECUTOR_GET_CONNECTION_ERROR *ErrCode = New("21003", "executor get connection error")
38-
var TRANSACTION_WITHOUT_BEGIN *ErrCode = New("22001", "Transaction without begin")
39-
var TRANSACTION_COMMIT_ERROR *ErrCode = New("22002", "Transaction commit error")
40-
var TRANSACTION_BUSINESS_ERROR *ErrCode = New("22003", "Business error in transaction")
41-
var CONNECTION_PREPARE_ERROR *ErrCode = New("23001", "Connection prepare error")
42-
var STATEMENT_QUERY_ERROR *ErrCode = New("24001", "statement query error")
43-
var STATEMENT_EXEC_ERROR *ErrCode = New("24002", "statement exec error")
44-
var QUERY_TYPE_ERROR *ErrCode = New("25001", "select data convert error")
45-
var RESULT_POINTER_IS_NIL *ErrCode = New("31000", "result type is a nil pointer")
46-
var RESULT_ISNOT_POINTER *ErrCode = New("31001", "result type is not pointer")
47-
var RESULT_PTR_VALUE_IS_POINTER *ErrCode = New("31002", "result type is pointer of pointer")
48-
var RUNNER_NOT_READY *ErrCode = New("31003", "Runner not ready, may sql or param have some error")
49-
var RESULT_NAME_NOT_FOUND *ErrCode = New("31004", "result name not found")
50-
var RESULT_SELECT_EMPTY_VALUE *ErrCode = New("31005", "select return empty value")
51-
var RESULT_SET_VALUE_FAILED *ErrCode = New("31006", "result set value failed")
19+
var (
20+
FACTORY_INITED = New("10002", "Factory have been initialized")
21+
PARSE_MODEL_TABLEINFO_FAILED = New("11001", "Parse Model's table info failed")
22+
MODEL_NOT_REGISTER = New("11002", "Register model not found")
23+
OBJECT_NOT_SUPPORT = New("11101", "Object not support")
24+
PARSE_OBJECT_NOT_STRUCT = New("11102", "Parse interface's info but not a struct")
25+
PARSE_OBJECT_NOT_SLICE = New("11103", "Parse interface's info but not a slice")
26+
PARSE_OBJECT_NOT_MAP = New("11104", "Parse interface's info but not a map")
27+
PARSE_OBJECT_NOT_SIMPLETYPE = New("11105", "Parse interface's info but not a simple type")
28+
SLICE_SLICE_NOT_SUPPORT = New("11106", "Parse interface's info: [][]slice not support")
29+
GET_OBJECTINFO_FAILED = New("11121", "Parse interface's info failed")
30+
SQL_ID_DUPLICATES = New("11205", "Sql id is duplicates")
31+
DESERIALIZE_FAILED = New("11206", "Deserialize value failed")
32+
PARSE_SQL_VAR_ERROR = New("12001", "SQL PARSE ERROR")
33+
PARSE_SQL_PARAM_ERROR = New("12002", "SQL PARSE parameter error")
34+
PARSE_SQL_PARAM_VAR_NUMBER_ERROR = New("12003", "SQL PARSE parameter var number error")
35+
PARSE_DYNAMIC_SQL_ERROR = New("12010", "Parse dynamic sql error")
36+
EXECUTOR_COMMIT_ERROR = New("21001", "executor was closed when transaction commit")
37+
EXECUTOR_BEGIN_ERROR = New("21002", "executor was closed when transaction begin")
38+
EXECUTOR_QUERY_ERROR = New("21003", "executor was closed when exec sql")
39+
EXECUTOR_GET_CONNECTION_ERROR = New("21003", "executor get connection error")
40+
TRANSACTION_WITHOUT_BEGIN = New("22001", "Transaction without begin")
41+
TRANSACTION_COMMIT_ERROR = New("22002", "Transaction commit error")
42+
TRANSACTION_BUSINESS_ERROR = New("22003", "Business error in transaction")
43+
CONNECTION_PREPARE_ERROR = New("23001", "Connection prepare error")
44+
STATEMENT_QUERY_ERROR = New("24001", "statement query error")
45+
STATEMENT_EXEC_ERROR = New("24002", "statement exec error")
46+
QUERY_TYPE_ERROR = New("25001", "select data convert error")
47+
RESULT_POINTER_IS_NIL = New("31000", "result type is a nil pointer")
48+
RESULT_ISNOT_POINTER = New("31001", "result type is not pointer")
49+
RESULT_PTR_VALUE_IS_POINTER = New("31002", "result type is pointer of pointer")
50+
RUNNER_NOT_READY = New("31003", "Runner not ready, may sql or param have some error")
51+
RESULT_NAME_NOT_FOUND = New("31004", "result name not found")
52+
RESULT_SELECT_EMPTY_VALUE = New("31005", "select return empty value")
53+
RESULT_SET_VALUE_FAILED = New("31006", "result set value failed")
54+
)
5255

5356
func New(code, message string) *ErrCode {
5457
ret := &ErrCode{

faccreator.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (C) 2019, Xiongfa Li.
2+
// All right reserved.
3+
// @author xiongfa.li
4+
// @version V1.0
5+
// Description:
6+
7+
package gobatis
8+
9+
import (
10+
"github.com/xfali/gobatis/datasource"
11+
"github.com/xfali/gobatis/factory"
12+
"github.com/xfali/gobatis/logging"
13+
"time"
14+
)
15+
16+
type FacOpt func(f *factory.DefaultFactory)
17+
18+
func NewFactory(opts ...FacOpt) factory.Factory {
19+
f := &factory.DefaultFactory{
20+
Log: logging.DefaultLogf,
21+
}
22+
23+
if len(opts) > 0 {
24+
for _, opt := range opts {
25+
opt(f)
26+
}
27+
}
28+
29+
//For compatibility with older versions
30+
if f.DataSource == nil {
31+
f.DataSource = &datasource.MysqlDataSource{
32+
Host: f.Host,
33+
Port: f.Port,
34+
DBName: f.DBName,
35+
Username: f.Username,
36+
Password: f.Password,
37+
Charset: f.Charset,
38+
}
39+
}
40+
41+
if f.InitDB() != nil {
42+
return nil
43+
}
44+
45+
return f
46+
}
47+
48+
func SetMaxConn(v int) FacOpt {
49+
return func(f *factory.DefaultFactory) {
50+
f.MaxConn = v
51+
}
52+
}
53+
54+
func SetMaxIdleConn(v int) FacOpt {
55+
return func(f *factory.DefaultFactory) {
56+
f.MaxIdleConn = v
57+
}
58+
}
59+
60+
func SetConnMaxLifetime(v time.Duration) FacOpt {
61+
return func(f *factory.DefaultFactory) {
62+
f.ConnMaxLifetime = v
63+
}
64+
}
65+
66+
func SetLog(v logging.LogFunc) FacOpt {
67+
return func(f *factory.DefaultFactory) {
68+
f.Log = v
69+
}
70+
}
71+
72+
func SetDataSource(v datasource.DataSource) FacOpt {
73+
return func(f *factory.DefaultFactory) {
74+
f.WithLock(func(fac *factory.DefaultFactory) {
75+
fac.DataSource = v
76+
})
77+
}
78+
}

0 commit comments

Comments
 (0)