Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gdb/gdb_core: issue title When cross-database query is not done using Schema, the corresponding table structure fields cannot be obtained correctly; if you want to add a parameter, you can directly use data as the table field. #3879

Open
yimuysl001 opened this issue Oct 22, 2024 · 4 comments

Comments

@yimuysl001
Copy link

Description

不使用Schema 跨库查询时(使用事务时无法正确切换到相应的库),无法正确获取到相应的表结构字段;希望添加一个参数,可以直接使用data作为表字段

	_, err := g.DB("local").Model("BAK..TBA").Data(map[string]interface{}{
		"id":   2,
		"name": "aaa",
	}).Insert()
	fmt.Println(err) //报错 The table "BAK".."TBA" may not exist, or the table contains no fields
	_, err = g.DB("local").Schema("BAK").Model("TBA").Data(map[string]interface{}{
		"id":   2,
		"name": "aaa",
	}).Insert()
	fmt.Println(err) //处理成功

Additional

func (c *Core) mappingAndFilterData(ctx context.Context, schema, table string, data map[string]interface{}, filter bool) (map[string]interface{}, error) {
    
    // 是否可以直接从table中把 schema 拆出来,
	fieldsMap, err := c.db.TableFields(ctx, c.guessPrimaryTableName(table), schema)
	if err != nil {
		return nil, err
	}
   
 
	if len(fieldsMap) == 0 {
         // 或者没查到数据使用data的key ,而不是直接报错
        
		return nil, gerror.Newf(`The table %s may not exist, or the table contains no fields`, table)
	}
	fieldsKeyMap := make(map[string]interface{}, len(fieldsMap))
	for k := range fieldsMap {
		fieldsKeyMap[k] = nil
	}
	// Automatic data key to table field name mapping.
	var foundKey string
	for dataKey, dataValue := range data {
		if _, ok := fieldsKeyMap[dataKey]; !ok {
			foundKey, _ = gutil.MapPossibleItemByKey(fieldsKeyMap, dataKey)
			if foundKey != "" {
				if _, ok = data[foundKey]; !ok {
					data[foundKey] = dataValue
				}
				delete(data, dataKey)
			}
		}
	}
	// Data filtering.
	// It deletes all key-value pairs that has incorrect field name.
	if filter {
		for dataKey := range data {
			if _, ok := fieldsMap[dataKey]; !ok {
				delete(data, dataKey)
			}
		}
		if len(data) == 0 {
			return nil, gerror.Newf(`input data match no fields in table %s`, table)
		}
	}
	return data, nil
}



func (c *Core) fieldsToSequence(ctx context.Context, table string, fields []string) ([]string, error) {
	var (
		fieldSet               = gset.NewStrSetFrom(fields)
		fieldsResultInSequence = make([]string, 0)
		tableFields, err       = c.db.TableFields(ctx, table)
	)
	if err != nil {
		return nil, err
	}
     /****/
    // todo 希望如果没查询到字段,能返回fields
    if len(tableFields)<1 {
        return fields,nil
    }
    /****/
	// Sort the fields in order.
	var fieldsOfTableInSequence = make([]string, len(tableFields))
	for _, field := range tableFields {
		fieldsOfTableInSequence[field.Index] = field.Name
	}
	// Sort the input fields.
	for _, fieldName := range fieldsOfTableInSequence {
		if fieldSet.Contains(fieldName) {
			fieldsResultInSequence = append(fieldsResultInSequence, fieldName)
		}
	}
	return fieldsResultInSequence, nil
}


@Issues-translate-bot Issues-translate-bot changed the title gdb/gdb_core: issue title 不使用Schema 跨库查询时,无法正确获取到相应的表结构字段;希望添加一个参数,可以直接使用data作为表字段 gdb/gdb_core: issue title When cross-database query is not done using Schema, the corresponding table structure fields cannot be obtained correctly; if you want to add a parameter, you can directly use data as the table field. Oct 22, 2024
@gqcn
Copy link
Member

gqcn commented Oct 24, 2024

@yimuysl001 您好,我们很喜欢你的建议,麻烦提交一下完整的最小可运行代码呢,如果评估可以开发,我们可以拿来作为单测验证。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@yimuysl001 Hello, we like your suggestion very much. Please submit the complete minimum runnable code. If the evaluation can be developed, we can use it as a single test verification.

@yimuysl001
Copy link
Author

@gqcn 已提交pr

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@gqcn has submitted PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants