3

Go sql.Driver的mysql Driver 中的一个有意思的行为

 2 years ago
source link: https://www.imhanjm.com/2018/11/29/go%20sql.driver%E7%9A%84mysql%20driver%20%E4%B8%AD%E7%9A%84%E4%B8%80%E4%B8%AA%E6%9C%89%E6%84%8F%E6%80%9D%E7%9A%84%E8%A1%8C%E4%B8%BA/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Go sql.Driver的mysql Driver 中的一个有意思的行为

发表于 2018-11-29

| 0 Comments

如果参数中没有参数, 则直接query.
如果sql中有参数, 且打开了InterpolateParams开关, 那么就客户端直接拼参数到SQL里, 不需要prepare直接query.
如果sql中有参数, 且没有打开InterpolateParams(默认设置), 且带参数, 就会走先prepare再发query参数两步.

github.com/go-sql-driver/mysql/connection.go:370
func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) {
//...
if len(args) != 0 {
if !mc.cfg.InterpolateParams {
return nil, driver.ErrSkip
}
// try client-side prepare to reduce roundtrip
prepared, err := mc.interpolateParams(query, args)
if err != nil {
return nil, err
}
query = prepared
}
// Send command
err := mc.writeCommandPacketStr(comQuery, query)
if err == nil {
// Read Result
// ...

后续再补篇文章详细写写sql.Driver


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK