1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include "stdafx.h"
#include "Value.h"
#include "Core/Convert.h"
namespace sql {
Value::Value(MYSQL_BIND *data)
: data(data), nullValue(false), error(false), dataLength(0) {
// Clear the data, as per the manual. There are many fields that are not "public", so
// this is the "sanctioned" way of doing this...
memset(data, 0, sizeof(MYSQL_BIND));
// Set 'length' to 'buffer_length'. This is allowed in the documentation, and allows us
// to change 'length' without re-submitting the binds to MySQL.
data->length = &dataLength;
// Set is_null so that we can change null values. Documentation says that
// MYSQL_TYPE_NULL is an alternative.
data->is_null = &nullValue;
// Unsigned error.
data->error = &error;
// Set our representation to null.
clear();
}
Value::~Value() {
clear();
// Clear the value so that we don't have any lingering pointers to ourselves.
memset(data, 0, sizeof(MYSQL_BIND));
}
void Value::setInt(Long value) {
clear();
data->buffer_type = MYSQL_TYPE_LONGLONG;
data->buffer = &localBuffer;
data->buffer_length = sizeof(Long);
data->is_unsigned = false;
nullValue = false;
localBuffer.signedVal = value;
}
void Value::setUInt(Word value) {
clear();
data->buffer_type = MYSQL_TYPE_LONGLONG;
data->buffer = &localBuffer;
data->buffer_length = sizeof(Word);
data->is_unsigned = true;
nullValue = false;
localBuffer.unsignedVal = value;
}
void Value::setFloat(Double value) {
clear();
data->buffer_type = MYSQL_TYPE_DOUBLE;
data->buffer_length = sizeof(Double);
data->buffer = &localBuffer;
data->is_unsigned = false;
nullValue = false;
localBuffer.floatVal = value;
}
void Value::setString(Str *value) {
clear();
size_t length = convert(value->c_str(), null, 0);
data->buffer_type = MYSQL_TYPE_STRING;
data->buffer = malloc(length);
data->buffer_length = (unsigned long)length;
convert(value->c_str(), (char *)data->buffer, length);
dataLength = (unsigned long)(length - 1); // Don't add the null terminator.
nullValue = false;
}
void Value::setString(size_t length) {
clear();
data->buffer_type = MYSQL_TYPE_STRING;
data->buffer_length = (unsigned long)length;
if (length == 0)
data->buffer = null;
else
data->buffer = malloc(length);
dataLength = 0;
}
void Value::setNull() {
clear();
}
bool Value::isInt() const {
return data->buffer_type == MYSQL_TYPE_LONGLONG;
}
Long Value::getInt() const {
assert(isInt());
if (nullValue)
return 0;
if (data->is_unsigned)
return static_cast<Long>(localBuffer.unsignedVal);
else
return localBuffer.signedVal;
}
bool Value::isUInt() const {
return data->buffer_type == MYSQL_TYPE_LONGLONG;
}
Word Value::getUInt() const {
assert(isUInt());
if (nullValue)
return 0;
if (data->is_unsigned)
return localBuffer.unsignedVal;
else
return static_cast<Word>(localBuffer.signedVal);
}
bool Value::isFloat() const {
return data->buffer_type == MYSQL_TYPE_DOUBLE;
}
Double Value::getFloat() const {
assert(isFloat());
if (nullValue)
return 0;
return localBuffer.floatVal;
}
bool Value::isString() const {
return data->buffer_type == MYSQL_TYPE_STRING
|| data->buffer_type == MYSQL_TYPE_VAR_STRING;
}
Str *Value::getString(Engine &e) const {
assert(isString());
if (nullValue)
return new (e) Str();
if (data->buffer_length == 0 || dataLength == 0)
return new (e) Str();
const char *buffer = static_cast<const char *>(data->buffer);
GcArray<wchar> *converted = toWChar(e, buffer, dataLength);
return new (e) Str(converted);
}
bool Value::isNull() const {
return data->buffer_type == MYSQL_TYPE_NULL
|| nullValue;
}
void Value::clear() {
// Safeguard if we were not initialized.
if (!data)
return;
if (data->buffer && data->buffer != &localBuffer) {
free(data->buffer);
}
data->buffer = null;
data->buffer_type = MYSQL_TYPE_NULL;
nullValue = true;
data->is_unsigned = false;
error = false;
dataLength = 0;
}
}
|