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