How to extract raw query from dbr golang query builder

Viewed 2082

I'm new to the golang dbr library (https://godoc.org/github.com/gocraft/dbr) and I did not find an information about how to get a raw query using this library.

I need something similar to get_compiled_select() from php igniter. I need it to combine multiple complex queries with union.

3 Answers

The following will dump the query...

stmt := session.Select("*").From(table).Where("id = ?", ...)

buf := dbr.NewBuffer()
_ = stmt.Build(stmt.Dialect, buf)
fmt.Println(buf.String())

// print the interpolated values
for _, v := range stmt.WhereCond {
    fmt.Println(v)
}

Note that the output will not include the interpolated values.

I'm not so sure previous answer (setting the struct as public) is the wise solution, even if that's works.

IMO, better solution would be creating new getter function inside select.go

func (sel *SelectStmt) GetRaw() string {
return sel.raw.Query 
}

With this method, it should be easier to maintain.

u can set raw struct from expr as public.

I hope it helps u.

Related