Sfoglia il codice sorgente

Merge pull request #135 from go-sql-driver/column-cache

stmt: Cache Columns
Julien Schmidt 12 anni fa
parent
commit
6eeda903b7
3 ha cambiato i file con 11 aggiunte e 4 eliminazioni
  1. 1 0
      CHANGELOG.md
  2. 1 2
      connection.go
  3. 9 2
      statement.go

+ 1 - 0
CHANGELOG.md

@@ -9,6 +9,7 @@ Changes:
   - Changed the copyright header to include all contributors
   - Optimized the buffer for reading
   - Use the buffer also for writing. This results in zero allocations (by the driver) for most queries
+  - stmt.Query now caches column metadata
   - Improved the LOAD INFILE documentation
   - The driver struct is now exported to make the driver directly accessible
   - Refactored the driver tests

+ 1 - 2
connection.go

@@ -136,8 +136,7 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
 	columnCount, err := stmt.readPrepareResultPacket()
 	if err == nil {
 		if stmt.paramCount > 0 {
-			stmt.params, err = mc.readColumns(stmt.paramCount)
-			if err != nil {
+			if err = mc.readUntilEOF(); err != nil {
 				return nil, err
 			}
 		}

+ 9 - 2
statement.go

@@ -16,7 +16,7 @@ type mysqlStmt struct {
 	mc         *mysqlConn
 	id         uint32
 	paramCount int
-	params     []mysqlField
+	columns    []mysqlField // cached from the first query
 }
 
 func (stmt *mysqlStmt) Close() error {
@@ -88,7 +88,14 @@ func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
 
 	if resLen > 0 {
 		// Columns
-		rows.columns, err = mc.readColumns(resLen)
+		// If not cached, read them and cache them
+		if stmt.columns == nil {
+			rows.columns, err = mc.readColumns(resLen)
+			stmt.columns = rows.columns
+		} else {
+			rows.columns = stmt.columns
+			err = mc.readUntilEOF()
+		}
 	}
 
 	return rows, err