Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekwuno authored Feb 26, 2025
1 parent 1499ebb commit c4ffb6a
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions src/content/doc-sdk-golang/methods/raw-query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,62 @@ func QueryRaw(db *DB, queries *[]QueryStmt) error
### Example usage

```go
package main

import (
"fmt"
"log"
)

// QueryStmt represents a database query.
type QueryStmt struct {
SQL string
Vars map[string]interface{}
Result QueryResult
unmarshaler interface{}
}

// QueryResult is a simple type representing a query result.
type QueryResult struct {
Data string
}

// DB simulates a database connection.
type DB struct {
con Connection
}

// Connection simulates a connection that can send queries.
type Connection struct{}

// RPCResponse is a generic response wrapper.
type RPCResponse[T any] struct {
Result *T
}

// Send simulates sending the combined query to the database.
// For this example, it returns a dummy result for each query.
func (c *Connection) Send(res *RPCResponse[[]QueryResult], method, query string, params map[string]interface{}) error {
// For simplicity, we assume one result per query.
dummyResults := []QueryResult{
{Data: "Result for query: SELECT * FROM person WHERE id = 1"},
}
res.Result = &dummyResults
return nil
}

// GetUnmarshaler returns a dummy unmarshaler.
func (c *Connection) GetUnmarshaler() interface{} {
return nil
}

// QueryRaw executes the queries by concatenating them, sending them to the DB,
// and assigning each query its corresponding result.
func QueryRaw(db *DB, queries *[]QueryStmt) error {
preparedQuery := ""
parameters := map[string]interface{}{}

for i := 0; i < len(*queries); i++ {
// append query
preparedQuery += fmt.Sprintf("%s;", (*queries)[i].SQL)
for k, v := range (*queries)[i].Vars {
parameters[k] = v
Expand All @@ -64,17 +115,40 @@ func QueryRaw(db *DB, queries *[]QueryStmt) error {
return fmt.Errorf("no query to run")
}

var res connection.RPCResponse[[]QueryResult[cbor.RawMessage]]
var res RPCResponse[[]QueryResult]
if err := db.con.Send(&res, "query", preparedQuery, parameters); err != nil {
return err
}

for i := 0; i < len(*queries); i++ {
// assign results
(*queries)[i].Result = (*res.Result)[i]
(*queries)[i].unmarshaler = db.con.GetUnmarshaler()
}

return nil
}

func main() {
// Create a dummy database instance.
db := &DB{
con: Connection{},
}

// Define the query statement(s).
queries := []QueryStmt{
{
SQL: "SELECT * FROM person WHERE id = $id",
Vars: map[string]interface{}{"id": 1},
},
}

// Perform the query operation.
if err := QueryRaw(db, &queries); err != nil {
log.Fatalf("QueryRaw failed: %v", err)
}

// Print the query result.
fmt.Printf("Query result: %+v\n", queries[0].Result)
}

```

0 comments on commit c4ffb6a

Please sign in to comment.