Core/DBLayer: Refine field type checks in Field::GetXXX methods. Should give us logs of wrong GetXXX calls for a certain type without having to dig trough valgrind logs.

This commit is contained in:
Machiavelli
2012-03-25 14:10:06 +03:00
parent 2ba4c5fa68
commit 87176c84d3

View File

@@ -41,11 +41,12 @@ class Field
return 0;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_TINY))
{
sLog->outSQLDriver("Error: GetUInt8() on non-numeric field.");
sLog->outSQLDriver("Warning: GetUInt8() on non-tinyint field.");
return 0;
}
#endif
if (data.raw)
return *reinterpret_cast<uint8*>(data.value);
@@ -58,12 +59,13 @@ class Field
return 0;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_TINY))
{
sLog->outSQLDriver("Error: GeInt8() on non-numeric field.");
sLog->outSQLDriver("Warning: GetInt8() on non-tinyint field.");
return 0;
}
#endif
if (data.raw)
return *reinterpret_cast<int8*>(data.value);
return static_cast<int8>(atol((char*)data.value));
@@ -75,12 +77,13 @@ class Field
return 0;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_SHORT) && !IsType(MYSQL_TYPE_YEAR))
{
sLog->outSQLDriver("Error: GetUInt16() on non-numeric field.");
sLog->outSQLDriver("Warning: GetUInt16() on non-smallint field.");
return 0;
}
#endif
if (data.raw)
return *reinterpret_cast<uint16*>(data.value);
return static_cast<uint16>(atol((char*)data.value));
@@ -92,12 +95,13 @@ class Field
return 0;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_SHORT) && !IsType(MYSQL_TYPE_YEAR))
{
sLog->outSQLDriver("Error: GetInt16() on non-numeric field.");
sLog->outSQLDriver("Warning: GetInt16() on non-smallint field.");
return 0;
}
#endif
if (data.raw)
return *reinterpret_cast<int16*>(data.value);
return static_cast<int16>(atol((char*)data.value));
@@ -109,12 +113,13 @@ class Field
return 0;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_INT24) && !IsType(MYSQL_TYPE_LONG))
{
sLog->outSQLDriver("Error: GetUInt32() on non-numeric field.");
sLog->outSQLDriver("Warning: GetUInt32() on non-(medium)int field.");
return 0;
}
#endif
if (data.raw)
return *reinterpret_cast<uint32*>(data.value);
return static_cast<uint32>(atol((char*)data.value));
@@ -126,12 +131,13 @@ class Field
return 0;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_INT24) && !IsType(MYSQL_TYPE_LONG))
{
sLog->outSQLDriver("Error: GetInt32() on non-numeric field.");
sLog->outSQLDriver("Warning: GetInt32() on non-(medium)int field.");
return 0;
}
#endif
if (data.raw)
return *reinterpret_cast<int32*>(data.value);
return static_cast<int32>(atol((char*)data.value));
@@ -143,12 +149,13 @@ class Field
return 0;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_LONGLONG) && !IsType(MYSQL_TYPE_BIT))
{
sLog->outSQLDriver("Error: GetUInt64() on non-numeric field.");
sLog->outSQLDriver("Warning: GetUInt64() on non-bigint field.");
return 0;
}
#endif
if (data.raw)
return *reinterpret_cast<uint64*>(data.value);
return static_cast<uint64>(atol((char*)data.value));
@@ -160,12 +167,13 @@ class Field
return 0;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_LONGLONG) && !IsType(MYSQL_TYPE_BIT))
{
sLog->outSQLDriver("Error: GetInt64() on non-numeric field.");
sLog->outSQLDriver("Warning: GetInt64() on non-bigint field.");
return 0;
}
#endif
if (data.raw)
return *reinterpret_cast<int64*>(data.value);
return static_cast<int64>(strtol((char*)data.value, NULL, 10));
@@ -177,12 +185,13 @@ class Field
return 0.0f;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_FLOAT))
{
sLog->outSQLDriver("Error: GetFloat() on non-numeric field.");
sLog->outSQLDriver("Warning: GetFloat() on non-float field.");
return 0.0f;
}
#endif
if (data.raw)
return *reinterpret_cast<float*>(data.value);
return static_cast<float>(atof((char*)data.value));
@@ -194,12 +203,13 @@ class Field
return 0.0f;
#ifdef TRINITY_DEBUG
if (!IsNumeric())
if (!IsType(MYSQL_TYPE_FLOAT))
{
sLog->outSQLDriver("Error: GetDouble() on non-numeric field.");
sLog->outSQLDriver("Warning: GetDouble() on non-double field.");
return 0.0f;
}
#endif
if (data.raw)
return *reinterpret_cast<double*>(data.value);
return static_cast<double>(atof((char*)data.value));
@@ -316,6 +326,11 @@ class Field
}
}
bool IsType(enum_field_types type) const
{
return data.type == type;
}
bool IsNumeric() const
{
return (data.type == MYSQL_TYPE_TINY ||