Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(695)

Side by Side Diff: src/pkg/database/sql/sql.go

Issue 6460087: code review 6460087: database/sql: check NumInput on Stmt.Exec
Patch Set: diff -r caf30a0fbd77 https://code.google.com/p/go Created 11 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 provides a generic interface around SQL (or SQL-like) 5 // Package sql provides a generic interface around SQL (or SQL-like)
6 // databases. 6 // databases.
7 package sql 7 package sql
8 8
9 import ( 9 import (
10 "database/sql/driver" 10 "database/sql/driver"
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 return result{resi}, nil 362 return result{resi}, nil
363 } 363 }
364 } 364 }
365 365
366 sti, err := ci.Prepare(query) 366 sti, err := ci.Prepare(query)
367 if err != nil { 367 if err != nil {
368 return nil, err 368 return nil, err
369 } 369 }
370 defer sti.Close() 370 defer sti.Close()
371 371
372 » dargs, err := driverArgs(sti, args) 372 » return resultFromStatement(sti, args...)
373 » if err != nil {
374 » » return nil, err
375 » }
376
377 » resi, err := sti.Exec(dargs)
378 » if err != nil {
379 » » return nil, err
380 » }
381 » return result{resi}, nil
382 } 373 }
383 374
384 // Query executes a query that returns rows, typically a SELECT. 375 // Query executes a query that returns rows, typically a SELECT.
385 func (db *DB) Query(query string, args ...interface{}) (*Rows, error) { 376 func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
386 stmt, err := db.Prepare(query) 377 stmt, err := db.Prepare(query)
387 if err != nil { 378 if err != nil {
388 return nil, err 379 return nil, err
389 } 380 }
390 rows, err := stmt.Query(args...) 381 rows, err := stmt.Query(args...)
391 if err != nil { 382 if err != nil {
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 return nil, err 592 return nil, err
602 } 593 }
603 } 594 }
604 595
605 sti, err := ci.Prepare(query) 596 sti, err := ci.Prepare(query)
606 if err != nil { 597 if err != nil {
607 return nil, err 598 return nil, err
608 } 599 }
609 defer sti.Close() 600 defer sti.Close()
610 601
611 » dargs, err := driverArgs(sti, args) 602 » return resultFromStatement(sti, args...)
612 » if err != nil {
613 » » return nil, err
614 » }
615
616 » resi, err := sti.Exec(dargs)
617 » if err != nil {
618 » » return nil, err
619 » }
620 » return result{resi}, nil
621 } 603 }
622 604
623 // Query executes a query that returns rows, typically a SELECT. 605 // Query executes a query that returns rows, typically a SELECT.
624 func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) { 606 func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
625 if tx.done { 607 if tx.done {
626 return nil, ErrTxDone 608 return nil, ErrTxDone
627 } 609 }
628 stmt, err := tx.Prepare(query) 610 stmt, err := tx.Prepare(query)
629 if err != nil { 611 if err != nil {
630 return nil, err 612 return nil, err
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 657
676 // Exec executes a prepared statement with the given arguments and 658 // Exec executes a prepared statement with the given arguments and
677 // returns a Result summarizing the effect of the statement. 659 // returns a Result summarizing the effect of the statement.
678 func (s *Stmt) Exec(args ...interface{}) (Result, error) { 660 func (s *Stmt) Exec(args ...interface{}) (Result, error) {
679 _, releaseConn, si, err := s.connStmt() 661 _, releaseConn, si, err := s.connStmt()
680 if err != nil { 662 if err != nil {
681 return nil, err 663 return nil, err
682 } 664 }
683 defer releaseConn(nil) 665 defer releaseConn(nil)
684 666
667 return resultFromStatement(si, args...)
668 }
669
670 func resultFromStatement(si driver.Stmt, args ...interface{}) (Result, error) {
685 // -1 means the driver doesn't know how to count the number of 671 // -1 means the driver doesn't know how to count the number of
686 // placeholders, so we won't sanity check input here and instead let the 672 // placeholders, so we won't sanity check input here and instead let the
687 // driver deal with errors. 673 // driver deal with errors.
688 if want := si.NumInput(); want != -1 && len(args) != want { 674 if want := si.NumInput(); want != -1 && len(args) != want {
689 return nil, fmt.Errorf("sql: expected %d arguments, got %d", wan t, len(args)) 675 return nil, fmt.Errorf("sql: expected %d arguments, got %d", wan t, len(args))
690 } 676 }
691 677
692 dargs, err := driverArgs(si, args) 678 dargs, err := driverArgs(si, args)
693 if err != nil { 679 if err != nil {
694 return nil, err 680 return nil, err
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
1035 1021
1036 // A Result summarizes an executed SQL command. 1022 // A Result summarizes an executed SQL command.
1037 type Result interface { 1023 type Result interface {
1038 LastInsertId() (int64, error) 1024 LastInsertId() (int64, error)
1039 RowsAffected() (int64, error) 1025 RowsAffected() (int64, error)
1040 } 1026 }
1041 1027
1042 type result struct { 1028 type result struct {
1043 driver.Result 1029 driver.Result
1044 } 1030 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b