-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
修改内置interface的默认实现,修改DefaultFactory,使之能更好的集成多种数据源
- Loading branch information
Showing
11 changed files
with
256 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,5 @@ package datasource | |
|
||
type DataSource interface { | ||
DriverName() string | ||
Url() string | ||
Info() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (C) 2019, Xiongfa Li. | ||
// All right reserved. | ||
// @author xiongfa.li | ||
// @version V1.0 | ||
// Description: | ||
|
||
package datasource | ||
|
||
import "fmt" | ||
|
||
//import _ "github.com/lib/pq" | ||
|
||
type PostgreDataSource struct { | ||
Host string | ||
Port int | ||
DBName string | ||
Username string | ||
Password string | ||
SslMode string | ||
} | ||
|
||
func (ds *PostgreDataSource) DriverName() string { | ||
return "postgres" | ||
} | ||
|
||
func (ds *PostgreDataSource) Info() 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (C) 2019, Xiongfa Li. | ||
// All right reserved. | ||
// @author xiongfa.li | ||
// @version V1.0 | ||
// Description: | ||
|
||
package gobatis | ||
|
||
import ( | ||
"github.com/xfali/gobatis/datasource" | ||
"github.com/xfali/gobatis/factory" | ||
"github.com/xfali/gobatis/logging" | ||
"time" | ||
) | ||
|
||
type FacOpt func(f *factory.DefaultFactory) | ||
|
||
func NewFactory(opts ...FacOpt) factory.Factory { | ||
f := &factory.DefaultFactory{ | ||
Log: logging.DefaultLogf, | ||
} | ||
|
||
if len(opts) > 0 { | ||
for _, opt := range opts { | ||
opt(f) | ||
} | ||
} | ||
|
||
//For compatibility with older versions | ||
if f.DataSource == nil { | ||
f.DataSource = &datasource.MysqlDataSource{ | ||
Host: f.Host, | ||
Port: f.Port, | ||
DBName: f.DBName, | ||
Username: f.Username, | ||
Password: f.Password, | ||
Charset: f.Charset, | ||
} | ||
} | ||
|
||
if f.InitDB() != nil { | ||
return nil | ||
} | ||
|
||
return f | ||
} | ||
|
||
func SetMaxConn(v int) FacOpt { | ||
return func(f *factory.DefaultFactory) { | ||
f.MaxConn = v | ||
} | ||
} | ||
|
||
func SetMaxIdleConn(v int) FacOpt { | ||
return func(f *factory.DefaultFactory) { | ||
f.MaxIdleConn = v | ||
} | ||
} | ||
|
||
func SetConnMaxLifetime(v time.Duration) FacOpt { | ||
return func(f *factory.DefaultFactory) { | ||
f.ConnMaxLifetime = v | ||
} | ||
} | ||
|
||
func SetLog(v logging.LogFunc) FacOpt { | ||
return func(f *factory.DefaultFactory) { | ||
f.Log = v | ||
} | ||
} | ||
|
||
func SetDataSource(v datasource.DataSource) FacOpt { | ||
return func(f *factory.DefaultFactory) { | ||
f.WithLock(func(fac *factory.DefaultFactory) { | ||
fac.DataSource = v | ||
}) | ||
} | ||
} |
Oops, something went wrong.