-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecution.go
More file actions
95 lines (88 loc) · 2.75 KB
/
Copy pathexecution.go
File metadata and controls
95 lines (88 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"database/sql"
"fmt"
"log"
"time"
. "github.com/cube2222/octosql/execution"
"github.com/cube2222/octosql/octosql"
"github.com/cube2222/octosql/physical"
)
type DatasourceExecuting struct {
fields []physical.SchemaField
table string
placeholderExprTypes []octosql.Type
placeholderExprs []Expression
db *sql.DB
stmt *sql.Stmt
}
func (d *DatasourceExecuting) Run(ctx ExecutionContext, produce ProduceFn, metaSend MetaSendFn) error {
placeholderValues := make([]interface{}, len(d.placeholderExprs))
for i := range d.placeholderExprs {
value, err := d.placeholderExprs[i].Evaluate(ctx)
if err != nil {
return fmt.Errorf("couldn't evaluate pushed-down predicate placeholder expression: %w", err)
}
// TODO: Use internal function for this.
placeholderValues[i] = value.ToRawGoValue(d.placeholderExprTypes[i])
}
rows, err := d.stmt.QueryContext(ctx, placeholderValues...)
if err != nil {
return fmt.Errorf("couldn't execute database query: %w", err)
}
for rows.Next() {
values := make([]interface{}, len(d.fields))
references := make([]interface{}, len(d.fields))
for i := range references {
references[i] = &values[i]
}
err := rows.Scan(references...)
if err != nil {
return fmt.Errorf("couldn't get row values: %w", err)
}
recordValues := make([]octosql.Value, len(values))
for i, value := range values {
switch value := value.(type) {
case int:
recordValues[i] = octosql.NewInt(value)
case int8:
recordValues[i] = octosql.NewInt(int(value))
case int16:
recordValues[i] = octosql.NewInt(int(value))
case int32:
recordValues[i] = octosql.NewInt(int(value))
case int64:
recordValues[i] = octosql.NewInt(int(value))
case uint8:
recordValues[i] = octosql.NewInt(int(value))
case uint16:
recordValues[i] = octosql.NewInt(int(value))
case uint32:
recordValues[i] = octosql.NewInt(int(value))
case uint64:
recordValues[i] = octosql.NewInt(int(value))
case bool:
recordValues[i] = octosql.NewBoolean(value)
case float32:
recordValues[i] = octosql.NewFloat(float64(value))
case float64:
recordValues[i] = octosql.NewFloat(value)
case string:
recordValues[i] = octosql.NewString(value)
case time.Time:
recordValues[i] = octosql.NewTime(value)
case nil:
recordValues[i] = octosql.NewNull()
case []byte:
recordValues[i] = octosql.NewString(string(value))
default:
log.Printf("unknown mysql value type, setting null: %T, %+v", value, value)
recordValues[i] = octosql.NewNull()
}
}
if err := produce(ProduceFromExecutionContext(ctx), NewRecord(recordValues, false, time.Time{})); err != nil {
return fmt.Errorf("couldn't produce record: %w", err)
}
}
return nil
}