参见带参数的查询围棋数据库/ SQL包 [英] See query with arguments in Go database/sql package

查看:151
本文介绍了参见带参数的查询围棋数据库/ SQL包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试传递参数给sql.DB.Query方法(使用数据库/ SQL包的行为,并在的 github.com/lib/pq )。有没有什么办法让后它被处理,看参数是如何插入原始的查询字符串?我在想,比如,写一个prepared查询,然后检查所产生的语句。有任何想法吗?谢谢!

I'm trying to test the behavior of passing arguments to the sql.DB.Query method (using the database/sql package, and the PostgreSQL driver at github.com/lib/pq). Is there any way to get the raw query string after it's been processed to see how the arguments were inserted? I was thinking, for example, of writing a prepared query and then inspecting the resulting statement. Any ideas? Thanks!

推荐答案

它看起来并不像它在插入查询本身的参数。它发送该查询,然后等待响应于发送参数。我分叉PQ,并把一些日志记录code在 - 这是一个简单的查询的输出:

It doesn't look like it inserts parameters in the query itself. It sends the query, then waits for the response to send parameters. I forked pq and put some logging code in--this was the output for a simple query:

 wes-macbook:testpq go run testpq.go
2014/01/03 19:08:56 bytes: [0 0 0 84 0 3 0 0 101 120 116 114 97 95 102 108 111 97 116 95 100 105 103 105 116 115 0 50 0 99 108 105 101 110 116 95 101 110 99 111 100 105 110 103 0 85 84 70 56 0 100 97 116 101 115 116 121 108 101 0 73 83 79 44 32 77 68 89 0 117 115 101 114 0 119 102 114 101 101 109 97 110 0 0]
2014/01/03 19:08:56 string: Textra_float_digits2client_encodingUTF8datestyleISO, MDYuserwfreeman
2014/01/03 19:08:56 bytes: [80 0 0 0 45 0 115 101 108 101 99 116 32 42 32 102 114 111 109 32 116 101 115 116 32 119 104 101 114 101 32 116 101 115 116 46 110 97 109 101 61 36 49 0 0 0]
2014/01/03 19:08:56 string: P-select * from test where test.name=$1
2014/01/03 19:08:56 bytes: [68 0 0 0 6 83 0]
2014/01/03 19:08:56 string: DS
2014/01/03 19:08:56 bytes: [83 0 0 0 4]
2014/01/03 19:08:56 string: S
2014/01/03 19:08:56 bytes: [66 0 0 0 20 0 0 0 0 0 1 0 0 0 4 97 115 100 102 0 0]
2014/01/03 19:08:56 string: Basdf
2014/01/03 19:08:56 bytes: [69 0 0 0 9 0 0 0 0 0]
2014/01/03 19:08:56 string: E
2014/01/03 19:08:56 bytes: [83 0 0 0 4]
2014/01/03 19:08:56 string: S

我所做的只是调整在send()code:

All I did was tweak the send() code:

func (cn *conn) send(m *writeBuf) {
   b := (*m)[1:]
   binary.BigEndian.PutUint32(b, uint32(len(b)))

   if (*m)[0] == 0 {
      *m = b
   }

   // new code here
   log.Println("bytes:", *m)
   log.Println("string:", string(*m))

   _, err := cn.c.Write(*m)
   if err != nil {
      panic(err)
   }
}

我的示例code,虽然它的pretty简单:
https://gist.github.com/8249430

这篇关于参见带参数的查询围棋数据库/ SQL包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆