driver_go18_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2017 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. // +build go1.8
  9. package mysql
  10. import (
  11. "context"
  12. "database/sql"
  13. "database/sql/driver"
  14. "fmt"
  15. "math"
  16. "reflect"
  17. "testing"
  18. "time"
  19. )
  20. // static interface implementation checks of mysqlConn
  21. var (
  22. _ driver.ConnBeginTx = &mysqlConn{}
  23. _ driver.ConnPrepareContext = &mysqlConn{}
  24. _ driver.ExecerContext = &mysqlConn{}
  25. _ driver.Pinger = &mysqlConn{}
  26. _ driver.QueryerContext = &mysqlConn{}
  27. )
  28. // static interface implementation checks of mysqlStmt
  29. var (
  30. _ driver.StmtExecContext = &mysqlStmt{}
  31. _ driver.StmtQueryContext = &mysqlStmt{}
  32. )
  33. // Ensure that all the driver interfaces are implemented
  34. var (
  35. // _ driver.RowsColumnTypeLength = &binaryRows{}
  36. // _ driver.RowsColumnTypeLength = &textRows{}
  37. _ driver.RowsColumnTypeDatabaseTypeName = &binaryRows{}
  38. _ driver.RowsColumnTypeDatabaseTypeName = &textRows{}
  39. _ driver.RowsColumnTypeNullable = &binaryRows{}
  40. _ driver.RowsColumnTypeNullable = &textRows{}
  41. _ driver.RowsColumnTypePrecisionScale = &binaryRows{}
  42. _ driver.RowsColumnTypePrecisionScale = &textRows{}
  43. _ driver.RowsColumnTypeScanType = &binaryRows{}
  44. _ driver.RowsColumnTypeScanType = &textRows{}
  45. _ driver.RowsNextResultSet = &binaryRows{}
  46. _ driver.RowsNextResultSet = &textRows{}
  47. )
  48. func TestMultiResultSet(t *testing.T) {
  49. type result struct {
  50. values [][]int
  51. columns []string
  52. }
  53. // checkRows is a helper test function to validate rows containing 3 result
  54. // sets with specific values and columns. The basic query would look like this:
  55. //
  56. // SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4;
  57. // SELECT 0 UNION SELECT 1;
  58. // SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;
  59. //
  60. // to distinguish test cases the first string argument is put in front of
  61. // every error or fatal message.
  62. checkRows := func(desc string, rows *sql.Rows, dbt *DBTest) {
  63. expected := []result{
  64. {
  65. values: [][]int{{1, 2}, {3, 4}},
  66. columns: []string{"col1", "col2"},
  67. },
  68. {
  69. values: [][]int{{1, 2, 3}, {4, 5, 6}},
  70. columns: []string{"col1", "col2", "col3"},
  71. },
  72. }
  73. var res1 result
  74. for rows.Next() {
  75. var res [2]int
  76. if err := rows.Scan(&res[0], &res[1]); err != nil {
  77. dbt.Fatal(err)
  78. }
  79. res1.values = append(res1.values, res[:])
  80. }
  81. cols, err := rows.Columns()
  82. if err != nil {
  83. dbt.Fatal(desc, err)
  84. }
  85. res1.columns = cols
  86. if !reflect.DeepEqual(expected[0], res1) {
  87. dbt.Error(desc, "want =", expected[0], "got =", res1)
  88. }
  89. if !rows.NextResultSet() {
  90. dbt.Fatal(desc, "expected next result set")
  91. }
  92. // ignoring one result set
  93. if !rows.NextResultSet() {
  94. dbt.Fatal(desc, "expected next result set")
  95. }
  96. var res2 result
  97. cols, err = rows.Columns()
  98. if err != nil {
  99. dbt.Fatal(desc, err)
  100. }
  101. res2.columns = cols
  102. for rows.Next() {
  103. var res [3]int
  104. if err := rows.Scan(&res[0], &res[1], &res[2]); err != nil {
  105. dbt.Fatal(desc, err)
  106. }
  107. res2.values = append(res2.values, res[:])
  108. }
  109. if !reflect.DeepEqual(expected[1], res2) {
  110. dbt.Error(desc, "want =", expected[1], "got =", res2)
  111. }
  112. if rows.NextResultSet() {
  113. dbt.Error(desc, "unexpected next result set")
  114. }
  115. if err := rows.Err(); err != nil {
  116. dbt.Error(desc, err)
  117. }
  118. }
  119. runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) {
  120. rows := dbt.mustQuery(`DO 1;
  121. SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4;
  122. DO 1;
  123. SELECT 0 UNION SELECT 1;
  124. SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;`)
  125. defer rows.Close()
  126. checkRows("query: ", rows, dbt)
  127. })
  128. runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) {
  129. queries := []string{
  130. `
  131. DROP PROCEDURE IF EXISTS test_mrss;
  132. CREATE PROCEDURE test_mrss()
  133. BEGIN
  134. DO 1;
  135. SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4;
  136. DO 1;
  137. SELECT 0 UNION SELECT 1;
  138. SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;
  139. END
  140. `,
  141. `
  142. DROP PROCEDURE IF EXISTS test_mrss;
  143. CREATE PROCEDURE test_mrss()
  144. BEGIN
  145. SELECT 1 AS col1, 2 AS col2 UNION SELECT 3, 4;
  146. SELECT 0 UNION SELECT 1;
  147. SELECT 1 AS col1, 2 AS col2, 3 AS col3 UNION SELECT 4, 5, 6;
  148. END
  149. `,
  150. }
  151. defer dbt.mustExec("DROP PROCEDURE IF EXISTS test_mrss")
  152. for i, query := range queries {
  153. dbt.mustExec(query)
  154. stmt, err := dbt.db.Prepare("CALL test_mrss()")
  155. if err != nil {
  156. dbt.Fatalf("%v (i=%d)", err, i)
  157. }
  158. defer stmt.Close()
  159. for j := 0; j < 2; j++ {
  160. rows, err := stmt.Query()
  161. if err != nil {
  162. dbt.Fatalf("%v (i=%d) (j=%d)", err, i, j)
  163. }
  164. checkRows(fmt.Sprintf("prepared stmt query (i=%d) (j=%d): ", i, j), rows, dbt)
  165. }
  166. }
  167. })
  168. }
  169. func TestMultiResultSetNoSelect(t *testing.T) {
  170. runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) {
  171. rows := dbt.mustQuery("DO 1; DO 2;")
  172. defer rows.Close()
  173. if rows.Next() {
  174. dbt.Error("unexpected row")
  175. }
  176. if rows.NextResultSet() {
  177. dbt.Error("unexpected next result set")
  178. }
  179. if err := rows.Err(); err != nil {
  180. dbt.Error("expected nil; got ", err)
  181. }
  182. })
  183. }
  184. // tests if rows are set in a proper state if some results were ignored before
  185. // calling rows.NextResultSet.
  186. func TestSkipResults(t *testing.T) {
  187. runTests(t, dsn, func(dbt *DBTest) {
  188. rows := dbt.mustQuery("SELECT 1, 2")
  189. defer rows.Close()
  190. if !rows.Next() {
  191. dbt.Error("expected row")
  192. }
  193. if rows.NextResultSet() {
  194. dbt.Error("unexpected next result set")
  195. }
  196. if err := rows.Err(); err != nil {
  197. dbt.Error("expected nil; got ", err)
  198. }
  199. })
  200. }
  201. func TestPingContext(t *testing.T) {
  202. runTests(t, dsn, func(dbt *DBTest) {
  203. ctx, cancel := context.WithCancel(context.Background())
  204. cancel()
  205. if err := dbt.db.PingContext(ctx); err != context.Canceled {
  206. dbt.Errorf("expected context.Canceled, got %v", err)
  207. }
  208. })
  209. }
  210. func TestContextCancelExec(t *testing.T) {
  211. runTests(t, dsn, func(dbt *DBTest) {
  212. dbt.mustExec("CREATE TABLE test (v INTEGER)")
  213. ctx, cancel := context.WithCancel(context.Background())
  214. // Delay execution for just a bit until db.ExecContext has begun.
  215. defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
  216. // This query will be canceled.
  217. startTime := time.Now()
  218. if _, err := dbt.db.ExecContext(ctx, "INSERT INTO test VALUES (SLEEP(1))"); err != context.Canceled {
  219. dbt.Errorf("expected context.Canceled, got %v", err)
  220. }
  221. if d := time.Since(startTime); d > 500*time.Millisecond {
  222. dbt.Errorf("too long execution time: %s", d)
  223. }
  224. // Wait for the INSERT query has done.
  225. time.Sleep(time.Second)
  226. // Check how many times the query is executed.
  227. var v int
  228. if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
  229. dbt.Fatalf("%s", err.Error())
  230. }
  231. if v != 1 { // TODO: need to kill the query, and v should be 0.
  232. dbt.Errorf("expected val to be 1, got %d", v)
  233. }
  234. // Context is already canceled, so error should come before execution.
  235. if _, err := dbt.db.ExecContext(ctx, "INSERT INTO test VALUES (1)"); err == nil {
  236. dbt.Error("expected error")
  237. } else if err.Error() != "context canceled" {
  238. dbt.Fatalf("unexpected error: %s", err)
  239. }
  240. // The second insert query will fail, so the table has no changes.
  241. if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
  242. dbt.Fatalf("%s", err.Error())
  243. }
  244. if v != 1 {
  245. dbt.Errorf("expected val to be 1, got %d", v)
  246. }
  247. })
  248. }
  249. func TestContextCancelQuery(t *testing.T) {
  250. runTests(t, dsn, func(dbt *DBTest) {
  251. dbt.mustExec("CREATE TABLE test (v INTEGER)")
  252. ctx, cancel := context.WithCancel(context.Background())
  253. // Delay execution for just a bit until db.ExecContext has begun.
  254. defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
  255. // This query will be canceled.
  256. startTime := time.Now()
  257. if _, err := dbt.db.QueryContext(ctx, "INSERT INTO test VALUES (SLEEP(1))"); err != context.Canceled {
  258. dbt.Errorf("expected context.Canceled, got %v", err)
  259. }
  260. if d := time.Since(startTime); d > 500*time.Millisecond {
  261. dbt.Errorf("too long execution time: %s", d)
  262. }
  263. // Wait for the INSERT query has done.
  264. time.Sleep(time.Second)
  265. // Check how many times the query is executed.
  266. var v int
  267. if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
  268. dbt.Fatalf("%s", err.Error())
  269. }
  270. if v != 1 { // TODO: need to kill the query, and v should be 0.
  271. dbt.Errorf("expected val to be 1, got %d", v)
  272. }
  273. // Context is already canceled, so error should come before execution.
  274. if _, err := dbt.db.QueryContext(ctx, "INSERT INTO test VALUES (1)"); err != context.Canceled {
  275. dbt.Errorf("expected context.Canceled, got %v", err)
  276. }
  277. // The second insert query will fail, so the table has no changes.
  278. if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
  279. dbt.Fatalf("%s", err.Error())
  280. }
  281. if v != 1 {
  282. dbt.Errorf("expected val to be 1, got %d", v)
  283. }
  284. })
  285. }
  286. func TestContextCancelQueryRow(t *testing.T) {
  287. runTests(t, dsn, func(dbt *DBTest) {
  288. dbt.mustExec("CREATE TABLE test (v INTEGER)")
  289. dbt.mustExec("INSERT INTO test VALUES (1), (2), (3)")
  290. ctx, cancel := context.WithCancel(context.Background())
  291. rows, err := dbt.db.QueryContext(ctx, "SELECT v FROM test")
  292. if err != nil {
  293. dbt.Fatalf("%s", err.Error())
  294. }
  295. // the first row will be succeed.
  296. var v int
  297. if !rows.Next() {
  298. dbt.Fatalf("unexpected end")
  299. }
  300. if err := rows.Scan(&v); err != nil {
  301. dbt.Fatalf("%s", err.Error())
  302. }
  303. cancel()
  304. // make sure the driver recieve cancel request.
  305. time.Sleep(100 * time.Millisecond)
  306. if rows.Next() {
  307. dbt.Errorf("expected end, but not")
  308. }
  309. if err := rows.Err(); err != context.Canceled {
  310. dbt.Errorf("expected context.Canceled, got %v", err)
  311. }
  312. })
  313. }
  314. func TestContextCancelPrepare(t *testing.T) {
  315. runTests(t, dsn, func(dbt *DBTest) {
  316. ctx, cancel := context.WithCancel(context.Background())
  317. cancel()
  318. if _, err := dbt.db.PrepareContext(ctx, "SELECT 1"); err != context.Canceled {
  319. dbt.Errorf("expected context.Canceled, got %v", err)
  320. }
  321. })
  322. }
  323. func TestContextCancelStmtExec(t *testing.T) {
  324. runTests(t, dsn, func(dbt *DBTest) {
  325. dbt.mustExec("CREATE TABLE test (v INTEGER)")
  326. ctx, cancel := context.WithCancel(context.Background())
  327. stmt, err := dbt.db.PrepareContext(ctx, "INSERT INTO test VALUES (SLEEP(1))")
  328. if err != nil {
  329. dbt.Fatalf("unexpected error: %v", err)
  330. }
  331. // Delay execution for just a bit until db.ExecContext has begun.
  332. defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
  333. // This query will be canceled.
  334. startTime := time.Now()
  335. if _, err := stmt.ExecContext(ctx); err != context.Canceled {
  336. dbt.Errorf("expected context.Canceled, got %v", err)
  337. }
  338. if d := time.Since(startTime); d > 500*time.Millisecond {
  339. dbt.Errorf("too long execution time: %s", d)
  340. }
  341. // Wait for the INSERT query has done.
  342. time.Sleep(time.Second)
  343. // Check how many times the query is executed.
  344. var v int
  345. if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
  346. dbt.Fatalf("%s", err.Error())
  347. }
  348. if v != 1 { // TODO: need to kill the query, and v should be 0.
  349. dbt.Errorf("expected val to be 1, got %d", v)
  350. }
  351. })
  352. }
  353. func TestContextCancelStmtQuery(t *testing.T) {
  354. runTests(t, dsn, func(dbt *DBTest) {
  355. dbt.mustExec("CREATE TABLE test (v INTEGER)")
  356. ctx, cancel := context.WithCancel(context.Background())
  357. stmt, err := dbt.db.PrepareContext(ctx, "INSERT INTO test VALUES (SLEEP(1))")
  358. if err != nil {
  359. dbt.Fatalf("unexpected error: %v", err)
  360. }
  361. // Delay execution for just a bit until db.ExecContext has begun.
  362. defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
  363. // This query will be canceled.
  364. startTime := time.Now()
  365. if _, err := stmt.QueryContext(ctx); err != context.Canceled {
  366. dbt.Errorf("expected context.Canceled, got %v", err)
  367. }
  368. if d := time.Since(startTime); d > 500*time.Millisecond {
  369. dbt.Errorf("too long execution time: %s", d)
  370. }
  371. // Wait for the INSERT query has done.
  372. time.Sleep(time.Second)
  373. // Check how many times the query is executed.
  374. var v int
  375. if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
  376. dbt.Fatalf("%s", err.Error())
  377. }
  378. if v != 1 { // TODO: need to kill the query, and v should be 0.
  379. dbt.Errorf("expected val to be 1, got %d", v)
  380. }
  381. })
  382. }
  383. func TestContextCancelBegin(t *testing.T) {
  384. runTests(t, dsn, func(dbt *DBTest) {
  385. dbt.mustExec("CREATE TABLE test (v INTEGER)")
  386. ctx, cancel := context.WithCancel(context.Background())
  387. tx, err := dbt.db.BeginTx(ctx, nil)
  388. if err != nil {
  389. dbt.Fatal(err)
  390. }
  391. // Delay execution for just a bit until db.ExecContext has begun.
  392. defer time.AfterFunc(100*time.Millisecond, cancel).Stop()
  393. // This query will be canceled.
  394. startTime := time.Now()
  395. if _, err := tx.ExecContext(ctx, "INSERT INTO test VALUES (SLEEP(1))"); err != context.Canceled {
  396. dbt.Errorf("expected context.Canceled, got %v", err)
  397. }
  398. if d := time.Since(startTime); d > 500*time.Millisecond {
  399. dbt.Errorf("too long execution time: %s", d)
  400. }
  401. // Transaction is canceled, so expect an error.
  402. switch err := tx.Commit(); err {
  403. case sql.ErrTxDone:
  404. // because the transaction has already been rollbacked.
  405. // the database/sql package watches ctx
  406. // and rollbacks when ctx is canceled.
  407. case context.Canceled:
  408. // the database/sql package rollbacks on another goroutine,
  409. // so the transaction may not be rollbacked depending on goroutine scheduling.
  410. default:
  411. dbt.Errorf("expected sql.ErrTxDone or context.Canceled, got %v", err)
  412. }
  413. // Context is canceled, so cannot begin a transaction.
  414. if _, err := dbt.db.BeginTx(ctx, nil); err != context.Canceled {
  415. dbt.Errorf("expected context.Canceled, got %v", err)
  416. }
  417. })
  418. }
  419. func TestContextBeginIsolationLevel(t *testing.T) {
  420. runTests(t, dsn, func(dbt *DBTest) {
  421. dbt.mustExec("CREATE TABLE test (v INTEGER)")
  422. ctx, cancel := context.WithCancel(context.Background())
  423. defer cancel()
  424. tx1, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
  425. Isolation: sql.LevelRepeatableRead,
  426. })
  427. if err != nil {
  428. dbt.Fatal(err)
  429. }
  430. tx2, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
  431. Isolation: sql.LevelReadCommitted,
  432. })
  433. if err != nil {
  434. dbt.Fatal(err)
  435. }
  436. _, err = tx1.ExecContext(ctx, "INSERT INTO test VALUES (1)")
  437. if err != nil {
  438. dbt.Fatal(err)
  439. }
  440. var v int
  441. row := tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM test")
  442. if err := row.Scan(&v); err != nil {
  443. dbt.Fatal(err)
  444. }
  445. // Because writer transaction wasn't commited yet, it should be available
  446. if v != 0 {
  447. dbt.Errorf("expected val to be 0, got %d", v)
  448. }
  449. err = tx1.Commit()
  450. if err != nil {
  451. dbt.Fatal(err)
  452. }
  453. row = tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM test")
  454. if err := row.Scan(&v); err != nil {
  455. dbt.Fatal(err)
  456. }
  457. // Data written by writer transaction is already commited, it should be selectable
  458. if v != 1 {
  459. dbt.Errorf("expected val to be 1, got %d", v)
  460. }
  461. tx2.Commit()
  462. })
  463. }
  464. func TestContextBeginReadOnly(t *testing.T) {
  465. runTests(t, dsn, func(dbt *DBTest) {
  466. dbt.mustExec("CREATE TABLE test (v INTEGER)")
  467. ctx, cancel := context.WithCancel(context.Background())
  468. defer cancel()
  469. tx, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
  470. ReadOnly: true,
  471. })
  472. if _, ok := err.(*MySQLError); ok {
  473. dbt.Skip("It seems that your MySQL does not support READ ONLY transactions")
  474. return
  475. } else if err != nil {
  476. dbt.Fatal(err)
  477. }
  478. // INSERT queries fail in a READ ONLY transaction.
  479. _, err = tx.ExecContext(ctx, "INSERT INTO test VALUES (1)")
  480. if _, ok := err.(*MySQLError); !ok {
  481. dbt.Errorf("expected MySQLError, got %v", err)
  482. }
  483. // SELECT queries can be executed.
  484. var v int
  485. row := tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM test")
  486. if err := row.Scan(&v); err != nil {
  487. dbt.Fatal(err)
  488. }
  489. if v != 0 {
  490. dbt.Errorf("expected val to be 0, got %d", v)
  491. }
  492. if err := tx.Commit(); err != nil {
  493. dbt.Fatal(err)
  494. }
  495. })
  496. }
  497. func TestRowsColumnTypes(t *testing.T) {
  498. niNULL := sql.NullInt64{Int64: 0, Valid: false}
  499. ni0 := sql.NullInt64{Int64: 0, Valid: true}
  500. ni1 := sql.NullInt64{Int64: 1, Valid: true}
  501. ni42 := sql.NullInt64{Int64: 42, Valid: true}
  502. nfNULL := sql.NullFloat64{Float64: 0.0, Valid: false}
  503. nf0 := sql.NullFloat64{Float64: 0.0, Valid: true}
  504. nf1337 := sql.NullFloat64{Float64: 13.37, Valid: true}
  505. nt0 := NullTime{Time: time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC), Valid: true}
  506. nt1 := NullTime{Time: time.Date(2006, 01, 02, 15, 04, 05, 100000000, time.UTC), Valid: true}
  507. nt2 := NullTime{Time: time.Date(2006, 01, 02, 15, 04, 05, 110000000, time.UTC), Valid: true}
  508. nt6 := NullTime{Time: time.Date(2006, 01, 02, 15, 04, 05, 111111000, time.UTC), Valid: true}
  509. nd1 := NullTime{Time: time.Date(2006, 01, 02, 0, 0, 0, 0, time.UTC), Valid: true}
  510. nd2 := NullTime{Time: time.Date(2006, 03, 04, 0, 0, 0, 0, time.UTC), Valid: true}
  511. ndNULL := NullTime{Time: time.Time{}, Valid: false}
  512. rbNULL := sql.RawBytes(nil)
  513. rb0 := sql.RawBytes("0")
  514. rb42 := sql.RawBytes("42")
  515. rbTest := sql.RawBytes("Test")
  516. rb0pad4 := sql.RawBytes("0\x00\x00\x00") // BINARY right-pads values with 0x00
  517. rbx0 := sql.RawBytes("\x00")
  518. rbx42 := sql.RawBytes("\x42")
  519. var columns = []struct {
  520. name string
  521. fieldType string // type used when creating table schema
  522. databaseTypeName string // actual type used by MySQL
  523. scanType reflect.Type
  524. nullable bool
  525. precision int64 // 0 if not ok
  526. scale int64
  527. valuesIn [3]string
  528. valuesOut [3]interface{}
  529. }{
  530. {"bit8null", "BIT(8)", "BIT", scanTypeRawBytes, true, 0, 0, [3]string{"0x0", "NULL", "0x42"}, [3]interface{}{rbx0, rbNULL, rbx42}},
  531. {"boolnull", "BOOL", "TINYINT", scanTypeNullInt, true, 0, 0, [3]string{"NULL", "true", "0"}, [3]interface{}{niNULL, ni1, ni0}},
  532. {"bool", "BOOL NOT NULL", "TINYINT", scanTypeInt8, false, 0, 0, [3]string{"1", "0", "FALSE"}, [3]interface{}{int8(1), int8(0), int8(0)}},
  533. {"intnull", "INTEGER", "INT", scanTypeNullInt, true, 0, 0, [3]string{"0", "NULL", "42"}, [3]interface{}{ni0, niNULL, ni42}},
  534. {"smallint", "SMALLINT NOT NULL", "SMALLINT", scanTypeInt16, false, 0, 0, [3]string{"0", "-32768", "32767"}, [3]interface{}{int16(0), int16(-32768), int16(32767)}},
  535. {"smallintnull", "SMALLINT", "SMALLINT", scanTypeNullInt, true, 0, 0, [3]string{"0", "NULL", "42"}, [3]interface{}{ni0, niNULL, ni42}},
  536. {"int3null", "INT(3)", "INT", scanTypeNullInt, true, 0, 0, [3]string{"0", "NULL", "42"}, [3]interface{}{ni0, niNULL, ni42}},
  537. {"int7", "INT(7) NOT NULL", "INT", scanTypeInt32, false, 0, 0, [3]string{"0", "-1337", "42"}, [3]interface{}{int32(0), int32(-1337), int32(42)}},
  538. {"mediumintnull", "MEDIUMINT", "MEDIUMINT", scanTypeNullInt, true, 0, 0, [3]string{"0", "42", "NULL"}, [3]interface{}{ni0, ni42, niNULL}},
  539. {"bigint", "BIGINT NOT NULL", "BIGINT", scanTypeInt64, false, 0, 0, [3]string{"0", "65535", "-42"}, [3]interface{}{int64(0), int64(65535), int64(-42)}},
  540. {"bigintnull", "BIGINT", "BIGINT", scanTypeNullInt, true, 0, 0, [3]string{"NULL", "1", "42"}, [3]interface{}{niNULL, ni1, ni42}},
  541. {"tinyuint", "TINYINT UNSIGNED NOT NULL", "TINYINT", scanTypeUint8, false, 0, 0, [3]string{"0", "255", "42"}, [3]interface{}{uint8(0), uint8(255), uint8(42)}},
  542. {"smalluint", "SMALLINT UNSIGNED NOT NULL", "SMALLINT", scanTypeUint16, false, 0, 0, [3]string{"0", "65535", "42"}, [3]interface{}{uint16(0), uint16(65535), uint16(42)}},
  543. {"biguint", "BIGINT UNSIGNED NOT NULL", "BIGINT", scanTypeUint64, false, 0, 0, [3]string{"0", "65535", "42"}, [3]interface{}{uint64(0), uint64(65535), uint64(42)}},
  544. {"uint13", "INT(13) UNSIGNED NOT NULL", "INT", scanTypeUint32, false, 0, 0, [3]string{"0", "1337", "42"}, [3]interface{}{uint32(0), uint32(1337), uint32(42)}},
  545. {"float", "FLOAT NOT NULL", "FLOAT", scanTypeFloat32, false, math.MaxInt64, math.MaxInt64, [3]string{"0", "42", "13.37"}, [3]interface{}{float32(0), float32(42), float32(13.37)}},
  546. {"floatnull", "FLOAT", "FLOAT", scanTypeNullFloat, true, math.MaxInt64, math.MaxInt64, [3]string{"0", "NULL", "13.37"}, [3]interface{}{nf0, nfNULL, nf1337}},
  547. {"float74null", "FLOAT(7,4)", "FLOAT", scanTypeNullFloat, true, math.MaxInt64, 4, [3]string{"0", "NULL", "13.37"}, [3]interface{}{nf0, nfNULL, nf1337}},
  548. {"double", "DOUBLE NOT NULL", "DOUBLE", scanTypeFloat64, false, math.MaxInt64, math.MaxInt64, [3]string{"0", "42", "13.37"}, [3]interface{}{float64(0), float64(42), float64(13.37)}},
  549. {"doublenull", "DOUBLE", "DOUBLE", scanTypeNullFloat, true, math.MaxInt64, math.MaxInt64, [3]string{"0", "NULL", "13.37"}, [3]interface{}{nf0, nfNULL, nf1337}},
  550. {"decimal1", "DECIMAL(10,6) NOT NULL", "DECIMAL", scanTypeRawBytes, false, 10, 6, [3]string{"0", "13.37", "1234.123456"}, [3]interface{}{sql.RawBytes("0.000000"), sql.RawBytes("13.370000"), sql.RawBytes("1234.123456")}},
  551. {"decimal1null", "DECIMAL(10,6)", "DECIMAL", scanTypeRawBytes, true, 10, 6, [3]string{"0", "NULL", "1234.123456"}, [3]interface{}{sql.RawBytes("0.000000"), rbNULL, sql.RawBytes("1234.123456")}},
  552. {"decimal2", "DECIMAL(8,4) NOT NULL", "DECIMAL", scanTypeRawBytes, false, 8, 4, [3]string{"0", "13.37", "1234.123456"}, [3]interface{}{sql.RawBytes("0.0000"), sql.RawBytes("13.3700"), sql.RawBytes("1234.1235")}},
  553. {"decimal2null", "DECIMAL(8,4)", "DECIMAL", scanTypeRawBytes, true, 8, 4, [3]string{"0", "NULL", "1234.123456"}, [3]interface{}{sql.RawBytes("0.0000"), rbNULL, sql.RawBytes("1234.1235")}},
  554. {"decimal3", "DECIMAL(5,0) NOT NULL", "DECIMAL", scanTypeRawBytes, false, 5, 0, [3]string{"0", "13.37", "-12345.123456"}, [3]interface{}{rb0, sql.RawBytes("13"), sql.RawBytes("-12345")}},
  555. {"decimal3null", "DECIMAL(5,0)", "DECIMAL", scanTypeRawBytes, true, 5, 0, [3]string{"0", "NULL", "-12345.123456"}, [3]interface{}{rb0, rbNULL, sql.RawBytes("-12345")}},
  556. {"char25null", "CHAR(25)", "CHAR", scanTypeRawBytes, true, 0, 0, [3]string{"0", "NULL", "'Test'"}, [3]interface{}{rb0, rbNULL, rbTest}},
  557. {"varchar42", "VARCHAR(42) NOT NULL", "VARCHAR", scanTypeRawBytes, false, 0, 0, [3]string{"0", "'Test'", "42"}, [3]interface{}{rb0, rbTest, rb42}},
  558. {"binary4null", "BINARY(4)", "BINARY", scanTypeRawBytes, true, 0, 0, [3]string{"0", "NULL", "'Test'"}, [3]interface{}{rb0pad4, rbNULL, rbTest}},
  559. {"varbinary42", "VARBINARY(42) NOT NULL", "VARBINARY", scanTypeRawBytes, false, 0, 0, [3]string{"0", "'Test'", "42"}, [3]interface{}{rb0, rbTest, rb42}},
  560. {"tinyblobnull", "TINYBLOB", "BLOB", scanTypeRawBytes, true, 0, 0, [3]string{"0", "NULL", "'Test'"}, [3]interface{}{rb0, rbNULL, rbTest}},
  561. {"tinytextnull", "TINYTEXT", "TEXT", scanTypeRawBytes, true, 0, 0, [3]string{"0", "NULL", "'Test'"}, [3]interface{}{rb0, rbNULL, rbTest}},
  562. {"blobnull", "BLOB", "BLOB", scanTypeRawBytes, true, 0, 0, [3]string{"0", "NULL", "'Test'"}, [3]interface{}{rb0, rbNULL, rbTest}},
  563. {"textnull", "TEXT", "TEXT", scanTypeRawBytes, true, 0, 0, [3]string{"0", "NULL", "'Test'"}, [3]interface{}{rb0, rbNULL, rbTest}},
  564. {"mediumblob", "MEDIUMBLOB NOT NULL", "BLOB", scanTypeRawBytes, false, 0, 0, [3]string{"0", "'Test'", "42"}, [3]interface{}{rb0, rbTest, rb42}},
  565. {"mediumtext", "MEDIUMTEXT NOT NULL", "TEXT", scanTypeRawBytes, false, 0, 0, [3]string{"0", "'Test'", "42"}, [3]interface{}{rb0, rbTest, rb42}},
  566. {"longblob", "LONGBLOB NOT NULL", "BLOB", scanTypeRawBytes, false, 0, 0, [3]string{"0", "'Test'", "42"}, [3]interface{}{rb0, rbTest, rb42}},
  567. {"longtext", "LONGTEXT NOT NULL", "TEXT", scanTypeRawBytes, false, 0, 0, [3]string{"0", "'Test'", "42"}, [3]interface{}{rb0, rbTest, rb42}},
  568. {"datetime", "DATETIME", "DATETIME", scanTypeNullTime, true, 0, 0, [3]string{"'2006-01-02 15:04:05'", "'2006-01-02 15:04:05.1'", "'2006-01-02 15:04:05.111111'"}, [3]interface{}{nt0, nt0, nt0}},
  569. {"datetime2", "DATETIME(2)", "DATETIME", scanTypeNullTime, true, 2, 2, [3]string{"'2006-01-02 15:04:05'", "'2006-01-02 15:04:05.1'", "'2006-01-02 15:04:05.111111'"}, [3]interface{}{nt0, nt1, nt2}},
  570. {"datetime6", "DATETIME(6)", "DATETIME", scanTypeNullTime, true, 6, 6, [3]string{"'2006-01-02 15:04:05'", "'2006-01-02 15:04:05.1'", "'2006-01-02 15:04:05.111111'"}, [3]interface{}{nt0, nt1, nt6}},
  571. {"date", "DATE", "DATE", scanTypeNullTime, true, 0, 0, [3]string{"'2006-01-02'", "NULL", "'2006-03-04'"}, [3]interface{}{nd1, ndNULL, nd2}},
  572. {"year", "YEAR NOT NULL", "YEAR", scanTypeUint16, false, 0, 0, [3]string{"2006", "2000", "1994"}, [3]interface{}{uint16(2006), uint16(2000), uint16(1994)}},
  573. }
  574. schema := ""
  575. values1 := ""
  576. values2 := ""
  577. values3 := ""
  578. for _, column := range columns {
  579. schema += fmt.Sprintf("`%s` %s, ", column.name, column.fieldType)
  580. values1 += column.valuesIn[0] + ", "
  581. values2 += column.valuesIn[1] + ", "
  582. values3 += column.valuesIn[2] + ", "
  583. }
  584. schema = schema[:len(schema)-2]
  585. values1 = values1[:len(values1)-2]
  586. values2 = values2[:len(values2)-2]
  587. values3 = values3[:len(values3)-2]
  588. dsns := []string{
  589. dsn + "&parseTime=true",
  590. dsn + "&parseTime=false",
  591. }
  592. for _, testdsn := range dsns {
  593. runTests(t, testdsn, func(dbt *DBTest) {
  594. dbt.mustExec("CREATE TABLE test (" + schema + ")")
  595. dbt.mustExec("INSERT INTO test VALUES (" + values1 + "), (" + values2 + "), (" + values3 + ")")
  596. rows, err := dbt.db.Query("SELECT * FROM test")
  597. if err != nil {
  598. t.Fatalf("Query: %v", err)
  599. }
  600. tt, err := rows.ColumnTypes()
  601. if err != nil {
  602. t.Fatalf("ColumnTypes: %v", err)
  603. }
  604. if len(tt) != len(columns) {
  605. t.Fatalf("unexpected number of columns: expected %d, got %d", len(columns), len(tt))
  606. }
  607. types := make([]reflect.Type, len(tt))
  608. for i, tp := range tt {
  609. column := columns[i]
  610. // Name
  611. name := tp.Name()
  612. if name != column.name {
  613. t.Errorf("column name mismatch %s != %s", name, column.name)
  614. continue
  615. }
  616. // DatabaseTypeName
  617. databaseTypeName := tp.DatabaseTypeName()
  618. if databaseTypeName != column.databaseTypeName {
  619. t.Errorf("databasetypename name mismatch for column %q: %s != %s", name, databaseTypeName, column.databaseTypeName)
  620. continue
  621. }
  622. // ScanType
  623. scanType := tp.ScanType()
  624. if scanType != column.scanType {
  625. if scanType == nil {
  626. t.Errorf("scantype is null for column %q", name)
  627. } else {
  628. t.Errorf("scantype mismatch for column %q: %s != %s", name, scanType.Name(), column.scanType.Name())
  629. }
  630. continue
  631. }
  632. types[i] = scanType
  633. // Nullable
  634. nullable, ok := tp.Nullable()
  635. if !ok {
  636. t.Errorf("nullable not ok %q", name)
  637. continue
  638. }
  639. if nullable != column.nullable {
  640. t.Errorf("nullable mismatch for column %q: %t != %t", name, nullable, column.nullable)
  641. }
  642. // Length
  643. // length, ok := tp.Length()
  644. // if length != column.length {
  645. // if !ok {
  646. // t.Errorf("length not ok for column %q", name)
  647. // } else {
  648. // t.Errorf("length mismatch for column %q: %d != %d", name, length, column.length)
  649. // }
  650. // continue
  651. // }
  652. // Precision and Scale
  653. precision, scale, ok := tp.DecimalSize()
  654. if precision != column.precision {
  655. if !ok {
  656. t.Errorf("precision not ok for column %q", name)
  657. } else {
  658. t.Errorf("precision mismatch for column %q: %d != %d", name, precision, column.precision)
  659. }
  660. continue
  661. }
  662. if scale != column.scale {
  663. if !ok {
  664. t.Errorf("scale not ok for column %q", name)
  665. } else {
  666. t.Errorf("scale mismatch for column %q: %d != %d", name, scale, column.scale)
  667. }
  668. continue
  669. }
  670. }
  671. values := make([]interface{}, len(tt))
  672. for i := range values {
  673. values[i] = reflect.New(types[i]).Interface()
  674. }
  675. i := 0
  676. for rows.Next() {
  677. err = rows.Scan(values...)
  678. if err != nil {
  679. t.Fatalf("failed to scan values in %v", err)
  680. }
  681. for j := range values {
  682. value := reflect.ValueOf(values[j]).Elem().Interface()
  683. if !reflect.DeepEqual(value, columns[j].valuesOut[i]) {
  684. if columns[j].scanType == scanTypeRawBytes {
  685. t.Errorf("row %d, column %d: %v != %v", i, j, string(value.(sql.RawBytes)), string(columns[j].valuesOut[i].(sql.RawBytes)))
  686. } else {
  687. t.Errorf("row %d, column %d: %v != %v", i, j, value, columns[j].valuesOut[i])
  688. }
  689. }
  690. }
  691. i++
  692. }
  693. if i != 3 {
  694. t.Errorf("expected 3 rows, got %d", i)
  695. }
  696. if err := rows.Close(); err != nil {
  697. t.Errorf("error closing rows: %s", err)
  698. }
  699. })
  700. }
  701. }
  702. func TestValuerWithValueReceiverGivenNilValue(t *testing.T) {
  703. runTests(t, dsn, func(dbt *DBTest) {
  704. dbt.mustExec("CREATE TABLE test (value VARCHAR(255))")
  705. dbt.db.Exec("INSERT INTO test VALUES (?)", (*testValuer)(nil))
  706. // This test will panic on the INSERT if ConvertValue() does not check for typed nil before calling Value()
  707. })
  708. }