From 534cd3be1c934ae07b4c59cf6c747d2ee9c50b14 Mon Sep 17 00:00:00 2001 From: chenghonour Date: Fri, 17 Jul 2020 14:28:50 +0800 Subject: [PATCH] add table field method --- database/gdb/gdb_model_fields.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/database/gdb/gdb_model_fields.go b/database/gdb/gdb_model_fields.go index 580b57fc7..b051afba6 100644 --- a/database/gdb/gdb_model_fields.go +++ b/database/gdb/gdb_model_fields.go @@ -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 +} \ No newline at end of file