You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Clint Kelly <cl...@gmail.com> on 2014/02/28 04:33:27 UTC

Naming variables in a prepared statement in the DataStax Java driver

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

Re: Naming variables in a prepared statement in the DataStax Java driver

Posted by Clint Kelly <cl...@gmail.com>.
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 <cl...@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
>