SQL Where like safe filter expression for entql and gorm
- use reflect to get model graph
- support join relation
package main
import "gorm.io/gorm"
func TestMiniQuery() {
var db *gorm.DB
// build to username = ? and Profile__age > ?
db.Model(User{}).Scopes(ApplyMiniQuery(`username="wener" && Profile.age > 18`)).Rows()
}
- generate entsql - recommend
- depends on generated graph - will validate when build
- support edge filter
- generate entql
- function is not as complete as entsql
- do not depends on generated graph - validate when entql to sql
package main
import "entgo.io/ent/dialect/sql"
func TestEntSQL() {
var s *sql.Selector
s.Where(sql.P(func(builder *sql.Builder) {
b := &entmq.MiniQLToEntSQLBuilder{
Node: node, // *sqlgraph.Node
Graph: schemaGraph, // *sqlgraph.Schema
QueryString: query,
}
builder.Join(b)
err := b.Err()
if err != nil {
panic(errors.Wrap(err, "invalid query"))
}
}))
}