17 MySqlBinding::getString()
const {
19 validateAccess<std::string>();
21 return (std::string());
23 return (std::string(buffer_.begin(), buffer_.begin() + length_));
27 MySqlBinding::getStringOrDefault(
const std::string& default_value)
const {
29 return (default_value);
35 MySqlBinding::getJSON()
const {
39 std::string s = getString();
44 MySqlBinding::getBlob()
const {
46 validateAccess<std::vector<uint8_t> >();
48 return (std::vector<uint8_t>());
50 return (std::vector<uint8_t>(buffer_.begin(), buffer_.begin() + length_));
54 MySqlBinding::getBlobOrDefault(
const std::vector<uint8_t>& default_value)
const {
56 return (default_value);
61 boost::posix_time::ptime
62 MySqlBinding::getTimestamp()
const {
64 validateAccess<boost::posix_time::ptime>();
67 const MYSQL_TIME* database_time = reinterpret_cast<const MYSQL_TIME*>(&buffer_[0]);
68 return (convertFromDatabaseTime(*database_time));
71 boost::posix_time::ptime
72 MySqlBinding::getTimestampOrDefault(
const boost::posix_time::ptime& default_value)
const {
74 return (default_value);
76 return (getTimestamp());
80 MySqlBinding::createString(
const unsigned long length) {
87 MySqlBinding::createString(
const std::string& value) {
90 binding->setBufferValue(value.begin(), value.end());
95 MySqlBinding::condCreateString(
const std::string& value) {
96 return (value.empty() ? MySqlBinding::createNull() : createString(value));
100 MySqlBinding::createBlob(
const unsigned long length) {
107 MySqlBinding::createTimestamp(
const boost::posix_time::ptime& timestamp) {
110 binding->setTimestampValue(timestamp);
115 MySqlBinding::createTimestamp() {
122 MySqlBinding::createNull() {
128 MySqlBinding::convertToDatabaseTime(
const time_t input_time,
129 MYSQL_TIME& output_time) {
133 (void) localtime_r(&input_time, &time_tm);
136 output_time.year = time_tm.tm_year + 1900;
137 output_time.month = time_tm.tm_mon + 1;
138 output_time.day = time_tm.tm_mday;
139 output_time.hour = time_tm.tm_hour;
140 output_time.minute = time_tm.tm_min;
141 output_time.second = time_tm.tm_sec;
142 output_time.second_part = 0;
143 output_time.neg = my_bool(0);
147 MySqlBinding::convertToDatabaseTime(
const time_t cltt,
148 const uint32_t valid_lifetime,
149 MYSQL_TIME& expire) {
153 int64_t expire_time_64 = static_cast<int64_t>(cltt) +
154 static_cast<int64_t>(valid_lifetime);
158 if (expire_time_64 > DatabaseConnection::MAX_DB_TIME) {
162 const time_t expire_time = static_cast<const time_t>(expire_time_64);
166 (void) localtime_r(&expire_time, &expire_tm);
169 expire.year = expire_tm.tm_year + 1900;
170 expire.month = expire_tm.tm_mon + 1;
171 expire.day = expire_tm.tm_mday;
172 expire.hour = expire_tm.tm_hour;
173 expire.minute = expire_tm.tm_min;
174 expire.second = expire_tm.tm_sec;
175 expire.second_part = 0;
176 expire.neg = my_bool(0);
180 MySqlBinding::convertFromDatabaseTime(
const MYSQL_TIME& expire,
181 uint32_t valid_lifetime,
185 memset(&expire_tm, 0,
sizeof(expire_tm));
187 expire_tm.tm_year = expire.year - 1900;
188 expire_tm.tm_mon = expire.month - 1;
189 expire_tm.tm_mday = expire.day;
190 expire_tm.tm_hour = expire.hour;
191 expire_tm.tm_min = expire.minute;
192 expire_tm.tm_sec = expire.second;
193 expire_tm.tm_isdst = -1;
196 cltt = mktime(&expire_tm) - valid_lifetime;
199 boost::posix_time::ptime
200 MySqlBinding::convertFromDatabaseTime(
const MYSQL_TIME& database_time) {
202 struct tm converted_tm;
203 memset(&converted_tm, 0,
sizeof(converted_tm));
205 converted_tm.tm_year = database_time.year - 1900;
206 converted_tm.tm_mon = database_time.month - 1;
207 converted_tm.tm_mday = database_time.day;
208 converted_tm.tm_hour = database_time.hour;
209 converted_tm.tm_min = database_time.minute;
210 converted_tm.tm_sec = database_time.second;
211 converted_tm.tm_isdst = -1;
214 return (boost::posix_time::ptime_from_tm(converted_tm));
217 MySqlBinding::MySqlBinding(enum_field_types buffer_type,
219 : buffer_(length), length_(length),
220 null_value_(buffer_type == MYSQL_TYPE_NULL) {
221 memset(&bind_, 0,
sizeof(MYSQL_BIND));
222 bind_.buffer_type = buffer_type;
224 if (buffer_type != MYSQL_TYPE_NULL) {
225 bind_.buffer = &buffer_[0];
226 bind_.buffer_length = length_;
227 bind_.length = &length_;
228 bind_.is_null = &null_value_;
233 MySqlBinding::setBufferLength(
const unsigned long length) {
241 buffer_.resize(length_ > 0 ? length_ : 1);
242 bind_.buffer = &buffer_[0];
243 bind_.buffer_length = length_;
247 MySqlBinding::setTimestampValue(
const boost::posix_time::ptime& timestamp) {
249 tm td_tm = to_tm(timestamp);
251 time_t tt = mktime(&td_tm);
253 MYSQL_TIME database_time;
256 memcpy(static_cast<void*>(&buffer_[0]), reinterpret_cast<char*>(&database_time),
258 bind_.buffer = &buffer_[0];
static void convertToDatabaseTime(const time_t input_time, MYSQL_TIME &output_time)
Converts time_t value to database time.
boost::shared_ptr< Element > ElementPtr
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
#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...
Trait class for column types supported in MySQL.
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< MySqlBinding > MySqlBindingPtr
Shared pointer to the Binding class.
MySQL binding used in prepared statements.