remove arbitrary restrictions on text fields in MysqlAdapter.SetDefaultColumn

save allocs in mysql query adapter
This commit is contained in:
Azareal 2020-07-13 16:53:04 +10:00
parent d1f977154f
commit 4bdc528744
1 changed files with 40 additions and 16 deletions

View File

@ -123,10 +123,14 @@ func (a *MysqlAdapter) CreateTable(name, table, charset, collation string, colum
q += "," q += ","
} else { } else {
q += "(" q += "("
for _, column := range strings.Split(key.Columns, ",") { for i, column := range strings.Split(key.Columns, ",") {
q += "`" + column + "`," if i != 0 {
q += ",`" + column + "`"
} else {
q += "`" + column + "`"
} }
q = q[0:len(q)-1] + ")," }
q += "),"
} }
} }
} }
@ -167,9 +171,6 @@ func (a *MysqlAdapter) ChangeColumn(name, table, colName string, col DBTableColu
} }
func (a *MysqlAdapter) SetDefaultColumn(name, table, colName, colType, defaultStr string) (string, error) { func (a *MysqlAdapter) SetDefaultColumn(name, table, colName, colType, defaultStr string) (string, error) {
if colType == "text" {
return "", errors.New("text fields cannot have default values")
}
if defaultStr == "" { if defaultStr == "" {
defaultStr = "''" defaultStr = "''"
} }
@ -864,6 +865,25 @@ func (a *MysqlAdapter) buildOrderby(orderby string) (q string) {
return q return q
} }
func (a *MysqlAdapter) buildOrderbySb(sb *strings.Builder, orderby string) {
if len(orderby) != 0 {
ord := processOrderby(orderby)
sb.Grow(10 + (len(ord) * 8) - 1)
sb.WriteString(" ORDER BY ")
for i, col := range ord {
// TODO: We might want to escape this column
if i != 0 {
sb.WriteString(",`")
} else {
sb.WriteString("`")
}
sb.WriteString(strings.Replace(col.Column, ".", "`.`", -1))
sb.WriteString("` ")
sb.WriteString(strings.ToUpper(col.Order))
}
}
}
func (a *MysqlAdapter) SimpleSelect(name, table, cols, where, orderby, limit string) (string, error) { func (a *MysqlAdapter) SimpleSelect(name, table, cols, where, orderby, limit 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")
@ -898,8 +918,8 @@ func (a *MysqlAdapter) SimpleSelect(name, table, cols, where, orderby, limit str
if err != nil { if err != nil {
return "", err return "", err
} }
sb.WriteString(a.buildOrderby(orderby)) a.buildOrderbySb(sb, orderby)
sb.WriteString(a.buildLimit(limit)) a.buildLimitSb(sb, limit)
q := strings.TrimSpace(sb.String()) q := strings.TrimSpace(sb.String())
queryStrPool.Put(sb) queryStrPool.Put(sb)
@ -960,11 +980,8 @@ func (a *MysqlAdapter) complexSelect(preBuilder *selectPrebuilder, sb *strings.B
} }
} }
orderby := a.buildOrderby(preBuilder.orderby) a.buildOrderbySb(sb, preBuilder.orderby)
limit := a.buildLimit(preBuilder.limit) a.buildLimitSb(sb, preBuilder.limit)
sb.Grow(len(orderby) + len(limit))
sb.WriteString(orderby)
sb.WriteString(limit)
return nil return nil
} }
@ -1143,6 +1160,13 @@ func (a *MysqlAdapter) buildLimit(limit string) (q string) {
return q return q
} }
func (a *MysqlAdapter) buildLimitSb(sb *strings.Builder, limit string) {
if limit != "" {
sb.WriteString(" LIMIT ")
sb.WriteString(limit)
}
}
func (a *MysqlAdapter) buildJoinColumns(cols string) (q string) { func (a *MysqlAdapter) buildJoinColumns(cols string) (q string) {
for _, col := range processColumns(cols) { for _, col := range processColumns(cols) {
// TODO: Move the stirng and number logic to processColumns? // TODO: Move the stirng and number logic to processColumns?
@ -1208,7 +1232,7 @@ func (a *MysqlAdapter) SimpleCount(name, table, where, limit string) (q string,
if err != nil { if err != nil {
return "", err return "", err
} }
sb.WriteString(a.buildLimit(limit)) a.buildLimitSb(sb, limit)
q = strings.TrimSpace(sb.String()) q = strings.TrimSpace(sb.String())
queryStrPool.Put(sb) queryStrPool.Put(sb)