|
| 1 | +package sql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + stdSql "database/sql" |
| 6 | +) |
| 7 | + |
| 8 | +// Rows is the result of a query. Its cursor starts before the first row |
| 9 | +// of the result set. Use Next to advance from row to row. |
| 10 | +type Rows struct { |
| 11 | + ctx context.Context |
| 12 | + rows *stdSql.Rows |
| 13 | + killerPool *stdSql.DB |
| 14 | + connectionID string |
| 15 | +} |
| 16 | + |
| 17 | +// Unleak will release the reference to the killerPool |
| 18 | +// in order to prevent a memory leak. |
| 19 | +func (rs *Rows) Unleak() { |
| 20 | + rs.killerPool = nil |
| 21 | + rs.connectionID = "" |
| 22 | +} |
| 23 | + |
| 24 | +// Close closes the Rows, preventing further enumeration. If Next is called |
| 25 | +// and returns false and there are no further result sets, |
| 26 | +// the Rows are closed automatically and it will suffice to check the |
| 27 | +// result of Err. Close is idempotent and does not affect the result of Err. |
| 28 | +func (rs *Rows) Close() error { |
| 29 | + err := rs.rows.Close() |
| 30 | + if rs.ctx.Err() != nil { |
| 31 | + kill(rs.killerPool, rs.connectionID) |
| 32 | + } |
| 33 | + rs.Unleak() |
| 34 | + return err |
| 35 | +} |
| 36 | + |
| 37 | +// ColumnTypes returns column information such as column type, length, |
| 38 | +// and nullable. Some information may not be available from some drivers. |
| 39 | +func (rs *Rows) ColumnTypes() ([]*stdSql.ColumnType, error) { |
| 40 | + ct, err := rs.rows.ColumnTypes() |
| 41 | + if rs.ctx.Err() != nil { |
| 42 | + kill(rs.killerPool, rs.connectionID) |
| 43 | + } |
| 44 | + return ct, err |
| 45 | +} |
| 46 | + |
| 47 | +// Columns returns the column names. |
| 48 | +// Columns returns an error if the rows are closed. |
| 49 | +func (rs *Rows) Columns() ([]string, error) { |
| 50 | + cols, err := rs.rows.Columns() |
| 51 | + if rs.ctx.Err() != nil { |
| 52 | + kill(rs.killerPool, rs.connectionID) |
| 53 | + } |
| 54 | + return cols, err |
| 55 | +} |
| 56 | + |
| 57 | +// Err returns the error, if any, that was encountered during iteration. |
| 58 | +// Err may be called after an explicit or implicit Close. |
| 59 | +func (rs *Rows) Err() error { |
| 60 | + err := rs.rows.Err() |
| 61 | + if rs.ctx.Err() != nil { |
| 62 | + kill(rs.killerPool, rs.connectionID) |
| 63 | + } |
| 64 | + return err |
| 65 | +} |
| 66 | + |
| 67 | +// Next prepares the next result row for reading with the Scan method. It |
| 68 | +// returns true on success, or false if there is no next result row or an error |
| 69 | +// happened while preparing it. Err should be consulted to distinguish between |
| 70 | +// the two cases. |
| 71 | +// |
| 72 | +// Every call to Scan, even the first one, must be preceded by a call to Next. |
| 73 | +func (rs *Rows) Next() bool { |
| 74 | + return rs.rows.Next() |
| 75 | +} |
| 76 | + |
| 77 | +// NextResultSet prepares the next result set for reading. It reports whether |
| 78 | +// there is further result sets, or false if there is no further result set |
| 79 | +// or if there is an error advancing to it. The Err method should be consulted |
| 80 | +// to distinguish between the two cases. |
| 81 | +// |
| 82 | +// After calling NextResultSet, the Next method should always be called before |
| 83 | +// scanning. If there are further result sets they may not have rows in the result |
| 84 | +// set. |
| 85 | +func (rs *Rows) NextResultSet() bool { |
| 86 | + return rs.rows.NextResultSet() |
| 87 | +} |
| 88 | + |
| 89 | +// Scan copies the columns in the current row into the values pointed |
| 90 | +// at by dest. The number of values in dest must be the same as the |
| 91 | +// number of columns in Rows. |
| 92 | +// |
| 93 | +// Scan converts columns read from the database into the following |
| 94 | +// common Go types and special types provided by the sql package: |
| 95 | +// |
| 96 | +// *string |
| 97 | +// *[]byte |
| 98 | +// *int, *int8, *int16, *int32, *int64 |
| 99 | +// *uint, *uint8, *uint16, *uint32, *uint64 |
| 100 | +// *bool |
| 101 | +// *float32, *float64 |
| 102 | +// *interface{} |
| 103 | +// *RawBytes |
| 104 | +// *Rows (cursor value) |
| 105 | +// any type implementing Scanner (see Scanner docs) |
| 106 | +// |
| 107 | +// In the most simple case, if the type of the value from the source |
| 108 | +// column is an integer, bool or string type T and dest is of type *T, |
| 109 | +// Scan simply assigns the value through the pointer. |
| 110 | +// |
| 111 | +// Scan also converts between string and numeric types, as long as no |
| 112 | +// information would be lost. While Scan stringifies all numbers |
| 113 | +// scanned from numeric database columns into *string, scans into |
| 114 | +// numeric types are checked for overflow. For example, a float64 with |
| 115 | +// value 300 or a string with value "300" can scan into a uint16, but |
| 116 | +// not into a uint8, though float64(255) or "255" can scan into a |
| 117 | +// uint8. One exception is that scans of some float64 numbers to |
| 118 | +// strings may lose information when stringifying. In general, scan |
| 119 | +// floating point columns into *float64. |
| 120 | +// |
| 121 | +// If a dest argument has type *[]byte, Scan saves in that argument a |
| 122 | +// copy of the corresponding data. The copy is owned by the caller and |
| 123 | +// can be modified and held indefinitely. The copy can be avoided by |
| 124 | +// using an argument of type *RawBytes instead; see the documentation |
| 125 | +// for RawBytes for restrictions on its use. |
| 126 | +// |
| 127 | +// If an argument has type *interface{}, Scan copies the value |
| 128 | +// provided by the underlying driver without conversion. When scanning |
| 129 | +// from a source value of type []byte to *interface{}, a copy of the |
| 130 | +// slice is made and the caller owns the result. |
| 131 | +// |
| 132 | +// Source values of type time.Time may be scanned into values of type |
| 133 | +// *time.Time, *interface{}, *string, or *[]byte. When converting to |
| 134 | +// the latter two, time.RFC3339Nano is used. |
| 135 | +// |
| 136 | +// Source values of type bool may be scanned into types *bool, |
| 137 | +// *interface{}, *string, *[]byte, or *RawBytes. |
| 138 | +// |
| 139 | +// For scanning into *bool, the source may be true, false, 1, 0, or |
| 140 | +// string inputs parseable by strconv.ParseBool. |
| 141 | +// |
| 142 | +// Scan can also convert a cursor returned from a query, such as |
| 143 | +// "select cursor(select * from my_table) from dual", into a |
| 144 | +// *Rows value that can itself be scanned from. The parent |
| 145 | +// select query will close any cursor *Rows if the parent *Rows is closed. |
| 146 | +func (rs *Rows) Scan(dest ...interface{}) error { |
| 147 | + err := rs.rows.Scan(dest...) |
| 148 | + if rs.ctx.Err() != nil { |
| 149 | + kill(rs.killerPool, rs.connectionID) |
| 150 | + } |
| 151 | + return err |
| 152 | +} |
0 commit comments