Optimise SimpleInsert.
This commit is contained in:
parent
7bc2146088
commit
12d733932b
|
@ -265,15 +265,26 @@ func (a *MysqlAdapter) AddForeignKey(name string, table string, column string, f
|
||||||
return querystr, nil
|
return querystr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var silen1 = len("INSERT INTO ``() VALUES () ")
|
||||||
func (adapter *MysqlAdapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) {
|
func (adapter *MysqlAdapter) SimpleInsert(name string, table string, columns string, fields string) (string, error) {
|
||||||
if table == "" {
|
if table == "" {
|
||||||
return "", errors.New("You need a name for this table")
|
return "", errors.New("You need a name for this table")
|
||||||
}
|
}
|
||||||
|
|
||||||
var querystr = "INSERT INTO `" + table + "`("
|
var sb strings.Builder
|
||||||
|
sb.Grow(silen1 + len(table))
|
||||||
|
sb.WriteString("INSERT INTO `")
|
||||||
|
sb.WriteString(table)
|
||||||
|
sb.WriteString("`(")
|
||||||
if columns != "" {
|
if columns != "" {
|
||||||
querystr += adapter.buildColumns(columns) + ") VALUES ("
|
sb.WriteString(adapter.buildColumns(columns))
|
||||||
for _, field := range processFields(fields) {
|
sb.WriteString(") VALUES (")
|
||||||
|
fs := processFields(fields)
|
||||||
|
sb.Grow(len(fs) * 3)
|
||||||
|
for i, field := range fs {
|
||||||
|
if i != 0 {
|
||||||
|
sb.WriteString(",")
|
||||||
|
}
|
||||||
nameLen := len(field.Name)
|
nameLen := len(field.Name)
|
||||||
if field.Name[0] == '"' && field.Name[nameLen-1] == '"' && nameLen >= 3 {
|
if field.Name[0] == '"' && field.Name[nameLen-1] == '"' && nameLen >= 3 {
|
||||||
field.Name = "'" + field.Name[1:nameLen-1] + "'"
|
field.Name = "'" + field.Name[1:nameLen-1] + "'"
|
||||||
|
@ -281,17 +292,17 @@ func (adapter *MysqlAdapter) SimpleInsert(name string, table string, columns str
|
||||||
if field.Name[0] == '\'' && field.Name[nameLen-1] == '\'' && nameLen >= 3 {
|
if field.Name[0] == '\'' && field.Name[nameLen-1] == '\'' && nameLen >= 3 {
|
||||||
field.Name = "'" + strings.Replace(field.Name[1:nameLen-1], "'", "''", -1) + "'"
|
field.Name = "'" + strings.Replace(field.Name[1:nameLen-1], "'", "''", -1) + "'"
|
||||||
}
|
}
|
||||||
querystr += field.Name + ","
|
sb.WriteString(field.Name)
|
||||||
}
|
}
|
||||||
querystr = querystr[0 : len(querystr)-1]
|
sb.WriteString(")")
|
||||||
} else {
|
} else {
|
||||||
querystr += ") VALUES ("
|
sb.WriteString(") VALUES ()")
|
||||||
}
|
}
|
||||||
querystr += ")"
|
|
||||||
|
|
||||||
// TODO: Shunt the table name logic and associated stmt list up to the a higher layer to reduce the amount of unnecessary overhead in the builder / accumulator
|
// TODO: Shunt the table name logic and associated stmt list up to the a higher layer to reduce the amount of unnecessary overhead in the builder / accumulator
|
||||||
adapter.pushStatement(name, "insert", querystr)
|
q := sb.String()
|
||||||
return querystr, nil
|
adapter.pushStatement(name, "insert", q)
|
||||||
|
return q, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (adapter *MysqlAdapter) buildColumns(columns string) (querystr string) {
|
func (adapter *MysqlAdapter) buildColumns(columns string) (querystr string) {
|
||||||
|
|
Loading…
Reference in New Issue