diff --git a/contrib/drivers/mssql/mssql.go b/contrib/drivers/mssql/mssql.go index c5ff3d6e3..6fdf45618 100644 --- a/contrib/drivers/mssql/mssql.go +++ b/contrib/drivers/mssql/mssql.go @@ -235,7 +235,7 @@ func (d *DriverMssql) Tables(ctx context.Context, schema ...string) (tables []st return } -// TableFields retrieves and returns the fields information of specified table of current schema. +// TableFields retrieves and returns the fields' information of specified table of current schema. // // Also see DriverMysql.TableFields. func (d *DriverMssql) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error) { diff --git a/database/gdb/gdb_core_structure.go b/database/gdb/gdb_core_structure.go index d9b03eb7e..09ec8ce15 100644 --- a/database/gdb/gdb_core_structure.go +++ b/database/gdb/gdb_core_structure.go @@ -218,7 +218,7 @@ func (c *Core) convertFieldValueToLocalValue(fieldValue interface{}, fieldType s // mappingAndFilterData automatically mappings the map key to table field and removes // all key-value pairs that are not the field of given table. func (c *Core) mappingAndFilterData(schema, table string, data map[string]interface{}, filter bool) (map[string]interface{}, error) { - fieldsMap, err := c.db.TableFields(c.GetCtx(), c.guessPrimaryTableName(table), schema) + fieldsMap, err := c.TableFields(c.guessPrimaryTableName(table), schema) if err != nil { return nil, err } diff --git a/database/gdb/gdb_core_utility.go b/database/gdb/gdb_core_utility.go index a8f555684..0e0ff4403 100644 --- a/database/gdb/gdb_core_utility.go +++ b/database/gdb/gdb_core_utility.go @@ -90,19 +90,23 @@ func (c *Core) Tables(schema ...string) (tables []string, err error) { // TableFields retrieves and returns the fields' information of specified table of current schema. // // Note that it returns a map containing the field name and its corresponding fields. -// As a map is unsorted, the TableField struct has a "Index" field marks its sequence in the fields. +// As a map is unsorted, the TableField struct has an "Index" field marks its sequence in the fields. // // It's using cache feature to enhance the performance, which is never expired util the process restarts. // // It does nothing in default. func (c *Core) TableFields(table string, schema ...string) (fields map[string]*TableField, err error) { - return + // It does nothing if given table is empty, especially in sub-query. + if table == "" { + return map[string]*TableField{}, nil + } + return c.db.TableFields(c.GetCtx(), table, schema...) } // HasField determine whether the field exists in the table. func (c *Core) HasField(table, field string, schema ...string) (bool, error) { table = c.guessPrimaryTableName(table) - tableFields, err := c.db.TableFields(c.GetCtx(), table, schema...) + tableFields, err := c.TableFields(table, schema...) if err != nil { return false, err } diff --git a/database/gdb/gdb_driver_mysql.go b/database/gdb/gdb_driver_mysql.go index e25f27172..66589d88e 100644 --- a/database/gdb/gdb_driver_mysql.go +++ b/database/gdb/gdb_driver_mysql.go @@ -128,7 +128,10 @@ func (d *DriverMysql) TableFields(ctx context.Context, table string, schema ...s charL, charR := d.GetChars() table = gstr.Trim(table, charL+charR) if gstr.Contains(table, " ") { - return nil, gerror.NewCode(gcode.CodeInvalidParameter, "function TableFields supports only single table operations") + return nil, gerror.NewCode( + gcode.CodeInvalidParameter, + "function TableFields supports only single table operations", + ) } useSchema := d.schema.Val() if len(schema) > 0 && schema[0] != "" { @@ -144,7 +147,10 @@ func (d *DriverMysql) TableFields(ctx context.Context, table string, schema ...s if link, err = d.SlaveLink(useSchema); err != nil { return nil } - result, err = d.DoGetAll(ctx, link, fmt.Sprintf(`SHOW FULL COLUMNS FROM %s`, d.QuoteWord(table))) + result, err = d.DoGetAll( + ctx, link, + fmt.Sprintf(`SHOW FULL COLUMNS FROM %s`, d.QuoteWord(table)), + ) if err != nil { return nil } diff --git a/database/gdb/gdb_model_utility.go b/database/gdb/gdb_model_utility.go index b04b7e7a5..2559217e8 100644 --- a/database/gdb/gdb_model_utility.go +++ b/database/gdb/gdb_model_utility.go @@ -31,15 +31,14 @@ func (m *Model) QuoteWord(s string) string { // // Also see DriverMysql.TableFields. func (m *Model) TableFields(tableStr string, schema ...string) (fields map[string]*TableField, err error) { - useSchema := m.schema + var ( + table = m.db.GetCore().guessPrimaryTableName(tableStr) + useSchema = m.schema + ) if len(schema) > 0 && schema[0] != "" { useSchema = schema[0] } - return m.db.TableFields( - m.GetCtx(), - m.db.GetCore().guessPrimaryTableName(tableStr), - useSchema, - ) + return m.db.GetCore().TableFields(table, useSchema) } // getModel creates and returns a cloned model of current model if `safe` is true, or else it returns