How to create prepared statements?

231 views
Skip to first unread message

Sisso

unread,
Jan 28, 2021, 5:34:27 AM1/28/21
to gocql
I am confused about how I can create a prepared statement. The lack of information in documentation a bit of dig into the code looks it is not possible.

Something like:

func initalization(c *gocql.Session) {
   getUserStm = c.Prepare("select * from user wher id = ?")
}

func  GetUser(c *gocql.Session, id string) .. {
   return c.Exec(getUserStm.bind("id"))...
}

How can I create all the prepared statements during app initialization, then just bind variables when I need to call it? It is possible?

I am aware there are some adhoc "cache", but this doesn't tackle the issue of invalid queries are only detected during execution after the approval of the release. For now, the only solution I find is to smoke test all my queries before switch traffic. That is very code intensive and not always possible.

Martin Sucha

unread,
Jan 28, 2021, 6:15:27 AM1/28/21
to Sisso, gocql
Hello,

On Thu, Jan 28, 2021 at 11:34 AM Sisso <sis...@gmail.com> wrote:
I am confused about how I can create a prepared statement. The lack of information in documentation a bit of dig into the code looks it is not possible.
We have a pull request for adding package documentation, it will be merged soon: https://github.com/gocql/gocql/pull/1513
 

Something like:

func initalization(c *gocql.Session) {
   getUserStm = c.Prepare("select * from user wher id = ?")
}

func  GetUser(c *gocql.Session, id string) .. {
   return c.Exec(getUserStm.bind("id"))...
}

How can I create all the prepared statements during app initialization, then just bind variables when I need to call it? It is possible?
There is no exported API for prepared statements. gocql manages it's own internal cache of prepared statements, all data manipulation queries (select/update/insert/delete/batch) are automatically prepared.

> I am aware there are some adhoc "cache", but this doesn't tackle the issue of invalid queries are only detected during execution after the approval of the release. For now, the only solution I find is to smoke test all my queries before switch traffic. That is very code intensive and not always possible.
So your use case would be to verify that the syntax of the statements is correct in advance? At the moment the only way to find out is to actually execute the query (for example in tests, as you mentioned). Note that preparing the statement would not catch all reasons why a query might fail anyway (e.g. if you don't bind the value), so I think having tests in your code is a better option.

Martin


This email, including attached files, may contain confidential information and is intended only for the use of the individual and/or entity to which it is addressed. If you are not the intended recipient, disclosure, copying, use, or distribution of the information included in this email and/or in its attachments is prohibited.
If you have received it by mistake, please do not read, copy or use it, or disclose its contents to others. Please notify the sender that you have received this email by mistake by replying to the email, and then delete the email and any copies and attachments of it. Thank you.
Reply all
Reply to author
Forward
0 new messages