MCPcopy Create free account
hub / github.com/PostHog/duckgres / ExecuteQuery

Function ExecuteQuery

tests/integration/compare.go:71–117  ·  view source on GitHub ↗

ExecuteQuery executes a query and returns the result

(db *sql.DB, query string)

Source from the content-addressed store, hash-verified

69
70// ExecuteQuery executes a query and returns the result
71func ExecuteQuery(db *sql.DB, query string) (*QueryResult, error) {
72 rows, err := db.Query(query)
73 if err != nil {
74 return &QueryResult{Error: err}, nil
75 }
76 defer func() { _ = rows.Close() }()
77
78 columns, err := rows.Columns()
79 if err != nil {
80 return nil, fmt.Errorf("failed to get columns: %w", err)
81 }
82
83 var result [][]interface{}
84 for rows.Next() {
85 // Use sql.RawBytes to completely avoid driver parsing issues
86 rawValues := make([]sql.RawBytes, len(columns))
87 valuePtrs := make([]interface{}, len(columns))
88 for i := range rawValues {
89 valuePtrs[i] = &rawValues[i]
90 }
91
92 if err := rows.Scan(valuePtrs...); err != nil {
93 return nil, fmt.Errorf("failed to scan row: %w", err)
94 }
95
96 // Convert RawBytes to appropriate types
97 values := make([]interface{}, len(columns))
98 for i := range rawValues {
99 if rawValues[i] == nil {
100 values[i] = nil
101 } else {
102 values[i] = parseValue(string(rawValues[i]))
103 }
104 }
105
106 result = append(result, values)
107 }
108
109 if err := rows.Err(); err != nil {
110 return nil, fmt.Errorf("row iteration error: %w", err)
111 }
112
113 return &QueryResult{
114 Columns: columns,
115 Rows: result,
116 }, nil
117}
118
119// parseValue attempts to parse a string value into an appropriate Go type
120func parseValue(s string) interface{} {

Callers 11

CompareQueriesFunction · 0.85
TestDMLInsertFunction · 0.85
TestDMLInsertOnConflictFunction · 0.85
TestDMLUpdateFunction · 0.85
TestDMLUpdateFromJoinFunction · 0.85
TestDMLDeleteUsingFunction · 0.85
TestDMLWithCTEFunction · 0.85
runQueryTestFunction · 0.85
TestDDLViewsFunction · 0.85
TestDDLTruncateFunction · 0.85
TestCopyFromStdinFunction · 0.85

Calls 7

parseValueFunction · 0.85
QueryMethod · 0.65
CloseMethod · 0.65
ColumnsMethod · 0.65
NextMethod · 0.65
ScanMethod · 0.65
ErrMethod · 0.65

Tested by 10

TestDMLInsertFunction · 0.68
TestDMLInsertOnConflictFunction · 0.68
TestDMLUpdateFunction · 0.68
TestDMLUpdateFromJoinFunction · 0.68
TestDMLDeleteUsingFunction · 0.68
TestDMLWithCTEFunction · 0.68
runQueryTestFunction · 0.68
TestDDLViewsFunction · 0.68
TestDDLTruncateFunction · 0.68
TestCopyFromStdinFunction · 0.68