github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/ext/array/array_test.go (about) 1 package array_test 2 3 import ( 4 "fmt" 5 "log" 6 "math" 7 "reflect" 8 "testing" 9 10 "github.com/ncruces/go-sqlite3" 11 "github.com/ncruces/go-sqlite3/driver" 12 _ "github.com/ncruces/go-sqlite3/embed" 13 "github.com/ncruces/go-sqlite3/ext/array" 14 _ "github.com/ncruces/go-sqlite3/tests/testcfg" 15 ) 16 17 func Example_driver() { 18 db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error { 19 array.Register(c) 20 return nil 21 }) 22 if err != nil { 23 log.Fatal(err) 24 } 25 defer db.Close() 26 27 rows, err := db.Query(` 28 SELECT name 29 FROM pragma_function_list 30 WHERE name like 'geopoly%' AND narg IN array(?)`, 31 sqlite3.Pointer([]int{2, 3, 4})) 32 if err != nil { 33 log.Fatal(err) 34 } 35 defer rows.Close() 36 37 for rows.Next() { 38 var name string 39 err := rows.Scan(&name) 40 if err != nil { 41 log.Fatal(err) 42 } 43 fmt.Printf("%s\n", name) 44 } 45 if err := rows.Err(); err != nil { 46 log.Fatal(err) 47 } 48 // Unordered output: 49 // geopoly_regular 50 // geopoly_overlap 51 // geopoly_contains_point 52 // geopoly_within 53 } 54 55 func Example() { 56 db, err := sqlite3.Open(":memory:") 57 if err != nil { 58 log.Fatal(err) 59 } 60 defer db.Close() 61 62 array.Register(db) 63 64 stmt, _, err := db.Prepare(` 65 SELECT name 66 FROM pragma_function_list 67 WHERE name like 'geopoly%' AND narg IN array(?)`) 68 if err != nil { 69 log.Fatal(err) 70 } 71 defer stmt.Close() 72 73 err = stmt.BindPointer(1, [...]int{2, 3, 4}) 74 if err != nil { 75 log.Fatal(err) 76 } 77 78 for stmt.Step() { 79 fmt.Printf("%s\n", stmt.ColumnText(0)) 80 } 81 if err := stmt.Err(); err != nil { 82 log.Fatal(err) 83 } 84 // Unordered output: 85 // geopoly_regular 86 // geopoly_overlap 87 // geopoly_contains_point 88 // geopoly_within 89 } 90 91 func Test_cursor_Column(t *testing.T) { 92 t.Parallel() 93 94 db, err := driver.Open(":memory:", func(c *sqlite3.Conn) error { 95 array.Register(c) 96 return nil 97 }) 98 if err != nil { 99 t.Fatal(err) 100 } 101 defer db.Close() 102 103 rows, err := db.Query(` 104 SELECT rowid, value FROM array(?)`, 105 sqlite3.Pointer(&[...]any{nil, true, 1, uint(2), math.Pi, "text", []byte{1, 2, 3}})) 106 if err != nil { 107 t.Fatal(err) 108 } 109 defer rows.Close() 110 111 want := []string{"nil", "int64", "int64", "int64", "float64", "string", "[]uint8"} 112 113 for rows.Next() { 114 var id, val any 115 err := rows.Scan(&id, &val) 116 if err != nil { 117 t.Fatal(err) 118 } 119 if want := want[0]; val == nil { 120 if want != "nil" { 121 t.Errorf("got nil, want %s", want) 122 } 123 } else if got := reflect.TypeOf(val).String(); got != want { 124 t.Errorf("got %s, want %s", got, want) 125 } 126 want = want[1:] 127 } 128 if err := rows.Err(); err != nil { 129 log.Fatal(err) 130 } 131 } 132 133 func Test_array_errors(t *testing.T) { 134 t.Parallel() 135 136 db, err := sqlite3.Open(":memory:") 137 if err != nil { 138 t.Fatal(err) 139 } 140 defer db.Close() 141 142 array.Register(db) 143 144 err = db.Exec(`SELECT * FROM array()`) 145 if err == nil { 146 t.Fatal("want error") 147 } else { 148 t.Log(err) 149 } 150 151 err = db.Exec(`SELECT * FROM array(?)`) 152 if err == nil { 153 t.Fatal("want error") 154 } else { 155 t.Log(err) 156 } 157 }