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);
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);
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());
161 if (!value.
get().isV4()) {
163 << value.
get().toText() <<
"' is not an IPv4 address");
173 binding->setTimestampValue(timestamp);
192 MYSQL_TIME& output_time) {
195 memset(&output_time, 0,
sizeof(MYSQL_TIME));
199 (void) localtime_r(&input_time, &time_tm);
202 output_time.year = time_tm.tm_year + 1900;
203 output_time.month = time_tm.tm_mon + 1;
204 output_time.day = time_tm.tm_mday;
205 output_time.hour = time_tm.tm_hour;
206 output_time.minute = time_tm.tm_min;
207 output_time.second = time_tm.tm_sec;
208 output_time.second_part = 0;
214 MYSQL_TIME& output_time) {
215 if (input_time.is_not_a_date_time()) {
220 memset(&output_time, 0,
sizeof(MYSQL_TIME));
222 output_time.year = input_time.date().year();
223 output_time.month = input_time.date().month();
224 output_time.day = input_time.date().day();
225 output_time.hour = input_time.time_of_day().hours();
226 output_time.minute = input_time.time_of_day().minutes();
227 output_time.second = input_time.time_of_day().seconds();
230 output_time.second_part = 0;
238 const uint32_t valid_lifetime,
239 MYSQL_TIME& expire) {
243 int64_t expire_time_64 =
static_cast<int64_t
>(cltt) +
244 static_cast<int64_t
>(valid_lifetime);
253 memset(&expire, 0,
sizeof(MYSQL_TIME));
255 const time_t expire_time =
static_cast<time_t
>(expire_time_64);
259 (void) localtime_r(&expire_time, &expire_tm);
262 expire.year = expire_tm.tm_year + 1900;
263 expire.month = expire_tm.tm_mon + 1;
264 expire.day = expire_tm.tm_mday;
265 expire.hour = expire_tm.tm_hour;
266 expire.minute = expire_tm.tm_min;
267 expire.second = expire_tm.tm_sec;
268 expire.second_part = 0;
274 uint32_t valid_lifetime,
278 memset(&expire_tm, 0,
sizeof(expire_tm));
280 expire_tm.tm_year = expire.year - 1900;
281 expire_tm.tm_mon = expire.month - 1;
282 expire_tm.tm_mday = expire.day;
283 expire_tm.tm_hour = expire.hour;
284 expire_tm.tm_min = expire.minute;
285 expire_tm.tm_sec = expire.second;
286 expire_tm.tm_isdst = -1;
289 cltt = mktime(&expire_tm) - valid_lifetime;
298 ptime pt(boost::gregorian::date(database_time.year,
299 boost::gregorian::greg_month(database_time.month),
301 time_duration(database_time.hour, database_time.minute,
302 database_time.second, fractional));
307MySqlBinding::MySqlBinding(enum_field_types buffer_type,
311 : buffer_(length > 0 ? length : 1), length_(length),
312 null_value_(buffer_type == MYSQL_TYPE_NULL) {
313 memset(&bind_, 0,
sizeof(MYSQL_BIND));
314 bind_.buffer_type = buffer_type;
316 if (buffer_type != MYSQL_TYPE_NULL) {
317 bind_.buffer = &buffer_[0];
318 bind_.buffer_length = length_;
319 bind_.length = &length_;
320 bind_.is_null = &null_value_;
325MySqlBinding::setBufferLength(
const unsigned long length) {
333 buffer_.resize(length_ > 0 ? length_ : 1);
334 bind_.buffer = &buffer_[0];
335 bind_.buffer_length = length_;
339MySqlBinding::setTimestampValue(
const ptime& timestamp) {
340 MYSQL_TIME database_time;
343 memcpy(
static_cast<void*
>(&buffer_[0]),
reinterpret_cast<char*
>(&database_time),
345 bind_.buffer = &buffer_[0];
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
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.
static MySqlBindingPtr createInteger()
Creates binding of numeric type for receiving data.
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.
static MySqlBindingPtr createBool()
Creates binding having a bool type for receiving data.
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...
T getInteger() const
Returns numeric 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.
A template representing an optional value.
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.