美文网首页
goframe框架查询报错field字段显示为驼峰导致查询报错

goframe框架查询报错field字段显示为驼峰导致查询报错

作者: 鸿雁长飞光不度 | 来源:发表于2023-04-03 14:59 被阅读0次

    goframe线上发现sql报查询的字段不存在,发现是驼峰形式的,主要是下面的代码,去schema获取表信息的时候没有获取到,重启了一下服务就好了。

    / mappingAndFilterToTableFields mappings and changes given field name to really table field name.
    // Eg:
    // ID        -> id
    // NICK_Name -> nickname.
    func (m *Model) mappingAndFilterToTableFields(fields []string, filter bool) []string {
        fieldsMap, _ := m.TableFields(m.tablesInit)
        if len(fieldsMap) == 0 {
            return fields
        }
        var (
            inputFieldsArray  = gstr.SplitAndTrim(gstr.Join(fields, ","), ",")
            outputFieldsArray = make([]string, 0, len(inputFieldsArray))
        )
        fieldsKeyMap := make(map[string]interface{}, len(fieldsMap))
        for k := range fieldsMap {
            fieldsKeyMap[k] = nil
        }
        for _, field := range inputFieldsArray {
            if _, ok := fieldsKeyMap[field]; !ok {
                if !gregex.IsMatchString(regularFieldNameWithoutDotRegPattern, field) {
                    // Eg: user.id, user.name
                    outputFieldsArray = append(outputFieldsArray, field)
                    continue
                } else {
                    // Eg: id, name
                    if foundKey, _ := gutil.MapPossibleItemByKey(fieldsKeyMap, field); foundKey != "" {
                        outputFieldsArray = append(outputFieldsArray, foundKey)
                    } else if !filter {
                        outputFieldsArray = append(outputFieldsArray, field)
                    }
                }
            } else {
                outputFieldsArray = append(outputFieldsArray, field)
            }
        }
        return outputFieldsArray
    }
    
    
    // TableFields retrieves and returns the fields' information of specified table of current schema.
    //
    // Also see DriverMysql.TableFields.
    func (d *Driver) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error) {
        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",
            )
        }
        table, _ = gregex.ReplaceString("\"", "", table)
        useSchema := d.GetSchema()
        if len(schema) > 0 && schema[0] != "" {
            useSchema = schema[0]
        }
        v := tableFieldsMap.GetOrSetFuncLock(
            fmt.Sprintf(`pgsql_table_fields_%s_%s@group:%s`, table, useSchema, d.GetGroup()),
            func() interface{} {
                var (
                    result       gdb.Result
                    link         gdb.Link
                    structureSql = fmt.Sprintf(`
    SELECT a.attname AS field, t.typname AS type,a.attnotnull as null,
        (case when d.contype is not null then 'pri' else '' end)  as key
          ,ic.column_default as default_value,b.description as comment
          ,coalesce(character_maximum_length, numeric_precision, -1) as length
          ,numeric_scale as scale
    FROM pg_attribute a
             left join pg_class c on a.attrelid = c.oid
             left join pg_constraint d on d.conrelid = c.oid and a.attnum = d.conkey[1]
             left join pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid
             left join  pg_type t ON  a.atttypid = t.oid
             left join information_schema.columns ic on ic.column_name = a.attname and ic.table_name = c.relname
    WHERE c.relname = '%s' and a.attisdropped is false and a.attnum > 0
    ORDER BY a.attnum`,
                        table,
                    )
                )
                if link, err = d.SlaveLink(useSchema); err != nil {
                    return nil
                }
                structureSql, _ = gregex.ReplaceString(`[\n\r\s]+`, " ", gstr.Trim(structureSql))
                result, err = d.DoGetAll(ctx, link, structureSql)
                if err != nil {
                    return nil
                }
                fields = make(map[string]*gdb.TableField)
                for i, m := range result {
                    fields[m["field"].String()] = &gdb.TableField{
                        Index:   i,
                        Name:    m["field"].String(),
                        Type:    m["type"].String(),
                        Null:    m["null"].Bool(),
                        Key:     m["key"].String(),
                        Default: m["default_value"].Val(),
                        Comment: m["comment"].String(),
                    }
                }
                return fields
            },
        )
        if v != nil {
            fields = v.(map[string]*gdb.TableField)
        }
        return
    }
    

    相关文章

      网友评论

          本文标题:goframe框架查询报错field字段显示为驼峰导致查询报错

          本文链接:https://www.haomeiwen.com/subject/tbmcddtx.html