0

I want to Perform LIKE operation in MYSQL using SQL boiler and golang

I am using

github.com/volatiletech/sqlboiler/v4/queries/qm

.

clause = qm.Where(fmt.Sprintf("post.deleted_at is null"))
    queryMods := []qm.QueryMod{
        clause,
        qm.Offset(gpi.Offset),
        qm.Limit(gpi.Limit),
        orderByMod,
        qm.Load(dbmodels.PostRels.ImpartWealth), // the user who posted
    }
    if searchKey != "" {
    where := fmt.Sprintf(`user on user.impart_wealth_id=post.impart_wealth_id 
and user.screen_name like ? or user.email like ? `)
queryMods = append(queryMods, qm.InnerJoin(where, "%"+searchKey +"%", "%"+searchKey +"%"))
    }
    posts, err := dbmodels.Posts(queryMods...).All(ctx, m.db)



[]qm.QueryMod{qmhelper.WhereQueryMod{Clause:"post.deleted_at is null",
 Args:[]interface {}(nil)}, 
qm.offsetQueryMod{offset:0},
 qm.limitQueryMod{limit:1}, 
qm.orderByQueryMod{clause:"created_at desc, post_id desc"},
qm.loadQueryMod{relationship:"ImpartWealth", mods:[]qm.QueryMod(nil)}, 
qm.innerJoinQueryMod{clause:"user on user.impart_wealth_id=post.impart_wealth_id \n\t\tand user.screen_name like ? or user.email like ? ",
 args:[]interface {}{"%j%", "%j%"}}}  





  

Here Like is not working.

Data is getting, but like operation not working, that is not getting the data that filter using the email or screenname.

filtering not working

Mia Mia
  • 143
  • 12
  • Are you sure the panic's caused by `like`? A panic in Go usually outputs a stacktrace, can you include that in your question? – mkopriva Jul 21 '21 at 15:12
  • @mkopriva , I updated the error.. I am new to this .. its working if I remove the code inside if searchKey != "" this – Mia Mia Jul 21 '21 at 15:19
  • Unfortunately that's not the stacktrace, either you have omitted it, or the code that prints the error that you have included discarded the stacktrace. Without it will be difficult to help you. – mkopriva Jul 21 '21 at 15:22
  • Here's an example of a stacktrace: https://play.golang.org/p/ol8ECz_dYQR and notice how it points out, correctly, that the error is on line 15. (`/tmp/sandbox375163025/prog.go:15`) – mkopriva Jul 21 '21 at 15:25
  • @mkopriva updated the data that getting after the error – Mia Mia Jul 21 '21 at 15:30
  • 1
    show `fmt.Printf("%#v\n",queryMods)`, i think something is nil inside. –  Jul 21 '21 at 15:33
  • Can you show the code for the function `github.com/impartwealthapp/backend/pkg/models/dbmodels.NewQuery`? It seems like it is calling `qm.Apply` which is then causing the panic, so it's highly likely that `NewQuery` is passing bad arguments to `Apply`. – mkopriva Jul 21 '21 at 15:33
  • @MiaMia mh-cbon is correct [this line](https://github.com/volatiletech/sqlboiler/blob/475493476401b3fd8fd0801eece058e514d284d7/queries/qm/query_mods.go#L41) will panic only if one of the mods is `nil`. However [`qm.WhereIn`](https://github.com/volatiletech/sqlboiler/blob/475493476401b3fd8fd0801eece058e514d284d7/queries/qm/query_mods.go#L309-L314) does not, under any circumstance, return `nil`. Can you show `NewQuery`? Does it, or anything in-between, modify the `queryMods` slice before passing it to `Apply`? Somewhere something must be appending `nil`. – mkopriva Jul 21 '21 at 15:40
  • @mkopriva func NewQuery(mods ...qm.QueryMod) *queries.Query { q := &queries.Query{} queries.SetDialect(q, &dialect) qm.Apply(q, mods...) return q } – Mia Mia Jul 21 '21 at 15:49
  • @mh-cbon []qm.QueryMod{qm.QueryMod(nil), qm.offsetQueryMod{offset:0}, qm.limitQueryMod{limit:1}, qm.orderByQueryMod{clause:"created_at desc, post_id desc"}, qm.loadQueryMod{relationship:"ImpartWealth", mods:[]qm.QueryMod(nil)}, qm.whereInQueryMod{clause:"exists (\n\t\t\tselect * from ImpartWealth where ImpartWealth.impart_wealth_id = post.impart_wealth_id\n\t\t\tand ImpartWealth.screen_name like ? or ImpartWealth.email like ?) ", args:[]interface {}{"%j%", "%j%"}}} – Mia Mia Jul 21 '21 at 15:49
  • 1
    @MiaMia your first mod is `nil`, the `clause` it seems like. – mkopriva Jul 21 '21 at 15:51
  • 1
    @MiaMia also do not post extensive code snippets into comments, they are very difficult to read. You can edit the question as many time as you want by adding details and proper syntax highliting. – mkopriva Jul 21 '21 at 15:52
  • @mkopriva ok, will not repeat again I will check the clause – Mia Mia Jul 21 '21 at 15:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235152/discussion-between-mia-mia-and-mkopriva). – Mia Mia Jul 21 '21 at 16:20

1 Answers1

0

I got the Answer.

if gpi.SearchKey != "" {
        where := fmt.Sprintf(`user on user.impart_wealth_id=post.impart_wealth_id 
        and (user.screen_name like ? or user.email like ? ) `)
        queryMods = append(queryMods, qm.InnerJoin(where, "%"+gpi.SearchKey+"%", "%"+gpi.SearchKey+"%"))
    }
Mia Mia
  • 143
  • 12