This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
@@ -218,6 +219,31 @@ func (t Table) GetDeleteDML(count int) (string, error) {
|
||||
return dml.String(), nil
|
||||
}
|
||||
|
||||
func (t Table) GetOrderByDML(ordering ...OrderBy) (string, error) {
|
||||
var dml strings.Builder
|
||||
dml.WriteString(" ORDER BY")
|
||||
for _, orderBy := range ordering {
|
||||
col, err := t.getColumnByField(orderBy.Field)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dml.WriteString(" ")
|
||||
dml.WriteString(col.Name)
|
||||
dml.WriteString(" ")
|
||||
dml.WriteString(orderBy.Direction)
|
||||
}
|
||||
return dml.String(), nil
|
||||
}
|
||||
|
||||
func (t Table) getColumnByField(fieldName string) (Column, error) {
|
||||
for _, col := range t.columns {
|
||||
if col.FieldName == fieldName {
|
||||
return col, nil
|
||||
}
|
||||
}
|
||||
return Column{}, errors.New("No column for field " + fieldName + " in table " + t.name)
|
||||
}
|
||||
|
||||
func (t Table) IsPkAuto() bool {
|
||||
for _, constr := range t.constraints {
|
||||
if constr.Type != "pk" {
|
||||
|
||||
Reference in New Issue
Block a user