11 #include <boost/date_time/gregorian/gregorian.hpp>
23 MySqlBinding::getString()
const {
25 validateAccess<std::string>();
27 return (std::string());
33 MySqlBinding::getStringOrDefault(
const std::string& default_value)
const {
35 return (default_value);
41 MySqlBinding::getJSON()
const {
45 std::string s = getString();
46 return (Element::fromJSON(s));
50 MySqlBinding::getBlob()
const {
52 validateAccess<std::vector<uint8_t> >();
54 return (std::vector<uint8_t>());
56 return (std::vector<uint8_t>(
buffer_.begin(),
buffer_.begin() + length_));
60 MySqlBinding::getBlobOrDefault(
const std::vector<uint8_t>& default_value)
const {
62 return (default_value);
68 MySqlBinding::getFloat()
const {
75 return (getInteger<float>());
79 MySqlBinding::getTimestamp()
const {
81 validateAccess<ptime>();
84 const MYSQL_TIME* database_time =
reinterpret_cast<const MYSQL_TIME*
>(&
buffer_[0]);
85 return (convertFromDatabaseTime(*database_time));
89 MySqlBinding::getTimestampOrDefault(
const ptime& default_value)
const {
91 return (default_value);
93 return (getTimestamp());
97 MySqlBinding::createString(
const unsigned long length) {
104 MySqlBinding::createString(
const std::string& value) {
107 binding->setBufferValue(value.begin(), value.end());
113 return (value.
unspecified() ? MySqlBinding::createNull() : createString(value));
117 MySqlBinding::createBlob(
const unsigned long length) {
124 MySqlBinding::createFloat(
const float value) {
131 return (createInteger<float>(value));
135 MySqlBinding::createBool(
const bool value) {
136 return (createInteger<uint8_t>(static_cast<uint8_t>(value)));
142 return (MySqlBinding::createNull());
145 return (createInteger<uint8_t>(static_cast<uint8_t>(value.
get())));
152 return (MySqlBinding::createNull());
158 << value.
get().
toText() <<
"' is not an IPv4 address");
161 return (createInteger<uint32_t>(value.
get().
toUint32()));
165 MySqlBinding::createTimestamp(
const boost::posix_time::ptime& timestamp) {
168 binding->setTimestampValue(timestamp);
173 MySqlBinding::createTimestamp() {
180 MySqlBinding::createNull() {
186 MySqlBinding::convertToDatabaseTime(
const time_t input_time,
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;
208 MySqlBinding::convertToDatabaseTime(
const boost::posix_time::ptime& input_time,
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;
232 MySqlBinding::convertToDatabaseTime(
const time_t cltt,
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);
243 if (expire_time_64 > DatabaseConnection::MAX_DB_TIME) {
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;
268 MySqlBinding::convertFromDatabaseTime(
const MYSQL_TIME& expire,
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;
288 MySqlBinding::convertFromDatabaseTime(
const MYSQL_TIME& database_time) {
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));
302 MySqlBinding::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) {
313 bind_.buffer_length = length_;
314 bind_.length = &length_;
315 bind_.is_null = &null_value_;
320 MySqlBinding::setBufferLength(
const unsigned long length) {
328 buffer_.resize(length_ > 0 ? length_ : 1);
330 bind_.buffer_length = length_;
334 MySqlBinding::setTimestampValue(
const ptime& timestamp) {
335 MYSQL_TIME database_time;
338 memcpy(static_cast<void*>(&
buffer_[0]), reinterpret_cast<char*>(&database_time),
bool my_bool
my_bool type in MySQL 8.x.
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
uint32_t toUint32() const
Converts IPv4 address to uint32_t.
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Converts time_t value to database time.
boost::shared_ptr< Element > ElementPtr
bool isV4() const
Convenience function to check for an IPv4 address.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
const OutputBuffer * buffer_
Trait class for column types supported in MySQL.
Defines the logger used by the top-level component of kea-dhcp-ddns.
T get() const
Retrieves the encapsulated value.
std::string toText() const
Convert the address to a string.
boost::shared_ptr< MySqlBinding > MySqlBindingPtr
Shared pointer to the Binding class.
MySQL binding used in prepared statements.