add table field method

This commit is contained in:
chenghonour 2020-07-17 14:28:50 +08:00
parent 78536de1b5
commit 534cd3be1c

View File

@ -127,3 +127,24 @@ func (m *Model) FieldsExStr(fields string, prefix ...string) string {
newFields = m.db.QuoteString(newFields)
return newFields
}
// HasField determine whether the field exists in the table.
func (m *Model) HasField(field string) bool {
tableFields, err := m.db.TableFields(m.tables)
if err != nil {
panic(err)
}
if len(tableFields) == 0 {
panic(fmt.Sprintf(`empty table fields for table "%s"`, m.tables))
}
fieldsArray := make([]string, len(tableFields))
for k, v := range tableFields {
fieldsArray[v.Index] = k
}
for _, f := range fieldsArray {
if f == field {
return true
}
}
return false
}