Google Cloud Spanner C++ Client 2.13.0
A C++ Client Library for Google Cloud Spanner
Loading...
Searching...
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...
 

Friends

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

Note
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>.

Example
// 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 ( )
default

◆ SqlStatement() [2/5]

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

Constructs a SqlStatement without parameters.

◆ SqlStatement() [3/5]

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

Constructs a SqlStatement with specified parameters.

◆ SqlStatement() [4/5]

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

Copy and move.

◆ SqlStatement() [5/5]

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

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.

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ 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
inline

Returns the collection of parameters.

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

◆ sql()

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

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 
)
friend

◆ operator<<

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

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

Warning
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 
)
friend