简体   繁体   中英

How to extract raw query from dbr golang query builder

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM