Skip to content

Commit

Permalink
backport: Initialise plans using sync.Once
Browse files Browse the repository at this point in the history
Backport of 7f030ea. Fixes data race.
  • Loading branch information
neelance committed Mar 22, 2016
1 parent c87af80 commit d1ddc3c
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
"database/sql/driver"
"errors"
"fmt"
"log"
"os"
"reflect"
"regexp"
"strings"
"sync"
"time"
"log"
"os"
)

// Oracle String (empty string is null)
Expand Down Expand Up @@ -283,9 +284,10 @@ type bindPlan struct {
versField string
autoIncrIdx int
autoIncrFieldName string
once sync.Once
}

func (plan bindPlan) createBindInstance(elem reflect.Value, conv TypeConverter) (bindInstance, error) {
func (plan *bindPlan) createBindInstance(elem reflect.Value, conv TypeConverter) (bindInstance, error) {
bi := bindInstance{query: plan.query, autoIncrIdx: plan.autoIncrIdx, autoIncrFieldName: plan.autoIncrFieldName, versField: plan.versField}
if plan.versField != "" {
bi.existingVersion = elem.FieldByName(plan.versField).Int()
Expand Down Expand Up @@ -339,8 +341,8 @@ type bindInstance struct {
}

func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
plan := t.insertPlan
if plan.query == "" {
plan := &t.insertPlan
plan.once.Do(func() {
plan.autoIncrIdx = -1

s := bytes.Buffer{}
Expand Down Expand Up @@ -390,15 +392,14 @@ func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
s.WriteString(t.dbmap.Dialect.QuerySuffix())

plan.query = s.String()
t.insertPlan = plan
}
})

return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}

func (t *TableMap) bindUpdate(elem reflect.Value) (bindInstance, error) {
plan := t.updatePlan
if plan.query == "" {
plan := &t.updatePlan
plan.once.Do(func() {

s := bytes.Buffer{}
s.WriteString(fmt.Sprintf("update %s set ", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName)))
Expand Down Expand Up @@ -448,15 +449,14 @@ func (t *TableMap) bindUpdate(elem reflect.Value) (bindInstance, error) {
s.WriteString(t.dbmap.Dialect.QuerySuffix())

plan.query = s.String()
t.updatePlan = plan
}
})

return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}

func (t *TableMap) bindDelete(elem reflect.Value) (bindInstance, error) {
plan := t.deletePlan
if plan.query == "" {
plan := &t.deletePlan
plan.once.Do(func() {

s := bytes.Buffer{}
s.WriteString(fmt.Sprintf("delete from %s", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName)))
Expand Down Expand Up @@ -494,15 +494,14 @@ func (t *TableMap) bindDelete(elem reflect.Value) (bindInstance, error) {
s.WriteString(t.dbmap.Dialect.QuerySuffix())

plan.query = s.String()
t.deletePlan = plan
}
})

return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}

func (t *TableMap) bindGet() bindPlan {
plan := t.getPlan
if plan.query == "" {
func (t *TableMap) bindGet() *bindPlan {
plan := &t.getPlan
plan.once.Do(func() {

s := bytes.Buffer{}
s.WriteString("select ")
Expand Down Expand Up @@ -535,8 +534,7 @@ func (t *TableMap) bindGet() bindPlan {
s.WriteString(t.dbmap.Dialect.QuerySuffix())

plan.query = s.String()
t.getPlan = plan
}
})

return plan
}
Expand Down

0 comments on commit d1ddc3c

Please sign in to comment.