Skip to content

Commit

Permalink
Add minor changes (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m authored Sep 30, 2018
1 parent eb6ce79 commit c66bb5b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
6 changes: 3 additions & 3 deletions bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ set +e
clear

echo "postgres"
SODA_DIALECT=postgres go test -bench=.
SODA_DIALECT=postgres go test -tags sqlite -bench=.
echo "--------------------"
echo "mysql"
SODA_DIALECT=mysql go test -bench=.
SODA_DIALECT=mysql go test -tags sqlite -bench=.
echo "--------------------"
echo "sqlite"
SODA_DIALECT=sqlite go test -bench=.
SODA_DIALECT=sqlite go test -tags sqlite -bench=.
10 changes: 6 additions & 4 deletions executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
return err
}

cols := columns.ForStructWithAlias(m.Value, m.TableName(), m.As)
tn := m.TableName()
cols := columns.ForStructWithAlias(m.Value, tn, m.As)

if sm.TableName() == m.TableName() {
if tn == sm.TableName() {
cols.Remove(excludeColumns...)
}

Expand Down Expand Up @@ -161,10 +162,11 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
return err
}

cols := columns.ForStructWithAlias(model, m.TableName(), m.As)
tn := m.TableName()
cols := columns.ForStructWithAlias(model, tn, m.As)
cols.Remove("id", "created_at")

if m.TableName() == sm.TableName() {
if tn == sm.TableName() {
cols.Remove(excludeColumns...)
}

Expand Down
4 changes: 2 additions & 2 deletions finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (q *Query) Find(model interface{}, id interface{}) error {
break
}
}
idq := fmt.Sprintf("%s.id = ?", tn)
idq := m.whereID()
switch t := id.(type) {
case uuid.UUID:
return q.Where(idq, t.String()).First(model)
Expand Down Expand Up @@ -220,7 +220,7 @@ func (q *Query) eagerAssociations(model interface{}) error {
return err
}

//disable eager mode for current connection.
// disable eager mode for current connection.
q.eager = false
q.Connection.eager = false

Expand Down
11 changes: 6 additions & 5 deletions sql_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ func (sq *sqlBuilder) buildPaginationClauses(sql string) string {
return sql
}

// columnCache is used to prevent columns rebuilding.
var columnCache = map[string]columns.Columns{}
var columnCacheMutex = sync.Mutex{}
var columnCacheMutex = sync.RWMutex{}

func (sq *sqlBuilder) buildColumns() columns.Columns {
tableName := sq.Model.TableName()
Expand All @@ -219,11 +220,11 @@ func (sq *sqlBuilder) buildColumns() columns.Columns {
asName = strings.Replace(tableName, ".", "_", -1)
}
acl := len(sq.AddColumns)
if acl <= 0 {
columnCacheMutex.Lock()
if acl == 0 {
columnCacheMutex.RLock()
cols, ok := columnCache[tableName]
columnCacheMutex.Unlock()
//if alias is different, remake columns
columnCacheMutex.RUnlock()
// if alias is the same, don't remake columns
if ok && cols.TableAlias == asName {
return cols
}
Expand Down

0 comments on commit c66bb5b

Please sign in to comment.