Coder Social home page Coder Social logo

How to pass values to a query about gocqlx HOT 6 CLOSED

gronka avatar gronka commented on June 5, 2024
How to pass values to a query

from gocqlx.

Comments (6)

mykaul avatar mykaul commented on June 5, 2024

Are you asking specifically about Go? Have you looked at the examples @ https://github.com/scylladb/gocql (root dir, there are several ones) ?

from gocqlx.

mykaul avatar mykaul commented on June 5, 2024

Are you asking specifically about Go? Have you looked at the examples @ https://github.com/scylladb/gocql (root dir, there are several ones) ?

Or the examples in this repo?

from gocqlx.

gronka avatar gronka commented on June 5, 2024

Are you asking specifically about Go? Have you looked at the examples @ https://github.com/scylladb/gocql (root dir, there are several ones) ?

gocqlx. I've used gocql a lot; I'd like to handwrite fewer queries. I see lots of examples in the tests, but it's taking me a long time to piece them together into a select * from users where email='[email protected]' statement - and after that where user_id={uuid_var}. I see strings being passed around, and byte arrays being used instead of gocql.UUID, so I expect that to take a bit of time to handle as well.

  • I added some lines to demonstrate initialization of Csm in my code

from gocqlx.

gronka avatar gronka commented on June 5, 2024

Great! I was able to figure my mistakes from this issue: #109

My solution:

stmt, names := qb.Select("aim.surfers").Where(qb.Eq("surfer_uuid")).Limit(1).ToCql()
res := uy.Csm.Query(stmt, names).Bind(in.SurferUuid)
if err := res.GetRelease(&out); err != nil {                                  
  uy.Error(err)                                                               
}  

I appreciate the library greatly. My thoughts as a newcomer: the docline for GetRelease did not convey what it does to me; maybe it's using driver-side terminology.
GetRelease calls Get and releases the query, a released query cannot be reused.

Also Queryx.Names is a similar example:
Queryx is a wrapper around gocql.Query which adds struct binding capabilities.

from gocqlx.

mykaul avatar mykaul commented on June 5, 2024

@avelanarius - anything we can do to improve the documentation around GetRelease() ?

from gocqlx.

gronka avatar gronka commented on June 5, 2024

Just to share - my original interpretations that tripped me up:

  1. I thought the names variable was used to bind values to a query. Because the querybuilder returns it, it seemed like it was used to build the query - you can see I was trying to cram prepared query parameters into names lol
  2. I thought Bind() was used to bind values to the output struct (as opposed to binding to the query). The doc for Bind() seems fine, but I never thought to look it up. I assumed it was a wrapper for gocql.Iter.MapScan/StructScan
  3. I assumed GetRelease() was a simple cleanup function. What tripped me up here is the doc for Get() says scans first row into a destination, and it simply didn't click in my brain that destination meant struct to unpack the query result. The language was too abstracted for me. It does mention Iter.StructScan, but it just didn't click for me; it's been a long time since I've had to deal with Iter directly

I'm still not sure what names does - at this point I guess BindMap() uses it, but I'm not sure if anything else does. There were no compiler errors since many functions accept ...string or ...interface{} (probably unavoidable)

imo, an examples.go file with some comments might be best/simplest. Compile time feedback could be given if it's possible to create a type Names ...

from gocqlx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.