@@ -23,7 +23,7 @@ library for Go.
2323 DB.SQL(` SELECT * FROM people LIMIT 10 ` ).QueryStructs(&people)
2424 ` ` `
2525
26- * JSON Document retrieval (single trip to Postgres ! , requires Postgres 9.3 +)
26+ * JSON Document retrieval (single trip to Postgres , requires Postgres 9.3 +)
2727
2828 ` ` ` go
2929 DB.SelectDoc("id", "user_name", "avatar").
@@ -140,7 +140,7 @@ func init() {
140140 panic("Could not ping database")
141141 }
142142
143- // set to reasonable values
143+ // set to reasonable values for production
144144 db.SetMaxIdleConns(4)
145145 db.SetMaxOpenConns(16)
146146
@@ -163,7 +163,7 @@ type Post struct {
163163 Body string ` db:" body" `
164164 UserID int64 ` db:" user_id" `
165165 State string ` db:" state" `
166- UpdatedAt dat.Nulltime ` db:" updated_at" `
166+ UpdatedAt dat.NullTime ` db:" updated_at" `
167167 CreatedAt dat.NullTime ` db:" created_at" `
168168}
169169
@@ -260,7 +260,6 @@ DB.InsertInto("payments").
260260// ensure session user can only update his information
261261DB.Update("users").
262262 SetWhitelist(user, "user_name", "avatar", "quote").
263- Record(userData).
264263 Where("id = $1", session.UserID).
265264 Exec()
266265` ` `
@@ -282,7 +281,7 @@ b.MustInterpolate() == "SELECT * FROM posts WHERE id IN (10,20,30,40,50)"
282281` dat` uses [logxi](https:// github.com/mgutz/logxi) for logging. By default,
283282*logxi* logs all warnings and errors to the console. ` dat` logs the
284283SQL and its arguments on any error . In addition, ` dat` logs slow queries
285- as warnings if ` LogQueriesThreshold > 0`
284+ as warnings if ` runner. LogQueriesThreshold > 0`
286285
287286To trace all SQL , set environment variable
288287
@@ -298,7 +297,7 @@ Use `Returning` and `QueryStruct` to insert and update struct fields in one
298297trip
299298
300299` ` ` go
301- post := Post{Title: "Swith to Postgres", State: "open"}
300+ var post Post
302301
303302err := DB.
304303 InsertInto("posts").
@@ -316,15 +315,15 @@ post := Post{Title: "Go is awesome", State: "open"}
316315err := DB.
317316 InsertInto("posts").
318317 Blacklist("id", "user_id", "created_at", "updated_at").
319- Record(post).
318+ Record(& post).
320319 Returning("id", "created_at", "updated_at").
321320 QueryStruct(&post)
322321
323322// use wildcard to include all columns
324323err := DB.
325324 InsertInto("posts").
326325 Whitelist("*").
327- Record(post).
326+ Record(& post).
328327 Returning("id", "created_at", "updated_at").
329328 QueryStruct(&post)
330329
@@ -491,7 +490,7 @@ result, err = DB.
491490
492491### Joins
493492
494- Define JOINs as arguments to ` From`
493+ Define JOINs in argument to ` From`
495494
496495` ` ` go
497496err = DB.
@@ -594,6 +593,52 @@ func getUsers(conn runner.Connection) ([]*dto.Users, error) {
594593}
595594```
596595
596+ #### Nested Transactions
597+
598+ Nested transaction logic is as follows:
599+
600+ * If `Commit` is called in a nested transaction, the operation results in no operation (NOOP).
601+ Only the top level `Commit` commits the transaction to the database.
602+
603+ * If `Rollback` is called in a nested transaction, then the entire
604+ transaction is rolled back. `Tx.IsRollbacked` is set to true.
605+
606+ * Either `defer Tx.AutoCommit()` or `defer Tx.AutoRollback()` **MUST BE CALLED**
607+ for each corresponding `Begin`. The internal state of nested transactions is
608+ tracked in these two methods.
609+
610+ ```go
611+ func nested(conn runner.Connection) error {
612+ tx, err := conn.Begin()
613+ if err != nil {
614+ return err
615+ }
616+ defer tx.AutoRollback()
617+
618+ _, err := tx.SQL(`INSERT INTO users (email) values $1`, ' me@home.com ' ).Exec()
619+ if err != nil {
620+ return err
621+ }
622+ // prevents AutoRollback
623+ tx.Commit()
624+ }
625+
626+ func top() {
627+ tx, err := DB.Begin()
628+ if err != nil {
629+ logger.Fatal("Could not create transaction")
630+ }
631+ defer tx.AutoRollback()
632+
633+ err := nested(tx)
634+ if err != nil {
635+ return
636+ }
637+ // top level commits the transaction
638+ tx.Commit()
639+ }
640+ ```
641+
597642### Dates
598643
599644Use `dat.NullTime` type to properly handle nullable dates
@@ -661,80 +706,6 @@ err := DB.
661706 QueryStruct(&post)
662707```
663708
664- ### Transactions
665-
666- ```go
667- // Start transaction
668- tx, err := DB.Begin()
669- if err != nil {
670- return err
671- }
672- // safe to call tx.Rollback() or tx.Commit() later
673- defer tx.AutoRollback()
674-
675- // Issue statements that might cause errors
676- res, err := tx.
677- Update("posts").
678- Set("state", "deleted").
679- Where("deleted_at IS NOT NULL").
680- Exec()
681-
682- if err != nil {
683- // AutoRollback will rollback the transaction
684- return err
685- }
686-
687- // commit to prevent AutoRollback from rolling back the transaction
688- tx.Commit()
689- return nil
690- ```
691-
692- #### Nested Transactions
693-
694- Nested transaction logic is as follows:
695-
696- * If `Commit` is called in a nested transaction, the operation results in no operatoin (NOOP).
697- Only the top level `Commit` commits the transaction to the database.
698-
699- * If `Rollback` is called in a nested transaction, then the entire
700- transaction is rolled back. `Tx.IsRollbacked` is set to true.
701-
702- * Either `defer Tx.AutoCommit()` or `defer Tx.AutoRollback()` **MUST BE CALLED**
703- for each corresponding `Begin`. The internal state of nested transactions is
704- tracked in these two methods.
705-
706- ```go
707- func nested(conn runner.Connection) error {
708- tx, err := conn.Begin()
709- if err != nil {
710- return err
711- }
712- defer tx.AutoRollback()
713-
714- _, err := tx.SQL(`INSERT INTO users (email) values $1`, ' me@home.com ' ).Exec()
715- if err != nil {
716- return err
717- }
718- // prevents AutoRollback
719- tx.Commit()
720- }
721-
722- func top() {
723- tx, err := DB.Begin()
724- if err != nil {
725- logger.Fatal("Could not create transaction")
726- }
727- defer tx.AutoRollback()
728-
729- err := nested(tx)
730- if err != nil {
731- return
732- }
733- // top level commits the transaction
734- tx.Commit()
735- }
736- ```
737-
738709### Caching
739710
740711dat implements caching backed by an in-memory or Redis store. The in-memory store
0 commit comments