Comments (2)
It's very important, and how we can application IN statement that in SQL with JSON query?
from datatypes.
Your Question
Is there a way to build an SQL query that searches multiple-values (not multiple keys) in a JSON array?
SELECT * FROM table WHERE JSON_CONTAINS(obj->'$.array_of_ints', [1,2,3]); for: { "obj": {"array_of_ints":[1,2,3,4]} } the result will be TRUE.
Or:
SELECT * FROM table WHERE JSON_CONTAINS(obj->'$.array_of_ints', 2); for: { "obj":{"array_of_ints":[1,2,3,4]} } the result will be TRUE.
The document you expected this should be explained
https://gorm.io/docs/data_types.html
Expected answer
Would like to know the methods to generate a JSON_CONTAINS SQL query using SQLBuilder
// Build implements clause.Expression
func (json *JSONArrayExpression) Build(builder clause.Builder) {
if stmt, ok := builder.(*gorm.Statement); ok {
switch stmt.Dialector.Name() {
case "mysql":
builder.WriteString("JSON_CONTAINS (" + stmt.Quote(json.column) + ", JSON_ARRAY(")
builder.AddVar(stmt, json.equalsValue)
builder.WriteString("))")
}
}
}
datatypes.JSONArrayExpression
append a JSON_ARRAY('')
string in Build
method, and this does not support slice value, I finally found a solution to this problem.
package cdatatypes
import (
"gorm.io/gen/field"
"gorm.io/gorm/clause"
)
type ExprCond struct {
clause.Expr
field.String
}
func Cond(expr clause.Expr) *ExprCond {
return &ExprCond{
Expr: expr,
String: field.String{},
}
}
func (c *ExprCond) BeCond() interface{} { return c.Expr }
func (c *ExprCond) CondError() error { return nil }
Then use the code to support custom conditions. And this cond implements gen.Condition
interface
cdatatypes.Cond(gorm.Expr("JSON_CONTAINS (`tag`, ?)", tags))
from datatypes.
Related Issues (20)
- how to set json type? HOT 1
- Use civil.Date for date fields HOT 1
- Exclude Unused Imports
- JSONSet support for PostgreSQL
- The JSON_SET feature is already supported and merged, any plans to release a new version?
- 数字数组查询不对劲 HOT 2
- 支持JSON_REMOVE
- Why can't find the function JSONArrayQuery definition? HOT 8
- Found in json.go, but unable to use
- Need Tag for latest PR HOT 2
- JSONType field Data: define a valid foreign key for relations or implement the Valuer/Scanner HOT 4
- Custom enum type sql
- v1.0.7 没有JsonSet功能?
- Please add the JSONArray support for sqlite, thx.
- Is there a solution that allows me to use gob encode / decode with data containing `JsonType` ?
- Greater/Lesser function for JSONQuery
- Add
- Add Enum type support for Postgres
- datatypes.JSONSlice not found HOT 2
- 希望可以支持一下JSONQuery("key").NotEquals(...)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datatypes.