This commit is contained in:
John Guo
2022-02-15 23:04:37 +08:00
parent f0b78253b2
commit 37c6320dd7
5 changed files with 22 additions and 13 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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