incubator-cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Clint Kelly <clint.ke...@gmail.com>
Subject Re: Naming variables in a prepared statement in the DataStax Java driver
Date Fri, 28 Feb 2014 03:44:31 GMT
Ah never mind, I see, currently you can refer to the ?'s by name by using
the name of the column to which the ? refers.  And this works as long as
each column is present only one in the statement.

Sorry for the extra list traffic!


On Thu, Feb 27, 2014 at 7:33 PM, Clint Kelly <clint.kelly@gmail.com> wrote:

> Folks,
>
> Is there a way to name the variables in a prepared statement when using
> the DataStax Java driver?
>
> For example, instead of doing:
>
> ByteBuffer byteBuffer = ... // some application logic
> String query = "SELECT * FROM foo WHERE bar = ?";
> PreparedStatement preparedStatement = session.prepare(query);
> BoundStatement boundStatement = preparedStatement.bind(byteBuffer);
>
> I'd like to be able to be able to name the fields indicated by the ?s,
> e.g.,:
>
> ByteBuffer byteBuffer = ... // some application logic
> String query = "SELECT * FROM foo WHERE bar = ?<bar>"; // I just made up
> this syntax
> PreparedStatement preparedStatement = session.prepare(query);
> BoundStatement boundStatement = preparedStatement.bind("bar", byteBuffer);
>
> Looking at the DataStax API docs, it seems like there should be a way to
> be able to do this, but I can't tell for sure.
>
> This would be particularly useful when I have some application logic in
> which I have very long queries with lots of bound variables and then
> sometimes extend them with different clauses.  Right now this code gets
> very verbose, because I cannot figure out how to break up my "bind"
> statements to bind different values to a bound statement in separate
> statements.  In other words, I'd like to be able to do something like:
>
>     BoundStatement boundStatement = // Create from a prepared statement
>     boundStatement = boundStatement.bind( ... ); // Bind all of the values
> that I use in every flavor of this query
>     if ( ... )  {
>          boundStatement = boundStatement.bind("some field", someVal);
>     else {
>          boundStatement = boundStatement.bind("other field", otherVal);
>
> Thanks!
>
> Best regards,
> Clint
>

Mime
View raw message