MCPcopy
hub / github.com/jmoiron/sqlx / TestNamedQueryContext

Function TestNamedQueryContext

sqlx_context_test.go:418–647  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

416}
417
418func TestNamedQueryContext(t *testing.T) {
419 var schema = Schema{
420 create: `
421 CREATE TABLE place (
422 id integer PRIMARY KEY,
423 name text NULL
424 );
425 CREATE TABLE person (
426 first_name text NULL,
427 last_name text NULL,
428 email text NULL
429 );
430 CREATE TABLE placeperson (
431 first_name text NULL,
432 last_name text NULL,
433 email text NULL,
434 place_id integer NULL
435 );
436 CREATE TABLE jsperson (
437 "FIRST" text NULL,
438 last_name text NULL,
439 "EMAIL" text NULL
440 );`,
441 drop: `
442 drop table person;
443 drop table jsperson;
444 drop table place;
445 drop table placeperson;
446 `,
447 }
448
449 RunWithSchemaContext(context.Background(), schema, t, func(ctx context.Context, db *DB, t *testing.T) {
450 type Person struct {
451 FirstName sql.NullString `db:"first_name"`
452 LastName sql.NullString `db:"last_name"`
453 Email sql.NullString
454 }
455
456 p := Person{
457 FirstName: sql.NullString{String: "ben", Valid: true},
458 LastName: sql.NullString{String: "doe", Valid: true},
459 Email: sql.NullString{String: "ben@doe.com", Valid: true},
460 }
461
462 q1 := `INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)`
463 _, err := db.NamedExecContext(ctx, q1, p)
464 if err != nil {
465 log.Fatal(err)
466 }
467
468 p2 := &Person{}
469 rows, err := db.NamedQueryContext(ctx, "SELECT * FROM person WHERE first_name=:first_name", p)
470 if err != nil {
471 log.Fatal(err)
472 }
473 for rows.Next() {
474 err = rows.StructScan(p2)
475 if err != nil {

Callers

nothing calls this directly

Calls 11

NewMapperFuncFunction · 0.92
RunWithSchemaContextFunction · 0.85
NamedQueryContextMethod · 0.80
NextMethod · 0.80
ErrorMethod · 0.80
ErrorfMethod · 0.80
DriverNameMethod · 0.65
QueryxContextMethod · 0.65
NamedExecContextMethod · 0.45
StructScanMethod · 0.45
PrepareNamedMethod · 0.45

Tested by

no test coverage detected