Class SimpleStatement


  • public class SimpleStatement
    extends RegularStatement
    A simple RegularStatement implementation built directly from a query string.
    • Constructor Detail

      • SimpleStatement

        public SimpleStatement​(String query)
        Creates a new SimpleStatement with the provided query string (and no values).
        Parameters:
        query - the query string.
      • SimpleStatement

        public SimpleStatement​(String query,
                               Object... values)
        Creates a new SimpleStatement with the provided query string and values.

        This version of SimpleStatement is useful when you want to execute a query only once (and thus do not want to resort to prepared statement), but do not want to convert all column values to string (typically, if you have blob values, encoding them to a hexadecimal string is not very efficient). In that case, you can provide a query string with bind markers to this constructor along with the values for those bind variables. When executed, the server will prepare the provided, bind the provided values to that prepare statement and execute the resulting statement. Thus,

           session.execute(new SimpleStatement(query, value1, value2, value3));
         
        is functionally equivalent to
           PreparedStatement ps = session.prepare(query);
           session.execute(ps.bind(value1, value2, value3));
         
        except that the former version:
        • Requires only one round-trip to a Cassandra node.
        • Does not left any prepared statement stored in memory (neither client or server side) once it has been executed.

        Note that the types of the values provided to this method will not be validated by the driver as is done by BoundStatement.bind(java.lang.Object...) since query is not parsed (and hence the driver cannot know what those values should be). The codec to serialize each value will be chosen in the codec registry associated with the cluster executing this statement, based on the value's Java type (this is the equivalent to calling CodecRegistry.codecFor(Object)). If too many or too few values are provided, or if a value is not a valid one for the variable it is bound to, an InvalidQueryException will be thrown by Cassandra at execution time. A CodecNotFoundException may be thrown by this constructor however, if the codec registry does not know how to handle one of the values.

        If you have a single value of type Map<String, Object>, you can't call this constructor using the varargs syntax, because the signature collides with SimpleStatement(String, Map). To prevent this, pass an explicit array object:

         new SimpleStatement("...", new Object[]{m});
         
        Parameters:
        query - the query string.
        values - values required for the execution of query.
        Throws:
        IllegalArgumentException - if the number of values is greater than 65535.
      • SimpleStatement

        public SimpleStatement​(String query,
                               Map<String,​Object> values)
        Creates a new SimpleStatement with the provided query string and named values.

        This constructor requires that the query string use named placeholders, for example:

        
         new SimpleStatement("SELECT * FROM users WHERE id = :i", ImmutableMap.<String, Object>of("i", 1));
         
        Make sure that the map is correctly typed Map<String, Object>, otherwise you might accidentally call SimpleStatement(String, Object...) with a positional value of type map.

        The types of the values will be handled the same way as with anonymous placeholders (see SimpleStatement(String, Object...)).

        Simple statements with named values are only supported starting with native protocol v3. With earlier versions, an UnsupportedFeatureException will be thrown at execution time.

        Parameters:
        query - the query string.
        values - named values required for the execution of query.
        Throws:
        IllegalArgumentException - if the number of values is greater than 65535.