2014-05-19 22:05:33 +00:00
|
|
|
#include <mysqlxx/Value.h>
|
2011-03-18 20:30:13 +00:00
|
|
|
#include <mysqlxx/ResultBase.h>
|
|
|
|
#include <mysqlxx/Query.h>
|
|
|
|
#include <mysqlxx/Exception.h>
|
|
|
|
|
|
|
|
|
2017-01-13 20:37:37 +00:00
|
|
|
namespace mysqlxx
|
|
|
|
{
|
|
|
|
|
|
|
|
UInt64 Value::readUIntText(const char * buf, size_t length) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 x = 0;
|
|
|
|
const char * end = buf + length;
|
|
|
|
|
|
|
|
while (buf != end)
|
|
|
|
{
|
|
|
|
switch (*buf)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
x *= 10;
|
|
|
|
x += *buf - '0';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throwException("Cannot parse unsigned integer");
|
|
|
|
}
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Int64 Value::readIntText(const char * buf, size_t length) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
bool negative = false;
|
2018-12-26 00:31:51 +00:00
|
|
|
UInt64 x = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * end = buf + length;
|
|
|
|
|
|
|
|
while (buf != end)
|
|
|
|
{
|
|
|
|
switch (*buf)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
negative = true;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
x *= 10;
|
|
|
|
x += *buf - '0';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throwException("Cannot parse signed integer");
|
|
|
|
}
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
|
2018-12-26 00:31:51 +00:00
|
|
|
return negative ? -x : x;
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double Value::readFloatText(const char * buf, size_t length) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
bool negative = false;
|
|
|
|
double x = 0;
|
|
|
|
bool after_point = false;
|
|
|
|
double power_of_ten = 1;
|
|
|
|
const char * end = buf + length;
|
|
|
|
|
|
|
|
while (buf != end)
|
|
|
|
{
|
|
|
|
switch (*buf)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
negative = true;
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
after_point = true;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
if (after_point)
|
|
|
|
{
|
|
|
|
power_of_ten /= 10;
|
|
|
|
x += (*buf - '0') * power_of_ten;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x *= 10;
|
|
|
|
x += *buf - '0';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
{
|
|
|
|
++buf;
|
|
|
|
Int32 exponent = readIntText(buf, end - buf);
|
2018-02-16 14:51:14 +00:00
|
|
|
x *= preciseExp10(exponent);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (negative)
|
|
|
|
x = -x;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
x = std::numeric_limits<double>::infinity();
|
|
|
|
if (negative)
|
|
|
|
x = -x;
|
|
|
|
return x;
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
x = std::numeric_limits<double>::quiet_NaN();
|
|
|
|
return x;
|
|
|
|
default:
|
|
|
|
throwException("Cannot parse floating point number");
|
|
|
|
}
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
if (negative)
|
|
|
|
x = -x;
|
|
|
|
|
|
|
|
return x;
|
2017-01-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Value::throwException(const char * text) const
|
2011-03-18 20:30:13 +00:00
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
static constexpr size_t preview_length = 1000;
|
2018-09-17 03:09:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::stringstream info;
|
|
|
|
info << text;
|
2011-03-18 20:30:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!isNull())
|
|
|
|
{
|
|
|
|
info << ": ";
|
|
|
|
info.write(m_data, m_length);
|
|
|
|
}
|
2011-03-18 20:30:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (res && res->getQuery())
|
2020-03-23 02:12:31 +00:00
|
|
|
info << ", query: " << res->getQuery()->str().substr(0, preview_length);
|
2011-03-18 20:30:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
throw CannotParseValue(info.str());
|
2011-03-18 20:30:13 +00:00
|
|
|
}
|
2017-01-13 20:37:37 +00:00
|
|
|
|
|
|
|
}
|