connection_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2016 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "database/sql/driver"
  11. "testing"
  12. )
  13. func TestInterpolateParams(t *testing.T) {
  14. mc := &mysqlConn{
  15. buf: newBuffer(nil),
  16. maxAllowedPacket: maxPacketSize,
  17. cfg: &Config{
  18. InterpolateParams: true,
  19. },
  20. }
  21. q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42), "gopher"})
  22. if err != nil {
  23. t.Errorf("Expected err=nil, got %#v", err)
  24. return
  25. }
  26. expected := `SELECT 42+'gopher'`
  27. if q != expected {
  28. t.Errorf("Expected: %q\nGot: %q", expected, q)
  29. }
  30. }
  31. func TestInterpolateParamsTooManyPlaceholders(t *testing.T) {
  32. mc := &mysqlConn{
  33. buf: newBuffer(nil),
  34. maxAllowedPacket: maxPacketSize,
  35. cfg: &Config{
  36. InterpolateParams: true,
  37. },
  38. }
  39. q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42)})
  40. if err != driver.ErrSkip {
  41. t.Errorf("Expected err=driver.ErrSkip, got err=%#v, q=%#v", err, q)
  42. }
  43. }
  44. // We don't support placeholder in string literal for now.
  45. // https://github.com/go-sql-driver/mysql/pull/490
  46. func TestInterpolateParamsPlaceholderInString(t *testing.T) {
  47. mc := &mysqlConn{
  48. buf: newBuffer(nil),
  49. maxAllowedPacket: maxPacketSize,
  50. cfg: &Config{
  51. InterpolateParams: true,
  52. },
  53. }
  54. q, err := mc.interpolateParams("SELECT 'abc?xyz',?", []driver.Value{int64(42)})
  55. // When InterpolateParams support string literal, this should return `"SELECT 'abc?xyz', 42`
  56. if err != driver.ErrSkip {
  57. t.Errorf("Expected err=driver.ErrSkip, got err=%#v, q=%#v", err, q)
  58. }
  59. }