connection_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }
  60. func TestCheckNamedValue(t *testing.T) {
  61. value := driver.NamedValue{Value: ^uint64(0)}
  62. x := &mysqlConn{}
  63. err := x.CheckNamedValue(&value)
  64. if err != nil {
  65. t.Fatal("uint64 high-bit not convertible", err)
  66. }
  67. if value.Value != "18446744073709551615" {
  68. t.Fatalf("uint64 high-bit not converted, got %#v %T", value.Value, value.Value)
  69. }
  70. }