Google Cloud Spanner C++ Client 2.8.0
A C++ Client Library for Google Cloud Spanner
No Matches
Public Types | Public Member Functions | Friends | List of all members
google::cloud::spanner::SqlStatement Class Reference

Represents a potentially parameterized SQL statement. More...

#include <google/cloud/spanner/sql_statement.h>

Public Types

using ParamType = std::unordered_map< std::string, Value >
 Type alias for parameter collection. More...

Public Member Functions

 SqlStatement ()=default
 SqlStatement (std::string statement)
 Constructs a SqlStatement without parameters. More...
 SqlStatement (std::string statement, ParamType params)
 Constructs a SqlStatement with specified parameters. More...
 SqlStatement (SqlStatement const &)=default
 Copy and move. More...
 SqlStatement (SqlStatement &&)=default
SqlStatementoperator= (SqlStatement const &)=default
SqlStatementoperator= (SqlStatement &&)=default
std::string const & sql () const
 Returns the SQL statement. More...
ParamType const & params () const
 Returns the collection of parameters. More...
std::vector< std::string > ParameterNames () const
 Returns the names of all the parameters. More...
google::cloud::StatusOr< ValueGetParameter (std::string const &parameter_name) const
 Returns the value of the requested parameter. More...


bool operator== (SqlStatement const &a, SqlStatement const &b)
bool operator!= (SqlStatement const &a, SqlStatement const &b)
std::ostream & operator<< (std::ostream &os, SqlStatement const &stmt)
 Outputs a string representation of the given stmt to the given os. More...

Detailed Description

Represents a potentially parameterized SQL statement.

Details on case sensitivity for SQL statements and string values can be found here: Case Sensitivity

SqlStatement equality comparisons are case-sensitive.

Parameter placeholders are specified by @<param name> in the SQL string. Values for parameters are a collection of std::pair<std::string const, google::cloud:spanner::Value>.

// Cloud Spanner STRUCT<> types are represented by std::tuple<...>. The
// following represents a STRUCT<> with two unnamed STRING fields.
using NameType = std::tuple<std::string, std::string>;
auto singer_info = NameType{"Elena", "Campbell"};
auto rows = client.ExecuteQuery(spanner::SqlStatement(
"SELECT SingerId FROM Singers WHERE (FirstName, LastName) = @name",
{{"name", spanner::Value(singer_info)}}));
Represents a potentially parameterized SQL statement.
Definition: sql_statement.h:51
The Value class represents a type-safe, nullable Spanner value.
Definition: value.h:170

Member Typedef Documentation

◆ ParamType

using google::cloud::spanner::SqlStatement::ParamType = std::unordered_map<std::string, Value>

Type alias for parameter collection.

Constructor & Destructor Documentation

◆ SqlStatement() [1/5]

google::cloud::spanner::SqlStatement::SqlStatement ( )

◆ SqlStatement() [2/5]

google::cloud::spanner::SqlStatement::SqlStatement ( std::string  statement)

Constructs a SqlStatement without parameters.

◆ SqlStatement() [3/5]

google::cloud::spanner::SqlStatement::SqlStatement ( std::string  statement,
ParamType  params 

Constructs a SqlStatement with specified parameters.

◆ SqlStatement() [4/5]

google::cloud::spanner::SqlStatement::SqlStatement ( SqlStatement const &  )

Copy and move.

◆ SqlStatement() [5/5]

google::cloud::spanner::SqlStatement::SqlStatement ( SqlStatement &&  )

Member Function Documentation

◆ GetParameter()

google::cloud::StatusOr< Value > google::cloud::spanner::SqlStatement::GetParameter ( std::string const &  parameter_name) const

Returns the value of the requested parameter.

parameter_namename of requested parameter.
StatusCode::kNotFound returned for invalid names.

◆ operator=() [1/2]

SqlStatement & google::cloud::spanner::SqlStatement::operator= ( SqlStatement &&  )

◆ operator=() [2/2]

SqlStatement & google::cloud::spanner::SqlStatement::operator= ( SqlStatement const &  )

◆ ParameterNames()

std::vector< std::string > google::cloud::spanner::SqlStatement::ParameterNames ( ) const

Returns the names of all the parameters.

◆ params()

ParamType const & google::cloud::spanner::SqlStatement::params ( ) const

Returns the collection of parameters.

If no parameters were specified, the container will be empty.

◆ sql()

std::string const & google::cloud::spanner::SqlStatement::sql ( ) const

Returns the SQL statement.

No parameter substitution is performed in the statement string.

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( SqlStatement const &  a,
SqlStatement const &  b 

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
SqlStatement const &  stmt 

Outputs a string representation of the given stmt to the given os.

This is intended for debugging and human consumption only, not machine consumption, as the output format may change without notice.

◆ operator==

bool operator== ( SqlStatement const &  a,
SqlStatement const &  b