11#include <boost/date_time/gregorian/gregorian.hpp>
14using namespace boost::posix_time;
25 validateAccess<std::string>();
27 return (std::string());
29 return (std::string(buffer_.begin(), buffer_.begin() + length_));
35 return (default_value);
46 return (Element::fromJSON(s));
52 validateAccess<std::vector<uint8_t> >();
54 return (std::vector<uint8_t>());
56 return (std::vector<uint8_t>(buffer_.begin(), buffer_.begin() + length_));
62 return (default_value);
75 return (getInteger<float>());
81 validateAccess<ptime>();
84 const MYSQL_TIME* database_time =
reinterpret_cast<const MYSQL_TIME*
>(&buffer_[0]);
91 return (default_value);
107 binding->setBufferValue(value.begin(), value.end());
131 return (createInteger<float>(value));
136 return (createInteger<uint8_t>(
static_cast<uint8_t
>(value)));
145 return (createInteger<uint8_t>(
static_cast<uint8_t
>(value.
get())));
156 if (!value.
get().isV4()) {
158 << value.
get().toText() <<
"' is not an IPv4 address");
161 return (createInteger<uint32_t>(value.
get().toUint32()));
168 binding->setTimestampValue(timestamp);
187 MYSQL_TIME& output_time) {
190 memset(&output_time, 0,
sizeof(MYSQL_TIME));
194 (void) localtime_r(&input_time, &time_tm);
197 output_time.year = time_tm.tm_year + 1900;
198 output_time.month = time_tm.tm_mon + 1;
199 output_time.day = time_tm.tm_mday;
200 output_time.hour = time_tm.tm_hour;
201 output_time.minute = time_tm.tm_min;
202 output_time.second = time_tm.tm_sec;
203 output_time.second_part = 0;
209 MYSQL_TIME& output_time) {
210 if (input_time.is_not_a_date_time()) {
215 memset(&output_time, 0,
sizeof(MYSQL_TIME));
217 output_time.year = input_time.date().year();
218 output_time.month = input_time.date().month();
219 output_time.day = input_time.date().day();
220 output_time.hour = input_time.time_of_day().hours();
221 output_time.minute = input_time.time_of_day().minutes();
222 output_time.second = input_time.time_of_day().seconds();
225 output_time.second_part = 0;
233 const uint32_t valid_lifetime,
234 MYSQL_TIME& expire) {
238 int64_t expire_time_64 =
static_cast<int64_t
>(cltt) +
239 static_cast<int64_t
>(valid_lifetime);
248 memset(&expire, 0,
sizeof(MYSQL_TIME));
250 const time_t expire_time =
static_cast<time_t
>(expire_time_64);
254 (void) localtime_r(&expire_time, &expire_tm);
257 expire.year = expire_tm.tm_year + 1900;
258 expire.month = expire_tm.tm_mon + 1;
259 expire.day = expire_tm.tm_mday;
260 expire.hour = expire_tm.tm_hour;
261 expire.minute = expire_tm.tm_min;
262 expire.second = expire_tm.tm_sec;
263 expire.second_part = 0;
269 uint32_t valid_lifetime,
273 memset(&expire_tm, 0,
sizeof(expire_tm));
275 expire_tm.tm_year = expire.year - 1900;
276 expire_tm.tm_mon = expire.month - 1;
277 expire_tm.tm_mday = expire.day;
278 expire_tm.tm_hour = expire.hour;
279 expire_tm.tm_min = expire.minute;
280 expire_tm.tm_sec = expire.second;
281 expire_tm.tm_isdst = -1;
284 cltt = mktime(&expire_tm) - valid_lifetime;
293 ptime pt(boost::gregorian::date(database_time.year,
294 boost::gregorian::greg_month(database_time.month),
296 time_duration(database_time.hour, database_time.minute,
297 database_time.second, fractional));
302MySqlBinding::MySqlBinding(enum_field_types buffer_type,
306 : buffer_(length > 0 ? length : 1), length_(length),
307 null_value_(buffer_type == MYSQL_TYPE_NULL) {
308 memset(&bind_, 0,
sizeof(MYSQL_BIND));
309 bind_.buffer_type = buffer_type;
311 if (buffer_type != MYSQL_TYPE_NULL) {
312 bind_.buffer = &buffer_[0];
313 bind_.buffer_length = length_;
314 bind_.length = &length_;
315 bind_.is_null = &null_value_;
320MySqlBinding::setBufferLength(
const unsigned long length) {
328 buffer_.resize(length_ > 0 ? length_ : 1);
329 bind_.buffer = &buffer_[0];
330 bind_.buffer_length = length_;
334MySqlBinding::setTimestampValue(
const ptime& timestamp) {
335 MYSQL_TIME database_time;
338 memcpy(
static_cast<void*
>(&buffer_[0]),
reinterpret_cast<char*
>(&database_time),
340 bind_.buffer = &buffer_[0];
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static const time_t MAX_DB_TIME
Defines maximum value for time that can be reliably stored.
MySQL binding used in prepared statements.
static void convertFromDatabaseTime(const MYSQL_TIME &expire, uint32_t valid_lifetime, time_t &cltt)
Converts Database Time to Lease Times.
static MySqlBindingPtr condCreateBool(const util::Optional< bool > &value)
Conditionally creates binding of uint8_t type representing a boolean value if provided value is speci...
std::vector< uint8_t > getBlobOrDefault(const std::vector< uint8_t > &default_value) const
Returns value held in the binding as blob.
std::vector< uint8_t > getBlob() const
Returns value held in the binding as blob.
static MySqlBindingPtr condCreateString(const util::Optional< std::string > &value)
Conditionally creates binding of text type for sending data if provided value is unspecified.
static MySqlBindingPtr createString(const unsigned long length)
Creates binding of text type for receiving data.
bool amNull() const
Checks if the bound value is NULL.
std::string getString() const
Returns value held in the binding as string.
data::ElementPtr getJSON() const
Returns value held in the binding as JSON.
std::string getStringOrDefault(const std::string &default_value) const
Returns value held in the binding as string.
static MySqlBindingPtr createTimestamp()
Creates binding of timestamp type for receiving data.
static MySqlBindingPtr createFloat(const float value)
Creates binding having a float type for sending data.
float getFloat() const
Returns float value held in the binding.
static MySqlBindingPtr createNull()
Creates binding encapsulating a NULL value.
boost::posix_time::ptime getTimestamp() const
Returns timestamp value held in the binding.
static MySqlBindingPtr createBlob(const unsigned long length)
Creates binding of blob type for receiving data.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Converts time_t value to database time.
static MySqlBindingPtr condCreateIPv4Address(const util::Optional< asiolink::IOAddress > &value)
Conditionally creates binding of uint32_t type representing an IPv4 address if provided value is spec...
static MySqlBindingPtr createBool(const bool value)
Creates binding having a bool type for sending data.
boost::posix_time::ptime getTimestampOrDefault(const boost::posix_time::ptime &default_value) const
Returns timestamp value held in the binding.
T get() const
Retrieves the encapsulated value.
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
boost::shared_ptr< MySqlBinding > MySqlBindingPtr
Shared pointer to the Binding class.
bool my_bool
my_bool type in MySQL 8.x.
Defines the logger used by the top-level component of kea-lfc.
Trait class for column types supported in MySQL.