mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-19 09:14:27 +08:00
add comments
This commit is contained in:
@@ -6,22 +6,27 @@ import (
|
||||
"github.com/mayswind/lab/pkg/errs"
|
||||
)
|
||||
|
||||
// DataStore represents a data storage containing a series of database shards
|
||||
type DataStore struct {
|
||||
databases []*Database
|
||||
}
|
||||
|
||||
// Choose returns a database instance by sharding key
|
||||
func (s *DataStore) Choose(key int64) *Database {
|
||||
return s.databases[0]
|
||||
}
|
||||
|
||||
// Query returns a new database session in a specific database by sharding key
|
||||
func (s *DataStore) Query(key int64) *xorm.Session {
|
||||
return s.Choose(key).NewSession()
|
||||
}
|
||||
|
||||
// DoTransaction runs a new database transaction in a specific database by sharding key
|
||||
func (s *DataStore) DoTransaction(key int64, fn func(sess *xorm.Session) error) (err error) {
|
||||
return s.Choose(key).DoTransaction(fn)
|
||||
}
|
||||
|
||||
// SyncStructs updates database structs by database models
|
||||
func (s *DataStore) SyncStructs(beans ...interface{}) error {
|
||||
var err error
|
||||
|
||||
@@ -36,6 +41,7 @@ func (s *DataStore) SyncStructs(beans ...interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// NewDataStore returns a new data storage by a series of database
|
||||
func NewDataStore(databases ...*Database) (*DataStore, error) {
|
||||
if len(databases) < 1 {
|
||||
return nil, errs.ErrDatabaseIsNull
|
||||
|
||||
Reference in New Issue
Block a user