example
sql: SELECT id, name, email FROM user WHERE id = ?;
package main
import (
"fmt"
"github.com/aacanakin/qb"
)
func main() {
db, err := qb.New("postgres", "user=postgres dbname=qb_test sslmode=disable")
if err != nil {
panic(err)
}
defer db.Close()
statement := qb.Statement()
sql := `
SELECT id, name, email
FROM user
WHERE id = ?
`
statement.Text(sql)
fmt.Println(statement.SQL())
// execute the select
var id int
var name, email string
db.Engine().DB().QueryRow(statement.SQL(), 5).Scan(&id, &name, &email)
}
这里故意写了很糟糕缩进的 sql 语句
Here, you can build the same sql without using Statement() call. However, if you don't use Statement() constructor, your query will have \t \n characters that is not needed. Moreover, Statement() checks if you added ";" character and ensures the sql has ";". You can put any tabs at the start of sql statement. Statement().Text() would clean them for you.
在这里,您可以在不使用Statement()调用的情况下构建相同的sql。但是,如果您不使用Statement()构造函数,您的查询将具有不需要的\t \n个字符。此外,Statement()检查是否添加了“;”字符,并确保sql具有“;”。您可以将任何制表符放在sql语句的开头。text()将为您清除它们