Skip to content

Commit

Permalink
Update docs and examples
Browse files Browse the repository at this point in the history
Signed-off-by: Levko Kravets <levko.ne@gmail.com>
  • Loading branch information
kravets-levko committed Oct 2, 2024
1 parent cc60b2f commit 5d8a59c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 30 deletions.
8 changes: 7 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,17 @@ Use the driverctx package under driverctx/ctx.go to add callbacks to the query c
Passing parameters to a query is supported when run against servers with version DBR 14.1.
// Named parameters:
p := dbsql.Parameter{Name: "p_bool", Value: true},
rows, err1 := db.QueryContext(ctx, `select * from sometable where condition=:p_bool`,dbsql.Parameter{Name: "p_bool", Value: true})
rows, err := db.QueryContext(ctx, `select * from sometable where condition=:p_bool`,dbsql.Parameter{Name: "p_bool", Value: true})
// Positional parameters - both `dbsql.Parameter` and plain values can be used:
rows, err := db.Query(`select *, ? from sometable where field=?`,dbsql.Parameter{Value: "123.456"}, "another parameter")
For complex types, you can specify the SQL type using the dbsql.Parameter type field. If this field is set, the value field MUST be set to a string.
Please note that named and positional parameters cannot be used together in the single query.
# Staging Ingestion
The Go driver now supports staging operations. In order to use a staging operation, you first must update the context with a list of folders that you are allowing the driver to access.
Expand Down
99 changes: 70 additions & 29 deletions examples/parameters/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"database/sql"
"fmt"
"log"
Expand All @@ -12,6 +11,74 @@ import (
"github.com/joho/godotenv"
)

func queryWithNamedParameters(db *sql.DB) {
var p_bool bool
var p_int int
var p_double float64
var p_float float32
var p_date string

err := db.QueryRow(`
SELECT
:p_bool AS col_bool,
:p_int AS col_int,
:p_double AS col_double,
:p_float AS col_float,
:p_date AS col_date
`,
dbsql.Parameter{Name: "p_bool", Value: true},
dbsql.Parameter{Name: "p_int", Value: int(1234)},
dbsql.Parameter{Name: "p_double", Type: dbsql.SqlDouble, Value: "3.14"},
dbsql.Parameter{Name: "p_float", Type: dbsql.SqlFloat, Value: "3.14"},
dbsql.Parameter{Name: "p_date", Type: dbsql.SqlDate, Value: "2017-07-23 00:00:00"},
).Scan(&p_bool, &p_int, &p_double, &p_float, &p_date)

if err != nil {
if err == sql.ErrNoRows {
fmt.Println("not found")
return
} else {
fmt.Printf("err: %v\n", err)
}
} else {
fmt.Println(p_bool, p_int, p_double, p_float, p_date)
}
}

func queryWithPositionalParameters(db *sql.DB) {
var p_bool bool
var p_int int
var p_double float64
var p_float float32
var p_date string

err := db.QueryRow(`
SELECT
:p_bool AS col_bool,
:p_int AS col_int,
:p_double AS col_double,
:p_float AS col_float,
:p_date AS col_date
`,
true,
int(1234),
"3.14",
dbsql.Parameter{Type: dbsql.SqlFloat, Value: "3.14"},
dbsql.Parameter{Type: dbsql.SqlDate, Value: "2017-07-23 00:00:00"},
).Scan(&p_bool, &p_int, &p_double, &p_float, &p_date)

if err != nil {
if err == sql.ErrNoRows {
fmt.Println("not found")
return
} else {
fmt.Printf("err: %v\n", err)
}
} else {
fmt.Println(p_bool, p_int, p_double, p_float, p_date)
}
}

func main() {
// Opening a driver typically will not attempt to connect to the database.
err := godotenv.Load()
Expand All @@ -36,33 +103,7 @@ func main() {
}
db := sql.OpenDB(connector)
defer db.Close()
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
// defer cancel()
ctx := context.Background()
var p_bool bool
var p_int int
var p_double float64
var p_float float32
var p_date string
err1 := db.QueryRowContext(ctx, `SELECT
:p_bool AS col_bool,
:p_int AS col_int,
:p_double AS col_double,
:p_float AS col_float,
:p_date AS col_date`,
dbsql.Parameter{Name: "p_bool", Value: true},
dbsql.Parameter{Name: "p_int", Value: int(1234)},
dbsql.Parameter{Name: "p_double", Type: dbsql.SqlDouble, Value: "3.14"},
dbsql.Parameter{Name: "p_float", Type: dbsql.SqlFloat, Value: "3.14"},
dbsql.Parameter{Name: "p_date", Type: dbsql.SqlDate, Value: "2017-07-23 00:00:00"}).Scan(&p_bool, &p_int, &p_double, &p_float, &p_date)

if err1 != nil {
if err1 == sql.ErrNoRows {
fmt.Println("not found")
return
} else {
fmt.Printf("err: %v\n", err1)
}
}

queryWithNamedParameters(db)
queryWithPositionalParameters(db)
}

0 comments on commit 5d8a59c

Please sign in to comment.