Kea 2.7.5
|
MySQL binding used in prepared statements. More...
#include <mysql_binding.h>
Public Member Functions | |
bool | amNull () const |
Checks if the bound value is NULL. | |
std::vector< uint8_t > | getBlob () const |
Returns value held in the binding as blob. | |
std::vector< uint8_t > | getBlobOrDefault (const std::vector< uint8_t > &default_value) const |
Returns value held in the binding as blob. | |
bool | getBool () const |
Returns boolean value held in the binding. | |
float | getFloat () const |
Returns float value held in the binding. | |
template<typename T > | |
T | getInteger () const |
Returns numeric value held in the binding. | |
template<typename T > | |
T | getIntegerOrDefault (T default_value) const |
Returns numeric value held in the binding. | |
data::ElementPtr | getJSON () const |
Returns value held in the binding as JSON. | |
MYSQL_BIND & | getMySqlBinding () |
Returns reference to the native binding. | |
std::string | getString () const |
Returns value held in the binding as string. | |
std::string | getStringOrDefault (const std::string &default_value) const |
Returns value held in the binding as string. | |
boost::posix_time::ptime | getTimestamp () const |
Returns timestamp value held in the binding. | |
boost::posix_time::ptime | getTimestampOrDefault (const boost::posix_time::ptime &default_value) const |
Returns timestamp value held in the binding. | |
enum_field_types | getType () const |
Returns MySQL column type for the binding. | |
Static Public Member Functions | |
static MySqlBindingPtr | condCreateBool (const util::Optional< bool > &value) |
Conditionally creates binding of uint8_t type representing a boolean value if provided value is specified. | |
template<typename T > | |
static MySqlBindingPtr | condCreateFloat (const util::Optional< T > &value) |
Conditionally creates binding of float type for sending data if provided value is specified. | |
template<typename T > | |
static MySqlBindingPtr | condCreateInteger (const util::Optional< T > &value) |
Conditionally creates binding of numeric type for sending data if provided value is specified. | |
static MySqlBindingPtr | condCreateIPv4Address (const util::Optional< asiolink::IOAddress > &value) |
Conditionally creates binding of uint32_t type representing an IPv4 address if provided value is specified. | |
static MySqlBindingPtr | condCreateString (const util::Optional< std::string > &value) |
Conditionally creates binding of text type for sending data if provided value is unspecified. | |
static boost::posix_time::ptime | convertFromDatabaseTime (const MYSQL_TIME &database_time) |
Converts database time to POSIX time. | |
static void | convertFromDatabaseTime (const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt) |
Converts Database Time to Lease Times. | |
static void | convertToDatabaseTime (const boost::posix_time::ptime &input_time, MYSQL_TIME &output_time) |
Converts POSIX time value to database time. | |
static void | convertToDatabaseTime (const time_t cltt, const uint32_t valid_lifetime, MYSQL_TIME &expire) |
Converts Lease Time to Database Times. | |
static void | convertToDatabaseTime (const time_t input_time, MYSQL_TIME &output_time) |
Converts time_t value to database time. | |
static MySqlBindingPtr | createBlob (const unsigned long length) |
Creates binding of blob type for receiving data. | |
template<typename Iterator > | |
static MySqlBindingPtr | createBlob (Iterator begin, Iterator end) |
Creates binding of blob type for sending data. | |
static MySqlBindingPtr | createBool () |
Creates binding having a bool type for receiving data. | |
static MySqlBindingPtr | createBool (const bool value) |
Creates binding having a bool type for sending data. | |
static MySqlBindingPtr | createFloat (const float value) |
Creates binding having a float type for sending data. | |
template<typename T > | |
static MySqlBindingPtr | createInteger () |
Creates binding of numeric type for receiving data. | |
template<typename T > | |
static MySqlBindingPtr | createInteger (T value) |
Creates binding of numeric type for sending data. | |
static MySqlBindingPtr | createNull () |
Creates binding encapsulating a NULL value. | |
static MySqlBindingPtr | createString (const std::string &value) |
Creates binding of text type for sending data. | |
static MySqlBindingPtr | createString (const unsigned long length) |
Creates binding of text type for receiving data. | |
static MySqlBindingPtr | createTimestamp () |
Creates binding of timestamp type for receiving data. | |
static MySqlBindingPtr | createTimestamp (const boost::posix_time::ptime ×tamp) |
Creates binding of timestamp type for sending data. | |
MySQL binding used in prepared statements.
Kea uses prepared statements to execute queries in a database. Prepared statements include placeholders for the input parameters. These parameters are passed to the prepared statements via a collection of MYSQL_BIND
structures. The same structures are used to receive the values from the database as a result of SELECT statements.
The MYSQL_BIND
structure contains information about the data type and length. It also contains pointer to the buffer actually holding the data to be passed to the database, a flag indicating if the value is null etc.
The MySqlBinding
is a C++ wrapper around this structure which is meant to ease management of the MySQL bindings. The major benefit is that the MySqlBinding
class owns the buffer, holding the data as well as other variables which are assigned to the MYSQL_BIND
structure. It also automatically detects the appropriate enum_field_types
value based on the C++ type used in the binding.
Definition at line 162 of file mysql_binding.h.
|
inline |
Checks if the bound value is NULL.
Definition at line 332 of file mysql_binding.h.
References isc::db::MLM_TRUE.
Referenced by getBlobOrDefault(), getIntegerOrDefault(), getJSON(), getStringOrDefault(), and getTimestampOrDefault().
|
static |
Conditionally creates binding of uint8_t
type representing a boolean value if provided value is specified.
value | Boolean value for which the binding should be created. |
Definition at line 145 of file mysql_binding.cc.
References createNull(), isc::util::Optional< T >::get(), and isc::util::Optional< T >::unspecified().
|
inlinestatic |
Conditionally creates binding of float type for sending data if provided value is specified.
T | Floating point type to be converted to float. |
value | Value to be stored in the database as float. |
Definition at line 445 of file mysql_binding.h.
References createNull(), isc::util::Optional< T >::get(), and isc::util::Optional< T >::unspecified().
|
inlinestatic |
Conditionally creates binding of numeric type for sending data if provided value is specified.
T | Numeric type corresponding to the binding type, e.g. uint8_t , uint16_t etc. |
value | Numeric value to be sent to the database. |
Definition at line 425 of file mysql_binding.h.
References createNull(), isc::util::Optional< T >::get(), and isc::util::Optional< T >::unspecified().
|
static |
Conditionally creates binding of uint32_t
type representing an IPv4 address if provided value is specified.
value | IOAddress encapsulating an IPv4 address. |
Definition at line 154 of file mysql_binding.cc.
References createNull(), and isc_throw.
|
static |
Conditionally creates binding of text type for sending data if provided value is unspecified.
value | String value to be sent to the database. |
Definition at line 112 of file mysql_binding.cc.
References createNull(), and createString().
|
static |
Converts database time to POSIX time.
database_time | Reference to MYSQL_TIME object where database time is stored. |
Definition at line 293 of file mysql_binding.cc.
|
static |
Converts Database Time to Lease Times.
Within the database, time is stored as "expire" (time of expiry of the lease) and valid lifetime. In the DHCP server, the information is stored client last transmit time and valid lifetime. These are related by the equation:
This method converts from the times in the database into times able to be inserted into the lease object.
expire | Reference to MYSQL_TIME object from where the expiry time of the lease is taken. |
valid_lifetime | lifetime of the lease. |
cltt | Reference to location where client last transmit time is put. |
Definition at line 273 of file mysql_binding.cc.
Referenced by isc::db::MySqlConnection::convertFromDatabaseTime(), and getTimestamp().
|
static |
Converts POSIX time value to database time.
input_time | A POSIX time value representing local time. |
output_time | Reference to MYSQL_TIME object where converted time will be put. |
Definition at line 213 of file mysql_binding.cc.
References isc_throw.
|
static |
Converts Lease Time to Database Times.
Within the DHCP servers, times are stored as client last transmit time and valid lifetime. In the database, the information is stored as valid lifetime and "expire" (time of expiry of the lease). They are related by the equation:
This method converts from the times in the lease object into times able to be added to the database.
cltt | Client last transmit time |
valid_lifetime | Valid lifetime |
expire | Reference to MYSQL_TIME object where the expiry time of the DHCP lease will be put. |
isc::BadValue | if the sum of the calculated expiration time is greater than the value of LeaseMgr::MAX_DB_TIME . |
Definition at line 237 of file mysql_binding.cc.
References isc_throw, and isc::db::DatabaseConnection::MAX_DB_TIME.
|
static |
Converts time_t value to database time.
input_time | A time_t value representing time. |
output_time | Reference to MYSQL_TIME object where converted time will be put. |
Definition at line 191 of file mysql_binding.cc.
Referenced by isc::db::MySqlConnection::convertToDatabaseTime(), and isc::db::MySqlConnection::convertToDatabaseTime().
|
static |
Creates binding of blob type for receiving data.
length | Length of the buffer into which received data will be stored. |
Definition at line 117 of file mysql_binding.cc.
|
inlinestatic |
Creates binding of blob type for sending data.
Iterator | Type of the iterator. |
begin | Iterator pointing to the beginning of the input buffer holding the data to be sent to the database. |
end | Iterator pointing to the end of the input buffer holding the data to be sent to the database. |
Definition at line 378 of file mysql_binding.h.
|
static |
Creates binding having a bool type for receiving data.
uint8_t
value representing the boolean value. Definition at line 135 of file mysql_binding.cc.
|
static |
Creates binding having a bool type for sending data.
value | Boolean value to be sent to the database. |
uint8_t
value representing the boolean value. Definition at line 140 of file mysql_binding.cc.
|
static |
Creates binding having a float type for sending data.
value | Float value to be sent to the database. |
Definition at line 124 of file mysql_binding.cc.
|
inlinestatic |
Creates binding of numeric type for receiving data.
Numeric | type corresponding to the binding type, e.g. uint8_t , uint16_t etc. |
Definition at line 392 of file mysql_binding.h.
|
inlinestatic |
Creates binding of numeric type for sending data.
Numeric | type corresponding to the binding type, e.g. uint8_t , uint16_t etc. |
value | Numeric value to be sent to the database. |
Definition at line 408 of file mysql_binding.h.
|
static |
Creates binding encapsulating a NULL value.
This method is used to create a binding encapsulating a NULL value, which can be used to assign NULL to any type of column.
Definition at line 185 of file mysql_binding.cc.
Referenced by condCreateBool(), condCreateFloat(), condCreateInteger(), condCreateIPv4Address(), and condCreateString().
|
static |
Creates binding of text type for sending data.
value | String value to be sent to the database. |
Definition at line 104 of file mysql_binding.cc.
|
static |
Creates binding of text type for receiving data.
length | Length of the buffer into which received data will be stored. |
Definition at line 97 of file mysql_binding.cc.
Referenced by condCreateString().
|
static |
Creates binding of timestamp type for receiving data.
Definition at line 178 of file mysql_binding.cc.
|
static |
Creates binding of timestamp type for sending data.
timestamp | Timestamp value to be sent to the database. |
Definition at line 170 of file mysql_binding.cc.
std::vector< uint8_t > isc::db::MySqlBinding::getBlob | ( | ) | const |
Returns value held in the binding as blob.
Call MySqlBinding::amNull
to verify that the value is not null prior to calling this method.
InvalidOperation | if the value is NULL or the binding type is not MYSQL_TYPE_BLOB . |
Definition at line 50 of file mysql_binding.cc.
Referenced by getBlobOrDefault().
std::vector< uint8_t > isc::db::MySqlBinding::getBlobOrDefault | ( | const std::vector< uint8_t > & | default_value | ) | const |
Returns value held in the binding as blob.
If the value to be returned is null, a default value is returned.
default_value | Default value. |
InvalidOperation | if the binding type is not MYSQL_TYPE_BLOB . |
Definition at line 60 of file mysql_binding.cc.
References amNull(), and getBlob().
|
inline |
Returns boolean value held in the binding.
Call MySqlBinding::amNull
to verify that the value is not null prior to calling this method.
InvalidOperation | if the value is NULL or the binding type is not uint8_t. |
Definition at line 302 of file mysql_binding.h.
float isc::db::MySqlBinding::getFloat | ( | ) | const |
Returns float value held in the binding.
Call MySqlBinding::amNull
to verify that the value is not null prior to calling this method.
InvalidOperation | if the value is NULL or the binding type does not match the template parameter. |
Definition at line 68 of file mysql_binding.cc.
|
inline |
Returns numeric value held in the binding.
Call MySqlBinding::amNull
to verify that the value is not null prior to calling this method.
Numeric | type corresponding to the binding type, e.g. uint8_t , uint16_t etc. |
InvalidOperation | if the value is NULL or the binding type does not match the template parameter. |
Definition at line 253 of file mysql_binding.h.
|
inline |
Returns numeric value held in the binding.
If the value to be returned is null, a default value is returned.
Numeric | type corresponding to the binding type, e.g. uint8_t , uint16_t etc. |
default_value | Default value. |
InvalidOperation | if the binding type does not match the template parameter. |
Definition at line 275 of file mysql_binding.h.
References amNull().
ElementPtr isc::db::MySqlBinding::getJSON | ( | ) | const |
Returns value held in the binding as JSON.
Call MySqlBinding::amNull
to verify that the value is not null prior to calling this method.
InvalidOperation | if the binding is not MYSQL_TYPE_STRING . |
data::JSONError | if the string value is not a valid JSON. |
Definition at line 41 of file mysql_binding.cc.
References amNull(), isc::data::Element::fromJSON(), and getString().
|
inline |
Returns reference to the native binding.
The returned reference is only valid for the lifetime of the object. Make sure that the object is not destroyed as long as the binding is required. In particular, do not destroy this object before database query is complete.
Definition at line 180 of file mysql_binding.h.
std::string isc::db::MySqlBinding::getString | ( | ) | const |
Returns value held in the binding as string.
Call MySqlBinding::amNull
to verify that the value is not null prior to calling this method.
InvalidOperation | if the value is NULL or the binding type is not MYSQL_TYPE_STRING . |
Definition at line 23 of file mysql_binding.cc.
Referenced by getJSON(), and getStringOrDefault().
std::string isc::db::MySqlBinding::getStringOrDefault | ( | const std::string & | default_value | ) | const |
Returns value held in the binding as string.
If the value to be returned is null, a default value is returned.
default_value | Default value. |
InvalidOperation | if the binding type is not MYSQL_TYPE_STRING . |
Definition at line 33 of file mysql_binding.cc.
References amNull(), and getString().
ptime isc::db::MySqlBinding::getTimestamp | ( | ) | const |
Returns timestamp value held in the binding.
Call MySqlBinding::amNull
to verify that the value is not null prior to calling this method.
InvalidOperation | if the value is NULL or the binding type is not MYSQL_TYPE_TIMESTAMP . |
Definition at line 79 of file mysql_binding.cc.
References convertFromDatabaseTime().
Referenced by getTimestampOrDefault().
ptime isc::db::MySqlBinding::getTimestampOrDefault | ( | const boost::posix_time::ptime & | default_value | ) | const |
Returns timestamp value held in the binding.
If the value to be returned is null, a default value is returned.
default_value | Default value. |
InvalidOperation | if the binding type is not MYSQL_TYPE_TIMESTAMP . |
Definition at line 89 of file mysql_binding.cc.
References amNull(), and getTimestamp().
|
inline |
Returns MySQL column type for the binding.
Definition at line 168 of file mysql_binding.h.