Skip to content

Commit

Permalink
transaction manager constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
quintans committed Jun 13, 2022
1 parent 7ae9502 commit 5d89cde
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
33 changes: 9 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ Another example with an update
store.Update(PUBLISHER).Submit(&publisher)
```

and the shorter version...
and the shortest version...

```go
store.Modify(&publisher)
```
<br>
Besides the examples in this page there are a lot more examples in the
[Kanban taskboard](//github.com/quintans/taskboard) specifically [here](//github.com/quintans/taskboard/blob/master/biz/impl/TaskBoardService.go).


## Features

Expand Down Expand Up @@ -209,31 +207,18 @@ var (
// the transaction manager
var TM ITransactionManager

func init() {
func main() {
// database configuration
mydb, err := sql.Open("mysql", "root:root@/gosql?parseTime=true")
if err != nil {
panic(err)
}

translator := trx.NewMySQL5Translator()

// transaction manager
TM = NewTransactionManager(
// database
mydb,
// database context factory
func(c dbx.IConnection) IDb {
return NewDb(c, translator)
},
// statement cache
1000,
)
}
tm = NewDefaultTransactionManager(mydb, trx.NewMySQL5Translator())

func main() {
// get the databse context
store := TM.Store()
// get the database context
store := tm.Store()
// the target entity
var publisher Publisher
// Retrieve
Expand All @@ -246,14 +231,14 @@ func main() {
}
```

Source from [intro.go](intro.go).
Source from [basic.go](./example/basic.go).

You can also check out [common.go](test/common/common.go) for a lot more examples.
You can also check out [common.go](./test/common/common.go) for a lot more examples.

<br>

In the following chapters I will try to explain the several aspects of the library using a set of examples.
These examples are supported by tables defined in [tables_mysql.sql](test/mysql/tables_mysql.sql), a MySQL database sql script.
These examples are supported by tables defined in [tables_mysql.sql](./test/mysql/tables_mysql.sql), a MySQL database sql script.

I will start first by describing the table model and how to map the entities.

Expand Down
31 changes: 26 additions & 5 deletions db/transaction_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,34 @@ type TransactionManager struct {
// dbFactory is a database connection factory. This factory accepts boolean flag that indicates if the created IDb is still valid.
// This may be useful if an Entity holds a reference to the IDb to do lazy loading.
func NewTransactionManager(database *sql.DB, dbFactory func(dbx.IConnection) IDb, capacity int) *TransactionManager {
this := new(TransactionManager)
this.database = database
this.dbFactory = dbFactory
t := new(TransactionManager)
t.database = database
t.dbFactory = dbFactory
if capacity > 1 {
this.stmtCache = cache.NewLRUCache(capacity)
t.stmtCache = cache.NewLRUCache(capacity)
}
return this
return t
}

func NewDefaultTransactionManager(database *sql.DB, translator Translator) *TransactionManager {
// transaction manager
return NewTransactionManager(
database,
func(c dbx.IConnection) IDb {
return NewDb(c, translator)
},
1000,
)
}

func (t *TransactionManager) SetCacheSize(capacity int) {
if capacity > 1 {
t.stmtCache = cache.NewLRUCache(capacity)
}
}

func (t *TransactionManager) SetDbFactory(dbFactory func(dbx.IConnection) IDb) {
t.dbFactory = dbFactory
}

func (t *TransactionManager) With(db IDb) ITransactionManager {
Expand Down
24 changes: 5 additions & 19 deletions example/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"github.com/quintans/goSQL/db"
"github.com/quintans/goSQL/dbx"
trx "github.com/quintans/goSQL/translators"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -27,38 +26,25 @@ var (
)

// the transaction manager
var TM db.ITransactionManager
var tm db.ITransactionManager

func init() {
func main() {
// database configuration
mydb, err := sql.Open("mysql", "root:root@/gosql?parseTime=true")
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
}

translator := trx.NewMySQL5Translator()

// transaction manager
TM = db.NewTransactionManager(
// database
mydb,
// database context factory
func(c dbx.IConnection) db.IDb {
return db.NewDb(c, translator)
},
// statement cache
1000,
)
}
tm = db.NewDefaultTransactionManager(mydb, trx.NewMySQL5Translator())

func main() {
// get the databse context
store := TM.Store()
store := tm.Store()
// the target entity
var publisher Publisher
// Retrieve
_, err := store.Retrieve(&publisher, 2)
_, err = store.Retrieve(&publisher, 2)
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
Expand Down

0 comments on commit 5d89cde

Please sign in to comment.