Skip to content

Commit 6eeda90

Browse files
committed
Merge pull request #135 from go-sql-driver/column-cache
stmt: Cache Columns
2 parents f41250e + c4a3fc1 commit 6eeda90

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Changes:
99
- Changed the copyright header to include all contributors
1010
- Optimized the buffer for reading
1111
- Use the buffer also for writing. This results in zero allocations (by the driver) for most queries
12+
- stmt.Query now caches column metadata
1213
- Improved the LOAD INFILE documentation
1314
- The driver struct is now exported to make the driver directly accessible
1415
- Refactored the driver tests

connection.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
136136
columnCount, err := stmt.readPrepareResultPacket()
137137
if err == nil {
138138
if stmt.paramCount > 0 {
139-
stmt.params, err = mc.readColumns(stmt.paramCount)
140-
if err != nil {
139+
if err = mc.readUntilEOF(); err != nil {
141140
return nil, err
142141
}
143142
}

statement.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type mysqlStmt struct {
1616
mc *mysqlConn
1717
id uint32
1818
paramCount int
19-
params []mysqlField
19+
columns []mysqlField // cached from the first query
2020
}
2121

2222
func (stmt *mysqlStmt) Close() error {
@@ -88,7 +88,14 @@ func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
8888

8989
if resLen > 0 {
9090
// Columns
91-
rows.columns, err = mc.readColumns(resLen)
91+
// If not cached, read them and cache them
92+
if stmt.columns == nil {
93+
rows.columns, err = mc.readColumns(resLen)
94+
stmt.columns = rows.columns
95+
} else {
96+
rows.columns = stmt.columns
97+
err = mc.readUntilEOF()
98+
}
9299
}
93100

94101
return rows, err

0 commit comments

Comments
 (0)