aboutsummaryrefslogtreecommitdiff
path: root/src/server/database/Database/Field.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/database/Database/Field.h')
-rw-r--r--src/server/database/Database/Field.h381
1 files changed, 35 insertions, 346 deletions
diff --git a/src/server/database/Database/Field.h b/src/server/database/Database/Field.h
index 8035dd16869..18cba2acd82 100644
--- a/src/server/database/Database/Field.h
+++ b/src/server/database/Database/Field.h
@@ -18,13 +18,23 @@
#ifndef _FIELD_H
#define _FIELD_H
-#include "Common.h"
-#include "Log.h"
+#include "Define.h"
+#include "DatabaseEnvFwd.h"
+#include <vector>
-#ifdef _WIN32
-#include <winsock2.h>
-#endif
-#include <mysql.h>
+enum class DatabaseFieldTypes : uint8
+{
+ Null,
+ Int8,
+ Int16,
+ Int32,
+ Int64,
+ Float,
+ Double,
+ Decimal,
+ Date,
+ Binary
+};
/**
@class Field
@@ -70,234 +80,19 @@ class TC_DATABASE_API Field
return GetUInt8() == 1 ? true : false;
}
- uint8 GetUInt8() const
- {
- if (!data.value)
- return 0;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_TINY))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetUInt8() on non-tinyint field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<uint8*>(data.value);
- return static_cast<uint8>(strtoul((char*)data.value, nullptr, 10));
- }
-
- int8 GetInt8() const
- {
- if (!data.value)
- return 0;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_TINY))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetInt8() on non-tinyint field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<int8*>(data.value);
- return static_cast<int8>(strtol((char*)data.value, NULL, 10));
- }
-
- uint16 GetUInt16() const
- {
- if (!data.value)
- return 0;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_SHORT) && !IsType(MYSQL_TYPE_YEAR))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetUInt16() on non-smallint field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<uint16*>(data.value);
- return static_cast<uint16>(strtoul((char*)data.value, nullptr, 10));
- }
-
- int16 GetInt16() const
- {
- if (!data.value)
- return 0;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_SHORT) && !IsType(MYSQL_TYPE_YEAR))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetInt16() on non-smallint field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<int16*>(data.value);
- return static_cast<int16>(strtol((char*)data.value, NULL, 10));
- }
-
- uint32 GetUInt32() const
- {
- if (!data.value)
- return 0;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_INT24) && !IsType(MYSQL_TYPE_LONG))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetUInt32() on non-(medium)int field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<uint32*>(data.value);
- return static_cast<uint32>(strtoul((char*)data.value, nullptr, 10));
- }
-
- int32 GetInt32() const
- {
- if (!data.value)
- return 0;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_INT24) && !IsType(MYSQL_TYPE_LONG))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetInt32() on non-(medium)int field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<int32*>(data.value);
- return static_cast<int32>(strtol((char*)data.value, NULL, 10));
- }
-
- uint64 GetUInt64() const
- {
- if (!data.value)
- return 0;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_LONGLONG) && !IsType(MYSQL_TYPE_BIT))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetUInt64() on non-bigint field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<uint64*>(data.value);
- return static_cast<uint64>(strtoull((char*)data.value, nullptr, 10));
- }
-
- int64 GetInt64() const
- {
- if (!data.value)
- return 0;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_LONGLONG) && !IsType(MYSQL_TYPE_BIT))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetInt64() on non-bigint field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<int64*>(data.value);
- return static_cast<int64>(strtoll((char*)data.value, NULL, 10));
- }
-
- float GetFloat() const
- {
- if (!data.value)
- return 0.0f;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_FLOAT))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetFloat() on non-float field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0.0f;
- }
- #endif
-
- if (data.raw)
- return *reinterpret_cast<float*>(data.value);
- return static_cast<float>(atof((char*)data.value));
- }
-
- double GetDouble() const
- {
- if (!data.value)
- return 0.0f;
-
- #ifdef TRINITY_DEBUG
- if (!IsType(MYSQL_TYPE_DOUBLE) && !IsType(MYSQL_TYPE_NEWDECIMAL))
- {
- TC_LOG_WARN("sql.sql", "Warning: GetDouble() on non-double/non-decimal field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return 0.0f;
- }
- #endif
-
- if (data.raw && !IsType(MYSQL_TYPE_NEWDECIMAL))
- return *reinterpret_cast<double*>(data.value);
- return static_cast<double>(atof((char*)data.value));
- }
-
- char const* GetCString() const
- {
- if (!data.value)
- return NULL;
-
- #ifdef TRINITY_DEBUG
- if (IsNumeric())
- {
- TC_LOG_WARN("sql.sql", "Error: GetCString() on numeric field %s.%s (%s.%s) at index %u. Using type: %s.",
- meta.TableAlias, meta.Alias, meta.TableName, meta.Name, meta.Index, meta.Type);
- return NULL;
- }
- #endif
- return static_cast<char const*>(data.value);
- }
-
- std::string GetString() const
- {
- if (!data.value)
- return "";
-
- char const* string = GetCString();
- if (!string)
- return "";
-
- return std::string(string, data.length);
- }
-
- std::vector<uint8> GetBinary() const
- {
- std::vector<uint8> result;
- if (!data.value || !data.length)
- return result;
-
- result.resize(data.length);
- memcpy(result.data(), data.value, data.length);
- return result;
- }
+ uint8 GetUInt8() const;
+ int8 GetInt8() const;
+ uint16 GetUInt16() const;
+ int16 GetInt16() const;
+ uint32 GetUInt32() const;
+ int32 GetInt32() const;
+ uint64 GetUInt64() const;
+ int64 GetInt64() const;
+ float GetFloat() const;
+ double GetDouble() const;
+ char const* GetCString() const;
+ std::string GetString() const;
+ std::vector<uint8> GetBinary() const;
bool IsNull() const
{
@@ -320,13 +115,13 @@ class TC_DATABASE_API Field
{
uint32 length; // Length (prepared strings only)
void* value; // Actual data in memory
- enum_field_types type; // Field type
+ DatabaseFieldTypes type; // Field type
bool raw; // Raw bytes? (Prepared statement or ad hoc)
} data;
#pragma pack(pop)
- void SetByteValue(void* newValue, enum_field_types newType, uint32 length);
- void SetStructuredValue(char* newValue, enum_field_types newType, uint32 length);
+ void SetByteValue(void* newValue, DatabaseFieldTypes newType, uint32 length);
+ void SetStructuredValue(char* newValue, DatabaseFieldTypes newType, uint32 length);
void CleanUp()
{
@@ -336,120 +131,14 @@ class TC_DATABASE_API Field
data.value = NULL;
}
- static uint32 SizeForType(MYSQL_FIELD* field)
- {
- switch (field->type)
- {
- case MYSQL_TYPE_NULL:
- return 0;
- case MYSQL_TYPE_TINY:
- return 1;
- case MYSQL_TYPE_YEAR:
- case MYSQL_TYPE_SHORT:
- return 2;
- case MYSQL_TYPE_INT24:
- case MYSQL_TYPE_LONG:
- case MYSQL_TYPE_FLOAT:
- return 4;
- case MYSQL_TYPE_DOUBLE:
- case MYSQL_TYPE_LONGLONG:
- case MYSQL_TYPE_BIT:
- return 8;
-
- case MYSQL_TYPE_TIMESTAMP:
- case MYSQL_TYPE_DATE:
- case MYSQL_TYPE_TIME:
- case MYSQL_TYPE_DATETIME:
- return sizeof(MYSQL_TIME);
-
- case MYSQL_TYPE_TINY_BLOB:
- case MYSQL_TYPE_MEDIUM_BLOB:
- case MYSQL_TYPE_LONG_BLOB:
- case MYSQL_TYPE_BLOB:
- case MYSQL_TYPE_STRING:
- case MYSQL_TYPE_VAR_STRING:
- return field->max_length + 1;
-
- case MYSQL_TYPE_DECIMAL:
- case MYSQL_TYPE_NEWDECIMAL:
- return 64;
-
- case MYSQL_TYPE_GEOMETRY:
- /*
- Following types are not sent over the wire:
- MYSQL_TYPE_ENUM:
- MYSQL_TYPE_SET:
- */
- default:
- TC_LOG_WARN("sql.sql", "SQL::SizeForType(): invalid field type %u", uint32(field->type));
- return 0;
- }
- }
-
- bool IsType(enum_field_types type) const
- {
- return data.type == type;
- }
+ bool IsType(DatabaseFieldTypes type) const;
- bool IsNumeric() const
- {
- return (data.type == MYSQL_TYPE_TINY ||
- data.type == MYSQL_TYPE_SHORT ||
- data.type == MYSQL_TYPE_INT24 ||
- data.type == MYSQL_TYPE_LONG ||
- data.type == MYSQL_TYPE_FLOAT ||
- data.type == MYSQL_TYPE_DOUBLE ||
- data.type == MYSQL_TYPE_LONGLONG );
- }
+ bool IsNumeric() const;
private:
#ifdef TRINITY_DEBUG
- static char const* FieldTypeToString(enum_field_types type)
- {
- 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-";
- }
- }
-
- void SetMetadata(MYSQL_FIELD* 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;
- }
-
+ void SetMetadata(MYSQL_FIELD* field, uint32 fieldIndex);
Metadata meta;
-
#endif
};