Skip to content
This repository has been archived by the owner on Jun 19, 2019. It is now read-only.

Improve column string performance #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions column.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package core

import (
"bytes"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -73,30 +74,45 @@ func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable

// String generate column description string according dialect
func (col *Column) String(d Dialect) string {
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
var b bytes.Buffer
fmt.Fprint(&b, d.QuoteStr())
fmt.Fprint(&b, col.Name)
fmt.Fprint(&b, d.QuoteStr(), " ")

sql += d.SqlType(col) + " "
sqlType := d.SqlType(col)
fmt.Fprint(&b, sqlType, " ")

if col.IsPrimaryKey {
sql += "PRIMARY KEY "
fmt.Fprint(&b, "PRIMARY KEY ")

if col.IsAutoIncrement {
sql += d.AutoIncrStr() + " "
fmt.Fprint(&b, d.AutoIncrStr(), " ")
}
}

if col.Default != "" {
sql += "DEFAULT " + col.Default + " "
fmt.Fprint(&b, "DEFAULT ")
if strings.EqualFold(sqlType, "BOOL") {
if strings.EqualFold(col.Default, "FALSE") || col.Default == "0" {
fmt.Fprint(&b, "FALSE")
} else {
fmt.Fprint(&b, "TRUE")
}
} else {
fmt.Fprint(&b, col.Default)
}
fmt.Fprint(&b, " ")
}

if d.ShowCreateNull() {
if col.Nullable {
sql += "NULL "
fmt.Fprint(&b, "NULL ")
} else {
sql += "NOT NULL "
fmt.Fprint(&b, "NOT NULL ")
}
}

return sql
return b.String()
}

// StringNoPk generate column description string according dialect without primary keys
Expand Down