简体   繁体   English

如何从 dbr golang 查询构建器中提取原始查询

[英]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.我是新来的golang dbr库( https://godoc.org/github.com/gocraft/dbr )和我没有找到关于如何使用这个库获得原始查询的信息。

I need something similar to get_compiled_select() from php igniter.我需要类似于 php 点火器中的get_compiled_select()东西。 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 IMO,更好的解决方案是在 select.go 中创建新的 getter 函数

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.您可以将 expr 中的原始结构设置为 public。

I hope it helps u.我希望它可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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