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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ type JSONArrayExpression struct {
column string
keys []string
equalsValue interface{}
// Add a new field to indicate whether to perform a length comparison
length bool
// Add a new field to store the expected length value
lengthValue int
}

// Contains checks if column[keys] contains the value given. The keys parameter is only supported for MySQL and SQLite.
Expand All @@ -475,6 +479,13 @@ func (json *JSONArrayExpression) In(value interface{}, keys ...string) *JSONArra
return json
}

// Length checks if the length of the JSON array matches the given value.
func (json *JSONArrayExpression) Length(value int) *JSONArrayExpression {
json.length = true
json.lengthValue = value
return json
}

// Build implements clause.Expression
func (json *JSONArrayExpression) Build(builder clause.Builder) {
if stmt, ok := builder.(*gorm.Statement); ok {
Expand Down Expand Up @@ -504,6 +515,10 @@ func (json *JSONArrayExpression) Build(builder clause.Builder) {
builder.WriteByte(')')
}
builder.WriteByte(')')
// Add new logic to handle length comparison
case json.length:
builder.WriteString("JSON_LENGTH(" + stmt.Quote(json.column) + ") = ")
builder.AddVar(stmt, json.lengthValue)
}
case "sqlite":
switch {
Expand Down Expand Up @@ -551,13 +566,19 @@ func (json *JSONArrayExpression) Build(builder clause.Builder) {
builder.WriteString(" IN ")
builder.AddVar(stmt, json.equalsValue)
builder.WriteString(" END")
case json.length:
builder.WriteString("json_array_length(" + stmt.Quote(json.column) + ") = ")
builder.AddVar(stmt, json.lengthValue)
}
case "postgres":
switch {
case json.contains:
builder.WriteString(stmt.Quote(json.column))
builder.WriteString(" ? ")
builder.AddVar(stmt, json.equalsValue)
case json.length:
builder.WriteString("array_length(" + stmt.Quote(json.column) + "::jsonb::text[], 1) = ")
builder.AddVar(stmt, json.lengthValue)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,5 +523,20 @@ func TestJSONArrayQuery(t *testing.T) {
t.Fatalf("failed to find params with json value and keys, got error %v", err)
}
AssertEqual(t, len(retMultiple), 1)

// 新增的长度测试用例
var retLength []Param
if err := DB.Where(datatypes.JSONArrayQuery("config").Length(2)).Find(&retLength).Error; err != nil {
t.Fatalf("failed to find params with json array length, got error %v", err)
}
AssertEqual(t, len(retLength), 1)
AssertEqual(t, retLength[0].DisplayName, cmp1.DisplayName)

// 测试嵌套数组的长度
if err := DB.Where(datatypes.JSONArrayQuery("config").Length(2).Contains("a", "test")).Find(&retLength).Error; err != nil {
t.Fatalf("failed to find params with json array length and contains, got error %v", err)
}
AssertEqual(t, len(retLength), 1)
AssertEqual(t, retLength[0].DisplayName, cmp3.DisplayName)
}
}
Loading