MCPcopy Create free account
hub / github.com/DATA-DOG/go-sqlmock / ExampleRows_rawBytes

Function ExampleRows_rawBytes

rows_test.go:95–139  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

93}
94
95func ExampleRows_rawBytes() {
96 db, mock, err := New()
97 if err != nil {
98 fmt.Println("failed to open sqlmock database:", err)
99 }
100 defer db.Close()
101
102 rows := NewRows([]string{"id", "binary"}).
103 AddRow(1, []byte(`one binary value with some text!`)).
104 AddRow(2, []byte(`two binary value with even more text than the first one`))
105
106 mock.ExpectQuery("SELECT").WillReturnRows(rows)
107
108 rs, _ := db.Query("SELECT")
109 defer rs.Close()
110
111 type scanned struct {
112 id int
113 raw sql.RawBytes
114 }
115 fmt.Println("initial read...")
116 var ss []scanned
117 for rs.Next() {
118 var s scanned
119 rs.Scan(&s.id, &s.raw)
120 ss = append(ss, s)
121 fmt.Println("scanned id:", s.id, "and raw:", string(s.raw))
122 }
123
124 if rs.Err() != nil {
125 fmt.Println("got rows error:", rs.Err())
126 }
127
128 fmt.Println("after reading all...")
129 for _, s := range ss {
130 fmt.Println("scanned id:", s.id, "and raw:", string(s.raw))
131 }
132 // Output:
133 // initial read...
134 // scanned id: 1 and raw: one binary value with some text!
135 // scanned id: 2 and raw: two binary value with even more text than the first one
136 // after reading all...
137 // scanned id: 1 and raw: ☠☠☠ MEMORY OVERWRITTEN ☠
138 // scanned id: 2 and raw: ☠☠☠ MEMORY OVERWRITTEN ☠☠☠ ☠☠☠ MEMORY
139}
140
141func ExampleRows_expectToBeClosed() {
142 db, mock, err := New()

Callers

nothing calls this directly

Calls 9

NewFunction · 0.85
NewRowsFunction · 0.85
AddRowMethod · 0.80
NextMethod · 0.80
ExpectQueryMethod · 0.65
CloseMethod · 0.45
WillReturnRowsMethod · 0.45
QueryMethod · 0.45
ScanMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…