Skip to content

Add a new field to indicate whether to perform a length comparison #291

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Arty0628
Copy link

@Arty0628 Arty0628 commented May 7, 2025

  • [ yes ] Do only one thing
  • [ yes ] Non breaking API changes
  • [ yes ] Tested

What did this pull request do?

This PR adds a length function to the JSONArrayQuery feature, enabling the comparison of the length of JSON array fields. Specifically, it adds length - related fields to the JSONArrayExpression struct, a new Length method for setting comparison conditions, and implements the construction logic of length comparison SQL statements for MySQL, SQLite, and PostgreSQL databases in the Build method. This resolves the issue that the original code could not verify the equality of JSON array lengths

User Case Description

In the business scenario, data query and creation operations are required based on conditions involving JSON array - type fields. For example, in the DoGetOrCreateStrategyKey interface, data is queried and created according to the incoming StrategyIds array. The original JSONArrayQuery only supports verifying the containment of array elements and cannot verify whether the array lengths are the same. The newly added length function ensures that during querying, existing data is only returned when both the length and content of the JSON array match. When the lengths are the same but the contents are different, it is treated as new data for creation, meeting more precise business query requirements.

Specific example scenario

You can only verify that it belongs to, not that it is equal to, for example, this interface:

func DoGetOrCreateStrategyKey(ctx context.Context, request GetOrCreateStrategyKeyRequest) (*po.key, error) {
	q := query.Use(db.DB).mydb
	clauses := []gen.Condition{q.Source.Eq(request.GetSource()), q.KeyType.Eq(int32(request.GetKeyType()))}
	var attrs []field.AssignExpr
	switch request.GetKeyType() {
	case factory_delivery.StrategyKeyType_SingleStrategy, factory_delivery.StrategyKeyType_MultiStrategy:
		if len(request.StrategyIds) > 5 {
			logs.CtxWarn(ctx, "too many strategy ids")
		}
		for _, sid := range request.StrategyIds {
			clauses = append(clauses, gen.Cond(datatypes.JSONArrayQuery(q.StrategyIds.ColumnName().String()).Contains(sid))...)
		}
		result, err := json.JsonIns.Marshal(request.StrategyIds)
		if err != nil {
			logs.CtxError(ctx, "[GetOrCreateStrategyKey] marshal failed, err: %v", err)
			return nil, bizerr.NewSystemErr("can not marshal data")
		}
		attrs = append(attrs, q.StrategyIds.Value(string(result)))
	default:
		return nil, bizerr.NewParamErr("invalid strategy_key_type")
	}

	do := q.WithContext(ctx).Where(clauses...)
	if request.GetExtra() != "" {
		attrs = append(attrs, q.Extra.Value(request.GetExtra()))
	}
	model, err := do.FirstOrCreate()
	return model, nil
}

Two requests are passed in separately:

{
    "Source": "TEST",
    "StrategyIds": [
        112141551,
        888888888,
        999999988
    ]
}
{
    "Source": "TEST",
    "StrategyIds": [
        112141551,
        888888888
    ]
}

The second request was supposed to create a new piece of data, but it returned the data created by the first request.

After adding the length attribute, you can compare with one click, which is more convenient

@Arty0628
Copy link
Author

Arty0628 commented May 7, 2025

Add a new field to indicate whether to perform a length comparison

@jinzhu
Copy link
Member

jinzhu commented May 25, 2025

Hi @Arty0628

Can you add some tests for this?

@Arty0628
Copy link
Author

你好@Arty0628

您可以添加一些测试吗?

@jinzhu Of course, I have pushed the test code, please check it out

zhouyuhan.888 added 2 commits May 27, 2025 11:26
@jinzhu
Copy link
Member

jinzhu commented Jun 9, 2025

please fix those tests, thank you.

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

Successfully merging this pull request may close these issues.

2 participants