Skip to content

Commit fef899b

Browse files
committed
修改factory的接口方法
1 parent ab98e71 commit fef899b

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

faccreator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func CreateFactory(opts ...FacOpt) (factory.Factory, error) {
4343
}
4444
}
4545

46-
err := f.InitDB()
46+
err := f.Open(f.DataSource)
4747
if err != nil {
4848
return nil, err
4949
}

factory/default_factory.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ type DefaultFactory struct {
4545
mutex sync.Mutex
4646
}
4747

48-
func (f *DefaultFactory) InitDB() error {
48+
func (f *DefaultFactory) Open(ds datasource.DataSource) error {
4949
f.mutex.Lock()
5050
defer f.mutex.Unlock()
5151

5252
if f.db != nil {
5353
return errors.FACTORY_INITED
5454
}
5555

56+
if ds != nil {
57+
f.DataSource = ds
58+
}
59+
5660
db, err := sql.Open(f.DataSource.DriverName(), f.DataSource.DriverInfo())
5761
if err != nil {
5862
return err
@@ -73,6 +77,10 @@ func (f *DefaultFactory) Close() error {
7377
return nil
7478
}
7579

80+
func (f *DefaultFactory) GetDataSource() datasource.DataSource {
81+
return f.DataSource
82+
}
83+
7684
func (f *DefaultFactory) CreateTransaction() transaction.Transaction {
7785
return transaction.NewDefaultTransaction(f.DataSource, f.db)
7886
}

factory/factory.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
package factory
1010

1111
import (
12+
"github.com/xfali/gobatis/datasource"
1213
"github.com/xfali/gobatis/executor"
1314
"github.com/xfali/gobatis/logging"
1415
"github.com/xfali/gobatis/session"
1516
"github.com/xfali/gobatis/transaction"
1617
)
1718

1819
type Factory interface {
19-
InitDB() error
20+
Open(datasource.DataSource) error
2021
Close() error
2122

23+
GetDataSource() datasource.DataSource
24+
2225
CreateTransaction() transaction.Transaction
2326
CreateExecutor(transaction.Transaction) executor.Executor
2427
CreateSession() session.SqlSession

0 commit comments

Comments
 (0)