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

Function TestNamedQuery

sqlx_test.go:590–819  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

588}
589
590func TestNamedQuery(t *testing.T) {
591 var schema = Schema{
592 create: `
593 CREATE TABLE place (
594 id integer PRIMARY KEY,
595 name text NULL
596 );
597 CREATE TABLE person (
598 first_name text NULL,
599 last_name text NULL,
600 email text NULL
601 );
602 CREATE TABLE placeperson (
603 first_name text NULL,
604 last_name text NULL,
605 email text NULL,
606 place_id integer NULL
607 );
608 CREATE TABLE jsperson (
609 "FIRST" text NULL,
610 last_name text NULL,
611 "EMAIL" text NULL
612 );`,
613 drop: `
614 drop table person;
615 drop table jsperson;
616 drop table place;
617 drop table placeperson;
618 `,
619 }
620
621 RunWithSchema(schema, t, func(db *DB, t *testing.T, now string) {
622 type Person struct {
623 FirstName sql.NullString `db:"first_name"`
624 LastName sql.NullString `db:"last_name"`
625 Email sql.NullString
626 }
627
628 p := Person{
629 FirstName: sql.NullString{String: "ben", Valid: true},
630 LastName: sql.NullString{String: "doe", Valid: true},
631 Email: sql.NullString{String: "ben@doe.com", Valid: true},
632 }
633
634 q1 := `INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)`
635 _, err := db.NamedExec(q1, p)
636 if err != nil {
637 log.Fatal(err)
638 }
639
640 p2 := &Person{}
641 rows, err := db.NamedQuery("SELECT * FROM person WHERE first_name=:first_name", p)
642 if err != nil {
643 log.Fatal(err)
644 }
645 for rows.Next() {
646 err = rows.StructScan(p2)
647 if err != nil {

Callers

nothing calls this directly

Calls 11

NewMapperFuncFunction · 0.92
RunWithSchemaFunction · 0.85
NextMethod · 0.80
ErrorMethod · 0.80
ErrorfMethod · 0.80
DriverNameMethod · 0.65
QueryxMethod · 0.65
NamedExecMethod · 0.45
NamedQueryMethod · 0.45
StructScanMethod · 0.45
PrepareNamedMethod · 0.45

Tested by

no test coverage detected