sqliteclidriver: always get columns in the right order

This commit is contained in:
mappu 2024-06-30 13:12:11 +12:00
parent 8d051a14e5
commit 3b17ddd8a4

View File

@ -267,7 +267,7 @@ func (s *SCStmt) Query(args []driver.Value) (driver.Rows, error) {
// We expect some kind of thing on stdout // We expect some kind of thing on stdout
// If something happens on stderr, or to the process, pr will read an error // If something happens on stderr, or to the process, pr will read an error
ret := []map[string]any{} ret := []OrderedKV{}
decoder := json.NewDecoder(pr) decoder := json.NewDecoder(pr)
err = decoder.Decode(&ret) err = decoder.Decode(&ret)
if err != nil { if err != nil {
@ -276,13 +276,11 @@ func (s *SCStmt) Query(args []driver.Value) (driver.Rows, error) {
// Check if this was the data or the sentinel // Check if this was the data or the sentinel
wasSentinel := false wasSentinel := false
if len(ret) > 0 { if len(ret) > 0 && len(ret[0]) > 0 && ret[0][0].Key == sentinelKey {
if val, ok := ret[0][sentinelKey]; ok { if check, ok := ret[0][0].Value.(string); ok && check == sentinelVal {
if check, ok := val.(string); ok && check == sentinelVal { // It was the sentinel
// It was the sentinel wasSentinel = true
wasSentinel = true // Nothing more to parse
// Nothing more to parse
}
} }
} }
@ -312,8 +310,8 @@ func (s *SCStmt) Query(args []driver.Value) (driver.Rows, error) {
var columnNames []string var columnNames []string
if len(ret) > 0 { if len(ret) > 0 {
for k, _ := range ret[0] { for _, cell := range ret[0] {
columnNames = append(columnNames, k) columnNames = append(columnNames, cell.Key)
} }
} }
@ -347,7 +345,7 @@ var _ driver.Result = &SCResult{} // interface assertion
type SCRows struct { type SCRows struct {
idx int idx int
columns []string columns []string
data []map[string]any data []OrderedKV
} }
func (r *SCRows) Columns() []string { func (r *SCRows) Columns() []string {
@ -370,12 +368,9 @@ func (r *SCRows) Next(dest []driver.Value) error {
} }
for i := 0; i < len(dest); i++ { for i := 0; i < len(dest); i++ {
cell, ok := r.data[r.idx][r.columns[i]] cell := r.data[r.idx][i]
if !ok {
return fmt.Errorf("Result row %d is missing column #%d %q, unexpected", r.idx, i, r.columns[i])
}
dest[i] = cell dest[i] = cell.Value
} }
r.idx++ r.idx++