mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +01:00
Core/DBLayer: Optimize string based query results by eliminating unneeded buffer copies
This commit is contained in:
@@ -17,19 +17,17 @@
|
||||
|
||||
#include "Field.h"
|
||||
#include "Log.h"
|
||||
#include "MySQLHacks.h"
|
||||
|
||||
Field::Field()
|
||||
{
|
||||
data.value = NULL;
|
||||
data.type = DatabaseFieldTypes::Null;
|
||||
data.value = nullptr;
|
||||
data.length = 0;
|
||||
data.raw = false;
|
||||
meta = nullptr;
|
||||
}
|
||||
|
||||
Field::~Field()
|
||||
{
|
||||
CleanUp();
|
||||
}
|
||||
Field::~Field() = default;
|
||||
|
||||
uint8 Field::GetUInt8() const
|
||||
{
|
||||
@@ -45,8 +43,8 @@ uint8 Field::GetUInt8() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<uint8*>(data.value);
|
||||
return static_cast<uint8>(strtoul((char*)data.value, nullptr, 10));
|
||||
return *reinterpret_cast<uint8 const*>(data.value);
|
||||
return static_cast<uint8>(strtoul(data.value, nullptr, 10));
|
||||
}
|
||||
|
||||
int8 Field::GetInt8() const
|
||||
@@ -63,8 +61,8 @@ int8 Field::GetInt8() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<int8*>(data.value);
|
||||
return static_cast<int8>(strtol((char*)data.value, NULL, 10));
|
||||
return *reinterpret_cast<int8 const*>(data.value);
|
||||
return static_cast<int8>(strtol(data.value, nullptr, 10));
|
||||
}
|
||||
|
||||
uint16 Field::GetUInt16() const
|
||||
@@ -81,8 +79,8 @@ uint16 Field::GetUInt16() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<uint16*>(data.value);
|
||||
return static_cast<uint16>(strtoul((char*)data.value, nullptr, 10));
|
||||
return *reinterpret_cast<uint16 const*>(data.value);
|
||||
return static_cast<uint16>(strtoul(data.value, nullptr, 10));
|
||||
}
|
||||
|
||||
int16 Field::GetInt16() const
|
||||
@@ -99,8 +97,8 @@ int16 Field::GetInt16() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<int16*>(data.value);
|
||||
return static_cast<int16>(strtol((char*)data.value, NULL, 10));
|
||||
return *reinterpret_cast<int16 const*>(data.value);
|
||||
return static_cast<int16>(strtol(data.value, nullptr, 10));
|
||||
}
|
||||
|
||||
uint32 Field::GetUInt32() const
|
||||
@@ -117,8 +115,8 @@ uint32 Field::GetUInt32() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<uint32*>(data.value);
|
||||
return static_cast<uint32>(strtoul((char*)data.value, nullptr, 10));
|
||||
return *reinterpret_cast<uint32 const*>(data.value);
|
||||
return static_cast<uint32>(strtoul(data.value, nullptr, 10));
|
||||
}
|
||||
|
||||
int32 Field::GetInt32() const
|
||||
@@ -135,8 +133,8 @@ int32 Field::GetInt32() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<int32*>(data.value);
|
||||
return static_cast<int32>(strtol((char*)data.value, NULL, 10));
|
||||
return *reinterpret_cast<int32 const*>(data.value);
|
||||
return static_cast<int32>(strtol(data.value, nullptr, 10));
|
||||
}
|
||||
|
||||
uint64 Field::GetUInt64() const
|
||||
@@ -153,8 +151,8 @@ uint64 Field::GetUInt64() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<uint64*>(data.value);
|
||||
return static_cast<uint64>(strtoull((char*)data.value, nullptr, 10));
|
||||
return *reinterpret_cast<uint64 const*>(data.value);
|
||||
return static_cast<uint64>(strtoull(data.value, nullptr, 10));
|
||||
}
|
||||
|
||||
int64 Field::GetInt64() const
|
||||
@@ -171,8 +169,8 @@ int64 Field::GetInt64() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<int64*>(data.value);
|
||||
return static_cast<int64>(strtoll((char*)data.value, NULL, 10));
|
||||
return *reinterpret_cast<int64 const*>(data.value);
|
||||
return static_cast<int64>(strtoll(data.value, nullptr, 10));
|
||||
}
|
||||
|
||||
float Field::GetFloat() const
|
||||
@@ -189,8 +187,8 @@ float Field::GetFloat() const
|
||||
#endif
|
||||
|
||||
if (data.raw)
|
||||
return *reinterpret_cast<float*>(data.value);
|
||||
return static_cast<float>(atof((char*)data.value));
|
||||
return *reinterpret_cast<float const*>(data.value);
|
||||
return static_cast<float>(atof(data.value));
|
||||
}
|
||||
|
||||
double Field::GetDouble() const
|
||||
@@ -207,14 +205,14 @@ double Field::GetDouble() const
|
||||
#endif
|
||||
|
||||
if (data.raw && !IsType(DatabaseFieldTypes::Decimal))
|
||||
return *reinterpret_cast<double*>(data.value);
|
||||
return static_cast<double>(atof((char*)data.value));
|
||||
return *reinterpret_cast<double const*>(data.value);
|
||||
return static_cast<double>(atof(data.value));
|
||||
}
|
||||
|
||||
char const* Field::GetCString() const
|
||||
{
|
||||
if (!data.value)
|
||||
return NULL;
|
||||
return nullptr;
|
||||
|
||||
#ifdef TRINITY_DEBUG
|
||||
if (IsNumeric() && data.raw)
|
||||
@@ -249,99 +247,44 @@ std::vector<uint8> Field::GetBinary() const
|
||||
return result;
|
||||
}
|
||||
|
||||
void Field::SetByteValue(void* newValue, DatabaseFieldTypes newType, uint32 length)
|
||||
void Field::SetByteValue(char const* newValue, uint32 length)
|
||||
{
|
||||
// This value stores raw bytes that have to be explicitly cast later
|
||||
data.value = newValue;
|
||||
data.length = length;
|
||||
data.type = newType;
|
||||
data.raw = true;
|
||||
}
|
||||
|
||||
void Field::SetStructuredValue(char* newValue, DatabaseFieldTypes newType, uint32 length)
|
||||
void Field::SetStructuredValue(char const* newValue, uint32 length)
|
||||
{
|
||||
if (data.value)
|
||||
CleanUp();
|
||||
|
||||
// This value stores somewhat structured data that needs function style casting
|
||||
if (newValue)
|
||||
{
|
||||
data.value = new char[length + 1];
|
||||
memcpy(data.value, newValue, length);
|
||||
*(reinterpret_cast<char*>(data.value) + length) = '\0';
|
||||
data.length = length;
|
||||
}
|
||||
|
||||
data.type = newType;
|
||||
data.value = newValue;
|
||||
data.length = length;
|
||||
data.raw = false;
|
||||
}
|
||||
|
||||
bool Field::IsType(DatabaseFieldTypes type) const
|
||||
{
|
||||
return data.type == type;
|
||||
return meta->Type == type;
|
||||
}
|
||||
|
||||
bool Field::IsNumeric() const
|
||||
{
|
||||
return (data.type == DatabaseFieldTypes::Int8 ||
|
||||
data.type == DatabaseFieldTypes::Int16 ||
|
||||
data.type == DatabaseFieldTypes::Int32 ||
|
||||
data.type == DatabaseFieldTypes::Int64 ||
|
||||
data.type == DatabaseFieldTypes::Float ||
|
||||
data.type == DatabaseFieldTypes::Double);
|
||||
return (meta->Type == DatabaseFieldTypes::Int8 ||
|
||||
meta->Type == DatabaseFieldTypes::Int16 ||
|
||||
meta->Type == DatabaseFieldTypes::Int32 ||
|
||||
meta->Type == DatabaseFieldTypes::Int64 ||
|
||||
meta->Type == DatabaseFieldTypes::Float ||
|
||||
meta->Type == DatabaseFieldTypes::Double);
|
||||
}
|
||||
|
||||
#ifdef TRINITY_DEBUG
|
||||
|
||||
void Field::LogWrongType(char const* getter) const
|
||||
{
|
||||
TC_LOG_WARN("sql.sql", "Warning: %s on %s field %s.%s (%s.%s) at index %u.",
|
||||
getter, meta.Type, meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index);
|
||||
getter, meta->TypeName, meta->TableAlias, meta->Alias, meta->TableName, meta->Name, meta->Index);
|
||||
}
|
||||
|
||||
#include "MySQLHacks.h"
|
||||
|
||||
static char const* FieldTypeToString(enum_field_types type)
|
||||
void Field::SetMetadata(QueryResultFieldMetadata const* fieldMeta)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case MYSQL_TYPE_BIT: return "BIT";
|
||||
case MYSQL_TYPE_BLOB: return "BLOB";
|
||||
case MYSQL_TYPE_DATE: return "DATE";
|
||||
case MYSQL_TYPE_DATETIME: return "DATETIME";
|
||||
case MYSQL_TYPE_NEWDECIMAL: return "NEWDECIMAL";
|
||||
case MYSQL_TYPE_DECIMAL: return "DECIMAL";
|
||||
case MYSQL_TYPE_DOUBLE: return "DOUBLE";
|
||||
case MYSQL_TYPE_ENUM: return "ENUM";
|
||||
case MYSQL_TYPE_FLOAT: return "FLOAT";
|
||||
case MYSQL_TYPE_GEOMETRY: return "GEOMETRY";
|
||||
case MYSQL_TYPE_INT24: return "INT24";
|
||||
case MYSQL_TYPE_LONG: return "LONG";
|
||||
case MYSQL_TYPE_LONGLONG: return "LONGLONG";
|
||||
case MYSQL_TYPE_LONG_BLOB: return "LONG_BLOB";
|
||||
case MYSQL_TYPE_MEDIUM_BLOB: return "MEDIUM_BLOB";
|
||||
case MYSQL_TYPE_NEWDATE: return "NEWDATE";
|
||||
case MYSQL_TYPE_NULL: return "NULL";
|
||||
case MYSQL_TYPE_SET: return "SET";
|
||||
case MYSQL_TYPE_SHORT: return "SHORT";
|
||||
case MYSQL_TYPE_STRING: return "STRING";
|
||||
case MYSQL_TYPE_TIME: return "TIME";
|
||||
case MYSQL_TYPE_TIMESTAMP: return "TIMESTAMP";
|
||||
case MYSQL_TYPE_TINY: return "TINY";
|
||||
case MYSQL_TYPE_TINY_BLOB: return "TINY_BLOB";
|
||||
case MYSQL_TYPE_VAR_STRING: return "VAR_STRING";
|
||||
case MYSQL_TYPE_YEAR: return "YEAR";
|
||||
default: return "-Unknown-";
|
||||
}
|
||||
meta = fieldMeta;
|
||||
}
|
||||
|
||||
void Field::SetMetadata(MySQLField* field, uint32 fieldIndex)
|
||||
{
|
||||
meta.TableName = field->org_table;
|
||||
meta.TableAlias = field->table;
|
||||
meta.Name = field->org_name;
|
||||
meta.Alias = field->name;
|
||||
meta.Type = FieldTypeToString(field->type);
|
||||
meta.Index = fieldIndex;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user