OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package sql | 5 package sql |
6 | 6 |
7 import ( | 7 import ( |
8 "database/sql/driver" | 8 "database/sql/driver" |
9 "fmt" | 9 "fmt" |
10 "reflect" | 10 "reflect" |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 } | 244 } |
245 } | 245 } |
246 | 246 |
247 func TestStatementQueryRow(t *testing.T) { | 247 func TestStatementQueryRow(t *testing.T) { |
248 db := newTestDB(t, "people") | 248 db := newTestDB(t, "people") |
249 defer closeDB(t, db) | 249 defer closeDB(t, db) |
250 stmt, err := db.Prepare("SELECT|people|age|name=?") | 250 stmt, err := db.Prepare("SELECT|people|age|name=?") |
251 if err != nil { | 251 if err != nil { |
252 t.Fatalf("Prepare: %v", err) | 252 t.Fatalf("Prepare: %v", err) |
253 } | 253 } |
| 254 defer stmt.Close() |
254 var age int | 255 var age int |
255 for n, tt := range []struct { | 256 for n, tt := range []struct { |
256 name string | 257 name string |
257 want int | 258 want int |
258 }{ | 259 }{ |
259 {"Alice", 1}, | 260 {"Alice", 1}, |
260 {"Bob", 2}, | 261 {"Bob", 2}, |
261 {"Chris", 3}, | 262 {"Chris", 3}, |
262 } { | 263 } { |
263 if err := stmt.QueryRow(tt.name).Scan(&age); err != nil { | 264 if err := stmt.QueryRow(tt.name).Scan(&age); err != nil { |
(...skipping 20 matching lines...) Expand all Loading... |
284 } | 285 } |
285 | 286 |
286 func TestExec(t *testing.T) { | 287 func TestExec(t *testing.T) { |
287 db := newTestDB(t, "foo") | 288 db := newTestDB(t, "foo") |
288 defer closeDB(t, db) | 289 defer closeDB(t, db) |
289 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool") | 290 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool") |
290 stmt, err := db.Prepare("INSERT|t1|name=?,age=?") | 291 stmt, err := db.Prepare("INSERT|t1|name=?,age=?") |
291 if err != nil { | 292 if err != nil { |
292 t.Errorf("Stmt, err = %v, %v", stmt, err) | 293 t.Errorf("Stmt, err = %v, %v", stmt, err) |
293 } | 294 } |
| 295 defer stmt.Close() |
294 | 296 |
295 type execTest struct { | 297 type execTest struct { |
296 args []interface{} | 298 args []interface{} |
297 wantErr string | 299 wantErr string |
298 } | 300 } |
299 execTests := []execTest{ | 301 execTests := []execTest{ |
300 // Okay: | 302 // Okay: |
301 {[]interface{}{"Brad", 31}, ""}, | 303 {[]interface{}{"Brad", 31}, ""}, |
302 {[]interface{}{"Brad", int64(31)}, ""}, | 304 {[]interface{}{"Brad", int64(31)}, ""}, |
303 {[]interface{}{"Bob", "32"}, ""}, | 305 {[]interface{}{"Bob", "32"}, ""}, |
(...skipping 21 matching lines...) Expand all Loading... |
325 } | 327 } |
326 | 328 |
327 func TestTxStmt(t *testing.T) { | 329 func TestTxStmt(t *testing.T) { |
328 db := newTestDB(t, "") | 330 db := newTestDB(t, "") |
329 defer closeDB(t, db) | 331 defer closeDB(t, db) |
330 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool") | 332 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool") |
331 stmt, err := db.Prepare("INSERT|t1|name=?,age=?") | 333 stmt, err := db.Prepare("INSERT|t1|name=?,age=?") |
332 if err != nil { | 334 if err != nil { |
333 t.Fatalf("Stmt, err = %v, %v", stmt, err) | 335 t.Fatalf("Stmt, err = %v, %v", stmt, err) |
334 } | 336 } |
| 337 defer stmt.Close() |
335 tx, err := db.Begin() | 338 tx, err := db.Begin() |
336 if err != nil { | 339 if err != nil { |
337 t.Fatalf("Begin = %v", err) | 340 t.Fatalf("Begin = %v", err) |
338 } | 341 } |
339 » _, err = tx.Stmt(stmt).Exec("Bobby", 7) | 342 » txs := tx.Stmt(stmt) |
| 343 » defer txs.Close() |
| 344 » _, err = txs.Exec("Bobby", 7) |
340 if err != nil { | 345 if err != nil { |
341 t.Fatalf("Exec = %v", err) | 346 t.Fatalf("Exec = %v", err) |
342 } | 347 } |
343 err = tx.Commit() | 348 err = tx.Commit() |
344 if err != nil { | 349 if err != nil { |
345 t.Fatalf("Commit = %v", err) | 350 t.Fatalf("Commit = %v", err) |
346 } | 351 } |
347 } | 352 } |
348 | 353 |
349 // Issue: http://golang.org/issue/2784 | 354 // Issue: http://golang.org/issue/2784 |
350 // This test didn't fail before because we got luckly with the fakedb driver. | 355 // This test didn't fail before because we got luckly with the fakedb driver. |
351 // It was failing, and now not, in github.com/bradfitz/go-sql-test | 356 // It was failing, and now not, in github.com/bradfitz/go-sql-test |
352 func TestTxQuery(t *testing.T) { | 357 func TestTxQuery(t *testing.T) { |
353 db := newTestDB(t, "") | 358 db := newTestDB(t, "") |
354 defer closeDB(t, db) | 359 defer closeDB(t, db) |
355 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool") | 360 exec(t, db, "CREATE|t1|name=string,age=int32,dead=bool") |
356 exec(t, db, "INSERT|t1|name=Alice") | 361 exec(t, db, "INSERT|t1|name=Alice") |
357 | 362 |
358 tx, err := db.Begin() | 363 tx, err := db.Begin() |
359 if err != nil { | 364 if err != nil { |
360 t.Fatal(err) | 365 t.Fatal(err) |
361 } | 366 } |
362 defer tx.Rollback() | 367 defer tx.Rollback() |
363 | 368 |
364 r, err := tx.Query("SELECT|t1|name|") | 369 r, err := tx.Query("SELECT|t1|name|") |
365 if err != nil { | 370 if err != nil { |
366 t.Fatal(err) | 371 t.Fatal(err) |
367 } | 372 } |
| 373 defer r.Close() |
368 | 374 |
369 if !r.Next() { | 375 if !r.Next() { |
370 if r.Err() != nil { | 376 if r.Err() != nil { |
371 t.Fatal(r.Err()) | 377 t.Fatal(r.Err()) |
372 } | 378 } |
373 t.Fatal("expected one row") | 379 t.Fatal("expected one row") |
374 } | 380 } |
375 | 381 |
376 var x string | 382 var x string |
377 err = r.Scan(&x) | 383 err = r.Scan(&x) |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 | 560 |
555 // Inserts with db.Exec: | 561 // Inserts with db.Exec: |
556 exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 1, "alice", spec.
rows[0].nullParam, spec.rows[0].notNullParam) | 562 exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 1, "alice", spec.
rows[0].nullParam, spec.rows[0].notNullParam) |
557 exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 2, "bob", spec.ro
ws[1].nullParam, spec.rows[1].notNullParam) | 563 exec(t, db, "INSERT|t|id=?,name=?,nullf=?,notnullf=?", 2, "bob", spec.ro
ws[1].nullParam, spec.rows[1].notNullParam) |
558 | 564 |
559 // Inserts with a prepared statement: | 565 // Inserts with a prepared statement: |
560 stmt, err := db.Prepare("INSERT|t|id=?,name=?,nullf=?,notnullf=?") | 566 stmt, err := db.Prepare("INSERT|t|id=?,name=?,nullf=?,notnullf=?") |
561 if err != nil { | 567 if err != nil { |
562 t.Fatalf("prepare: %v", err) | 568 t.Fatalf("prepare: %v", err) |
563 } | 569 } |
| 570 defer stmt.Close() |
564 if _, err := stmt.Exec(3, "chris", spec.rows[2].nullParam, spec.rows[2].
notNullParam); err != nil { | 571 if _, err := stmt.Exec(3, "chris", spec.rows[2].nullParam, spec.rows[2].
notNullParam); err != nil { |
565 t.Errorf("exec insert chris: %v", err) | 572 t.Errorf("exec insert chris: %v", err) |
566 } | 573 } |
567 if _, err := stmt.Exec(4, "dave", spec.rows[3].nullParam, spec.rows[3].n
otNullParam); err != nil { | 574 if _, err := stmt.Exec(4, "dave", spec.rows[3].nullParam, spec.rows[3].n
otNullParam); err != nil { |
568 t.Errorf("exec insert dave: %v", err) | 575 t.Errorf("exec insert dave: %v", err) |
569 } | 576 } |
570 if _, err := stmt.Exec(5, "eleanor", spec.rows[4].nullParam, spec.rows[4
].notNullParam); err != nil { | 577 if _, err := stmt.Exec(5, "eleanor", spec.rows[4].nullParam, spec.rows[4
].notNullParam); err != nil { |
571 t.Errorf("exec insert eleanor: %v", err) | 578 t.Errorf("exec insert eleanor: %v", err) |
572 } | 579 } |
573 | 580 |
(...skipping 24 matching lines...) Expand all Loading... |
598 if !reflect.DeepEqual(bindValDeref, spec.rows[i].scanNullVal) { | 605 if !reflect.DeepEqual(bindValDeref, spec.rows[i].scanNullVal) { |
599 t.Errorf("id=%d got %#v, want %#v", id, bindValDeref, sp
ec.rows[i].scanNullVal) | 606 t.Errorf("id=%d got %#v, want %#v", id, bindValDeref, sp
ec.rows[i].scanNullVal) |
600 } | 607 } |
601 } | 608 } |
602 } | 609 } |
603 | 610 |
604 func stack() string { | 611 func stack() string { |
605 buf := make([]byte, 1024) | 612 buf := make([]byte, 1024) |
606 return string(buf[:runtime.Stack(buf, false)]) | 613 return string(buf[:runtime.Stack(buf, false)]) |
607 } | 614 } |
OLD | NEW |