@@ -37,6 +37,7 @@ func TestExample(t *testing.T) {
3737 session .ExecStmt (`DROP KEYSPACE examples` )
3838
3939 basicCreateAndPopulateKeyspace (t , session )
40+ createAndPopulateKeyspaceAllTypes (t , session )
4041 basicReadScyllaVersion (t , session )
4142
4243 datatypesBlob (t , session )
@@ -154,6 +155,170 @@ func basicCreateAndPopulateKeyspace(t *testing.T, session gocqlx.Session) {
154155 }
155156}
156157
158+ // This example shows how to use query builders and table models to build
159+ // queries with all types. It uses "BindStruct" function for parameter binding and "Select"
160+ // function for loading data to a slice.
161+ func createAndPopulateKeyspaceAllTypes (t * testing.T , session gocqlx.Session ) {
162+ err := session .ExecStmt (`CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}` )
163+ if err != nil {
164+ t .Fatal ("create keyspace:" , err )
165+ }
166+
167+ // generated with schemagen
168+ type CheckTypesStruct struct {
169+ AsciI string
170+ BigInt int64
171+ BloB []byte
172+ BooleaN bool
173+ DatE time.Time
174+ DecimaL inf.Dec
175+ DoublE float64
176+ DuratioN gocql.Duration
177+ FloaT float32
178+ Id [16 ]byte
179+ InT int32
180+ IneT string
181+ ListInt []int32
182+ MapIntText map [int32 ]string
183+ SetInt []int32
184+ SmallInt int16
185+ TexT string
186+ TimE time.Duration
187+ TimestamP time.Time
188+ TimeuuiD [16 ]byte
189+ TinyInt int8
190+ VarChar string
191+ VarInt int64
192+ }
193+
194+ err = session .ExecStmt (`CREATE TABLE IF NOT EXISTS examples.check_types (
195+ asci_i ascii,
196+ big_int bigint,
197+ blo_b blob,
198+ boolea_n boolean,
199+ dat_e date,
200+ decima_l decimal,
201+ doubl_e double,
202+ duratio_n duration,
203+ floa_t float,
204+ ine_t inet,
205+ in_t int,
206+ small_int smallint,
207+ tex_t text,
208+ tim_e time,
209+ timestam_p timestamp,
210+ timeuui_d timeuuid,
211+ tiny_int tinyint,
212+ id uuid PRIMARY KEY,
213+ var_char varchar,
214+ var_int varint,
215+ map_int_text map<int, text>,
216+ list_int list<int>,
217+ set_int set<int>)` )
218+ if err != nil {
219+ t .Fatal ("create table:" , err )
220+ }
221+
222+ // generated with schemagen
223+ checkTypesTable := table .New (table.Metadata {
224+ Name : "examples.check_types" ,
225+ Columns : []string {
226+ "asci_i" ,
227+ "big_int" ,
228+ "blo_b" ,
229+ "boolea_n" ,
230+ "dat_e" ,
231+ "decima_l" ,
232+ "doubl_e" ,
233+ "duratio_n" ,
234+ "floa_t" ,
235+ "id" ,
236+ "in_t" ,
237+ "ine_t" ,
238+ "list_int" ,
239+ "map_int_text" ,
240+ "set_int" ,
241+ "small_int" ,
242+ "tex_t" ,
243+ "tim_e" ,
244+ "timestam_p" ,
245+ "timeuui_d" ,
246+ "tiny_int" ,
247+ "var_char" ,
248+ "var_int" ,
249+ },
250+ PartKey : []string {"id" },
251+ SortKey : []string {},
252+ })
253+
254+ // Insert song using query builder.
255+ insertCheckTypes := qb .Insert ("examples.check_types" ).
256+ Columns ("asci_i" , "big_int" , "blo_b" , "boolea_n" , "dat_e" , "decima_l" , "doubl_e" , "duratio_n" , "floa_t" , "ine_t" , "in_t" , "small_int" , "tex_t" , "tim_e" , "timestam_p" , "timeuui_d" , "tiny_int" , "id" , "var_char" , "var_int" , "map_int_text" , "list_int" , "set_int" ).Query (session )
257+
258+ var byteId [16 ]byte
259+ id := []byte ("756716f7-2e54-4715-9f00-91dcbea6cf50" )
260+ copy (byteId [:], id )
261+
262+ date := time .Date (2021 , time .December , 11 , 10 , 23 , 0 , 0 , time .UTC )
263+ var double float64 = 1.2
264+ var float float32 = 1.3
265+ var integer int32 = 123
266+ listInt := []int32 {1 , 2 , 3 }
267+ mapIntStr := map [int32 ]string {
268+ 1 : "a" ,
269+ 2 : "b" ,
270+ }
271+ setInt := []int32 {2 , 4 , 6 }
272+ var smallInt int16 = 12
273+ var tinyInt int8 = 14
274+ var varInt int64 = 20
275+
276+ insertCheckTypes .BindStruct (CheckTypesStruct {
277+ AsciI : "test qscci" ,
278+ BigInt : 9223372036854775806 , //MAXINT64 - 1,
279+ BloB : []byte ("this is blob test" ),
280+ BooleaN : false ,
281+ DatE : date ,
282+ DecimaL : * inf .NewDec (1 , 1 ),
283+ DoublE : double ,
284+ DuratioN : gocql.Duration {Months : 1 , Days : 1 , Nanoseconds : 86400 },
285+ FloaT : float ,
286+ Id : byteId ,
287+ InT : integer ,
288+ IneT : "127.0.0.1" ,
289+ ListInt : listInt ,
290+ MapIntText : mapIntStr ,
291+ SetInt : setInt ,
292+ SmallInt : smallInt ,
293+ TexT : "text example" ,
294+ TimE : 86400000000 ,
295+ TimestamP : date ,
296+ TimeuuiD : gocql .TimeUUID (),
297+ TinyInt : tinyInt ,
298+ VarChar : "test varchar" ,
299+ VarInt : varInt ,
300+ })
301+ if err := insertCheckTypes .ExecRelease (); err != nil {
302+ t .Fatal ("ExecRelease() failed:" , err )
303+ }
304+
305+ // Query and displays data.
306+ queryCheckTypes := checkTypesTable .SelectQuery (session )
307+
308+ queryCheckTypes .BindStruct (& CheckTypesStruct {
309+ Id : byteId ,
310+ })
311+
312+ var items []* CheckTypesStruct
313+ if err := queryCheckTypes .Select (& items ); err != nil {
314+ t .Fatal ("Select() failed:" , err )
315+ }
316+
317+ for _ , i := range items {
318+ t .Logf ("%+v" , * i )
319+ }
320+ }
321+
157322// This example shows how to load a single value using "Get" function.
158323// Get can also work with UDTs and types that implement gocql marshalling functions.
159324func basicReadScyllaVersion (t * testing.T , session gocqlx.Session ) {
0 commit comments