Skip to content

Instantly share code, notes, and snippets.

@yvasiyarov
Created April 1, 2014 11:09
Show Gist options
  • Save yvasiyarov/9911956 to your computer and use it in GitHub Desktop.
Save yvasiyarov/9911956 to your computer and use it in GitHub Desktop.
rows.Scan() optimal usage
//Prepare buffers for reading: one time before read first chunk
treader.rawBuffer = make([]sql.RawBytes, len(treader.columns))
// rows.Scan wants '[]interface{}' as an argument, so we must copy the
// references into such a slice
// See http://code.google.com/p/go-wiki/wiki/InterfaceSlice for details
treader.scanCallArgs = make([]interface{}, len(treader.rawBuffer))
for i := range treader.rawBuffer {
treader.scanCallArgs[i] = &treader.rawBuffer[i]
}
//Read one chunk of data
csvRows := make([][]string, 0, chunkSize)
lastRowID := 0
for rows.Next() {
err := rows.Scan(treader.scanCallArgs...)
if err != nil {
panic(fmt.Errorf(string("Can not parse query result %s "), err.Error()))
}
csvCurrentRow := make([]string, 0, len(treader.scanCallArgs))
for i, col := range treader.rawBuffer {
if i == treader.primaryKeyIndex {
strCol := string(col)
if _, ok := treader.blacklistedColumns[i]; !ok {
csvCurrentRow = append(csvCurrentRow, strCol)
}
if pk, err := strconv.Atoi(strCol); err != nil {
panic(fmt.Errorf(string("Primary key must be int: %s"), strCol))
} else {
lastRowID = pk
}
} else if _, ok := treader.blacklistedColumns[i]; ok {
//column is blacklisted
continue
} else if col == nil {
csvCurrentRow = append(csvCurrentRow, "")
} else {
csvCurrentRow = append(csvCurrentRow, string(col))
}
}
csvCurrentRow = treader.fileInfo.mapper(csvCurrentRow, treader.fieldMap)
csvRows = append(csvRows, csvCurrentRow)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment